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 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 Undo => Snapshot(undoField); public IReadOnlyList 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 CommandsOf(object handle) => ((IEnumerable)commandsField.GetValue(handle)!).OfType().ToArray(); public bool TrySetRedo(IReadOnlyList nextFirst) { try { var list = (IList)redoField.GetValue(manager)!; list.Clear(); for (var index = nextFirst.Count - 1; index >= 0; index--) list.Add(nextFirst[index]); return true; } catch (Exception exception) when (exception is ArgumentException or InvalidCastException or NotSupportedException) { 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).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 Snapshot(FieldInfo field) { var list = (ICollection)field.GetValue(manager)!; var snapshot = new object[list.Count]; list.CopyTo(snapshot, 0); return snapshot; } }