From 6239f59b9d2bdf98d9f00e9de401715fb7a2acfa Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 18 Oct 2020 09:16:52 -0700 Subject: [PATCH] Inline checksum logic for gen3, simplify else case --- PKHeX.Core/PKM/PK3.cs | 2 ++ PKHeX.Core/PKM/PKM.cs | 16 +--------------- PKHeX.Core/PKM/Util/PKMConverter.cs | 2 +- PKHeX.Core/PKM/Util/PokeCrypto.cs | 6 +++--- 4 files changed, 7 insertions(+), 19 deletions(-) diff --git a/PKHeX.Core/PKM/PK3.cs b/PKHeX.Core/PKM/PK3.cs index f9f2d3a8a..b37415e7a 100644 --- a/PKHeX.Core/PKM/PK3.cs +++ b/PKHeX.Core/PKM/PK3.cs @@ -201,6 +201,8 @@ namespace PKHeX.Core base.RefreshChecksum(); } + protected override ushort CalculateChecksum() => PokeCrypto.GetCHK3(Data); + public PK4 ConvertToPK4() { PK4 pk4 = new PK4 // Convert away! diff --git a/PKHeX.Core/PKM/PKM.cs b/PKHeX.Core/PKM/PKM.cs index b44e4382f..a4975f0df 100644 --- a/PKHeX.Core/PKM/PKM.cs +++ b/PKHeX.Core/PKM/PKM.cs @@ -39,21 +39,7 @@ namespace PKHeX.Core protected byte[] GetData(int Offset, int Length) => Data.Slice(Offset, Length); - protected virtual ushort CalculateChecksum() - { - ushort chk = 0; - switch (Format) - { - case 3: - for (int i = 32; i < SIZE_STORED; i += 2) - chk += BitConverter.ToUInt16(Data, i); - return chk; - default: // 4+ - for (int i = 8; i < SIZE_STORED; i += 2) - chk += BitConverter.ToUInt16(Data, i); - return chk; - } - } + protected virtual ushort CalculateChecksum() => PokeCrypto.GetCHK(Data, SIZE_STORED); protected abstract byte[] Encrypt(); public abstract int Format { get; } diff --git a/PKHeX.Core/PKM/Util/PKMConverter.cs b/PKHeX.Core/PKM/Util/PKMConverter.cs index 9d58b2f67..f44c4153c 100644 --- a/PKHeX.Core/PKM/Util/PKMConverter.cs +++ b/PKHeX.Core/PKM/Util/PKMConverter.cs @@ -78,7 +78,7 @@ namespace PKHeX.Core case PokeCrypto.SIZE_6PARTY: // collision with PGT, same size. if (BitConverter.ToUInt16(data, 0x4) != 0) // Bad Sanity? return -1; - if (BitConverter.ToUInt32(data, 0x06) == PokeCrypto.GetCHK(data)) + if (BitConverter.ToUInt32(data, 0x06) == PokeCrypto.GetCHK(data, PokeCrypto.SIZE_6STORED)) return 6; if (BitConverter.ToUInt16(data, 0x58) != 0) // Encrypted? { diff --git a/PKHeX.Core/PKM/Util/PokeCrypto.cs b/PKHeX.Core/PKM/Util/PokeCrypto.cs index e13f9dce5..567de32c0 100644 --- a/PKHeX.Core/PKM/Util/PokeCrypto.cs +++ b/PKHeX.Core/PKM/Util/PokeCrypto.cs @@ -313,12 +313,12 @@ namespace PKHeX.Core /// Gets the checksum of a 232 byte array. /// /// Decrypted Pokémon data. - public static ushort GetCHK(byte[] data) + /// Offset at which the Stored data ends and the Party data starts. + public static ushort GetCHK(byte[] data, int partyStart) { ushort chk = 0; - for (int i = 8; i < SIZE_6STORED; i += 2) + for (int i = 8; i < partyStart; i += 2) chk += BitConverter.ToUInt16(data, i); - return chk; }