From d3dd4fb2a573eb9c480f4e2e0ad8087b779173a1 Mon Sep 17 00:00:00 2001 From: Kurt Date: Wed, 20 Feb 2019 17:59:54 -0800 Subject: [PATCH] Track slot modified count on sort/delete/mod Sorting will always show multiples of boxcount since it repositions empty slots --- PKHeX.Core/Editing/Saves/BoxManipClear.cs | 7 +++---- PKHeX.Core/Editing/Saves/BoxManipModify.cs | 5 ++--- PKHeX.Core/Editing/Saves/BoxManipSort.cs | 5 ++--- PKHeX.Core/Editing/Saves/BoxManipulator.cs | 6 +++--- PKHeX.Core/Editing/Saves/IBoxManip.cs | 2 +- PKHeX.Core/Saves/SaveFile.cs | 21 ++++++++++++++----- .../Controls/SAV Editor/BoxMenuStrip.cs | 2 +- .../Controls/SAV Editor/SAVEditor.cs | 4 ++-- 8 files changed, 30 insertions(+), 22 deletions(-) diff --git a/PKHeX.Core/Editing/Saves/BoxManipClear.cs b/PKHeX.Core/Editing/Saves/BoxManipClear.cs index f2a624bb7..158452db2 100644 --- a/PKHeX.Core/Editing/Saves/BoxManipClear.cs +++ b/PKHeX.Core/Editing/Saves/BoxManipClear.cs @@ -16,11 +16,10 @@ public class BoxManipClear : IBoxManip protected Func CriteriaSimple { private get; set; } protected Func CriteriaSAV { private get; set; } - public virtual bool Execute(SaveFile SAV, BoxManipParam param) + public virtual int Execute(SaveFile SAV, BoxManipParam param) { bool Method(PKM p) => param.Reverse ^ (CriteriaSAV?.Invoke(p, SAV) ?? CriteriaSimple?.Invoke(p) ?? true); - SAV.ClearBoxes(param.Start, param.Stop, Method); - return true; + return SAV.ClearBoxes(param.Start, param.Stop, Method); } protected BoxManipClear() { } @@ -56,7 +55,7 @@ public sealed class BoxManipClearDuplicate : BoxManipClear { private readonly HashSet HashSet = new HashSet(); - public override bool Execute(SaveFile SAV, BoxManipParam param) + public override int Execute(SaveFile SAV, BoxManipParam param) { HashSet.Clear(); return base.Execute(SAV, param); diff --git a/PKHeX.Core/Editing/Saves/BoxManipModify.cs b/PKHeX.Core/Editing/Saves/BoxManipModify.cs index 7889c71b9..1e0cedf82 100644 --- a/PKHeX.Core/Editing/Saves/BoxManipModify.cs +++ b/PKHeX.Core/Editing/Saves/BoxManipModify.cs @@ -15,11 +15,10 @@ public sealed class BoxManipModify : IBoxManip private readonly Action Action; private readonly Action ActionComplex; - public bool Execute(SaveFile SAV, BoxManipParam param) + public int Execute(SaveFile SAV, BoxManipParam param) { var method = Action ?? (px => ActionComplex(px, SAV)); - SAV.ModifyBoxes(method, param.Start, param.Stop); - return true; + return SAV.ModifyBoxes(method, param.Start, param.Stop); } private BoxManipModify(BoxManipType type, Action action, Func usable = null) diff --git a/PKHeX.Core/Editing/Saves/BoxManipSort.cs b/PKHeX.Core/Editing/Saves/BoxManipSort.cs index 6bb463b93..3e9878296 100644 --- a/PKHeX.Core/Editing/Saves/BoxManipSort.cs +++ b/PKHeX.Core/Editing/Saves/BoxManipSort.cs @@ -15,11 +15,10 @@ public sealed class BoxManipSort : IBoxManip private readonly Func, IEnumerable> SorterSimple; private readonly Func, SaveFile, IEnumerable> SorterComplex; - public bool Execute(SaveFile SAV, BoxManipParam param) + public int Execute(SaveFile SAV, BoxManipParam param) { IEnumerable Method(IEnumerable p) => SorterSimple != null ? SorterSimple(p) : SorterComplex(p, SAV); - SAV.SortBoxes(param.Start, param.Stop, Method, param.Reverse); - return true; + return SAV.SortBoxes(param.Start, param.Stop, Method, param.Reverse); } private BoxManipSort(BoxManipType type, Func, IEnumerable> sorter, Func usable = null) diff --git a/PKHeX.Core/Editing/Saves/BoxManipulator.cs b/PKHeX.Core/Editing/Saves/BoxManipulator.cs index 62f240c46..3b54394db 100644 --- a/PKHeX.Core/Editing/Saves/BoxManipulator.cs +++ b/PKHeX.Core/Editing/Saves/BoxManipulator.cs @@ -31,10 +31,10 @@ public bool Execute(IBoxManip manip, int box, bool allBoxes, bool reverse = fals return false; var result = manip.Execute(SAV, param); - if (!result) + if (result <= 0) return false; var success = manip.GetSuccess(allBoxes); - FinishBoxManipulation(success, allBoxes); + FinishBoxManipulation(success, allBoxes, result); return true; } @@ -67,6 +67,6 @@ public bool Execute(BoxManipType type, int box, bool allBoxes, bool reverse = fa /// /// Optional message to show if applicable. /// Indicates if all boxes were manipulated, or just one box. - protected abstract void FinishBoxManipulation(string message, bool all); + protected abstract void FinishBoxManipulation(string message, bool all, int count); } } diff --git a/PKHeX.Core/Editing/Saves/IBoxManip.cs b/PKHeX.Core/Editing/Saves/IBoxManip.cs index fd1226273..a6e13804c 100644 --- a/PKHeX.Core/Editing/Saves/IBoxManip.cs +++ b/PKHeX.Core/Editing/Saves/IBoxManip.cs @@ -11,6 +11,6 @@ public interface IBoxManip string GetFail(bool all); string GetSuccess(bool all); - bool Execute(SaveFile SAV, BoxManipParam param); + int Execute(SaveFile SAV, BoxManipParam param); } } \ No newline at end of file diff --git a/PKHeX.Core/Saves/SaveFile.cs b/PKHeX.Core/Saves/SaveFile.cs index c6ff5be76..e6cb7c5cc 100644 --- a/PKHeX.Core/Saves/SaveFile.cs +++ b/PKHeX.Core/Saves/SaveFile.cs @@ -684,7 +684,7 @@ public bool IsAnySlotLockedInBox(int BoxStart, int BoxEnd) .Any(slot => WithinRange(slot, BoxStart*BoxSlotCount, (BoxEnd + 1)*BoxSlotCount)); } - public void SortBoxes(int BoxStart = 0, int BoxEnd = -1, Func, IEnumerable> sortMethod = null, bool reverse = false) + public int SortBoxes(int BoxStart = 0, int BoxEnd = -1, Func, IEnumerable> sortMethod = null, bool reverse = false) { var BD = BoxData; int start = BoxSlotCount * BoxStart; @@ -701,7 +701,7 @@ public void SortBoxes(int BoxStart = 0, int BoxEnd = -1, Func, var result = Sorted.ToArray(); var boxclone = new PKM[BD.Count]; BD.CopyTo(boxclone, 0); - result.CopyTo(boxclone, skip, start); + int count = result.CopyTo(boxclone, skip, start); SlotPointerUtil.UpdateRepointFrom(boxclone, BD, 0, SlotPointers); @@ -710,14 +710,16 @@ public void SortBoxes(int BoxStart = 0, int BoxEnd = -1, Func, pk.StorageFlags = StorageSlotFlag.None; BoxData = boxclone; + return count; } - public void ClearBoxes(int BoxStart = 0, int BoxEnd = -1, Func deleteCriteria = null) + public int ClearBoxes(int BoxStart = 0, int BoxEnd = -1, Func deleteCriteria = null) { if (BoxEnd < 0) BoxEnd = BoxCount - 1; var blank = BlankPKM.EncryptedBoxData; + int deleted = 0; for (int i = BoxStart; i <= BoxEnd; i++) { for (int p = 0; p < BoxSlotCount; p++) @@ -725,6 +727,8 @@ public void ClearBoxes(int BoxStart = 0, int BoxEnd = -1, Func delete if (IsSlotOverwriteProtected(i, p)) continue; var ofs = GetBoxSlotOffset(i, p); + if (!IsPKMPresent(ofs)) + continue; if (deleteCriteria != null) { var pk = GetStoredSlot(ofs); @@ -733,27 +737,34 @@ public void ClearBoxes(int BoxStart = 0, int BoxEnd = -1, Func delete } SetData(blank, ofs); + ++deleted; } } + return deleted; } - public void ModifyBoxes(Action action, int BoxStart = 0, int BoxEnd = -1) + public int ModifyBoxes(Action action, int BoxStart = 0, int BoxEnd = -1) { if (BoxEnd < 0) BoxEnd = BoxCount - 1; var BD = BoxData; + int modified = 0; for (int b = BoxStart; b <= BoxEnd; b++) { for (int s = 0; s < BoxSlotCount; s++) { + var index = (b * BoxSlotCount) + s; + if (BD[index].Species == 0) + continue; if (IsSlotOverwriteProtected(b, s)) continue; - var index = (b * BoxSlotCount) + s; action(BD[index]); + ++modified; } } BoxData = BD; + return modified; } public byte[] PCBinary => BoxData.SelectMany(pk => pk.EncryptedBoxData).ToArray(); diff --git a/PKHeX.WinForms/Controls/SAV Editor/BoxMenuStrip.cs b/PKHeX.WinForms/Controls/SAV Editor/BoxMenuStrip.cs index 868c6e08e..8870d3d36 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/BoxMenuStrip.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/BoxMenuStrip.cs @@ -131,7 +131,7 @@ public BoxManipulatorWF(SAVEditor editor) Editor = editor; } - protected override void FinishBoxManipulation(string message, bool all) => Editor.FinishBoxManipulation(message, all); + protected override void FinishBoxManipulation(string message, bool all, int count) => Editor.FinishBoxManipulation(message, all, count); protected override bool CanManipulateRegion(int start, int end, string prompt, string fail) { diff --git a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs index 9c7e41775..4896d5a02 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs @@ -409,12 +409,12 @@ private void ClickBoxSort(object sender, MouseEventArgs e) SortMenu.Show(pt); } - public void FinishBoxManipulation(string message, bool all) + public void FinishBoxManipulation(string message, bool all, int count) { SetPKMBoxes(); UpdateBoxViewers(all); if (message != null) - WinFormsUtil.Alert(message); + WinFormsUtil.Alert(message + $" ({count})"); else SystemSounds.Asterisk.Play(); }