diff --git a/PKHeX.WinForms/MainWindow/Main.cs b/PKHeX.WinForms/MainWindow/Main.cs index 68b152389..2fa41c45d 100644 --- a/PKHeX.WinForms/MainWindow/Main.cs +++ b/PKHeX.WinForms/MainWindow/Main.cs @@ -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 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; } diff --git a/PKHeX.WinForms/MainWindow/MainPK2.cs b/PKHeX.WinForms/MainWindow/MainPK2.cs index 640baa930..b3fcc3954 100644 --- a/PKHeX.WinForms/MainWindow/MainPK2.cs +++ b/PKHeX.WinForms/MainWindow/MainPK2.cs @@ -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(); diff --git a/PKHeX/Saves/SAV1.cs b/PKHeX/Saves/SAV1.cs index 5b00dbf6b..f4541f950 100644 --- a/PKHeX/Saves/SAV1.cs +++ b/PKHeX/Saves/SAV1.cs @@ -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. diff --git a/PKHeX/Saves/SAV2.cs b/PKHeX/Saves/SAV2.cs index 70248c81a..7349c4e1b 100644 --- a/PKHeX/Saves/SAV2.cs +++ b/PKHeX/Saves/SAV2.cs @@ -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; diff --git a/PKHeX/Saves/SAV3.cs b/PKHeX/Saves/SAV3.cs index 0b5c851b8..0cf1a2efd 100644 --- a/PKHeX/Saves/SAV3.cs +++ b/PKHeX/Saves/SAV3.cs @@ -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++) diff --git a/PKHeX/Saves/SaveUtil.cs b/PKHeX/Saves/SaveUtil.cs index 8ed75a6bc..48282469c 100644 --- a/PKHeX/Saves/SaveUtil.cs +++ b/PKHeX/Saves/SaveUtil.cs @@ -425,7 +425,7 @@ namespace PKHeX.Core /// Version to create the save file for. /// Trainer Name /// - private static SaveFile getBlankSAV(GameVersion Game, string OT) + public static SaveFile getBlankSAV(GameVersion Game, string OT) { var SAV = getBlankSAV(Game); if (SAV == null)