diff --git a/PKHeX.Core/Editing/Saves/BoxManipClear.cs b/PKHeX.Core/Editing/Saves/BoxManipClear.cs index e2bc95840..f2a624bb7 100644 --- a/PKHeX.Core/Editing/Saves/BoxManipClear.cs +++ b/PKHeX.Core/Editing/Saves/BoxManipClear.cs @@ -1,29 +1,30 @@ using System; using System.Collections.Generic; -using System.Linq; using PKHeX.Core.Searching; namespace PKHeX.Core { - public sealed class BoxManipClear : IBoxManip + public class BoxManipClear : IBoxManip { - public BoxManipType Type { get; } + public BoxManipType Type { get; protected set; } public Func Usable { get; set; } public string GetPrompt(bool all) => all ? MessageStrings.MsgSaveBoxClearAll : MessageStrings.MsgSaveBoxClearCurrent; public string GetFail(bool all) => all ? MessageStrings.MsgSaveBoxClearAllFailBattle : MessageStrings.MsgSaveBoxClearCurrentFailBattle; public string GetSuccess(bool all) => all ? MessageStrings.MsgSaveBoxClearAllSuccess : MessageStrings.MsgSaveBoxClearCurrentSuccess; - private readonly Func CriteriaSimple; - private readonly Func CriteriaSAV; + protected Func CriteriaSimple { private get; set; } + protected Func CriteriaSAV { private get; set; } - public bool Execute(SaveFile SAV, BoxManipParam param) + public virtual bool 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; } + protected BoxManipClear() { } + private BoxManipClear(BoxManipType type, Func criteria, Func usable = null) { Type = type; @@ -47,15 +48,32 @@ namespace PKHeX.Core new BoxManipClear(BoxManipType.DeleteUntrained, pk => pk.EVTotal == 0), new BoxManipClear(BoxManipType.DeleteItemless, pk => pk.HeldItem == 0), new BoxManipClear(BoxManipType.DeleteIllegal, pk => !new LegalityAnalysis(pk).Valid), - new BoxManipClear(BoxManipType.DeleteClones, (pk, sav) => IsClone(pk, sav, CloneDetectionMethod.HashDetails)), + new BoxManipClearDuplicate(BoxManipType.DeleteClones, pk => SearchUtil.GetCloneDetectMethod(CloneDetectionMethod.HashDetails)(pk)), }; + } - private static bool IsClone(PKM pk, SaveFile sav, CloneDetectionMethod type) + public sealed class BoxManipClearDuplicate : BoxManipClear + { + private readonly HashSet HashSet = new HashSet(); + + public override bool Execute(SaveFile SAV, BoxManipParam param) { - var method = SearchUtil.GetCloneDetectMethod(type); - var ident = method(pk); - var count = sav.BoxData.Count(p => method(p) == ident); - return count > 1; + HashSet.Clear(); + return base.Execute(SAV, param); + } + + public BoxManipClearDuplicate(BoxManipType type, Func criteria, Func usable = null) + { + Type = type; + Usable = usable; + CriteriaSimple = pk => + { + var result = criteria(pk); + if (HashSet.Contains(result)) + return true; + HashSet.Add(result); + return false; + }; } } } \ No newline at end of file