Added component validators

This commit is contained in:
Anders Ejlersen 2026-05-18 20:37:05 +02:00
parent 35f271e12b
commit d7145ad772
30 changed files with 448 additions and 200 deletions

147
Editor/Objects/Report.cs Normal file
View file

@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using UnityEditor;
namespace Module.ProjectValidator.Editor
{
internal sealed class Report
{
public static Report Active { get; private set; }
public static bool HasActive => Active != null;
public readonly List<Entry> Entries = new();
private readonly Dictionary<GUID, MappingEntry> _assetToSeverityMapping = new();
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
{
AssetGuid = assetGuid,
AssetName = EditorAssetUtility.GetAssetName(assetGuid),
ScenePath = scenePath,
FieldPath = fieldPath,
ScenePathRichText = ProjectValidatorUtility.ApplyRichTextToScenePath(scenePath),
FieldPathRichText = ProjectValidatorUtility.ApplyRichTextToFieldPath(fieldPath),
Type = type,
Severity = severity,
SeverityStr = severity.ToString(),
SeverityResult = message
});
if (_assetToSeverityMapping.TryGetValue(assetGuid, out var mapping))
{
if (mapping.Severity < severity)
_assetToSeverityMapping[assetGuid] = new MappingEntry(severity, false);
}
else
{
_assetToSeverityMapping.Add(assetGuid, new MappingEntry(severity, false));
}
}
public void RebuildAssetMapping()
{
ProjectValidatorUtility.RebuildAssetMapping(_assetToSeverityMapping);
}
public void RebuildInstanceMapping()
{
ProjectValidatorUtility.RebuildSceneInstanceMapping(this, _instanceToSeverityMapping);
}
public bool TryGetSeverityFor(string guid, out MappingEntry mapping)
{
if (GUID.TryParse(guid, out var assetGuid) && _assetToSeverityMapping.TryGetValue(assetGuid, out mapping))
return true;
mapping = new MappingEntry();
return false;
}
public bool TryGetSeverityFor(int instanceId, out MappingEntry mapping)
{
if (_instanceToSeverityMapping.TryGetValue(instanceId, out mapping))
return true;
mapping = new MappingEntry();
return false;
}
public bool TryGetSeverityFor(GUID assetGuid, string scenePath, out MappingEntry mapping)
{
if (!_assetToSeverityMapping.TryGetValue(assetGuid, out mapping))
return false;
mapping = new MappingEntry();
for (var i = 0; i < Entries.Count; i++)
{
if (Entries[i].AssetGuid != assetGuid || Entries[i].ScenePath != scenePath || Entries[i].Severity <= mapping.Severity)
continue;
mapping = new MappingEntry(Entries[i].Severity, false);
if (mapping.Severity == EValidatorSeverity.MaxSeverityLevel)
break;
}
return mapping.Severity != EValidatorSeverity.Valid;
}
public void SetAsActive()
{
Active = this;
}
public static void ClearActive()
{
Active = null;
}
public struct Entry
{
public GUID AssetGuid;
public string AssetName;
public string ScenePath;
public string FieldPath;
public string ScenePathRichText;
public string FieldPathRichText;
public string Type;
public EValidatorSeverity Severity;
public string SeverityStr;
public string SeverityResult;
public bool Filter(string filter)
{
return AssetName.Contains(filter, StringComparison.InvariantCultureIgnoreCase) ||
ScenePath.Contains(filter, StringComparison.InvariantCultureIgnoreCase) ||
FieldPath.Contains(filter, StringComparison.InvariantCultureIgnoreCase) ||
Type.Contains(filter, StringComparison.InvariantCultureIgnoreCase) ||
SeverityResult.Contains(filter, StringComparison.InvariantCultureIgnoreCase) ||
SeverityStr.Contains(filter, StringComparison.InvariantCultureIgnoreCase);
}
}
public struct MappingEntry
{
public readonly EValidatorSeverity Severity;
public readonly bool IsRedirect;
public MappingEntry(EValidatorSeverity severity, bool isRedirect)
{
Severity = severity;
IsRedirect = isRedirect;
}
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5c669e16f53a4a15a091760e48c3ccd1
timeCreated: 1778925243

209
Editor/Objects/TypeTree.cs Normal file
View 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;
}
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1b6dbf381f424075bd842ec7b63c638e
timeCreated: 1778953293

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

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5818c3b082764233853707adf7444023
timeCreated: 1778955285