2019-09-03 02:30:58 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tracks <see cref="PKM"/> slot changes and provides the ability to revert a change.
|
|
|
|
|
/// </summary>
|
2019-10-04 02:09:02 +00:00
|
|
|
|
public sealed class SlotChangelog
|
2019-09-03 02:30:58 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly SaveFile SAV;
|
2020-12-22 01:17:56 +00:00
|
|
|
|
private readonly Stack<SlotReversion> UndoStack = new();
|
|
|
|
|
private readonly Stack<SlotReversion> RedoStack = new();
|
2019-09-03 02:30:58 +00:00
|
|
|
|
|
|
|
|
|
public SlotChangelog(SaveFile sav) => SAV = sav;
|
|
|
|
|
|
|
|
|
|
public bool CanUndo => UndoStack.Count != 0;
|
|
|
|
|
public bool CanRedo => RedoStack.Count != 0;
|
|
|
|
|
|
|
|
|
|
public void AddNewChange(ISlotInfo info)
|
|
|
|
|
{
|
|
|
|
|
var revert = GetReversion(info, SAV);
|
|
|
|
|
AddUndo(revert);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ISlotInfo Undo()
|
|
|
|
|
{
|
|
|
|
|
var change = UndoStack.Pop();
|
|
|
|
|
var revert = GetReversion(change.Info, SAV);
|
|
|
|
|
AddRedo(revert);
|
|
|
|
|
change.Revert(SAV);
|
|
|
|
|
return change.Info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ISlotInfo Redo()
|
|
|
|
|
{
|
|
|
|
|
var change = RedoStack.Pop();
|
|
|
|
|
var revert = GetReversion(change.Info, SAV);
|
|
|
|
|
AddUndo(revert);
|
|
|
|
|
change.Revert(SAV);
|
|
|
|
|
return change.Info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddRedo(SlotReversion change)
|
|
|
|
|
{
|
|
|
|
|
RedoStack.Push(change);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddUndo(SlotReversion change)
|
|
|
|
|
{
|
|
|
|
|
UndoStack.Push(change);
|
|
|
|
|
RedoStack.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 01:08:49 +00:00
|
|
|
|
private static SlotReversion GetReversion(ISlotInfo info, SaveFile sav) => info switch
|
2019-09-03 02:30:58 +00:00
|
|
|
|
{
|
2021-01-02 01:08:49 +00:00
|
|
|
|
SlotInfoParty p => new PartyReversion(p, sav),
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => new SingleSlotReversion(info, sav),
|
2021-01-02 01:08:49 +00:00
|
|
|
|
};
|
2019-09-03 02:30:58 +00:00
|
|
|
|
|
|
|
|
|
private abstract class SlotReversion
|
|
|
|
|
{
|
|
|
|
|
internal readonly ISlotInfo Info;
|
|
|
|
|
protected SlotReversion(ISlotInfo info) => Info = info;
|
|
|
|
|
|
|
|
|
|
public abstract void Revert(SaveFile sav);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 20:51:13 +00:00
|
|
|
|
private sealed class PartyReversion : SlotReversion
|
2019-09-03 02:30:58 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IList<PKM> Party;
|
|
|
|
|
public PartyReversion(ISlotInfo info, SaveFile s) : base(info) => Party = s.PartyData;
|
|
|
|
|
|
|
|
|
|
public override void Revert(SaveFile sav) => sav.PartyData = Party;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 20:51:13 +00:00
|
|
|
|
private sealed class SingleSlotReversion : SlotReversion
|
2019-09-03 02:30:58 +00:00
|
|
|
|
{
|
2020-06-17 02:46:22 +00:00
|
|
|
|
private readonly PKM Entity;
|
|
|
|
|
public SingleSlotReversion(ISlotInfo info, SaveFile sav) : base(info) => Entity = info.Read(sav);
|
2019-09-03 02:30:58 +00:00
|
|
|
|
|
2020-06-17 02:46:22 +00:00
|
|
|
|
public override void Revert(SaveFile sav) => Info.WriteTo(sav, Entity, PKMImportSetting.Skip);
|
2019-09-03 02:30:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|