From c9dad89e11f00317b45a2a3f30a9b3dc0230840e Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 29 Nov 2015 12:59:13 -0800 Subject: [PATCH] Minor refactoring Cleaned up a little bit of code and used more magic references. --- Misc/PKX.cs | 6 +++--- Misc/SAV6.cs | 6 +++++- PKX/f1-Main.cs | 44 ++++++++++++++++++++------------------------ 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/Misc/PKX.cs b/Misc/PKX.cs index ae7b910e6..27f33ec6e 100644 --- a/Misc/PKX.cs +++ b/Misc/PKX.cs @@ -600,7 +600,7 @@ namespace PKHeX // Dynamic handling of checksums regardless of save size. int verificationOffset = savefile.Length - 0x200 + 0x10; - if (BitConverter.ToUInt32(savefile, verificationOffset) != 0x42454546) + if (BitConverter.ToUInt32(savefile, verificationOffset) != SAV6.BEEF) verificationOffset -= 0x200; // No savegames have more than 0x3D blocks, maybe in the future? int count = (savefile.Length - verificationOffset - 0x8) / 8; @@ -644,7 +644,7 @@ namespace PKHeX // Dynamic handling of checksums regardless of save size. int verificationOffset = savefile.Length - 0x200 + 0x10; - if (BitConverter.ToUInt32(savefile, verificationOffset) != 0x42454546) + if (BitConverter.ToUInt32(savefile, verificationOffset) != SAV6.BEEF) verificationOffset -= 0x200; // No savegames have more than 0x3D blocks, maybe in the future? int count = (savefile.Length - verificationOffset - 0x8) / 8; @@ -690,7 +690,7 @@ namespace PKHeX // Dynamic handling of checksums regardless of save size. int verificationOffset = savefile.Length - 0x200 + 0x10; - if (BitConverter.ToUInt32(savefile, verificationOffset) != 0x42454546) + if (BitConverter.ToUInt32(savefile, verificationOffset) != SAV6.BEEF) verificationOffset -= 0x200; // No savegames have more than 0x3D blocks, maybe in the future? int count = (savefile.Length - verificationOffset - 0x8) / 8; diff --git a/Misc/SAV6.cs b/Misc/SAV6.cs index 663485c7b..afc1401aa 100644 --- a/Misc/SAV6.cs +++ b/Misc/SAV6.cs @@ -20,6 +20,10 @@ namespace PKHeX } public class SAV6 : PKX { + internal static readonly int SIZE_XY = 0x65600; + internal static readonly int SIZE_ORAS = 0x76000; + internal static readonly int BEEF = 0x42454546; + // Global Settings internal static bool SetUpdateDex = true; internal static bool SetUpdatePK6 = true; @@ -182,7 +186,7 @@ namespace PKHeX private void getBlockInfo() { BlockInfoOffset = Data.Length - 0x200 + 0x10; - if (BitConverter.ToUInt32(Data, BlockInfoOffset) != 0x42454546) // BEEF, nice! + if (BitConverter.ToUInt32(Data, BlockInfoOffset) != BEEF) BlockInfoOffset -= 0x200; // No savegames have more than 0x3D blocks, maybe in the future? int count = (Data.Length - BlockInfoOffset - 0x8) / 8; BlockInfoOffset += 4; diff --git a/PKX/f1-Main.cs b/PKX/f1-Main.cs index 9529e3adf..96a9fbd0d 100644 --- a/PKX/f1-Main.cs +++ b/PKX/f1-Main.cs @@ -138,7 +138,7 @@ namespace PKHeX #region Global Variables: Always Visible! public static readonly byte[] blankEK6 = PKX.encryptArray(new byte[PK6.SIZE_PARTY]); public static PK6 pk6 = new PK6(new byte[PK6.SIZE_PARTY]); // Tab Pokemon Data Storage - public static SAV6 SAV = new SAV6(new byte[0x760000]); + public static SAV6 SAV = new SAV6(new byte[SAV6.SIZE_ORAS]); public static byte[] originalSAV; // original save for CyberGadget Codes public static byte[] ramsav; // original ramsav for ramsav exporting public string pathSDF; @@ -458,9 +458,9 @@ namespace PKHeX #endregion #region Saves - if ((input.Length == 0x76000) && BitConverter.ToUInt32(input, 0x75E10) == 0x42454546) // ORAS + if ((input.Length == SAV6.SIZE_ORAS) && BitConverter.ToUInt32(input, SAV6.SIZE_ORAS - 0x1F0) == SAV6.BEEF) // ORAS openMAIN(input, path); - else if ((input.Length == 0x65600) && BitConverter.ToUInt32(input, 0x65410) == 0x42454546) // XY + else if ((input.Length == SAV6.SIZE_XY) && BitConverter.ToUInt32(input, SAV6.SIZE_XY - 0x1F0) == SAV6.BEEF) // XY openMAIN(input, path); // Verify the Data Input Size is Proper else if (input.Length == 0x100000) @@ -476,10 +476,10 @@ namespace PKHeX savshift += 0x7F000; if (sdr == DialogResult.Cancel) return; - byte[] psdata = input.Skip(0x5400 + savshift).Take(0x76000).ToArray(); - if (BitConverter.ToUInt32(psdata, psdata.Length - 0x1F0) != 0x42454546) - Array.Resize(ref psdata, 0x65600); - if (BitConverter.ToUInt32(psdata, psdata.Length - 0x1F0) != 0x42454546) + byte[] psdata = input.Skip(0x5400 + savshift).Take(SAV6.SIZE_ORAS).ToArray(); + if (BitConverter.ToUInt32(psdata, psdata.Length - 0x1F0) != SAV6.BEEF) + Array.Resize(ref psdata, SAV6.SIZE_XY); + if (BitConverter.ToUInt32(psdata, psdata.Length - 0x1F0) != SAV6.BEEF) return; openMAIN(psdata, path); @@ -573,7 +573,7 @@ namespace PKHeX if (input.Length == 0x80000) // Scan for FEEB in XY location, 3DS only overwrites data if file already exists. for (int i = 0x60000; i < 0x64000; i+=4) - if (BitConverter.ToUInt32(input, i) == 0x42454546) { Array.Resize(ref input, 0x70000); break; } + if (BitConverter.ToUInt32(input, i) == SAV6.BEEF) { Array.Resize(ref input, 0x70000); break; } ramsav = (byte[])input.Clone(); try { openMAIN(ram2sav.getMAIN(input), path, true); } @@ -630,7 +630,7 @@ namespace PKHeX { // Detection of stored Decryption XORpads: if (ModifierKeys == Keys.Control) return false; // no xorpad compatible - byte[] savID = new byte[0x10]; Array.Copy(input, 0x10, savID, 0, 0x10); + byte[] savID = input.Take(0x10).ToArray(); string exepath = Application.StartupPath; string xorpath = exepath.Clone().ToString(); string[] XORpads = Directory.GetFiles(xorpath); @@ -640,29 +640,25 @@ namespace PKHeX { // Fix xorpad alignment byte[] xorpad = data; - if (xorpad.Length == 0x10009C) - { - Array.Copy(xorpad, 0x9C, xorpad, 0, 0x100000); - Array.Resize(ref xorpad, 0x100000); - } - byte[] xorID = new byte[0x10]; Array.Copy(xorpad, 0x10, xorID, 0, 0x10); - if (!xorID.SequenceEqual(savID)) continue; + if (xorpad.Length == 0x10009C) // Trim off Powersaves' header + xorpad = xorpad.Skip(0x9C).ToArray(); // returns 0x100000 + + if (!xorpad.Take(0x10).SequenceEqual(savID)) continue; // Set up Decrypted File - byte[] decryptedPS = new byte[0x76000]; - Array.Copy(input, 0x5400, decryptedPS, 0, 0x76000); + byte[] decryptedPS = input.Skip(0x5400).Take(SAV6.SIZE_ORAS).ToArray(); // xor through and decrypt - for (int z = 0; z < 0x76000; z++) + for (int z = 0; z < decryptedPS.Length; z++) decryptedPS[z] ^= xorpad[0x5400 + z]; // Weakly check the validity of the decrypted content - if (BitConverter.ToUInt32(decryptedPS, 0x76000 - 0x1F0) != 0x42454546) // Not OR/AS - if (BitConverter.ToUInt32(decryptedPS, 0x65600 - 0x1F0) != 0x42454546) - continue; // Not X/Y, so continue. + if (BitConverter.ToUInt32(decryptedPS, SAV6.SIZE_ORAS - 0x1F0) != SAV6.BEEF) // Not OR/AS + if (BitConverter.ToUInt32(decryptedPS, SAV6.SIZE_XY - 0x1F0) != SAV6.BEEF) // Not X/Y + continue; else - Array.Resize(ref decryptedPS, 0x65600); // set to X/Y size - else Array.Resize(ref decryptedPS, 0x76000); // set to ORAS size just in case + Array.Resize(ref decryptedPS, SAV6.SIZE_XY); // set to X/Y size + else Array.Resize(ref decryptedPS, SAV6.SIZE_ORAS); // set to ORAS size just in case // Save file is now decrypted!