From 80dcb922c21aa23099929993c9511f808612834e Mon Sep 17 00:00:00 2001 From: uurha Date: Sun, 1 Dec 2024 02:13:13 +0100 Subject: [PATCH 1/2] Implement Bindings for Handlers --- .../Editor/CustomEditors/MultiEditor.cs | 3 +- .../Editor/Drawers/BasePropertyDrawer.cs.meta | 3 - .../Editor/Drawers/HandlerBinderRegistry.cs | 34 ------ .../Editor/Drawers/HandlerBinding.meta | 3 + .../Editor/Drawers/HandlerBinding/Binding.cs | 102 ++++++++++++++++++ .../Drawers/HandlerBinding/Binding.cs.meta | 3 + .../Drawers/HandlerBinding/BindingInfo.cs | 18 ++++ .../HandlerBinding/BindingInfo.cs.meta | 3 + .../Drawers/HandlerBinding/Filters.meta | 3 + .../Filters/AttributeHandlersFilter.cs | 27 +++++ .../Filters/AttributeHandlersFilter.cs.meta | 3 + .../Filters/FieldHandlersFilter.cs | 24 +++++ .../Filters/FieldHandlersFilter.cs.meta | 3 + .../HandlerBinding/Filters/HandlersFilter.cs | 35 ++++++ .../Filters/HandlersFilter.cs.meta | 3 + .../HandlerBinding/HandlerBindingAttribute.cs | 20 ++++ .../HandlerBindingAttribute.cs.meta | 3 + .../HandlerBinding/HandlerBindingRegistry.cs | 33 ++++++ .../HandlerBindingRegistry.cs.meta} | 0 .../HandlerBinding/TypeHandlerBinder.cs | 52 +++++++++ .../TypeHandlerBinder.cs.meta | 0 .../Drawers/Handlers/BinderAttribute.cs | 15 --- .../Drawers/Handlers/BinderAttribute.cs.meta | 3 - .../Drawers/Handlers/TypeHandlerBinder.cs | 71 ------------ .../Drawers/HandlersTypeCollection.meta | 3 - .../AttributeHandlersTypeCollection.cs | 42 -------- .../AttributeHandlersTypeCollection.cs.meta | 3 - .../BaseHandlersTypeCollection.cs | 17 --- .../BaseHandlersTypeCollection.cs.meta | 3 - .../HandlersFieldTypeCollection.cs | 42 -------- .../HandlersFieldTypeCollection.cs.meta | 3 - .../HandlersTypeCollection.cs | 45 -------- .../HandlersTypeCollection.cs.meta | 3 - ...asePropertyDrawer.cs => PropertyDrawer.cs} | 82 ++++++++++---- .../Editor/Drawers/PropertyDrawer.cs.meta | 3 + 35 files changed, 402 insertions(+), 308 deletions(-) delete mode 100644 Assets/BetterCommons/Editor/Drawers/BasePropertyDrawer.cs.meta delete mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinderRegistry.cs create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding.meta create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding/Binding.cs create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding/Binding.cs.meta create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding/BindingInfo.cs create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding/BindingInfo.cs.meta create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters.meta create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/AttributeHandlersFilter.cs create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/AttributeHandlersFilter.cs.meta create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/FieldHandlersFilter.cs create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/FieldHandlersFilter.cs.meta create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/HandlersFilter.cs create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/HandlersFilter.cs.meta create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding/HandlerBindingAttribute.cs create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding/HandlerBindingAttribute.cs.meta create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding/HandlerBindingRegistry.cs rename Assets/BetterCommons/Editor/Drawers/{HandlerBinderRegistry.cs.meta => HandlerBinding/HandlerBindingRegistry.cs.meta} (100%) create mode 100644 Assets/BetterCommons/Editor/Drawers/HandlerBinding/TypeHandlerBinder.cs rename Assets/BetterCommons/Editor/Drawers/{Handlers => HandlerBinding}/TypeHandlerBinder.cs.meta (100%) delete mode 100644 Assets/BetterCommons/Editor/Drawers/Handlers/BinderAttribute.cs delete mode 100644 Assets/BetterCommons/Editor/Drawers/Handlers/BinderAttribute.cs.meta delete mode 100644 Assets/BetterCommons/Editor/Drawers/Handlers/TypeHandlerBinder.cs delete mode 100644 Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection.meta delete mode 100644 Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/AttributeHandlersTypeCollection.cs delete mode 100644 Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/AttributeHandlersTypeCollection.cs.meta delete mode 100644 Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/BaseHandlersTypeCollection.cs delete mode 100644 Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/BaseHandlersTypeCollection.cs.meta delete mode 100644 Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersFieldTypeCollection.cs delete mode 100644 Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersFieldTypeCollection.cs.meta delete mode 100644 Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersTypeCollection.cs delete mode 100644 Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersTypeCollection.cs.meta rename Assets/BetterCommons/Editor/Drawers/{BasePropertyDrawer.cs => PropertyDrawer.cs} (63%) create mode 100644 Assets/BetterCommons/Editor/Drawers/PropertyDrawer.cs.meta diff --git a/Assets/BetterCommons/Editor/CustomEditors/MultiEditor.cs b/Assets/BetterCommons/Editor/CustomEditors/MultiEditor.cs index fbd77aa..bf18a88 100644 --- a/Assets/BetterCommons/Editor/CustomEditors/MultiEditor.cs +++ b/Assets/BetterCommons/Editor/CustomEditors/MultiEditor.cs @@ -60,8 +60,7 @@ bool WherePredicate((Type type, MultiEditorAttribute attribute) x) return att.EditorFor == targetType; } - return typeof(ExtendedEditor).GetAllInheritedTypesWithoutUnityObject() - .Select(type => (type, type.GetCustomAttribute())) + return typeof(ExtendedEditor).GetAllInheritedTypesWithoutUnityObject().Select(type => (type, type.GetCustomAttribute())) .Where(WherePredicate).OrderBy(x => x.Item2.Order).ToArray(); } diff --git a/Assets/BetterCommons/Editor/Drawers/BasePropertyDrawer.cs.meta b/Assets/BetterCommons/Editor/Drawers/BasePropertyDrawer.cs.meta deleted file mode 100644 index ecea6f7..0000000 --- a/Assets/BetterCommons/Editor/Drawers/BasePropertyDrawer.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: bbb270479ba84bb8bcfca7d8ae9f13d6 -timeCreated: 1716417021 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinderRegistry.cs b/Assets/BetterCommons/Editor/Drawers/HandlerBinderRegistry.cs deleted file mode 100644 index 827d3d3..0000000 --- a/Assets/BetterCommons/Editor/Drawers/HandlerBinderRegistry.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Better.Commons.EditorAddons.Drawers.Handlers; -using Better.Commons.Runtime.Extensions; - -namespace Better.Commons.EditorAddons.Drawers -{ - public static class HandlerBinderRegistry - { - private static readonly Dictionary _binders; - - static HandlerBinderRegistry() - { - var binderType = typeof(TypeHandlerBinder); - _binders = binderType.GetAllInheritedTypes() - .Select(value => (value.GetCustomAttribute()?.HandlerType, Value: value)) - .Where(tuple => tuple.HandlerType != null && tuple.Value.HasParameterlessConstructor()) - .ToDictionary(key=>key.HandlerType, value => (TypeHandlerBinder)Activator.CreateInstance(value.Value)); - } - - public static TypeHandlerBinder GetMap() where THandler : SerializedPropertyHandler - { - var handlerType = typeof(THandler); - if (_binders.TryGetValue(handlerType, out var binder) && binder is TypeHandlerBinder typeHandlerBinder) - { - return typeHandlerBinder; - } - - return null; - } - } -} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding.meta b/Assets/BetterCommons/Editor/Drawers/HandlerBinding.meta new file mode 100644 index 0000000..3bda8aa --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 998d68dad67544febc1028f14e0c6fe3 +timeCreated: 1731731991 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Binding.cs b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Binding.cs new file mode 100644 index 0000000..2b1bfae --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Binding.cs @@ -0,0 +1,102 @@ +using System; +using System.Linq; +using System.Reflection; + +namespace Better.Commons.EditorAddons.Drawers.Handlers +{ + public class Binding + { + public BindingInfo[] Binds { get; } + public Type HandlerType { get; } + + public Binding(Type handlerType) + { + HandlerType = handlerType; + var handlerBindings = HandlerType.GetCustomAttributes(true).Select(attribute => attribute.BindingInfo); + + Binds = handlerBindings.ToArray(); + } + + public int GetBindingPriority(Type attributeType, Type fieldType) + { + var value = GetFieldPriority(fieldType); + if (value < 0) + { + return value; + } + + var buffer = GetAttributePriority(attributeType); + if (buffer < 0) + { + return buffer; + } + + return value + buffer; + } + + private int GetFieldPriority(Type fieldType) + { + if (!IsFieldTypeSupported(fieldType)) + { + return -1; + } + + if (Binds.Any(bind => bind.FieldType != null && bind.FieldType == fieldType)) + { + return 2; + } + + if (Binds.Any(bind => bind.FieldType != null && bind.FieldType.IsAssignableFrom(fieldType))) + { + return 1; + } + + if (Binds.Any(bind => bind.AnyFieldType)) + { + return 0; + } + + return 0; + } + + private int GetAttributePriority(Type attributeType) + { + if (attributeType == null) + { + return 0; + } + + if (Binds.All(bind => bind.AttributeType == null)) + { + return -1; + } + + if (Binds.Any(bind => bind.AttributeType != null && bind.AttributeType == attributeType)) + { + return 2; + } + + if (Binds.Any(bind => bind.AttributeType != null && bind.AttributeType.IsAssignableFrom(attributeType))) + { + return 1; + } + + return 0; + } + + public bool IsFieldTypeSupported(Type fieldType) + { + var fieldSupported = Binds.Select(bind => bind.FieldType) + .Where(bindFieldType => bindFieldType != null && fieldType != null) + .Any(bindFieldType => bindFieldType.IsAssignableFrom(fieldType)); + + if (fieldSupported) + { + return true; + } + + var anyFieldSupported = Binds.Any(bind => bind.AnyFieldType); + return anyFieldSupported; + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Binding.cs.meta b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Binding.cs.meta new file mode 100644 index 0000000..6b1a619 --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Binding.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 04f4f3e926ce47ed95b3d5f907dc29e4 +timeCreated: 1731722087 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding/BindingInfo.cs b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/BindingInfo.cs new file mode 100644 index 0000000..0410d92 --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/BindingInfo.cs @@ -0,0 +1,18 @@ +using System; + +namespace Better.Commons.EditorAddons.Drawers +{ + public class BindingInfo + { + public BindingInfo(Type fieldType, Type attributeType, bool anyFieldType) + { + AttributeType = attributeType; + FieldType = fieldType; + AnyFieldType = anyFieldType; + } + + public Type AttributeType { get; } + public Type FieldType { get; } + public bool AnyFieldType { get; } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding/BindingInfo.cs.meta b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/BindingInfo.cs.meta new file mode 100644 index 0000000..5ab01a6 --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/BindingInfo.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 770bd72c235b4e58a4b2e2233ec14ab0 +timeCreated: 1731897690 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters.meta b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters.meta new file mode 100644 index 0000000..d0c5c19 --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1f58f02a575a448790d82fe866f2f92a +timeCreated: 1733008502 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/AttributeHandlersFilter.cs b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/AttributeHandlersFilter.cs new file mode 100644 index 0000000..98fa37a --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/AttributeHandlersFilter.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Better.Commons.EditorAddons.Drawers.Handlers +{ + public class AttributeHandlersFilter : FieldHandlersFilter + { + protected readonly Type _attributeType; + + public AttributeHandlersFilter(Type fieldType, Type attributeType) : base(fieldType) + { + _attributeType = attributeType; + } + + protected override int GetBindingPriority(Binding bind) + { + return bind.GetBindingPriority(_attributeType, _fieldType); + } + + protected override bool TryFilter(Binding binding) + { + return binding.Binds.Any(bind => bind.AttributeType.IsAssignableFrom(_attributeType)) + && base.TryFilter(binding); + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/AttributeHandlersFilter.cs.meta b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/AttributeHandlersFilter.cs.meta new file mode 100644 index 0000000..649a82a --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/AttributeHandlersFilter.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ca78f1c91e4142648c050d1f667579f1 +timeCreated: 1733008194 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/FieldHandlersFilter.cs b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/FieldHandlersFilter.cs new file mode 100644 index 0000000..88e7d32 --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/FieldHandlersFilter.cs @@ -0,0 +1,24 @@ +using System; + +namespace Better.Commons.EditorAddons.Drawers.Handlers +{ + public class FieldHandlersFilter : HandlersFilter + { + protected readonly Type _fieldType; + + public FieldHandlersFilter(Type fieldType) + { + _fieldType = fieldType; + } + + protected override int GetBindingPriority(Binding bind) + { + return bind.GetBindingPriority(null, _fieldType); + } + + protected override bool TryFilter(Binding binding) + { + return binding.IsFieldTypeSupported(_fieldType); + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/FieldHandlersFilter.cs.meta b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/FieldHandlersFilter.cs.meta new file mode 100644 index 0000000..210217b --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/FieldHandlersFilter.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a5d7fb23845a4bc2a518dedbf6ac7342 +timeCreated: 1733011865 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/HandlersFilter.cs b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/HandlersFilter.cs new file mode 100644 index 0000000..84633a7 --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/HandlersFilter.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; +using System.Linq; + +namespace Better.Commons.EditorAddons.Drawers.Handlers +{ + public abstract class HandlersFilter + { + public bool TryFilter(HashSet bindings, out Binding filteredBinding) + { + var candidates = new HashSet(); + + foreach (var binding in bindings) + { + if (!TryFilter(binding)) continue; + candidates.Add(binding); + } + + var sortedCandidates = SortCandidates(candidates); + + filteredBinding = sortedCandidates.FirstOrDefault(); + return filteredBinding != null; + } + + protected virtual IEnumerable SortCandidates(IEnumerable candidates) + { + candidates = candidates.OrderByDescending(GetBindingPriority); + return candidates; + } + + protected abstract int GetBindingPriority(Binding bind); + + + protected abstract bool TryFilter(Binding binding); + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/HandlersFilter.cs.meta b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/HandlersFilter.cs.meta new file mode 100644 index 0000000..8b67d7b --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/Filters/HandlersFilter.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3f3c7333197a434b8d5e7994a90beb7e +timeCreated: 1733008194 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding/HandlerBindingAttribute.cs b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/HandlerBindingAttribute.cs new file mode 100644 index 0000000..41e3fb0 --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/HandlerBindingAttribute.cs @@ -0,0 +1,20 @@ +using System; + +namespace Better.Commons.EditorAddons.Drawers +{ + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] + public class HandlerBindingAttribute : Attribute + { + public BindingInfo BindingInfo { get; } + + public HandlerBindingAttribute(Type fieldType, Type attributeType) + { + BindingInfo = new BindingInfo(fieldType, attributeType, false); + } + + public HandlerBindingAttribute(Type attributeType) + { + BindingInfo = new BindingInfo(null, attributeType, true); + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding/HandlerBindingAttribute.cs.meta b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/HandlerBindingAttribute.cs.meta new file mode 100644 index 0000000..5e3004b --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/HandlerBindingAttribute.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 958b6dda07cb4a37bf17e6767066df51 +timeCreated: 1731888427 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding/HandlerBindingRegistry.cs b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/HandlerBindingRegistry.cs new file mode 100644 index 0000000..6f786c3 --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/HandlerBindingRegistry.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Better.Commons.EditorAddons.Drawers.Handlers; +using Better.Commons.Runtime.Extensions; + +namespace Better.Commons.EditorAddons.Drawers +{ + public static class BindingRegistry + { + private static readonly HashSet _bindings; + + static BindingRegistry() + { + _bindings = GetBindings(); + } + + private static HashSet GetBindings() + { + var handlerTypes = typeof(SerializedPropertyHandler).GetAllInheritedTypes().Where(type => !type.IsAbstract); + var boundHandlers = handlerTypes.Where(type => type.IsDefined(typeof(HandlerBindingAttribute))); + var bindings = boundHandlers.Select(type => new Binding(type)); + return bindings.ToHashSet(); + } + + public static TypeHandlerBinder GetBinder() where THandler : SerializedPropertyHandler + { + var bindings = _bindings.Where(binding => typeof(THandler).IsAssignableFrom(binding.HandlerType)).ToHashSet(); + var binder = new TypeHandlerBinder(bindings); + return binder; + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinderRegistry.cs.meta b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/HandlerBindingRegistry.cs.meta similarity index 100% rename from Assets/BetterCommons/Editor/Drawers/HandlerBinderRegistry.cs.meta rename to Assets/BetterCommons/Editor/Drawers/HandlerBinding/HandlerBindingRegistry.cs.meta diff --git a/Assets/BetterCommons/Editor/Drawers/HandlerBinding/TypeHandlerBinder.cs b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/TypeHandlerBinder.cs new file mode 100644 index 0000000..14e236d --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/TypeHandlerBinder.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Better.Commons.Runtime.Utility; + +namespace Better.Commons.EditorAddons.Drawers.Handlers +{ + public abstract class TypeHandlerBinder + { + protected HashSet _bindings; + + public TypeHandlerBinder(HashSet bindings) + { + _bindings = bindings; + } + + public virtual bool IsSupported(Type fieldType) + { + if (fieldType == null) + { + DebugUtility.LogException(nameof(fieldType)); + } + + return _bindings.Any(binding => binding.IsFieldTypeSupported(fieldType)); + } + } + + public class TypeHandlerBinder : TypeHandlerBinder where THandler : SerializedPropertyHandler + { + public TypeHandlerBinder(HashSet bindings) : base(bindings) + { + } + + public bool TryFindByFilter(HandlersFilter filter, out THandler handler) + { + if (filter == null) + { + handler = null; + return false; + } + + if (filter.TryFilter(_bindings, out var filteredBinding) && typeof(THandler).IsAssignableFrom(filteredBinding.HandlerType)) + { + handler = (THandler)Activator.CreateInstance(filteredBinding.HandlerType); + return true; + } + + handler = null; + return false; + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/Handlers/TypeHandlerBinder.cs.meta b/Assets/BetterCommons/Editor/Drawers/HandlerBinding/TypeHandlerBinder.cs.meta similarity index 100% rename from Assets/BetterCommons/Editor/Drawers/Handlers/TypeHandlerBinder.cs.meta rename to Assets/BetterCommons/Editor/Drawers/HandlerBinding/TypeHandlerBinder.cs.meta diff --git a/Assets/BetterCommons/Editor/Drawers/Handlers/BinderAttribute.cs b/Assets/BetterCommons/Editor/Drawers/Handlers/BinderAttribute.cs deleted file mode 100644 index 7992b04..0000000 --- a/Assets/BetterCommons/Editor/Drawers/Handlers/BinderAttribute.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace Better.Commons.EditorAddons.Drawers.Handlers -{ - [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)] - public class BinderAttribute : Attribute - { - public Type HandlerType { get; } - - public BinderAttribute(Type handlerType) - { - HandlerType = handlerType; - } - } -} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/Handlers/BinderAttribute.cs.meta b/Assets/BetterCommons/Editor/Drawers/Handlers/BinderAttribute.cs.meta deleted file mode 100644 index 4271f6d..0000000 --- a/Assets/BetterCommons/Editor/Drawers/Handlers/BinderAttribute.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 5812ba66f7da4e14af84b01d6e06a907 -timeCreated: 1716772606 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/Handlers/TypeHandlerBinder.cs b/Assets/BetterCommons/Editor/Drawers/Handlers/TypeHandlerBinder.cs deleted file mode 100644 index f78b3b3..0000000 --- a/Assets/BetterCommons/Editor/Drawers/Handlers/TypeHandlerBinder.cs +++ /dev/null @@ -1,71 +0,0 @@ -using System; -using System.Collections.Generic; -using Better.Commons.EditorAddons.Drawers.HandlersTypeCollection; -using Better.Commons.Runtime.Utility; - -namespace Better.Commons.EditorAddons.Drawers.Handlers -{ - public abstract class TypeHandlerBinder - { - protected HashSet _availableTypes; - protected BaseHandlersTypeCollection _handlersCollection; - - protected TypeHandlerBinder() - { - Construct(); - } - - private void Construct() - { - _handlersCollection = GenerateCollection(); - _availableTypes = GenerateAvailable(); - } - - public virtual bool IsSupported(Type type) - { - if (type == null) - { - DebugUtility.LogException(nameof(type)); - } - - return _availableTypes.Contains(type); - } - - - protected abstract BaseHandlersTypeCollection GenerateCollection(); - - protected abstract HashSet GenerateAvailable(); - } - - //TODO: Maybe make Locator> - public abstract class TypeHandlerBinder : TypeHandlerBinder where THandler : SerializedPropertyHandler - { - public THandler GetHandler(Type fieldType, Type attributeType) - { - if (fieldType == null) - { - DebugUtility.LogException(nameof(fieldType)); - return null; - } - - if (attributeType == null) - { - DebugUtility.LogException(nameof(attributeType)); - return null; - } - - if (!IsSupported(fieldType)) - { - return null; - } - - if (_handlersCollection.TryGetValue(attributeType, fieldType, out var type)) - { - return (THandler)Activator.CreateInstance(type); - } - - DebugUtility.LogException($"Supported types not found for {fieldType}"); - return null; - } - } -} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection.meta b/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection.meta deleted file mode 100644 index 066e45d..0000000 --- a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 6f4928d2cdeb4ab9b5eb4c365634e6c3 -timeCreated: 1712534303 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/AttributeHandlersTypeCollection.cs b/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/AttributeHandlersTypeCollection.cs deleted file mode 100644 index 8720148..0000000 --- a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/AttributeHandlersTypeCollection.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Better.Commons.EditorAddons.Drawers.HandlersTypeCollection -{ - public class AttributeHandlersTypeCollection : BaseHandlersTypeCollection - { - private readonly Dictionary _dictionary; - - public AttributeHandlersTypeCollection() - { - _dictionary = new Dictionary(); - } - - public AttributeHandlersTypeCollection(IEqualityComparer equalityComparer) - { - _dictionary = new Dictionary(equalityComparer); - } - - public void Add(Type attributeType, Type wrapper) - { - _dictionary.Add(attributeType, wrapper); - } - - public override bool TryGetValue(Type attributeType, Type fieldType, out Type wrapperType) - { - if (_dictionary.TryGetValue(attributeType, out var wrapper)) - { - wrapperType = wrapper; - return true; - } - - wrapperType = null; - return false; - } - - public override IEnumerator GetEnumerator() - { - return ((IEnumerable)_dictionary.Keys).GetEnumerator(); - } - } -} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/AttributeHandlersTypeCollection.cs.meta b/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/AttributeHandlersTypeCollection.cs.meta deleted file mode 100644 index 740d0ed..0000000 --- a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/AttributeHandlersTypeCollection.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: feebd52d5a114f29bd2bc67371cfae27 -timeCreated: 1688641730 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/BaseHandlersTypeCollection.cs b/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/BaseHandlersTypeCollection.cs deleted file mode 100644 index 368e29e..0000000 --- a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/BaseHandlersTypeCollection.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; - -namespace Better.Commons.EditorAddons.Drawers.HandlersTypeCollection -{ - public abstract class BaseHandlersTypeCollection : IEnumerable - { - public abstract bool TryGetValue(Type attributeType, Type fieldType, out Type wrapperType); - public abstract IEnumerator GetEnumerator(); - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - } -} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/BaseHandlersTypeCollection.cs.meta b/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/BaseHandlersTypeCollection.cs.meta deleted file mode 100644 index baec360..0000000 --- a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/BaseHandlersTypeCollection.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: d043460555e141e89e929ed234e31337 -timeCreated: 1688641547 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersFieldTypeCollection.cs b/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersFieldTypeCollection.cs deleted file mode 100644 index 6044167..0000000 --- a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersFieldTypeCollection.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Better.Commons.EditorAddons.Drawers.HandlersTypeCollection -{ - public class HandlersFieldTypeCollection : BaseHandlersTypeCollection - { - protected Dictionary _dictionary; - - public HandlersFieldTypeCollection() - { - _dictionary = new Dictionary(); - } - - public HandlersFieldTypeCollection(IEqualityComparer equalityComparer) - { - _dictionary = new Dictionary(equalityComparer); - } - - public override bool TryGetValue(Type attributeType, Type fieldType, out Type wrapperType) - { - if (_dictionary.TryGetValue(fieldType, out var type)) - { - wrapperType = type; - return true; - } - - wrapperType = null; - return false; - } - - public override IEnumerator GetEnumerator() - { - return ((IEnumerable)_dictionary.Keys).GetEnumerator(); - } - - public void Add(Type fieldType, Type wrapperType) - { - _dictionary.Add(fieldType, wrapperType); - } - } -} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersFieldTypeCollection.cs.meta b/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersFieldTypeCollection.cs.meta deleted file mode 100644 index e885db5..0000000 --- a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersFieldTypeCollection.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 21aacba8d2a34fa1adee04bd6dfde47a -timeCreated: 1709431995 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersTypeCollection.cs b/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersTypeCollection.cs deleted file mode 100644 index faa0b02..0000000 --- a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersTypeCollection.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Better.Commons.EditorAddons.Drawers.HandlersTypeCollection -{ - public class HandlersTypeCollection : BaseHandlersTypeCollection - { - protected Dictionary> _dictionary; - - public HandlersTypeCollection() - { - _dictionary = new Dictionary>(); - } - - public HandlersTypeCollection(IEqualityComparer equalityComparer) - { - _dictionary = new Dictionary>(equalityComparer); - } - - public override bool TryGetValue(Type attributeType, Type fieldType, out Type wrapperType) - { - if (_dictionary.TryGetValue(attributeType, out var dictionary)) - { - if (dictionary.TryGetValue(fieldType, out var type)) - { - wrapperType = type; - return true; - } - } - - wrapperType = null; - return false; - } - - public override IEnumerator GetEnumerator() - { - return ((IEnumerable)_dictionary.Keys).GetEnumerator(); - } - - public void Add(Type attributeType, Dictionary fieldToWrapper) - { - _dictionary.Add(attributeType, fieldToWrapper); - } - } -} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersTypeCollection.cs.meta b/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersTypeCollection.cs.meta deleted file mode 100644 index f0d89a6..0000000 --- a/Assets/BetterCommons/Editor/Drawers/HandlersTypeCollection/HandlersTypeCollection.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: a997aa6e54834524a863fb961cf089a8 -timeCreated: 1663104305 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/BasePropertyDrawer.cs b/Assets/BetterCommons/Editor/Drawers/PropertyDrawer.cs similarity index 63% rename from Assets/BetterCommons/Editor/Drawers/BasePropertyDrawer.cs rename to Assets/BetterCommons/Editor/Drawers/PropertyDrawer.cs index fc74b0b..0d6879c 100644 --- a/Assets/BetterCommons/Editor/Drawers/BasePropertyDrawer.cs +++ b/Assets/BetterCommons/Editor/Drawers/PropertyDrawer.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Reflection; using Better.Commons.EditorAddons.Drawers.Base; using Better.Commons.EditorAddons.Drawers.Container; @@ -12,16 +13,14 @@ namespace Better.Commons.EditorAddons.Drawers { - public abstract class BasePropertyDrawer : PropertyDrawer where THandler : SerializedPropertyHandler where TAttribute : MultiPropertyAttribute + public abstract class PropertyDrawer : PropertyDrawer where THandler : SerializedPropertyHandler { protected FieldInfo FieldInfo { get; private set; } - protected TAttribute Attribute { get; private set; } - protected HandlerCollection Handlers { get; private set; } protected ElementsContainer Container { get; private set; } protected TypeHandlerBinder TypeHandlersBinder { get; private set; } - protected BasePropertyDrawer() + protected PropertyDrawer() { EditorApplication.update += ScheduleSubscribe; } @@ -32,7 +31,7 @@ private void ScheduleSubscribe() Selection.selectionChanged += OnSelectionChanged; } - ~BasePropertyDrawer() + ~PropertyDrawer() { EditorApplication.update += DeconstructOnMainThread; } @@ -51,51 +50,71 @@ protected THandler GetHandler(SerializedProperty property) return null; } - var attributeType = Attribute.GetType(); var fieldType = GetFieldOrElementType(); var cached = new CachedSerializedProperty(property); Handlers.Revalidate(); - + if (Handlers.TryGetValue(cached, out var value)) { return value.Handler; } - var handler = TypeHandlersBinder.GetHandler(fieldType, attributeType); - if (handler == null) + var filter = GetFilter(); + if (TypeHandlersBinder.TryFindByFilter(filter, out var handler)) { - return null; + var collectionValue = new CollectionValue(handler, fieldType); + Handlers.Add(cached, collectionValue); + + return handler; } - var collectionValue = new CollectionValue(handler, fieldType); - Handlers.Add(cached, collectionValue); + DebugUtility.LogException(nameof(handler)); + return null; + } - return handler; + protected virtual HandlersFilter GetFilter() + { + var fieldType = GetFieldOrElementType(); + var filter = new FieldHandlersFilter(fieldType); + return filter; } public sealed override VisualElement CreatePropertyGUI(SerializedProperty property) { + if (Container == null) + { + OnInitialized(); + } + if (Container != null) { ContainerReleased(Container); Container = null; } - + Container = new ElementsContainer(property); FieldInfo = fieldInfo; - Attribute = (TAttribute)attribute; Handlers = new HandlerCollection(); - TypeHandlersBinder = HandlerBinderRegistry.GetMap(); + TypeHandlersBinder = BindingRegistry.GetBinder(); PopulateContainer(Container); Container.Use(); - - var subState = StyleDefinition.CombineSubState(typeof(TAttribute).Name, GetType().Name); + + var subState = GetContainerClassName(); Container.RootElement.AddToClassList(subState); return Container.RootElement; } + protected virtual void OnInitialized() + { + } + + protected virtual string GetContainerClassName() + { + return StyleDefinition.CombineSubState(typeof(THandler).Name, GetType().Name); + } + protected abstract void PopulateContainer(ElementsContainer container); private void DeconstructOnMainThread() @@ -125,9 +144,34 @@ private void Deconstruct() Handlers?.Deconstruct(); ContainerReleased(Container); } - + protected virtual void ContainerReleased(ElementsContainer container) { } } + + public abstract class PropertyDrawer : PropertyDrawer where THandler : SerializedPropertyHandler where TAttribute : MultiPropertyAttribute + { + protected TAttribute Attribute { get; private set; } + + protected override void OnInitialized() + { + base.OnInitialized(); + Attribute = (TAttribute)attribute; + } + + protected override HandlersFilter GetFilter() + { + var fieldType = GetFieldOrElementType(); + var attributeType = Attribute.GetType(); + var filter = new AttributeHandlersFilter(fieldType, attributeType); + return filter; + } + + protected override string GetContainerClassName() + { + var baseName = base.GetContainerClassName(); + return StyleDefinition.CombineSubState(typeof(TAttribute).Name, baseName); + } + } } \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Drawers/PropertyDrawer.cs.meta b/Assets/BetterCommons/Editor/Drawers/PropertyDrawer.cs.meta new file mode 100644 index 0000000..82f0d6b --- /dev/null +++ b/Assets/BetterCommons/Editor/Drawers/PropertyDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fc6e2b4105cc40e5b2cbe350cd93419e +timeCreated: 1733015473 \ No newline at end of file From a2c5984e87e142bea197fe20a820cb2822db2060 Mon Sep 17 00:00:00 2001 From: uurha Date: Sun, 1 Dec 2024 02:13:30 +0100 Subject: [PATCH 2/2] package.json --- Assets/BetterCommons/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/BetterCommons/package.json b/Assets/BetterCommons/package.json index fdaff72..94dcb78 100644 --- a/Assets/BetterCommons/package.json +++ b/Assets/BetterCommons/package.json @@ -1,7 +1,7 @@ { "name": "com.tdw.better.commons", "displayName": "Better Commons", - "version": "0.0.59", + "version": "0.0.60", "unity": "2021.3", "description": " ", "dependencies": {