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

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feature/refactoring
Browse files Browse the repository at this point in the history
# Conflicts:
#	Assets/BetterValidation/Editor/EditorAddons/Handlers/ClampWrapper.cs
#	Assets/BetterValidation/Editor/EditorAddons/Handlers/ClampWrapper.cs.meta
#	Assets/BetterValidation/Editor/EditorAddons/Handlers/MaxWrapper.cs
#	Assets/BetterValidation/Editor/EditorAddons/Handlers/MaxWrapper.cs.meta
#	Assets/BetterValidation/Editor/EditorAddons/Utility/ValidationAttributeBinder.cs
  • Loading branch information
uurha committed Aug 3, 2024
2 parents 6d3d801 + 400f7c1 commit d37f79c
Show file tree
Hide file tree
Showing 22 changed files with 193 additions and 29 deletions.
3 changes: 3 additions & 0 deletions Assets/BetterValidation/Editor/EditorAddons/Extensions.meta

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,6 +1,7 @@
using System;
using Better.Validation.EditorAddons.Handlers;

namespace Better.Validation.EditorAddons.Handlers
namespace Better.Validation.EditorAddons.Extensions
{
public static class ValidationWrapperExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using Better.Commons.EditorAddons.Extensions;
using Better.Validation.Runtime.Attributes;
using UnityEditor;
using UnityEngine;

namespace Better.Validation.EditorAddons.Handlers
{
public class ClampWrapper : PropertyValidationHandler
{
public override bool IsSupported()
{
return Property.propertyType is SerializedPropertyType.Integer or SerializedPropertyType.Float;
}

public override ValidationValue<string> Validate()
{
var clampAttribute = (ClampAttribute)Attribute;
var minValue = clampAttribute.Min;
var maxValue = clampAttribute.Max;
float value;
switch (Property.propertyType)
{
case SerializedPropertyType.Float:
value = Property.floatValue;
break;
case SerializedPropertyType.Integer:
value = Property.intValue;
break;
default:
return GetNotValidValue($"Property: {Property.displayName} has invalid type: {Property.propertyType}");
}

if (value >= minValue && value <= maxValue)
{
return GetClearValue();
}

value = Mathf.Clamp(value, minValue, maxValue);
switch (Property.propertyType)
{
case SerializedPropertyType.Float:
Property.SetValue(value);
break;
case SerializedPropertyType.Integer:
Property.SetValue((int)value);
break;
}

EditorUtility.SetDirty(Property.serializedObject.targetObject);
Property.serializedObject.ApplyModifiedProperties();
return GetClearValue();
}
}
}

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
Expand Up @@ -32,21 +32,21 @@ public override ValidationValue<string> Validate()
var methodName = $"\"{att.MethodName.FormatBoldItalic()}\"";
if (method == null)
{
return GetNotValidCache($"Method with name {methodName} not found");
return GetNotValidValue($"Method with name {methodName} not found");
}

var parameters = method.GetParameters();
if (parameters.Length > 1)
{
return GetNotValidCache($"Method with name {methodName} has {parameters.Length}. It's not supported");
return GetNotValidValue($"Method with name {methodName} has {parameters.Length}. It's not supported");
}

var parameterInfo = parameters[0];
var parameterType = parameterInfo.ParameterType;
var fieldCacheType = fieldCache.Type;
if (parameterType != fieldCacheType)
{
return GetNotValidCache(
return GetNotValidValue(
$"Method with name {methodName} has parameter of type \"{parameterType.Name.FormatBoldItalic()}\". But used on field of type \"{fieldCacheType.Name.FormatBoldItalic()}\"");
}

Expand All @@ -73,7 +73,7 @@ private ValidationValue<string> InvokeMethod(MethodInfo method, CachedFieldInfo
{
var fieldInfo = fieldCache.FieldInfo;
var name = fieldInfo.FieldType.IsArrayOrList() ? Property.GetArrayPath() : propertyContainer.GetType().Name;
return GetNotValidCache(
return GetNotValidValue(
$"Validation failed of \"{fieldInfo.Name.FormatBoldItalic()}\" in \"{name.FormatBoldItalic()}\"");
}
}
Expand All @@ -82,11 +82,11 @@ private ValidationValue<string> InvokeMethod(MethodInfo method, CachedFieldInfo
var result = (string)method.Invoke(propertyContainer, parameters);
if (!string.IsNullOrEmpty(result))
{
return GetNotValidCache(result);
return GetNotValidValue(result);
}
}

return GetClearCache();
return GetClearValue();
}

public override bool IsSupported()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override ValidationValue<string> Validate()
{
if (obj)
{
return GetClearCache();
return GetClearValue();
}
}

Expand Down Expand Up @@ -50,13 +50,13 @@ public override ValidationValue<string> Validate()

if (!obj)
{
return GetNotValidCache($"Reference of \"{requiredType.Name.FormatBoldItalic()}\" not found");
return GetNotValidValue($"Reference of \"{requiredType.Name.FormatBoldItalic()}\" not found");
}

EditorUtility.SetDirty(targetObject);
Property.objectReferenceValue = obj;
propertySerializedObject.ApplyModifiedProperties();
return GetClearCache();
return GetClearValue();
}

protected Type GetFieldOrElementType()
Expand Down
52 changes: 52 additions & 0 deletions Assets/BetterValidation/Editor/EditorAddons/Handlers/MaxWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using Better.Commons.EditorAddons.Extensions;
using Better.Validation.Runtime.Attributes;
using UnityEditor;

namespace Better.Validation.EditorAddons.Handlers
{
public class MaxWrapper : PropertyValidationHandler
{
public override bool IsSupported()
{
return Property.propertyType is SerializedPropertyType.Integer or SerializedPropertyType.Float;
}

public override ValidationValue<string> Validate()
{
var maxAttribute = (MaxAttribute)Attribute;
var maxValue = maxAttribute.Max;
bool isValid;
switch (Property.propertyType)
{
case SerializedPropertyType.Float:
isValid = Property.floatValue <= maxValue;
break;
case SerializedPropertyType.Integer:
isValid = Property.intValue <= maxValue;
break;
default:
return GetNotValidValue($"Property: {Property.displayName} has invalid type: {Property.propertyType}");
}

if (isValid)
{
return GetClearValue();
}

switch (Property.propertyType)
{
case SerializedPropertyType.Float:
Property.SetValue(maxValue);
break;
case SerializedPropertyType.Integer:
Property.SetValue((int)maxValue);
break;
}

EditorUtility.SetDirty(Property.serializedObject.targetObject);
Property.serializedObject.ApplyModifiedProperties();
return GetClearValue();
}
}
}

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
Expand Up @@ -16,18 +16,18 @@ public MissingComponentHandler(Object target)

public override ValidationValue<string> Validate()
{
if (!_target) return GetClearCache();
if (!_target) return GetClearValue();
if (_target is GameObject gameObject)
{
var components = gameObject.GetComponents<Component>();
for (var index = components.Length - 1; index >= 0; index--)
{
var obj = components[index];
if (!obj) return GetNotValidCache($"Missing Component on GameObject: {_target.name}");
if (!obj) return GetNotValidValue($"Missing Component on GameObject: {_target.name}");
}
}

return GetClearCache();
return GetClearValue();
}

public override bool IsSupported()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public override ValidationValue<string> Validate()
var fieldName = $"\"{Property.displayName.FormatBoldItalic()}\"";
if (Property.objectReferenceInstanceIDValue != 0)
{
return GetNotValidCache($"Object in {fieldName} field is missing reference");
return GetNotValidValue($"Object in {fieldName} field is missing reference");
}
}

return GetClearCache();
return GetClearValue();
}

public override bool IsSupported()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public override ValidationValue<string> Validate()
var fieldName = $"\"{Property.displayName.FormatBoldItalic()}\"";
if (Property.objectReferenceInstanceIDValue != 0)
{
return GetNotValidCache($"Object in {fieldName} field is missing reference");
return GetNotValidValue($"Object in {fieldName} field is missing reference");
}

return GetNotValidCache($"Object in {fieldName} field is null");
return GetNotValidValue($"Object in {fieldName} field is null");
}

return GetClearCache();
return GetClearValue();
}

public override bool IsSupported()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public override ValidationValue<string> Validate()
var str = $"\"{Property.displayName.FormatBoldItalic()}\"";
if (!PrefabUtility.IsPartOfNonAssetPrefabInstance(obj))
{
return GetNotValidCache($"Object in {str} field is not prefab");
return GetNotValidValue($"Object in {str} field is not prefab");
}

return GetNotValidCache($"Object in {str} field is prefab instance in scene");
return GetNotValidValue($"Object in {str} field is prefab instance in scene");
}

return GetClearCache();
return GetClearValue();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ private ValidationValue<string> ValidateNotPrefabContext(Object obj, Object targ
if (isTargetInScene && !isObjectInScene)
{
var str = $"\"{Property.displayName.FormatBoldItalic()}\"";;
return GetNotValidCache($"Object in {str} field is not scene object");
return GetNotValidValue($"Object in {str} field is not scene object");
}

if (!isTargetInScene)
{
return ValueTuple(obj, target);
}

return GetClearCache();
return GetClearValue();
}

private bool IsObjectInScene(Object obj)
Expand All @@ -62,11 +62,11 @@ private ValidationValue<string> ValueTuple(Object obj, Object target)
var equals = objRoot == targetRoot;
if (!equals)
{
return GetNotValidCache(
return GetNotValidValue(
$"Object in \"{Property.displayName.FormatBoldItalic()}\" field is not part of \"{target.name.FormatBoldItalic()}\" prefab");
}

return GetClearCache();
return GetClearValue();
}

private static Object GetOutermostPrefabInstanceRoot(Object obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public abstract class ValidationHandler : SerializedPropertyHandler

public abstract bool IsSupported();

public static ValidationValue<string> GetNotValidCache(string value)
public static ValidationValue<string> GetNotValidValue(string value)
{
ValidationValue.Set(false, value);
return ValidationValue;
}

public static ValidationValue<string> GetClearCache()
public static ValidationValue<string> GetClearValue()
{
ValidationValue.Set(true, string.Empty);
return ValidationValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Better.Commons.EditorAddons.Drawers.Handlers;
using Better.Commons.EditorAddons.Extensions;
using Better.Commons.Runtime.Extensions;
using Better.Validation.EditorAddons.Extensions;
using Better.Validation.EditorAddons.Handlers;
using Better.Validation.Runtime.Attributes;

Expand Down Expand Up @@ -37,7 +38,7 @@ public static IEnumerable<ValidationCommandData> PropertyIterationWithAttributes

propertyHandler.Setup(data.Property, fieldInfo.FieldInfo, validationAttribute);

var result = handler.IsSupported() ? handler.Validate() : ValidationHandler.GetClearCache();
var result = handler.IsSupported() ? handler.Validate() : ValidationHandler.GetClearValue();

if (result.State) continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ protected override BaseHandlersTypeCollection GenerateCollection()
{ typeof(SceneReferenceAttribute), typeof(SceneReferenceHandler) },
{ typeof(FindAttribute), typeof(FindComponentHandler) },
{ typeof(DataValidationAttribute), typeof(DataValidationHandler) },
{ typeof(MaxAttribute), typeof(MaxWrapper) },
{ typeof(ClampAttribute), typeof(ClampWrapper) },
};
}

Expand Down
20 changes: 20 additions & 0 deletions Assets/BetterValidation/Runtime/Attributes/ClampAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Diagnostics;
using Better.Internal.Core.Runtime;

namespace Better.Validation.Runtime.Attributes
{
[Conditional(Defines.Editor)]
[AttributeUsage(AttributeTargets.Field)]
public class ClampAttribute : ValidationAttribute
{
public float Min { get; }
public float Max { get; }

public ClampAttribute(float min, float max)
{
Min = min;
Max = max;
}
}
}

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

18 changes: 18 additions & 0 deletions Assets/BetterValidation/Runtime/Attributes/MaxAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Diagnostics;
using Better.Internal.Core.Runtime;

namespace Better.Validation.Runtime.Attributes
{
[Conditional(Defines.Editor)]
[AttributeUsage(AttributeTargets.Field)]
public class MaxAttribute : ValidationAttribute
{
public float Max { get; }

public MaxAttribute(float max)
{
Max = max;
}
}
}
Loading

0 comments on commit d37f79c

Please sign in to comment.