2019-12-13 18:25:26 +00:00
using System ;
using System.Collections.Generic ;
2019-12-14 06:31:00 +00:00
using System.Diagnostics ;
2019-12-13 18:25:26 +00:00
using System.Drawing ;
using System.IO ;
using System.Linq ;
using System.Threading ;
using System.Windows.Forms ;
using static System . Windows . Forms . ListBox ;
namespace RMDEC
{
2019-12-15 04:50:00 +00:00
public partial class mvProjectToolset : Form
2019-12-13 18:25:26 +00:00
{
public MVProject mvProject ;
2019-12-15 04:50:00 +00:00
public mvProjectToolset ( MVProject proj )
2019-12-13 18:25:26 +00:00
{
mvProject = proj ;
InitializeComponent ( ) ;
}
2019-12-14 06:31:00 +00:00
internal long _encryptedImageCount = 0 ;
internal long _encryptedMusicCount = 0 ;
private long unencryptedImageCount = 0 ;
private long unencryptedMusicCount = 0 ;
2019-12-13 18:25:26 +00:00
private List < string > encFileList = new List < string > ( ) ;
private List < string > decFileList = new List < string > ( ) ;
2019-12-14 06:31:00 +00:00
private string [ ] blacklistedFiles = { Path . Combine ( "icon" , "icon.png" ) , Path . Combine ( "img" , "system" , "Window.png" ) , Path . Combine ( "img" , "system" , "Loading.png" ) } ;
2019-12-13 21:11:54 +00:00
2019-12-13 18:25:26 +00:00
private bool encryptedIndexingComplete = false ;
private bool unencryptedIndexingComplete = false ;
2019-12-14 06:31:00 +00:00
private long encryptedImageCount {
get
{
return _encryptedImageCount ;
}
set
{
_encryptedImageCount = value ;
bool curent = mvProject . EncryptedImages ;
if ( value < = 0 )
{
if ( curent ! = false )
{
mvProject . EncryptedImages = false ;
}
}
else
{
if ( curent ! = true )
{
mvProject . EncryptedImages = true ;
}
}
updateTitle ( ) ;
}
}
private long encryptedMusicCount
{
get
{
return _encryptedMusicCount ;
}
set
{
_encryptedMusicCount = value ;
bool curent = mvProject . EncryptedAudio ;
if ( value < = 0 )
{
if ( curent ! = false )
{
mvProject . EncryptedAudio = false ;
}
}
else
{
if ( curent ! = true )
{
mvProject . EncryptedAudio = true ;
}
}
updateTitle ( ) ;
}
}
2019-12-13 18:25:26 +00:00
private void onIndexThreadComplete ( )
{
if ( encryptedIndexingComplete & & unencryptedIndexingComplete )
{
processingText . Text = "Waiting" ;
processingText . BackColor = Color . Red ;
decryptSelected . Enabled = true ;
encryptSelected . Enabled = true ;
makeMV . Enabled = true ;
unmakeMV . Enabled = true ;
processingBar . Style = ProgressBarStyle . Continuous ;
this . Cursor = Cursors . Arrow ;
}
}
private void indexFiles ( )
{
processingText . Text = "Indexing" ;
processingText . BackColor = Color . Yellow ;
processingBar . Style = ProgressBarStyle . Marquee ;
this . Cursor = Cursors . WaitCursor ;
//Index Encrypted Files
Thread indexEncryptedFilesThread = new Thread ( ( ) = >
{
List < string > pngList = new List < string > ( ) ;
List < string > oggList = new List < string > ( ) ;
2019-12-13 21:11:54 +00:00
List < string > m4aList = new List < string > ( ) ;
2019-12-13 18:25:26 +00:00
foreach ( string file in Directory . EnumerateFiles ( mvProject . FilePath , "*" , SearchOption . AllDirectories ) )
{
2019-12-13 21:11:54 +00:00
string fileExtension = Path . GetExtension ( file ) . ToLower ( ) ;
2019-12-13 18:25:26 +00:00
switch ( fileExtension )
{
case ".rpgmvp" :
pngList . Add ( file ) ;
break ;
case ".rpgmvo" :
oggList . Add ( file ) ;
break ;
case ".rpgmvm" :
2019-12-13 21:11:54 +00:00
m4aList . Add ( file ) ;
2019-12-13 18:25:26 +00:00
break ;
}
}
2019-12-14 06:31:00 +00:00
2019-12-13 18:25:26 +00:00
//add PNG
foreach ( string png in pngList )
{
string relativeName = png . Remove ( 0 , mvProject . FilePath . Length + 1 ) ;
Invoke ( ( Action ) delegate
{
encryptedFileList . Items . Add ( "[PNG] " + relativeName ) ;
} ) ;
encFileList . Add ( png ) ;
}
//add OGG
foreach ( string ogg in oggList )
{
string relativeName = ogg . Remove ( 0 , mvProject . FilePath . Length + 1 ) ;
Invoke ( ( Action ) delegate
{
encryptedFileList . Items . Add ( "[OGG] " + relativeName ) ;
} ) ;
encFileList . Add ( ogg ) ;
}
//add M4A
2019-12-13 21:11:54 +00:00
foreach ( string m4a in m4aList )
2019-12-13 18:25:26 +00:00
{
2019-12-13 21:11:54 +00:00
string relativeName = m4a . Remove ( 0 , mvProject . FilePath . Length + 1 ) ;
2019-12-13 18:25:26 +00:00
Invoke ( ( Action ) delegate
{
encryptedFileList . Items . Add ( "[M4A] " + relativeName ) ;
} ) ;
2019-12-13 21:11:54 +00:00
encFileList . Add ( m4a ) ;
2019-12-13 18:25:26 +00:00
}
Invoke ( ( Action ) delegate
{
2019-12-14 06:31:00 +00:00
encryptedImageCount = pngList . Count ;
encryptedMusicCount = oggList . Count + m4aList . Count ;
2019-12-13 18:25:26 +00:00
encryptedIndexingComplete = true ;
onIndexThreadComplete ( ) ;
} ) ;
} ) ;
indexEncryptedFilesThread . Start ( ) ;
//Index Unencrypted Files
Thread indexUnencryptedFilesThread = new Thread ( ( ) = >
{
List < string > pngList = new List < string > ( ) ;
List < string > oggList = new List < string > ( ) ;
2019-12-13 21:11:54 +00:00
List < string > m4aList = new List < string > ( ) ;
2019-12-13 18:25:26 +00:00
foreach ( string file in Directory . EnumerateFiles ( mvProject . FilePath , "*" , SearchOption . AllDirectories ) )
{
2019-12-13 21:11:54 +00:00
string relativeName = file . Remove ( 0 , mvProject . FilePath . Length + 1 ) ;
string fileExtension = Path . GetExtension ( file ) . ToLower ( ) ;
if ( blacklistedFiles . Contains ( relativeName ) )
{
continue ;
}
2019-12-13 18:25:26 +00:00
switch ( fileExtension )
{
case ".png" :
pngList . Add ( file ) ;
break ;
case ".ogg" :
oggList . Add ( file ) ;
break ;
2019-12-13 21:11:54 +00:00
case ".m4a" :
m4aList . Add ( file ) ;
2019-12-13 18:25:26 +00:00
break ;
}
}
//add PNG
foreach ( string png in pngList )
{
string relativeName = png . Remove ( 0 , mvProject . FilePath . Length + 1 ) ;
2019-12-13 21:11:54 +00:00
2019-12-13 18:25:26 +00:00
Invoke ( ( Action ) delegate
{
decryptedFileList . Items . Add ( "[PNG] " + relativeName ) ;
} ) ;
decFileList . Add ( png ) ;
}
//add OGG
foreach ( string ogg in oggList )
{
string relativeName = ogg . Remove ( 0 , mvProject . FilePath . Length + 1 ) ;
2019-12-13 21:11:54 +00:00
2019-12-13 18:25:26 +00:00
Invoke ( ( Action ) delegate
{
decryptedFileList . Items . Add ( "[OGG] " + relativeName ) ;
} ) ;
decFileList . Add ( ogg ) ;
}
//add M4A
2019-12-13 21:11:54 +00:00
foreach ( string m4a in m4aList )
2019-12-13 18:25:26 +00:00
{
2019-12-13 21:11:54 +00:00
string relativeName = m4a . Remove ( 0 , mvProject . FilePath . Length + 1 ) ;
2019-12-13 18:25:26 +00:00
Invoke ( ( Action ) delegate
{
decryptedFileList . Items . Add ( "[M4A] " + relativeName ) ;
} ) ;
2019-12-13 21:11:54 +00:00
decFileList . Add ( m4a ) ;
2019-12-13 18:25:26 +00:00
}
Invoke ( ( Action ) delegate
{
2019-12-14 06:31:00 +00:00
unencryptedImageCount = pngList . Count ;
unencryptedMusicCount = oggList . Count + m4aList . Count ;
2019-12-13 18:25:26 +00:00
unencryptedIndexingComplete = true ;
onIndexThreadComplete ( ) ;
} ) ;
} ) ;
indexUnencryptedFilesThread . Start ( ) ;
}
private void MVProjectToolset_Load ( object sender , EventArgs e )
{
2019-12-14 06:31:00 +00:00
updateTitle ( ) ;
2019-12-13 18:25:26 +00:00
indexFiles ( ) ;
}
2019-12-14 06:31:00 +00:00
private void updateTitle ( )
{
this . Text = "RMDec - [RMMV] " + mvProject . GameTitle + " - " + mvProject . FilePath + " - encryptedImages: " + mvProject . EncryptedImages . ToString ( ) + " - encrypedAudio: " + mvProject . EncryptedAudio . ToString ( ) ;
}
private string encodeFileExtension ( string fileName )
{
string extension = Path . GetExtension ( fileName ) . ToLower ( ) ;
string newExtension = "" ;
switch ( extension )
{
case ".png" :
newExtension = ".rpgmvp" ;
Invoke ( ( Action ) delegate
{
encryptedImageCount + = 1 ;
unencryptedImageCount - = 1 ;
} ) ;
break ;
case ".ogg" :
newExtension = ".rpgmvo" ;
Invoke ( ( Action ) delegate
{
encryptedMusicCount + = 1 ;
unencryptedMusicCount - = 1 ;
} ) ;
break ;
case ".m4a" :
newExtension = ".rpgmvm" ;
Invoke ( ( Action ) delegate
{
encryptedMusicCount + = 1 ;
unencryptedMusicCount - = 1 ;
} ) ;
break ;
}
return newExtension ;
}
2019-12-13 18:25:26 +00:00
private void encryptSelected_Click ( object sender , EventArgs e )
{
2019-12-13 21:11:54 +00:00
SelectedIndexCollection itemList = decryptedFileList . SelectedIndices ;
int itemCount = itemList . Count ;
if ( itemCount < 1 )
{
return ;
}
int item = 0 ;
processingBar . Style = ProgressBarStyle . Continuous ;
processingBar . Maximum = itemCount ;
processingText . BackColor = Color . Yellow ;
encryptSelected . Enabled = false ;
decryptSelected . Enabled = false ;
Thread decryptFilesThread = new Thread ( ( ) = >
{
int i = 1 ;
int total = itemCount ;
do
{
Invoke ( ( Action ) delegate
{
itemCount = itemList . Count ;
item = itemList [ 0 ] ;
processingText . Text = "Encrypting " + i . ToString ( ) + "/" + total . ToString ( ) ;
} ) ;
2019-12-13 18:25:26 +00:00
2019-12-13 21:11:54 +00:00
string decryptedFile = decFileList [ item ] ;
2019-12-14 06:31:00 +00:00
string newExtension = encodeFileExtension ( decryptedFile ) ;
2019-12-13 21:11:54 +00:00
string encryptedFile = Path . ChangeExtension ( decryptedFile , newExtension ) ;
FileStream Decrypted = File . OpenRead ( decryptedFile ) ;
FileStream Encrypted = File . OpenWrite ( encryptedFile ) ;
mvProject . EncryptFile ( Decrypted , Encrypted ) ;
Encrypted . Close ( ) ;
Decrypted . Close ( ) ;
File . Delete ( decryptedFile ) ;
Invoke ( ( Action ) delegate
{
string entry = decryptedFileList . Items [ item ] . ToString ( ) ;
string encryptedEntry = Path . ChangeExtension ( entry , newExtension ) ;
encryptedFileList . Items . Add ( encryptedEntry ) ;
encFileList . Add ( encryptedFile ) ;
decryptedFileList . Items . RemoveAt ( item ) ;
decFileList . RemoveAt ( item ) ;
processingBar . Value = i ;
} ) ;
i + + ;
} while ( itemCount > 1 ) ;
Invoke ( ( Action ) delegate
{
encryptSelected . Enabled = true ;
decryptSelected . Enabled = true ;
processingText . BackColor = Color . Green ;
processingText . Text = "Encrypted " + total . ToString ( ) + " files!" ;
} ) ;
} ) ;
decryptFilesThread . Start ( ) ;
2019-12-13 18:25:26 +00:00
}
2019-12-14 06:31:00 +00:00
private string decodeFileExtension ( string fileName )
{
string extension = Path . GetExtension ( fileName ) . ToLower ( ) ;
string newExtension = "" ;
switch ( extension )
{
case ".rpgmvp" :
newExtension = ".png" ;
Invoke ( ( Action ) delegate
{
encryptedImageCount - = 1 ;
unencryptedImageCount + = 1 ;
} ) ;
break ;
case ".rpgmvo" :
newExtension = "ogg" ;
Invoke ( ( Action ) delegate
{
encryptedMusicCount - = 1 ;
unencryptedMusicCount + = 1 ;
} ) ;
break ;
case ".rpgmvm" :
newExtension = ".m4a" ;
Invoke ( ( Action ) delegate
{
encryptedMusicCount - = 1 ;
unencryptedMusicCount + = 1 ;
} ) ;
break ;
}
return newExtension ;
}
2019-12-13 18:25:26 +00:00
private void decryptSelected_Click ( object sender , EventArgs e )
{
SelectedIndexCollection itemList = encryptedFileList . SelectedIndices ;
int itemCount = itemList . Count ;
if ( itemCount < 1 )
{
return ;
}
int item = 0 ;
processingBar . Style = ProgressBarStyle . Continuous ;
processingBar . Maximum = itemCount ;
processingText . BackColor = Color . Yellow ;
encryptSelected . Enabled = false ;
decryptSelected . Enabled = false ;
2019-12-14 06:31:00 +00:00
makeMV . Enabled = false ;
unmakeMV . Enabled = false ;
2019-12-13 18:25:26 +00:00
Thread decryptFilesThread = new Thread ( ( ) = >
{
int i = 1 ;
int total = itemCount ;
do
{
Invoke ( ( Action ) delegate
{
itemCount = itemList . Count ;
item = itemList [ 0 ] ;
processingText . Text = "Decrypting " + i . ToString ( ) + "/" + total . ToString ( ) ;
} ) ;
string encryptedFile = encFileList [ item ] ;
2019-12-14 06:31:00 +00:00
string newExtension = decodeFileExtension ( encryptedFile ) ;
2019-12-13 18:25:26 +00:00
string decryptedFile = Path . ChangeExtension ( encryptedFile , newExtension ) ;
FileStream Encrypted = File . OpenRead ( encryptedFile ) ;
FileStream Decrypted = File . OpenWrite ( decryptedFile ) ;
mvProject . DecryptFile ( Encrypted , Decrypted ) ;
Encrypted . Close ( ) ;
Decrypted . Close ( ) ;
File . Delete ( encryptedFile ) ;
Invoke ( ( Action ) delegate
{
string entry = encryptedFileList . Items [ item ] . ToString ( ) ;
string decryptedEntry = Path . ChangeExtension ( entry , newExtension ) ;
decryptedFileList . Items . Add ( decryptedEntry ) ;
decFileList . Add ( decryptedFile ) ;
2019-12-13 21:11:54 +00:00
encryptedFileList . Items . RemoveAt ( item ) ;
2019-12-13 18:25:26 +00:00
encFileList . RemoveAt ( item ) ;
processingBar . Value = i ;
} ) ;
i + + ;
} while ( itemCount > 1 ) ;
Invoke ( ( Action ) delegate
{
encryptSelected . Enabled = true ;
decryptSelected . Enabled = true ;
2019-12-14 06:31:00 +00:00
makeMV . Enabled = true ;
unmakeMV . Enabled = true ;
2019-12-13 18:25:26 +00:00
processingText . BackColor = Color . Green ;
processingText . Text = "Decrypted " + total . ToString ( ) + " files!" ;
} ) ;
} ) ;
2019-12-13 21:11:54 +00:00
decryptFilesThread . Start ( ) ;
2019-12-13 18:25:26 +00:00
}
2019-12-14 06:31:00 +00:00
private void onMakeMvComplete ( )
{
string rpgProject = Path . Combine ( mvProject . FilePath , "Game.rpgproject" ) ;
File . WriteAllText ( rpgProject , "RPGMV 1.0.0" ) ;
processingText . BackColor = Color . Green ;
processingText . Text = "Game Un-Deployed!" ;
encryptSelected . Enabled = true ;
decryptSelected . Enabled = true ;
makeMV . Enabled = true ;
unmakeMV . Enabled = true ;
2019-12-15 06:47:58 +00:00
Process . Start ( mvProject . FilePath + "\\" ) ;
2019-12-14 06:31:00 +00:00
}
private void makeMV_Click ( object sender , EventArgs e )
{
int itemCount = encFileList . Count ;
//Decrypt Everything
if ( itemCount > 0 )
{
processingBar . Style = ProgressBarStyle . Continuous ;
processingBar . Maximum = itemCount ;
processingText . BackColor = Color . Yellow ;
encryptSelected . Enabled = false ;
decryptSelected . Enabled = false ;
makeMV . Enabled = false ;
unmakeMV . Enabled = false ;
Thread decryptFilesThread = new Thread ( ( ) = >
{
for ( int i = 0 ; i < itemCount ; i + + )
{
Invoke ( ( Action ) delegate
{
processingText . Text = "Decrypting " + i . ToString ( ) + "/" + itemCount . ToString ( ) ;
} ) ;
string encryptedFile = encFileList [ 0 ] ;
string newExtension = decodeFileExtension ( encryptedFile ) ;
string decryptedFile = Path . ChangeExtension ( encryptedFile , newExtension ) ;
FileStream Encrypted = File . OpenRead ( encryptedFile ) ;
FileStream Decrypted = File . OpenWrite ( decryptedFile ) ;
mvProject . DecryptFile ( Encrypted , Decrypted ) ;
Encrypted . Close ( ) ;
Decrypted . Close ( ) ;
File . Delete ( encryptedFile ) ;
Invoke ( ( Action ) delegate
{
string entry = encryptedFileList . Items [ 0 ] . ToString ( ) ;
string decryptedEntry = Path . ChangeExtension ( entry , newExtension ) ;
decryptedFileList . Items . Add ( decryptedEntry ) ;
decFileList . Add ( decryptedFile ) ;
encryptedFileList . Items . RemoveAt ( 0 ) ;
encFileList . RemoveAt ( 0 ) ;
processingBar . Value = i ;
} ) ;
}
Invoke ( ( Action ) delegate
{
onMakeMvComplete ( ) ;
} ) ;
} ) ;
decryptFilesThread . Start ( ) ;
}
else
{
onMakeMvComplete ( ) ;
}
}
private void onUnMakeMvComplete ( )
{
string rpgProject = Path . Combine ( mvProject . FilePath , "Game.rpgproject" ) ;
2019-12-14 21:10:00 +00:00
if ( File . Exists ( rpgProject ) )
{
File . Delete ( rpgProject ) ;
}
2019-12-14 06:31:00 +00:00
processingText . BackColor = Color . Green ;
processingText . Text = "Game Deployed!" ;
encryptSelected . Enabled = true ;
decryptSelected . Enabled = true ;
makeMV . Enabled = true ;
unmakeMV . Enabled = true ;
}
private void unmakeMV_Click ( object sender , EventArgs e )
{
int itemCount = decFileList . Count ;
//Encrypt Everything
if ( itemCount > 0 )
{
processingBar . Style = ProgressBarStyle . Continuous ;
processingBar . Maximum = itemCount ;
processingText . BackColor = Color . Yellow ;
encryptSelected . Enabled = false ;
decryptSelected . Enabled = false ;
makeMV . Enabled = false ;
unmakeMV . Enabled = false ;
Thread encryptFilesThread = new Thread ( ( ) = >
{
for ( int i = 0 ; i < itemCount ; i + + )
{
Invoke ( ( Action ) delegate
{
processingText . Text = "Encrypting " + i . ToString ( ) + "/" + itemCount . ToString ( ) ;
} ) ;
string decryptedFile = decFileList [ 0 ] ;
string newExtension = encodeFileExtension ( decryptedFile ) ;
string encryptedFile = Path . ChangeExtension ( decryptedFile , newExtension ) ;
FileStream Encrypted = File . OpenWrite ( encryptedFile ) ;
FileStream Decrypted = File . OpenRead ( decryptedFile ) ;
mvProject . EncryptFile ( Decrypted , Encrypted ) ;
Encrypted . Close ( ) ;
Decrypted . Close ( ) ;
File . Delete ( decryptedFile ) ;
Invoke ( ( Action ) delegate
{
string entry = decryptedFileList . Items [ 0 ] . ToString ( ) ;
string encryptedEntry = Path . ChangeExtension ( entry , newExtension ) ;
encryptedFileList . Items . Add ( encryptedEntry ) ;
encFileList . Add ( encryptedFile ) ;
decryptedFileList . Items . RemoveAt ( 0 ) ;
decFileList . RemoveAt ( 0 ) ;
processingBar . Value = i ;
} ) ;
}
Invoke ( ( Action ) delegate
{
onUnMakeMvComplete ( ) ;
} ) ;
} ) ;
encryptFilesThread . Start ( ) ;
}
else
{
onUnMakeMvComplete ( ) ;
}
}
private void MVProjectToolset_FormClosing ( object sender , FormClosingEventArgs e )
{
if ( mvProject . EncryptedAudio = = true & & unencryptedMusicCount > 0 )
{
DialogResult res = MessageBox . Show ( "Warning: You have mixed unencrypted/encrypted Audio files!\n\nMV Requires ALL Decrypted or ALL Encrypted\nThe game WILL CRASH when attemping to load these files\nPlease Encrypt ALL Audio Files or Decrypt ALL Audio Files." , "Mixed Warning" , MessageBoxButtons . OKCancel , MessageBoxIcon . Warning ) ;
if ( res = = DialogResult . OK )
{
e . Cancel = true ;
}
}
if ( mvProject . EncryptedImages = = true & & unencryptedImageCount > 0 )
{
DialogResult res = MessageBox . Show ( "Warning: You have mixed unencrypted/encrypted Image files!\n\nMV Requires ALL Decrypted or ALL Encrypted\nThe game WILL CRASH when attemping to load these files\nPlease Encrypt ALL Images or Decrypt ALL Images." , "Mixed Warning" , MessageBoxButtons . OKCancel , MessageBoxIcon . Warning ) ;
if ( res = = DialogResult . OK )
{
e . Cancel = true ;
}
}
}
2019-12-13 18:25:26 +00:00
2019-12-14 06:31:00 +00:00
2019-12-13 18:25:26 +00:00
}
}