mirror of
https://github.com/codewriter-packages/Tri-Inspector.git
synced 2025-01-22 00:08:51 -05:00
Add ActionResolver
This commit is contained in:
parent
bdc01494ee
commit
fb8041857a
32
Editor/Resolvers/ActionResolver.cs
Normal file
32
Editor/Resolvers/ActionResolver.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Editor/Resolvers/ActionResolver.cs.meta
Normal file
3
Editor/Resolvers/ActionResolver.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 188152364708438db80df00df894ae87
|
||||
timeCreated: 1652265564
|
26
Editor/Resolvers/ErrorActionResolver.cs
Normal file
26
Editor/Resolvers/ErrorActionResolver.cs
Normal 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"));
|
||||
}
|
||||
}
|
||||
}
|
3
Editor/Resolvers/ErrorActionResolver.cs.meta
Normal file
3
Editor/Resolvers/ErrorActionResolver.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 52fddb0e965c4a8e82a2956d2f6ba53b
|
||||
timeCreated: 1652265995
|
64
Editor/Resolvers/InstanceActionResolver.cs
Normal file
64
Editor/Resolvers/InstanceActionResolver.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Editor/Resolvers/InstanceActionResolver.cs.meta
Normal file
3
Editor/Resolvers/InstanceActionResolver.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 737737f290e8433e9f160970cf996e4c
|
||||
timeCreated: 1652266150
|
Loading…
Reference in New Issue
Block a user