@@ -20,7 +20,10 @@ public abstract class CacheObject
2020 public ReflectionWindow . MemberInfoType MemberInfoType { get ; set ; }
2121 public Type DeclaringType { get ; set ; }
2222 public object DeclaringInstance { get ; set ; }
23+ public string FullName => $ "{ MemberInfo . DeclaringType . Name } .{ MemberInfo . Name } ";
24+ public string ReflectionException ;
2325
26+ // methods
2427 public abstract void DrawValue ( Rect window , float width ) ;
2528 public abstract void SetValue ( ) ;
2629
@@ -29,73 +32,63 @@ public static CacheObject GetCacheObject(object obj)
2932 return GetCacheObject ( obj , null , null ) ;
3033 }
3134
35+ /// <summary>
36+ /// Gets the CacheObject subclass for an object or MemberInfo
37+ /// </summary>
38+ /// <param name="obj">The current value (can be null if memberInfo is not null)</param>
39+ /// <param name="memberInfo">The MemberInfo (can be null if obj is not null)</param>
40+ /// <param name="declaringInstance">If MemberInfo is not null, the declaring class instance. Can be null if static.</param>
41+ /// <returns></returns>
3242 public static CacheObject GetCacheObject ( object obj , MemberInfo memberInfo , object declaringInstance )
3343 {
3444 CacheObject holder ;
3545
3646 var type = ReflectionHelpers . GetActualType ( obj ) ?? ( memberInfo as FieldInfo ) ? . FieldType ?? ( memberInfo as PropertyInfo ) ? . PropertyType ;
3747
38- if ( obj is Il2CppSystem . Object || typeof ( Il2CppSystem . Object ) . IsAssignableFrom ( type ) )
48+ if ( ( obj is Il2CppSystem . Object || typeof ( Il2CppSystem . Object ) . IsAssignableFrom ( type ) )
49+ && ( type . FullName . Contains ( "UnityEngine.GameObject" ) || type . FullName . Contains ( "UnityEngine.Transform" ) ) )
3950 {
40- var name = type . FullName ;
41- if ( name == "UnityEngine.GameObject" || name == "UnityEngine.Transform" )
42- {
43- holder = new CacheGameObject ( obj ) ;
44- }
45- else
46- {
47- holder = new CacheIl2CppObject ( ) ;
48- }
51+ holder = new CacheGameObject ( obj ) ;
4952 }
50- else
53+ else if ( type . IsPrimitive || type == typeof ( string ) )
5154 {
52- if ( type . IsPrimitive || type == typeof ( string ) )
53- {
54- holder = new CachePrimitive ( obj ) ;
55- }
56- else if ( type . IsEnum )
57- {
58- holder = new CacheEnum ( obj ) ;
59- }
60- else if ( typeof ( System . Collections . IEnumerable ) . IsAssignableFrom ( type ) || ReflectionHelpers . IsList ( type ) )
61- {
62- holder = new CacheList ( obj ) ;
63- }
64- else if ( type . IsValueType )
65- {
66- holder = new CacheStruct ( obj ) ;
67- }
68- else
69- {
70- holder = new CacheOther ( ) ;
71- }
55+ holder = new CachePrimitive ( obj ) ;
7256 }
73-
74- if ( holder == null )
57+ else if ( type . IsEnum )
7558 {
76- return null ;
59+ holder = new CacheEnum ( obj ) ;
60+ }
61+ else if ( typeof ( System . Collections . IEnumerable ) . IsAssignableFrom ( type ) || ReflectionHelpers . IsList ( type ) )
62+ {
63+ holder = new CacheList ( obj ) ;
64+ }
65+ else
66+ {
67+ holder = new CacheOther ( ) ;
7768 }
7869
7970 if ( memberInfo != null )
8071 {
8172 holder . MemberInfo = memberInfo ;
8273 holder . DeclaringType = memberInfo . DeclaringType ;
83-
84- if ( declaringInstance is Il2CppSystem . Object ilInstance && ilInstance . GetType ( ) != memberInfo . DeclaringType )
85- {
86- try
87- {
88- holder . DeclaringInstance = ilInstance . Il2CppCast ( holder . DeclaringType ) ;
89- }
90- catch
91- {
92- holder . DeclaringInstance = declaringInstance ;
93- }
94- }
95- else
96- {
97- holder . DeclaringInstance = declaringInstance ;
98- }
74+ holder . DeclaringInstance = declaringInstance ;
75+
76+ //if (declaringInstance is Il2CppSystem.Object ilInstance && ilInstance.GetType() != memberInfo.DeclaringType)
77+ //{
78+ // try
79+ // {
80+ // holder.DeclaringInstance = ilInstance.Il2CppCast(holder.DeclaringType);
81+ // }
82+ // catch (Exception e)
83+ // {
84+ // holder.ReflectionException = ReflectionHelpers.ExceptionToString(e);
85+ // holder.DeclaringInstance = declaringInstance;
86+ // }
87+ //}
88+ //else
89+ //{
90+ // holder.DeclaringInstance = declaringInstance;
91+ //}
9992
10093 if ( memberInfo . MemberType == MemberTypes . Field )
10194 {
@@ -121,14 +114,18 @@ public void Draw(Rect window, float labelWidth = 180f)
121114 {
122115 if ( MemberInfo != null )
123116 {
124- GUILayout . Label ( "<color=cyan>" + MemberInfo . Name + ":</color>" , new GUILayoutOption [ ] { GUILayout . Width ( labelWidth ) } ) ;
117+ GUILayout . Label ( "<color=cyan>" + FullName + ":</color>" , new GUILayoutOption [ ] { GUILayout . Width ( labelWidth ) } ) ;
125118 }
126119 else
127120 {
128121 GUILayout . Space ( labelWidth ) ;
129122 }
130123
131- if ( Value == null )
124+ if ( ! string . IsNullOrEmpty ( ReflectionException ) )
125+ {
126+ GUILayout . Label ( "<color=red>Reflection failed!</color> (" + ReflectionException + ")" , null ) ;
127+ }
128+ else if ( Value == null )
132129 {
133130 GUILayout . Label ( "<i>null (" + this . ValueType + ")</i>" , null ) ;
134131 }
@@ -138,9 +135,9 @@ public void Draw(Rect window, float labelWidth = 180f)
138135 }
139136 }
140137
141- public virtual void UpdateValue ( object obj )
138+ public virtual void UpdateValue ( )
142139 {
143- if ( MemberInfo == null )
140+ if ( MemberInfo == null || ! string . IsNullOrEmpty ( ReflectionException ) )
144141 {
145142 return ;
146143 }
@@ -155,12 +152,15 @@ public virtual void UpdateValue(object obj)
155152 else if ( MemberInfo . MemberType == MemberTypes . Property )
156153 {
157154 var pi = MemberInfo as PropertyInfo ;
158- Value = pi . GetValue ( pi . GetAccessors ( ) [ 0 ] . IsStatic ? null : DeclaringInstance , null ) ;
155+ bool isStatic = pi . GetAccessors ( ) [ 0 ] . IsStatic ;
156+ var target = isStatic ? null : DeclaringInstance ;
157+ Value = pi . GetValue ( target , null ) ;
159158 }
159+ //ReflectionException = null;
160160 }
161- catch // (Exception e)
161+ catch ( Exception e )
162162 {
163- //MelonLogger.Log($"Error updating MemberInfo value | {e.GetType()}: {e.Message}\r\n{e.StackTrace}" );
163+ ReflectionException = ReflectionHelpers . ExceptionToString ( e ) ;
164164 }
165165 }
166166
0 commit comments