2014-06-28 21:22:05 +00:00
using System ;
2014-07-31 22:06:48 +00:00
using System.Collections ;
2014-06-28 21:22:05 +00:00
using System.Drawing ;
2014-07-31 22:06:48 +00:00
using System.IO ;
2015-12-23 15:58:43 +00:00
using System.Linq ;
2015-03-11 01:44:51 +00:00
using System.Windows.Forms ;
2014-06-28 21:22:05 +00:00
namespace PKHeX
{
2014-11-22 03:47:30 +00:00
public partial class SAV_EventFlagsXY : Form
2014-06-28 21:22:05 +00:00
{
2015-09-21 03:34:09 +00:00
public SAV_EventFlagsXY ( )
2014-06-28 21:22:05 +00:00
{
InitializeComponent ( ) ;
2015-09-21 03:34:09 +00:00
Util . TranslateInterface ( this , Main . curlanguage ) ;
2014-07-31 22:06:48 +00:00
2015-03-11 01:44:51 +00:00
AllowDrop = true ;
DragEnter + = tabMain_DragEnter ;
DragDrop + = tabMain_DragDrop ;
2014-07-31 22:06:48 +00:00
Setup ( ) ;
nud . Text = "0" ; // Prompts an update for flag 0.
2015-09-21 03:34:09 +00:00
// No Volcanic Ash in X/Y
2014-06-28 21:22:05 +00:00
}
2016-01-17 21:27:24 +00:00
private bool setup = true ;
2016-02-10 08:33:42 +00:00
private CheckBox [ ] chka ;
private readonly bool [ ] flags = new bool [ 3072 ] ;
private readonly ushort [ ] Constants = new ushort [ ( Main . SAV . EventFlag - Main . SAV . EventConst ) / 2 ] ;
2014-07-31 22:06:48 +00:00
private void B_Cancel_Click ( object sender , EventArgs e )
{
Close ( ) ;
}
private void B_Save_Click ( object sender , EventArgs e )
{
// Gather Updated Flags
2015-03-11 01:44:51 +00:00
foreach ( CheckBox flag in chka )
flags [ getFlagNum ( flag ) ] = flag . Checked ;
2014-07-31 22:06:48 +00:00
2015-02-01 04:40:35 +00:00
byte [ ] data = new byte [ flags . Length / 8 ] ;
2014-07-31 22:06:48 +00:00
for ( int i = 0 ; i < flags . Length ; i + + )
if ( flags [ i ] )
2015-02-01 04:40:35 +00:00
data [ i / 8 ] | = ( byte ) ( 1 < < i % 8 ) ;
2015-09-21 03:34:09 +00:00
2015-10-24 03:13:32 +00:00
Array . Copy ( data , 0 , Main . SAV . Data , Main . SAV . EventFlag , 0x180 ) ;
2015-09-21 03:34:09 +00:00
// No Volcanic Ash in X/Y
2015-02-01 04:40:35 +00:00
// Copy back Constants
changeConstantIndex ( null , null ) ; // Trigger Saving
for ( int i = 0 ; i < Constants . Length ; i + + )
2015-10-24 03:13:32 +00:00
Array . Copy ( BitConverter . GetBytes ( Constants [ i ] ) , 0 , Main . SAV . Data , Main . SAV . EventConst + 2 * i , 2 ) ;
2015-02-01 04:40:35 +00:00
2015-03-11 01:44:51 +00:00
Close ( ) ;
2014-07-31 22:06:48 +00:00
}
private void Setup ( )
{
// Fill Bit arrays
2015-02-01 04:40:35 +00:00
2015-03-11 01:44:51 +00:00
chka = new [ ] {
2014-07-31 22:06:48 +00:00
flag_0001 , flag_0002 , flag_0003 , flag_0004 , flag_0005 ,
flag_2237 , flag_2238 , flag_2239 ,
flag_0115 , flag_0963 , // Mewtwo
2014-08-01 05:36:42 +00:00
flag_0114 , flag_0790 , // Zygarde
2014-08-04 23:22:47 +00:00
flag_0285 , flag_0286 , flag_0287 , flag_0288 , flag_0289 , // Statuettes
flag_0290 , flag_0291 , flag_0292 , flag_0293 , flag_0294 , // Super Unlocks
2014-08-05 01:48:22 +00:00
flag_0675 , // Chatelaine 50
2014-08-10 03:55:52 +00:00
flag_2546 , // Pokedex
2014-07-31 22:06:48 +00:00
} ;
2014-12-12 05:45:01 +00:00
byte [ ] data = new byte [ 0x180 ] ;
2015-10-24 03:13:32 +00:00
Array . Copy ( Main . SAV . Data , Main . SAV . EventFlag , data , 0 , 0x180 ) ;
2014-07-31 22:06:48 +00:00
BitArray BitRegion = new BitArray ( data ) ;
BitRegion . CopyTo ( flags , 0 ) ;
2014-06-28 21:22:05 +00:00
2015-02-01 04:40:35 +00:00
// Setup Event Constant Editor
CB_Stats . Items . Clear ( ) ;
2015-09-21 03:34:09 +00:00
for ( int i = 0 ; i < Constants . Length ; i + = 2 )
2015-02-01 04:40:35 +00:00
{
2016-01-17 21:27:24 +00:00
CB_Stats . Items . Add ( $"0x{i.ToString(" X3 ")}" ) ;
2015-10-24 03:13:32 +00:00
Constants [ i / 2 ] = BitConverter . ToUInt16 ( Main . SAV . Data , Main . SAV . EventConst + i ) ;
2015-02-01 04:40:35 +00:00
}
CB_Stats . SelectedIndex = 0 ;
2014-07-31 22:06:48 +00:00
// Populate Flags
setup = true ;
popFlags ( ) ;
}
private void popFlags ( )
{
2015-03-11 01:44:51 +00:00
if ( ! setup ) return ;
foreach ( CheckBox flag in chka )
flag . Checked = flags [ getFlagNum ( flag ) ] ;
2014-07-31 22:06:48 +00:00
2015-03-11 01:44:51 +00:00
changeCustomFlag ( null , null ) ;
2014-07-31 22:06:48 +00:00
}
2014-08-05 01:48:22 +00:00
2014-07-31 22:06:48 +00:00
private int getFlagNum ( CheckBox chk )
{
try
{
string source = chk . Name ;
return Convert . ToInt32 ( source . Substring ( Math . Max ( 0 , source . Length - 4 ) ) ) ;
}
catch { return 0 ; }
}
private void changeCustomBool ( object sender , EventArgs e )
{
flags [ ( int ) nud . Value ] = CHK_CustomFlag . Checked ;
popFlags ( ) ;
}
private void changeCustomFlag ( object sender , EventArgs e )
2014-06-28 21:22:05 +00:00
{
int flag = ( int ) nud . Value ;
2014-07-31 22:06:48 +00:00
if ( flag > = 3072 )
2014-06-28 21:22:05 +00:00
{
CHK_CustomFlag . Checked = false ;
2014-07-31 22:06:48 +00:00
CHK_CustomFlag . Enabled = false ;
2014-06-28 21:22:05 +00:00
nud . BackColor = Color . Red ;
}
else
{
2014-07-31 22:06:48 +00:00
CHK_CustomFlag . Enabled = true ;
2015-09-21 03:34:09 +00:00
nud . BackColor = Main . defaultControlWhite ;
2014-07-31 22:06:48 +00:00
CHK_CustomFlag . Checked = flags [ flag ] ;
2014-06-28 21:22:05 +00:00
}
}
2014-08-05 01:48:22 +00:00
private void changeCustomFlag ( object sender , KeyEventArgs e )
{
changeCustomFlag ( null , ( EventArgs ) e ) ;
}
2014-07-31 22:06:48 +00:00
private void toggleFlag ( object sender , EventArgs e )
2014-06-28 21:22:05 +00:00
{
2016-01-18 01:07:19 +00:00
flags [ getFlagNum ( ( CheckBox ) sender ) ] = ( ( CheckBox ) sender ) . Checked ;
2014-07-31 22:06:48 +00:00
changeCustomFlag ( sender , e ) ;
2014-06-28 21:22:05 +00:00
}
2014-07-31 22:06:48 +00:00
private void changeSAV ( object sender , EventArgs e )
{
if ( TB_NewSAV . Text . Length > 0 & & TB_OldSAV . Text . Length > 0 )
2014-06-28 21:22:05 +00:00
{
2014-07-31 22:06:48 +00:00
diffSaves ( ) ;
2014-06-28 21:22:05 +00:00
}
2014-07-31 22:06:48 +00:00
}
private void diffSaves ( )
{
BitArray oldBits = new BitArray ( olddata ) ;
BitArray newBits = new BitArray ( newdata ) ;
2014-06-28 21:22:05 +00:00
2015-02-06 03:17:47 +00:00
string tbIsSet = "" ;
string tbUnSet = "" ;
2014-07-31 22:06:48 +00:00
for ( int i = 0 ; i < oldBits . Length ; i + + )
{
2015-03-12 04:44:12 +00:00
if ( oldBits [ i ] = = newBits [ i ] ) continue ;
if ( newBits [ i ] )
2016-01-18 01:07:19 +00:00
tbIsSet + = i . ToString ( "0000" ) + "," ;
2015-03-12 04:44:12 +00:00
else
2016-01-18 01:07:19 +00:00
tbUnSet + = i . ToString ( "0000" ) + "," ;
2014-07-31 22:06:48 +00:00
}
2015-02-06 03:17:47 +00:00
TB_IsSet . Text = tbIsSet ;
TB_UnSet . Text = tbUnSet ;
2014-06-28 21:22:05 +00:00
}
2016-01-17 21:27:24 +00:00
private readonly byte [ ] olddata = new byte [ 0x180 ] ;
private readonly byte [ ] newdata = new byte [ 0x180 ] ;
2014-07-31 22:06:48 +00:00
private void openSAV ( object sender , EventArgs e )
2014-06-28 21:22:05 +00:00
{
2014-07-31 22:06:48 +00:00
OpenFileDialog ofd = new OpenFileDialog ( ) ;
if ( ofd . ShowDialog ( ) = = DialogResult . OK )
2015-02-06 03:17:47 +00:00
loadSAV ( sender , ofd . FileName ) ;
}
private void loadSAV ( object sender , string path )
{
FileInfo fi = new FileInfo ( path ) ;
2015-12-23 15:58:43 +00:00
byte [ ] eventflags ;
if ( fi . Length = = SAV6 . SIZE_XY )
eventflags = File . ReadAllBytes ( path ) . Skip ( Main . SAV . EventFlag ) . Take ( 0x180 ) . ToArray ( ) ;
else
2015-03-11 01:44:51 +00:00
{
2016-01-28 04:24:32 +00:00
Util . Error ( "Invalid SAV Size" , string . Format ( "File Size: 0x{1} ({0} bytes)" , fi . Length , fi . Length . ToString ( "X5" ) ) , "File Loaded: " + path ) ;
2015-12-23 15:58:43 +00:00
return ;
2015-03-11 01:44:51 +00:00
}
2015-02-01 04:40:35 +00:00
2014-07-31 22:06:48 +00:00
Button bs = ( Button ) sender ;
if ( bs . Name = = "B_LoadOld" )
2015-02-06 03:17:47 +00:00
{ Array . Copy ( eventflags , olddata , 0x180 ) ; TB_OldSAV . Text = path ; }
2015-02-01 04:40:35 +00:00
else
2015-02-06 03:17:47 +00:00
{ Array . Copy ( eventflags , newdata , 0x180 ) ; TB_NewSAV . Text = path ; }
2014-06-28 21:22:05 +00:00
}
2015-02-01 04:40:35 +00:00
int entry = - 1 ;
private void changeConstantIndex ( object sender , EventArgs e )
{
if ( entry > - 1 ) // Set Entry
2016-01-18 01:07:19 +00:00
Constants [ entry ] = ( ushort ) Math . Min ( Util . ToUInt32 ( MT_Stat . Text ) , 0xFFFF ) ;
2015-02-01 04:40:35 +00:00
entry = CB_Stats . SelectedIndex ; // Get Entry
MT_Stat . Text = Constants [ entry ] . ToString ( ) ;
}
2015-02-06 03:17:47 +00:00
// Drag & Drop Events
private void tabMain_DragEnter ( object sender , DragEventArgs e )
{
if ( e . Data . GetDataPresent ( DataFormats . FileDrop ) ) e . Effect = DragDropEffects . Copy ;
}
private void tabMain_DragDrop ( object sender , DragEventArgs e )
{
string [ ] files = ( string [ ] ) e . Data . GetData ( DataFormats . FileDrop ) ;
2016-01-18 01:07:19 +00:00
loadSAV ( Util . Prompt ( MessageBoxButtons . YesNo , "FlagDiff Researcher:" , "Yes: Old Save" + Environment . NewLine + "No: New Save" ) = = DialogResult . Yes ? B_LoadOld : B_LoadNew , files [ 0 ] ) ;
2015-02-06 03:17:47 +00:00
}
2014-06-28 21:22:05 +00:00
}
2015-02-01 04:40:35 +00:00
}