Refactoring & fixes

Fix blank sav creation for gens 1-3 (data vs Data references, version
overrides)
Fix main form init with gen2 first (check for altform usage like all
other gens)
Use GameVersion specific blank fetch instead of generation ID
This commit is contained in:
Kurt 2017-01-14 16:43:16 -08:00
parent 12c46d18a8
commit 3f218968c5
6 changed files with 100 additions and 76 deletions

View file

@ -47,9 +47,8 @@ namespace PKHeX.WinForms
catch { }
}).Start();
setPKMFormatMode(SAV.Generation, SAV.Version);
CB_ExtraBytes.SelectedIndex = 0;
getFieldsfromPKM = populateFieldsPK7;
getPKMfromFields = preparePK7;
// Set up form properties and arrays.
SlotPictureBoxes = new[] {
@ -203,7 +202,7 @@ namespace PKHeX.WinForms
}
#region Important Variables
public static SaveFile SAV = SaveUtil.getBlankSAV(7, "PKHeX");
public static SaveFile SAV = SaveUtil.getBlankSAV(GameVersion.SN, "PKHeX");
public static PKM pkm = SAV.BlankPKM; // Tab Pokemon Data Storage
private LegalityAnalysis Legality = new LegalityAnalysis(pkm);
@ -1151,57 +1150,7 @@ namespace PKHeX.WinForms
switch (SAV.Generation)
{
case 1:
getFieldsfromPKM = populateFieldsPK1;
getPKMfromFields = preparePK1;
extraBytes = new byte[] {};
break;
case 2:
getFieldsfromPKM = populateFieldsPK2;
getPKMfromFields = preparePK2;
extraBytes = new byte[] { };
break;
case 3:
if (SAV.Version == GameVersion.COLO)
{
getFieldsfromPKM = populateFieldsCK3;
getPKMfromFields = prepareCK3;
extraBytes = CK3.ExtraBytes;
break;
}
if (SAV.Version == GameVersion.XD)
{
getFieldsfromPKM = populateFieldsXK3;
getPKMfromFields = prepareXK3;
extraBytes = XK3.ExtraBytes;
break;
}
getFieldsfromPKM = populateFieldsPK3;
getPKMfromFields = preparePK3;
extraBytes = PK3.ExtraBytes;
break;
case 4:
if (SAV.Version == GameVersion.BATREV)
{
getFieldsfromPKM = populateFieldsBK4;
getPKMfromFields = prepareBK4;
}
else
{
getFieldsfromPKM = populateFieldsPK4;
getPKMfromFields = preparePK4;
}
extraBytes = PK4.ExtraBytes;
break;
case 5:
getFieldsfromPKM = populateFieldsPK5;
getPKMfromFields = preparePK5;
extraBytes = PK5.ExtraBytes;
break;
case 6:
getFieldsfromPKM = populateFieldsPK6;
getPKMfromFields = preparePK6;
extraBytes = PK6.ExtraBytes;
TB_GameSync.Enabled = SAV.GameSyncID != null;
TB_GameSync.MaxLength = SAV.GameSyncIDSize;
TB_GameSync.Text = (SAV.GameSyncID ?? 0.ToString()).PadLeft(SAV.GameSyncIDSize, '0');
@ -1209,9 +1158,6 @@ namespace PKHeX.WinForms
TB_Secure2.Text = SAV.Secure2?.ToString("X16");
break;
case 7:
getFieldsfromPKM = populateFieldsPK7;
getPKMfromFields = preparePK7;
extraBytes = PK7.ExtraBytes;
TB_GameSync.Enabled = SAV.GameSyncID != null;
TB_GameSync.MaxLength = SAV.GameSyncIDSize;
TB_GameSync.Text = (SAV.GameSyncID ?? 0.ToString()).PadLeft(SAV.GameSyncIDSize, '0');
@ -1244,14 +1190,8 @@ namespace PKHeX.WinForms
CHK_HackedStats.Enabled = CHK_HackedStats.Visible = MT_Level.Enabled = MT_Level.Visible = MT_Form.Enabled = MT_Form.Visible = HaX;
TB_Level.Visible = !HaX;
// Load Extra Byte List
if (GB_ExtraBytes.Enabled)
{
CB_ExtraBytes.Items.Clear();
foreach (byte b in extraBytes)
CB_ExtraBytes.Items.Add("0x" + b.ToString("X2"));
CB_ExtraBytes.SelectedIndex = 0;
}
// Setup PKM Preparation/Extra Bytes
setPKMFormatMode(SAV.Generation, SAV.Version);
// pk2 save files do not have an Origin Game stored. Prompt the met location list to update.
if (SAV.Generation == 2)
@ -1375,7 +1315,7 @@ namespace PKHeX.WinForms
// Load Data
populateFields(pkm);
{
CB_Species.SelectedValue = 493;
CB_Species.SelectedValue = SAV.MaxSpeciesID;
CB_Move1.SelectedValue = 1;
TB_OT.Text = "PKHeX";
TB_TID.Text = 12345.ToString();
@ -1383,10 +1323,11 @@ namespace PKHeX.WinForms
CB_GameOrigin.SelectedIndex = 0;
int curlang = Array.IndexOf(GameInfo.lang_val, curlanguage);
CB_Language.SelectedIndex = curlang > CB_Language.Items.Count - 1 ? 1 : curlang;
CB_BoxSelect.SelectedIndex = 0;
CB_Ball.SelectedIndex = 0;
CB_Country.SelectedIndex = 0;
CB_Ball.SelectedIndex = Math.Min(0, CB_Ball.Items.Count - 1);
CB_Country.SelectedIndex = Math.Min(0, CB_Country.Items.Count - 1);
CAL_MetDate.Value = CAL_EggDate.Value = DateTime.Today;
CB_BoxSelect.SelectedIndex = 0;
}
}
private void InitializeLanguage()
@ -1436,6 +1377,79 @@ namespace PKHeX.WinForms
}
private Action getFieldsfromPKM;
private Func<PKM> getPKMfromFields;
private void setPKMFormatMode(int Format, GameVersion version)
{
byte[] extraBytes = new byte[0];
switch (Format)
{
case 1:
getFieldsfromPKM = populateFieldsPK1;
getPKMfromFields = preparePK1;
break;
case 2:
getFieldsfromPKM = populateFieldsPK2;
getPKMfromFields = preparePK2;
break;
case 3:
if (version == GameVersion.COLO)
{
getFieldsfromPKM = populateFieldsCK3;
getPKMfromFields = prepareCK3;
extraBytes = CK3.ExtraBytes;
break;
}
if (version == GameVersion.XD)
{
getFieldsfromPKM = populateFieldsXK3;
getPKMfromFields = prepareXK3;
extraBytes = XK3.ExtraBytes;
break;
}
getFieldsfromPKM = populateFieldsPK3;
getPKMfromFields = preparePK3;
extraBytes = PK3.ExtraBytes;
break;
case 4:
if (version == GameVersion.BATREV)
{
getFieldsfromPKM = populateFieldsBK4;
getPKMfromFields = prepareBK4;
}
else
{
getFieldsfromPKM = populateFieldsPK4;
getPKMfromFields = preparePK4;
}
extraBytes = PK4.ExtraBytes;
break;
case 5:
getFieldsfromPKM = populateFieldsPK5;
getPKMfromFields = preparePK5;
extraBytes = PK5.ExtraBytes;
break;
case 6:
getFieldsfromPKM = populateFieldsPK6;
getPKMfromFields = preparePK6;
extraBytes = PK6.ExtraBytes;
break;
case 7:
getFieldsfromPKM = populateFieldsPK7;
getPKMfromFields = preparePK7;
extraBytes = PK7.ExtraBytes;
break;
}
// Load Extra Byte List
GB_ExtraBytes.Visible = extraBytes.Length != 0;
if (GB_ExtraBytes.Visible)
{
CB_ExtraBytes.Items.Clear();
foreach (byte b in extraBytes)
CB_ExtraBytes.Items.Add("0x" + b.ToString("X2"));
CB_ExtraBytes.SelectedIndex = 0;
}
}
public void populateFields(PKM pk, bool focus = true)
{
if (pk == null) { WinFormsUtil.Error("Attempted to load a null file."); return; }

View file

@ -21,7 +21,7 @@ namespace PKHeX.WinForms
TB_Level.Text = pk2.Stat_Level.ToString();
TB_EXP.Text = pk2.EXP.ToString();
CB_HeldItem.SelectedValue = pk2.HeldItem;
CB_Form.SelectedIndex = pk2.AltForm;
CB_Form.SelectedIndex = CB_Form.Items.Count > pk2.AltForm ? pk2.AltForm : CB_Form.Items.Count - 1;
CHK_IsEgg.Checked = pk2.IsEgg;
TB_Friendship.Text = pk2.CurrentFriendship.ToString();

View file

@ -14,13 +14,17 @@ namespace PKHeX.Core
return 1 <= gen && gen <= 2;
}).ToArray();
public SAV1(byte[] data = null)
public SAV1(byte[] data = null, GameVersion versionOverride = GameVersion.Any)
{
Data = data == null ? new byte[SaveUtil.SIZE_G1RAW] : (byte[])data.Clone();
BAK = (byte[])Data.Clone();
Exportable = !Data.SequenceEqual(new byte[Data.Length]);
Version = data == null ? GameVersion.RBY : SaveUtil.getIsG1SAV(Data);
if (data == null)
Version = GameVersion.RBY;
else if (versionOverride != GameVersion.Any)
Version = versionOverride;
else Version = SaveUtil.getIsG1SAV(Data);
if (Version == GameVersion.Invalid)
return;
@ -28,7 +32,7 @@ namespace PKHeX.Core
Array.Resize(ref Data, Data.Length + SIZE_RESERVED);
Party = getPartyOffset(0);
Japanese = SaveUtil.getIsG1SAVJ(data);
Japanese = SaveUtil.getIsG1SAVJ(Data);
Personal = PersonalTable.RBY;
// Stash boxes after the save file's end.

View file

@ -14,13 +14,19 @@ namespace PKHeX.Core
return 1 <= gen && gen <= 2;
}).ToArray();
public SAV2(byte[] data = null)
public SAV2(byte[] data = null, GameVersion versionOverride = GameVersion.Any)
{
Data = data == null ? new byte[SaveUtil.SIZE_G2RAW_U] : (byte[])data.Clone();
BAK = (byte[])Data.Clone();
Exportable = !Data.SequenceEqual(new byte[Data.Length]);
Version = data == null ? GameVersion.GSC : SaveUtil.getIsG2SAV(Data);
if (data == null)
Version = GameVersion.C;
else if (versionOverride != GameVersion.Any)
Version = versionOverride;
else
Version = SaveUtil.getIsG2SAV(Data);
if (Version == GameVersion.Invalid)
return;

View file

@ -52,7 +52,7 @@ namespace PKHeX.Core
BlockOrder1[i] = BitConverter.ToInt16(Data, i*0x1000 + 0xFF4);
int zeroBlock1 = Array.IndexOf(BlockOrder1, 0);
if (data.Length > SaveUtil.SIZE_G3RAWHALF)
if (Data.Length > SaveUtil.SIZE_G3RAWHALF)
{
int[] BlockOrder2 = new int[14];
for (int i = 0; i < 14; i++)

View file

@ -425,7 +425,7 @@ namespace PKHeX.Core
/// <param name="Game">Version to create the save file for.</param>
/// <param name="OT">Trainer Name</param>
/// <returns></returns>
private static SaveFile getBlankSAV(GameVersion Game, string OT)
public static SaveFile getBlankSAV(GameVersion Game, string OT)
{
var SAV = getBlankSAV(Game);
if (SAV == null)