From 5dcf2f4dc84d41319c39621ccdcf8e25477b8822 Mon Sep 17 00:00:00 2001 From: Kurt Date: Fri, 29 Mar 2019 17:52:26 -0700 Subject: [PATCH] Use enum instead of nullable bool tristate --- PKHeX.Core/Saves/SAV1.cs | 2 +- PKHeX.Core/Saves/SAV3RSBox.cs | 18 ++---- PKHeX.Core/Saves/SaveFile.cs | 55 +++++++++++++------ PKHeX.Core/Saves/Storage/PKMImportSetting.cs | 9 +++ PKHeX.Core/Saves/Util/BoxUtil.cs | 8 +-- PKHeX.Core/Saves/Util/SaveExtensions.cs | 2 +- .../Controls/SAV Editor/SAVEditor.cs | 16 +++--- PKHeX.WinForms/MainWindow/Main.cs | 5 +- 8 files changed, 68 insertions(+), 47 deletions(-) create mode 100644 PKHeX.Core/Saves/Storage/PKMImportSetting.cs diff --git a/PKHeX.Core/Saves/SAV1.cs b/PKHeX.Core/Saves/SAV1.cs index 6c07eb6b5..8a98e2698 100644 --- a/PKHeX.Core/Saves/SAV1.cs +++ b/PKHeX.Core/Saves/SAV1.cs @@ -515,7 +515,7 @@ namespace PKHeX.Core return GetFlag(Offsets.DexCaught + ofs, bit & 7); } - public override void SetStoredSlot(PKM pkm, int offset, bool? trade = null, bool? dex = null) + public override void SetStoredSlot(PKM pkm, int offset, PKMImportSetting trade = PKMImportSetting.UseDefault, PKMImportSetting dex = PKMImportSetting.UseDefault) { // pkm that have never been boxed have yet to save the 'current level' for box indication // set this value at this time diff --git a/PKHeX.Core/Saves/SAV3RSBox.cs b/PKHeX.Core/Saves/SAV3RSBox.cs index de7ac79c5..9c0855e05 100644 --- a/PKHeX.Core/Saves/SAV3RSBox.cs +++ b/PKHeX.Core/Saves/SAV3RSBox.cs @@ -181,21 +181,11 @@ namespace PKHeX.Core protected override void SetDex(PKM pkm) { /* No Pokedex for this game, do nothing */ } - public override void SetStoredSlot(PKM pkm, int offset, bool? trade = null, bool? dex = null) + public override void SetStoredSlot(PKM pkm, int offset, PKMImportSetting trade = PKMImportSetting.UseDefault, PKMImportSetting dex = PKMImportSetting.UseDefault) { - if (pkm == null) return; - if (pkm.GetType() != PKMType) - throw new InvalidCastException($"PKM Format needs to be {PKMType} when setting to a Gen{Generation} Save File."); - if (trade ?? SetUpdatePKM) - SetPKM(pkm); - if (dex ?? SetUpdateDex) - SetDex(pkm); - byte[] data = pkm.EncryptedBoxData; - SetData(data, offset); - - BitConverter.GetBytes((ushort)pkm.TID).CopyTo(Data, offset + data.Length + 0); - BitConverter.GetBytes((ushort)pkm.SID).CopyTo(Data, offset + data.Length + 2); - Edited = true; + base.SetStoredSlot(pkm, offset, trade, dex); + BitConverter.GetBytes((ushort)pkm.TID).CopyTo(Data, offset + PKX.SIZE_3STORED + 0); + BitConverter.GetBytes((ushort)pkm.SID).CopyTo(Data, offset + PKX.SIZE_3STORED + 2); } public override string GetString(byte[] data, int offset, int length) => StringConverter3.GetString3(data, offset, length, Japanese); diff --git a/PKHeX.Core/Saves/SaveFile.cs b/PKHeX.Core/Saves/SaveFile.cs index 6b4344956..8b9c9f4d7 100644 --- a/PKHeX.Core/Saves/SaveFile.cs +++ b/PKHeX.Core/Saves/SaveFile.cs @@ -10,8 +10,8 @@ namespace PKHeX.Core /// public abstract class SaveFile : ITrainerInfo, IGameValueLimit { - public static bool SetUpdateDex { protected get; set; } = true; - public static bool SetUpdatePKM { protected get; set; } = true; + public static PKMImportSetting SetUpdateDex { protected get; set; } = PKMImportSetting.Update; + public static PKMImportSetting SetUpdatePKM { protected get; set; } = PKMImportSetting.Update; // General Object Properties public byte[] Data; @@ -586,9 +586,14 @@ namespace PKHeX.Core return GetBoxSlotOffset(box, slot); } - public void SetBoxSlotAtIndex(PKM pkm, int box, int slot, bool? trade, bool? dex = null) => SetStoredSlot(pkm, GetBoxSlotOffset(box, slot), trade, dex); - public void SetBoxSlotAtIndex(PKM pkm, int index, bool? trade, bool? dex = null) => SetStoredSlot(pkm, GetBoxSlotOffset(index), trade, dex); - public void SetPartySlotAtIndex(PKM pkm, int index, bool? trade = null, bool? dex = null) => SetPartySlot(pkm, GetPartyOffset(index), trade, dex); + public void SetBoxSlotAtIndex(PKM pkm, int box, int slot, PKMImportSetting trade = PKMImportSetting.UseDefault, PKMImportSetting dex = PKMImportSetting.UseDefault) + => SetStoredSlot(pkm, GetBoxSlotOffset(box, slot), trade, dex); + + public void SetBoxSlotAtIndex(PKM pkm, int index, PKMImportSetting trade = PKMImportSetting.UseDefault, PKMImportSetting dex = PKMImportSetting.UseDefault) + => SetStoredSlot(pkm, GetBoxSlotOffset(index), trade, dex); + + public void SetPartySlotAtIndex(PKM pkm, int index, PKMImportSetting trade = PKMImportSetting.UseDefault, PKMImportSetting dex = PKMImportSetting.UseDefault) + => SetPartySlot(pkm, GetPartyOffset(index), trade, dex); public virtual PKM GetPartySlot(int offset) => GetPKM(DecryptPKM(GetData(offset, SIZE_PARTY))); @@ -597,15 +602,12 @@ namespace PKHeX.Core return GetPKM(DecryptPKM(GetData(offset, SIZE_STORED))); } - public void SetPartySlot(PKM pkm, int offset, bool? trade = null, bool? dex = null) + public void SetPartySlot(PKM pkm, int offset, PKMImportSetting trade = PKMImportSetting.UseDefault, PKMImportSetting dex = PKMImportSetting.UseDefault) { if (pkm == null) return; if (pkm.GetType() != PKMType) throw new ArgumentException($"PKM Format needs to be {PKMType} when setting to this Save File."); - if (trade ?? SetUpdatePKM) - SetPKM(pkm); - if (dex ?? SetUpdateDex) - SetDex(pkm); + UpdatePKM(pkm, trade, dex); SetPartyValues(pkm, isParty: true); int i = GetPartyIndex(offset); @@ -627,6 +629,28 @@ namespace PKHeX.Core Edited = true; } + protected void UpdatePKM(PKM pkm, PKMImportSetting trade, PKMImportSetting dex) + { + if (GetTradeUpdateSetting(trade)) + SetPKM(pkm); + if (GetDexUpdateSetting(dex)) + SetDex(pkm); + } + + private static bool GetTradeUpdateSetting(PKMImportSetting trade = PKMImportSetting.UseDefault) + { + if (trade == PKMImportSetting.UseDefault) + trade = SetUpdatePKM; + return trade == PKMImportSetting.Update; + } + + private static bool GetDexUpdateSetting(PKMImportSetting trade = PKMImportSetting.UseDefault) + { + if (trade == PKMImportSetting.UseDefault) + trade = SetUpdateDex; + return trade == PKMImportSetting.Update; + } + private int GetPartyIndex(int offset) { for (int i = 0; i < 6; i++) @@ -637,15 +661,12 @@ namespace PKHeX.Core return -1; } - public virtual void SetStoredSlot(PKM pkm, int offset, bool? trade = null, bool? dex = null) + public virtual void SetStoredSlot(PKM pkm, int offset, PKMImportSetting trade = PKMImportSetting.UseDefault, PKMImportSetting dex = PKMImportSetting.UseDefault) { if (pkm == null) return; if (pkm.GetType() != PKMType) throw new ArgumentException($"PKM Format needs to be {PKMType} when setting to this Save File."); - if (trade ?? SetUpdatePKM) - SetPKM(pkm); - if (dex ?? SetUpdateDex) - SetDex(pkm); + UpdatePKM(pkm, trade, dex); SetPartyValues(pkm, isParty: false); SetData(pkm.EncryptedBoxData, offset); Edited = true; @@ -662,7 +683,7 @@ namespace PKHeX.Core int slotFrom = GetPartyOffset(i); SetData(GetData(slotFrom, SIZE_PARTY), slotTo); } - SetStoredSlot(BlankPKM, GetPartyOffset(5), false, false); + SetStoredSlot(BlankPKM, GetPartyOffset(5), PKMImportSetting.Skip, PKMImportSetting.Skip); PartyCount--; } @@ -784,7 +805,7 @@ namespace PKHeX.Core var pk = GetStoredSlot(ofs); action(pk); ++modified; - SetStoredSlot(pk, ofs, false, false); + SetStoredSlot(pk, ofs, PKMImportSetting.Skip, PKMImportSetting.Skip); } } return modified; diff --git a/PKHeX.Core/Saves/Storage/PKMImportSetting.cs b/PKHeX.Core/Saves/Storage/PKMImportSetting.cs new file mode 100644 index 000000000..184f9b6a2 --- /dev/null +++ b/PKHeX.Core/Saves/Storage/PKMImportSetting.cs @@ -0,0 +1,9 @@ +namespace PKHeX.Core +{ + public enum PKMImportSetting + { + UseDefault, + Update, + Skip, + } +} \ No newline at end of file diff --git a/PKHeX.Core/Saves/Util/BoxUtil.cs b/PKHeX.Core/Saves/Util/BoxUtil.cs index 874019db1..f51ec8c5f 100644 --- a/PKHeX.Core/Saves/Util/BoxUtil.cs +++ b/PKHeX.Core/Saves/Util/BoxUtil.cs @@ -90,7 +90,7 @@ namespace PKHeX.Core /// 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, bool? noSetb = null, bool all = false) + 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) { if (string.IsNullOrWhiteSpace(path) || !Directory.Exists(path)) { result = MsgSaveBoxExportPathInvalid; return -1; } @@ -111,7 +111,7 @@ namespace PKHeX.Core /// Overwrite existing full slots. If true, will only overwrite empty slots. /// Bypass option to not modify properties when setting to Save File. /// Count of files imported. - public static int LoadBoxes(this SaveFile SAV, IEnumerable filepaths, out string result, int boxStart = 0, bool boxClear = false, bool overwrite = false, bool? noSetb = null) + public static int LoadBoxes(this SaveFile SAV, IEnumerable filepaths, out string result, int boxStart = 0, bool boxClear = false, bool overwrite = false, PKMImportSetting noSetb = PKMImportSetting.UseDefault) { var pks = GetPossiblePKMsFromPaths(SAV, filepaths); return SAV.LoadBoxes(pks, out result, boxStart, boxClear, overwrite, noSetb); @@ -128,7 +128,7 @@ namespace PKHeX.Core /// Overwrite existing full slots. If true, will only overwrite empty slots. /// 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, bool? noSetb = null) + 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) { var pks = encounters.Select(z => z.ConvertToPKM(SAV)); return SAV.LoadBoxes(pks, out result, boxStart, boxClear, overwrite, noSetb); @@ -145,7 +145,7 @@ namespace PKHeX.Core /// Overwrite existing full slots. If true, will only overwrite empty slots. /// 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, bool? noSetb = null) + 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) { if (!SAV.HasBox) { result = MsgSaveBoxFailNone; return -1; } diff --git a/PKHeX.Core/Saves/Util/SaveExtensions.cs b/PKHeX.Core/Saves/Util/SaveExtensions.cs index 17eded4be..159e30647 100644 --- a/PKHeX.Core/Saves/Util/SaveExtensions.cs +++ b/PKHeX.Core/Saves/Util/SaveExtensions.cs @@ -116,7 +116,7 @@ namespace PKHeX.Core /// First box to start loading to. All prior boxes are not modified. /// 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, bool? noSetb = null) + public static int ImportPKMs(this SaveFile SAV, IEnumerable compat, bool overwrite = false, int boxStart = 0, PKMImportSetting noSetb = PKMImportSetting.UseDefault) { int startCount = boxStart * SAV.BoxSlotCount; int maxCount = SAV.SlotCount; diff --git a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs index 4896d5a02..8fa73f305 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs @@ -695,9 +695,9 @@ namespace PKHeX.WinForms.Controls } // File I/O - public bool GetBulkImportSettings(out bool clearAll, out bool overwrite, out bool? noSetb) + public bool GetBulkImportSettings(out bool clearAll, out bool overwrite, out PKMImportSetting noSetb) { - clearAll = false; noSetb = false; overwrite = false; + clearAll = false; noSetb = PKMImportSetting.UseDefault; overwrite = false; var dr = WinFormsUtil.Prompt(MessageBoxButtons.YesNoCancel, MsgSaveBoxImportClear, MsgSaveBoxImportClearNo); if (dr == DialogResult.Cancel) return false; @@ -709,7 +709,7 @@ namespace PKHeX.WinForms.Controls private static bool IsFolderPath(out string path) { - FolderBrowserDialog fbd = new FolderBrowserDialog(); + var fbd = new FolderBrowserDialog(); var result = fbd.ShowDialog() == DialogResult.OK; path = fbd.SelectedPath; return result; @@ -785,7 +785,7 @@ namespace PKHeX.WinForms.Controls return false; } - bool? noSetb = GetPKMSetOverride(ModifyPKM); + var noSetb = GetPKMSetOverride(ModifyPKM); PKM[] data = b.BattlePKMs; int offset = SAV.GetBoxOffset(Box.CurrentBox); int slotSkipped = 0; @@ -1121,7 +1121,7 @@ namespace PKHeX.WinForms.Controls ReloadSlots(); } - private static bool? GetPKMSetOverride(bool currentSetting) + private static PKMImportSetting GetPKMSetOverride(bool currentSetting) { var yn = currentSetting ? MsgYes : MsgNo; DialogResult noSet = WinFormsUtil.Prompt(MessageBoxButtons.YesNoCancel, @@ -1131,9 +1131,9 @@ namespace PKHeX.WinForms.Controls string.Format(MsgSaveBoxImportModifyCurrent, yn)); switch (noSet) { - case DialogResult.Yes: return true; - case DialogResult.No: return false; - default: return null; + case DialogResult.Yes: return PKMImportSetting.Update; + case DialogResult.No: return PKMImportSetting.Skip; + default: return PKMImportSetting.UseDefault; } } diff --git a/PKHeX.WinForms/MainWindow/Main.cs b/PKHeX.WinForms/MainWindow/Main.cs index 19fc77ebd..1f80348be 100644 --- a/PKHeX.WinForms/MainWindow/Main.cs +++ b/PKHeX.WinForms/MainWindow/Main.cs @@ -428,8 +428,9 @@ namespace PKHeX.WinForms PKME_Tabs.Unicode = Unicode = settings.Unicode; PKME_Tabs.UpdateUnicode(GenderSymbols); PKX.AllowShinySprite = settings.ShinySprites; - SaveFile.SetUpdateDex = settings.SetUpdateDex; - SaveFile.SetUpdatePKM = C_SAV.ModifyPKM = PKME_Tabs.ModifyPKM = settings.SetUpdatePKM; + SaveFile.SetUpdateDex = settings.SetUpdateDex ? PKMImportSetting.Update : PKMImportSetting.Skip; + SaveFile.SetUpdatePKM = settings.SetUpdatePKM ? PKMImportSetting.Update : PKMImportSetting.Skip; + C_SAV.ModifyPKM = PKME_Tabs.ModifyPKM = settings.SetUpdatePKM; CommonEdits.ShowdownSetIVMarkings = settings.ApplyMarkings; C_SAV.FlagIllegal = settings.FlagIllegal; C_SAV.M.GlowHover = settings.HoverSlotGlowEdges;