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

Commit 0c30679

Browse files
committed
1.3.2 cleanup
- cleanup - fixed a mistake with FieldInfos on reflection window, causing all values to be null. - improved displaying of generic objects (now shows object Type after the name)
1 parent 4115935 commit 0c30679

File tree

10 files changed

+321
-249
lines changed

10 files changed

+321
-249
lines changed

src/Inspectors/Reflection/FieldInfoHolder.cs

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,24 @@ public FieldInfoHolder(Type _type, FieldInfo _fieldInfo)
2222

2323
public override void UpdateValue(object obj)
2424
{
25-
m_value = fieldInfo.GetValue(fieldInfo.IsStatic ? null : obj);
25+
try
26+
{
27+
if (obj is Il2CppSystem.Object ilObject)
28+
{
29+
var declaringType = this.fieldInfo.DeclaringType;
30+
31+
var cast = CppExplorer.Il2CppCast(obj, declaringType);
32+
m_value = this.fieldInfo.GetValue(fieldInfo.IsStatic ? null : cast);
33+
}
34+
else
35+
{
36+
m_value = this.fieldInfo.GetValue(fieldInfo.IsStatic ? null : obj);
37+
}
38+
}
39+
catch (Exception e)
40+
{
41+
MelonLogger.Log($"Error updating FieldInfoHolder | {e.GetType()}: {e.Message}\r\n{e.StackTrace}");
42+
}
2643
}
2744

2845
public override void Draw(ReflectionWindow window)
@@ -32,51 +49,72 @@ public override void Draw(ReflectionWindow window)
3249

3350
public override void SetValue(object obj)
3451
{
35-
if (fieldInfo.FieldType.IsEnum)
52+
try
3653
{
37-
if (System.Enum.Parse(fieldInfo.FieldType, m_value.ToString()) is object enumValue && enumValue != null)
54+
if (fieldInfo.FieldType.IsEnum)
3855
{
39-
m_value = enumValue;
40-
}
41-
}
42-
else if (fieldInfo.FieldType.IsPrimitive)
43-
{
44-
if (fieldInfo.FieldType == typeof(float))
45-
{
46-
if (float.TryParse(m_value.ToString(), out float f))
47-
{
48-
m_value = f;
49-
}
50-
else
56+
if (Enum.Parse(fieldInfo.FieldType, m_value.ToString()) is object enumValue && enumValue != null)
5157
{
52-
MelonLogger.LogWarning("Cannot parse " + m_value.ToString() + " to a float!");
58+
m_value = enumValue;
5359
}
5460
}
55-
else if (fieldInfo.FieldType == typeof(double))
61+
else if (fieldInfo.FieldType.IsPrimitive)
5662
{
57-
if (double.TryParse(m_value.ToString(), out double d))
63+
if (fieldInfo.FieldType == typeof(float))
5864
{
59-
m_value = d;
65+
if (float.TryParse(m_value.ToString(), out float f))
66+
{
67+
m_value = f;
68+
}
69+
else
70+
{
71+
MelonLogger.LogWarning("Cannot parse " + m_value.ToString() + " to a float!");
72+
}
6073
}
61-
else
74+
else if (fieldInfo.FieldType == typeof(double))
6275
{
63-
MelonLogger.LogWarning("Cannot parse " + m_value.ToString() + " to a double!");
76+
if (double.TryParse(m_value.ToString(), out double d))
77+
{
78+
m_value = d;
79+
}
80+
else
81+
{
82+
MelonLogger.LogWarning("Cannot parse " + m_value.ToString() + " to a double!");
83+
}
6484
}
65-
}
66-
else if (fieldInfo.FieldType != typeof(bool))
67-
{
68-
if (int.TryParse(m_value.ToString(), out int i))
85+
else if (fieldInfo.FieldType != typeof(bool))
6986
{
70-
m_value = i;
87+
if (int.TryParse(m_value.ToString(), out int i))
88+
{
89+
m_value = i;
90+
}
91+
else
92+
{
93+
MelonLogger.LogWarning("Cannot parse " + m_value.ToString() + " to an integer! type: " + fieldInfo.FieldType);
94+
}
7195
}
7296
else
7397
{
74-
MelonLogger.LogWarning("Cannot parse " + m_value.ToString() + " to an integer! type: " + fieldInfo.FieldType);
98+
MelonLogger.Log("Unsupported primitive field type: " + fieldInfo.FieldType.FullName);
7599
}
76100
}
77-
}
78101

79-
fieldInfo.SetValue(fieldInfo.IsStatic ? null : obj, m_value);
102+
if (obj is Il2CppSystem.Object ilObject)
103+
{
104+
var declaringType = this.fieldInfo.DeclaringType;
105+
106+
var cast = CppExplorer.Il2CppCast(obj, declaringType);
107+
fieldInfo.SetValue(fieldInfo.IsStatic ? null : cast, m_value);
108+
}
109+
else
110+
{
111+
fieldInfo.SetValue(fieldInfo.IsStatic ? null : obj, m_value);
112+
}
113+
}
114+
catch (Exception e)
115+
{
116+
MelonLogger.Log($"Error setting FieldInfoHolder | {e.GetType()}: {e.Message}\r\n{e.StackTrace}");
117+
}
80118
}
81119
}
82120
}

src/Inspectors/Reflection/PropertyInfoHolder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ public override void UpdateValue(object obj)
3333
if (obj is Il2CppSystem.Object ilObject)
3434
{
3535
var declaringType = this.propInfo.DeclaringType;
36+
3637
if (declaringType == typeof(Il2CppObjectBase))
3738
{
3839
m_value = ilObject.Pointer;
3940
}
4041
else
4142
{
4243
var cast = CppExplorer.Il2CppCast(obj, declaringType);
43-
m_value = this.propInfo.GetValue(cast, null);
44+
m_value = this.propInfo.GetValue(this.propInfo.GetAccessors()[0].IsStatic ? null : cast, null);
4445
}
4546
}
4647
else
@@ -70,7 +71,7 @@ public override void SetValue(object obj)
7071
{
7172
if (propInfo.PropertyType.IsEnum)
7273
{
73-
if (System.Enum.Parse(propInfo.PropertyType, m_value.ToString()) is object enumValue && enumValue != null)
74+
if (Enum.Parse(propInfo.PropertyType, m_value.ToString()) is object enumValue && enumValue != null)
7475
{
7576
m_value = enumValue;
7677
}

src/Inspectors/ReflectionWindow.cs

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -31,54 +31,6 @@ public enum MemberFilter
3131
Field
3232
}
3333

34-
public Type GetActualType(object m_object)
35-
{
36-
if (m_object is Il2CppSystem.Object ilObject)
37-
{
38-
var iltype = ilObject.GetIl2CppType();
39-
return Type.GetType(iltype.AssemblyQualifiedName);
40-
}
41-
else
42-
{
43-
return m_object.GetType();
44-
}
45-
}
46-
47-
public Type[] GetAllBaseTypes(object m_object)
48-
{
49-
var list = new List<Type>();
50-
51-
if (m_object is Il2CppSystem.Object ilObject)
52-
{
53-
var ilType = ilObject.GetIl2CppType();
54-
if (Type.GetType(ilType.AssemblyQualifiedName) is Type ilTypeToManaged)
55-
{
56-
list.Add(ilTypeToManaged);
57-
58-
while (ilType.BaseType != null)
59-
{
60-
ilType = ilType.BaseType;
61-
if (Type.GetType(ilType.AssemblyQualifiedName) is Type ilBaseTypeToManaged)
62-
{
63-
list.Add(ilBaseTypeToManaged);
64-
}
65-
}
66-
}
67-
}
68-
else
69-
{
70-
var type = m_object.GetType();
71-
list.Add(type);
72-
while (type.BaseType != null)
73-
{
74-
type = type.BaseType;
75-
list.Add(type);
76-
}
77-
}
78-
79-
return list.ToArray();
80-
}
81-
8234
public override void Init()
8335
{
8436
m_object = Target;
@@ -101,7 +53,7 @@ public override void Init()
10153
}
10254
catch { }
10355

104-
UpdateValues();
56+
UpdateValues(true);
10557
}
10658

10759
public override void Update()
@@ -112,9 +64,9 @@ public override void Update()
11264
}
11365
}
11466

115-
private void UpdateValues()
67+
private void UpdateValues(bool forceAll = false)
11668
{
117-
if (m_filter == MemberFilter.Both || m_filter == MemberFilter.Field)
69+
if (forceAll || m_filter == MemberFilter.Both || m_filter == MemberFilter.Field)
11870
{
11971
foreach (var holder in this.m_FieldInfos)
12072
{
@@ -125,7 +77,7 @@ private void UpdateValues()
12577
}
12678
}
12779

128-
if (m_filter == MemberFilter.Both || m_filter == MemberFilter.Property)
80+
if (forceAll || m_filter == MemberFilter.Both || m_filter == MemberFilter.Property)
12981
{
13082
foreach (var holder in this.m_PropertyInfos)
13183
{
@@ -278,6 +230,56 @@ private void FilterToggle(MemberFilter mode, string label)
278230
GUI.color = Color.white;
279231
}
280232

233+
// ============ HELPERS ===============
234+
235+
public Type GetActualType(object m_object)
236+
{
237+
if (m_object is Il2CppSystem.Object ilObject)
238+
{
239+
var iltype = ilObject.GetIl2CppType();
240+
return Type.GetType(iltype.AssemblyQualifiedName);
241+
}
242+
else
243+
{
244+
return m_object.GetType();
245+
}
246+
}
247+
248+
public Type[] GetAllBaseTypes(object m_object)
249+
{
250+
var list = new List<Type>();
251+
252+
if (m_object is Il2CppSystem.Object ilObject)
253+
{
254+
var ilType = ilObject.GetIl2CppType();
255+
if (Type.GetType(ilType.AssemblyQualifiedName) is Type ilTypeToManaged)
256+
{
257+
list.Add(ilTypeToManaged);
258+
259+
while (ilType.BaseType != null)
260+
{
261+
ilType = ilType.BaseType;
262+
if (Type.GetType(ilType.AssemblyQualifiedName) is Type ilBaseTypeToManaged)
263+
{
264+
list.Add(ilBaseTypeToManaged);
265+
}
266+
}
267+
}
268+
}
269+
else
270+
{
271+
var type = m_object.GetType();
272+
list.Add(type);
273+
while (type.BaseType != null)
274+
{
275+
type = type.BaseType;
276+
list.Add(type);
277+
}
278+
}
279+
280+
return list.ToArray();
281+
}
282+
281283
public static bool IsList(Type t)
282284
{
283285
return t.IsGenericType

src/MainMenu/Pages/SearchPage.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class SearchPage : WindowPage
2020

2121
private string m_searchInput = "";
2222
private string m_typeInput = "";
23-
private int m_limit = 100;
23+
private int m_limit = 20;
2424
private int m_pageOffset = 0;
2525
private List<object> m_searchResults = new List<object>();
2626
private Vector2 resultsScroll = Vector2.zero;
@@ -85,11 +85,11 @@ public override void DrawWindow()
8585

8686
int count = m_searchResults.Count;
8787

88-
if (count > CppExplorer.ArrayLimit)
88+
if (count > this.m_limit)
8989
{
9090
// prev/next page buttons
9191
GUILayout.BeginHorizontal(null);
92-
int maxOffset = (int)Mathf.Ceil(count / CppExplorer.ArrayLimit);
92+
int maxOffset = (int)Mathf.Ceil(count / this.m_limit);
9393
if (GUILayout.Button("< Prev", null))
9494
{
9595
if (m_pageOffset > 0) m_pageOffset--;
@@ -110,7 +110,7 @@ public override void DrawWindow()
110110

111111
if (m_searchResults.Count > 0)
112112
{
113-
int offset = m_pageOffset * CppExplorer.ArrayLimit;
113+
int offset = m_pageOffset * this.m_limit;
114114
int preiterated = 0;
115115

116116
if (offset >= count) m_pageOffset = 0;
@@ -123,7 +123,7 @@ public override void DrawWindow()
123123
continue;
124124
}
125125

126-
if (i - offset > CppExplorer.ArrayLimit - 1)
126+
if (i - offset > this.m_limit - 1)
127127
{
128128
break;
129129
}
@@ -257,14 +257,14 @@ private void Search()
257257

258258
private List<object> FindAllObjectsOfType(string _search, string _type)
259259
{
260-
Il2CppSystem.Type type = null;
260+
Il2CppSystem.Type searchType = null;
261261

262262
if (TypeMode == TypeFilter.Custom)
263263
{
264264
try
265265
{
266266
var findType = CppExplorer.GetType(_type);
267-
type = Il2CppSystem.Type.GetType(findType.AssemblyQualifiedName);
267+
searchType = Il2CppSystem.Type.GetType(findType.AssemblyQualifiedName);
268268
}
269269
catch (Exception e)
270270
{
@@ -273,26 +273,26 @@ private List<object> FindAllObjectsOfType(string _search, string _type)
273273
}
274274
else if (TypeMode == TypeFilter.Object)
275275
{
276-
type = CppExplorer.ObjectType;
276+
searchType = CppExplorer.ObjectType;
277277
}
278278
else if (TypeMode == TypeFilter.GameObject)
279279
{
280-
type = CppExplorer.GameObjectType;
280+
searchType = CppExplorer.GameObjectType;
281281
}
282282
else if (TypeMode == TypeFilter.Component)
283283
{
284-
type = CppExplorer.ComponentType;
284+
searchType = CppExplorer.ComponentType;
285285
}
286286

287-
if (!CppExplorer.ObjectType.IsAssignableFrom(type))
287+
if (!CppExplorer.ObjectType.IsAssignableFrom(searchType))
288288
{
289-
MelonLogger.LogError("Your Class Type must inherit from UnityEngine.Object! Leave blank to default to UnityEngine.Object");
289+
MelonLogger.LogError("Your Custom Class Type must inherit from UnityEngine.Object!");
290290
return new List<object>();
291291
}
292292

293293
var matches = new List<object>();
294294

295-
var allObjectsOfType = Resources.FindObjectsOfTypeAll(type);
295+
var allObjectsOfType = Resources.FindObjectsOfTypeAll(searchType);
296296

297297
foreach (var obj in allObjectsOfType)
298298
{
@@ -301,6 +301,13 @@ private List<object> FindAllObjectsOfType(string _search, string _type)
301301
continue;
302302
}
303303

304+
if (searchType == CppExplorer.ComponentType && CppExplorer.TransformType.IsAssignableFrom(obj.GetIl2CppType()))
305+
{
306+
// Transforms shouldn't really be counted as Components, skip them.
307+
// They're more akin to GameObjects.
308+
continue;
309+
}
310+
304311
if (SceneMode != SceneFilter.Any && !FilterScene(obj, this.SceneMode))
305312
{
306313
continue;

0 commit comments

Comments
 (0)