Added component validators
This commit is contained in:
parent
35f271e12b
commit
d7145ad772
30 changed files with 448 additions and 200 deletions
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue