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 #29 from techno-dwarf-works/feature/refactoring
Browse files Browse the repository at this point in the history
Version 1.6.2
  • Loading branch information
uurha authored Aug 3, 2024
2 parents 400f7c1 + d37f79c commit 98ab269
Show file tree
Hide file tree
Showing 97 changed files with 1,270 additions and 1,339 deletions.

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

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

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

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

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

26 changes: 0 additions & 26 deletions Assets/Better/Validation/Editor/Resources/ValidationSettings.asset

This file was deleted.

This file was deleted.

8 changes: 0 additions & 8 deletions Assets/Better/Validation/Resources.meta

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Better.Commons.Runtime.Comparers;
using UnityEngine;

namespace Better.Validation.EditorAddons.WindowModule
namespace Better.Validation.EditorAddons.Comparers
{
public class GameObjectGroupingComparer : BaseComparer<GameObjectGroupingComparer, Object>, IEqualityComparer<Object>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,67 +1,51 @@
using System.Reflection;
using Better.Commons.EditorAddons.Drawers.Attributes;
using Better.Commons.EditorAddons.Drawers.Base;
using Better.Commons.EditorAddons.Drawers.Caching;
using Better.Commons.EditorAddons.Drawers;
using Better.Commons.EditorAddons.Drawers.Container;
using Better.Commons.EditorAddons.Extensions;
using Better.Commons.EditorAddons.Utility;
using Better.Commons.Runtime.Drawers.Attributes;
using Better.Commons.Runtime.Extensions;
using Better.Validation.EditorAddons.Handlers;
using Better.Validation.EditorAddons.Utility;
using Better.Validation.EditorAddons.Wrappers;
using Better.Validation.Runtime.Attributes;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace Better.Validation.EditorAddons.Drawers
{
[MultiCustomPropertyDrawer(typeof(ValidationAttribute))]
public class ValidationDrawer : MultiFieldDrawer<PropertyValidationWrapper>
[CustomPropertyDrawer(typeof(ValidationAttribute))]
public class ValidationDrawer : BasePropertyDrawer<PropertyValidationHandler, ValidationAttribute>
{
private CacheValue<MutableTuple<string, ValidationType>> _validationResult = new CacheValue<MutableTuple<string, ValidationType>>();

public ValidationDrawer(FieldInfo fieldInfo, MultiPropertyAttribute attribute) : base(fieldInfo, attribute)
protected override void PopulateContainer(ElementsContainer container)
{
UpdateDrawer(container);
container.SerializedPropertyChanged += UpdateDrawer;
}

protected override bool PreDraw(ref Rect position, SerializedProperty property, GUIContent label)
{
var cache = ValidateCachedProperties(property, ValidationAttributeUtility.Instance);
var validationWrapper = cache.Value;
var wrapper = validationWrapper.Wrapper;
if (!cache.IsValid)
{
if (cache.Value == null)
{
return false;
}

wrapper.SetProperty(property, (ValidationAttribute)_attribute);
}
private void UpdateDrawer(ElementsContainer container)
{
var handler = GetHandler(container.SerializedProperty);
handler.Setup(container.SerializedProperty, FieldInfo, Attribute);

if (wrapper.IsSupported())
if (handler.IsSupported())
{
var validation = wrapper.Validate();
_validationResult.Set(validation.IsValid, new MutableTuple<string, ValidationType>(validation.Value, wrapper.Type));
}
var validation = handler.Validate();

return true;
}
HelpBox helpBox;
if (!container.TryGetByTag(container.SerializedProperty, out var element))
{
helpBox = new HelpBox();
element = container.CreateElementFrom(helpBox);
element.AddTag(container.SerializedProperty);
}
else
{
helpBox = element.Q<HelpBox>();
}

protected override Rect PreparePropertyRect(Rect original)
{
return original;
}
helpBox.text = validation.Result;
helpBox.messageType = handler.Type.GetMessageType();

protected override void PostDraw(Rect position, SerializedProperty property, GUIContent label)
{
if (!_validationResult.IsValid)
{
var (value, type) = _validationResult.Value;
ExtendedGUIUtility.HelpBox(value, type.GetIconType());
helpBox.style.SetVisible(!validation.State);
}
}

protected override WrapperCollection<PropertyValidationWrapper> GenerateCollection()
{
return new WrapperCollection<PropertyValidationWrapper>();
}
}
}
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,13 +1,14 @@
using System;
using Better.Validation.EditorAddons.Handlers;

namespace Better.Validation.EditorAddons.Wrappers
namespace Better.Validation.EditorAddons.Extensions
{
public static class ValidationWrapperExtensions
{
public static T Copy<T>(this T target) where T : PropertyValidationWrapper
public static T Copy<T>(this T target) where T : PropertyValidationHandler
{
var copy = (T)Activator.CreateInstance(target.GetType());
copy.SetProperty(target.Property, target.Attribute);
copy.Setup(target.Property, target.FieldInfo, target.Attribute);
return copy;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
using System;
using Better.Commons.EditorAddons.Drawers.Caching;
using Better.Commons.EditorAddons.Extensions;
using Better.Commons.EditorAddons.Utility;
using Better.Validation.Runtime.Attributes;
using UnityEditor;
using UnityEngine;

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

public override CacheValue<string> Validate()
public override ValidationValue<string> Validate()
{
var clampAttribute = (ClampAttribute)Attribute;
var minValue = clampAttribute.Min;
var maxValue = clampAttribute.Max;
var value = Property.propertyType switch
float value;
switch (Property.propertyType)
{
SerializedPropertyType.Float => Property.floatValue,
SerializedPropertyType.Integer => Property.intValue,
_ => throw new ArgumentOutOfRangeException()
};
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 GetClearCache();
return GetClearValue();
}

value = Mathf.Clamp(value, minValue, maxValue);
Expand All @@ -45,7 +49,7 @@ public override CacheValue<string> Validate()

EditorUtility.SetDirty(Property.serializedObject.targetObject);
Property.serializedObject.ApplyModifiedProperties();
return GetClearCache();
return GetClearValue();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,53 @@
using System.Reflection;
using Better.Commons.EditorAddons.Drawers.Caching;
using Better.Commons.EditorAddons.Extensions;
using Better.Commons.EditorAddons.Helpers;
using Better.Commons.EditorAddons.Utility;
using Better.Commons.Runtime.Extensions;
using Better.Internal.Core.Runtime;
using Better.Validation.Runtime.Attributes;

namespace Better.Validation.EditorAddons.Wrappers
namespace Better.Validation.EditorAddons.Handlers
{
public class DataValidationWrapper : PropertyValidationWrapper
public class ValidationValue<T>
{
public override CacheValue<string> Validate()
public bool State { get; set; }

public T Result { get; set; }

public void Set(bool state, T result)
{
State = state;
Result = result;
}
}

public class DataValidationHandler : PropertyValidationHandler
{
public override ValidationValue<string> Validate()
{
var fieldCache = Property.GetFieldInfoAndStaticTypeFromProperty();
var att = (DataValidationAttribute)Attribute;

var propertyContainer = Property.GetPropertyContainer();
var method = propertyContainer.GetType().GetMethod(att.MethodName, Defines.MethodFlags);
var methodName = ExtendedGUIUtility.BeautifyFormat(att.MethodName);
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];
if (parameterInfo.ParameterType != fieldCache.Type)
var parameterType = parameterInfo.ParameterType;
var fieldCacheType = fieldCache.Type;
if (parameterType != fieldCacheType)
{
return GetNotValidCache(
$"Method with name {methodName} has parameter of type {ExtendedGUIUtility.BeautifyFormat(parameterInfo.ParameterType.Name)}. But used on field of type {ExtendedGUIUtility.BeautifyFormat(fieldCache.Type.Name)}");
return GetNotValidValue(
$"Method with name {methodName} has parameter of type \"{parameterType.Name.FormatBoldItalic()}\". But used on field of type \"{fieldCacheType.Name.FormatBoldItalic()}\"");
}

if (method.IsStatic)
Expand All @@ -45,7 +58,7 @@ public override CacheValue<string> Validate()
return InvokeMethod(method, fieldCache, propertyContainer);
}

private CacheValue<string> InvokeMethod(MethodInfo method, CachedFieldInfo fieldCache, object propertyContainer)
private ValidationValue<string> InvokeMethod(MethodInfo method, CachedFieldInfo fieldCache, object propertyContainer)
{
var value = Property.GetValue();
var parameters = new object[] { value };
Expand All @@ -58,21 +71,22 @@ private CacheValue<string> InvokeMethod(MethodInfo method, CachedFieldInfo field
var result = (bool)method.Invoke(propertyContainer, parameters);
if (!result)
{
var name = fieldCache.FieldInfo.FieldType.IsArrayOrList() ? Property.GetArrayPath() : propertyContainer.GetType().Name;
return GetNotValidCache(
$"Validation failed of {ExtendedGUIUtility.BeautifyFormat(fieldCache.FieldInfo.Name)} in {ExtendedGUIUtility.BeautifyFormat(name)}");
var fieldInfo = fieldCache.FieldInfo;
var name = fieldInfo.FieldType.IsArrayOrList() ? Property.GetArrayPath() : propertyContainer.GetType().Name;
return GetNotValidValue(
$"Validation failed of \"{fieldInfo.Name.FormatBoldItalic()}\" in \"{name.FormatBoldItalic()}\"");
}
}
else if (method.ReturnType == typeof(string))
{
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
Loading

0 comments on commit 98ab269

Please sign in to comment.