Added initial version 0.1.0

This commit is contained in:
Anders Ejlersen 2026-05-17 22:12:04 +02:00
parent 6aa4cb8596
commit 416759c213
64 changed files with 2181 additions and 0 deletions

141
Editor/Validators/Report.cs Normal file
View file

@ -0,0 +1,141 @@
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)
{
Entries.Add(new Entry
{
AssetGuid = assetGuid,
AssetName = EditorAssetUtility.GetAssetName(assetGuid),
ScenePath = scenePath,
FieldPath = fieldPath,
ScenePathRichText = ProjectValidatorUtility.ApplyRichTextToScenePath(scenePath),
FieldPathRichText = ProjectValidatorUtility.ApplyRichTextToFieldPath(fieldPath),
Type = ProjectValidatorUtility.GetAttributeShortName(attribute),
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

View file

@ -0,0 +1,111 @@
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;
}
}
}
}

View file

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

View file

@ -0,0 +1,40 @@
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);
}
}
}

View file

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

View file

@ -0,0 +1,296 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.Pool;
using UnityEngine.SceneManagement;
namespace Module.ProjectValidator.Editor
{
internal static class ValidatorRunner
{
private static bool _initialized;
private static ValidatorList _validatorList;
private static TypeTree _typeTree;
public static bool Run(bool showWindow = true)
{
if (!ProjectValidatorUtility.IsValidForRun())
return false;
Initialize();
var report = new Report();
ValidateAllScenes(report);
ValidateAllAssets(report);
report.RebuildAssetMapping();
report.RebuildInstanceMapping();
report.SetAsActive();
ProjectValidatorUtility.RefreshUnityWindows();
if (showWindow)
ProjectValidatorUtility.OpenWindow();
return true;
}
public static void Clear()
{
if (!Report.HasActive)
return;
Report.ClearActive();
ProjectValidatorUtility.ClearWindow();
}
private static void Initialize()
{
if (_initialized)
return;
var settings = ProjectValidatorSettings.GetOrCreate();
var assemblies = GetAssembliesFrom(settings);
_validatorList = new ValidatorList();
_typeTree = new TypeTree();
FetchAllValidators();
FetchAllTypesWithValidators<MonoBehaviour>(assemblies);
FetchAllTypesWithValidators<ScriptableObject>(assemblies);
_initialized = true;
}
private static Assembly[] GetAssembliesFrom(ProjectValidatorSettings settings)
{
var assemblies = new List<Assembly>(settings.assemblies.Count);
for (var i = 0; i < settings.assemblies.Count; i++)
{
try
{
var assembly = Assembly.Load(settings.assemblies[i]);
assemblies.Add(assembly);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
return assemblies.ToArray();
}
private static void FetchAllValidators()
{
var types = TypeCache.GetTypesDerivedFrom(typeof(IAttributeValidator<>));
for (var i = 0; i < types.Count; i++)
{
_validatorList.Add(types[i]);
}
}
private static void FetchAllTypesWithValidators<T>(Assembly[] assemblies)
{
var types = TypeCache.GetTypesDerivedFrom<T>();
for (var i = 0; i < types.Count; i++)
{
var type = types[i];
if (Array.IndexOf(assemblies, type.Assembly) != -1)
_typeTree.Add(type, _validatorList);
}
}
private static void ValidateAllScenes(Report report)
{
var assets = EditorAssetUtility.LoadAllAssets<SceneAsset>();
var rootObjects = new List<GameObject>();
for (var i = 0; i < assets.Length; i++)
{
try
{
var assetPath = AssetDatabase.GetAssetPath(assets[i]);
var scene = SceneManager.GetSceneByPath(assetPath);
var isLoaded = scene.isLoaded;
if (!isLoaded)
scene = EditorSceneManager.OpenScene(assetPath, OpenSceneMode.Additive);
scene.GetRootGameObjects(rootObjects);
for (var j = 0; j < rootObjects.Count; j++)
{
ValidateGameObject(rootObjects[j], string.Empty, report);
}
if (!isLoaded)
EditorSceneManager.CloseScene(scene, true);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
}
private static void ValidateAllAssets(Report report)
{
ValidateAssetsBytype<ScriptableObject>(report);
}
private static void ValidateAssetsBytype<T>(Report report) where T : UnityEngine.Object
{
var assets = EditorAssetUtility.LoadAllAssets<T>();
for (var i = 0; i < assets.Length; i++)
{
try
{
ValidateUnityObject(assets[i], report);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
}
private static void ValidateUnityObject(UnityEngine.Object obj, Report report)
{
var assetGuid = EditorAssetUtility.ObjectToAssetGuid(obj);
Validate(assetGuid, string.Empty, obj, report);
}
private static void ValidateGameObject(GameObject gameObject, string scenePath, Report report)
{
ProjectValidatorUtility.AppendToScenePath(gameObject, ref scenePath);
ValidateComponents(gameObject, scenePath, report);
ValidateChildren(gameObject, scenePath, report);
}
private static void ValidateComponents(GameObject gameObject, string scenePath, Report report)
{
using var _ = ListPool<Component>.Get(out var components);
var assetGuid = EditorAssetUtility.GetAssetGuid(gameObject);
gameObject.GetComponents(components);
for (var i = 0; i < components.Count; i++)
{
Validate(assetGuid, scenePath, components[i], report);
}
}
private static void ValidateChildren(GameObject gameObject, string scenePath, Report report)
{
var transform = gameObject.transform;
for (var i = 0; i < transform.childCount; i++)
{
ValidateGameObject(transform.GetChild(i).gameObject, scenePath, report);
}
}
private static void Validate(GUID assetGuid, string scenePath, object obj, Report report)
{
var type = obj.GetType();
if (!_typeTree.Types.TryGetValue(type, out var entry))
return;
var fieldPath = obj.GetType().Name;
Validate(assetGuid, scenePath, fieldPath, obj, entry, report);
}
private static void Validate(GUID assetGuid, string scenePath, string parentFieldPath, object obj, TypeTree.Entry entry, Report report)
{
if (obj == null)
return;
if (entry.Fields != null)
{
for (var i = 0; i < entry.Fields.Count; i++)
{
try
{
var field = entry.Fields[i];
var value = field.FieldInfo.GetValue(obj);
var fieldPath = parentFieldPath;
ProjectValidatorUtility.AppendToFieldPath(field.FieldInfo, ref fieldPath);
if (value is IEnumerable<object> ie)
{
var idx = 0;
foreach (var eObj in ie)
{
var fieldPathArrElement = fieldPath;
ProjectValidatorUtility.AppendToFieldPath(idx, ref fieldPathArrElement);
ValidateField(field, eObj, assetGuid, scenePath, fieldPathArrElement, report);
idx++;
}
}
else
{
ValidateField(field, value, assetGuid, scenePath, fieldPath, report);
}
}
catch (Exception e)
{
Debug.LogException(e);
}
}
}
if (entry.Entries != null)
{
for (var i = 0; i < entry.Entries.Count; i++)
{
try
{
var e = entry.Entries[i];
var value = e.FieldInfo.GetValue(obj);
var fieldPath = parentFieldPath;
ProjectValidatorUtility.AppendToFieldPath(e.FieldInfo, ref fieldPath);
if (value is IEnumerable<object> ie)
{
var idx = 0;
foreach (var eObj in ie)
{
var fieldPathArrElement = fieldPath;
ProjectValidatorUtility.AppendToFieldPath(idx, ref fieldPathArrElement);
Validate(assetGuid, scenePath, fieldPathArrElement, eObj, e.Entry, report);
idx++;
}
}
else
{
Validate(assetGuid, scenePath, fieldPath, value, e.Entry, report);
}
}
catch (Exception e)
{
Debug.LogException(e);
}
}
}
}
private static void ValidateField(TypeTree.ValidatorField field, object value, GUID assetGuid, string scenePath, string fieldPath, Report report)
{
var result = (ValidatorResult)field.ValidatorMethod.Invoke(field.Validator, new[] { field.Attribute, value });
if (result.Severity != EValidatorSeverity.Valid)
report.Add(assetGuid, scenePath, fieldPath, field.Attribute, result.Severity, result.Message);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ce5eb9c29beb4d1fa857cf2079eb1852
timeCreated: 1778925235