From 3331e260bfaf5c4f0e244f23b45fe785bb300e53 Mon Sep 17 00:00:00 2001 From: Arne Kiesewetter Date: Wed, 2 Oct 2024 23:50:23 +0200 Subject: [PATCH] Remove array editing patches --- .../UI/SyncArrayEditor.cs | 426 ------------------ 1 file changed, 426 deletions(-) delete mode 100644 MonkeyLoader.Resonite.Integration/UI/SyncArrayEditor.cs diff --git a/MonkeyLoader.Resonite.Integration/UI/SyncArrayEditor.cs b/MonkeyLoader.Resonite.Integration/UI/SyncArrayEditor.cs deleted file mode 100644 index 6e0f162..0000000 --- a/MonkeyLoader.Resonite.Integration/UI/SyncArrayEditor.cs +++ /dev/null @@ -1,426 +0,0 @@ -using Elements.Core; -using FrooxEngine.UIX; -using FrooxEngine; -using HarmonyLib; -using MonkeyLoader.Patching; -using System; -using System.Diagnostics.CodeAnalysis; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; -using EnumerableToolkit; -using MonkeyLoader.Resonite.UI.Inspectors; - -namespace MonkeyLoader.Resonite.UI -{ - internal sealed class SyncArrayEditor : ResoniteCancelableEventHandlerMonkey - { - private static readonly MethodInfo _addCurveValueProxying = AccessTools.Method(typeof(SyncArrayEditor), nameof(AddCurveValueProxying)); - private static readonly MethodInfo _addLinearValueProxying = AccessTools.Method(typeof(SyncArrayEditor), nameof(AddLinearValueProxying)); - private static readonly MethodInfo _addListReferenceProxying = AccessTools.Method(typeof(SyncArrayEditor), nameof(AddListReferenceProxying)); - private static readonly MethodInfo _addListValueProxying = AccessTools.Method(typeof(SyncArrayEditor), nameof(AddListValueProxying)); - private static readonly Type _iWorldElementType = typeof(IWorldElement); - private static readonly Type _particleBurstType = typeof(ParticleBurst); - - public override bool CanBeDisabled => true; - - public override int Priority => HarmonyLib.Priority.High; - public override bool SkipCanceled => true; - - protected override bool AppliesTo(BuildArrayEditorEvent eventData) => Enabled; - - protected override IEnumerable GetFeaturePatches() => []; - - protected override void Handle(BuildArrayEditorEvent eventData) - => eventData.Canceled = BuildArray(eventData.Member, eventData.Name, eventData.FieldInfo, eventData.UI, eventData.LabelSize!.Value); - - private static void AddCurveValueProxying(SyncArray> array, SyncElementList.Point> list) - where T : IEquatable - { - foreach (var key in array) - { - var point = list.Add(); - point.Position.Value = key.time; - point.Value.Value = key.value; - } - - AddUpdateProxies(array, list, list.Elements); - - list.ElementsAdded += (list, startIndex, count) => - { - var addedElements = list.Elements.Skip(startIndex).Take(count).ToArray(); - var buffer = addedElements.Select(point => new CurveKey(point.Position, point.Value)).ToArray(); - - array.Insert(buffer, startIndex); - AddUpdateProxies(array, list, addedElements); - }; - - list.ElementsRemoved += (list, startIndex, count) => array.Remove(startIndex, count); - } - - private static void AddLinearValueProxying(SyncArray> array, SyncElementList.Point> list) - where T : IEquatable - { - foreach (var key in array) - { - var point = list.Add(); - point.Position.Value = key.time; - point.Value.Value = key.value; - } - - AddUpdateProxies(array, list, list.Elements); - - list.ElementsAdded += (list, startIndex, count) => - { - var addedElements = list.Elements.Skip(startIndex).Take(count).ToArray(); - var buffer = addedElements.Select(point => new LinearKey(point.Position, point.Value)).ToArray(); - - array.Insert(buffer, startIndex); - AddUpdateProxies(array, list, addedElements); - }; - - list.ElementsRemoved += (list, startIndex, count) => array.Remove(startIndex, count); - } - - private static void AddListReferenceProxying(SyncArray array, SyncElementList> list) - where T : class, IEquatable, IWorldElement - { - foreach (var reference in array) - { - var syncRef = list.Add(); - syncRef.Target = reference; - } - - AddUpdateProxies(array, list, list.Elements); - - list.ElementsAdded += (list, startIndex, count) => - { - var addedElements = list.Elements.Skip(startIndex).Take(count).ToArray(); - var buffer = addedElements.Select(syncRef => syncRef.Target).ToArray(); - - array.Insert(buffer, startIndex); - AddUpdateProxies(array, list, addedElements); - }; - - list.ElementsRemoved += (list, startIndex, count) => array.Remove(startIndex, count); - } - - private static void AddListValueProxying(SyncArray array, SyncElementList> list) - where T : IEquatable - { - foreach (var value in array) - { - var sync = list.Add(); - sync.Value = value; - } - - AddUpdateProxies(array, list, list.Elements); - - list.ElementsAdded += (list, startIndex, count) => - { - var addedElements = list.Elements.Skip(startIndex).Take(count).ToArray(); - var buffer = addedElements.Select(sync => sync.Value).ToArray(); - - array.Insert(buffer, startIndex); - AddUpdateProxies(array, list, addedElements); - }; - - list.ElementsRemoved += (list, startIndex, count) => array.Remove(startIndex, count); - } - - private static void AddParticleBurstListProxying(SyncArray> array, SyncElementList.Point> list) - { - foreach (var burst in array) - { - var point = list.Add(); - point.Position.Value = burst.time; - point.Value.Value = new int2(burst.value.minCount, burst.value.maxCount); - } - - AddUpdateProxies(array, list, list.Elements); - - list.ElementsAdded += (list, startIndex, count) => - { - var addedElements = list.Elements.Skip(startIndex).Take(count).ToArray(); - var buffer = addedElements.Select(point => new LinearKey(point.Position, new ParticleBurst() { minCount = point.Value.Value.x, maxCount = point.Value.Value.y })).ToArray(); - - array.Insert(buffer, startIndex); - AddUpdateProxies(array, list, addedElements); - }; - - list.ElementsRemoved += (list, startIndex, count) => array.Remove(startIndex, count); - } - - private static void AddTubePointProxying(SyncArray array, SyncElementList.Point> list) - { - foreach (var tubePoint in array) - { - var point = list.Add(); - point.Position.Value = tubePoint.radius; - point.Value.Value = tubePoint.position; - } - - AddUpdateProxies(array, list, list.Elements); - - list.ElementsAdded += (list, startIndex, count) => - { - var addedElements = list.Elements.Skip(startIndex).Take(count).ToArray(); - var buffer = addedElements.Select(point => new TubePoint(point.Value.Value, point.Position.Value)).ToArray(); - - array.Insert(buffer, startIndex); - AddUpdateProxies(array, list, addedElements); - }; - - list.ElementsRemoved += (list, startIndex, count) => array.Remove(startIndex, count); - } - - private static void AddUpdateProxies(SyncArray> array, - SyncElementList.Point> list, IEnumerable.Point> elements) - where T : IEquatable - { - foreach (var point in elements) - { - point.Changed += syncObject => - { - var index = list.IndexOfElement(point); - array[index] = new LinearKey(point.Position, point.Value); - }; - } - } - - private static void AddUpdateProxies(SyncArray> array, - SyncElementList.Point> list, IEnumerable.Point> elements) - { - foreach (var point in elements) - { - point.Changed += field => - { - var index = list.IndexOfElement(point); - var key = new LinearKey(point.Position, new ParticleBurst() { minCount = point.Value.Value.x, maxCount = point.Value.Value.y }); - array[index] = key; - }; - } - } - - private static void AddUpdateProxies(SyncArray array, SyncElementList> list, IEnumerable> elements) - where T : IEquatable - { - foreach (var sync in elements) - { - sync.OnValueChange += field => - { - var index = list.IndexOfElement(sync); - array[index] = sync.Value; - }; - } - } - - private static void AddUpdateProxies(SyncArray array, SyncElementList> list, IEnumerable> elements) - where T : class, IEquatable, IWorldElement - { - foreach (var sync in elements) - { - sync.OnValueChange += field => - { - var index = list.IndexOfElement(sync); - array[index] = sync.Target; - }; - } - } - - private static void AddUpdateProxies(SyncArray array, SyncElementList.Point> list, IEnumerable.Point> elements) - { - foreach (var point in elements) - { - point.Changed += field => - { - var index = list.IndexOfElement(point); - var tubePoint = new TubePoint(point.Value.Value, point.Position.Value); - array[index] = tubePoint; - }; - } - } - - private static void AddUpdateProxies(SyncArray> array, - SyncElementList.Point> list, IEnumerable.Point> elements) - where T : IEquatable - { - foreach (var point in elements) - { - point.Changed += syncObject => - { - var index = list.IndexOfElement(point); - array[index] = new CurveKey(point.Position, point.Value, array[index].leftTangent, array[index].rightTangent); - }; - } - } - - private static bool BuildArray(ISyncArray array, string name, FieldInfo fieldInfo, UIBuilder ui, float labelSize) - { - if (!TryGetGenericParameters(typeof(SyncArray<>), array.GetType(), out var genericParameters)) - return false; - - var isSyncLinear = TryGetGenericParameters(typeof(SyncLinear<>), array.GetType(), out var syncLinearGenericParameters); - - var isSyncCurve = TryGetGenericParameters(typeof(SyncCurve<>), array.GetType(), out var syncCurveGenericParameters); - - var arrayType = genericParameters!.Value.First(); - var syncLinearType = syncLinearGenericParameters?.First(); - var syncCurveType = syncCurveGenericParameters?.First(); - - var isParticleBurst = syncLinearType == _particleBurstType; - - if (isSyncLinear && isParticleBurst) - syncLinearType = typeof(int2); - - var proxySlotName = $"{name}-{array.ReferenceID}-Proxy"; - var proxiesSlot = ui.World.AssetsSlot; - if (proxiesSlot.FindChild(proxySlotName) is not Slot proxySlot) - { - proxySlot = proxiesSlot.AddSlot(proxySlotName); - array.FindNearestParent().Destroyed += (IDestroyable _) => proxySlot.Destroy(); - } - proxySlot.DestroyWhenLocalUserLeaves(); - - ISyncList list; - FieldInfo listField; - - if (isSyncLinear && SupportsLerp(syncLinearType!)) - { - var gradientType = typeof(ValueGradientDriver<>).MakeGenericType(syncLinearType); - var gradient = GetOrAttachComponent(proxySlot, gradientType, out var attachedNew); - - list = (ISyncList)gradient.GetSyncMember(nameof(ValueGradientDriver.Points)); - listField = gradient.GetSyncMemberFieldInfo(nameof(ValueGradientDriver.Points)); - - if (attachedNew) - { - if (isParticleBurst) - AddParticleBurstListProxying((SyncArray>)array, (SyncElementList.Point>)list); - else - _addLinearValueProxying.MakeGenericMethod(syncLinearType).Invoke(null, [array, list]); - } - } - else if (isSyncCurve && SupportsLerp(syncCurveType!)) - { - var gradientType = typeof(ValueGradientDriver<>).MakeGenericType(syncCurveType); - var gradient = GetOrAttachComponent(proxySlot, gradientType, out var attachedNew); - - list = (ISyncList)gradient.GetSyncMember(nameof(ValueGradientDriver.Points)); - listField = gradient.GetSyncMemberFieldInfo(nameof(ValueGradientDriver.Points)); - - if (attachedNew) - { - _addCurveValueProxying.MakeGenericMethod(syncCurveType).Invoke(null, [array, list]); - } - } - else - { - if (arrayType == typeof(TubePoint)) - { - var gradient = GetOrAttachComponent(proxySlot, typeof(ValueGradientDriver), out var attachedNew); - - list = (ISyncList)gradient.GetSyncMember(nameof(ValueGradientDriver.Points)); - listField = gradient.GetSyncMemberFieldInfo(nameof(ValueGradientDriver.Points)); - - if (attachedNew) - { - AddTubePointProxying((SyncArray)array, (SyncElementList.Point>)list); - } - } - else if (Coder.IsEnginePrimitive(arrayType)) - { - var multiplexerType = typeof(ValueMultiplexer<>).MakeGenericType(arrayType); - var multiplexer = GetOrAttachComponent(proxySlot, multiplexerType, out var attachedNew); - list = (ISyncList)multiplexer.GetSyncMember(nameof(ValueMultiplexer.Values)); - listField = multiplexer.GetSyncMemberFieldInfo(nameof(ValueMultiplexer.Values)); - - if (attachedNew) - _addListValueProxying.MakeGenericMethod(arrayType).Invoke(null, [array, list]); - } - else if (_iWorldElementType.IsAssignableFrom(arrayType)) - { - var multiplexerType = typeof(ReferenceMultiplexer<>).MakeGenericType(arrayType); - var multiplexer = GetOrAttachComponent(proxySlot, multiplexerType, out var attachedNew); - list = (ISyncList)multiplexer.GetSyncMember(nameof(ReferenceMultiplexer.References)); - listField = multiplexer.GetSyncMemberFieldInfo(nameof(ReferenceMultiplexer.References)); - - if (attachedNew) - _addListReferenceProxying.MakeGenericMethod(arrayType).Invoke(null, [array, list]); - } - else - { - proxySlot.Destroy(); - return false; - } - } - - ui.Panel().Slot.GetComponent(); - var memberFieldSlot = SyncMemberEditorBuilder.GenerateMemberField(array, name, ui, labelSize); - ui.NestOut(); - if (!array.IsDriven) - { - SyncMemberEditorBuilder.BuildList(list, name, listField, ui); - var listSlot = ui.Current; - listSlot.DestroyWhenLocalUserLeaves(); - void ArrayChanged(IChangeable changeable) - { - if (((ISyncArray)changeable).IsDriven) - { - listSlot.DestroyChildren(); - listSlot.Components.ToArray().Do((Component c) => c.Destroy()); - listSlot.AttachComponent().MinHeight.Value = 24f; - var newUi = new UIBuilder(listSlot, listSlot); - RadiantUI_Constants.SetupEditorStyle(newUi); - newUi.Text("(array is driven)"); - proxySlot?.Destroy(); - array.Changed -= ArrayChanged; - } - } - array.Changed += ArrayChanged; - } - else - { - LocaleString text = "(array is driven)"; - ui.Text(in text); - } - - return true; - } - - private static Component GetOrAttachComponent(Slot targetSlot, Type type, out bool attachedNew) - { - attachedNew = false; - if (targetSlot.GetComponent(type) is not Component comp) - { - comp = targetSlot.AttachComponent(type); - attachedNew = true; - } - return comp; - } - - private static bool SupportsLerp(Type type) - { - var coderType = typeof(Coder<>).MakeGenericType(type); - return Traverse.Create(coderType).Property(nameof(Coder.SupportsLerp)).Value; - } - - private static bool TryGetGenericParameters(Type baseType, Type concreteType, [NotNullWhen(true)] out Sequence? genericParameters) - { - genericParameters = null; - - if (concreteType is null || baseType is null || !baseType.IsGenericType) - return false; - - if (concreteType.IsGenericType && concreteType.GetGenericTypeDefinition() == baseType) - { - genericParameters = concreteType.GetGenericArguments(); - return true; - } - - return TryGetGenericParameters(baseType, concreteType.BaseType, out genericParameters); - } - } -} \ No newline at end of file