mirror of
https://github.com/codewriter-packages/Tri-Inspector.git
synced 2025-01-22 00:08:51 -05:00
Add Type processors
This commit is contained in:
parent
a1fd95dc2e
commit
d601ee062a
@ -86,4 +86,17 @@ namespace TriInspector
|
|||||||
public Type ValidatorType { get; }
|
public Type ValidatorType { get; }
|
||||||
public bool ApplyOnArrayElement { get; set; }
|
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; }
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,7 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using TriInspector.Utilities;
|
using TriInspector.Utilities;
|
||||||
|
|
||||||
namespace TriInspector
|
namespace TriInspector
|
||||||
@ -11,54 +9,13 @@ namespace TriInspector
|
|||||||
private static readonly Dictionary<Type, TriTypeDefinition> Cache =
|
private static readonly Dictionary<Type, TriTypeDefinition> Cache =
|
||||||
new Dictionary<Type, TriTypeDefinition>();
|
new Dictionary<Type, TriTypeDefinition>();
|
||||||
|
|
||||||
private TriTypeDefinition(Type type)
|
private TriTypeDefinition(IReadOnlyList<TriPropertyDefinition> properties)
|
||||||
{
|
{
|
||||||
var fieldsOffset = 1;
|
Properties = properties;
|
||||||
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<TriPropertyDefinition>()
|
|
||||||
.Concat(fields)
|
|
||||||
.Concat(properties)
|
|
||||||
.Concat(methods)
|
|
||||||
.OrderBy(it => it.Order)
|
|
||||||
.ToList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IReadOnlyList<TriPropertyDefinition> Properties { get; }
|
public IReadOnlyList<TriPropertyDefinition> Properties { get; }
|
||||||
|
|
||||||
private static bool IsSerialized(FieldInfo fieldInfo)
|
|
||||||
{
|
|
||||||
return TriUnitySerializationUtilities.IsSerializableByUnity(fieldInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool IsSerialized(PropertyInfo propertyInfo)
|
|
||||||
{
|
|
||||||
return propertyInfo.GetCustomAttribute<ShowInInspector>() != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool IsSerialized(MethodInfo methodInfo)
|
|
||||||
{
|
|
||||||
return methodInfo.GetCustomAttribute<ButtonAttribute>() != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static TriTypeDefinition GetCached(Type type)
|
public static TriTypeDefinition GetCached(Type type)
|
||||||
{
|
{
|
||||||
if (Cache.TryGetValue(type, out var definition))
|
if (Cache.TryGetValue(type, out var definition))
|
||||||
@ -66,7 +23,15 @@ namespace TriInspector
|
|||||||
return definition;
|
return definition;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Cache[type] = new TriTypeDefinition(type);
|
var processors = TriDrawersUtilities.AllTypeProcessors;
|
||||||
|
var properties = new List<TriPropertyDefinition>();
|
||||||
|
|
||||||
|
foreach (var processor in processors)
|
||||||
|
{
|
||||||
|
processor.ProcessType(type, properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Cache[type] = new TriTypeDefinition(properties);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
14
Editor/TriTypeProcessor.cs
Normal file
14
Editor/TriTypeProcessor.cs
Normal file
@ -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<TriPropertyDefinition> properties)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
Editor/TriTypeProcessor.cs.meta
Normal file
3
Editor/TriTypeProcessor.cs.meta
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 0a7c26cf735e465d8373066f830cf5c4
|
||||||
|
timeCreated: 1660754122
|
3
Editor/TypeProcessors.meta
Normal file
3
Editor/TypeProcessors.meta
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 52aec2fc04054420970349733f05237b
|
||||||
|
timeCreated: 1660755366
|
30
Editor/TypeProcessors/TriRegisterButtonsTypeProcessor.cs
Normal file
30
Editor/TypeProcessors/TriRegisterButtonsTypeProcessor.cs
Normal file
@ -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<TriPropertyDefinition> 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<ButtonAttribute>() != null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: abe3b6771e754b2da1b3d226784ad02e
|
||||||
|
timeCreated: 1660755911
|
@ -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<TriPropertyDefinition> 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<ShowInInspector>() != null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: c57e9a6e1c4e46558fb3c43ffb404a57
|
||||||
|
timeCreated: 1660755901
|
@ -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<TriPropertyDefinition> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 5a19f117ac85440cb8896b8b90a9f17a
|
||||||
|
timeCreated: 1660755421
|
27
Editor/TypeProcessors/TriSortPropertiesTypeProcessor.cs
Normal file
27
Editor/TypeProcessors/TriSortPropertiesTypeProcessor.cs
Normal file
@ -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<TriPropertyDefinition> properties)
|
||||||
|
{
|
||||||
|
properties.Sort(PropertyOrderComparer.Instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class PropertyOrderComparer : IComparer<TriPropertyDefinition>
|
||||||
|
{
|
||||||
|
public static readonly PropertyOrderComparer Instance = new PropertyOrderComparer();
|
||||||
|
|
||||||
|
public int Compare(TriPropertyDefinition x, TriPropertyDefinition y)
|
||||||
|
{
|
||||||
|
return x.Order.CompareTo(y.Order);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 900dee18b5e245198d1891d10d0a954b
|
||||||
|
timeCreated: 1660756062
|
@ -25,6 +25,8 @@ namespace TriInspector.Utilities
|
|||||||
private static IReadOnlyList<RegisterTriPropertyHideProcessor> _allHideProcessorTypesBackingField;
|
private static IReadOnlyList<RegisterTriPropertyHideProcessor> _allHideProcessorTypesBackingField;
|
||||||
private static IReadOnlyList<RegisterTriPropertyDisableProcessor> _allDisableProcessorTypesBackingField;
|
private static IReadOnlyList<RegisterTriPropertyDisableProcessor> _allDisableProcessorTypesBackingField;
|
||||||
|
|
||||||
|
private static IReadOnlyList<TriTypeProcessor> _allTypeProcessorBackingField;
|
||||||
|
|
||||||
private static IDictionary<Type, TriGroupDrawer> AllGroupDrawersCache
|
private static IDictionary<Type, TriGroupDrawer> AllGroupDrawersCache
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@ -46,6 +48,24 @@ namespace TriInspector.Utilities
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IReadOnlyList<TriTypeProcessor> AllTypeProcessors
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_allTypeProcessorBackingField == null)
|
||||||
|
{
|
||||||
|
_allTypeProcessorBackingField = (
|
||||||
|
from asm in TriReflectionUtilities.Assemblies
|
||||||
|
from attr in asm.GetCustomAttributes<RegisterTriTypeProcessorAttribute>()
|
||||||
|
orderby attr.Order
|
||||||
|
select (TriTypeProcessor) Activator.CreateInstance(attr.ProcessorType)
|
||||||
|
).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
return _allTypeProcessorBackingField;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static IReadOnlyList<RegisterTriValueDrawerAttribute> AllValueDrawerTypes
|
public static IReadOnlyList<RegisterTriValueDrawerAttribute> AllValueDrawerTypes
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
Loading…
Reference in New Issue
Block a user