From 22cb999513d1d75f46d7927522a7ee1ba2bcd2b3 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 22 Mar 2015 12:56:33 -0700 Subject: [PATCH] Pattern Finding & Multiple Additons Added: ExeFS offset finding for different patch versions. Added: Normal Pokemart stock editing Added: TMHM move names for Personal editor. --- pk3DS/Subforms/Mart.cs | 61 ++++++++++++++++++++++++----- pk3DS/Subforms/Personal.Designer.cs | 14 +++---- pk3DS/Subforms/Personal.cs | 24 ++++++++++-- pk3DS/Subforms/Pickup.cs | 1 + pk3DS/Subforms/TMHM.cs | 36 ++++++++++++++++- pk3DS/Util.cs | 3 +- 6 files changed, 117 insertions(+), 22 deletions(-) diff --git a/pk3DS/Subforms/Mart.cs b/pk3DS/Subforms/Mart.cs index 155219f..b9d6215 100644 --- a/pk3DS/Subforms/Mart.cs +++ b/pk3DS/Subforms/Mart.cs @@ -14,6 +14,7 @@ public Mart() 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, new byte[] { 0x00, 0x72, 0x6F, 0x6D, 0x3A, 0x2F, 0x44, 0x6C, 0x6C, 0x53, 0x74, 0x61, 0x72, 0x74, 0x4D, 0x65, 0x6E, 0x75, 0x2E, 0x63, 0x72, 0x6F, 0x00 }, 0x400000, 0) + 0x17; codebin = files[0]; itemlist[0] = ""; setupDGV(); @@ -23,17 +24,59 @@ public Mart() string codebin; string[] itemlist = Main.getText((Main.oras) ? 114 : 96); byte[] data; - byte[] entries = { 7, 6, 4, 3, 8, - 8, 3, 3, 4, - 3, 6, 6, - 7, 4 }; - private const int offset = 0x0047AB58; + private byte[] entries = Main.oras + ? new byte[] // ORAS + { + 3, 10, 14, 17, 18, 19, 19, 19, 19, // General + 3, // Unused + 7, 6, 4, 3, 8, + 8, 3, 3, 4, + 3, 6, 6, + 7, 4 + } + : new byte[] // XY + { + 3, 10, 14, 17, 18, 19, 19, 19, 19, // General + 3, // Unused + 2, 10, 3, 9, 1, 1, // Misc + 3, 3, // Balls + 5, 5, // TMs + 6, // Vitamins + 7, // Balls + 5, // TMs + 5, // TMs + 8, // Battle + 3, // Balls + }; + + private int offset = Main.oras ? 0x0047AB58 : 0x0043C89E; int dataoffset; - string[] locations = { "Slateport Market [Incenses]", "Slateport Market [Vitamins]", "Slateport Market [TMs]", "Rustboro Mart [Poke Balls]", "Slateport Mart [Misc]", - "Mauville Mart [TMs]", "Verdanturf Mart [Poke Balls]", "Fallarbor Mart [Poke Balls]", "Lavaridge Town [Herbs]", - "Lilycove Dept Store, 2F Left [Run Away Items]", "Lilycove Dept Store, 3F Left [Vitamins]", "Lilycove Dept Store, 3F Right [Misc]", - "Lilycove Dept Store, 4F Left [Offensive TMs]", "Lilycove Dept Store, 4F Right [Defensive TMs]" }; + string[] locations = Main.oras + ? new[] // ORAS + { + "No Badges", "1 Badge", "2 Badges", "3 Badges", "4 Badges", "5 Badges", "6 Badges", "7 Badges", "8 Badges", + "Unused", + "Slateport Market [Incenses]", "Slateport Market [Vitamins]", "Slateport Market [TMs]", "Rustboro Mart [Poke Balls]", "Slateport Mart [Misc]", + "Mauville Mart [TMs]", "Verdanturf Mart [Poke Balls]", "Fallarbor Mart [Poke Balls]", "Lavaridge Town [Herbs]", + "Lilycove Dept Store, 2F Left [Run Away Items]", "Lilycove Dept Store, 3F Left [Vitamins]", "Lilycove Dept Store, 3F Right [Misc]", + "Lilycove Dept Store, 4F Left [Offensive TMs]", "Lilycove Dept Store, 4F Right [Defensive TMs]" + } + : new[] // XY + { + "No Badges", "1 Badge", "2 Badges", "3 Badges", "4 Badges", "5 Badges", "6 Badges", "7 Badges", "8 Badges", + "Unused", + "Herbs", "Balls", "Stones", "Incence", "Aquacorde Balls", "Aquacorde Potion", + "Lumiose North Boulevard [Balls]", "Cyllage City [Balls]", + "Shalour City [TMs]", "Lumiose South Boulevard [TMs]", + "Laverre City [Vitamins]", + "Snowbelle City [Balls]", + "Kiloude City [TMs]", + "Anistar City [TMs]", + "Santalune City [X-Stat]", + "Coumarine City [Balls]" + }; + private void getDataOffset(int index) { dataoffset = offset; // reset diff --git a/pk3DS/Subforms/Personal.Designer.cs b/pk3DS/Subforms/Personal.Designer.cs index 57eb264..fbc64c7 100644 --- a/pk3DS/Subforms/Personal.Designer.cs +++ b/pk3DS/Subforms/Personal.Designer.cs @@ -881,7 +881,7 @@ private void InitializeComponent() // Lbl_OrasTutors // this.Lbl_OrasTutors.AutoSize = true; - this.Lbl_OrasTutors.Location = new System.Drawing.Point(278, 3); + this.Lbl_OrasTutors.Location = new System.Drawing.Point(298, 3); this.Lbl_OrasTutors.Name = "Lbl_OrasTutors"; this.Lbl_OrasTutors.Size = new System.Drawing.Size(65, 13); this.Lbl_OrasTutors.TabIndex = 7; @@ -891,7 +891,7 @@ private void InitializeComponent() // label27 // this.label27.AutoSize = true; - this.label27.Location = new System.Drawing.Point(139, 3); + this.label27.Location = new System.Drawing.Point(159, 3); this.label27.Name = "label27"; this.label27.Size = new System.Drawing.Size(78, 13); this.label27.TabIndex = 6; @@ -912,9 +912,9 @@ private void InitializeComponent() | System.Windows.Forms.AnchorStyles.Left))); this.CLB_OrasTutors.Enabled = false; this.CLB_OrasTutors.FormattingEnabled = true; - this.CLB_OrasTutors.Location = new System.Drawing.Point(281, 19); + this.CLB_OrasTutors.Location = new System.Drawing.Point(301, 19); this.CLB_OrasTutors.Name = "CLB_OrasTutors"; - this.CLB_OrasTutors.Size = new System.Drawing.Size(133, 349); + this.CLB_OrasTutors.Size = new System.Drawing.Size(133, 319); this.CLB_OrasTutors.TabIndex = 4; this.CLB_OrasTutors.Visible = false; // @@ -923,9 +923,9 @@ private void InitializeComponent() this.CLB_MoveTutors.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left))); this.CLB_MoveTutors.FormattingEnabled = true; - this.CLB_MoveTutors.Location = new System.Drawing.Point(142, 19); + this.CLB_MoveTutors.Location = new System.Drawing.Point(162, 19); this.CLB_MoveTutors.Name = "CLB_MoveTutors"; - this.CLB_MoveTutors.Size = new System.Drawing.Size(133, 349); + this.CLB_MoveTutors.Size = new System.Drawing.Size(133, 319); this.CLB_MoveTutors.TabIndex = 3; // // CLB_TMHM @@ -935,7 +935,7 @@ private void InitializeComponent() this.CLB_TMHM.FormattingEnabled = true; this.CLB_TMHM.Location = new System.Drawing.Point(9, 19); this.CLB_TMHM.Name = "CLB_TMHM"; - this.CLB_TMHM.Size = new System.Drawing.Size(124, 349); + this.CLB_TMHM.Size = new System.Drawing.Size(147, 319); this.CLB_TMHM.TabIndex = 2; // // PB_MonSprite diff --git a/pk3DS/Subforms/Personal.cs b/pk3DS/Subforms/Personal.cs index 9ce2420..6f56bfd 100644 --- a/pk3DS/Subforms/Personal.cs +++ b/pk3DS/Subforms/Personal.cs @@ -80,10 +80,26 @@ private void Setup() abilities[0] = items[0] = moves[0] = ""; AltForms = getFormList(data, Main.oras, species, forms, types, items); species = getPersonalEntryList(data, Main.oras, AltForms, species); - for (int i = 1; i <= 100; i++) - CLB_TMHM.Items.Add("TM" + i.ToString("00")); - for (int i = 1; i <= 7; i++) - CLB_TMHM.Items.Add("HM" + i.ToString("00")); + + ushort[] TMs = new ushort[0]; + ushort[] HMs = new ushort[0]; + TMHM.getTMHMList(Main.oras, ref TMs, ref HMs); + CLB_TMHM.Items.Clear(); + + if (TMs.Length == 0) // No ExeFS to grab TMs from. + { + for (int i = 1; i <= 100; i++) + CLB_TMHM.Items.Add("TM" + i.ToString("00")); + for (int i = 1; i <= 7; i++) + CLB_TMHM.Items.Add("HM" + i.ToString("00")); + } + else // Use TMHM moves. + { + for (int i = 1; i <= 100; i++) + CLB_TMHM.Items.Add(String.Format("TM{0} {1}", i.ToString("00"), moves[TMs[i - 1]])); + for (int i = 1; i <= 7; i++) + CLB_TMHM.Items.Add(String.Format("HM{0} {1}", i.ToString("00"), moves[HMs[i - 1]])); + } for (int i = 0; i < tutormoves.Length - 1; i++) CLB_MoveTutors.Items.Add(moves[tutormoves[i]]); diff --git a/pk3DS/Subforms/Pickup.cs b/pk3DS/Subforms/Pickup.cs index 5f2769d..de22c7c 100644 --- a/pk3DS/Subforms/Pickup.cs +++ b/pk3DS/Subforms/Pickup.cs @@ -15,6 +15,7 @@ public Pickup() 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, new byte[] { 0x1E, 0x28, 0x32, 0x3C, 0x46, 0x50, 0x5A, 0x5E, 0x62, 0x05, 0x0A, 0x0F, 0x14, 0x19, 0x1E, 0x23, 0x28, 0x2D, 0x32 }, 0x400000, 0) - 0x3A; codebin = files[0]; itemlist[0] = ""; setupDGV(); diff --git a/pk3DS/Subforms/TMHM.cs b/pk3DS/Subforms/TMHM.cs index e656e37..7eaf5a5 100644 --- a/pk3DS/Subforms/TMHM.cs +++ b/pk3DS/Subforms/TMHM.cs @@ -16,6 +16,7 @@ public TMHM() 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, new byte[] { 0xD4, 0x00, 0xAE, 0x02, 0xAF, 0x02, 0xB0, 0x02 }, 0x400000, 0) + 8; codebin = files[0]; movelist[0] = ""; setupDGV(); @@ -23,7 +24,7 @@ public TMHM() } string codebin; string[] movelist = Main.getText((Main.oras) ? 14 : 13); - int offset = (Main.oras) ? 0x004A67EE : 0x00464796; + int offset = (Main.oras) ? 0x004A67EE : 0x00464796; // Default byte[] data; int dataoffset; private void getDataOffset() @@ -147,5 +148,38 @@ private void B_RandomTM_Click(object sender, EventArgs e) dgvTM.Rows[i].Cells[1].Value = movelist[randomMoves[ctr++]]; } } + + internal static void getTMHMList(bool oras, ref ushort[] TMs, ref ushort[] HMs) + { + if (Main.ExeFS == null) return; + string[] files = Directory.GetFiles(Main.ExeFS); + if (!File.Exists(files[0]) || !Path.GetFileNameWithoutExtension(files[0]).Contains("code")) return; + byte[] data = File.ReadAllBytes(files[0]); + int dataoffset = Util.IndexOfBytes(data, new byte[] { 0xD4, 0x00, 0xAE, 0x02, 0xAF, 0x02, 0xB0, 0x02 }, 0x400000, 0) + 8; + if (data.Length % 0x200 != 0) return; + + List tms = new List(); + List hms = new List(); + + for (int i = 0; i < 92; i++) // 1-92 TMs stored sequentially + tms.Add(BitConverter.ToUInt16(data, dataoffset + 2 * i)); + for (int i = 92; i < 92 + 5; i++) + hms.Add(BitConverter.ToUInt16(data, dataoffset + 2 * i)); + if (Main.oras) + { + hms.Add(BitConverter.ToUInt16(data, dataoffset + 2 * 97)); + for (int i = 98; i < 106; i++) + tms.Add(BitConverter.ToUInt16(data, dataoffset + 2 * i)); + hms.Add(BitConverter.ToUInt16(data, dataoffset + 2 * 106)); + } + else + { + for (int i = 97; i < 105; i++) + tms.Add(BitConverter.ToUInt16(data, dataoffset + 2 * i)); + } + + TMs = tms.ToArray(); + HMs = hms.ToArray(); + } } } \ No newline at end of file diff --git a/pk3DS/Util.cs b/pk3DS/Util.cs index 642a77e..09d3184 100644 --- a/pk3DS/Util.cs +++ b/pk3DS/Util.cs @@ -587,8 +587,9 @@ internal static int IndexOfBytes(byte[] array, byte[] pattern, int startIndex, i int endIndex = count > 0 ? startIndex + count : array.Length; int fidx = 0; - while (i++ < endIndex) + while (i++ != endIndex - 1) { + if (array[i] != pattern[fidx]) i -= fidx; fidx = (array[i] == pattern[fidx]) ? ++fidx : 0; if (fidx == pattern.Length) return i - fidx + 1;