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 _attributeValidators = new(); private readonly Dictionary> _componentValidators = new(); public readonly List GameObjectValidators = 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 { 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 bool TryGetAttributeValidator(Type type, out object validatorInstance) { return _attributeValidators.TryGetValue(type, out validatorInstance); } public bool TryGetComponentValidator(Type type, out List validatorInstances) { return _componentValidators.TryGetValue(type, out validatorInstances); } } }