@@ -18,28 +18,17 @@ public class CacheMethod : CacheObjectBase
18
18
private ParameterInfo [ ] m_arguments ;
19
19
private string [ ] m_argumentInput ;
20
20
21
- public bool HasParameters
22
- {
23
- get
24
- {
25
- if ( m_hasParams == null )
26
- {
27
- m_hasParams = ( MemInfo as MethodInfo ) . GetParameters ( ) . Length > 0 ;
28
- }
29
- return ( bool ) m_hasParams ;
30
- }
31
- }
32
- private bool ? m_hasParams ;
21
+ public bool HasParameters => m_arguments != null && m_arguments . Length > 0 ;
33
22
34
23
public static bool CanEvaluate ( MethodInfo mi )
35
24
{
36
- // generic type args not supported yet
25
+ // TODO generic args
37
26
if ( mi . GetGenericArguments ( ) . Length > 0 )
38
27
{
39
28
return false ;
40
29
}
41
30
42
- // only primitive and string args supported
31
+ // primitive and string args supported
43
32
foreach ( var param in mi . GetParameters ( ) )
44
33
{
45
34
if ( ! param . ParameterType . IsPrimitive && param . ParameterType != typeof ( string ) )
@@ -64,7 +53,84 @@ public override void Init()
64
53
public override void UpdateValue ( )
65
54
{
66
55
//base.UpdateValue();
67
- }
56
+ }
57
+
58
+ private void Evaluate ( )
59
+ {
60
+ var mi = MemInfo as MethodInfo ;
61
+
62
+ object ret = null ;
63
+
64
+ if ( ! HasParameters )
65
+ {
66
+ ret = mi . Invoke ( mi . IsStatic ? null : DeclaringInstance , new object [ 0 ] ) ;
67
+ m_evaluated = true ;
68
+ }
69
+ else
70
+ {
71
+ var parsedArgs = new List < object > ( ) ;
72
+ for ( int i = 0 ; i < m_arguments . Length ; i ++ )
73
+ {
74
+ var input = m_argumentInput [ i ] ;
75
+ var type = m_arguments [ i ] . ParameterType ;
76
+
77
+ if ( type == typeof ( string ) )
78
+ {
79
+ parsedArgs . Add ( input ) ;
80
+ }
81
+ else
82
+ {
83
+ try
84
+ {
85
+ if ( type . GetMethod ( "Parse" , new Type [ ] { typeof ( string ) } ) . Invoke ( null , new object [ ] { input } ) is object parsed )
86
+ {
87
+ parsedArgs . Add ( parsed ) ;
88
+ }
89
+ else
90
+ {
91
+ // try add a null arg i guess
92
+ parsedArgs . Add ( null ) ;
93
+ }
94
+
95
+ }
96
+ catch
97
+ {
98
+ MelonLogger . Log ( $ "Unable to parse '{ input } ' to type '{ type . Name } '") ;
99
+ break ;
100
+ }
101
+ }
102
+ }
103
+
104
+ try
105
+ {
106
+ ret = mi . Invoke ( mi . IsStatic ? null : DeclaringInstance , parsedArgs . ToArray ( ) ) ;
107
+ m_evaluated = true ;
108
+ }
109
+ catch ( Exception e )
110
+ {
111
+ MelonLogger . Log ( $ "Exception evaluating: { e . GetType ( ) } , { e . Message } ") ;
112
+ }
113
+ }
114
+
115
+ if ( ret != null )
116
+ {
117
+ m_cachedReturnValue = GetCacheObject ( ret ) ;
118
+
119
+ if ( m_cachedReturnValue is IExpandHeight expander )
120
+ {
121
+ expander . WhiteSpace = 0f ;
122
+ expander . ButtonWidthOffset += 70f ;
123
+ }
124
+
125
+ m_cachedReturnValue . UpdateValue ( ) ;
126
+ }
127
+ else
128
+ {
129
+ m_cachedReturnValue = null ;
130
+ }
131
+ }
132
+
133
+ // ==== GUI DRAW ====
68
134
69
135
public override void DrawValue ( Rect window , float width )
70
136
{
@@ -124,15 +190,7 @@ public override void DrawValue(Rect window, float width)
124
190
{
125
191
if ( m_cachedReturnValue != null )
126
192
{
127
- try
128
- {
129
- m_cachedReturnValue . DrawValue ( window , width ) ;
130
- }
131
- catch ( Exception e )
132
- {
133
- MelonLogger . Log ( "Exception drawing m_cachedReturnValue!" ) ;
134
- MelonLogger . Log ( e . ToString ( ) ) ;
135
- }
193
+ m_cachedReturnValue . DrawValue ( window , width ) ;
136
194
}
137
195
else
138
196
{
@@ -147,82 +205,5 @@ public override void DrawValue(Rect window, float width)
147
205
148
206
GUILayout . EndVertical ( ) ;
149
207
}
150
-
151
- private void Evaluate ( )
152
- {
153
- var mi = MemInfo as MethodInfo ;
154
-
155
- object ret = null ;
156
-
157
- if ( ! HasParameters )
158
- {
159
- ret = mi . Invoke ( mi . IsStatic ? null : DeclaringInstance , new object [ 0 ] ) ;
160
- m_evaluated = true ;
161
- }
162
- else
163
- {
164
- var arguments = new List < object > ( ) ;
165
- for ( int i = 0 ; i < m_arguments . Length ; i ++ )
166
- {
167
- var input = m_argumentInput [ i ] ;
168
- var type = m_arguments [ i ] . ParameterType ;
169
-
170
- if ( type == typeof ( string ) )
171
- {
172
- arguments . Add ( input ) ;
173
- }
174
- else
175
- {
176
- try
177
- {
178
- if ( type . GetMethod ( "Parse" , new Type [ ] { typeof ( string ) } ) . Invoke ( null , new object [ ] { input } ) is object parsed )
179
- {
180
- arguments . Add ( parsed ) ;
181
- }
182
- else
183
- {
184
- throw new Exception ( ) ;
185
- }
186
-
187
- }
188
- catch
189
- {
190
- MelonLogger . Log ( $ "Unable to parse '{ input } ' to type '{ type . Name } '") ;
191
- break ;
192
- }
193
- }
194
- }
195
-
196
- if ( arguments . Count == m_arguments . Length )
197
- {
198
- ret = mi . Invoke ( mi . IsStatic ? null : DeclaringInstance , arguments . ToArray ( ) ) ;
199
- m_evaluated = true ;
200
- }
201
- else
202
- {
203
- MelonLogger . Log ( $ "Did not invoke because { m_arguments . Length - arguments . Count } arguments could not be parsed!") ;
204
- }
205
- }
206
-
207
- if ( ret != null )
208
- {
209
- m_cachedReturnValue = GetCacheObject ( ret ) ;
210
- if ( m_cachedReturnValue is CacheList cacheList )
211
- {
212
- cacheList . WhiteSpace = 0f ;
213
- cacheList . ButtonWidthOffset += 70f ;
214
- }
215
- else if ( m_cachedReturnValue is CacheDictionary cacheDict )
216
- {
217
- cacheDict . WhiteSpace = 0f ;
218
- cacheDict . ButtonWidthOffset += 70f ;
219
- }
220
- m_cachedReturnValue . UpdateValue ( ) ;
221
- }
222
- else
223
- {
224
- m_cachedReturnValue = null ;
225
- }
226
- }
227
208
}
228
209
}
0 commit comments