From 7c1e995bdf80f7e709acccf88b9e8d88c42756c1 Mon Sep 17 00:00:00 2001 From: VladV Date: Mon, 9 May 2022 15:06:51 +0300 Subject: [PATCH] Add method resolvers --- Editor/Resolvers.meta | 3 + Editor/Resolvers/ErrorMethodResolver.cs | 25 +++++++++ Editor/Resolvers/ErrorMethodResolver.cs.meta | 3 + Editor/Resolvers/InstanceMethodResolver.cs | 56 +++++++++++++++++++ .../Resolvers/InstanceMethodResolver.cs.meta | 3 + Editor/Resolvers/MethodResolver.cs | 23 ++++++++ Editor/Resolvers/MethodResolver.cs.meta | 3 + 7 files changed, 116 insertions(+) create mode 100644 Editor/Resolvers.meta create mode 100644 Editor/Resolvers/ErrorMethodResolver.cs create mode 100644 Editor/Resolvers/ErrorMethodResolver.cs.meta create mode 100644 Editor/Resolvers/InstanceMethodResolver.cs create mode 100644 Editor/Resolvers/InstanceMethodResolver.cs.meta create mode 100644 Editor/Resolvers/MethodResolver.cs create mode 100644 Editor/Resolvers/MethodResolver.cs.meta diff --git a/Editor/Resolvers.meta b/Editor/Resolvers.meta new file mode 100644 index 0000000..9f05666 --- /dev/null +++ b/Editor/Resolvers.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ca8a2709dde94e318cb5cfdc801d9045 +timeCreated: 1652095622 \ No newline at end of file diff --git a/Editor/Resolvers/ErrorMethodResolver.cs b/Editor/Resolvers/ErrorMethodResolver.cs new file mode 100644 index 0000000..c2cb165 --- /dev/null +++ b/Editor/Resolvers/ErrorMethodResolver.cs @@ -0,0 +1,25 @@ +using System; + +namespace TriInspector.Resolvers +{ + internal class ErrorMethodResolver : MethodResolver + { + private readonly string _method; + + public ErrorMethodResolver(TriPropertyDefinition propertyDefinition, string method) + { + _method = method; + } + + public override bool TryGetErrorString(out string error) + { + error = $"Method '{_method}' not exists or has wrong signature"; + return true; + } + + public override TReturnType InvokeForTarget(TriProperty property, int targetIndex) + { + throw new InvalidOperationException("Method not exists"); + } + } +} \ No newline at end of file diff --git a/Editor/Resolvers/ErrorMethodResolver.cs.meta b/Editor/Resolvers/ErrorMethodResolver.cs.meta new file mode 100644 index 0000000..7f433fc --- /dev/null +++ b/Editor/Resolvers/ErrorMethodResolver.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b3b804d7029c42c0b5f5a692281dacbf +timeCreated: 1652096547 \ No newline at end of file diff --git a/Editor/Resolvers/InstanceMethodResolver.cs b/Editor/Resolvers/InstanceMethodResolver.cs new file mode 100644 index 0000000..1e61409 --- /dev/null +++ b/Editor/Resolvers/InstanceMethodResolver.cs @@ -0,0 +1,56 @@ +using System; +using System.Reflection; + +namespace TriInspector.Resolvers +{ + internal class InstanceMethodResolver : MethodResolver + { + private readonly MethodInfo _methodInfo; + + public static bool TryResolve(TriPropertyDefinition propertyDefinition, string method, + out MethodResolver resolver) + { + var parentType = propertyDefinition.MemberInfo.DeclaringType; + if (parentType == null) + { + resolver = null; + return false; + } + + const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; + + foreach (var methodInfo in parentType.GetMethods(flags)) + { + if (methodInfo.Name == method && + methodInfo.ReturnType == typeof(TReturnType) && + methodInfo.GetParameters() is var parameterInfos && + parameterInfos.Length == 0) + { + resolver = new InstanceMethodResolver(methodInfo); + return true; + } + } + + resolver = null; + return false; + } + + private InstanceMethodResolver(MethodInfo methodInfo) + { + _methodInfo = methodInfo; + } + + public override bool TryGetErrorString(out string error) + { + error = ""; + return false; + } + + public override TReturnType InvokeForTarget(TriProperty property, int targetIndex) + { + var parentValue = property.Parent.GetValue(targetIndex); + + return (TReturnType) _methodInfo.Invoke(parentValue, Array.Empty()); + } + } +} \ No newline at end of file diff --git a/Editor/Resolvers/InstanceMethodResolver.cs.meta b/Editor/Resolvers/InstanceMethodResolver.cs.meta new file mode 100644 index 0000000..4d6bbeb --- /dev/null +++ b/Editor/Resolvers/InstanceMethodResolver.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cee79123a0e84d5d9795ea92ed1b6d9e +timeCreated: 1652097174 \ No newline at end of file diff --git a/Editor/Resolvers/MethodResolver.cs b/Editor/Resolvers/MethodResolver.cs new file mode 100644 index 0000000..73dd225 --- /dev/null +++ b/Editor/Resolvers/MethodResolver.cs @@ -0,0 +1,23 @@ +namespace TriInspector.Resolvers +{ + public static class MethodResolver + { + public static MethodResolver Resolve(TriPropertyDefinition propertyDefinition, + string method) + { + if (InstanceMethodResolver.TryResolve(propertyDefinition, method, out var imr)) + { + return imr; + } + + return new ErrorMethodResolver(propertyDefinition, method); + } + } + + public abstract class MethodResolver + { + public abstract bool TryGetErrorString(out string error); + + public abstract TReturnType InvokeForTarget(TriProperty property, int targetIndex); + } +} \ No newline at end of file diff --git a/Editor/Resolvers/MethodResolver.cs.meta b/Editor/Resolvers/MethodResolver.cs.meta new file mode 100644 index 0000000..bfaafa4 --- /dev/null +++ b/Editor/Resolvers/MethodResolver.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 467299addfb7498fbb7e7f0e449c4364 +timeCreated: 1652095635 \ No newline at end of file