241 行
9.8 KiB
C#
241 行
9.8 KiB
C#
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 IReadOnlyList<object> AddedItems { get; private set; } = [];
|
|
|
|
public IReadOnlyList<object> RemovedItems { get; private set; } = [];
|
|
|
|
public int AddedAt { get; private set; }
|
|
|
|
public int RemovedAt { get; private set; }
|
|
|
|
public IEnumerable<object> AddedObjects => this switch
|
|
{
|
|
{ Kind: CommandKind.ListItems } => AddedItems,
|
|
{ Kind: CommandKind.Property, NewValue: IUndoRedoable value } => [value],
|
|
{ Kind: CommandKind.Property, OldValue: IEnumerable and not string, NewValue: IEnumerable and not string } => (UndoTreeMembers.ItemsOf(NewValue) ?? []).Except(UndoTreeMembers.ItemsOf(OldValue) ?? [], ReferenceEqualityComparer.Instance),
|
|
_ => [],
|
|
};
|
|
|
|
public object? CallTarget { get; private set; }
|
|
|
|
public string CallMethod { get; private set; } = string.Empty;
|
|
|
|
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);
|
|
}
|
|
|
|
if (command is UndoRedoAsyncTaskCommand)
|
|
return ReadCall(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 changes = Kind is CommandKind.Property or CommandKind.Indexer && IsReadable;
|
|
var before = changes ? TextOf(OldValue) : string.Empty;
|
|
var after = changes ? TextOf(NewValue) : string.Empty;
|
|
var added = AddedObjects.ToArray();
|
|
return new UndoTreeCommandInfo(Kind, Target, targetLabel, member, changes ? $"{before} → {after}" : string.Empty, Key, ChangesOwnership, added, Before: before, After: after, AddedCount: Kind == CommandKind.ListItems ? Added : added.Length, RemovedCount: Removed);
|
|
}
|
|
|
|
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.AddedItems = (added.GetValue(command) as IEnumerable)?.Cast<object>().ToArray() ?? [];
|
|
view.RemovedItems = (removed.GetValue(command) as IEnumerable)?.Cast<object>().ToArray() ?? [];
|
|
view.Added = view.AddedItems.Count;
|
|
view.Removed = view.RemovedItems.Count;
|
|
view.AddedAt = type.GetField("addedItemsStartingIndex", NonPublicInstance)?.GetValue(command) as int? ?? 0;
|
|
view.RemovedAt = type.GetField("removedItemsStartingIndex", NonPublicInstance)?.GetValue(command) as int? ?? 0;
|
|
view.IsReadable = true;
|
|
return view;
|
|
}
|
|
|
|
static UndoRedoCommandView ReadCall(IUndoRedoCommandBase command)
|
|
{
|
|
var view = new UndoRedoCommandView(command, CommandKind.Action);
|
|
var delegates = command.GetType().GetFields(NonPublicInstance)
|
|
.Where(field => field.FieldType == typeof(Func<Task>))
|
|
.Select(field => field.GetValue(command) as Delegate)
|
|
.ToArray();
|
|
if (delegates.FirstOrDefault() is not { Target: { } target } first || delegates.Any(delegated => delegated?.Method.Name != first.Method.Name || !ReferenceEquals(delegated.Target, target)))
|
|
return view;
|
|
|
|
view.CallTarget = target;
|
|
view.CallMethod = first.Method.Name;
|
|
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<UndoRedoEventArgs>));
|
|
}
|
|
|
|
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<DisplayAttribute>()?.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);
|
|
}
|
|
}
|