namespace PKHeX.Core
{
public abstract class BoxManipulator
{
protected abstract SaveFile SAV { get; }
///
/// Executes the provided with the provided parameters.
///
/// Manipulation to perform on the box data.
/// Single box to modify; if is set, this param is ignored.
/// Indicates if all boxes are to be manipulated, or just one box.
/// Manipulation action should be inverted (criteria) or reversed (sort).
/// True if operation succeeded, false if no changes made.
public bool Execute(IBoxManip manip, int box, bool allBoxes, bool reverse = false)
{
bool usable = manip.Usable?.Invoke(SAV) ?? true;
if (!usable)
return false;
var param = new BoxManipParam
{
Reverse = reverse,
Start = allBoxes ? 0 : box,
Stop = allBoxes ? SAV.BoxCount - 1 : box,
};
var prompt = manip.GetPrompt(allBoxes);
var fail = manip.GetFail(allBoxes);
if (!CanManipulateRegion(param.Start, param.Stop, prompt, fail))
return false;
var result = manip.Execute(SAV, param);
if (result <= 0)
return false;
var success = manip.GetSuccess(allBoxes);
FinishBoxManipulation(success, allBoxes, result);
return true;
}
///
/// Executes the provided with the provided parameters.
///
/// Manipulation to perform on the box data.
/// Single box to modify; if is set, this param is ignored.
/// Indicates if all boxes are to be manipulated, or just one box.
/// Manipulation action should be inverted (criteria) or reversed (sort).
/// True if operation succeeded, false if no changes made.
public bool Execute(BoxManipType type, int box, bool allBoxes, bool reverse = false)
{
var manip = type.GetManip();
return Execute(manip, box, allBoxes, reverse);
}
///
/// Sanity check for modifying the box data.
///
/// Start box
/// End box
/// Message asking about the operation.
/// Message indicating the operation cannot be performed.
///
protected virtual bool CanManipulateRegion(int start, int end, string prompt, string fail) => !SAV.IsAnySlotLockedInBox(start, end);
///
/// Called if the operation modified any data.
///
/// Optional message to show if applicable.
/// Indicates if all boxes were manipulated, or just one box.
/// Count of manipulated slots
protected abstract void FinishBoxManipulation(string message, bool all, int count);
}
}