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