diff --git a/Assets/Better/Validation/Editor.meta b/Assets/Better/Editor.meta similarity index 77% rename from Assets/Better/Validation/Editor.meta rename to Assets/Better/Editor.meta index bc3cdb3..90538fe 100644 --- a/Assets/Better/Validation/Editor.meta +++ b/Assets/Better/Editor.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 7f6452b97b5587841aaabb0501341cd4 +guid: 426c69501a4b1b442ace971fb5c55519 folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/Better/Validation.meta b/Assets/Better/Editor/Validation.meta similarity index 77% rename from Assets/Better/Validation.meta rename to Assets/Better/Editor/Validation.meta index 21833f1..6dfe2ea 100644 --- a/Assets/Better/Validation.meta +++ b/Assets/Better/Editor/Validation.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 60ad88e549305164fa456de41d61b8f8 +guid: fc0e8a20aedd04c42b296a3068ba3dd1 folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/Better/Validation/Editor/Resources.meta b/Assets/Better/Editor/Validation/Resources.meta similarity index 77% rename from Assets/Better/Validation/Editor/Resources.meta rename to Assets/Better/Editor/Validation/Resources.meta index a0ab7ef..463de8c 100644 --- a/Assets/Better/Validation/Editor/Resources.meta +++ b/Assets/Better/Editor/Validation/Resources.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 7cd900e0faf5e124cae048f7fc11172a +guid: ca0ec50311d722d4f939c0c2d67d634a folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/Better/Validation/Resources/ValidationSettings.asset b/Assets/Better/Editor/Validation/Resources/ValidationSettings.asset similarity index 86% rename from Assets/Better/Validation/Resources/ValidationSettings.asset rename to Assets/Better/Editor/Validation/Resources/ValidationSettings.asset index 7a41a14..239df04 100644 --- a/Assets/Better/Validation/Resources/ValidationSettings.asset +++ b/Assets/Better/Editor/Validation/Resources/ValidationSettings.asset @@ -15,12 +15,12 @@ MonoBehaviour: _disableBuildValidation: 0 _buildLoggingLevel: 1 _validationSteps: - - rid: 1811633586712084480 - - rid: 1811633586712084481 + - rid: 5242900319484510208 + - rid: 5242900319484510209 references: version: 2 RefIds: - - rid: 1811633586712084480 + - rid: 5242900319484510208 type: {class: ProjectValidationStep, ns: Better.Validation.EditorAddons.PreBuildValidation, asm: BetterValidation.Editor} - - rid: 1811633586712084481 + - rid: 5242900319484510209 type: {class: AllSceneValidationStep, ns: Better.Validation.EditorAddons.PreBuildValidation, asm: BetterValidation.Editor} diff --git a/Assets/Better/Validation/Resources/ValidationSettings.asset.meta b/Assets/Better/Editor/Validation/Resources/ValidationSettings.asset.meta similarity index 79% rename from Assets/Better/Validation/Resources/ValidationSettings.asset.meta rename to Assets/Better/Editor/Validation/Resources/ValidationSettings.asset.meta index 0605f54..5e2f66f 100644 --- a/Assets/Better/Validation/Resources/ValidationSettings.asset.meta +++ b/Assets/Better/Editor/Validation/Resources/ValidationSettings.asset.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 30201084575d42145b7d0f6b470fcd3a +guid: b600c85ac14194f46890c28cb46e9012 NativeFormatImporter: externalObjects: {} mainObjectFileID: 11400000 diff --git a/Assets/Better/Validation/Editor/Resources/ValidationSettings.asset b/Assets/Better/Validation/Editor/Resources/ValidationSettings.asset deleted file mode 100644 index 4d1492b..0000000 --- a/Assets/Better/Validation/Editor/Resources/ValidationSettings.asset +++ /dev/null @@ -1,26 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!114 &11400000 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 89a02ae4bedf42c29b1abd8ca87a5605, type: 3} - m_Name: ValidationSettings - m_EditorClassIdentifier: - disableBuildValidation: 0 - buildLoggingLevel: 1 - validationSteps: - - rid: 4526805336792236032 - - rid: 4526805336792236033 - references: - version: 2 - RefIds: - - rid: 4526805336792236032 - type: {class: ProjectValidationStep, ns: Better.Validation.EditorAddons.PreBuildValidation.Models, asm: Better.Validation.Editor} - - rid: 4526805336792236033 - type: {class: AllSceneValidationStep, ns: Better.Validation.EditorAddons.PreBuildValidation.Models, asm: Better.Validation.Editor} diff --git a/Assets/Better/Validation/Editor/Resources/ValidationSettings.asset.meta b/Assets/Better/Validation/Editor/Resources/ValidationSettings.asset.meta deleted file mode 100644 index 7bae1cd..0000000 --- a/Assets/Better/Validation/Editor/Resources/ValidationSettings.asset.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 95fd5aa3831a90549a80b1aeefdc0617 -NativeFormatImporter: - externalObjects: {} - mainObjectFileID: 11400000 - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Better/Validation/Resources.meta b/Assets/Better/Validation/Resources.meta deleted file mode 100644 index eb64e5e..0000000 --- a/Assets/Better/Validation/Resources.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 0ba2a324adf74a14a8b650413862005a -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/BetterValidation/Editor/EditorAddons/Comparers/GameObjectGroupingComparer.cs b/Assets/BetterValidation/Editor/EditorAddons/Comparers/GameObjectGroupingComparer.cs index 77148fa..3e6487e 100644 --- a/Assets/BetterValidation/Editor/EditorAddons/Comparers/GameObjectGroupingComparer.cs +++ b/Assets/BetterValidation/Editor/EditorAddons/Comparers/GameObjectGroupingComparer.cs @@ -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, IEqualityComparer { diff --git a/Assets/BetterValidation/Editor/EditorAddons/Drawers/ValidationDrawer.cs b/Assets/BetterValidation/Editor/EditorAddons/Drawers/ValidationDrawer.cs index aa61f9f..d0322e8 100644 --- a/Assets/BetterValidation/Editor/EditorAddons/Drawers/ValidationDrawer.cs +++ b/Assets/BetterValidation/Editor/EditorAddons/Drawers/ValidationDrawer.cs @@ -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 + [CustomPropertyDrawer(typeof(ValidationAttribute))] + public class ValidationDrawer : BasePropertyDrawer { - private CacheValue> _validationResult = new CacheValue>(); - - 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(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(); + } - 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 GenerateCollection() - { - return new WrapperCollection(); - } } } \ No newline at end of file diff --git a/Assets/BetterValidation/Editor/EditorAddons/Extensions.meta b/Assets/BetterValidation/Editor/EditorAddons/Extensions.meta new file mode 100644 index 0000000..a444a5e --- /dev/null +++ b/Assets/BetterValidation/Editor/EditorAddons/Extensions.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: bcfb4909067347e383c1fa671f956ed8 +timeCreated: 1722651618 \ No newline at end of file diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/ValidationWrapperExtensions.cs b/Assets/BetterValidation/Editor/EditorAddons/Extensions/ValidationWrapperExtensions.cs similarity index 57% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/ValidationWrapperExtensions.cs rename to Assets/BetterValidation/Editor/EditorAddons/Extensions/ValidationWrapperExtensions.cs index f777fd2..5215920 100644 --- a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/ValidationWrapperExtensions.cs +++ b/Assets/BetterValidation/Editor/EditorAddons/Extensions/ValidationWrapperExtensions.cs @@ -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(this T target) where T : PropertyValidationWrapper + public static T Copy(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; } } diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/ValidationWrapperExtensions.cs.meta b/Assets/BetterValidation/Editor/EditorAddons/Extensions/ValidationWrapperExtensions.cs.meta similarity index 100% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/ValidationWrapperExtensions.cs.meta rename to Assets/BetterValidation/Editor/EditorAddons/Extensions/ValidationWrapperExtensions.cs.meta diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers.meta b/Assets/BetterValidation/Editor/EditorAddons/Handlers.meta similarity index 100% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers.meta rename to Assets/BetterValidation/Editor/EditorAddons/Handlers.meta diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/ClampWrapper.cs b/Assets/BetterValidation/Editor/EditorAddons/Handlers/ClampWrapper.cs similarity index 60% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/ClampWrapper.cs rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/ClampWrapper.cs index 116b3c8..9839703 100644 --- a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/ClampWrapper.cs +++ b/Assets/BetterValidation/Editor/EditorAddons/Handlers/ClampWrapper.cs @@ -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 Validate() + public override ValidationValue 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); @@ -45,7 +49,7 @@ public override CacheValue Validate() EditorUtility.SetDirty(Property.serializedObject.targetObject); Property.serializedObject.ApplyModifiedProperties(); - return GetClearCache(); + return GetClearValue(); } } } \ No newline at end of file diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/ClampWrapper.cs.meta b/Assets/BetterValidation/Editor/EditorAddons/Handlers/ClampWrapper.cs.meta similarity index 100% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/ClampWrapper.cs.meta rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/ClampWrapper.cs.meta diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/DataValidationWrapper.cs b/Assets/BetterValidation/Editor/EditorAddons/Handlers/DataValidationHandler.cs similarity index 56% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/DataValidationWrapper.cs rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/DataValidationHandler.cs index e2c50c3..5792e96 100644 --- a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/DataValidationWrapper.cs +++ b/Assets/BetterValidation/Editor/EditorAddons/Handlers/DataValidationHandler.cs @@ -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 { - public override CacheValue 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 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) @@ -45,7 +58,7 @@ public override CacheValue Validate() return InvokeMethod(method, fieldCache, propertyContainer); } - private CacheValue InvokeMethod(MethodInfo method, CachedFieldInfo fieldCache, object propertyContainer) + private ValidationValue InvokeMethod(MethodInfo method, CachedFieldInfo fieldCache, object propertyContainer) { var value = Property.GetValue(); var parameters = new object[] { value }; @@ -58,9 +71,10 @@ private CacheValue 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)) @@ -68,11 +82,11 @@ private CacheValue InvokeMethod(MethodInfo method, CachedFieldInfo field 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() diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/DataValidationWrapper.cs.meta b/Assets/BetterValidation/Editor/EditorAddons/Handlers/DataValidationHandler.cs.meta similarity index 100% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/DataValidationWrapper.cs.meta rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/DataValidationHandler.cs.meta diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/RequireComponentWrapper.cs b/Assets/BetterValidation/Editor/EditorAddons/Handlers/FindComponentHandler.cs similarity index 51% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/RequireComponentWrapper.cs rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/FindComponentHandler.cs index 27525a4..3ad367c 100644 --- a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/RequireComponentWrapper.cs +++ b/Assets/BetterValidation/Editor/EditorAddons/Handlers/FindComponentHandler.cs @@ -1,48 +1,47 @@ using System; -using Better.Commons.EditorAddons.Drawers.Caching; -using Better.Commons.EditorAddons.Utility; +using Better.Commons.Runtime.Extensions; using Better.Validation.Runtime.Attributes; using UnityEditor; using UnityEngine; -namespace Better.Validation.EditorAddons.Wrappers +namespace Better.Validation.EditorAddons.Handlers { - public class RequireComponentWrapper : PropertyValidationWrapper + public class FindComponentHandler : PropertyValidationHandler { - private FindAttribute _attributeData; + private FindAttribute _findAttribute; - public override void SetProperty(SerializedProperty property, ValidationAttribute attribute) + protected override void OnSetup() { - base.SetProperty(property, attribute); - _attributeData = (FindAttribute)attribute; + _findAttribute = (FindAttribute)Attribute; } - - public override CacheValue Validate() + + public override ValidationValue Validate() { var obj = Property.objectReferenceValue; - if (_attributeData.ValidateIfFieldEmpty) + if (_findAttribute.ValidateIfFieldEmpty) { if (obj) { - return GetClearCache(); + return GetClearValue(); } } var propertySerializedObject = Property.serializedObject; var targetObject = propertySerializedObject.targetObject; var gameObject = ((Component)targetObject)?.gameObject; + var requiredType = GetFieldOrElementType(); if (gameObject) { - switch (_attributeData.RequireDirection) + switch (_findAttribute.RequireDirection) { case RequireDirection.Parent: - obj = gameObject.GetComponentInParent(_attributeData.RequiredType); + obj = gameObject.GetComponentInParent(requiredType); break; case RequireDirection.None: - obj = gameObject.GetComponent(_attributeData.RequiredType); + obj = gameObject.GetComponent(requiredType); break; case RequireDirection.Child: - obj = gameObject.GetComponentInChildren(_attributeData.RequiredType); + obj = gameObject.GetComponentInChildren(requiredType); break; default: throw new ArgumentOutOfRangeException(); @@ -51,13 +50,27 @@ public override CacheValue Validate() if (!obj) { - return GetNotValidCache($"Reference of {ExtendedGUIUtility.BeautifyFormat(_attributeData.RequiredType.Name)} 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() + { + var t = _findAttribute.RequiredType; + if (t != null) + { + return t; + } + + var fieldType = FieldInfo.FieldType; + if (fieldType.IsArrayOrList()) + return fieldType.GetCollectionElementType(); + return fieldType; } public override bool IsSupported() diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/RequireComponentWrapper.cs.meta b/Assets/BetterValidation/Editor/EditorAddons/Handlers/FindComponentHandler.cs.meta similarity index 100% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/RequireComponentWrapper.cs.meta rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/FindComponentHandler.cs.meta diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/MaxWrapper.cs b/Assets/BetterValidation/Editor/EditorAddons/Handlers/MaxWrapper.cs similarity index 56% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/MaxWrapper.cs rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/MaxWrapper.cs index 9edc112..b948d52 100644 --- a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/MaxWrapper.cs +++ b/Assets/BetterValidation/Editor/EditorAddons/Handlers/MaxWrapper.cs @@ -1,33 +1,37 @@ 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; -namespace Better.Validation.EditorAddons.Wrappers +namespace Better.Validation.EditorAddons.Handlers { - public class MaxWrapper : PropertyValidationWrapper + public class MaxWrapper : PropertyValidationHandler { public override bool IsSupported() { return Property.propertyType is SerializedPropertyType.Integer or SerializedPropertyType.Float; } - public override CacheValue Validate() + public override ValidationValue Validate() { var maxAttribute = (MaxAttribute)Attribute; var maxValue = maxAttribute.Max; - var isValid = Property.propertyType switch + bool isValid; + switch (Property.propertyType) { - SerializedPropertyType.Float => Property.floatValue <= maxValue, - SerializedPropertyType.Integer => Property.intValue <= maxValue, - _ => throw new ArgumentOutOfRangeException() - }; + 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 GetClearCache(); + return GetClearValue(); } switch (Property.propertyType) @@ -42,7 +46,7 @@ public override CacheValue Validate() EditorUtility.SetDirty(Property.serializedObject.targetObject); Property.serializedObject.ApplyModifiedProperties(); - return GetClearCache(); + return GetClearValue(); } } } \ No newline at end of file diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/MaxWrapper.cs.meta b/Assets/BetterValidation/Editor/EditorAddons/Handlers/MaxWrapper.cs.meta similarity index 100% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/MaxWrapper.cs.meta rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/MaxWrapper.cs.meta diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/MissingComponentWrapper.cs b/Assets/BetterValidation/Editor/EditorAddons/Handlers/MissingComponentHandler.cs similarity index 60% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/MissingComponentWrapper.cs rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/MissingComponentHandler.cs index 9d92a54..400dd3c 100644 --- a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/MissingComponentWrapper.cs +++ b/Assets/BetterValidation/Editor/EditorAddons/Handlers/MissingComponentHandler.cs @@ -1,34 +1,33 @@ -using Better.Commons.EditorAddons.Drawers.Caching; -using Better.Validation.Runtime.Attributes; +using Better.Validation.Runtime.Attributes; using UnityEngine; -namespace Better.Validation.EditorAddons.Wrappers +namespace Better.Validation.EditorAddons.Handlers { - public class MissingComponentWrapper : ValidationWrapper + public class MissingComponentHandler : ValidationHandler { private Object _target; - public MissingComponentWrapper(Object target) + public MissingComponentHandler(Object target) { _target = target; } public override ValidationType Type => ValidationType.Error; - public override CacheValue Validate() + public override ValidationValue Validate() { - if (!_target) return GetClearCache(); + if (!_target) return GetClearValue(); if (_target is GameObject gameObject) { var components = gameObject.GetComponents(); 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() diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/MissingComponentWrapper.cs.meta b/Assets/BetterValidation/Editor/EditorAddons/Handlers/MissingComponentHandler.cs.meta similarity index 100% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/MissingComponentWrapper.cs.meta rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/MissingComponentHandler.cs.meta diff --git a/Assets/BetterValidation/Editor/EditorAddons/Handlers/MissingReferenceHandler.cs b/Assets/BetterValidation/Editor/EditorAddons/Handlers/MissingReferenceHandler.cs new file mode 100644 index 0000000..b9db5c7 --- /dev/null +++ b/Assets/BetterValidation/Editor/EditorAddons/Handlers/MissingReferenceHandler.cs @@ -0,0 +1,27 @@ +using Better.Commons.Runtime.Extensions; +using UnityEditor; + +namespace Better.Validation.EditorAddons.Handlers +{ + public class MissingReferenceHandler : PropertyValidationHandler + { + public override ValidationValue Validate() + { + if (Property.objectReferenceValue.IsNullOrDestroyed()) + { + var fieldName = $"\"{Property.displayName.FormatBoldItalic()}\""; + if (Property.objectReferenceInstanceIDValue != 0) + { + return GetNotValidValue($"Object in {fieldName} field is missing reference"); + } + } + + return GetClearValue(); + } + + public override bool IsSupported() + { + return Property.propertyType == SerializedPropertyType.ObjectReference; + } + } +} \ No newline at end of file diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/MissingReferenceWrapper.cs.meta b/Assets/BetterValidation/Editor/EditorAddons/Handlers/MissingReferenceHandler.cs.meta similarity index 100% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/MissingReferenceWrapper.cs.meta rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/MissingReferenceHandler.cs.meta diff --git a/Assets/BetterValidation/Editor/EditorAddons/Handlers/NotNullHandler.cs b/Assets/BetterValidation/Editor/EditorAddons/Handlers/NotNullHandler.cs new file mode 100644 index 0000000..273ab54 --- /dev/null +++ b/Assets/BetterValidation/Editor/EditorAddons/Handlers/NotNullHandler.cs @@ -0,0 +1,29 @@ +using Better.Commons.Runtime.Extensions; +using UnityEditor; + +namespace Better.Validation.EditorAddons.Handlers +{ + public class NotNullHandler : PropertyValidationHandler + { + public override ValidationValue Validate() + { + if (Property.objectReferenceValue.IsNullOrDestroyed()) + { + var fieldName = $"\"{Property.displayName.FormatBoldItalic()}\""; + if (Property.objectReferenceInstanceIDValue != 0) + { + return GetNotValidValue($"Object in {fieldName} field is missing reference"); + } + + return GetNotValidValue($"Object in {fieldName} field is null"); + } + + return GetClearValue(); + } + + public override bool IsSupported() + { + return Property.propertyType == SerializedPropertyType.ObjectReference; + } + } +} \ No newline at end of file diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/NotNullWrapper.cs.meta b/Assets/BetterValidation/Editor/EditorAddons/Handlers/NotNullHandler.cs.meta similarity index 100% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/NotNullWrapper.cs.meta rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/NotNullHandler.cs.meta diff --git a/Assets/BetterValidation/Editor/EditorAddons/Handlers/PrefabHandler.cs b/Assets/BetterValidation/Editor/EditorAddons/Handlers/PrefabHandler.cs new file mode 100644 index 0000000..a05fc68 --- /dev/null +++ b/Assets/BetterValidation/Editor/EditorAddons/Handlers/PrefabHandler.cs @@ -0,0 +1,31 @@ +using Better.Commons.Runtime.Extensions; +using UnityEditor; + +namespace Better.Validation.EditorAddons.Handlers +{ + public class PrefabHandler : NotNullHandler + { + public override ValidationValue Validate() + { + var baseResult = base.Validate(); + if (!baseResult.State) + { + return baseResult; + } + + var obj = Property.objectReferenceValue; + if (!PrefabUtility.IsPartOfPrefabAsset(obj)) + { + var str = $"\"{Property.displayName.FormatBoldItalic()}\""; + if (!PrefabUtility.IsPartOfNonAssetPrefabInstance(obj)) + { + return GetNotValidValue($"Object in {str} field is not prefab"); + } + + return GetNotValidValue($"Object in {str} field is prefab instance in scene"); + } + + return GetClearValue(); + } + } +} \ No newline at end of file diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/PrefabWrapper.cs.meta b/Assets/BetterValidation/Editor/EditorAddons/Handlers/PrefabHandler.cs.meta similarity index 100% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/PrefabWrapper.cs.meta rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/PrefabHandler.cs.meta diff --git a/Assets/BetterValidation/Editor/EditorAddons/Handlers/PropertyValidationHandler.cs b/Assets/BetterValidation/Editor/EditorAddons/Handlers/PropertyValidationHandler.cs new file mode 100644 index 0000000..65bb1af --- /dev/null +++ b/Assets/BetterValidation/Editor/EditorAddons/Handlers/PropertyValidationHandler.cs @@ -0,0 +1,34 @@ +using System; +using System.Reflection; +using Better.Validation.Runtime.Attributes; +using UnityEditor; + +namespace Better.Validation.EditorAddons.Handlers +{ + public abstract class PropertyValidationHandler : ValidationHandler + { + protected internal SerializedProperty Property { get; private set; } + protected internal ValidationAttribute Attribute { get; private set; } + protected internal FieldInfo FieldInfo { get; private set; } + + public override ValidationType Type => Attribute.ValidationType; + + public void Setup(SerializedProperty property, FieldInfo fieldInfo, ValidationAttribute attribute) + { + Property = property; + Attribute = attribute; + FieldInfo = fieldInfo; + + OnSetup(); + } + + protected virtual void OnSetup() + { + } + + public override void Deconstruct() + { + Property = null; + } + } +} \ No newline at end of file diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/PropertyValidationWrapper.cs.meta b/Assets/BetterValidation/Editor/EditorAddons/Handlers/PropertyValidationHandler.cs.meta similarity index 100% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/PropertyValidationWrapper.cs.meta rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/PropertyValidationHandler.cs.meta diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/SceneReferenceWrapper.cs b/Assets/BetterValidation/Editor/EditorAddons/Handlers/SceneReferenceHandler.cs similarity index 67% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/SceneReferenceWrapper.cs rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/SceneReferenceHandler.cs index 4bace79..14bdea1 100644 --- a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/SceneReferenceWrapper.cs +++ b/Assets/BetterValidation/Editor/EditorAddons/Handlers/SceneReferenceHandler.cs @@ -1,17 +1,15 @@ -using Better.Commons.EditorAddons.Drawers.Caching; -using Better.Commons.EditorAddons.Utility; -using Better.Commons.Runtime.Extensions; +using Better.Commons.Runtime.Extensions; using UnityEditor; using UnityEngine; -namespace Better.Validation.EditorAddons.Wrappers +namespace Better.Validation.EditorAddons.Handlers { - public class SceneReferenceWrapper : NotNullWrapper + public class SceneReferenceHandler : NotNullHandler { - public override CacheValue Validate() + public override ValidationValue Validate() { var baseResult = base.Validate(); - if (!baseResult.IsValid) + if (!baseResult.State) { return baseResult; } @@ -23,15 +21,15 @@ public override CacheValue Validate() return ValidateNotPrefabContext(obj, target); } - private CacheValue ValidateNotPrefabContext(Object obj, Object target) + private ValidationValue ValidateNotPrefabContext(Object obj, Object target) { var isObjectInScene = IsObjectInScene(obj); var isTargetInScene = IsObjectInScene(target); if (isTargetInScene && !isObjectInScene) { - var str = ExtendedGUIUtility.BeautifyFormat(Property.displayName); - return GetNotValidCache($"Object in {str} field is not scene object"); + var str = $"\"{Property.displayName.FormatBoldItalic()}\"";; + return GetNotValidValue($"Object in {str} field is not scene object"); } if (!isTargetInScene) @@ -39,7 +37,7 @@ private CacheValue ValidateNotPrefabContext(Object obj, Object target) return ValueTuple(obj, target); } - return GetClearCache(); + return GetClearValue(); } private bool IsObjectInScene(Object obj) @@ -57,18 +55,18 @@ private bool IsObjectInScene(Object obj) return false; } - private CacheValue ValueTuple(Object obj, Object target) + private ValidationValue ValueTuple(Object obj, Object target) { var objRoot = GetOutermostPrefabInstanceRoot(obj); var targetRoot = GetOutermostPrefabInstanceRoot(target); var equals = objRoot == targetRoot; if (!equals) { - return GetNotValidCache( - $"Object in {ExtendedGUIUtility.BeautifyFormat(Property.displayName)} field is not part of {ExtendedGUIUtility.BeautifyFormat(target.name)} prefab"); + 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) diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/SceneReferenceWrapper.cs.meta b/Assets/BetterValidation/Editor/EditorAddons/Handlers/SceneReferenceHandler.cs.meta similarity index 100% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/SceneReferenceWrapper.cs.meta rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/SceneReferenceHandler.cs.meta diff --git a/Assets/BetterValidation/Editor/EditorAddons/Handlers/ValidationHandler.cs b/Assets/BetterValidation/Editor/EditorAddons/Handlers/ValidationHandler.cs new file mode 100644 index 0000000..d09ef20 --- /dev/null +++ b/Assets/BetterValidation/Editor/EditorAddons/Handlers/ValidationHandler.cs @@ -0,0 +1,28 @@ +using Better.Commons.EditorAddons.Drawers.Handlers; +using Better.Validation.Runtime.Attributes; + +namespace Better.Validation.EditorAddons.Handlers +{ + public abstract class ValidationHandler : SerializedPropertyHandler + { + internal static readonly ValidationValue ValidationValue = new ValidationValue(); + + public abstract ValidationType Type { get; } + + public abstract ValidationValue Validate(); + + public abstract bool IsSupported(); + + public static ValidationValue GetNotValidValue(string value) + { + ValidationValue.Set(false, value); + return ValidationValue; + } + + public static ValidationValue GetClearValue() + { + ValidationValue.Set(true, string.Empty); + return ValidationValue; + } + } +} \ No newline at end of file diff --git a/Assets/BetterValidation/Editor/EditorAddons/Wrappers/ValidationWrapper.cs.meta b/Assets/BetterValidation/Editor/EditorAddons/Handlers/ValidationHandler.cs.meta similarity index 100% rename from Assets/BetterValidation/Editor/EditorAddons/Wrappers/ValidationWrapper.cs.meta rename to Assets/BetterValidation/Editor/EditorAddons/Handlers/ValidationHandler.cs.meta diff --git a/Assets/BetterValidation/Editor/EditorAddons/Iteration/Iterator.cs b/Assets/BetterValidation/Editor/EditorAddons/Iteration/Iterator.cs index 5709b70..6dfeb92 100644 --- a/Assets/BetterValidation/Editor/EditorAddons/Iteration/Iterator.cs +++ b/Assets/BetterValidation/Editor/EditorAddons/Iteration/Iterator.cs @@ -1,8 +1,8 @@ using System.Collections.Generic; using Better.Commons.Runtime.Extensions; using Better.Validation.EditorAddons.ContextResolver; +using Better.Validation.EditorAddons.Handlers; using Better.Validation.EditorAddons.Utility; -using Better.Validation.EditorAddons.Wrappers; using UnityEditor; using UnityEngine; @@ -39,7 +39,7 @@ public static List ObjectIteration(Object reference, OnPr if (!obj) { CacheData.SetTarget(reference); - var missingReference = new ValidationCommandData(CacheData, new MissingComponentWrapper(gameObject)); + var missingReference = new ValidationCommandData(CacheData, new MissingComponentHandler(gameObject)); missingReference.SetResultCompiler((data, result) => $"Missing Component on GameObject: {_path.Resolve(data.Target)}"); missingReference.Revalidate(); commandData.Add(missingReference); diff --git a/Assets/BetterValidation/Editor/EditorAddons/Iteration/IteratorFilter.cs b/Assets/BetterValidation/Editor/EditorAddons/Iteration/IteratorFilter.cs index 8868ebf..a20ce7f 100644 --- a/Assets/BetterValidation/Editor/EditorAddons/Iteration/IteratorFilter.cs +++ b/Assets/BetterValidation/Editor/EditorAddons/Iteration/IteratorFilter.cs @@ -1,31 +1,27 @@ using System.Collections.Generic; +using System.Linq; using System.Text; -using Better.Commons.EditorAddons.Drawers.Base; -using Better.Commons.EditorAddons.Drawers.Caching; +using Better.Commons.EditorAddons.Drawers; +using Better.Commons.EditorAddons.Drawers.Handlers; using Better.Commons.EditorAddons.Extensions; using Better.Commons.Runtime.Extensions; -using Better.Validation.EditorAddons.Utility; -using Better.Validation.EditorAddons.Wrappers; +using Better.Validation.EditorAddons.Extensions; +using Better.Validation.EditorAddons.Handlers; using Better.Validation.Runtime.Attributes; namespace Better.Validation.EditorAddons.Iteration { public static class IteratorFilter { - private class LocalCache : CacheValue> - { - } + private static readonly TypeHandlerBinder Wrappers = HandlerBinderRegistry.GetMap(); - private static readonly LocalCache CacheField = new LocalCache(); - private static readonly WrapperCollection Wrappers = new WrapperCollection(); - - private static readonly MissingReferenceWrapper ReferenceWrapper = new MissingReferenceWrapper(); + private static readonly MissingReferenceHandler ReferenceHandler = new MissingReferenceHandler(); public static IEnumerable PropertyIterationWithAttributes(IterationData data) { var fieldInfo = data.Property.GetFieldInfoAndStaticTypeFromProperty(); var list = data.Property.GetAttributes(); - if (fieldInfo == null || list == null) return null; + if (fieldInfo == null || list == null) return Enumerable.Empty(); var fieldType = fieldInfo.FieldInfo.FieldType; if (fieldType.IsArrayOrList()) @@ -36,22 +32,17 @@ public static IEnumerable PropertyIterationWithAttributes var dataList = new List(); foreach (var validationAttribute in list) { - ValidateCachedPropertiesUtility.Validate(Wrappers, CacheField, data.Property, fieldType, validationAttribute.GetType(), ValidationAttributeUtility.Instance); + var handler = Wrappers.GetHandler(fieldType, validationAttribute.GetType()); - var fieldValue = CacheField.Value; - if (fieldValue == null) - { - continue; - } + if (handler is not PropertyValidationHandler propertyHandler) continue; - var wrapper = fieldValue.Wrapper; - wrapper.SetProperty(data.Property, validationAttribute); + propertyHandler.Setup(data.Property, fieldInfo.FieldInfo, validationAttribute); - var result = wrapper.IsSupported() ? wrapper.Validate() : ValidationWrapper.GetClearCache(); + var result = handler.IsSupported() ? handler.Validate() : ValidationHandler.GetClearValue(); - if (result.IsValid) continue; + if (result.State) continue; - dataList.Add(GenerateData(data, wrapper, result.Value)); + dataList.Add(GenerateData(data, propertyHandler, result.Result)); } return dataList; @@ -59,24 +50,27 @@ public static IEnumerable PropertyIterationWithAttributes public static IEnumerable MissingPropertyIteration(IterationData data) { - ReferenceWrapper.SetProperty(data.Property, null); - if (!ReferenceWrapper.IsSupported()) return null; - var result = ReferenceWrapper.Validate(); - if (result.IsValid) return null; - var validationCommandData = new ValidationCommandData(data, ReferenceWrapper); + var fieldInfo = data.Property.GetFieldInfoAndStaticTypeFromProperty(); + if (fieldInfo == null) return Enumerable.Empty(); + + ReferenceHandler.Setup(data.Property, fieldInfo.FieldInfo, null); + if (!ReferenceHandler.IsSupported()) return null; + var result = ReferenceHandler.Validate(); + if (result.State) return null; + var validationCommandData = new ValidationCommandData(data, ReferenceHandler); validationCommandData.SetResultCompiler(ObjectCompiler); - validationCommandData.SetInitialResult(ObjectCompiler(validationCommandData, result.Value)); + validationCommandData.SetInitialResult(ObjectCompiler(validationCommandData, result.Result)); return new[] { validationCommandData }; } - private static ValidationCommandData GenerateData(IterationData data, PropertyValidationWrapper wrapper, string resultValue) + private static ValidationCommandData GenerateData(IterationData data, PropertyValidationHandler handler, string resultValue) { - var copy = new ValidationCommandData(data, wrapper.Copy()); + var copy = new ValidationCommandData(data, handler.Copy()); copy.SetResultCompiler(ObjectCompiler); copy.SetInitialResult(ObjectCompiler(copy, resultValue)); return copy; @@ -89,7 +83,7 @@ private static string ObjectCompiler(ValidationCommandData commandData, string r var target = commandData.Target; var resolvedPath = commandData.PathResolver.Resolve(target); var typeName = target.GetType().Name; - + var stringBuilder = new StringBuilder(); stringBuilder.AppendFormat("Validation failed with: {0}.", result); stringBuilder.AppendLine(); diff --git a/Assets/BetterValidation/Editor/EditorAddons/MenuItems.cs b/Assets/BetterValidation/Editor/EditorAddons/MenuItems.cs index 11473c8..23c2fd9 100644 --- a/Assets/BetterValidation/Editor/EditorAddons/MenuItems.cs +++ b/Assets/BetterValidation/Editor/EditorAddons/MenuItems.cs @@ -6,7 +6,7 @@ namespace Better.Validation.EditorAddons { public static class MenuItems { - [MenuItem(ValidationSettingProvider.Path + "/Open Validation Window", false, 50)] + [MenuItem(ValidationSettingsProvider.Path + "/Open Validation Window", false, 50)] private static void ValidateInProject() { ValidationWindow.OpenWindow(); diff --git a/Assets/BetterValidation/Editor/EditorAddons/PreBuildValidation/ValidationBuildProcess.cs b/Assets/BetterValidation/Editor/EditorAddons/PreBuildValidation/ValidationBuildProcess.cs index 8c17d71..c1fc349 100644 --- a/Assets/BetterValidation/Editor/EditorAddons/PreBuildValidation/ValidationBuildProcess.cs +++ b/Assets/BetterValidation/Editor/EditorAddons/PreBuildValidation/ValidationBuildProcess.cs @@ -63,7 +63,7 @@ private static StringBuilder CreateDialogMessage(List com str.AppendLine("Do you want to ignore those issues?"); str.AppendLine(); str.AppendLine(); - str.AppendFormat("(You can disable validation in Edit > Project Settings > {0})", ValidationSettingProvider.Path.Replace("/", " > ")); + str.AppendFormat("(You can disable validation in Edit > Project Settings > {0})", ValidationSettingsProvider.Path.Replace("/", " > ")); return str; } } diff --git a/Assets/BetterValidation/Editor/EditorAddons/Settings/BuildValidationStepsDrawer.cs b/Assets/BetterValidation/Editor/EditorAddons/Settings/BuildValidationStepsDrawer.cs deleted file mode 100644 index 6590a2f..0000000 --- a/Assets/BetterValidation/Editor/EditorAddons/Settings/BuildValidationStepsDrawer.cs +++ /dev/null @@ -1,138 +0,0 @@ -using System; -using System.Linq; -using Better.Commons.EditorAddons.DropDown; -using Better.Commons.EditorAddons.Enums; -using Better.Commons.EditorAddons.Extensions; -using Better.Commons.EditorAddons.Helpers; -using Better.Commons.EditorAddons.Utility; -using Better.Commons.Runtime.Extensions; -using Better.Validation.EditorAddons.PreBuildValidation; -using UnityEditor; -using UnityEditorInternal; -using UnityEngine; - -namespace Better.Validation.EditorAddons.Settings -{ - internal class BuildValidationStepsDrawer - { - private readonly Type[] _types; - private readonly ReorderableList _reorderableList; - private readonly SerializedProperty _stepsProperty; - private readonly GUIContent _popupHeader = new GUIContent("Build Steps"); - private readonly GUIContent _validationStepsLabel = new GUIContent("Validation Steps"); - private readonly GUIContent _rootContent = new GUIContent("Root"); - private const string NullName = "null"; - - public BuildValidationStepsDrawer(SerializedObject settingsObject, SerializedProperty stepsProperty) - { - _types = typeof(IBuildValidationStep).GetAllInheritedTypesWithoutUnityObject().ToArray(); - _stepsProperty = stepsProperty; - - _reorderableList = new ReorderableList(settingsObject, _stepsProperty, true, true, true, true) - { - drawElementCallback = DrawElementCallback, - drawHeaderCallback = HeaderCallback, - elementHeightCallback = ElementHeightCallback - }; - } - - private float ElementHeightCallback(int index) - { - var property = _stepsProperty.GetArrayElementAtIndex(index); - return EditorGUI.GetPropertyHeight(property, true); - } - - private void HeaderCallback(Rect rect) - { - EditorGUI.LabelField(rect, _validationStepsLabel); - } - - private void DrawElementCallback(Rect rect, int index, bool isActive, bool isFocused) - { - var property = _stepsProperty.GetArrayElementAtIndex(index); - - var type = property.GetManagedType(); - DrawButton(rect, property, type); - var label = new GUIContent(L10n.Tr($"Element {index.ToString()}")); - EditorGUI.PropertyField(rect, property, label, true); - } - - private void DrawButton(Rect rect, SerializedProperty serializedProperty, Type currentValue) - { - var typeName = currentValue == null ? NullName : currentValue.Name; - var content = IconType.GrayDropdown.GetIconGUIContent(); - content.text = typeName; - var buttonPosition = GetPopupPosition(rect); - if (GUI.Button(buttonPosition, content, Styles.Button)) - { - ShowDropDown(buttonPosition, serializedProperty, currentValue); - } - } - - private void ShowDropDown(Rect popupPosition, SerializedProperty serializedProperty, Type currentValue) - { - var copy = popupPosition; - copy.y += EditorGUIUtility.singleLineHeight; - - var popup = DropdownWindow.ShowWindow(GUIUtility.GUIToScreenRect(copy), _popupHeader); - var items = GenerateItemsTree(serializedProperty, currentValue); - - popup.SetItems(items); - } - - private DropdownCollection GenerateItemsTree(SerializedProperty serializedProperty, Type currentValue) - { - var collection = new DropdownCollection(new DropdownSubTree(_rootContent)); - foreach (var type in _types) - { - var typeName = type == null ? NullName : type.Name; - var guiContent = new GUIContent(typeName); - if (guiContent.image == null && type == currentValue) - { - guiContent.image = IconType.Checkmark.GetIcon(); - } - - var item = new DropdownItem(guiContent, OnSelectItem, new Tuple(serializedProperty, type)); - collection.AddChild(item); - } - - return collection; - } - - private static void OnSelectItem(object obj) - { - if (obj is Tuple(var serializedProperty, var type)) - { - if (!serializedProperty.Verify()) return; - serializedProperty.managedReferenceValue = type == null ? null : Activator.CreateInstance(type); - - var serializedObject = serializedProperty.serializedObject; - EditorUtility.SetDirty(serializedObject.targetObject); - serializedObject.ApplyModifiedProperties(); - } - } - - private static Rect GetPopupPosition(Rect currentPosition) - { - var popupPosition = new Rect(currentPosition); - popupPosition.width -= EditorGUIUtility.labelWidth; - popupPosition.x += EditorGUIUtility.labelWidth; - popupPosition.height = EditorGUIUtility.singleLineHeight; - return popupPosition; - } - - public void DoLayoutList() - { - using (var scope = new EditorGUI.ChangeCheckScope()) - { - _reorderableList.DoLayoutList(); - if (scope.changed) - { - var serializedObject = _stepsProperty.serializedObject; - EditorUtility.SetDirty(serializedObject.targetObject); - serializedObject.ApplyModifiedProperties(); - } - } - } - } -} \ No newline at end of file diff --git a/Assets/BetterValidation/Editor/EditorAddons/Settings/SelectElementBehaviour.cs b/Assets/BetterValidation/Editor/EditorAddons/Settings/SelectElementBehaviour.cs new file mode 100644 index 0000000..927ff1a --- /dev/null +++ b/Assets/BetterValidation/Editor/EditorAddons/Settings/SelectElementBehaviour.cs @@ -0,0 +1,19 @@ +using Better.Commons.EditorAddons.Drawers.BehavioredElements; +using Better.Commons.EditorAddons.Enums; +using Better.Commons.EditorAddons.Extensions; +using Better.Commons.Runtime.Extensions; +using UnityEngine.UIElements; + +namespace Better.Validation.EditorAddons.Settings +{ + public class SelectElementBehaviour : DefaultElementBehaviour