@@ -20,7 +20,10 @@ public abstract class CacheObject
20
20
public ReflectionWindow . MemberInfoType MemberInfoType { get ; set ; }
21
21
public Type DeclaringType { get ; set ; }
22
22
public object DeclaringInstance { get ; set ; }
23
+ public string FullName => $ "{ MemberInfo . DeclaringType . Name } .{ MemberInfo . Name } ";
24
+ public string ReflectionException ;
23
25
26
+ // methods
24
27
public abstract void DrawValue ( Rect window , float width ) ;
25
28
public abstract void SetValue ( ) ;
26
29
@@ -29,73 +32,63 @@ public static CacheObject GetCacheObject(object obj)
29
32
return GetCacheObject ( obj , null , null ) ;
30
33
}
31
34
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>
32
42
public static CacheObject GetCacheObject ( object obj , MemberInfo memberInfo , object declaringInstance )
33
43
{
34
44
CacheObject holder ;
35
45
36
46
var type = ReflectionHelpers . GetActualType ( obj ) ?? ( memberInfo as FieldInfo ) ? . FieldType ?? ( memberInfo as PropertyInfo ) ? . PropertyType ;
37
47
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" ) ) )
39
50
{
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 ) ;
49
52
}
50
- else
53
+ else if ( type . IsPrimitive || type == typeof ( string ) )
51
54
{
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 ) ;
72
56
}
73
-
74
- if ( holder == null )
57
+ else if ( type . IsEnum )
75
58
{
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 ( ) ;
77
68
}
78
69
79
70
if ( memberInfo != null )
80
71
{
81
72
holder . MemberInfo = memberInfo ;
82
73
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
+ //}
99
92
100
93
if ( memberInfo . MemberType == MemberTypes . Field )
101
94
{
@@ -121,14 +114,18 @@ public void Draw(Rect window, float labelWidth = 180f)
121
114
{
122
115
if ( MemberInfo != null )
123
116
{
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 ) } ) ;
125
118
}
126
119
else
127
120
{
128
121
GUILayout . Space ( labelWidth ) ;
129
122
}
130
123
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 )
132
129
{
133
130
GUILayout . Label ( "<i>null (" + this . ValueType + ")</i>" , null ) ;
134
131
}
@@ -138,9 +135,9 @@ public void Draw(Rect window, float labelWidth = 180f)
138
135
}
139
136
}
140
137
141
- public virtual void UpdateValue ( object obj )
138
+ public virtual void UpdateValue ( )
142
139
{
143
- if ( MemberInfo == null )
140
+ if ( MemberInfo == null || ! string . IsNullOrEmpty ( ReflectionException ) )
144
141
{
145
142
return ;
146
143
}
@@ -155,12 +152,15 @@ public virtual void UpdateValue(object obj)
155
152
else if ( MemberInfo . MemberType == MemberTypes . Property )
156
153
{
157
154
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 ) ;
159
158
}
159
+ //ReflectionException = null;
160
160
}
161
- catch // (Exception e)
161
+ catch ( Exception e )
162
162
{
163
- //MelonLogger.Log($"Error updating MemberInfo value | {e.GetType()}: {e.Message}\r\n{e.StackTrace}" );
163
+ ReflectionException = ReflectionHelpers . ExceptionToString ( e ) ;
164
164
}
165
165
}
166
166
0 commit comments