From d601ee062ad0de91d179222b1caa2b49470e3fc8 Mon Sep 17 00:00:00 2001 From: VladV Date: Wed, 17 Aug 2022 20:16:23 +0300 Subject: [PATCH] Add Type processors --- Editor/Attributes.cs | 13 +++++ Editor/TriTypeDefinition.cs | 57 ++++--------------- Editor/TriTypeProcessor.cs | 14 +++++ Editor/TriTypeProcessor.cs.meta | 3 + Editor/TypeProcessors.meta | 3 + .../TriRegisterButtonsTypeProcessor.cs | 30 ++++++++++ .../TriRegisterButtonsTypeProcessor.cs.meta | 3 + ...gisterShownByTriPropertiesTypeProcessor.cs | 30 ++++++++++ ...rShownByTriPropertiesTypeProcessor.cs.meta | 3 + ...isterUnitySerializedFieldsTypeProcessor.cs | 30 ++++++++++ ...UnitySerializedFieldsTypeProcessor.cs.meta | 3 + .../TriSortPropertiesTypeProcessor.cs | 27 +++++++++ .../TriSortPropertiesTypeProcessor.cs.meta | 3 + Editor/Utilities/TriDrawersUtilities.cs | 20 +++++++ 14 files changed, 193 insertions(+), 46 deletions(-) create mode 100644 Editor/TriTypeProcessor.cs create mode 100644 Editor/TriTypeProcessor.cs.meta create mode 100644 Editor/TypeProcessors.meta create mode 100644 Editor/TypeProcessors/TriRegisterButtonsTypeProcessor.cs create mode 100644 Editor/TypeProcessors/TriRegisterButtonsTypeProcessor.cs.meta create mode 100644 Editor/TypeProcessors/TriRegisterShownByTriPropertiesTypeProcessor.cs create mode 100644 Editor/TypeProcessors/TriRegisterShownByTriPropertiesTypeProcessor.cs.meta create mode 100644 Editor/TypeProcessors/TriRegisterUnitySerializedFieldsTypeProcessor.cs create mode 100644 Editor/TypeProcessors/TriRegisterUnitySerializedFieldsTypeProcessor.cs.meta create mode 100644 Editor/TypeProcessors/TriSortPropertiesTypeProcessor.cs create mode 100644 Editor/TypeProcessors/TriSortPropertiesTypeProcessor.cs.meta diff --git a/Editor/Attributes.cs b/Editor/Attributes.cs index dce818c..2f27f56 100644 --- a/Editor/Attributes.cs +++ b/Editor/Attributes.cs @@ -86,4 +86,17 @@ namespace TriInspector public Type ValidatorType { get; } public bool ApplyOnArrayElement { get; set; } } + + [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] + public class RegisterTriTypeProcessorAttribute : Attribute + { + public RegisterTriTypeProcessorAttribute(Type processorType, int order) + { + ProcessorType = processorType; + Order = order; + } + + public Type ProcessorType { get; } + public int Order { get; } + } } \ No newline at end of file diff --git a/Editor/TriTypeDefinition.cs b/Editor/TriTypeDefinition.cs index 7e9ba3f..00c0cf7 100644 --- a/Editor/TriTypeDefinition.cs +++ b/Editor/TriTypeDefinition.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Reflection; using TriInspector.Utilities; namespace TriInspector @@ -11,54 +9,13 @@ namespace TriInspector private static readonly Dictionary Cache = new Dictionary(); - private TriTypeDefinition(Type type) + private TriTypeDefinition(IReadOnlyList properties) { - var fieldsOffset = 1; - var fields = TriReflectionUtilities - .GetAllInstanceFieldsInDeclarationOrder(type) - .Where(IsSerialized) - .Select((it, ind) => new TriPropertyDefinition(ind + fieldsOffset, it)) - .ToList(); - - var propertiesOffset = fieldsOffset + fields.Count; - var properties = TriReflectionUtilities - .GetAllInstancePropertiesInDeclarationOrder(type) - .Where(IsSerialized) - .Select((it, ind) => new TriPropertyDefinition(ind + propertiesOffset, it)) - .ToList(); - - var methodsOffset = propertiesOffset + properties.Count; - var methods = TriReflectionUtilities - .GetAllInstanceMethodsInDeclarationOrder(type) - .Where(IsSerialized) - .Select((it, ind) => new TriPropertyDefinition(ind + methodsOffset, it)) - .ToList(); - - Properties = Enumerable.Empty() - .Concat(fields) - .Concat(properties) - .Concat(methods) - .OrderBy(it => it.Order) - .ToList(); + Properties = properties; } public IReadOnlyList Properties { get; } - private static bool IsSerialized(FieldInfo fieldInfo) - { - return TriUnitySerializationUtilities.IsSerializableByUnity(fieldInfo); - } - - private static bool IsSerialized(PropertyInfo propertyInfo) - { - return propertyInfo.GetCustomAttribute() != null; - } - - private static bool IsSerialized(MethodInfo methodInfo) - { - return methodInfo.GetCustomAttribute() != null; - } - public static TriTypeDefinition GetCached(Type type) { if (Cache.TryGetValue(type, out var definition)) @@ -66,7 +23,15 @@ namespace TriInspector return definition; } - return Cache[type] = new TriTypeDefinition(type); + var processors = TriDrawersUtilities.AllTypeProcessors; + var properties = new List(); + + foreach (var processor in processors) + { + processor.ProcessType(type, properties); + } + + return Cache[type] = new TriTypeDefinition(properties); } } } \ No newline at end of file diff --git a/Editor/TriTypeProcessor.cs b/Editor/TriTypeProcessor.cs new file mode 100644 index 0000000..9f2e5ab --- /dev/null +++ b/Editor/TriTypeProcessor.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; + +namespace TriInspector +{ + public abstract class TriTypeProcessor + { + internal int Order { get; set; } + + public virtual void ProcessType(Type type, List properties) + { + } + } +} \ No newline at end of file diff --git a/Editor/TriTypeProcessor.cs.meta b/Editor/TriTypeProcessor.cs.meta new file mode 100644 index 0000000..0479f51 --- /dev/null +++ b/Editor/TriTypeProcessor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 0a7c26cf735e465d8373066f830cf5c4 +timeCreated: 1660754122 \ No newline at end of file diff --git a/Editor/TypeProcessors.meta b/Editor/TypeProcessors.meta new file mode 100644 index 0000000..420706d --- /dev/null +++ b/Editor/TypeProcessors.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 52aec2fc04054420970349733f05237b +timeCreated: 1660755366 \ No newline at end of file diff --git a/Editor/TypeProcessors/TriRegisterButtonsTypeProcessor.cs b/Editor/TypeProcessors/TriRegisterButtonsTypeProcessor.cs new file mode 100644 index 0000000..92409bb --- /dev/null +++ b/Editor/TypeProcessors/TriRegisterButtonsTypeProcessor.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using TriInspector; +using TriInspector.TypeProcessors; +using TriInspector.Utilities; + +[assembly: RegisterTriTypeProcessor(typeof(TriRegisterButtonsTypeProcessor), 3)] + +namespace TriInspector.TypeProcessors +{ + public class TriRegisterButtonsTypeProcessor : TriTypeProcessor + { + public override void ProcessType(Type type, List properties) + { + const int methodsOffset = 20001; + + properties.AddRange(TriReflectionUtilities + .GetAllInstanceMethodsInDeclarationOrder(type) + .Where(IsSerialized) + .Select((it, ind) => new TriPropertyDefinition(ind + methodsOffset, it))); + } + + private static bool IsSerialized(MethodInfo methodInfo) + { + return methodInfo.GetCustomAttribute() != null; + } + } +} \ No newline at end of file diff --git a/Editor/TypeProcessors/TriRegisterButtonsTypeProcessor.cs.meta b/Editor/TypeProcessors/TriRegisterButtonsTypeProcessor.cs.meta new file mode 100644 index 0000000..fbf661c --- /dev/null +++ b/Editor/TypeProcessors/TriRegisterButtonsTypeProcessor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: abe3b6771e754b2da1b3d226784ad02e +timeCreated: 1660755911 \ No newline at end of file diff --git a/Editor/TypeProcessors/TriRegisterShownByTriPropertiesTypeProcessor.cs b/Editor/TypeProcessors/TriRegisterShownByTriPropertiesTypeProcessor.cs new file mode 100644 index 0000000..823e3ba --- /dev/null +++ b/Editor/TypeProcessors/TriRegisterShownByTriPropertiesTypeProcessor.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using TriInspector; +using TriInspector.TypeProcessors; +using TriInspector.Utilities; + +[assembly: RegisterTriTypeProcessor(typeof(TriRegisterShownByTriPropertiesTypeProcessor), 1)] + +namespace TriInspector.TypeProcessors +{ + public class TriRegisterShownByTriPropertiesTypeProcessor : TriTypeProcessor + { + public override void ProcessType(Type type, List properties) + { + const int propertiesOffset = 10001; + + properties.AddRange(TriReflectionUtilities + .GetAllInstancePropertiesInDeclarationOrder(type) + .Where(IsSerialized) + .Select((it, ind) => new TriPropertyDefinition(ind + propertiesOffset, it))); + } + + private static bool IsSerialized(PropertyInfo propertyInfo) + { + return propertyInfo.GetCustomAttribute() != null; + } + } +} \ No newline at end of file diff --git a/Editor/TypeProcessors/TriRegisterShownByTriPropertiesTypeProcessor.cs.meta b/Editor/TypeProcessors/TriRegisterShownByTriPropertiesTypeProcessor.cs.meta new file mode 100644 index 0000000..d7a2113 --- /dev/null +++ b/Editor/TypeProcessors/TriRegisterShownByTriPropertiesTypeProcessor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c57e9a6e1c4e46558fb3c43ffb404a57 +timeCreated: 1660755901 \ No newline at end of file diff --git a/Editor/TypeProcessors/TriRegisterUnitySerializedFieldsTypeProcessor.cs b/Editor/TypeProcessors/TriRegisterUnitySerializedFieldsTypeProcessor.cs new file mode 100644 index 0000000..cd391a2 --- /dev/null +++ b/Editor/TypeProcessors/TriRegisterUnitySerializedFieldsTypeProcessor.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using TriInspector; +using TriInspector.TypeProcessors; +using TriInspector.Utilities; + +[assembly: RegisterTriTypeProcessor(typeof(TriRegisterUnitySerializedFieldsTypeProcessor), 0)] + +namespace TriInspector.TypeProcessors +{ + public class TriRegisterUnitySerializedFieldsTypeProcessor : TriTypeProcessor + { + public override void ProcessType(Type type, List properties) + { + const int fieldsOffset = 1; + + properties.AddRange(TriReflectionUtilities + .GetAllInstanceFieldsInDeclarationOrder(type) + .Where(IsSerialized) + .Select((it, ind) => new TriPropertyDefinition(ind + fieldsOffset, it))); + } + + private static bool IsSerialized(FieldInfo fieldInfo) + { + return TriUnitySerializationUtilities.IsSerializableByUnity(fieldInfo); + } + } +} \ No newline at end of file diff --git a/Editor/TypeProcessors/TriRegisterUnitySerializedFieldsTypeProcessor.cs.meta b/Editor/TypeProcessors/TriRegisterUnitySerializedFieldsTypeProcessor.cs.meta new file mode 100644 index 0000000..e6f2a6f --- /dev/null +++ b/Editor/TypeProcessors/TriRegisterUnitySerializedFieldsTypeProcessor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5a19f117ac85440cb8896b8b90a9f17a +timeCreated: 1660755421 \ No newline at end of file diff --git a/Editor/TypeProcessors/TriSortPropertiesTypeProcessor.cs b/Editor/TypeProcessors/TriSortPropertiesTypeProcessor.cs new file mode 100644 index 0000000..15cd998 --- /dev/null +++ b/Editor/TypeProcessors/TriSortPropertiesTypeProcessor.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using TriInspector; +using TriInspector.TypeProcessors; + +[assembly: RegisterTriTypeProcessor(typeof(TriSortPropertiesTypeProcessor), 10000)] + +namespace TriInspector.TypeProcessors +{ + public class TriSortPropertiesTypeProcessor : TriTypeProcessor + { + public override void ProcessType(Type type, List properties) + { + properties.Sort(PropertyOrderComparer.Instance); + } + + private class PropertyOrderComparer : IComparer + { + public static readonly PropertyOrderComparer Instance = new PropertyOrderComparer(); + + public int Compare(TriPropertyDefinition x, TriPropertyDefinition y) + { + return x.Order.CompareTo(y.Order); + } + } + } +} \ No newline at end of file diff --git a/Editor/TypeProcessors/TriSortPropertiesTypeProcessor.cs.meta b/Editor/TypeProcessors/TriSortPropertiesTypeProcessor.cs.meta new file mode 100644 index 0000000..ed66a1d --- /dev/null +++ b/Editor/TypeProcessors/TriSortPropertiesTypeProcessor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 900dee18b5e245198d1891d10d0a954b +timeCreated: 1660756062 \ No newline at end of file diff --git a/Editor/Utilities/TriDrawersUtilities.cs b/Editor/Utilities/TriDrawersUtilities.cs index f6ee441..5b50b26 100644 --- a/Editor/Utilities/TriDrawersUtilities.cs +++ b/Editor/Utilities/TriDrawersUtilities.cs @@ -25,6 +25,8 @@ namespace TriInspector.Utilities private static IReadOnlyList _allHideProcessorTypesBackingField; private static IReadOnlyList _allDisableProcessorTypesBackingField; + private static IReadOnlyList _allTypeProcessorBackingField; + private static IDictionary AllGroupDrawersCache { get @@ -46,6 +48,24 @@ namespace TriInspector.Utilities } } + public static IReadOnlyList AllTypeProcessors + { + get + { + if (_allTypeProcessorBackingField == null) + { + _allTypeProcessorBackingField = ( + from asm in TriReflectionUtilities.Assemblies + from attr in asm.GetCustomAttributes() + orderby attr.Order + select (TriTypeProcessor) Activator.CreateInstance(attr.ProcessorType) + ).ToList(); + } + + return _allTypeProcessorBackingField; + } + } + public static IReadOnlyList AllValueDrawerTypes { get