From 609db23dc5263b1b31dfaaf9ad42cf10c8d45e17 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 30 Mar 2019 16:10:14 -0700 Subject: [PATCH] Consolidate pouch load/set --- PKHeX.Core/Saves/SAV1.cs | 7 ++----- PKHeX.Core/Saves/SAV2.cs | 10 ++-------- PKHeX.Core/Saves/SAV3.cs | 9 ++------- PKHeX.Core/Saves/SAV3Colosseum.cs | 10 ++-------- PKHeX.Core/Saves/SAV3XD.cs | 10 ++-------- PKHeX.Core/Saves/SAV4.cs | 10 ++-------- PKHeX.Core/Saves/SAV5.cs | 10 ++-------- PKHeX.Core/Saves/SAV6.cs | 10 ++-------- PKHeX.Core/Saves/SAV7.cs | 10 ++-------- PKHeX.Core/Saves/Substructures/Gen7/MyItem7b.cs | 8 ++------ .../Substructures/Inventory/InventoryPouch.cs | 16 ++++++++++++++++ .../Substructures/Inventory/InventoryPouch3.cs | 3 +-- .../Substructures/Inventory/InventoryPouch3GC.cs | 2 +- .../Substructures/Inventory/InventoryPouch4.cs | 3 +-- .../Substructures/Inventory/InventoryPouch7.cs | 2 +- .../Substructures/Inventory/InventoryPouchGB.cs | 4 ++-- 16 files changed, 42 insertions(+), 82 deletions(-) diff --git a/PKHeX.Core/Saves/SAV1.cs b/PKHeX.Core/Saves/SAV1.cs index e746f948c..9c83b7c7c 100644 --- a/PKHeX.Core/Saves/SAV1.cs +++ b/PKHeX.Core/Saves/SAV1.cs @@ -381,14 +381,11 @@ namespace PKHeX.Core new InventoryPouchGB(InventoryType.Items, legalItems, 99, Offsets.Items, 20), new InventoryPouchGB(InventoryType.PCItems, legalItems, 99, Offsets.PCItems, 50) }; - foreach (var p in pouch) - p.GetPouch(Data); - return pouch; + return pouch.LoadAll(Data); } set { - foreach (var p in value) - p.SetPouch(Data); + value.SaveAll(Data); } } diff --git a/PKHeX.Core/Saves/SAV2.cs b/PKHeX.Core/Saves/SAV2.cs index c7297959c..3ff2a5199 100644 --- a/PKHeX.Core/Saves/SAV2.cs +++ b/PKHeX.Core/Saves/SAV2.cs @@ -435,15 +435,9 @@ namespace PKHeX.Core new InventoryPouchGB(InventoryType.Balls, LegalBalls, 99, Offsets.PouchBall, 12), new InventoryPouchGB(InventoryType.PCItems, LegalItems.Concat(LegalKeyItems).Concat(LegalBalls).Concat(LegalTMHMs).ToArray(), 99, Offsets.PouchPC, 50) }; - foreach (var p in pouch) - p.GetPouch(Data); - return pouch; - } - set - { - foreach (var p in value) - p.SetPouch(Data); + return pouch.LoadAll(Data); } + set => value.SaveAll(Data); } private readonly byte[] DaycareFlags = new byte[2]; diff --git a/PKHeX.Core/Saves/SAV3.cs b/PKHeX.Core/Saves/SAV3.cs index 09a377ff1..ea36826f7 100644 --- a/PKHeX.Core/Saves/SAV3.cs +++ b/PKHeX.Core/Saves/SAV3.cs @@ -600,15 +600,10 @@ namespace PKHeX.Core { if (p.Type != InventoryType.PCItems) ((InventoryPouch3)p).SecurityKey = SecurityKey; - p.GetPouch(Data); } - return pouch; - } - set - { - foreach (var p in value) - p.SetPouch(Data); + return pouch.LoadAll(Data); } + set => value.SaveAll(Data); } private int DaycareSlotSize => RS ? SIZE_STORED : SIZE_STORED + 0x3C; // 0x38 mail + 4 exp diff --git a/PKHeX.Core/Saves/SAV3Colosseum.cs b/PKHeX.Core/Saves/SAV3Colosseum.cs index 22495db68..216015e3b 100644 --- a/PKHeX.Core/Saves/SAV3Colosseum.cs +++ b/PKHeX.Core/Saves/SAV3Colosseum.cs @@ -392,15 +392,9 @@ namespace PKHeX.Core new InventoryPouch3GC(InventoryType.Berries, LegalBerries, 999, OFS_PouchBerry, 46), new InventoryPouch3GC(InventoryType.Medicine, LegalCologne, 999, OFS_PouchCologne, 3), // Cologne }; - foreach (var p in pouch) - p.GetPouch(Data); - return pouch; - } - set - { - foreach (var p in value) - p.SetPouch(Data); + return pouch.LoadAll(Data); } + set => value.SaveAll(Data); } // Daycare Structure: diff --git a/PKHeX.Core/Saves/SAV3XD.cs b/PKHeX.Core/Saves/SAV3XD.cs index 8e7ae110e..e228b154e 100644 --- a/PKHeX.Core/Saves/SAV3XD.cs +++ b/PKHeX.Core/Saves/SAV3XD.cs @@ -337,15 +337,9 @@ namespace PKHeX.Core new InventoryPouch3GC(InventoryType.Medicine, LegalCologne, 999, OFS_PouchCologne, 3), // Cologne new InventoryPouch3GC(InventoryType.BattleItems, LegalDisc, 999, OFS_PouchDisc, 60) }; - foreach (var p in pouch) - p.GetPouch(Data); - return pouch; - } - set - { - foreach (var p in value) - p.SetPouch( Data); + return pouch.LoadAll(Data); } + set => value.SaveAll(Data); } // Daycare Structure: diff --git a/PKHeX.Core/Saves/SAV4.cs b/PKHeX.Core/Saves/SAV4.cs index 12ae2681f..c2900178b 100644 --- a/PKHeX.Core/Saves/SAV4.cs +++ b/PKHeX.Core/Saves/SAV4.cs @@ -333,15 +333,9 @@ namespace PKHeX.Core new InventoryPouch4(InventoryType.BattleItems, LegalBattleItems, 999, OFS_BattleItems), new InventoryPouch4(InventoryType.MailItems, LegalMailItems, 999, OFS_MailItems), }; - foreach (var p in pouch) - p.GetPouch(Data); - return pouch; - } - set - { - foreach (var p in value) - p.SetPouch(Data); + return pouch.LoadAll(Data); } + set => value.SaveAll(Data); } // Storage diff --git a/PKHeX.Core/Saves/SAV5.cs b/PKHeX.Core/Saves/SAV5.cs index d5e4e8f72..d63f24b15 100644 --- a/PKHeX.Core/Saves/SAV5.cs +++ b/PKHeX.Core/Saves/SAV5.cs @@ -201,15 +201,9 @@ namespace PKHeX.Core new InventoryPouch4(InventoryType.Medicine, LegalMedicine, 999, OFS_PouchMedicine), new InventoryPouch4(InventoryType.Berries, LegalBerries, 999, OFS_PouchBerry), }; - foreach (var p in pouch) - p.GetPouch(Data); - return pouch; - } - set - { - foreach (var p in value) - p.SetPouch(Data); + return pouch.LoadAll(Data); } + set => value.SaveAll(Data); } // Storage diff --git a/PKHeX.Core/Saves/SAV6.cs b/PKHeX.Core/Saves/SAV6.cs index 300ea8179..5c9a399f3 100644 --- a/PKHeX.Core/Saves/SAV6.cs +++ b/PKHeX.Core/Saves/SAV6.cs @@ -732,15 +732,9 @@ namespace PKHeX.Core new InventoryPouch4(InventoryType.Medicine, legalMedicine, 999, OFS_PouchMedicine), new InventoryPouch4(InventoryType.Berries, Legal.Pouch_Berry_XY, 999, OFS_PouchBerry), }; - foreach (var p in pouch) - p.GetPouch(Data); - return pouch; - } - set - { - foreach (var p in value) - p.SetPouch(Data); + return pouch.LoadAll(Data); } + set => value.SaveAll(Data); } // Storage diff --git a/PKHeX.Core/Saves/SAV7.cs b/PKHeX.Core/Saves/SAV7.cs index d2c89da00..59be9b7a9 100644 --- a/PKHeX.Core/Saves/SAV7.cs +++ b/PKHeX.Core/Saves/SAV7.cs @@ -837,15 +837,9 @@ namespace PKHeX.Core get { var bag = GetPouches(); - foreach (var p in bag) - p.GetPouch(Data); - return bag; - } - set - { - foreach (var p in value) - p.SetPouch(Data); + return bag.LoadAll(Data); } + set => value.SaveAll(Data); } private InventoryPouch[] GetPouches() diff --git a/PKHeX.Core/Saves/Substructures/Gen7/MyItem7b.cs b/PKHeX.Core/Saves/Substructures/Gen7/MyItem7b.cs index 712f75bfb..19e226f9c 100644 --- a/PKHeX.Core/Saves/Substructures/Gen7/MyItem7b.cs +++ b/PKHeX.Core/Saves/Substructures/Gen7/MyItem7b.cs @@ -26,17 +26,13 @@ namespace PKHeX.Core new InventoryPouch7b(InventoryType.ZCrystals, Legal.Pouch_PowerUp_GG, 999, PowerUp, PouchSize7b.PowerUp), new InventoryPouch7b(InventoryType.Candy, Legal.Pouch_Candy_GG, 999, Candy, PouchSize7b.Candy), }; - foreach (var p in pouch) - p.GetPouch(Data); - return pouch; + return pouch.LoadAll(Data); } set { foreach (var p in value) - { ((InventoryPouch7b)p).SanitizeCounts(); - p.SetPouch(Data); - } + value.SaveAll(Data); } } } diff --git a/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch.cs b/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch.cs index 886a28d84..2cb854ae1 100644 --- a/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch.cs +++ b/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch.cs @@ -221,4 +221,20 @@ namespace PKHeX.Core return Math.Min(MaxCount, requestVal); } } + + public static class InventoryPouchExtensions + { + public static InventoryPouch[] LoadAll(this InventoryPouch[] value, byte[] Data) + { + foreach (var p in value) + p.GetPouch(Data); + return value; + } + + public static void SaveAll(this InventoryPouch[] value, byte[] Data) + { + foreach (var p in value) + p.SetPouch(Data); + } + } } \ No newline at end of file diff --git a/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch3.cs b/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch3.cs index 310859c53..975a471da 100644 --- a/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch3.cs +++ b/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch3.cs @@ -9,12 +9,11 @@ namespace PKHeX.Core public InventoryPouch3(InventoryType type, ushort[] legal, int maxcount, int offset, int size) : base(type, legal, maxcount, offset, size) { - } public override void GetPouch(byte[] Data) { - InventoryItem[] items = new InventoryItem[PouchDataSize]; + var items = new InventoryItem[PouchDataSize]; for (int i = 0; i it == t.Index)) + if (LegalItems.All(z => z != t.Index)) continue; int index = Offset + Array.FindIndex(LegalItems, it => t.Index == it); Data[index] = (byte)t.Count;