Add ActionResolver

This commit is contained in:
VladV 2022-05-11 14:13:25 +03:00
parent bdc01494ee
commit fb8041857a
6 changed files with 131 additions and 0 deletions

View File

@ -0,0 +1,32 @@
using JetBrains.Annotations;
namespace TriInspector.Resolvers
{
public abstract class ActionResolver
{
public static ActionResolver Resolve(TriPropertyDefinition propertyDefinition, string method)
{
if (InstanceActionResolver.TryResolve(propertyDefinition, method, out var iar))
{
return iar;
}
return new ErrorActionResolver(propertyDefinition, method);
}
[PublicAPI]
public abstract bool TryGetErrorString(out string error);
[PublicAPI]
public abstract void InvokeForTarget(TriProperty property, int targetIndex);
[PublicAPI]
public void InvokeForAllTargets(TriProperty property)
{
for (var targetIndex = 0; targetIndex < property.PropertyTree.TargetObjects.Length; targetIndex++)
{
InvokeForTarget(property, targetIndex);
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 188152364708438db80df00df894ae87
timeCreated: 1652265564

View File

@ -0,0 +1,26 @@
using System;
using UnityEngine;
namespace TriInspector.Resolvers
{
internal sealed class ErrorActionResolver : ActionResolver
{
private readonly string _method;
public ErrorActionResolver(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 void InvokeForTarget(TriProperty property, int targetIndex)
{
Debug.LogException(new InvalidOperationException("Method not exists"));
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 52fddb0e965c4a8e82a2956d2f6ba53b
timeCreated: 1652265995

View File

@ -0,0 +1,64 @@
using System;
using System.Reflection;
using UnityEngine;
namespace TriInspector.Resolvers
{
internal sealed class InstanceActionResolver : ActionResolver
{
private readonly MethodInfo _methodInfo;
public static bool TryResolve(TriPropertyDefinition propertyDefinition, string method,
out ActionResolver 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(void) &&
methodInfo.GetParameters() is var parameterInfos &&
parameterInfos.Length == 0)
{
resolver = new InstanceActionResolver(methodInfo);
return true;
}
}
resolver = null;
return false;
}
private InstanceActionResolver(MethodInfo methodInfo)
{
_methodInfo = methodInfo;
}
public override bool TryGetErrorString(out string error)
{
error = "";
return false;
}
public override void InvokeForTarget(TriProperty property, int targetIndex)
{
var parentValue = property.Parent.GetValue(targetIndex);
try
{
_methodInfo.Invoke(parentValue, Array.Empty<object>());
}
catch (Exception e)
{
Debug.LogException(e);
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 737737f290e8433e9f160970cf996e4c
timeCreated: 1652266150