Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 129a7e3

Browse files
committed
Improved interaction with generic methods and some minor UI fixes
1 parent 643bb45 commit 129a7e3

File tree

3 files changed

+71
-65
lines changed

3 files changed

+71
-65
lines changed

src/CachedObjects/CacheObjectBase.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,7 @@ public virtual void UpdateValue()
305305
var pi = MemInfo as PropertyInfo;
306306
var target = pi.GetAccessors()[0].IsStatic ? null : DeclaringInstance;
307307

308-
if (HasParameters)
309-
{
310-
Value = pi.GetValue(target, ParseArguments());
311-
}
312-
else
313-
{
314-
Value = pi.GetValue(target, null);
315-
}
308+
Value = pi.GetValue(target, ParseArguments());
316309
}
317310

318311
ReflectionException = null;
@@ -397,7 +390,7 @@ public void Draw(Rect window, float labelWidth = 215f)
397390

398391
for (int i = 0; i < cm.GenericArgs.Length; i++)
399392
{
400-
var type = cm.GenericConstraints[i]?.FullName ?? "None";
393+
var type = cm.GenericConstraints[i]?.FullName ?? "Any";
401394
var input = cm.GenericArgInput[i];
402395
var label = $"<color={UIStyles.Syntax.Class_Instance}>{type}</color>";
403396

@@ -462,9 +455,9 @@ public void Draw(Rect window, float labelWidth = 215f)
462455
else
463456
{
464457
var lbl = $"Evaluate (";
465-
int args = m_arguments.Length;
466-
if (cm != null) args += cm.GenericArgs.Length;
467-
lbl += args + " params)";
458+
int len = m_arguments.Length;
459+
if (cm != null) len += cm.GenericArgs.Length;
460+
lbl += len + " params)";
468461

469462
if (GUILayout.Button(lbl, new GUILayoutOption[] { GUILayout.Width(150) }))
470463
{
@@ -504,7 +497,7 @@ public void Draw(Rect window, float labelWidth = 215f)
504497
}
505498
else if (Value == null && !(this is CacheMethod))
506499
{
507-
GUILayout.Label("<i>null (" + ValueTypeName + ")</i>", null);
500+
GUILayout.Label("<i>null (<color=" + UIStyles.Syntax.Class_Instance + ">" + ValueTypeName + "</color>)</i>", null);
508501
}
509502
else
510503
{

src/CachedObjects/Other/CacheMethod.cs

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -46,66 +46,29 @@ public override void UpdateValue()
4646

4747
public void Evaluate()
4848
{
49-
m_isEvaluating = false;
50-
51-
var mi = MemInfo as MethodInfo;
52-
object ret = null;
53-
54-
// Parse generic arguments
49+
MethodInfo mi;
5550
if (GenericArgs.Length > 0)
5651
{
57-
var list = new List<Type>();
58-
for (int i = 0; i < GenericArgs.Length; i++)
59-
{
60-
var input = GenericArgInput[i];
61-
if (ReflectionHelpers.GetTypeByName(input) is Type t)
62-
{
63-
if (GenericConstraints[i] == null)
64-
{
65-
list.Add(t);
66-
}
67-
else
68-
{
69-
if (GenericConstraints[i].IsAssignableFrom(t))
70-
{
71-
list.Add(t);
72-
}
73-
else
74-
{
75-
MelonLogger.Log($"Generic argument #{i} '{input}', is not assignable from the generic constraint!");
76-
return;
77-
}
78-
}
79-
}
80-
else
81-
{
82-
MelonLogger.Log($"Generic argument #{i}, could not get any type by the name of '{input}'!" +
83-
$" Make sure you use the full name, including the NameSpace.");
84-
return;
85-
}
86-
}
87-
88-
// make into a generic with type list
89-
mi = mi.MakeGenericMethod(list.ToArray());
52+
mi = MakeGenericMethodFromInput();
53+
if (mi == null) return;
54+
}
55+
else
56+
{
57+
mi = MemInfo as MethodInfo;
9058
}
9159

92-
// Parse arguments
93-
if (!HasParameters)
60+
object ret = null;
61+
62+
try
9463
{
95-
ret = mi.Invoke(mi.IsStatic ? null : DeclaringInstance, new object[0]);
64+
ret = mi.Invoke(mi.IsStatic ? null : DeclaringInstance, ParseArguments());
9665
m_evaluated = true;
66+
m_isEvaluating = false;
9767
}
98-
else
68+
catch (Exception e)
9969
{
100-
try
101-
{
102-
ret = mi.Invoke(mi.IsStatic ? null : DeclaringInstance, ParseArguments());
103-
m_evaluated = true;
104-
}
105-
catch (Exception e)
106-
{
107-
MelonLogger.Log($"Exception evaluating: {e.GetType()}, {e.Message}");
108-
}
70+
MelonLogger.LogWarning($"Exception evaluating: {e.GetType()}, {e.Message}");
71+
ReflectionException = ReflectionHelpers.ExceptionToString(e);
10972
}
11073

11174
if (ret != null)
@@ -119,6 +82,47 @@ public void Evaluate()
11982
}
12083
}
12184

85+
private MethodInfo MakeGenericMethodFromInput()
86+
{
87+
var mi = MemInfo as MethodInfo;
88+
89+
var list = new List<Type>();
90+
for (int i = 0; i < GenericArgs.Length; i++)
91+
{
92+
var input = GenericArgInput[i];
93+
if (ReflectionHelpers.GetTypeByName(input) is Type t)
94+
{
95+
if (GenericConstraints[i] == null)
96+
{
97+
list.Add(t);
98+
}
99+
else
100+
{
101+
if (GenericConstraints[i].IsAssignableFrom(t))
102+
{
103+
list.Add(t);
104+
}
105+
else
106+
{
107+
MelonLogger.LogWarning($"Generic argument #{i} '{input}', is not assignable from the generic constraint!");
108+
return null;
109+
}
110+
}
111+
}
112+
else
113+
{
114+
MelonLogger.LogWarning($"Generic argument #{i}, could not get any type by the name of '{input}'!" +
115+
$" Make sure you use the full name, including the NameSpace.");
116+
return null;
117+
}
118+
}
119+
120+
// make into a generic with type list
121+
mi = mi.MakeGenericMethod(list.ToArray());
122+
123+
return mi;
124+
}
125+
122126
// ==== GUI DRAW ====
123127

124128
public override void DrawValue(Rect window, float width)

src/CachedObjects/Other/CacheOther.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ public class CacheOther : CacheObjectBase
1212
public MethodInfo ToStringMethod => m_toStringMethod ?? GetToStringMethod();
1313
private MethodInfo m_toStringMethod;
1414

15+
public override void UpdateValue()
16+
{
17+
base.UpdateValue();
18+
19+
GetButtonLabel();
20+
}
21+
1522
public override void DrawValue(Rect window, float width)
1623
{
1724
GUI.skin.button.alignment = TextAnchor.MiddleLeft;
@@ -41,6 +48,8 @@ private MethodInfo GetToStringMethod()
4148

4249
private string GetButtonLabel()
4350
{
51+
if (Value == null) return null;
52+
4453
string label = (string)ToStringMethod?.Invoke(Value, null) ?? Value.ToString();
4554

4655
var classColor = ValueType.IsAbstract && ValueType.IsSealed

0 commit comments

Comments
 (0)