From 129a7e3765b326d0ba2b639068e1dcdb6daf8047 Mon Sep 17 00:00:00 2001
From: sinaioutlander <49360850+sinaioutlander@users.noreply.github.com>
Date: Fri, 18 Sep 2020 23:10:46 +1000
Subject: [PATCH] Improved interaction with generic methods and some minor UI
fixes
---
src/CachedObjects/CacheObjectBase.cs | 19 ++---
src/CachedObjects/Other/CacheMethod.cs | 114 +++++++++++++------------
src/CachedObjects/Other/CacheOther.cs | 9 ++
3 files changed, 74 insertions(+), 68 deletions(-)
diff --git a/src/CachedObjects/CacheObjectBase.cs b/src/CachedObjects/CacheObjectBase.cs
index 51901c4..b39f72c 100644
--- a/src/CachedObjects/CacheObjectBase.cs
+++ b/src/CachedObjects/CacheObjectBase.cs
@@ -305,14 +305,7 @@ namespace Explorer
var pi = MemInfo as PropertyInfo;
var target = pi.GetAccessors()[0].IsStatic ? null : DeclaringInstance;
- if (HasParameters)
- {
- Value = pi.GetValue(target, ParseArguments());
- }
- else
- {
- Value = pi.GetValue(target, null);
- }
+ Value = pi.GetValue(target, ParseArguments());
}
ReflectionException = null;
@@ -397,7 +390,7 @@ namespace Explorer
for (int i = 0; i < cm.GenericArgs.Length; i++)
{
- var type = cm.GenericConstraints[i]?.FullName ?? "None";
+ var type = cm.GenericConstraints[i]?.FullName ?? "Any";
var input = cm.GenericArgInput[i];
var label = $"{type}";
@@ -462,9 +455,9 @@ namespace Explorer
else
{
var lbl = $"Evaluate (";
- int args = m_arguments.Length;
- if (cm != null) args += cm.GenericArgs.Length;
- lbl += args + " params)";
+ int len = m_arguments.Length;
+ if (cm != null) len += cm.GenericArgs.Length;
+ lbl += len + " params)";
if (GUILayout.Button(lbl, new GUILayoutOption[] { GUILayout.Width(150) }))
{
@@ -504,7 +497,7 @@ namespace Explorer
}
else if (Value == null && !(this is CacheMethod))
{
- GUILayout.Label("null (" + ValueTypeName + ")", null);
+ GUILayout.Label("null (" + ValueTypeName + ")", null);
}
else
{
diff --git a/src/CachedObjects/Other/CacheMethod.cs b/src/CachedObjects/Other/CacheMethod.cs
index b1c2086..9a961eb 100644
--- a/src/CachedObjects/Other/CacheMethod.cs
+++ b/src/CachedObjects/Other/CacheMethod.cs
@@ -46,66 +46,29 @@ namespace Explorer
public void Evaluate()
{
- m_isEvaluating = false;
-
- var mi = MemInfo as MethodInfo;
- object ret = null;
-
- // Parse generic arguments
+ MethodInfo mi;
if (GenericArgs.Length > 0)
{
- var list = new List();
- for (int i = 0; i < GenericArgs.Length; i++)
- {
- var input = GenericArgInput[i];
- if (ReflectionHelpers.GetTypeByName(input) is Type t)
- {
- if (GenericConstraints[i] == null)
- {
- list.Add(t);
- }
- else
- {
- if (GenericConstraints[i].IsAssignableFrom(t))
- {
- list.Add(t);
- }
- else
- {
- MelonLogger.Log($"Generic argument #{i} '{input}', is not assignable from the generic constraint!");
- return;
- }
- }
- }
- else
- {
- MelonLogger.Log($"Generic argument #{i}, could not get any type by the name of '{input}'!" +
- $" Make sure you use the full name, including the NameSpace.");
- return;
- }
- }
-
- // make into a generic with type list
- mi = mi.MakeGenericMethod(list.ToArray());
- }
-
- // Parse arguments
- if (!HasParameters)
- {
- ret = mi.Invoke(mi.IsStatic ? null : DeclaringInstance, new object[0]);
- m_evaluated = true;
+ mi = MakeGenericMethodFromInput();
+ if (mi == null) return;
}
else
{
- try
- {
- ret = mi.Invoke(mi.IsStatic ? null : DeclaringInstance, ParseArguments());
- m_evaluated = true;
- }
- catch (Exception e)
- {
- MelonLogger.Log($"Exception evaluating: {e.GetType()}, {e.Message}");
- }
+ mi = MemInfo as MethodInfo;
+ }
+
+ object ret = null;
+
+ try
+ {
+ ret = mi.Invoke(mi.IsStatic ? null : DeclaringInstance, ParseArguments());
+ m_evaluated = true;
+ m_isEvaluating = false;
+ }
+ catch (Exception e)
+ {
+ MelonLogger.LogWarning($"Exception evaluating: {e.GetType()}, {e.Message}");
+ ReflectionException = ReflectionHelpers.ExceptionToString(e);
}
if (ret != null)
@@ -119,6 +82,47 @@ namespace Explorer
}
}
+ private MethodInfo MakeGenericMethodFromInput()
+ {
+ var mi = MemInfo as MethodInfo;
+
+ var list = new List();
+ for (int i = 0; i < GenericArgs.Length; i++)
+ {
+ var input = GenericArgInput[i];
+ if (ReflectionHelpers.GetTypeByName(input) is Type t)
+ {
+ if (GenericConstraints[i] == null)
+ {
+ list.Add(t);
+ }
+ else
+ {
+ if (GenericConstraints[i].IsAssignableFrom(t))
+ {
+ list.Add(t);
+ }
+ else
+ {
+ MelonLogger.LogWarning($"Generic argument #{i} '{input}', is not assignable from the generic constraint!");
+ return null;
+ }
+ }
+ }
+ else
+ {
+ MelonLogger.LogWarning($"Generic argument #{i}, could not get any type by the name of '{input}'!" +
+ $" Make sure you use the full name, including the NameSpace.");
+ return null;
+ }
+ }
+
+ // make into a generic with type list
+ mi = mi.MakeGenericMethod(list.ToArray());
+
+ return mi;
+ }
+
// ==== GUI DRAW ====
public override void DrawValue(Rect window, float width)
diff --git a/src/CachedObjects/Other/CacheOther.cs b/src/CachedObjects/Other/CacheOther.cs
index 284bb0a..983aa76 100644
--- a/src/CachedObjects/Other/CacheOther.cs
+++ b/src/CachedObjects/Other/CacheOther.cs
@@ -12,6 +12,13 @@ namespace Explorer
public MethodInfo ToStringMethod => m_toStringMethod ?? GetToStringMethod();
private MethodInfo m_toStringMethod;
+ public override void UpdateValue()
+ {
+ base.UpdateValue();
+
+ GetButtonLabel();
+ }
+
public override void DrawValue(Rect window, float width)
{
GUI.skin.button.alignment = TextAnchor.MiddleLeft;
@@ -41,6 +48,8 @@ namespace Explorer
private string GetButtonLabel()
{
+ if (Value == null) return null;
+
string label = (string)ToStringMethod?.Invoke(Value, null) ?? Value.ToString();
var classColor = ValueType.IsAbstract && ValueType.IsSealed