2023-04-20 08:15:36 +00:00
using Avalonia.Controls ;
2023-04-23 08:15:11 +00:00
using Avalonia.Input ;
2023-04-22 03:54:29 +00:00
using Avalonia.Interactivity ;
2023-04-23 08:15:11 +00:00
using Avalonia.Remote.Protocol.Input ;
using ChovySign_GUI.Popup.Global ;
2023-04-22 03:54:29 +00:00
using ChovySign_GUI.Popup.Global.KeySelector ;
using GameBuilder.Psp ;
using Ionic.Zlib ;
2023-04-23 08:15:11 +00:00
using Li.Utilities ;
2023-04-22 03:54:29 +00:00
using LibChovy.Config ;
2023-04-23 08:15:11 +00:00
using LibChovy.VersionKey ;
using SixLabors.ImageSharp.PixelFormats ;
2023-04-22 03:54:29 +00:00
using System ;
2023-04-23 08:15:11 +00:00
using System.ComponentModel ;
using System.IO ;
using System.IO.Compression ;
using System.Linq ;
2023-04-22 03:54:29 +00:00
using System.Net.Sockets ;
using System.Text ;
2023-04-23 08:15:11 +00:00
using System.Threading.Tasks ;
using Vita.ContentManager ;
using Vita.PsvImgTools ;
using static ChovySign_GUI . Popup . Global . MessageBox ;
using static PspCrypto . SceNpDrm ;
2023-04-20 08:15:36 +00:00
namespace ChovySign_GUI.Global
{
public partial class KeySelector : UserControl
{
2023-04-23 08:15:11 +00:00
2023-04-22 03:54:29 +00:00
private string licenseDataConfigKey
{
get
{
return "KEY_INDEX_" + keyIndex + "_LICENSE_DATA" ;
}
}
private string versionKeyConfigKey
{
get
{
return "KEY_INDEX_" + keyIndex + "_VERSION_KEY" ;
}
}
2023-04-23 08:15:11 +00:00
private bool lastValid ;
2023-04-22 03:54:29 +00:00
private int keyIndex = 1 ;
2023-04-23 08:15:11 +00:00
private NpDrmRif ? npRif ;
private byte [ ] ? versionKey ;
2023-04-22 03:54:29 +00:00
2023-04-23 08:15:11 +00:00
public bool IsValid
{
get
{
try
{
if ( vKey . Text is null ) return false ;
if ( vKey . Text . Length ! = 32 ) return false ;
if ( zRif . Text is null ) return false ;
if ( zRif . Text . Length < = 0 ) return false ;
byte [ ] rif = new NpDrmRif ( zRif . Text ) . Rif ;
if ( rif . Length < = 0 ) return false ;
return ( VersionKey is not null & & Rif is not null ) ;
}
catch { return false ; } ;
}
}
public byte [ ] ? VersionKey
{
get
{
return versionKey ;
}
set
{
if ( value is null ) return ;
versionKey = value ;
ChovyConfig . CurrentConfig . SetBytes ( versionKeyConfigKey , versionKey ) ;
vKey . Text = BitConverter . ToString ( versionKey ) . Replace ( "-" , "" ) ;
OnVersionKeyChanged ( new EventArgs ( ) ) ;
}
}
public byte [ ] ? Rif
2023-04-22 03:54:29 +00:00
{
2023-04-23 08:15:11 +00:00
get
{
if ( npRif is null ) return null ;
return npRif . Rif ;
}
set
{
if ( value is null ) return ;
2023-04-22 03:54:29 +00:00
2023-04-23 08:15:11 +00:00
npRif = new NpDrmRif ( value ) ;
zRif . Text = npRif . ZRif ;
ChovyConfig . CurrentConfig . SetBytes ( licenseDataConfigKey , npRif . Rif ) ;
2023-04-22 03:54:29 +00:00
2023-04-23 08:15:11 +00:00
OnRifChanged ( new EventArgs ( ) ) ;
}
}
private void reloadCfg ( )
{
byte [ ] ? rifData = ChovyConfig . CurrentConfig . GetBytes ( licenseDataConfigKey ) ;
byte [ ] ? vkeyData = ChovyConfig . CurrentConfig . GetBytes ( versionKeyConfigKey ) ;
2023-04-22 03:54:29 +00:00
2023-04-23 08:15:11 +00:00
if ( vkeyData is not null )
{
vKey . Text = BitConverter . ToString ( vkeyData ) . Replace ( "-" , "" ) ;
versionKey = vkeyData ;
}
2023-04-22 03:54:29 +00:00
2023-04-23 08:15:11 +00:00
if ( rifData is not null )
{
npRif = new NpDrmRif ( rifData ) ;
zRif . Text = npRif . ZRif ;
}
2023-04-22 03:54:29 +00:00
}
private async void getKeysClick ( object sender , RoutedEventArgs e )
{
Button ? btn = sender as Button ;
if ( btn is null ) return ;
btn . IsEnabled = false ;
Window ? currentWindow = this . VisualRoot as Window ;
if ( currentWindow is not Window ) throw new Exception ( "could not find current window" ) ;
KeyObtainMethods keyObt = new KeyObtainMethods ( ) ;
keyObt . KeyIndex = keyIndex ;
2023-04-23 08:15:11 +00:00
VersionKeyMethod ? method = await keyObt . ShowDialog < VersionKeyMethod > ( currentWindow ) ;
if ( method is null ) return ;
2023-04-22 03:54:29 +00:00
2023-04-23 08:15:11 +00:00
NpDrmInfo ? key = null ;
2023-04-22 03:54:29 +00:00
NpDrmRif ? rif = null ;
switch ( method )
{
case VersionKeyMethod . ACT_RIF_METHOD :
ActRifMethodGUI actRifMethodGUI = new ActRifMethodGUI ( ) ;
2023-04-23 08:15:11 +00:00
NpDrmInfo [ ] ? keys = await actRifMethodGUI . ShowDialog < NpDrmInfo [ ] > ( currentWindow ) ;
2023-04-22 03:54:29 +00:00
if ( keys is null ) break ;
2023-04-23 08:15:11 +00:00
2023-04-22 03:54:29 +00:00
key = keys [ keyIndex ] ;
rif = actRifMethodGUI . Rif ;
2023-04-23 08:15:11 +00:00
2023-04-22 03:54:29 +00:00
break ;
2023-04-23 08:15:11 +00:00
case VersionKeyMethod . EBOOT_PBP_METHOD :
CmaBackupPicker ebootBackupSelector = new CmaBackupPicker ( ) ;
ebootBackupSelector . BackupType = ( ( keyIndex = = 1 ) ? "PSGAME" : "PGAME" ) ;
string? gameBackupFolder = await ebootBackupSelector . ShowDialog < string > ( currentWindow ) ;
string accountId = ebootBackupSelector . AccountId ;
if ( gameBackupFolder is null ) break ;
if ( accountId = = "" ) break ;
key = CMAVersionKeyHelper . GetKeyFromGamePsvimg ( gameBackupFolder , accountId ) ;
rif = CMAVersionKeyHelper . GetRifFromLicensePsvimg ( gameBackupFolder , accountId ) ;
break ;
case VersionKeyMethod . KEYS_TXT_METHOD :
CmaBackupPicker pspLicenseBackupSelector = new CmaBackupPicker ( ) ;
pspLicenseBackupSelector . BackupType = "PGAME" ;
pspLicenseBackupSelector . Filter = KeysTxtMethod . TitleIds ;
gameBackupFolder = await pspLicenseBackupSelector . ShowDialog < string > ( currentWindow ) ;
accountId = pspLicenseBackupSelector . AccountId ;
if ( gameBackupFolder is null ) break ;
if ( accountId = = "" ) break ;
rif = CMAVersionKeyHelper . GetRifFromLicensePsvimg ( gameBackupFolder , accountId ) ;
if ( rif is null ) break ;
key = KeysTxtMethod . GetVersionKey ( rif . ContentId , this . KeyIndex ) ;
break ;
2023-04-22 03:54:29 +00:00
}
2023-04-23 08:15:11 +00:00
2023-04-22 03:54:29 +00:00
if ( key is not null )
{
2023-04-23 08:15:11 +00:00
if ( key . KeyIndex ! = this . keyIndex )
{
await MessageBox . Show ( currentWindow , "VersionKey obtained, but had keyindex: " + key . KeyIndex + " however keyindex " + this . keyIndex + " was required." , "KeyIndex mismatch!" , MessageBoxButtons . Ok ) ;
return ;
}
VersionKey = key . VersionKey ;
2023-04-22 03:54:29 +00:00
}
2023-04-23 08:15:11 +00:00
2023-04-22 03:54:29 +00:00
if ( rif is not null )
2023-04-23 08:15:11 +00:00
Rif = rif . Rif ;
2023-04-22 03:54:29 +00:00
btn . IsEnabled = true ;
}
public int KeyIndex
{
get
{
return keyIndex ;
}
set
{
keyIndex = value ;
reloadCfg ( ) ;
}
}
2023-04-23 08:15:11 +00:00
public event EventHandler < EventArgs > ? VersionKeyChanged ;
protected virtual void OnVersionKeyChanged ( EventArgs e )
{
if ( IsValid ! = lastValid ) OnValidStateChanged ( new EventArgs ( ) ) ;
if ( VersionKeyChanged is not null )
VersionKeyChanged ( this , e ) ;
}
public event EventHandler < EventArgs > ? RifChanged ;
protected virtual void OnRifChanged ( EventArgs e )
{
if ( IsValid ! = lastValid ) OnValidStateChanged ( new EventArgs ( ) ) ;
if ( RifChanged is not null )
RifChanged ( this , e ) ;
}
public event EventHandler < EventArgs > ? ValidStateChanged ;
protected virtual void OnValidStateChanged ( EventArgs e )
{
lastValid = IsValid ;
if ( ValidStateChanged is not null )
ValidStateChanged ( this , e ) ;
}
2023-04-20 08:15:36 +00:00
public KeySelector ( )
{
InitializeComponent ( ) ;
2023-04-22 03:54:29 +00:00
reloadCfg ( ) ;
2023-04-23 08:15:11 +00:00
lastValid = IsValid ;
2023-04-25 02:19:41 +00:00
zRif . TextChanged + = onZrifChanged ;
vKey . TextChanged + = onVkeyChanged ;
2023-04-23 08:15:11 +00:00
}
2023-04-25 02:19:41 +00:00
private void onVkeyChanged ( object? sender , EventArgs e )
2023-04-23 08:15:11 +00:00
{
2023-04-25 02:19:41 +00:00
FilteredTextBox ? txt = sender as FilteredTextBox ;
2023-04-23 08:15:11 +00:00
if ( txt is null ) return ;
if ( lastValid ! = IsValid )
OnValidStateChanged ( new EventArgs ( ) ) ;
try
{
if ( txt . Text is null ) return ;
if ( txt . Text . Length ! = 32 ) return ;
this . VersionKey = MathUtil . StringToByteArray ( txt . Text ) ;
}
catch { } ;
}
2023-04-25 02:19:41 +00:00
private void onZrifChanged ( object? sender , EventArgs e )
2023-04-23 08:15:11 +00:00
{
2023-04-25 02:19:41 +00:00
FilteredTextBox ? txt = sender as FilteredTextBox ;
2023-04-23 08:15:11 +00:00
if ( txt is null ) return ;
if ( lastValid ! = IsValid )
OnValidStateChanged ( new EventArgs ( ) ) ;
try
{
byte [ ] rifBytes = new NpDrmRif ( txt . Text ) . Rif ;
if ( rifBytes . Length ! = 0 ) this . Rif = rifBytes ;
}
catch { } ;
2023-04-20 08:15:36 +00:00
}
}
}