From 300ef4d60abcc04eea9f9c82ee2d79cccd7a7db3 Mon Sep 17 00:00:00 2001 From: Kurt Date: Mon, 29 Apr 2019 17:21:19 -0700 Subject: [PATCH] Minor simplifications Use "SetData" method to automatically apply the Edited flag Use appropriate method for CopyTo simplify expressions a tad --- PKHeX.Core/Saves/SAV4.cs | 12 +++++++++--- PKHeX.Core/Saves/SAV5.cs | 8 +++++--- PKHeX.Core/Saves/SAV6.cs | 7 ++----- PKHeX.Core/Saves/SAV7.cs | 11 ++++------- .../Subforms/Save Editors/Gen7/SAV_Trainer7GG.cs | 2 +- .../Subforms/Save Editors/SAV_EventFlags.cs | 3 +-- PKHeX.WinForms/Util/SpriteUtil.cs | 4 ++-- 7 files changed, 24 insertions(+), 23 deletions(-) diff --git a/PKHeX.Core/Saves/SAV4.cs b/PKHeX.Core/Saves/SAV4.cs index 79eb755fa..3c1cd58bf 100644 --- a/PKHeX.Core/Saves/SAV4.cs +++ b/PKHeX.Core/Saves/SAV4.cs @@ -791,8 +791,7 @@ protected override bool[] MysteryGiftReceivedFlags data[i >> 3] |= (byte)(1 << (i & 7)); } - data.CopyTo(Data, WondercardFlags); - Edited = true; + SetData(data, WondercardFlags); } } @@ -1302,6 +1301,13 @@ public override byte[] SetString(string value, int maxLength, int PadToSize = 0, public byte[] SealCase { get => GetData(Seal, (int) Seal4.MAX); set => SetData(value, Seal); } public byte GetSealCount(Seal4 id) => Data[Seal + (int)id]; public byte SetSealCount(Seal4 id, byte count) => Data[Seal + (int)id] = Math.Min(SealMaxCount, count); - public void SetAllSeals(byte count, bool unreleased = false) => Enumerable.Repeat(Math.Min(SealMaxCount, count), (int)(unreleased ? Seal4.MAX : Seal4.MAXLEGAL)).CopyTo(Data, Seal); + + public void SetAllSeals(byte count, bool unreleased = false) + { + var sealIndexCount = (int)(unreleased ? Seal4.MAX : Seal4.MAXLEGAL); + var val = Math.Min(count, SealMaxCount); + for (int i = 0; i < sealIndexCount; i++) + Data[Seal + i] = val; + } } } diff --git a/PKHeX.Core/Saves/SAV5.cs b/PKHeX.Core/Saves/SAV5.cs index d63f24b15..95b9b1033 100644 --- a/PKHeX.Core/Saves/SAV5.cs +++ b/PKHeX.Core/Saves/SAV5.cs @@ -227,7 +227,7 @@ public override string GetBoxName(int box) { if (box >= BoxCount) return string.Empty; - return Util.TrimFromFFFF(Encoding.Unicode.GetString(Data, PCLayout + (0x28 * box) + 4, 0x28)); + return Util.TrimFromFFFF(Encoding.Unicode.GetString(Data, GetBoxNameOffset(box), 0x28)); } public override void SetBoxName(int box, string value) @@ -235,10 +235,12 @@ public override void SetBoxName(int box, string value) if (value.Length > 38) return; value += '\uFFFF'; - Encoding.Unicode.GetBytes(value.PadRight(0x14, '\0')).CopyTo(Data, PCLayout + (0x28 * box) + 4); - Edited = true; + var data = Encoding.Unicode.GetBytes(value.PadRight(0x14, '\0')); + SetData(data, GetBoxNameOffset(box)); } + private int GetBoxNameOffset(int box) => PCLayout + (0x28 * box) + 4; + protected override int GetBoxWallpaperOffset(int box) { return PCLayout + 0x3C4 + box; diff --git a/PKHeX.Core/Saves/SAV6.cs b/PKHeX.Core/Saves/SAV6.cs index 050b960f0..ed8c311ab 100644 --- a/PKHeX.Core/Saves/SAV6.cs +++ b/PKHeX.Core/Saves/SAV6.cs @@ -990,8 +990,7 @@ protected override bool[] MysteryGiftReceivedFlags data[i >> 3] |= (byte)(1 << (i & 7)); } - data.CopyTo(Data, WondercardFlags); - Edited = true; + SetData(data, WondercardFlags); } } @@ -1056,9 +1055,7 @@ private void SetWC6(MysteryGift wc6, int index) if (index < 0 || index > GiftCountMax) return; - wc6.Data.CopyTo(Data, WondercardData + (index * WC6.Size)); - - Edited = true; + SetData(wc6.Data, WondercardData + (index * WC6.Size)); } // Gym History diff --git a/PKHeX.Core/Saves/SAV7.cs b/PKHeX.Core/Saves/SAV7.cs index d47da776f..a31ddcf46 100644 --- a/PKHeX.Core/Saves/SAV7.cs +++ b/PKHeX.Core/Saves/SAV7.cs @@ -957,8 +957,8 @@ public override string GetBoxName(int box) public override void SetBoxName(int box, string value) { - Encoding.Unicode.GetBytes(value.PadRight(0x11, '\0')).CopyTo(Data, PCLayout + (0x22 * box)); - Edited = true; + var data = Encoding.Unicode.GetBytes(value.PadRight(0x11, '\0')); + SetData(data, PCLayout + (0x22 * box)); } protected override PKM GetPKM(byte[] data) @@ -1427,8 +1427,7 @@ protected override bool[] MysteryGiftReceivedFlags data[i>>3] |= (byte)(1 << (i&7)); } - data.CopyTo(Data, WondercardFlags); - Edited = true; + SetData(data, WondercardFlags); } } @@ -1475,9 +1474,7 @@ private void SetWC7(MysteryGift wc7, int index) if (index < 0 || index > GiftCountMax) return; - wc7.Data.CopyTo(Data, WondercardData + (index * WC7.Size)); - - Edited = true; + SetData(wc7.Data, WondercardData + (index * WC7.Size)); } // Writeback Validity diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7GG.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7GG.cs index fc55d8ab2..d8e5381ae 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7GG.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7GG.cs @@ -196,7 +196,7 @@ private void ImportGP1From(string path, int index) return; } var gp1 = new GP1(); - data.CopyTo(gp1.Data); + data.CopyTo(gp1.Data, 0); Park[index] = gp1; UpdateGoSummary((int)NUD_GoIndex.Value); } diff --git a/PKHeX.WinForms/Subforms/Save Editors/SAV_EventFlags.cs b/PKHeX.WinForms/Subforms/Save Editors/SAV_EventFlags.cs index ece97ccd4..e6b56e502 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/SAV_EventFlags.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/SAV_EventFlags.cs @@ -91,8 +91,7 @@ private void B_Save_Click(object sender, EventArgs e) // Copy back Constants ChangeConstantIndex(null, EventArgs.Empty); // Trigger Saving SAV.EventConsts = Constants; - SAV.Data.CopyTo(Origin.Data, 0); - Origin.Edited = true; + SAV.SetData(Origin.Data, 0); Close(); } diff --git a/PKHeX.WinForms/Util/SpriteUtil.cs b/PKHeX.WinForms/Util/SpriteUtil.cs index b83f1959b..924ab617a 100644 --- a/PKHeX.WinForms/Util/SpriteUtil.cs +++ b/PKHeX.WinForms/Util/SpriteUtil.cs @@ -113,7 +113,7 @@ private static Image GetSprite(SaveFile sav) private static Image GetWallpaper(SaveFile sav, int box) { string s = BoxWallpaper.GetWallpaperResourceName(sav.Version, sav.GetBoxWallpaper(box)); - return (Bitmap)(Resources.ResourceManager.GetObject(s) ?? Resources.box_wp16xy); + return (Bitmap)Resources.ResourceManager.GetObject(s) ?? Resources.box_wp16xy; } private static Image GetSprite(PKM pk, SaveFile sav, int box, int slot, bool flagIllegal = false) @@ -159,7 +159,7 @@ private static Image GetSprite(PKM pk, SaveFile sav, int box, int slot, bool fla private const int SlotTeamShiftX = SpriteWidth - 19; private const int FlagIllegalShiftY = SpriteHeight - 16; - private static readonly Image[] PartyMarks = + private static readonly Bitmap[] PartyMarks = { Resources.party1, Resources.party2, Resources.party3, Resources.party4, Resources.party5, Resources.party6, };