Add Type processors

This commit is contained in:
VladV 2022-08-17 20:16:23 +03:00
parent a1fd95dc2e
commit d601ee062a
14 changed files with 193 additions and 46 deletions

View File

@ -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; }
}
}

View File

@ -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<Type, TriTypeDefinition> Cache =
new Dictionary<Type, TriTypeDefinition>();
private TriTypeDefinition(Type type)
private TriTypeDefinition(IReadOnlyList<TriPropertyDefinition> 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<TriPropertyDefinition>()
.Concat(fields)
.Concat(properties)
.Concat(methods)
.OrderBy(it => it.Order)
.ToList();
Properties = properties;
}
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)
{
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<TriPropertyDefinition>();
foreach (var processor in processors)
{
processor.ProcessType(type, properties);
}
return Cache[type] = new TriTypeDefinition(properties);
}
}
}

View 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)
{
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0a7c26cf735e465d8373066f830cf5c4
timeCreated: 1660754122

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 52aec2fc04054420970349733f05237b
timeCreated: 1660755366

View 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;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: abe3b6771e754b2da1b3d226784ad02e
timeCreated: 1660755911

View 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(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;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c57e9a6e1c4e46558fb3c43ffb404a57
timeCreated: 1660755901

View 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(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);
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5a19f117ac85440cb8896b8b90a9f17a
timeCreated: 1660755421

View 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);
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 900dee18b5e245198d1891d10d0a954b
timeCreated: 1660756062

View File

@ -25,6 +25,8 @@ namespace TriInspector.Utilities
private static IReadOnlyList<RegisterTriPropertyHideProcessor> _allHideProcessorTypesBackingField;
private static IReadOnlyList<RegisterTriPropertyDisableProcessor> _allDisableProcessorTypesBackingField;
private static IReadOnlyList<TriTypeProcessor> _allTypeProcessorBackingField;
private static IDictionary<Type, TriGroupDrawer> AllGroupDrawersCache
{
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
{
get