From e34c8df0449c7c8fd2d204d9f0f9c9a68386bfdf Mon Sep 17 00:00:00 2001 From: Kurt Date: Tue, 22 Dec 2020 20:50:33 -0800 Subject: [PATCH] Revise some method signatures --- PKHeX.Core/Legality/MoveList.cs | 4 +-- PKHeX.Core/Legality/Moves/MoveEgg.cs | 24 ++++++++--------- PKHeX.Core/PKM/Util/PokeCrypto.cs | 16 +++++++++++ PKHeX.Core/Saves/SAV4.cs | 27 +++++++------------ PKHeX.Core/Saves/Substructures/Gen7/QR7.cs | 6 ++--- .../Save Editors/Gen6/SAV_SecretBase.cs | 10 +++---- 6 files changed, 48 insertions(+), 39 deletions(-) diff --git a/PKHeX.Core/Legality/MoveList.cs b/PKHeX.Core/Legality/MoveList.cs index 7bf812d55..aedf8a43a 100644 --- a/PKHeX.Core/Legality/MoveList.cs +++ b/PKHeX.Core/Legality/MoveList.cs @@ -16,14 +16,14 @@ internal static IEnumerable GetValidRelearn(PKM pkm, int species, int form, return Array.Empty(); var r = new List(); - r.AddRange(MoveEgg.GetRelearnLVLMoves(pkm, species, 1, form, version)); + r.AddRange(MoveEgg.GetRelearnLVLMoves(pkm, species, form, 1, version)); if (pkm.Format == 6 && pkm.Species != (int)Species.Meowstic) form = 0; r.AddRange(MoveEgg.GetEggMoves(pkm, species, form, version)); if (inheritlvlmoves) - r.AddRange(MoveEgg.GetRelearnLVLMoves(pkm, species, 100, form, version)); + r.AddRange(MoveEgg.GetRelearnLVLMoves(pkm, species, form, 100, version)); return r.Distinct(); } diff --git a/PKHeX.Core/Legality/Moves/MoveEgg.cs b/PKHeX.Core/Legality/Moves/MoveEgg.cs index d08f66011..1cedebec9 100644 --- a/PKHeX.Core/Legality/Moves/MoveEgg.cs +++ b/PKHeX.Core/Legality/Moves/MoveEgg.cs @@ -6,7 +6,7 @@ namespace PKHeX.Core { public static class MoveEgg { - public static int[] GetEggMoves(PKM pkm, int species, int formnum, GameVersion version) + public static int[] GetEggMoves(PKM pkm, int species, int form, GameVersion version) { int gen = pkm.Format <= 2 || pkm.VC ? 2 : pkm.Generation; if (!pkm.InhabitedGeneration(gen, species) || (pkm.PersonalInfo.Genderless && !FixedGenderFromBiGender.Contains(species))) @@ -17,10 +17,10 @@ public static int[] GetEggMoves(PKM pkm, int species, int formnum, GameVersion v if (version == GameVersion.Any) version = (GameVersion)pkm.Version; - return GetEggMoves(gen, species, formnum, version); + return GetEggMoves(gen, species, form, version); } - public static int[] GetEggMoves(int gen, int species, int formnum, GameVersion version) + public static int[] GetEggMoves(int gen, int species, int form, GameVersion version) { switch (gen) { @@ -49,15 +49,15 @@ public static int[] GetEggMoves(int gen, int species, int formnum, GameVersion v case 7: // entries per form if required return version switch { - GameVersion.US => GetFormEggMoves(species, formnum, EggMovesUSUM), - GameVersion.UM => GetFormEggMoves(species, formnum, EggMovesUSUM), - _ => GetFormEggMoves(species, formnum, EggMovesSM) + GameVersion.US => GetFormEggMoves(species, form, EggMovesUSUM), + GameVersion.UM => GetFormEggMoves(species, form, EggMovesUSUM), + _ => GetFormEggMoves(species, form, EggMovesSM) }; case 8: return version switch { - _ => GetFormEggMoves(species, formnum, EggMovesSWSH) + _ => GetFormEggMoves(species, form, EggMovesSWSH) }; default: @@ -65,15 +65,15 @@ public static int[] GetEggMoves(int gen, int species, int formnum, GameVersion v } } - private static int[] GetFormEggMoves(int species, int formnum, IReadOnlyList table) + private static int[] GetFormEggMoves(int species, int form, IReadOnlyList table) { var entry = table[species]; - if (formnum > 0 && entry.FormTableIndex > species) - entry = table[entry.FormTableIndex + formnum - 1]; + if (form > 0 && entry.FormTableIndex > species) + entry = table[entry.FormTableIndex + form - 1]; return entry.Moves; } - internal static int[] GetRelearnLVLMoves(PKM pkm, int species, int lvl, int formnum, GameVersion version = GameVersion.Any) + internal static int[] GetRelearnLVLMoves(PKM pkm, int species, int form, int lvl, GameVersion version = GameVersion.Any) { if (version == GameVersion.Any) version = (GameVersion)pkm.Version; @@ -103,7 +103,7 @@ internal static int[] GetRelearnLVLMoves(PKM pkm, int species, int lvl, int form } return Array.Empty(); - int[] getMoves(IReadOnlyList moves, PersonalTable table) => moves[table.GetFormIndex(species, formnum)].GetMoves(lvl); + int[] getMoves(IReadOnlyList moves, PersonalTable table) => moves[table.GetFormIndex(species, form)].GetMoves(lvl); } public static bool GetIsSharedEggMove(PKM pkm, int gen, int move) diff --git a/PKHeX.Core/PKM/Util/PokeCrypto.cs b/PKHeX.Core/PKM/Util/PokeCrypto.cs index 567de32c0..9a7219c6c 100644 --- a/PKHeX.Core/PKM/Util/PokeCrypto.cs +++ b/PKHeX.Core/PKM/Util/PokeCrypto.cs @@ -335,6 +335,10 @@ public static ushort GetCHK3(byte[] data) return chk; } + /// + /// Decrypts the input data into a new array if it is encrypted, and updates the reference. + /// + /// Generation 3 Format encryption check which verifies the checksum public static void DecryptIfEncrypted3(ref byte[] pkm) { ushort chk = GetCHK3(pkm); @@ -342,18 +346,30 @@ public static void DecryptIfEncrypted3(ref byte[] pkm) pkm = DecryptArray3(pkm); } + /// + /// Decrypts the input data into a new array if it is encrypted, and updates the reference. + /// + /// Generation 4 & 5 Format encryption check which checks for the unused bytes public static void DecryptIfEncrypted45(ref byte[] pkm) { if (BitConverter.ToUInt32(pkm, 0x64) != 0) pkm = DecryptArray45(pkm); } + /// + /// Decrypts the input data into a new array if it is encrypted, and updates the reference. + /// + /// Generation 6 & 7 Format encryption check public static void DecryptIfEncrypted67(ref byte[] pkm) { if (BitConverter.ToUInt16(pkm, 0xC8) != 0 || BitConverter.ToUInt16(pkm, 0x58) != 0) pkm = DecryptArray6(pkm); } + /// + /// Decrypts the input data into a new array if it is encrypted, and updates the reference. + /// + /// Generation 8 Format encryption check public static void DecryptIfEncrypted8(ref byte[] pkm) { if (BitConverter.ToUInt16(pkm, 0x70) != 0 || BitConverter.ToUInt16(pkm, 0xC0) != 0) diff --git a/PKHeX.Core/Saves/SAV4.cs b/PKHeX.Core/Saves/SAV4.cs index 5c59819e3..42b320bf7 100644 --- a/PKHeX.Core/Saves/SAV4.cs +++ b/PKHeX.Core/Saves/SAV4.cs @@ -652,13 +652,13 @@ protected override void SetDex(PKM pkm) else if (pkm.Species == (int)Species.Pichu && HGSS) // Pichu (HGSS Only) { int form = pkm.Form == 1 ? 2 : pkm.Gender; - CheckInsertForm(ref forms, form); - SetForms(pkm.Species, forms); + if (TryInsertForm(forms, form)) + SetForms(pkm.Species, forms); } else { - CheckInsertForm(ref forms, pkm.Form); - SetForms(pkm.Species, forms); + if (TryInsertForm(forms, pkm.Form)) + SetForms(pkm.Species, forms); } } @@ -844,24 +844,17 @@ private static uint SetDexFormValues(int[] Forms, int BitsPerForm, int readCt) return Value; } - private static bool CheckInsertForm(ref int[] Forms, int FormNum) + private static bool TryInsertForm(int[] forms, int form) { - if (Forms.Any(num => num == FormNum)) - { + if (Array.IndexOf(forms, form) >= 0) return false; // already in list - } - if (Forms.All(num => num == -1)) - { - Forms[0] = FormNum; - return true; // none in list, insert at top - } // insert at first empty - int n1 = Array.IndexOf(Forms, -1); - if (n1 < 0) - return false; + var index = Array.IndexOf(forms, -1); + if (index < 0) + return false; // no free slots? - Forms[n1] = FormNum; + forms[index] = form; return true; } diff --git a/PKHeX.Core/Saves/Substructures/Gen7/QR7.cs b/PKHeX.Core/Saves/Substructures/Gen7/QR7.cs index 458bacb60..3d0b061ba 100644 --- a/PKHeX.Core/Saves/Substructures/Gen7/QR7.cs +++ b/PKHeX.Core/Saves/Substructures/Gen7/QR7.cs @@ -44,12 +44,12 @@ public static class QR7 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; - private static byte[] GetRawQR(int species, int formnum, bool shiny, int gender) + private static byte[] GetRawQR(int species, int form, bool shiny, int gender) { var basedata = (byte[])BaseQR.Clone(); BitConverter.GetBytes((ushort)species).CopyTo(basedata, 0x28); - var pi = PersonalTable.USUM.GetFormEntry(species, formnum); + var pi = PersonalTable.USUM.GetFormEntry(species, form); bool biGender = false; if (pi.OnlyMale) gender = 0; @@ -60,7 +60,7 @@ private static byte[] GetRawQR(int species, int formnum, bool shiny, int gender) else biGender = !GenderDifferences.Contains(species); - basedata[0x2A] = (byte)formnum; + basedata[0x2A] = (byte)form; basedata[0x2B] = (byte)gender; basedata[0x2C] = shiny ? 1 : 0; basedata[0x2D] = biGender ? 1 : 0; diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SecretBase.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SecretBase.cs index 6ee7d93a4..953591f82 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SecretBase.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SecretBase.cs @@ -446,13 +446,13 @@ private void LoadFavPKM() private void SetAbilityList() { - int newabil = Convert.ToInt16(MT_AbilNo.Text) >> 1; + int abilityIndex = Convert.ToInt16(MT_AbilNo.Text) >> 1; int species = WinFormsUtil.GetIndex(CB_Species); - int formnum = CB_Form.SelectedIndex; - var abils = PersonalTable.AO.GetFormEntry(species, formnum).Abilities; + int form = CB_Form.SelectedIndex; + var abilities = PersonalTable.AO.GetFormEntry(species, form).Abilities; - CB_Ability.DataSource = GameInfo.FilteredSources.GetAbilityList(abils, 6); - CB_Ability.SelectedIndex = newabil < 3 ? newabil : 0; + CB_Ability.DataSource = GameInfo.FilteredSources.GetAbilityList(abilities, 6); + CB_Ability.SelectedIndex = abilityIndex < 3 ? abilityIndex : 0; } private void SetForms()