Added component validators
This commit is contained in:
parent
35f271e12b
commit
d7145ad772
30 changed files with 448 additions and 200 deletions
3
Editor/Objects.meta
Normal file
3
Editor/Objects.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3cf6340ace4c49589cc0467648d03ee7
|
||||
timeCreated: 1778923205
|
||||
|
|
@ -14,6 +14,12 @@ namespace Module.ProjectValidator.Editor
|
|||
private readonly Dictionary<int, MappingEntry> _instanceToSeverityMapping = new();
|
||||
|
||||
public void Add(GUID assetGuid, string scenePath, string fieldPath, Attribute attribute, EValidatorSeverity severity, string message)
|
||||
{
|
||||
var type = ProjectValidatorUtility.GetAttributeShortName(attribute);
|
||||
Add(assetGuid, scenePath, fieldPath, type, severity, message);
|
||||
}
|
||||
|
||||
public void Add(GUID assetGuid, string scenePath, string fieldPath, string type, EValidatorSeverity severity, string message)
|
||||
{
|
||||
Entries.Add(new Entry
|
||||
{
|
||||
|
|
@ -23,7 +29,7 @@ namespace Module.ProjectValidator.Editor
|
|||
FieldPath = fieldPath,
|
||||
ScenePathRichText = ProjectValidatorUtility.ApplyRichTextToScenePath(scenePath),
|
||||
FieldPathRichText = ProjectValidatorUtility.ApplyRichTextToFieldPath(fieldPath),
|
||||
Type = ProjectValidatorUtility.GetAttributeShortName(attribute),
|
||||
Type = type,
|
||||
Severity = severity,
|
||||
SeverityStr = severity.ToString(),
|
||||
SeverityResult = message
|
||||
209
Editor/Objects/TypeTree.cs
Normal file
209
Editor/Objects/TypeTree.cs
Normal file
|
|
@ -0,0 +1,209 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Module.ProjectValidator.Editor
|
||||
{
|
||||
internal sealed class TypeTree
|
||||
{
|
||||
public readonly Dictionary<Type, Entry> Types = new();
|
||||
private readonly HashSet<Type> _scannedTypes = new();
|
||||
|
||||
public Entry Add(Type type, ValidatorList validatorList)
|
||||
{
|
||||
if (Types.TryGetValue(type, out var e))
|
||||
return e;
|
||||
|
||||
if (!_scannedTypes.Add(type))
|
||||
return null;
|
||||
|
||||
var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
var entry = new Entry();
|
||||
|
||||
if (validatorList.TryGetComponentValidator(type, out var componentValidators))
|
||||
entry.AddComponents(componentValidators);
|
||||
|
||||
for (var i = 0; i < fields.Length; i++)
|
||||
{
|
||||
var fi = fields[i];
|
||||
|
||||
if (!IsFieldSerializable(fi))
|
||||
continue;
|
||||
|
||||
var attributes = fi.GetCustomAttributes();
|
||||
|
||||
foreach (var attribute in attributes)
|
||||
{
|
||||
if (validatorList.TryGetAttributeValidator(attribute.GetType(), out var validator))
|
||||
entry.AddField(fi, attribute, validator);
|
||||
}
|
||||
|
||||
var nextType = GetFieldElementType(fi);
|
||||
|
||||
if (nextType == null || !IsTypeSerializable(nextType))
|
||||
continue;
|
||||
|
||||
e = Add(nextType, validatorList);
|
||||
|
||||
if (e != null)
|
||||
entry.AddField(fi, e);
|
||||
}
|
||||
|
||||
if (entry.IsEmpty())
|
||||
return null;
|
||||
|
||||
Types.Add(type, entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
private static Type GetFieldElementType(FieldInfo fi)
|
||||
{
|
||||
var type = fi.FieldType;
|
||||
|
||||
if (type.IsPrimitive || type.IsInterface || type.IsAbstract)
|
||||
{
|
||||
type = null;
|
||||
}
|
||||
else if (typeof(UnityEngine.Object).IsAssignableFrom(type))
|
||||
{
|
||||
type = null;
|
||||
}
|
||||
else if (type.IsArray)
|
||||
{
|
||||
type = type.GetElementType();
|
||||
}
|
||||
else if (typeof(IEnumerable<object>).IsAssignableFrom(type))
|
||||
{
|
||||
var args = type.GenericTypeArguments;
|
||||
type = args.Length == 1 ? type.GenericTypeArguments[0] : null;
|
||||
}
|
||||
|
||||
if (type != null && !IsTypeSerializable(type))
|
||||
type = null;
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
private static bool IsFieldSerializable(FieldInfo fieldInfo)
|
||||
{
|
||||
if (fieldInfo.IsNotSerialized)
|
||||
return false;
|
||||
if (fieldInfo.IsPrivate && fieldInfo.GetCustomAttribute<SerializeField>() == null)
|
||||
return false;
|
||||
|
||||
return IsTypeSerializable(fieldInfo.FieldType);
|
||||
}
|
||||
|
||||
private static bool IsTypeSerializable(Type type)
|
||||
{
|
||||
if (type == null)
|
||||
return false;
|
||||
|
||||
if (type.IsInterface)
|
||||
return false;
|
||||
if (type.IsAbstract)
|
||||
return false;
|
||||
|
||||
if (type.IsPrimitive)
|
||||
return true;
|
||||
if (type.IsEnum)
|
||||
return true;
|
||||
if (typeof(UnityEngine.Object).IsAssignableFrom(type))
|
||||
return true;
|
||||
|
||||
if (type.IsArray)
|
||||
{
|
||||
var eType = type.GetElementType();
|
||||
|
||||
if (eType == null || !IsTypeSerializable(eType))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (typeof(IEnumerable<object>).IsAssignableFrom(type))
|
||||
{
|
||||
var args = type.GetGenericArguments();
|
||||
|
||||
if (args.Length != 1 || !IsTypeSerializable(args[0]))
|
||||
return false;
|
||||
}
|
||||
|
||||
return type.GetCustomAttribute<SerializableAttribute>() != null;
|
||||
}
|
||||
|
||||
public sealed class Entry
|
||||
{
|
||||
public List<ValidatorComponent> Components;
|
||||
public List<ValidatorField> Fields;
|
||||
public List<FieldEntry> Entries;
|
||||
|
||||
public void AddComponents(List<object> components)
|
||||
{
|
||||
Components ??= new List<ValidatorComponent>();
|
||||
|
||||
for (var i = 0; i < components.Count; i++)
|
||||
{
|
||||
Components.Add(new ValidatorComponent(components[i]));
|
||||
}
|
||||
}
|
||||
|
||||
public void AddField(FieldInfo fieldInfo, Attribute attribute, object validator)
|
||||
{
|
||||
Fields ??= new List<ValidatorField>();
|
||||
Fields.Add(new ValidatorField(fieldInfo, attribute, validator));
|
||||
}
|
||||
|
||||
public void AddField(FieldInfo fieldInfo, Entry entry)
|
||||
{
|
||||
Entries ??= new List<FieldEntry>();
|
||||
Entries.Add(new FieldEntry(fieldInfo, entry));
|
||||
}
|
||||
|
||||
public bool IsEmpty()
|
||||
{
|
||||
return Components == null && Fields == null && Entries == null;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ValidatorField
|
||||
{
|
||||
public readonly FieldInfo FieldInfo;
|
||||
public readonly Attribute Attribute;
|
||||
|
||||
public readonly object Validator;
|
||||
public readonly MethodInfo ValidatorMethod;
|
||||
|
||||
public ValidatorField(FieldInfo fieldInfo, Attribute attribute, object validator)
|
||||
{
|
||||
FieldInfo = fieldInfo;
|
||||
Attribute = attribute;
|
||||
Validator = validator;
|
||||
ValidatorMethod = validator.GetType().GetMethod("Validate");
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ValidatorComponent
|
||||
{
|
||||
public readonly object Validator;
|
||||
public readonly MethodInfo ValidatorMethod;
|
||||
|
||||
public ValidatorComponent(object validator)
|
||||
{
|
||||
Validator = validator;
|
||||
ValidatorMethod = validator.GetType().GetMethod("Validate");
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class FieldEntry
|
||||
{
|
||||
public readonly FieldInfo FieldInfo;
|
||||
public readonly Entry Entry;
|
||||
|
||||
public FieldEntry(FieldInfo fieldInfo, Entry entry)
|
||||
{
|
||||
FieldInfo = fieldInfo;
|
||||
Entry = entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
72
Editor/Objects/ValidatorList.cs
Normal file
72
Editor/Objects/ValidatorList.cs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Module.ProjectValidator.Editor
|
||||
{
|
||||
internal sealed class ValidatorList
|
||||
{
|
||||
private readonly Dictionary<Type, object> _attributeValidators = new();
|
||||
private readonly Dictionary<Type, List<object>> _componentValidators = new();
|
||||
|
||||
public void AddAttribute(Type type)
|
||||
{
|
||||
if (type.IsInterface || type.IsAbstract)
|
||||
return;
|
||||
|
||||
var typeValidator = type.GetInterfaces().FirstOrDefault(typeInterface => typeInterface.IsGenericType && typeInterface.GetGenericTypeDefinition() == typeof(IAttributeValidator<>));
|
||||
var attType = typeValidator?.GetGenericArguments()[0];
|
||||
|
||||
if (attType == null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
var instance = FormatterServices.GetUninitializedObject(type);
|
||||
_attributeValidators.Add(attType, instance);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddComponent(Type type)
|
||||
{
|
||||
if (type.IsInterface || type.IsAbstract)
|
||||
return;
|
||||
|
||||
var typeValidator = type.GetInterfaces().FirstOrDefault(typeInterface => typeInterface.IsGenericType && typeInterface.GetGenericTypeDefinition() == typeof(IComponentValidator<>));
|
||||
var componentType = typeValidator?.GetGenericArguments()[0];
|
||||
|
||||
if (componentType == null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
var instance = FormatterServices.GetUninitializedObject(type);
|
||||
|
||||
if (_componentValidators.TryGetValue(componentType, out var list))
|
||||
list.Add(instance);
|
||||
else
|
||||
_componentValidators.Add(componentType, new List<object> { instance });
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGetAttributeValidator(Type type, out object validatorInstance)
|
||||
{
|
||||
return _attributeValidators.TryGetValue(type, out validatorInstance);
|
||||
}
|
||||
|
||||
public bool TryGetComponentValidator(Type type, out List<object> validatorInstances)
|
||||
{
|
||||
return _componentValidators.TryGetValue(type, out validatorInstances);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -39,6 +39,7 @@ namespace Module.ProjectValidator.Editor
|
|||
var container = new VisualElement { style = { flexDirection = FlexDirection.Column } };
|
||||
var propertyField = new PropertyField(serializedObject.FindProperty("assemblies"), "Assemblies");
|
||||
propertyField.RegisterCallback<ChangeEvent<string>>(_ => InternalEditorUtility.SaveToSerializedFileAndForget(new[] { serializedObject.targetObject }, AssetPath, true));
|
||||
propertyField.RegisterValueChangeCallback(_ => InternalEditorUtility.SaveToSerializedFileAndForget(new[] { serializedObject.targetObject }, AssetPath, true));
|
||||
container.Add(propertyField);
|
||||
root.Add(container);
|
||||
root.Bind(serializedObject);
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ namespace Module.ProjectValidator.Editor
|
|||
{
|
||||
public static T LoadFirstAsset<T>(string name) where T : Object
|
||||
{
|
||||
var guids = AssetDatabase.FindAssetGUIDs($"a:assets t:{typeof(T).Name} {name}");
|
||||
var guids = AssetDatabase.FindAssetGUIDs($"t:{typeof(T).Name} {name}");
|
||||
return guids.Length != 0 ? AssetDatabase.LoadAssetByGUID<T>(guids[0]) : null;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -41,11 +41,15 @@ namespace Module.ProjectValidator.Editor
|
|||
internal static string GetAttributeShortName(Attribute attribute)
|
||||
{
|
||||
var str = attribute.GetType().Name;
|
||||
var index = str.IndexOf("Attribute", StringComparison.Ordinal);
|
||||
|
||||
if (index != -1)
|
||||
str = str[..index];
|
||||
|
||||
str = str.Replace("Attribute", string.Empty);
|
||||
str = ObjectNames.NicifyVariableName(str);
|
||||
return str;
|
||||
}
|
||||
|
||||
internal static string GetComponentValidatorShortName(object obj)
|
||||
{
|
||||
var str = obj.GetType().Name;
|
||||
str = str.Replace("ComponentValidator", string.Empty);
|
||||
str = ObjectNames.NicifyVariableName(str);
|
||||
return str;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using UnityEditor;
|
||||
using UnityEditor.SceneManagement;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Pool;
|
||||
using UnityEngine.SceneManagement;
|
||||
using Debug = UnityEngine.Debug;
|
||||
|
||||
namespace Module.ProjectValidator.Editor
|
||||
{
|
||||
|
|
@ -19,6 +21,9 @@ namespace Module.ProjectValidator.Editor
|
|||
{
|
||||
if (!ProjectValidatorUtility.IsValidForRun())
|
||||
return false;
|
||||
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
||||
Initialize();
|
||||
|
||||
|
|
@ -34,6 +39,8 @@ namespace Module.ProjectValidator.Editor
|
|||
if (showWindow)
|
||||
ProjectValidatorUtility.OpenWindow();
|
||||
|
||||
stopwatch.Stop();
|
||||
Debug.Log(stopwatch.Elapsed.TotalMilliseconds + "ms");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -57,8 +64,9 @@ namespace Module.ProjectValidator.Editor
|
|||
_validatorList = new ValidatorList();
|
||||
_typeTree = new TypeTree();
|
||||
|
||||
FetchAllValidators();
|
||||
FetchAllTypesWithValidators<MonoBehaviour>(assemblies);
|
||||
FetchAllAttributeValidators();
|
||||
FetchAllComponentValidators();
|
||||
FetchAllTypesWithValidators<Component>(assemblies);
|
||||
FetchAllTypesWithValidators<ScriptableObject>(assemblies);
|
||||
_initialized = true;
|
||||
}
|
||||
|
|
@ -83,13 +91,23 @@ namespace Module.ProjectValidator.Editor
|
|||
return assemblies.ToArray();
|
||||
}
|
||||
|
||||
private static void FetchAllValidators()
|
||||
private static void FetchAllAttributeValidators()
|
||||
{
|
||||
var types = TypeCache.GetTypesDerivedFrom(typeof(IAttributeValidator<>));
|
||||
|
||||
for (var i = 0; i < types.Count; i++)
|
||||
{
|
||||
_validatorList.Add(types[i]);
|
||||
_validatorList.AddAttribute(types[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private static void FetchAllComponentValidators()
|
||||
{
|
||||
var types = TypeCache.GetTypesDerivedFrom(typeof(IComponentValidator<>));
|
||||
|
||||
for (var i = 0; i < types.Count; i++)
|
||||
{
|
||||
_validatorList.AddComponent(types[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -101,7 +119,7 @@ namespace Module.ProjectValidator.Editor
|
|||
{
|
||||
var type = types[i];
|
||||
|
||||
if (Array.IndexOf(assemblies, type.Assembly) != -1)
|
||||
if (assemblies.Length == 0 || Array.IndexOf(assemblies, type.Assembly) != -1)
|
||||
_typeTree.Add(type, _validatorList);
|
||||
}
|
||||
}
|
||||
|
|
@ -211,6 +229,22 @@ namespace Module.ProjectValidator.Editor
|
|||
{
|
||||
if (obj == null)
|
||||
return;
|
||||
|
||||
if (entry.Components != null)
|
||||
{
|
||||
for (var i = 0; i < entry.Components.Count; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
var component = entry.Components[i];
|
||||
ValidateComponent(component, obj, assetGuid, scenePath, report);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (entry.Fields != null)
|
||||
{
|
||||
|
|
@ -292,5 +326,20 @@ namespace Module.ProjectValidator.Editor
|
|||
if (result.Severity != EValidatorSeverity.Valid)
|
||||
report.Add(assetGuid, scenePath, fieldPath, field.Attribute, result.Severity, result.Message);
|
||||
}
|
||||
|
||||
private static void ValidateComponent(TypeTree.ValidatorComponent component, object value, GUID assetGuid, string scenePath, Report report)
|
||||
{
|
||||
using var _ = ListPool<ValidatorResult>.Get(out var results);
|
||||
component.ValidatorMethod.Invoke(component.Validator, new[] { value, results });
|
||||
var type = ProjectValidatorUtility.GetComponentValidatorShortName(component.Validator);
|
||||
|
||||
for (var i = 0; i < results.Count; i++)
|
||||
{
|
||||
var result = results[i];
|
||||
|
||||
if (result.Severity != EValidatorSeverity.Valid)
|
||||
report.Add(assetGuid, scenePath, string.Empty, type, result.Severity, result.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3cf6340ace4c49589cc0467648d03ee7
|
||||
timeCreated: 1778923205
|
||||
fileFormatVersion: 2
|
||||
guid: 372e41dfe29c0ef468ae3554d6fbd9f8
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
|
|||
8
Editor/Validators/Component.meta
Normal file
8
Editor/Validators/Component.meta
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e2f0308e61e26cd468af9c290586f6fb
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Module.ProjectValidator.Editor
|
||||
{
|
||||
internal sealed class ComponentValidatorMeshCollider : IComponentValidator<MeshCollider>
|
||||
{
|
||||
public void Validate(MeshCollider component, List<ValidatorResult> results)
|
||||
{
|
||||
if (component.sharedMesh == null)
|
||||
results.Add(ValidatorResult.Create(EValidatorSeverity.Error, "Missing mesh"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b42427573c2a4e68a9d3cc76773972c7
|
||||
timeCreated: 1779125565
|
||||
14
Editor/Validators/Component/ComponentValidatorMeshFilter.cs
Normal file
14
Editor/Validators/Component/ComponentValidatorMeshFilter.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Module.ProjectValidator.Editor
|
||||
{
|
||||
internal sealed class ComponentValidatorMeshFilter : IComponentValidator<MeshFilter>
|
||||
{
|
||||
public void Validate(MeshFilter component, List<ValidatorResult> results)
|
||||
{
|
||||
if (component.sharedMesh == null)
|
||||
results.Add(ValidatorResult.Create(EValidatorSeverity.Error, "Missing mesh"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e232459fff2b408aae26a2b1178e9b9f
|
||||
timeCreated: 1779125497
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Module.ProjectValidator.Editor
|
||||
{
|
||||
internal sealed class ComponentValidatorMeshRenderer : IComponentValidator<MeshRenderer>
|
||||
{
|
||||
public void Validate(MeshRenderer component, List<ValidatorResult> results)
|
||||
{
|
||||
var materials = component.sharedMaterials;
|
||||
|
||||
for (var i = 0; i < materials.Length; i++)
|
||||
{
|
||||
if (materials[i] == null)
|
||||
results.Add(ValidatorResult.Create(EValidatorSeverity.Error, $"Missing material in slot #{i}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: faf660447bfedc84f88e1e9809bc6583
|
||||
|
|
@ -1,111 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Module.ProjectValidator.Editor
|
||||
{
|
||||
internal sealed class TypeTree
|
||||
{
|
||||
public readonly Dictionary<Type, Entry> Types = new();
|
||||
|
||||
public Entry Add(Type type, ValidatorList validatorList)
|
||||
{
|
||||
if (Types.TryGetValue(type, out var e))
|
||||
return e;
|
||||
|
||||
var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
var entry = new Entry();
|
||||
|
||||
for (var i = 0; i < fields.Length; i++)
|
||||
{
|
||||
var fi = fields[i];
|
||||
|
||||
if (fi.IsNotSerialized)
|
||||
continue;
|
||||
|
||||
var attributes = fi.GetCustomAttributes();
|
||||
|
||||
foreach (var attribute in attributes)
|
||||
{
|
||||
if (validatorList.TryGetValue(attribute.GetType(), out var validator))
|
||||
entry.Add(fi, attribute, validator);
|
||||
}
|
||||
|
||||
var nextType = fi.FieldType;
|
||||
|
||||
if (nextType.IsPrimitive || nextType.IsInterface || nextType.IsAbstract)
|
||||
nextType = null;
|
||||
else if (nextType.IsArray)
|
||||
nextType = nextType.GetElementType();
|
||||
else if (typeof(IEnumerable<object>).IsAssignableFrom(nextType))
|
||||
nextType = nextType.GenericTypeArguments[0];
|
||||
|
||||
if (nextType == null || !nextType.IsSerializable)
|
||||
continue;
|
||||
|
||||
e = Add(nextType, validatorList);
|
||||
|
||||
if (e != null)
|
||||
entry.Add(fi, e);
|
||||
}
|
||||
|
||||
if (entry.IsEmpty())
|
||||
return null;
|
||||
|
||||
Types.Add(type, entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
public sealed class Entry
|
||||
{
|
||||
public List<ValidatorField> Fields;
|
||||
public List<FieldEntry> Entries;
|
||||
|
||||
public void Add(FieldInfo fieldInfo, Attribute attribute, object validator)
|
||||
{
|
||||
Fields ??= new List<ValidatorField>();
|
||||
Fields.Add(new ValidatorField(fieldInfo, attribute, validator));
|
||||
}
|
||||
|
||||
public void Add(FieldInfo fieldInfo, Entry entry)
|
||||
{
|
||||
Entries ??= new List<FieldEntry>();
|
||||
Entries.Add(new FieldEntry(fieldInfo, entry));
|
||||
}
|
||||
|
||||
public bool IsEmpty()
|
||||
{
|
||||
return Fields == null && Entries == null;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ValidatorField
|
||||
{
|
||||
public readonly FieldInfo FieldInfo;
|
||||
public readonly Attribute Attribute;
|
||||
|
||||
public readonly object Validator;
|
||||
public readonly MethodInfo ValidatorMethod;
|
||||
|
||||
public ValidatorField(FieldInfo fieldInfo, Attribute attribute, object validator)
|
||||
{
|
||||
FieldInfo = fieldInfo;
|
||||
Attribute = attribute;
|
||||
Validator = validator;
|
||||
ValidatorMethod = validator.GetType().GetMethod("Validate");
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class FieldEntry
|
||||
{
|
||||
public readonly FieldInfo FieldInfo;
|
||||
public readonly Entry Entry;
|
||||
|
||||
public FieldEntry(FieldInfo fieldInfo, Entry entry)
|
||||
{
|
||||
FieldInfo = fieldInfo;
|
||||
Entry = entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Module.ProjectValidator.Editor
|
||||
{
|
||||
internal sealed class ValidatorList
|
||||
{
|
||||
private readonly Dictionary<Type, object> _validators = new();
|
||||
|
||||
public void Add(Type type)
|
||||
{
|
||||
if (type.IsInterface || type.IsAbstract)
|
||||
return;
|
||||
|
||||
var typeValidator = type.GetInterfaces().FirstOrDefault(typeInterface => typeInterface.IsGenericType && typeInterface.GetGenericTypeDefinition() == typeof(IAttributeValidator<>));
|
||||
var attType = typeValidator?.GetGenericArguments()[0];
|
||||
|
||||
if (attType == null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
var instance = FormatterServices.GetUninitializedObject(type);
|
||||
_validators.Add(attType, instance);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGetValue(Type type, out object validatorInstance)
|
||||
{
|
||||
return _validators.TryGetValue(type, out validatorInstance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -18,6 +18,7 @@ namespace Module.ProjectValidator.Editor
|
|||
{
|
||||
var root = rootVisualElement;
|
||||
var asset = EditorAssetUtility.LoadFirstAsset<VisualTreeAsset>("UxmlEditorProjectValidatorWindow");
|
||||
root.styleSheets.Add(EditorAssetUtility.LoadFirstAsset<StyleSheet>("StyleSheetEditorProjectValidatorWindow"));
|
||||
root.Add(asset.Instantiate());
|
||||
|
||||
root.Q<ToolbarButton>("button-run").clicked += OnToolbarButtonRunClicked;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
<ui:UXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="True">
|
||||
<Style src="project://database/Assets/ProjectValidator/Editor/Window/Uxml/StyleSheetEditorProjectValidatorWindow.uss?fileID=7433441132597879392&guid=360a611fbc24ba94895f4251f434f40b&type=3#StyleSheetEditorProjectValidatorWindow"/>
|
||||
<uie:Toolbar name="toolbar">
|
||||
<uie:ToolbarButton text="Run" name="button-run" style="margin-left: 2px;"/>
|
||||
<uie:ToolbarButton text="Clear" name="button-clear"/>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue