From c8ec63992bd952cd8b88883c1b0fa0fc354b00ee Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 23 Feb 2025 15:50:17 -0600 Subject: [PATCH] Rework entity import settings param passing Closes #4418 Introduce a readonly record struct to contain all the settings selected. Add simple static get for none/all to disable/force the updates. Split the UpdateRecord behavior out of UpdatePKM (adapt to save file) so that it behaves similarly to UpdateDex. Only AdaptToSaveFile when opening a file in the PKM Editor. --- .../Editing/Saves/Slots/Info/ISlotInfo.cs | 4 +- .../Editing/Saves/Slots/Info/SlotInfoBox.cs | 4 +- .../Editing/Saves/Slots/Info/SlotInfoFile.cs | 2 +- .../Editing/Saves/Slots/Info/SlotInfoMisc.cs | 6 +- .../Editing/Saves/Slots/Info/SlotInfoParty.cs | 4 +- .../Editing/Saves/Slots/SlotChangelog.cs | 2 +- PKHeX.Core/Editing/Saves/Slots/SlotEditor.cs | 11 ++- PKHeX.Core/PersonalInfo/PersonalTable.cs | 2 +- PKHeX.Core/Saves/SAV6.cs | 4 +- PKHeX.Core/Saves/SAV7.cs | 4 +- PKHeX.Core/Saves/SAV8BS.cs | 4 +- PKHeX.Core/Saves/SAV8SWSH.cs | 4 +- PKHeX.Core/Saves/SAV9SV.cs | 4 +- PKHeX.Core/Saves/SaveFile.cs | 91 +++++++++++-------- .../Saves/Storage/EntityImportSettings.cs | 28 ++++++ PKHeX.Core/Saves/Storage/PKMImportSetting.cs | 22 ----- PKHeX.Core/Saves/Util/BoxUtil.cs | 24 ++--- PKHeX.Core/Saves/Util/SaveExtensions.cs | 8 +- .../Controls/SAV Editor/SAVEditor.cs | 29 +++--- PKHeX.WinForms/MainWindow/Main.cs | 10 +- .../Subforms/PKM Editors/BatchEditor.cs | 4 +- PKHeX.WinForms/Subforms/SAV_Database.cs | 4 +- PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs | 6 +- 23 files changed, 152 insertions(+), 129 deletions(-) create mode 100644 PKHeX.Core/Saves/Storage/EntityImportSettings.cs delete mode 100644 PKHeX.Core/Saves/Storage/PKMImportSetting.cs diff --git a/PKHeX.Core/Editing/Saves/Slots/Info/ISlotInfo.cs b/PKHeX.Core/Editing/Saves/Slots/Info/ISlotInfo.cs index 470bb7b40..03c1854f3 100644 --- a/PKHeX.Core/Editing/Saves/Slots/Info/ISlotInfo.cs +++ b/PKHeX.Core/Editing/Saves/Slots/Info/ISlotInfo.cs @@ -35,9 +35,9 @@ public interface ISlotInfo /// /// Save file to try writing to. /// Entity data to try writing. - /// Setting to use when importing the data + /// Setting to use when importing the data /// Returns false if it did not succeed. - bool WriteTo(SaveFile sav, PKM pk, PKMImportSetting setting = PKMImportSetting.UseDefault); + bool WriteTo(SaveFile sav, PKM pk, EntityImportSettings settings = default); /// /// Reads a from the . diff --git a/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoBox.cs b/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoBox.cs index 470572bff..4e9d5e412 100644 --- a/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoBox.cs +++ b/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoBox.cs @@ -9,9 +9,9 @@ public sealed record SlotInfoBox(int Box, int Slot) : ISlotInfo public bool CanWriteTo(SaveFile sav) => sav.HasBox && !sav.IsBoxSlotLocked(Box, Slot); public WriteBlockedMessage CanWriteTo(SaveFile sav, PKM pk) => WriteBlockedMessage.None; - public bool WriteTo(SaveFile sav, PKM pk, PKMImportSetting setting = PKMImportSetting.UseDefault) + public bool WriteTo(SaveFile sav, PKM pk, EntityImportSettings settings = default) { - sav.SetBoxSlotAtIndex(pk, Box, Slot, setting, setting); + sav.SetBoxSlotAtIndex(pk, Box, Slot, settings); return true; } diff --git a/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoFile.cs b/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoFile.cs index 6d43c5e1a..40e81c706 100644 --- a/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoFile.cs +++ b/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoFile.cs @@ -11,6 +11,6 @@ public sealed record SlotInfoFile(string Path) : ISlotInfo public bool CanWriteTo(SaveFile sav) => false; public WriteBlockedMessage CanWriteTo(SaveFile sav, PKM pk) => WriteBlockedMessage.InvalidDestination; - public bool WriteTo(SaveFile sav, PKM pk, PKMImportSetting setting = PKMImportSetting.UseDefault) => false; + public bool WriteTo(SaveFile sav, PKM pk, EntityImportSettings settings = default) => false; public PKM Read(SaveFile sav) => sav.BlankPKM; } diff --git a/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoMisc.cs b/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoMisc.cs index 60abbe987..17701f8a2 100644 --- a/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoMisc.cs +++ b/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoMisc.cs @@ -11,13 +11,13 @@ public sealed record SlotInfoMisc(Memory Data, int Slot, bool PartyFormat public bool CanWriteTo(SaveFile sav) => Mutable; public WriteBlockedMessage CanWriteTo(SaveFile sav, PKM pk) => Mutable ? WriteBlockedMessage.None : WriteBlockedMessage.InvalidDestination; - public bool WriteTo(SaveFile sav, PKM pk, PKMImportSetting setting = PKMImportSetting.UseDefault) + public bool WriteTo(SaveFile sav, PKM pk, EntityImportSettings settings = default) { var span = Data.Span; if (PartyFormat) - sav.SetSlotFormatParty(pk, span, setting, setting); + sav.SetSlotFormatParty(pk, span, settings); else - sav.SetSlotFormatStored(pk, span, setting, setting); + sav.SetSlotFormatStored(pk, span, settings); return true; } diff --git a/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoParty.cs b/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoParty.cs index af9778c6d..c6a4710b5 100644 --- a/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoParty.cs +++ b/PKHeX.Core/Editing/Saves/Slots/Info/SlotInfoParty.cs @@ -15,7 +15,7 @@ public WriteBlockedMessage CanWriteTo(SaveFile sav, PKM pk) => pk.IsEgg && sav.I ? WriteBlockedMessage.InvalidPartyConfiguration : WriteBlockedMessage.None; - public bool WriteTo(SaveFile sav, PKM pk, PKMImportSetting setting = PKMImportSetting.UseDefault) + public bool WriteTo(SaveFile sav, PKM pk, EntityImportSettings settings = default) { if (pk.Species == 0) { @@ -24,7 +24,7 @@ public bool WriteTo(SaveFile sav, PKM pk, PKMImportSetting setting = PKMImportSe return true; } Slot = Math.Min(Slot, sav.PartyCount); // realign if necessary - sav.SetPartySlotAtIndex(pk, Slot, setting, setting); + sav.SetPartySlotAtIndex(pk, Slot, settings); return true; } diff --git a/PKHeX.Core/Editing/Saves/Slots/SlotChangelog.cs b/PKHeX.Core/Editing/Saves/Slots/SlotChangelog.cs index 9135bc00d..9cd103fba 100644 --- a/PKHeX.Core/Editing/Saves/Slots/SlotChangelog.cs +++ b/PKHeX.Core/Editing/Saves/Slots/SlotChangelog.cs @@ -71,6 +71,6 @@ private sealed class SingleSlotReversion(ISlotInfo info, PKM Entity) : SlotRever { public SingleSlotReversion(ISlotInfo info, SaveFile sav) : this(info, info.Read(sav)) { } - public override void Revert(SaveFile sav) => Info.WriteTo(sav, Entity, PKMImportSetting.Skip); + public override void Revert(SaveFile sav) => Info.WriteTo(sav, Entity, EntityImportSettings.None); } } diff --git a/PKHeX.Core/Editing/Saves/Slots/SlotEditor.cs b/PKHeX.Core/Editing/Saves/Slots/SlotEditor.cs index 9df69b017..79ac1fc0c 100644 --- a/PKHeX.Core/Editing/Saves/Slots/SlotEditor.cs +++ b/PKHeX.Core/Editing/Saves/Slots/SlotEditor.cs @@ -68,16 +68,16 @@ public SlotTouchResult Swap(ISlotInfo source, ISlotInfo dest) if (!dest.CanWriteTo(SAV)) return SlotTouchResult.FailDestination; - const PKMImportSetting skip = PKMImportSetting.Skip; + var settings = EntityImportSettings.None; var s = source.Read(SAV); var d = dest.Read(SAV); - WriteSlot(source, s, SlotTouchType.None, skip); - WriteSlot(dest, d, SlotTouchType.Swap, skip); + WriteSlot(source, s, SlotTouchType.None, settings); + WriteSlot(dest, d, SlotTouchType.Swap, settings); return SlotTouchResult.Success; } - private bool WriteSlot(ISlotInfo slot, PKM pk, SlotTouchType type = SlotTouchType.Set, PKMImportSetting setDetail = PKMImportSetting.UseDefault) + private bool WriteSlot(ISlotInfo slot, PKM pk, SlotTouchType type = SlotTouchType.Set, EntityImportSettings setDetail = default) { Changelog.AddNewChange(slot); var result = slot.WriteTo(SAV, pk, setDetail); @@ -89,7 +89,8 @@ private bool WriteSlot(ISlotInfo slot, PKM pk, SlotTouchType type = SlotTouchTyp private bool DeleteSlot(ISlotInfo slot) { var pk = SAV.BlankPKM; - return WriteSlot(slot, pk, SlotTouchType.Delete, PKMImportSetting.Skip); + var settings = EntityImportSettings.None; + return WriteSlot(slot, pk, SlotTouchType.Delete, settings); } public void Undo() diff --git a/PKHeX.Core/PersonalInfo/PersonalTable.cs b/PKHeX.Core/PersonalInfo/PersonalTable.cs index 9cd709371..1323daab7 100644 --- a/PKHeX.Core/PersonalInfo/PersonalTable.cs +++ b/PKHeX.Core/PersonalInfo/PersonalTable.cs @@ -130,7 +130,7 @@ public static class PersonalTable private static void PopulateGen3Tutors() { - // Update Gen3 data with Emerald's data, FR/LG is a subset of Emerald's compatibility. + // Enable Gen3 data with Emerald's data, FR/LG is a subset of Emerald's compatibility. var machine = BinLinkerAccessor.Get(Util.GetBinaryResource("hmtm_g3.pkl"), "g3"u8); var tutors = BinLinkerAccessor.Get(Util.GetBinaryResource("tutors_g3.pkl"), "g3"u8); E.LoadTables(machine, tutors); diff --git a/PKHeX.Core/Saves/SAV6.cs b/PKHeX.Core/Saves/SAV6.cs index 8a0ed80b4..ec4cfe125 100644 --- a/PKHeX.Core/Saves/SAV6.cs +++ b/PKHeX.Core/Saves/SAV6.cs @@ -104,10 +104,10 @@ protected override void SetPKM(PKM pk, bool isParty = false) } pk.RefreshChecksum(); - if (SetUpdateRecords != PKMImportSetting.Skip) - AddCountAcquired(pk); } + protected override void SetRecord(PKM pk) => AddCountAcquired(pk); + private void AddCountAcquired(PKM pk) { Records.AddRecord(pk.WasEgg ? 009 : 007); // egg, capture diff --git a/PKHeX.Core/Saves/SAV7.cs b/PKHeX.Core/Saves/SAV7.cs index 71ac15763..04f338ca4 100644 --- a/PKHeX.Core/Saves/SAV7.cs +++ b/PKHeX.Core/Saves/SAV7.cs @@ -165,10 +165,10 @@ protected override void SetPKM(PKM pk, bool isParty = false) pk7.FormArgumentRemain = (byte)GetFormArgument(pk); pk.RefreshChecksum(); - if (SetUpdateRecords != PKMImportSetting.Skip) - AddCountAcquired(pk); } + protected override void SetRecord(PKM pk) => AddCountAcquired(pk); + private void AddCountAcquired(PKM pk) { Records.AddRecord(pk.WasEgg ? 008 : 006); // egg, capture diff --git a/PKHeX.Core/Saves/SAV8BS.cs b/PKHeX.Core/Saves/SAV8BS.cs index fab1ea676..9c80d39dc 100644 --- a/PKHeX.Core/Saves/SAV8BS.cs +++ b/PKHeX.Core/Saves/SAV8BS.cs @@ -312,10 +312,10 @@ protected override void SetPKM(PKM pk, bool isParty = false) pb8.UpdateHandler(this); pb8.RefreshChecksum(); - if (SetUpdateRecords != PKMImportSetting.Skip) - AddCountAcquired(pk); } + protected override void SetRecord(PKM pk) => AddCountAcquired(pk); + private void AddCountAcquired(PKM pk) { // There aren't many records, and they only track Capture/Fish/Hatch/Defeat. diff --git a/PKHeX.Core/Saves/SAV8SWSH.cs b/PKHeX.Core/Saves/SAV8SWSH.cs index b7330d47f..f13dd4367 100644 --- a/PKHeX.Core/Saves/SAV8SWSH.cs +++ b/PKHeX.Core/Saves/SAV8SWSH.cs @@ -198,10 +198,10 @@ protected override void SetPKM(PKM pk, bool isParty = false) } pk8.RefreshChecksum(); - if (SetUpdateRecords != PKMImportSetting.Skip) - AddCountAcquired(pk8); } + protected override void SetRecord(PKM pk) => AddCountAcquired(pk); + private static uint GetFormArgument(PKM pk) { if (pk.Form == 0) diff --git a/PKHeX.Core/Saves/SAV9SV.cs b/PKHeX.Core/Saves/SAV9SV.cs index c643e9715..7d725377f 100644 --- a/PKHeX.Core/Saves/SAV9SV.cs +++ b/PKHeX.Core/Saves/SAV9SV.cs @@ -205,10 +205,10 @@ protected override void SetPKM(PKM pk, bool isParty = false) } pk9.RefreshChecksum(); - if (SetUpdateRecords != PKMImportSetting.Skip) - AddCountAcquired(pk9); } + protected override void SetRecord(PKM pk) => AddCountAcquired(pk); + private static uint GetFormArgument(PKM pk) { if (pk.Form == 0) diff --git a/PKHeX.Core/Saves/SaveFile.cs b/PKHeX.Core/Saves/SaveFile.cs index 21f495607..649351392 100644 --- a/PKHeX.Core/Saves/SaveFile.cs +++ b/PKHeX.Core/Saves/SaveFile.cs @@ -1,6 +1,8 @@ +using Microsoft.VisualBasic.FileIO; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.IO; using System.Linq; namespace PKHeX.Core; @@ -212,7 +214,7 @@ public IList PartyData private Span GetPartySpan(int index) => PartyBuffer[GetPartyOffset(index)..]; public PKM GetPartySlotAtIndex(int index) => GetPartySlot(GetPartySpan(index)); - public void SetPartySlotAtIndex(PKM pk, int index, PKMImportSetting trade = PKMImportSetting.UseDefault, PKMImportSetting dex = PKMImportSetting.UseDefault) + public void SetPartySlotAtIndex(PKM pk, int index, EntityImportSettings settings = default) { // update party count if ((uint)index > 5) @@ -229,37 +231,38 @@ public void SetPartySlotAtIndex(PKM pk, int index, PKMImportSetting trade = PKMI PartyCount = index; } - SetPartySlot(pk, GetPartySpan(index), trade, dex); + SetPartySlot(pk, GetPartySpan(index), settings); } - public void SetSlotFormatParty(PKM pk, Span data, PKMImportSetting trade = PKMImportSetting.UseDefault, PKMImportSetting dex = PKMImportSetting.UseDefault) + public void SetSlotFormatParty(PKM pk, Span data, EntityImportSettings settings = default) { if (pk.GetType() != PKMType) throw new ArgumentException($"PKM Format needs to be {PKMType} when setting to this Save File."); - UpdatePKM(pk, isParty: true, trade, dex); + UpdatePKM(pk, isParty: true, settings); SetPartyValues(pk, isParty: true); WritePartySlot(pk, data); } - public void SetSlotFormatStored(PKM pk, Span data, PKMImportSetting trade = PKMImportSetting.UseDefault, PKMImportSetting dex = PKMImportSetting.UseDefault) + public void SetSlotFormatStored(PKM pk, Span data, EntityImportSettings settings = default) { if (pk.GetType() != PKMType) throw new ArgumentException($"PKM Format needs to be {PKMType} when setting to this Save File."); - UpdatePKM(pk, isParty: false, trade, dex); + UpdatePKM(pk, isParty: false, settings); SetPartyValues(pk, isParty: false); WriteSlotFormatStored(pk, data); } - public void SetPartySlot(PKM pk, Span data, PKMImportSetting trade = PKMImportSetting.UseDefault, PKMImportSetting dex = PKMImportSetting.UseDefault) => SetSlotFormatParty(pk, data, trade, dex); + public void SetPartySlot(PKM pk, Span data, EntityImportSettings settings = default) + => SetSlotFormatParty(pk, data, settings); - public void SetBoxSlot(PKM pk, Span data, PKMImportSetting trade = PKMImportSetting.UseDefault, PKMImportSetting dex = PKMImportSetting.UseDefault) + public void SetBoxSlot(PKM pk, Span data, EntityImportSettings settings = default) { if (pk.GetType() != PKMType) throw new ArgumentException($"PKM Format needs to be {PKMType} when setting to this Save File."); - UpdatePKM(pk, isParty: false, trade, dex); + UpdatePKM(pk, isParty: false, settings); SetPartyValues(pk, isParty: false); WriteBoxSlot(pk, data); } @@ -273,16 +276,17 @@ public void DeletePartySlot(int slot) for (int i = slot + 1; i <= newEmpty; i++) // Slide slots down { var current = GetPartySlotAtIndex(i); - SetPartySlotAtIndex(current, i - 1, PKMImportSetting.Skip, PKMImportSetting.Skip); + SetPartySlotAtIndex(current, i - 1, EntityImportSettings.None); } - SetPartySlotAtIndex(BlankPKM, newEmpty, PKMImportSetting.Skip, PKMImportSetting.Skip); + SetPartySlotAtIndex(BlankPKM, newEmpty, EntityImportSettings.None); // PartyCount will automatically update via above call. Do not adjust. } #region Slot Storing - public static PKMImportSetting SetUpdateDex { protected get; set; } = PKMImportSetting.Update; - public static PKMImportSetting SetUpdatePKM { protected get; set; } = PKMImportSetting.Update; - public static PKMImportSetting SetUpdateRecords { protected get; set; } = PKMImportSetting.Update; + public static EntityImportOption SetUpdateDex { protected get; set; } = EntityImportOption.Enable; + public static EntityImportOption SetUpdatePKM { protected get; set; } = EntityImportOption.Enable; + public static EntityImportOption SetUpdateRecords { protected get; set; } = EntityImportOption.Enable; + public static EntityImportSettings SetUpdateSettings => new(SetUpdatePKM, SetUpdateDex, SetUpdateRecords); public abstract Type PKMType { get; } protected abstract PKM GetPKM(byte[] data); @@ -321,41 +325,52 @@ protected virtual void SetPartyValues(PKM pk, bool isParty) pk.ResetPartyStats(); } + protected void UpdatePKM(PKM pk, bool isParty, EntityImportSettings settings = default) + { + if (IsUpdateAdapt(settings.UpdateToSaveFile)) + SetPKM(pk, isParty); + if (IsUpdateDex(settings.UpdatePokeDex)) + SetDex(pk); + if (IsUpdateRecord(settings.UpdateRecord)) + SetRecord(pk); + } + /// /// Conditions a for this save file as if it was traded to it. /// /// Entity to adapt - /// Entity exists in party format - /// Setting on whether to adapt - public void AdaptPKM(PKM pk, bool party = true, PKMImportSetting trade = PKMImportSetting.UseDefault) + /// Entity exists in party format + /// Setting on whether to adapt + public void AdaptToSaveFile(PKM pk, bool isParty = true, EntityImportOption option = EntityImportOption.UseDefault) { - if (GetTradeUpdateSetting(trade)) - SetPKM(pk, party); + if (IsUpdateAdapt(option)) + SetPKM(pk, isParty); } - protected void UpdatePKM(PKM pk, bool isParty, PKMImportSetting trade, PKMImportSetting dex) + private static bool IsUpdateAdapt(EntityImportOption option = EntityImportOption.UseDefault) { - AdaptPKM(pk, isParty, trade); - if (GetDexUpdateSetting(dex)) - SetDex(pk); + if (option == EntityImportOption.UseDefault) + option = SetUpdatePKM; + return option == EntityImportOption.Enable; } - private static bool GetTradeUpdateSetting(PKMImportSetting trade = PKMImportSetting.UseDefault) + private static bool IsUpdateDex(EntityImportOption option = EntityImportOption.UseDefault) { - if (trade == PKMImportSetting.UseDefault) - trade = SetUpdatePKM; - return trade == PKMImportSetting.Update; + if (option == EntityImportOption.UseDefault) + option = SetUpdateDex; + return option == EntityImportOption.Enable; } - private static bool GetDexUpdateSetting(PKMImportSetting trade = PKMImportSetting.UseDefault) + private static bool IsUpdateRecord(EntityImportOption option = EntityImportOption.UseDefault) { - if (trade == PKMImportSetting.UseDefault) - trade = SetUpdateDex; - return trade == PKMImportSetting.Update; + if (option == EntityImportOption.UseDefault) + option = SetUpdateDex; + return option == EntityImportOption.Enable; } protected virtual void SetPKM(PKM pk, bool isParty = false) { } protected virtual void SetDex(PKM pk) { } + protected virtual void SetRecord(PKM pk) { } #endregion #region Pokédex @@ -557,11 +572,11 @@ public int GetBoxSlotOffset(int index) return GetBoxSlotOffset(box, slot); } - public void SetBoxSlotAtIndex(PKM pk, int box, int slot, PKMImportSetting trade = PKMImportSetting.UseDefault, PKMImportSetting dex = PKMImportSetting.UseDefault) - => SetBoxSlot(pk, BoxBuffer[GetBoxSlotOffset(box, slot)..], trade, dex); + public void SetBoxSlotAtIndex(PKM pk, int box, int slot, EntityImportSettings settings = default) + => SetBoxSlot(pk, BoxBuffer[GetBoxSlotOffset(box, slot)..], settings); - public void SetBoxSlotAtIndex(PKM pk, int index, PKMImportSetting trade = PKMImportSetting.UseDefault, PKMImportSetting dex = PKMImportSetting.UseDefault) - => SetBoxSlot(pk, BoxBuffer[GetBoxSlotOffset(index)..], trade, dex); + public void SetBoxSlotAtIndex(PKM pk, int index, EntityImportSettings settings = default) + => SetBoxSlot(pk, BoxBuffer[GetBoxSlotOffset(index)..], settings); #endregion #region Storage Manipulations @@ -685,10 +700,11 @@ public int SortBoxes(int BoxStart = 0, int BoxEnd = -1, Func, i SlotPointerUtil.UpdateRepointFrom(boxclone, BD, 0, SlotPointers); + var settings = EntityImportSettings.None; for (int i = 0; i < boxclone.Length; i++) { var pk = boxclone[i]; - SetBoxSlotAtIndex(pk, i, PKMImportSetting.Skip, PKMImportSetting.Skip); + SetBoxSlotAtIndex(pk, i, settings); } return count; } @@ -754,6 +770,7 @@ public int ModifyBoxes(Action action, int BoxStart = 0, int BoxEnd = -1) var storage = BoxBuffer; int modified = 0; + var settings = EntityImportSettings.None; for (int b = BoxStart; b <= BoxEnd; b++) { for (int s = 0; s < BoxSlotCount; s++) @@ -767,7 +784,7 @@ public int ModifyBoxes(Action action, int BoxStart = 0, int BoxEnd = -1) var pk = GetBoxSlotAtIndex(b, s); action(pk); ++modified; - SetBoxSlot(pk, dest, PKMImportSetting.Skip, PKMImportSetting.Skip); + SetBoxSlot(pk, dest, settings); } } return modified; diff --git a/PKHeX.Core/Saves/Storage/EntityImportSettings.cs b/PKHeX.Core/Saves/Storage/EntityImportSettings.cs new file mode 100644 index 000000000..342c211dc --- /dev/null +++ b/PKHeX.Core/Saves/Storage/EntityImportSettings.cs @@ -0,0 +1,28 @@ +using static PKHeX.Core.EntityImportOption; + +namespace PKHeX.Core; + +/// +/// Settings to conditionally update entity and save file properties when adapting or importing to a save file. +/// +public readonly record struct EntityImportSettings( + EntityImportOption UpdateToSaveFile, + EntityImportOption UpdatePokeDex, + EntityImportOption UpdateRecord) +{ + public static EntityImportSettings None => new(Disable, Disable, Disable); + public static EntityImportSettings All => new(Enable, Enable, Enable); +} + +/// +/// Option to enable/disable conditional updates when adapting or importing to a save file. +/// +public enum EntityImportOption : byte +{ + /// + /// Use whatever the global setting is. + /// + UseDefault, + Enable, + Disable, +} diff --git a/PKHeX.Core/Saves/Storage/PKMImportSetting.cs b/PKHeX.Core/Saves/Storage/PKMImportSetting.cs deleted file mode 100644 index f6abd8563..000000000 --- a/PKHeX.Core/Saves/Storage/PKMImportSetting.cs +++ /dev/null @@ -1,22 +0,0 @@ -namespace PKHeX.Core; - -/// -/// Setting to conditionally update PKM properties when importing to a save file. -/// -public enum PKMImportSetting -{ - /// - /// Use whatever the global setting is. - /// - UseDefault, - - /// - /// Always update the PKM properties to match the save file. - /// - Update, - - /// - /// Never update the PKM properties to match the save file. - /// - Skip, -} diff --git a/PKHeX.Core/Saves/Util/BoxUtil.cs b/PKHeX.Core/Saves/Util/BoxUtil.cs index ec8382919..805056a7c 100644 --- a/PKHeX.Core/Saves/Util/BoxUtil.cs +++ b/PKHeX.Core/Saves/Util/BoxUtil.cs @@ -95,17 +95,17 @@ public static int DumpBox(this SaveFile sav, string path, int currentBox) /// First box to start loading to. All prior boxes are not modified. /// Instruction to clear boxes after the starting box. /// Overwrite existing full slots. If true, will only overwrite empty slots. - /// Bypass option to not modify properties when setting to Save File. + /// Bypass option to not modify properties when setting to Save File. /// Enumerate all files even in sub-folders. /// Count of files imported. - public static int LoadBoxes(this SaveFile sav, string path, out string result, int boxStart = 0, bool boxClear = false, bool overwrite = false, PKMImportSetting noSetb = PKMImportSetting.UseDefault, bool all = false) + public static int LoadBoxes(this SaveFile sav, string path, out string result, int boxStart = 0, bool boxClear = false, bool overwrite = false, EntityImportSettings settings = default, bool all = false) { if (string.IsNullOrWhiteSpace(path) || !Directory.Exists(path)) { result = MsgSaveBoxExportPathInvalid; return -1; } var option = all ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; var files = Directory.EnumerateFiles(path, "*.*", option); - return sav.LoadBoxes(files, out result, boxStart, boxClear, overwrite, noSetb); + return sav.LoadBoxes(files, out result, boxStart, boxClear, overwrite, settings); } /// @@ -117,12 +117,12 @@ public static int LoadBoxes(this SaveFile sav, string path, out string result, i /// First box to start loading to. All prior boxes are not modified. /// Instruction to clear boxes after the starting box. /// Overwrite existing full slots. If true, will only overwrite empty slots. - /// Bypass option to not modify properties when setting to Save File. + /// Bypass option to not modify properties when setting to Save File. /// Count of files imported. - public static int LoadBoxes(this SaveFile sav, IEnumerable files, out string result, int boxStart = 0, bool boxClear = false, bool overwrite = false, PKMImportSetting noSetb = PKMImportSetting.UseDefault) + public static int LoadBoxes(this SaveFile sav, IEnumerable files, out string result, int boxStart = 0, bool boxClear = false, bool overwrite = false, EntityImportSettings settings = default) { var pks = GetPossiblePKMsFromPaths(sav, files); - return sav.LoadBoxes(pks, out result, boxStart, boxClear, overwrite, noSetb); + return sav.LoadBoxes(pks, out result, boxStart, boxClear, overwrite, settings); } /// @@ -134,12 +134,12 @@ public static int LoadBoxes(this SaveFile sav, IEnumerable files, out st /// First box to start loading to. All prior boxes are not modified. /// Instruction to clear boxes after the starting box. /// Overwrite existing full slots. If true, will only overwrite empty slots. - /// Bypass option to not modify properties when setting to Save File. + /// Bypass option to not modify properties when setting to Save File. /// Count of files imported. - public static int LoadBoxes(this SaveFile sav, IEnumerable encounters, out string result, int boxStart = 0, bool boxClear = false, bool overwrite = false, PKMImportSetting noSetb = PKMImportSetting.UseDefault) + public static int LoadBoxes(this SaveFile sav, IEnumerable encounters, out string result, int boxStart = 0, bool boxClear = false, bool overwrite = false, EntityImportSettings settings = default) { var pks = encounters.Select(z => z.ConvertToPKM(sav)); - return sav.LoadBoxes(pks, out result, boxStart, boxClear, overwrite, noSetb); + return sav.LoadBoxes(pks, out result, boxStart, boxClear, overwrite, settings); } /// @@ -151,9 +151,9 @@ public static int LoadBoxes(this SaveFile sav, IEnumerableFirst box to start loading to. All prior boxes are not modified. /// Instruction to clear boxes after the starting box. /// Overwrite existing full slots. If true, will only overwrite empty slots. - /// Bypass option to not modify properties when setting to Save File. + /// Bypass option to not modify properties when setting to Save File. /// True if any files are imported. - public static int LoadBoxes(this SaveFile sav, IEnumerable pks, out string result, int boxStart = 0, bool boxClear = false, bool overwrite = false, PKMImportSetting noSetb = PKMImportSetting.UseDefault) + public static int LoadBoxes(this SaveFile sav, IEnumerable pks, out string result, int boxStart = 0, bool boxClear = false, bool overwrite = false, EntityImportSettings settings = default) { if (!sav.HasBox) { result = MsgSaveBoxFailNone; return -1; } @@ -162,7 +162,7 @@ public static int LoadBoxes(this SaveFile sav, IEnumerable pks, out string if (boxClear) sav.ClearBoxes(boxStart); - int ctr = sav.ImportPKMs(compat, overwrite, boxStart, noSetb); + int ctr = sav.ImportPKMs(compat, overwrite, boxStart, settings); if (ctr <= 0) { result = MsgSaveBoxImportNoFiles; diff --git a/PKHeX.Core/Saves/Util/SaveExtensions.cs b/PKHeX.Core/Saves/Util/SaveExtensions.cs index 20076e1fe..7f054dbf5 100644 --- a/PKHeX.Core/Saves/Util/SaveExtensions.cs +++ b/PKHeX.Core/Saves/Util/SaveExtensions.cs @@ -89,9 +89,9 @@ private static List GetSaveFileErrata(this SaveFile sav, PKM pk, IBasicS /// Compatible data that can be set to the without conversion. /// Overwrite existing full slots. If true, will only overwrite empty slots. /// First box to start loading to. All prior boxes are not modified. - /// Bypass option to not modify properties when setting to Save File. + /// Bypass option to not modify properties when setting to Save File. /// Count of injected . - public static int ImportPKMs(this SaveFile sav, IEnumerable compat, bool overwrite = false, int boxStart = 0, PKMImportSetting noSetb = PKMImportSetting.UseDefault) + public static int ImportPKMs(this SaveFile sav, IEnumerable compat, bool overwrite = false, int boxStart = 0, EntityImportSettings settings = default) { int startCount = boxStart * sav.BoxSlotCount; int maxCount = sav.SlotCount; @@ -109,7 +109,7 @@ public static int ImportPKMs(this SaveFile sav, IEnumerable compat, bool ov if (index >= maxCount) // Boxes full! break; - sav.SetBoxSlotAtIndex(pk, index, noSetb); + sav.SetBoxSlotAtIndex(pk, index, settings); } else { @@ -117,7 +117,7 @@ public static int ImportPKMs(this SaveFile sav, IEnumerable compat, bool ov if (index < 0) // Boxes full! break; - sav.SetBoxSlotAtIndex(pk, index, noSetb); + sav.SetBoxSlotAtIndex(pk, index, settings); nonOverwriteImport++; } diff --git a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs index 0903ec5d3..212d4aa4e 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs @@ -907,15 +907,15 @@ private void ClickVerifyStoredEntities(object sender, EventArgs e) } // File I/O - public bool GetBulkImportSettings(out bool clearAll, out bool overwrite, out PKMImportSetting noSetb) + public bool GetBulkImportSettings(out bool clearAll, out bool overwrite, out EntityImportSettings settings) { - clearAll = false; noSetb = PKMImportSetting.UseDefault; overwrite = false; + clearAll = false; settings = default; overwrite = false; var dr = WinFormsUtil.Prompt(MessageBoxButtons.YesNoCancel, MsgSaveBoxImportClear, MsgSaveBoxImportClearNo); if (dr == DialogResult.Cancel) return false; clearAll = dr == DialogResult.Yes; - noSetb = GetPKMSetOverride(ModifyPKM); + settings = GetImportSettingsOverride(); return true; } @@ -1016,8 +1016,8 @@ public bool OpenGroup(IPokeGroup b, out string c) return false; } - var noSetb = GetPKMSetOverride(ModifyPKM); - var slotSkipped = ImportGroup(b.Contents, SAV, Box.CurrentBox, noSetb); + var settings = GetImportSettingsOverride(); + var slotSkipped = ImportGroup(b.Contents, SAV, Box.CurrentBox, settings); SetPKMBoxes(); UpdateBoxViewers(); @@ -1027,7 +1027,7 @@ public bool OpenGroup(IPokeGroup b, out string c) return true; } - private static int ImportGroup(IEnumerable data, SaveFile sav, int box, PKMImportSetting noSetb) + private static int ImportGroup(IEnumerable data, SaveFile sav, int box, EntityImportSettings settings) { var type = sav.PKMType; int slotSkipped = 0; @@ -1047,7 +1047,7 @@ private static int ImportGroup(IEnumerable data, SaveFile sav, int box, PKM slotSkipped++; continue; } - sav.SetBoxSlotAtIndex(x, box, i, noSetb); + sav.SetBoxSlotAtIndex(x, box, i, settings); } return slotSkipped; @@ -1068,10 +1068,10 @@ public bool LoadBoxes(out string result, string? path = null) if (!Directory.Exists(path)) return false; - if (!GetBulkImportSettings(out bool clearAll, out var overwrite, out var noSetb)) + if (!GetBulkImportSettings(out bool clearAll, out var overwrite, out var settings)) return false; - SAV.LoadBoxes(path, out result, Box.CurrentBox, clearAll, overwrite, noSetb); + SAV.LoadBoxes(path, out result, Box.CurrentBox, clearAll, overwrite, settings); SetPKMBoxes(); UpdateBoxViewers(); return true; @@ -1382,19 +1382,18 @@ private void B_MailBox_Click(object sender, EventArgs e) ResetParty(); } - private static PKMImportSetting GetPKMSetOverride(bool currentSetting) + private static EntityImportSettings GetImportSettingsOverride() { - var yn = currentSetting ? MsgYes : MsgNo; var choice = WinFormsUtil.Prompt(MessageBoxButtons.YesNoCancel, MsgSaveBoxImportModifyIntro, MsgSaveBoxImportModifyYes + Environment.NewLine + MsgSaveBoxImportModifyNo + Environment.NewLine + - string.Format(MsgSaveBoxImportModifyCurrent, yn)); + string.Format(MsgSaveBoxImportModifyCurrent, SaveFile.SetUpdateSettings)); return choice switch { - DialogResult.Yes => PKMImportSetting.Update, - DialogResult.No => PKMImportSetting.Skip, - _ => PKMImportSetting.UseDefault, + DialogResult.Yes => EntityImportSettings.All, + DialogResult.No => EntityImportSettings.None, + _ => default, }; } diff --git a/PKHeX.WinForms/MainWindow/Main.cs b/PKHeX.WinForms/MainWindow/Main.cs index fe82d5a9a..e0da46c9f 100644 --- a/PKHeX.WinForms/MainWindow/Main.cs +++ b/PKHeX.WinForms/MainWindow/Main.cs @@ -436,9 +436,9 @@ private void ReloadProgramSettings(PKHeXSettings settings) SpriteBuilderUtil.SpriterPreference = settings.Sprite.SpritePreference; var write = settings.SlotWrite; - SaveFile.SetUpdateDex = write.SetUpdateDex ? PKMImportSetting.Update : PKMImportSetting.Skip; - SaveFile.SetUpdatePKM = write.SetUpdatePKM ? PKMImportSetting.Update : PKMImportSetting.Skip; - SaveFile.SetUpdateRecords = write.SetUpdateRecords ? PKMImportSetting.Update : PKMImportSetting.Skip; + SaveFile.SetUpdateDex = write.SetUpdateDex ? EntityImportOption.Enable : EntityImportOption.Disable; + SaveFile.SetUpdatePKM = write.SetUpdatePKM ? EntityImportOption.Enable : EntityImportOption.Disable; + SaveFile.SetUpdateRecords = write.SetUpdateRecords ? EntityImportOption.Enable : EntityImportOption.Disable; C_SAV.ModifyPKM = PKME_Tabs.ModifyPKM = settings.SlotWrite.SetUpdatePKM; CommonEdits.ShowdownSetIVMarkings = settings.Import.ApplyMarkings; @@ -666,7 +666,7 @@ private bool OpenPKM(PKM pk) Debug.WriteLine(c.GetDisplayString(pk, destType)); if (tmp is null) return false; - C_SAV.SAV.AdaptPKM(tmp); + C_SAV.SAV.AdaptToSaveFile(tmp); PKME_Tabs.PopulateFields(tmp); return true; } @@ -698,7 +698,7 @@ private bool OpenMysteryGift(MysteryGift tg, string path) return true; } - C_SAV.SAV.AdaptPKM(pk); + C_SAV.SAV.AdaptToSaveFile(pk); PKME_Tabs.PopulateFields(pk); Debug.WriteLine(c); return true; diff --git a/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.cs b/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.cs index 696950e89..c58849436 100644 --- a/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.cs +++ b/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.cs @@ -185,7 +185,7 @@ private void RunBatchEditSaveFile(IReadOnlyCollection sets SlotInfoLoader.AddPartyData(SAV, data); process(data); foreach (var slot in data) - slot.Source.WriteTo(SAV, slot.Entity, PKMImportSetting.Skip); + slot.Source.WriteTo(SAV, slot.Entity, EntityImportSettings.None); } if (boxes) { @@ -193,7 +193,7 @@ private void RunBatchEditSaveFile(IReadOnlyCollection sets SlotInfoLoader.AddBoxData(SAV, data); process(data); foreach (var slot in data) - slot.Source.WriteTo(SAV, slot.Entity, PKMImportSetting.Skip); + slot.Source.WriteTo(SAV, slot.Entity, EntityImportSettings.None); } void process(IList d) { diff --git a/PKHeX.WinForms/Subforms/SAV_Database.cs b/PKHeX.WinForms/Subforms/SAV_Database.cs index 27034e486..b8422de76 100644 --- a/PKHeX.WinForms/Subforms/SAV_Database.cs +++ b/PKHeX.WinForms/Subforms/SAV_Database.cs @@ -491,11 +491,11 @@ private void Menu_Export_Click(object sender, EventArgs e) private void Menu_Import_Click(object sender, EventArgs e) { - if (!BoxView.GetBulkImportSettings(out var clearAll, out var overwrite, out var noSetb)) + if (!BoxView.GetBulkImportSettings(out var clearAll, out var overwrite, out var settings)) return; int box = BoxView.Box.CurrentBox; - int ctr = SAV.LoadBoxes(Results.Select(z => z.Entity), out var result, box, clearAll, overwrite, noSetb); + int ctr = SAV.LoadBoxes(Results.Select(z => z.Entity), out var result, box, clearAll, overwrite, settings); if (ctr <= 0) return; diff --git a/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs b/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs index 3bf5a152e..8f6b451c2 100644 --- a/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs +++ b/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs @@ -140,7 +140,7 @@ private void ClickView(object sender, EventArgs e) WinFormsUtil.Error(c.GetDisplayString(temp, SAV.PKMType)); return; } - SAV.AdaptPKM(pk); + SAV.AdaptToSaveFile(pk); PKME_Tabs.PopulateFields(pk, false); slotSelected = index; slotColor = SpriteUtil.Spriter.View; @@ -407,11 +407,11 @@ private void UpdateSlotColor(int start) private void Menu_Import_Click(object sender, EventArgs e) { - if (!BoxView.GetBulkImportSettings(out var clearAll, out var overwrite, out var noSetb)) + if (!BoxView.GetBulkImportSettings(out var clearAll, out var overwrite, out var settings)) return; int box = BoxView.Box.CurrentBox; - int ctr = SAV.LoadBoxes(Results, out var result, box, clearAll, overwrite, noSetb); + int ctr = SAV.LoadBoxes(Results, out var result, box, clearAll, overwrite, settings); if (ctr <= 0) return;