PKHeX/PKHeX.Core/Editing/Saves/BoxManipClearDuplicate.cs
Kurt d7e1f65faa Add some xmldoc, file scoped namespaces
Not much of a git diff history for these files so I don't mind them being reformatted.
2021-12-09 18:58:30 -08:00

38 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
namespace PKHeX.Core;
/// <summary>
/// Clears contents of boxes by deleting all but the first duplicate detected.
/// </summary>
/// <typeparam name="T">Base type of the "is duplicate" hash for the duplicate detection.</typeparam>
public sealed class BoxManipClearDuplicate<T> : BoxManipBase
{
private readonly HashSet<T> HashSet = new();
private readonly Func<PKM, bool> Criteria;
public BoxManipClearDuplicate(BoxManipType type, Func<PKM, T> criteria) : this(type, criteria, _ => true) { }
public BoxManipClearDuplicate(BoxManipType type, Func<PKM, T> criteria, Func<SaveFile, bool> usable) : base(type, usable)
{
Criteria = pk =>
{
var result = criteria(pk);
if (HashSet.Contains(result))
return true;
HashSet.Add(result);
return false;
};
}
public override string GetPrompt(bool all) => all ? MessageStrings.MsgSaveBoxClearAll : MessageStrings.MsgSaveBoxClearCurrent;
public override string GetFail(bool all) => all ? MessageStrings.MsgSaveBoxClearAllFailBattle : MessageStrings.MsgSaveBoxClearCurrentFailBattle;
public override string GetSuccess(bool all) => all ? MessageStrings.MsgSaveBoxClearAllSuccess : MessageStrings.MsgSaveBoxClearCurrentSuccess;
public override int Execute(SaveFile sav, BoxManipParam param)
{
HashSet.Clear();
bool Method(PKM p) => param.Reverse ^ Criteria(p);
return sav.ClearBoxes(param.Start, param.Stop, Method);
}
}