using System.Collections; using System.Collections.Concurrent; using System.Collections.Specialized; using System.Reflection; using YukkuriMovieMaker.UndoRedo; namespace UndoTree; internal readonly record struct UndoTreeOwnerLink(object Owner, PropertyInfo Property, bool IsElement); internal class UndoTreeOwners { const int MaxDepth = 32; static readonly ConcurrentDictionary Properties = new(); readonly Dictionary parents = new(ReferenceEqualityComparer.Instance); public UndoTreeOwners(IEnumerable roots) { var rootSet = new HashSet(ReferenceEqualityComparer.Instance); var pending = new Queue<(object Owner, int Depth)>(); foreach (var root in roots) { if (rootSet.Add(root)) pending.Enqueue((root, 0)); } while (pending.Count > 0) { var (owner, depth) = pending.Dequeue(); if (depth < MaxDepth) Visit(owner, depth + 1, rootSet, pending); } } public UndoTreeOwnerLink? LinkOf(object target) => parents.TryGetValue(target, out var link) ? link : null; public IReadOnlyList OwnersOf(object target) { var owners = new List(); for (var current = target; parents.TryGetValue(current, out var link); current = link.Owner) owners.Add(link.Owner); owners.Reverse(); return owners; } void Visit(object owner, int childDepth, HashSet rootSet, Queue<(object Owner, int Depth)> pending) { foreach (var property in PropertiesOf(owner.GetType())) { object? value; try { value = property.GetValue(owner); } catch (TargetInvocationException) { continue; } if (value is IUndoRedoable child) { Link(child, new UndoTreeOwnerLink(owner, property, false), childDepth, rootSet, pending); continue; } if (value is not IEnumerable items || value is string) continue; if (value is INotifyCollectionChanged) parents.TryAdd(value, new UndoTreeOwnerLink(owner, property, false)); foreach (var item in items) { if (item is IUndoRedoable member) Link(member, new UndoTreeOwnerLink(owner, property, true), childDepth, rootSet, pending); } } } void Link(object child, UndoTreeOwnerLink link, int childDepth, HashSet rootSet, Queue<(object Owner, int Depth)> pending) { if (rootSet.Contains(child) || !parents.TryAdd(child, link)) return; pending.Enqueue((child, childDepth)); } static PropertyInfo[] PropertiesOf(Type type) => Properties.GetOrAdd(type, key => key.IsValueType ? [] : key.GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(IsLink) .ToArray()); static bool IsLink(PropertyInfo property) { var type = property.PropertyType; return property.CanRead && property.GetIndexParameters().Length == 0 && !type.IsValueType && type != typeof(string) && property.GetCustomAttribute() is null && (property.CanWrite || typeof(INotifyCollectionChanged).IsAssignableFrom(type) || typeof(IUndoRedoable).IsAssignableFrom(type)) && CanHoldChildren(type); } static bool CanHoldChildren(Type type) { if (typeof(IUndoRedoable).IsAssignableFrom(type)) return true; if (!typeof(IEnumerable).IsAssignableFrom(type)) return !type.IsSealed; var elements = type.GetInterfaces().Append(type) .Where(candidate => candidate.IsGenericType && candidate.GetGenericTypeDefinition() == typeof(IEnumerable<>)) .Select(candidate => candidate.GetGenericArguments()[0]) .ToArray(); return elements.Length == 0 || elements.Any(element => typeof(IUndoRedoable).IsAssignableFrom(element) || !(element.IsValueType || element.IsSealed)); } }