From 86fed7a83099b9d8f5b9f82b5c146873023a6e5c Mon Sep 17 00:00:00 2001 From: Govorunb Date: Wed, 8 Nov 2023 17:53:02 +1100 Subject: [PATCH] Allow short member names for static resolvers (#145) --- Editor/Resolvers/StaticFieldValueResolver.cs | 24 ++++++++++++------- Editor/Resolvers/StaticMethodValueResolver.cs | 22 ++++++++++------- .../Resolvers/StaticPropertyValueResolver.cs | 24 ++++++++++++------- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/Editor/Resolvers/StaticFieldValueResolver.cs b/Editor/Resolvers/StaticFieldValueResolver.cs index c937a17..f6c84fb 100644 --- a/Editor/Resolvers/StaticFieldValueResolver.cs +++ b/Editor/Resolvers/StaticFieldValueResolver.cs @@ -12,17 +12,23 @@ namespace TriInspector.Resolvers public static bool TryResolve(TriPropertyDefinition propertyDefinition, string expression, out ValueResolver resolver) { - if (expression.IndexOf('.') == -1) - { - resolver = null; - return false; - } + var type = propertyDefinition.OwnerType; + var fieldName = expression; var separatorIndex = expression.LastIndexOf('.'); - var className = expression.Substring(0, separatorIndex); - var methodName = expression.Substring(separatorIndex + 1); + if (separatorIndex >= 0) + { + var className = expression.Substring(0, separatorIndex); + fieldName = expression.Substring(separatorIndex + 1); - if (!TriReflectionUtilities.TryFindTypeByFullName(className, out var type)) + if (!TriReflectionUtilities.TryFindTypeByFullName(className, out type)) + { + resolver = null; + return false; + } + } + + if (type == null) { resolver = null; return false; @@ -32,7 +38,7 @@ namespace TriInspector.Resolvers foreach (var fieldInfo in type.GetFields(flags)) { - if (fieldInfo.Name == methodName && + if (fieldInfo.Name == fieldName && typeof(T).IsAssignableFrom(fieldInfo.FieldType)) { resolver = new StaticFieldValueResolver(fieldInfo); diff --git a/Editor/Resolvers/StaticMethodValueResolver.cs b/Editor/Resolvers/StaticMethodValueResolver.cs index 33e3a6c..da87b0c 100644 --- a/Editor/Resolvers/StaticMethodValueResolver.cs +++ b/Editor/Resolvers/StaticMethodValueResolver.cs @@ -12,17 +12,23 @@ namespace TriInspector.Resolvers public static bool TryResolve(TriPropertyDefinition propertyDefinition, string expression, out ValueResolver resolver) { - if (expression.IndexOf('.') == -1) + var type = propertyDefinition.OwnerType; + var methodName = expression; + + var separatorIndex = expression.LastIndexOf('.'); + if (separatorIndex >= 0) { - resolver = null; - return false; + var className = expression.Substring(0, separatorIndex); + methodName = expression.Substring(separatorIndex + 1); + + if (!TriReflectionUtilities.TryFindTypeByFullName(className, out type)) + { + resolver = null; + return false; + } } - var separatorIndex = expression.LastIndexOf('.'); - var className = expression.Substring(0, separatorIndex); - var methodName = expression.Substring(separatorIndex + 1); - - if (!TriReflectionUtilities.TryFindTypeByFullName(className, out var type)) + if (type == null) { resolver = null; return false; diff --git a/Editor/Resolvers/StaticPropertyValueResolver.cs b/Editor/Resolvers/StaticPropertyValueResolver.cs index bf130ef..3a3785e 100644 --- a/Editor/Resolvers/StaticPropertyValueResolver.cs +++ b/Editor/Resolvers/StaticPropertyValueResolver.cs @@ -12,17 +12,23 @@ namespace TriInspector.Resolvers public static bool TryResolve(TriPropertyDefinition propertyDefinition, string expression, out ValueResolver resolver) { - if (expression.IndexOf('.') == -1) + var type = propertyDefinition.OwnerType; + var propertyName = expression; + + var separatorIndex = expression.LastIndexOf('.'); + if (separatorIndex >= 0) { - resolver = null; - return false; + var className = expression.Substring(0, separatorIndex); + propertyName = expression.Substring(separatorIndex + 1); + + if (!TriReflectionUtilities.TryFindTypeByFullName(className, out type)) + { + resolver = null; + return false; + } } - var separatorIndex = expression.LastIndexOf('.'); - var className = expression.Substring(0, separatorIndex); - var methodName = expression.Substring(separatorIndex + 1); - - if (!TriReflectionUtilities.TryFindTypeByFullName(className, out var type)) + if (type == null) { resolver = null; return false; @@ -32,7 +38,7 @@ namespace TriInspector.Resolvers foreach (var propertyInfo in type.GetProperties(flags)) { - if (propertyInfo.Name == methodName && + if (propertyInfo.Name == propertyName && typeof(T).IsAssignableFrom(propertyInfo.PropertyType) && propertyInfo.CanRead) {