From cd742126947c0e386456844d3bfe2bc7faa25f5f Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 4 Dec 2016 11:05:28 -0800 Subject: [PATCH] Fix tmhm accessing for gen7 might as well replace the indexof method with a less convoluted method, should be faster --- pk3DS/Subforms/Gen7/TMEditor7.cs | 4 ++-- pk3DS/Util.cs | 17 ++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/pk3DS/Subforms/Gen7/TMEditor7.cs b/pk3DS/Subforms/Gen7/TMEditor7.cs index 5123aec..e746d43 100644 --- a/pk3DS/Subforms/Gen7/TMEditor7.cs +++ b/pk3DS/Subforms/Gen7/TMEditor7.cs @@ -16,7 +16,7 @@ public TMEditor7() if (!File.Exists(files[0]) || !Path.GetFileNameWithoutExtension(files[0]).Contains("code")) { Util.Alert("No .code.bin detected."); Close(); } data = File.ReadAllBytes(files[0]); if (data.Length % 0x200 != 0) { Util.Alert(".code.bin not decompressed. Aborting."); Close(); } - offset = Util.IndexOfBytes(data, Signature, 0x500000, 0) + 8; + offset = Util.IndexOfBytes(data, Signature, 0x400000, 0) + Signature.Length; codebin = files[0]; movelist[0] = ""; setupDGV(); @@ -119,7 +119,7 @@ internal static void getTMHMList(ref ushort[] TMs) string[] files = Directory.GetFiles(Main.ExeFSPath); if (!File.Exists(files[0]) || !Path.GetFileNameWithoutExtension(files[0]).Contains("code")) return; byte[] data = File.ReadAllBytes(files[0]); - int dataoffset = Util.IndexOfBytes(data, Signature, 0x500000, 0) + 8; + int dataoffset = Util.IndexOfBytes(data, Signature, 0x400000, 0) + Signature.Length; if (data.Length % 0x200 != 0) return; List tms = new List(); diff --git a/pk3DS/Util.cs b/pk3DS/Util.cs index c8bba56..043eaa0 100644 --- a/pk3DS/Util.cs +++ b/pk3DS/Util.cs @@ -592,16 +592,15 @@ internal static char[] Reverse(char[] charArray) // Find Code off of Reference internal static int IndexOfBytes(byte[] array, byte[] pattern, int startIndex, int count) { - int i = startIndex; - int endIndex = count > 0 ? startIndex + count : array.Length; - int fidx = 0; - - while (i++ != endIndex - 1) + int len = pattern.Length; + int endIndex = count > 0 ? startIndex + count + 1 : array.Length; + for (int i = startIndex; i < endIndex; i++) { - if (array[i] != pattern[fidx]) i -= fidx; - fidx = (array[i] == pattern[fidx]) ? ++fidx : 0; - if (fidx == pattern.Length) - return i - fidx + 1; + for (int j = 0; j < len; j++) + if (pattern[j] != array[i + j]) + goto next_i; + return i; + next_i:; } return -1; }