Improved interaction with generic methods and some minor UI fixes

This commit is contained in:
sinaioutlander 2020-09-18 23:10:46 +10:00
parent 643bb4519c
commit 129a7e3765
3 changed files with 74 additions and 68 deletions

View File

@ -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 = $"<color={UIStyles.Syntax.Class_Instance}>{type}</color>";
@ -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("<i>null (" + ValueTypeName + ")</i>", null);
GUILayout.Label("<i>null (<color=" + UIStyles.Syntax.Class_Instance + ">" + ValueTypeName + "</color>)</i>", null);
}
else
{

View File

@ -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<Type>();
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<Type>();
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)

View File

@ -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