Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #25 from techno-dwarf-works/feature/clean-up
Browse files Browse the repository at this point in the history
Clean up
  • Loading branch information
uurha committed Mar 8, 2024
1 parent 37b9cb8 commit 58e2625
Show file tree
Hide file tree
Showing 69 changed files with 509 additions and 429 deletions.
15 changes: 10 additions & 5 deletions Editor/BetterValidation.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
{
"name": "Better.Validation.Editor",
"rootNamespace": "Better.Validation",
"name": "BetterValidation.Editor",
"rootNamespace": "Better.Validation.EditorAddons",
"references": [
"GUID:441b78e90a9cf724ab41c6eed8c0b93d",
"GUID:19891d5296046644cbc12fcf3702cca3",
"GUID:01df13aca8d01e24a911bcc3e8277031",
"GUID:443314a5a4e67c14a88ae223776b6554",
"GUID:1ac867a6259e45a1856740fe8f7623aa",
"GUID:28da8d3b12e3efa47928e0c9070f853d",
"GUID:a59e3daedde9ca94bba45364d4ead25f"
"GUID:19891d5296046644cbc12fcf3702cca3",
"GUID:a59e3daedde9ca94bba45364d4ead25f",
"GUID:ecdd0819e3429eb44b13a432679a1135",
"GUID:1862b35041b066d42ab4d3caf773657b",
"GUID:af7fffdf1d83bc842b0f6e3a01efda16",
"GUID:441b78e90a9cf724ab41c6eed8c0b93d"
],
"includePlatforms": [
"Editor"
Expand Down
7 changes: 3 additions & 4 deletions Editor/EditorAddons/ContextResolver/AssetResolver.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Better.Validation.EditorAddons.Utilities;
using Better.Singletons.Runtime;
using Better.Validation.EditorAddons.Utility;
using UnityEditor;
using UnityEngine;

namespace Better.Validation.EditorAddons.ContextResolver
{
public class AssetResolver : IContextResolver
public class AssetPathResolver : PocoSingleton<AssetPathResolver>, IPathResolver
{
public static IContextResolver Instance { get; } = new AssetResolver();

public string Resolve(Object obj)
{
if (obj is GameObject gameObject)
Expand Down
2 changes: 1 addition & 1 deletion Editor/EditorAddons/ContextResolver/IContextResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Better.Validation.EditorAddons.ContextResolver
{
public interface IContextResolver
public interface IPathResolver
{
public string Resolve(Object obj);
}
Expand Down
7 changes: 3 additions & 4 deletions Editor/EditorAddons/ContextResolver/SceneResolver.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Better.Validation.EditorAddons.Utilities;
using Better.Singletons.Runtime;
using Better.Validation.EditorAddons.Utility;
using UnityEngine;

namespace Better.Validation.EditorAddons.ContextResolver
{
public class SceneResolver : IContextResolver
public class SceneResolver : PocoSingleton<SceneResolver>, IPathResolver
{
public static IContextResolver Instance { get; } = new SceneResolver();

public string Resolve(Object obj)
{
return obj.FullPath();
Expand Down
16 changes: 8 additions & 8 deletions Editor/EditorAddons/Drawers/ValidationDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Reflection;
using Better.EditorTools.Attributes;
using Better.EditorTools.Drawers.Base;
using Better.EditorTools.Helpers;
using Better.EditorTools.Helpers.Caching;
using Better.Tools.Runtime.Attributes;
using Better.Validation.EditorAddons.Utilities;
using Better.EditorTools.EditorAddons.Attributes;
using Better.EditorTools.EditorAddons.Drawers.Base;
using Better.EditorTools.EditorAddons.Helpers;
using Better.EditorTools.EditorAddons.Helpers.Caching;
using Better.EditorTools.Runtime.Attributes;
using Better.Validation.EditorAddons.Utility;
using Better.Validation.EditorAddons.Wrappers;
using Better.Validation.Runtime.Attributes;
using UnityEditor;
Expand All @@ -15,15 +15,15 @@ namespace Better.Validation.EditorAddons.Drawers
[MultiCustomPropertyDrawer(typeof(ValidationAttribute))]
public class ValidationDrawer : MultiFieldDrawer<PropertyValidationWrapper>
{
private Cache<MutableTuple<string, ValidationType>> _validationResult = new Cache<MutableTuple<string, ValidationType>>();
private CacheValue<MutableTuple<string, ValidationType>> _validationResult = new CacheValue<MutableTuple<string, ValidationType>>();

public ValidationDrawer(FieldInfo fieldInfo, MultiPropertyAttribute attribute) : base(fieldInfo, attribute)
{
}

protected override bool PreDraw(ref Rect position, SerializedProperty property, GUIContent label)
{
var cache = ValidateCachedProperties(property, ValidationUtility.Instance);
var cache = ValidateCachedProperties(property, ValidationAttributeUtility.Instance);
var validationWrapper = cache.Value;
var wrapper = validationWrapper.Wrapper;
if (!cache.IsValid)
Expand Down
6 changes: 3 additions & 3 deletions Editor/EditorAddons/Iteration/IterationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ public class IterationData
{
public Object Target { get; private set; }

public IContextResolver ContextResolver { get; private set; }
public IPathResolver PathResolver { get; private set; }

public SerializedProperty Property { get; private set; }
public SerializedObject Context { get; private set; }


public void SetResolver(IContextResolver context)
public void SetResolver(IPathResolver path)
{
ContextResolver = context;
PathResolver = path;
}

public void SetTarget(Object target)
Expand Down
28 changes: 16 additions & 12 deletions Editor/EditorAddons/Iteration/Iterator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using Better.Extensions.Runtime;
using Better.Validation.EditorAddons.ContextResolver;
using Better.Validation.EditorAddons.Utilities;
using Better.Validation.EditorAddons.Utility;
using Better.Validation.EditorAddons.Wrappers;
using UnityEditor;
using UnityEngine;
Expand All @@ -9,21 +10,26 @@ namespace Better.Validation.EditorAddons.Iteration
{
public static class Iterator
{
private static IContextResolver _context;
private static IPathResolver _path;
private static readonly IterationData CacheData = new IterationData();

public delegate IEnumerable<ValidationCommandData> OnPropertyIteration(IterationData commandData);

public static void SetContext(IContextResolver context)
public static void SetContext(IPathResolver path)
{
_context = context;
CacheData.SetResolver(context);
_path = path;
CacheData.SetResolver(path);
}

public static List<ValidationCommandData> ObjectIteration(Object go, OnPropertyIteration onPropertyIteration)
public static List<ValidationCommandData> ObjectIteration(Object reference, OnPropertyIteration onPropertyIteration)
{
var gameObject = go as GameObject;
var components = gameObject ? gameObject.GetComponents<Component>() : new[] { go };
if (reference.IsNullOrDestroyed())
{
return new List<ValidationCommandData>();
}

var gameObject = reference as GameObject;
var components = gameObject ? gameObject.GetComponents<Component>() : new[] { reference };

var commandData = new List<ValidationCommandData>();
EditorUtility.DisplayProgressBar("Validating components...", "", 0);
Expand All @@ -32,9 +38,9 @@ public static List<ValidationCommandData> ObjectIteration(Object go, OnPropertyI
var obj = components[index];
if (!obj)
{
CacheData.SetTarget(go);
CacheData.SetTarget(reference);
var missingReference = new ValidationCommandData(CacheData, new MissingComponentWrapper(gameObject));
missingReference.SetResultCompiler((data, result) => $"Missing Component on GameObject: {_context.Resolve(data.Target)}");
missingReference.SetResultCompiler((data, result) => $"Missing Component on GameObject: {_path.Resolve(data.Target)}");
missingReference.Revalidate();
commandData.Add(missingReference);
continue;
Expand Down Expand Up @@ -86,7 +92,5 @@ public static List<ValidationCommandData> ObjectsIteration(IReadOnlyList<Object>
EditorUtility.ClearProgressBar();
return list;
}


}
}
35 changes: 24 additions & 11 deletions Editor/EditorAddons/Iteration/IteratorFilter.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using System.Collections.Generic;
using Better.EditorTools;
using Better.EditorTools.Drawers.Base;
using Better.EditorTools.Helpers.Caching;
using System.Text;
using Better.EditorTools.EditorAddons.Drawers.Base;
using Better.EditorTools.EditorAddons.Helpers.Caching;
using Better.Extensions.EditorAddons;
using Better.Extensions.Runtime;
using Better.Validation.EditorAddons.Utilities;
using Better.Validation.EditorAddons.Utility;
using Better.Validation.EditorAddons.Wrappers;
using Better.Validation.Runtime.Attributes;

namespace Better.Validation.EditorAddons.Iteration
{
public static class IteratorFilter
{
private class LocalCache : Cache<WrapperCollectionValue<PropertyValidationWrapper>>
private class LocalCache : CacheValue<WrapperCollectionValue<PropertyValidationWrapper>>
{
}

Expand All @@ -26,11 +27,16 @@ public static IEnumerable<ValidationCommandData> PropertyIterationWithAttributes
var list = data.Property.GetAttributes<ValidationAttribute>();
if (fieldInfo == null || list == null) return null;

var fieldType = fieldInfo.FieldInfo.GetFieldOrElementType();
var fieldType = fieldInfo.FieldInfo.FieldType;
if (fieldType.IsArrayOrList())
{
fieldType = fieldType.GetCollectionElementType();
}

var dataList = new List<ValidationCommandData>();
foreach (var validationAttribute in list)
{
Wrappers.ValidateCachedProperties(CacheField, data.Property, fieldType, validationAttribute.GetType(), ValidationUtility.Instance);
ValidateCachedPropertiesUtility.Validate(Wrappers, CacheField, data.Property, fieldType, validationAttribute.GetType(), ValidationAttributeUtility.Instance);

var fieldValue = CacheField.Value;
if (fieldValue == null)
Expand Down Expand Up @@ -81,10 +87,17 @@ private static string ObjectCompiler(ValidationCommandData commandData, string r
var property = commandData.Property;
var path = property.IsArrayElement() ? property.GetArrayPath() : property.displayName;
var target = commandData.Target;
var str =
$"Validation failed with: <b>{result}</b>.\nPath: <i><b>{commandData.ContextResolver.Resolve(target)}</b></i>. Component: <i><b>{target.GetType().Name}</b></i>, Property: <i><b>{path}</b></i>";

return str;
var resolvedPath = commandData.PathResolver.Resolve(target);
var typeName = target.GetType().Name;

var stringBuilder = new StringBuilder();
stringBuilder.AppendFormat("Validation failed with: <b>{0}</b>.", result);
stringBuilder.AppendLine();
stringBuilder.AppendFormat("Path: <i><b>{0}</b></i>.", resolvedPath);
stringBuilder.AppendLine();
stringBuilder.AppendFormat("Component: <i><b>{0}</b></i>, Property: <i><b>{1}</b></i>", typeName, path);

return stringBuilder.ToString();
}
}
}
2 changes: 1 addition & 1 deletion Editor/EditorAddons/MenuItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Better.Validation.EditorAddons
{
public static class MenuItems
{
[MenuItem(ValidationSettingsTool.MenuItemPrefix + "/Open Validation Window", false, 50)]
[MenuItem(ValidationSettingProvider.Path + "/Open Validation Window", false, 50)]
private static void ValidateInProject()
{
ValidationWindow.OpenWindow();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Collections.Generic;

namespace Better.Validation.EditorAddons.PreBuildValidation.Interfaces
namespace Better.Validation.EditorAddons.PreBuildValidation
{
public interface IBuildValidationStep
{
public List<ValidationCommandData> GatherValidationData();
public List<ValidationCommandData> GatherValidationData(ValidatorCommands commands);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;

namespace Better.Validation.EditorAddons.PreBuildValidation
{
[Serializable]
public class AllSceneValidationStep : IBuildValidationStep
{
public List<ValidationCommandData> GatherValidationData(ValidatorCommands commands)
{
return commands.ValidateAttributesInAllScenes();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
using System;
using System.Collections.Generic;
using Better.Validation.EditorAddons.PreBuildValidation.Interfaces;

namespace Better.Validation.EditorAddons.PreBuildValidation.Models
namespace Better.Validation.EditorAddons.PreBuildValidation
{
[Serializable]
public class ProjectValidationStep : IBuildValidationStep
{
public List<ValidationCommandData> GatherValidationData()
public List<ValidationCommandData> GatherValidationData(ValidatorCommands commands)
{
var commands = new ValidatorCommands();
return commands.ValidateAttributesInProject();
}
}

[Serializable]
public class AllSceneValidationStep : IBuildValidationStep
{
public List<ValidationCommandData> GatherValidationData()
{
var commands = new ValidatorCommands();
return commands.ValidateAttributesInAllScenes();
}
}
}
36 changes: 21 additions & 15 deletions Editor/EditorAddons/PreBuildValidation/ValidationBuildProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Better.EditorTools.SettingsTools;
using Better.Tools.Runtime;
using Better.Internal.Core.Runtime;
using Better.Validation.EditorAddons.Settings;
using Better.Validation.EditorAddons.WindowModule;
using Better.Validation.Runtime.Attributes;
Expand All @@ -21,25 +20,37 @@ public class ValidationBuildProcess : IPreprocessBuildWithReport

public ValidationBuildProcess()
{
_settings = Resources.Load<ValidationSettings>(nameof(ValidationSettings));
_settings = ValidationSettings.Instance;
}

public void OnPreprocessBuild(BuildReport report)
{
if(_settings.DisableBuildValidation) return;
var validationCommands = new ValidatorCommands();
var commandDatas = new List<ValidationCommandData>();

var validationSteps = _settings.GetSteps();
foreach (var buildValidationStep in validationSteps)
{
commandDatas.AddRange(buildValidationStep.GatherValidationData());
commandDatas.AddRange(buildValidationStep.GatherValidationData(validationCommands));
}

commandDatas = commandDatas.Where(x => x.Type >= _settings.BuildLoggingLevel).ToList();
if (!commandDatas.Any()) return;

var str = CreateDialogMessage(commandDatas);
EditorApplication.Beep();
if (!EditorUtility.DisplayDialog("Validation failed", str.ToString(), "Ignore", "Resolve"))
{
ValidationWindow.OpenWindow(commandDatas);
throw new BuildFailedException("Pre build validation failed");
}
}

private static StringBuilder CreateDialogMessage(List<ValidationCommandData> commandDatas)
{
var str = new StringBuilder("Pre build validation failed.");
str.Append(Environment.NewLine);
str.AppendLine();
str.AppendLine("There are:");
var values = (ValidationType[])Enum.GetValues(typeof(ValidationType));
for (var index = values.Length - 1; index >= 0; index--)
Expand All @@ -48,19 +59,14 @@ public void OnPreprocessBuild(BuildReport report)
var count = commandDatas.Count(x => x.Type == value);
var appendix = count > 1 ? "s" : "";
str.AppendFormat("- {0} {1}{2}", count, value.ToString(), appendix);
str.Append(Environment.NewLine);
str.AppendLine();
}

str.AppendLine("Do you want to ignore those issues?");
str.Append(Environment.NewLine);
str.Append(Environment.NewLine);
str.AppendFormat("(You can disable validation in Edit > Project Settings > {0} > {1})", BetterEditorDefines.BetterPrefix, ProjectSettingsToolsContainer<ValidationSettingsTool>.Instance.NamespacePrefix);
EditorApplication.Beep();
if (!EditorUtility.DisplayDialog("Validation failed", str.ToString(), "Ignore", "Resolve"))
{
ValidationWindow.OpenWindow(commandDatas);
throw new BuildFailedException("Pre build validation failed");
}
str.AppendLine();
str.AppendLine();
str.AppendFormat("(You can disable validation in Edit > Project Settings > {0})", ValidationSettingProvider.Path.Replace("/", " > "));
return str;
}
}
}
Loading

0 comments on commit 58e2625

Please sign in to comment.