- Validator: Added option to enable/disable certain validators - Project Settings: Fixed issue, where changes weren't always saved - Unity: Removed deprecated warnings in Unity 6.4
130 lines
No EOL
4.4 KiB
C#
130 lines
No EOL
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
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 readonly List<IGameObjectValidator> GameObjectValidators = new();
|
|
public readonly Dictionary<Type, List<AssetValidator>> AssetValidators = 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 void AddGameObject(Type type)
|
|
{
|
|
if (type.IsInterface || type.IsAbstract)
|
|
return;
|
|
|
|
try
|
|
{
|
|
var instance = (IGameObjectValidator)FormatterServices.GetUninitializedObject(type);
|
|
GameObjectValidators.Add(instance);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogException(e);
|
|
}
|
|
}
|
|
|
|
public void AddAsset(Type type)
|
|
{
|
|
if (type.IsInterface || type.IsAbstract)
|
|
return;
|
|
|
|
var typeValidator = type.GetInterfaces().FirstOrDefault(typeInterface => typeInterface.IsGenericType && typeInterface.GetGenericTypeDefinition() == typeof(IAssetValidator<>));
|
|
var componentType = typeValidator?.GetGenericArguments()[0];
|
|
|
|
if (componentType == null)
|
|
return;
|
|
|
|
try
|
|
{
|
|
var instance = FormatterServices.GetUninitializedObject(type);
|
|
var validator = new AssetValidator(instance);
|
|
|
|
if (AssetValidators.TryGetValue(componentType, out var list))
|
|
list.Add(validator);
|
|
else
|
|
AssetValidators.Add(componentType, new List<AssetValidator> { validator });
|
|
}
|
|
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);
|
|
}
|
|
|
|
public sealed class AssetValidator
|
|
{
|
|
public readonly object Validator;
|
|
public readonly MethodInfo ValidatorMethod;
|
|
|
|
public AssetValidator(object validator)
|
|
{
|
|
Validator = validator;
|
|
ValidatorMethod = validator.GetType().GetMethod("Validate");
|
|
}
|
|
}
|
|
}
|
|
} |