From 027e0e19b10b747430e49ac0fddee8b7733b1058 Mon Sep 17 00:00:00 2001 From: Kaphotics Date: Mon, 18 Apr 2016 18:21:50 -0700 Subject: [PATCH] Refactoring --- Legality/Core.cs | 18 +++++++++--------- Legality/Data.cs | 42 ++++++++++++++++++++++++++++++++++++++++++ Misc/CodeGenerator.cs | 39 ++++++++++++--------------------------- Misc/PKX.cs | 4 ++-- Misc/Util.cs | 39 --------------------------------------- PKX/f1-Main.cs | 8 +++----- 6 files changed, 68 insertions(+), 82 deletions(-) diff --git a/Legality/Core.cs b/Legality/Core.cs index 4108bd338..f3fd29bc5 100644 --- a/Legality/Core.cs +++ b/Legality/Core.cs @@ -10,11 +10,11 @@ namespace PKHeX // PKHeX master personal.dat internal static readonly PersonalInfo[] PersonalAO = PersonalInfo.getPersonalArray(Properties.Resources.personal_ao, PersonalInfo.SizeAO); private static readonly PersonalInfo[] PersonalXY = PersonalInfo.getPersonalArray(Properties.Resources.personal_xy, PersonalInfo.SizeXY); - private static readonly EggMoves[] EggMoveXY = EggMoves.getArray(Util.unpackMini(Properties.Resources.eggmove_xy, "xy")); - private static readonly Learnset[] LevelUpXY = Learnset.getArray(Util.unpackMini(Properties.Resources.lvlmove_xy, "xy")); - private static readonly EggMoves[] EggMoveAO = EggMoves.getArray(Util.unpackMini(Properties.Resources.eggmove_ao, "ao")); - private static readonly Learnset[] LevelUpAO = Learnset.getArray(Util.unpackMini(Properties.Resources.lvlmove_ao, "ao")); - private static readonly Evolutions[] Evolves = Evolutions.getArray(Util.unpackMini(Properties.Resources.evos_ao, "ao")); + private static readonly EggMoves[] EggMoveXY = EggMoves.getArray(Data.unpackMini(Properties.Resources.eggmove_xy, "xy")); + private static readonly Learnset[] LevelUpXY = Learnset.getArray(Data.unpackMini(Properties.Resources.lvlmove_xy, "xy")); + private static readonly EggMoves[] EggMoveAO = EggMoves.getArray(Data.unpackMini(Properties.Resources.eggmove_ao, "ao")); + private static readonly Learnset[] LevelUpAO = Learnset.getArray(Data.unpackMini(Properties.Resources.lvlmove_ao, "ao")); + private static readonly Evolutions[] Evolves = Evolutions.getArray(Data.unpackMini(Properties.Resources.evos_ao, "ao")); private static readonly EncounterArea[] SlotsA; private static readonly EncounterArea[] SlotsO; private static readonly EncounterArea[] SlotsX; @@ -62,8 +62,8 @@ namespace PKHeX StaticA = getSpecial(GameVersion.AS); StaticO = getSpecial(GameVersion.OR); - var XSlots = EncounterArea.getArray(Util.unpackMini(Properties.Resources.encounter_x, "xy")); - var YSlots = EncounterArea.getArray(Util.unpackMini(Properties.Resources.encounter_y, "xy")); + var XSlots = EncounterArea.getArray(Data.unpackMini(Properties.Resources.encounter_x, "xy")); + var YSlots = EncounterArea.getArray(Data.unpackMini(Properties.Resources.encounter_y, "xy")); // Mark Horde Encounters foreach (var area in XSlots) @@ -81,8 +81,8 @@ namespace PKHeX SlotsX = addXYAltTiles(XSlots, SlotsXYAlt); SlotsY = addXYAltTiles(YSlots, SlotsXYAlt); - SlotsA = EncounterArea.getArray(Util.unpackMini(Properties.Resources.encounter_a, "ao")); - SlotsO = EncounterArea.getArray(Util.unpackMini(Properties.Resources.encounter_o, "ao")); + SlotsA = EncounterArea.getArray(Data.unpackMini(Properties.Resources.encounter_a, "ao")); + SlotsO = EncounterArea.getArray(Data.unpackMini(Properties.Resources.encounter_o, "ao")); // Mark Rock Smash Encounters foreach (var area in SlotsA) diff --git a/Legality/Data.cs b/Legality/Data.cs index 174706250..17fb236c0 100644 --- a/Legality/Data.cs +++ b/Legality/Data.cs @@ -336,4 +336,46 @@ namespace PKHeX return d; } } + + internal static class Data + { + internal static byte[][] unpackMini(byte[] fileData, string identifier) + { + using (var s = new MemoryStream(fileData)) + using (var br = new BinaryReader(s)) + { + if (identifier != new string(br.ReadChars(2))) + return null; + + ushort count = br.ReadUInt16(); + byte[][] returnData = new byte[count][]; + + uint[] offsets = new uint[count + 1]; + for (int i = 0; i < count; i++) + offsets[i] = br.ReadUInt32(); + + uint length = br.ReadUInt32(); + offsets[offsets.Length - 1] = length; + + for (int i = 0; i < count; i++) + { + br.BaseStream.Seek(offsets[i], SeekOrigin.Begin); + using (MemoryStream dataout = new MemoryStream()) + { + byte[] data = new byte[0]; + s.CopyTo(dataout, (int)offsets[i]); + int len = (int)offsets[i + 1] - (int)offsets[i]; + + if (len != 0) + { + data = dataout.ToArray(); + Array.Resize(ref data, len); + } + returnData[i] = data; + } + } + return returnData; + } + } + } } diff --git a/Misc/CodeGenerator.cs b/Misc/CodeGenerator.cs index 37a366175..dc4750fb2 100644 --- a/Misc/CodeGenerator.cs +++ b/Misc/CodeGenerator.cs @@ -82,16 +82,6 @@ namespace PKHeX RTB_Code.AppendText((writeoffset + i * 4 + 0x20000000).ToString("X8") + " "); RTB_Code.AppendText(BitConverter.ToUInt32(newdata,i*4).ToString("X8") + Environment.NewLine); } - - // Mat's Code - Unfinished - //for (int i = 0; i < newdata.Length / (4); i++) - //{ - // // Add Operator - - // RTB_Code.AppendText("00000001 "); // 01 00 00 00 - // RTB_Code.AppendText((writeoffset + i * 4).ToString("X8") + " "); - // RTB_Code.AppendText(BitConverter.ToUInt32(newdata,i*4).ToString("X8") + Environment.NewLine); - //} } private void B_Clear_Click(object sender, EventArgs e) { @@ -132,7 +122,7 @@ namespace PKHeX if (RTB_Code.Text.Length < 1) return; byte[] ncf = new byte[4 + (RTB_Code.Lines.Length-1)*3*4]; - Array.Copy(BitConverter.GetBytes(ncf.Length - 4), ncf, 4); + BitConverter.GetBytes(ncf.Length - 4).CopyTo(ncf, 4); for (int i = 0; i < RTB_Code.Lines.Length-1; i++) { @@ -140,9 +130,9 @@ namespace PKHeX string[] rip = line.Split(new[] {" "}, StringSplitOptions.None); // Write the 3 u32's to an array. - Array.Copy(BitConverter.GetBytes(uint.Parse(rip[0], NumberStyles.HexNumber)),0,ncf,4+i*12+0,4); - Array.Copy(BitConverter.GetBytes(uint.Parse(rip[1], NumberStyles.HexNumber)),0,ncf,4+i*12+4,4); - Array.Copy(BitConverter.GetBytes(uint.Parse(rip[2], NumberStyles.HexNumber)),0,ncf,4+i*12+8,4); + BitConverter.GetBytes(uint.Parse(rip[0], NumberStyles.HexNumber)).CopyTo(ncf,4+i*12+0); + BitConverter.GetBytes(uint.Parse(rip[1], NumberStyles.HexNumber)).CopyTo(ncf,4+i*12+4); + BitConverter.GetBytes(uint.Parse(rip[2], NumberStyles.HexNumber)).CopyTo(ncf,4+i*12+8); } SaveFileDialog sfd = new SaveFileDialog {FileName = "code.bin", Filter = "Code File|*.bin"}; @@ -193,8 +183,8 @@ namespace PKHeX for (int i = 0; i < newcyber.Length - 0x200; i += 4) { // Skip Party and Boxes - if (i == 0x14200) i += 260 * 6 + 4; // +4 to skip over party count - if (i == boxoffset) i += 232 * 30 * 31; + if (i == Main.SAV.Party) i += 260 * 6 + 4; // +4 to skip over party count + if (i == Main.SAV.Box) i += 232 * 30 * 31; if (BitConverter.ToUInt32(cybersav, i) == BitConverter.ToUInt32(newcyber, i)) continue; result += (0x20000000 + i).ToString("X8") + " "; @@ -228,7 +218,7 @@ namespace PKHeX { result += Environment.NewLine + - "--- Segment " + (lines / 128 + 1) + " ---" + + $"--- Segment {lines / 128 + 1} ---" + Environment.NewLine + Environment.NewLine; } if (lines > 10000) goto toomany; @@ -246,7 +236,7 @@ namespace PKHeX { result += Environment.NewLine + - "--- Segment " + (lines / 128 + 1) + " ---" + + $"--- Segment {lines / 128 + 1} ---" + Environment.NewLine + Environment.NewLine; } if (lines > 10000) goto toomany; @@ -269,7 +259,7 @@ namespace PKHeX { result += Environment.NewLine + - "--- Segment " + (lines / 128 + 1) + " ---" + + $"--- Segment {lines / 128 + 1} ---" + Environment.NewLine + Environment.NewLine; } if (lines > 10000) goto toomany; @@ -277,18 +267,13 @@ namespace PKHeX } if ((lines / 128 > 0) && CHK_Break.Checked) - { - Util.Alert($"{1 + lines/128} Code Segments.", - $"{lines} Lines."); - } + Util.Alert($"{1 + lines/128} Code Segments.", $"{lines} Lines."); + RTB_Code.Text = result; return; toomany: - { - Util.Alert("Too many differences detected.", - "Export your save instead."); - } + Util.Alert("Too many differences detected.", "Export your save instead."); } // Import diff --git a/Misc/PKX.cs b/Misc/PKX.cs index f8c38a570..d4f9bd107 100644 --- a/Misc/PKX.cs +++ b/Misc/PKX.cs @@ -343,7 +343,7 @@ namespace PKHeX // Summarize string filename = pk6.Nickname; if (pk6.Nickname != Main.specieslist[pk6.Species] && Main.specieslist[pk6.Species] != null) - filename += " (" + Main.specieslist[pk6.Species] + ")"; + filename += $" ({Main.specieslist[pk6.Species]})"; response[0] = $"{filename} [{Main.abilitylist[pk6.Ability]}] lv{pk6.Stat_Level} @ {Main.itemlist[pk6.HeldItem]} -- {Main.natures[pk6.Nature]}"; response[1] = $"{Main.movelist[pk6.Move1]} / {Main.movelist[pk6.Move2]} / {Main.movelist[pk6.Move3]} / {Main.movelist[pk6.Move4]}"; response[2] = string.Format( @@ -514,7 +514,7 @@ namespace PKHeX internal static uint getPSV(uint PID) { - return Convert.ToUInt16((PID >> 16 ^ PID & 0xFFFF) >> 4); + return (PID >> 16 ^ PID & 0xFFFF) >> 4; } internal static uint getTSV(uint TID, uint SID) { diff --git a/Misc/Util.cs b/Misc/Util.cs index 6c21158cb..36cedfeb9 100644 --- a/Misc/Util.cs +++ b/Misc/Util.cs @@ -471,45 +471,6 @@ namespace PKHeX } return cbList; } - internal static byte[][] unpackMini(byte[] fileData, string ident) - { - using (var s = new MemoryStream(fileData)) - using (var br = new BinaryReader(s)) - { - string fx = new string(br.ReadChars(2)); - - if (fx != ident) return null; - - ushort count = br.ReadUInt16(); - byte[][] returnData = new byte[count][]; - - uint[] offsets = new uint[count + 1]; - for (int i = 0; i < count; i++) - offsets[i] = br.ReadUInt32(); - - uint length = br.ReadUInt32(); - offsets[offsets.Length - 1] = length; - - for (int i = 0; i < count; i++) - { - br.BaseStream.Seek(offsets[i], SeekOrigin.Begin); - using (MemoryStream dataout = new MemoryStream()) - { - byte[] data = new byte[0]; - s.CopyTo(dataout, (int)offsets[i]); - int len = (int)offsets[i + 1] - (int)offsets[i]; - - if (len != 0) - { - data = dataout.ToArray(); - Array.Resize(ref data, len); - } - returnData[i] = data; - } - } - return returnData; - } - } // QR Utility internal static byte[] getQRData() diff --git a/PKX/f1-Main.cs b/PKX/f1-Main.cs index d127b8888..2ac17f7ec 100644 --- a/PKX/f1-Main.cs +++ b/PKX/f1-Main.cs @@ -293,9 +293,7 @@ namespace PKHeX private void mainMenuCodeGen(object sender, EventArgs e) { // Open Code Generator - PK6 formdata = null; - if (verifiedPKX()) formdata = preparepkx(); - CodeGenerator CodeGen = new CodeGenerator(formdata.Data); + CodeGenerator CodeGen = new CodeGenerator(verifiedPKX() ? preparepkx().Data : null); CodeGen.ShowDialog(); byte[] data = CodeGen.returnArray; @@ -793,13 +791,13 @@ namespace PKHeX // Enable Secondary Tools GB_SAVtools.Enabled = B_JPEG.Enabled = true; Menu_ExportSAV.Enabled = B_VerifyCHK.Enabled = SAV.Exportable; - + Menu_CodeGenerator.Enabled = !SAV.ORASDEMO && SAV.Exportable; + setBoxNames(); // Display the Box Names setPKXBoxes(); // Reload all of the PKX Windows // Version Exclusive Editors GB_SUBE.Visible = !SAV.ORAS; - B_OpenSecretBase.Visible = SAV.ORAS; if (SAV.Box > -1) {