@@ -18,28 +18,17 @@ public class CacheMethod : CacheObjectBase
1818 private ParameterInfo [ ] m_arguments ;
1919 private string [ ] m_argumentInput ;
2020
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 ;
3322
3423 public static bool CanEvaluate ( MethodInfo mi )
3524 {
36- // generic type args not supported yet
25+ // TODO generic args
3726 if ( mi . GetGenericArguments ( ) . Length > 0 )
3827 {
3928 return false ;
4029 }
4130
42- // only primitive and string args supported
31+ // primitive and string args supported
4332 foreach ( var param in mi . GetParameters ( ) )
4433 {
4534 if ( ! param . ParameterType . IsPrimitive && param . ParameterType != typeof ( string ) )
@@ -64,7 +53,84 @@ public override void Init()
6453 public override void UpdateValue ( )
6554 {
6655 //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 ====
68134
69135 public override void DrawValue ( Rect window , float width )
70136 {
@@ -124,15 +190,7 @@ public override void DrawValue(Rect window, float width)
124190 {
125191 if ( m_cachedReturnValue != null )
126192 {
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 ) ;
136194 }
137195 else
138196 {
@@ -147,82 +205,5 @@ public override void DrawValue(Rect window, float width)
147205
148206 GUILayout . EndVertical ( ) ;
149207 }
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- }
227208 }
228209}
0 commit comments