ファイル

158 行
5.5 KiB
C#

using System.Collections;
using System.Reflection;
using YukkuriMovieMaker.UndoRedo;
namespace UndoTree;
internal sealed class UndoRedoManagerReader : IUndoRedoHistory
{
const BindingFlags NonPublicInstance = BindingFlags.Instance | BindingFlags.NonPublic;
readonly UndoRedoManager manager;
readonly FieldInfo commandsField;
readonly bool canSwap;
FieldInfo undoField;
FieldInfo redoField;
internal UndoRedoManagerReader(UndoRedoManager manager, UndoRedoHistorySource source, FieldInfo undoField, FieldInfo redoField, FieldInfo commandsField, bool canSwap)
{
this.manager = manager;
Source = source;
this.undoField = undoField;
this.redoField = redoField;
this.commandsField = commandsField;
this.canSwap = canSwap;
}
public UndoRedoHistorySource Source { get; }
public bool CanReplay => true;
public bool CanRewriteUndo => true;
public bool CanRewriteRedo => true;
public bool IsConsistent
{
get
{
try
{
if (IsMatching())
return true;
if (!canSwap)
return false;
(undoField, redoField) = (redoField, undoField);
if (IsMatching())
return true;
(undoField, redoField) = (redoField, undoField);
return false;
}
catch (Exception exception) when (exception is TargetException or FieldAccessException or InvalidCastException or MemberAccessException)
{
return false;
}
}
}
public IReadOnlyList<object> Undo => Snapshot(undoField);
public IReadOnlyList<object> Redo => Snapshot(redoField);
public static UndoRedoManagerReader? Create(UndoRedoManager manager)
=> CreateByName(manager) ?? CreateByType(manager);
public static UndoRedoManagerReader? CreateByName(UndoRedoManager manager)
{
var type = typeof(UndoRedoManager);
var undo = type.GetField("undoList", NonPublicInstance);
var redo = type.GetField("redoList", NonPublicInstance);
if (undo is null || redo is null || ElementOf(undo.FieldType) is not { } element || ElementOf(redo.FieldType) != element)
return null;
var commands = CommandsFieldOf(element, "commands");
if (commands is null)
return null;
var reader = new UndoRedoManagerReader(manager, UndoRedoHistorySource.Name, undo, redo, commands, false);
return reader.IsConsistent ? reader : null;
}
public static UndoRedoManagerReader? CreateByType(UndoRedoManager manager)
{
var lists = typeof(UndoRedoManager).GetFields(NonPublicInstance)
.Where(field => ElementOf(field.FieldType) is { } element && CommandsFieldOf(element, null) is not null)
.OrderBy(field => field.MetadataToken)
.ToArray();
if (lists.Length != 2 || ElementOf(lists[0].FieldType) != ElementOf(lists[1].FieldType))
return null;
var commands = CommandsFieldOf(ElementOf(lists[0].FieldType)!, null)!;
var reader = new UndoRedoManagerReader(manager, UndoRedoHistorySource.Type, lists[0], lists[1], commands, true);
return reader.IsConsistent ? reader : null;
}
public IReadOnlyList<IUndoRedoCommandBase> CommandsOf(object handle)
=> ((IEnumerable)commandsField.GetValue(handle)!).OfType<IUndoRedoCommandBase>().ToArray();
public bool TrySetUndo(IReadOnlyList<object> oldestFirst) => TryReplace(undoField, oldestFirst);
public bool TrySetRedo(IReadOnlyList<object> nextFirst) => TryReplace(redoField, nextFirst.Reverse().ToArray());
bool TryReplace(FieldInfo field, IReadOnlyList<object> entries)
{
IList? list = null;
object[] previous = [];
try
{
list = (IList)field.GetValue(manager)!;
previous = list.Cast<object>().ToArray();
list.Clear();
foreach (var entry in entries)
list.Add(entry);
return true;
}
catch (Exception exception) when (exception is ArgumentException or InvalidCastException or NotSupportedException)
{
if (list is not null)
{
list.Clear();
foreach (var entry in previous)
list.Add(entry);
}
return false;
}
}
static Type? ElementOf(Type type)
=> type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>) ? type.GetGenericArguments()[0] : null;
static FieldInfo? CommandsFieldOf(Type element, string? name)
{
var fields = element.GetFields(NonPublicInstance);
if (name is not null)
return fields.FirstOrDefault(field => field.Name == name && typeof(IList).IsAssignableFrom(field.FieldType));
var candidates = fields.Where(field => typeof(IList<IUndoRedoCommandBase>).IsAssignableFrom(field.FieldType) && typeof(IList).IsAssignableFrom(field.FieldType)).ToArray();
return candidates.Length == 1 ? candidates[0] : null;
}
bool IsMatching()
=> (Count(undoField) > 0) == manager.IsUndoable && (Count(redoField) > 0) == manager.IsRedoable;
int Count(FieldInfo field) => ((ICollection)field.GetValue(manager)!).Count;
IReadOnlyList<object> Snapshot(FieldInfo field)
{
var list = (ICollection)field.GetValue(manager)!;
var snapshot = new object[list.Count];
list.CopyTo(snapshot, 0);
return snapshot;
}
}