using System.Collections; using System.ComponentModel.DataAnnotations; using System.Reflection; using YukkuriMovieMaker.Plugin.Effects; using YukkuriMovieMaker.UndoRedo; namespace UndoTree; internal sealed class UndoRedoCommandView { const BindingFlags NonPublicInstance = BindingFlags.Instance | BindingFlags.NonPublic; static readonly FieldInfo? CreatedField = FindCreatedField(); UndoRedoCommandView(IUndoRedoCommandBase command, CommandKind kind) { Command = command; Kind = kind; } public IUndoRedoCommandBase Command { get; } public CommandKind Kind { get; } public object? Target { get; private set; } public string Member { get; private set; } = string.Empty; public object? OldValue { get; private set; } public object? NewValue { get; private set; } public int Added { get; private set; } public int Removed { get; private set; } public bool IsReadable { get; private set; } public UndoTreeChangeKey? Key => Target is not null && Kind is CommandKind.Property or CommandKind.Indexer && IsReadable ? new UndoTreeChangeKey(Target, Member) : null; public static UndoRedoCommandView Read(IUndoRedoCommandBase command) { var type = command.GetType(); if (type.IsGenericType) { var definition = type.GetGenericTypeDefinition(); if (definition == typeof(UndoRedoPropertyChangedCommand<,>)) return ReadValueChange(command, CommandKind.Property, "propertyName"); if (definition == typeof(UndoRedoIndexerValueChangedCommand<,,>)) return ReadValueChange(command, CommandKind.Indexer, "index"); if (definition == typeof(UndoRedoAddRemoveListItemsCommand<>)) return ReadListItems(command); } return new UndoRedoCommandView(command, command.IsAction ? CommandKind.Action : CommandKind.Other); } public static bool IsLive(object target) { if (target is not UndoRedoable || CreatedField is null) return true; return CreatedField.GetValue(target) is not null; } public UndoTreeCommandInfo Describe() { var targetLabel = Kind is CommandKind.Property or CommandKind.Indexer ? Target is null ? Texts.UnnamedTarget : LabelOf(Target) : string.Empty; var member = Kind switch { CommandKind.Property => MemberLabel(Target, Member), CommandKind.Indexer => $"[{Member}]", CommandKind.ListItems => ListChangeOf(Added, Removed), CommandKind.Action => Texts.Operation, _ => Command.GetType().Name, }; var detail = Kind is CommandKind.Property or CommandKind.Indexer && IsReadable ? $"{TextOf(OldValue)} → {TextOf(NewValue)}" : string.Empty; return new UndoTreeCommandInfo(Kind, Target, targetLabel, member, detail, Key, ChangesOwnership); } bool ChangesOwnership => Kind is CommandKind.Property or CommandKind.Indexer ? !IsReadable || HoldsObject(OldValue) || HoldsObject(NewValue) : true; static bool HoldsObject(object? value) => value is not null and not string && !value.GetType().IsValueType; static UndoRedoCommandView ReadValueChange(IUndoRedoCommandBase command, CommandKind kind, string memberField) { var view = new UndoRedoCommandView(command, kind); var type = command.GetType(); var instance = type.GetField("instance", NonPublicInstance); var member = type.GetField(memberField, NonPublicInstance); var previous = type.GetField("oldValue", NonPublicInstance); var next = type.GetField("newValue", NonPublicInstance); if (instance is null || member is null || previous is null || next is null) return view; view.Target = instance.GetValue(command); view.Member = member.GetValue(command)?.ToString() ?? string.Empty; view.OldValue = previous.GetValue(command); view.NewValue = next.GetValue(command); view.IsReadable = view.Target is not null && view.Member.Length > 0; return view; } static UndoRedoCommandView ReadListItems(IUndoRedoCommandBase command) { var view = new UndoRedoCommandView(command, CommandKind.ListItems); var type = command.GetType(); var list = type.GetField("list", NonPublicInstance); var added = type.GetField("addedItems", NonPublicInstance); var removed = type.GetField("removedItems", NonPublicInstance); if (list is null || added is null || removed is null) return view; view.Target = list.GetValue(command); view.Added = (added.GetValue(command) as ICollection)?.Count ?? 0; view.Removed = (removed.GetValue(command) as ICollection)?.Count ?? 0; view.IsReadable = true; return view; } static FieldInfo? FindCreatedField() { var type = typeof(UndoRedoable); return type.GetField(nameof(IUndoRedoable.UndoRedoCommandCreated), NonPublicInstance) ?? type.GetFields(NonPublicInstance).FirstOrDefault(field => field.FieldType == typeof(EventHandler)); } public static bool TryGetLabel(object target, out string label) { label = string.Empty; try { if (target.GetType().GetProperty(nameof(IVideoEffect.Label), BindingFlags.Instance | BindingFlags.Public)?.GetValue(target) is not string { Length: > 0 } text) return false; label = UndoTreeText.Shorten(UndoTreeText.SingleLine(text)); return true; } catch (Exception exception) when (exception is TargetInvocationException or AmbiguousMatchException or TargetException) { return false; } } public static string LabelOf(object target) => TryGetLabel(target, out var label) ? label : UndoTreeNames.OfType(target.GetType()); internal static string MemberLabel(object? target, string member) { Type? owner = null; try { var property = target?.GetType().GetProperty(member, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); owner = property?.DeclaringType; var name = property?.GetCustomAttribute()?.GetName(); if (!string.IsNullOrEmpty(name)) return name; } catch (Exception exception) when (exception is InvalidOperationException or AmbiguousMatchException or TargetInvocationException) { } return UndoTreeNames.OfMember(owner, member); } internal static string ListChangeOf(int added, int removed) => (added, removed) switch { (> 0, 0) => string.Format(Texts.ListAdded, added), (0, > 0) => string.Format(Texts.ListRemoved, removed), _ => string.Format(Texts.ListChange, added, removed), }; internal static string TextOf(object? value) => value switch { null => Texts.ValueNone, string text => UndoTreeText.Shorten($"\"{UndoTreeText.SingleLine(text)}\""), bool flag => flag ? Texts.ValueOn : Texts.ValueOff, Enum enumeration => UndoTreeText.Shorten(UndoTreeNames.OfEnum(enumeration)), Type type => UndoTreeText.Shorten(UndoTreeNames.OfPlugin(type)), ICollection collection => $"[{collection.Count}]", _ => UndoTreeText.Shorten(ObjectText(value)), }; static string ObjectText(object value) { if (TryGetLabel(value, out var label)) return label; var text = value.ToString(); return string.IsNullOrEmpty(text) || text == value.GetType().ToString() ? UndoTreeNames.OfType(value.GetType()) : UndoTreeText.SingleLine(text); } }