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

Commit 45b5ce0

Browse files
committed
1.4.2
* Fixed a bug on the Reflection window which would prevent primitive values from being applied * Improved some parts of the Scene Explorer and the Reflection Window interfaces * Scene Explorer now has "page view" like other lists * Various minor cleanups and refactorings
1 parent e3d1add commit 45b5ce0

File tree

15 files changed

+519
-369
lines changed

15 files changed

+519
-369
lines changed

src/CachedObjects/CacheEnum.cs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,18 @@ namespace Explorer
1010
{
1111
public class CacheEnum : CacheObject
1212
{
13-
private readonly Type m_enumType;
14-
private readonly string[] m_names;
13+
public Type EnumType;
14+
public string[] EnumNames;
1515

16-
public CacheEnum(object obj)
16+
public override void Init()
1717
{
18-
if (obj != null)
19-
{
20-
m_enumType = obj.GetType();
21-
m_names = Enum.GetNames(m_enumType);
22-
}
18+
EnumType = Value.GetType();
19+
EnumNames = Enum.GetNames(EnumType);
2320
}
2421

2522
public override void DrawValue(Rect window, float width)
2623
{
27-
if (MemberInfo != null)
24+
if (CanWrite)
2825
{
2926
if (GUILayout.Button("<", new GUILayoutOption[] { GUILayout.Width(25) }))
3027
{
@@ -38,34 +35,18 @@ public override void DrawValue(Rect window, float width)
3835
}
3936
}
4037

41-
GUILayout.Label(Value.ToString(), null);
42-
}
43-
44-
public override void SetValue()
45-
{
46-
if (MemberInfo == null)
47-
{
48-
MelonLogger.Log("Trying to SetValue but the MemberInfo is null!");
49-
return;
50-
}
51-
52-
if (Enum.Parse(m_enumType, Value.ToString()) is object enumValue && enumValue != null)
53-
{
54-
Value = enumValue;
55-
}
56-
57-
SetValue(Value, MemberInfo, DeclaringInstance);
38+
GUILayout.Label(Value.ToString(), null);// + "<color=yellow><i> (" + ValueType + ")</i></color>", null);
5839
}
5940

6041
public void SetEnum(ref object value, int change)
6142
{
62-
var names = m_names.ToList();
43+
var names = EnumNames.ToList();
6344

6445
int newindex = names.IndexOf(value.ToString()) + change;
6546

6647
if ((change < 0 && newindex >= 0) || (change > 0 && newindex < names.Count))
6748
{
68-
value = Enum.Parse(m_enumType, names[newindex]);
49+
value = Enum.Parse(EnumType, names[newindex]);
6950
}
7051
}
7152
}

src/CachedObjects/CacheGameObject.cs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,37 @@ namespace Explorer
1010
{
1111
public class CacheGameObject : CacheObject
1212
{
13-
private GameObject m_gameObject;
14-
15-
public CacheGameObject(object obj)
13+
private GameObject GameObj
1614
{
17-
if (obj != null)
18-
m_gameObject = GetGameObject(obj);
19-
}
20-
21-
private GameObject GetGameObject(object obj)
22-
{
23-
if (obj is Il2CppSystem.Object ilObj)
15+
get
2416
{
25-
var ilType = ilObj.GetIl2CppType();
26-
27-
if (ilType == ReflectionHelpers.GameObjectType || ilType == ReflectionHelpers.TransformType)
17+
if (m_gameObject == null)
2818
{
29-
return ilObj.TryCast<GameObject>() ?? ilObj.TryCast<Transform>()?.gameObject;
19+
if (Value is Il2CppSystem.Object ilObj)
20+
{
21+
var ilType = ilObj.GetIl2CppType();
22+
23+
if (ilType == ReflectionHelpers.GameObjectType || ilType == ReflectionHelpers.TransformType)
24+
{
25+
m_gameObject = ilObj.TryCast<GameObject>() ?? ilObj.TryCast<Transform>()?.gameObject;
26+
}
27+
}
3028
}
31-
}
3229

33-
return null;
30+
return m_gameObject;
31+
}
3432
}
3533

36-
public override void DrawValue(Rect window, float width)
37-
{
38-
UIHelpers.GameobjButton(m_gameObject, null, false, width);
39-
}
34+
private GameObject m_gameObject;
4035

41-
public override void SetValue()
36+
public override void DrawValue(Rect window, float width)
4237
{
43-
throw new NotImplementedException("TODO");
38+
UIHelpers.GameobjButton(GameObj, null, false, width);
4439
}
4540

4641
public override void UpdateValue()
4742
{
4843
base.UpdateValue();
49-
50-
m_gameObject = GetGameObject(Value);
5144
}
5245
}
5346
}

src/CachedObjects/CacheList.cs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public partial class CacheList : CacheObject
1414
{
1515
public bool IsExpanded { get; set; }
1616
public int ArrayOffset { get; set; }
17+
public int ArrayLimit { get; set; } = 20;
1718

1819
public Type EntryType
1920
{
@@ -47,15 +48,6 @@ public IEnumerable Enumerable
4748
private IEnumerable m_enumerable;
4849
private CacheObject[] m_cachedEntries;
4950

50-
public CacheList(object obj)
51-
{
52-
if (obj != null)
53-
{
54-
Value = obj;
55-
EntryType = obj.GetType().GetGenericArguments()[0];
56-
}
57-
}
58-
5951
private IEnumerable CppListToEnumerable(object list)
6052
{
6153
if (EntryType == null) return null;
@@ -95,12 +87,12 @@ public override void DrawValue(Rect window, float width)
9587

9688
if (IsExpanded)
9789
{
98-
if (count > CppExplorer.ArrayLimit)
90+
if (count > ArrayLimit)
9991
{
10092
GUILayout.EndHorizontal();
10193
GUILayout.BeginHorizontal(null);
10294
GUILayout.Space(190);
103-
int maxOffset = (int)Mathf.Ceil((float)(count / (decimal)CppExplorer.ArrayLimit)) - 1;
95+
int maxOffset = (int)Mathf.Ceil((float)(count / (decimal)ArrayLimit)) - 1;
10496
GUILayout.Label($"Page {ArrayOffset + 1}/{maxOffset + 1}", new GUILayoutOption[] { GUILayout.Width(80) });
10597
// prev/next page buttons
10698
if (GUILayout.Button("< Prev", null))
@@ -111,13 +103,20 @@ public override void DrawValue(Rect window, float width)
111103
{
112104
if (ArrayOffset < maxOffset) ArrayOffset++;
113105
}
106+
GUILayout.Label("Limit: ", new GUILayoutOption[] { GUILayout.Width(50) });
107+
var limit = this.ArrayLimit.ToString();
108+
limit = GUILayout.TextField(limit, new GUILayoutOption[] { GUILayout.Width(50) });
109+
if (limit != ArrayLimit.ToString() && int.TryParse(limit, out int i))
110+
{
111+
ArrayLimit = i;
112+
}
114113
}
115114

116-
int offset = ArrayOffset * CppExplorer.ArrayLimit;
115+
int offset = ArrayOffset * ArrayLimit;
117116

118117
if (offset >= count) offset = 0;
119118

120-
for (int i = offset; i < offset + CppExplorer.ArrayLimit && i < count; i++)
119+
for (int i = offset; i < offset + ArrayLimit && i < count; i++)
121120
{
122121
var entry = m_cachedEntries[i];
123122

@@ -140,11 +139,6 @@ public override void DrawValue(Rect window, float width)
140139
}
141140
}
142141

143-
public override void SetValue()
144-
{
145-
throw new NotImplementedException("TODO");
146-
}
147-
148142
/// <summary>
149143
/// Called when the user presses the "Update" button, or if AutoUpdate is on.
150144
/// </summary>
@@ -161,7 +155,7 @@ public override void UpdateValue()
161155
var list = new List<CacheObject>();
162156
while (enumerator.MoveNext())
163157
{
164-
list.Add(GetCacheObject(enumerator.Current));
158+
list.Add(GetCacheObject(enumerator.Current, null, null, this.EntryType));
165159
}
166160

167161
m_cachedEntries = list.ToArray();

src/CachedObjects/CacheObject.cs

Lines changed: 73 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,45 @@ public abstract class CacheObject
1717

1818
// Reflection window only
1919
public MemberInfo MemberInfo { get; set; }
20-
public ReflectionWindow.MemberInfoType MemberInfoType { get; set; }
20+
// public ReflectionWindow.MemberInfoType MemberInfoType { get; set; }
2121
public Type DeclaringType { get; set; }
2222
public object DeclaringInstance { get; set; }
2323
public string FullName => $"{MemberInfo.DeclaringType.Name}.{MemberInfo.Name}";
2424
public string ReflectionException;
2525

26+
public bool CanWrite
27+
{
28+
get
29+
{
30+
if (MemberInfo is FieldInfo fi)
31+
{
32+
return !(fi.IsLiteral && !fi.IsInitOnly);
33+
}
34+
else if (MemberInfo is PropertyInfo pi)
35+
{
36+
return pi.CanWrite;
37+
}
38+
else
39+
{
40+
return false;
41+
}
42+
}
43+
}
44+
45+
public ReflectionWindow.MemberInfoType MemberInfoType
46+
{
47+
get
48+
{
49+
if (MemberInfo is FieldInfo) return ReflectionWindow.MemberInfoType.Field;
50+
if (MemberInfo is PropertyInfo) return ReflectionWindow.MemberInfoType.Property;
51+
if (MemberInfo is MethodInfo) return ReflectionWindow.MemberInfoType.Method;
52+
return ReflectionWindow.MemberInfoType.All;
53+
}
54+
}
55+
2656
// methods
57+
public virtual void Init() { }
2758
public abstract void DrawValue(Rect window, float width);
28-
public abstract void SetValue();
2959

3060
public static CacheObject GetCacheObject(object obj)
3161
{
@@ -41,63 +71,72 @@ public static CacheObject GetCacheObject(object obj)
4171
/// <returns></returns>
4272
public static CacheObject GetCacheObject(object obj, MemberInfo memberInfo, object declaringInstance)
4373
{
44-
CacheObject holder;
45-
4674
var type = ReflectionHelpers.GetActualType(obj) ?? (memberInfo as FieldInfo)?.FieldType ?? (memberInfo as PropertyInfo)?.PropertyType;
4775

76+
if (type == null)
77+
{
78+
MelonLogger.Log("Could not get type for object or memberinfo!");
79+
return null;
80+
}
81+
82+
return GetCacheObject(obj, memberInfo, declaringInstance, type);
83+
}
84+
85+
/// <summary>
86+
/// Gets the CacheObject subclass for an object or MemberInfo
87+
/// </summary>
88+
/// <param name="obj">The current value (can be null if memberInfo is not null)</param>
89+
/// <param name="memberInfo">The MemberInfo (can be null if obj is not null)</param>
90+
/// <param name="declaringInstance">If MemberInfo is not null, the declaring class instance. Can be null if static.</param>
91+
/// <param name="type">The type of the object or MemberInfo value.</param>
92+
/// <returns></returns>
93+
public static CacheObject GetCacheObject(object obj, MemberInfo memberInfo, object declaringInstance, Type type)
94+
{
95+
CacheObject holder;
96+
4897
if ((obj is Il2CppSystem.Object || typeof(Il2CppSystem.Object).IsAssignableFrom(type))
4998
&& (type.FullName.Contains("UnityEngine.GameObject") || type.FullName.Contains("UnityEngine.Transform")))
5099
{
51-
holder = new CacheGameObject(obj);
100+
holder = new CacheGameObject();
52101
}
53102
else if (type.IsPrimitive || type == typeof(string))
54103
{
55-
holder = new CachePrimitive(obj);
104+
holder = new CachePrimitive();
56105
}
57106
else if (type.IsEnum)
58107
{
59-
holder = new CacheEnum(obj);
108+
holder = new CacheEnum();
60109
}
61110
else if (typeof(System.Collections.IEnumerable).IsAssignableFrom(type) || ReflectionHelpers.IsList(type))
62111
{
63-
holder = new CacheList(obj);
112+
holder = new CacheList();
64113
}
65114
else
66115
{
67116
holder = new CacheOther();
68117
}
69118

119+
holder.Value = obj;
120+
holder.ValueType = type.FullName;
121+
70122
if (memberInfo != null)
71123
{
72-
holder.MemberInfo = memberInfo;
124+
holder.MemberInfo = memberInfo;
73125
holder.DeclaringType = memberInfo.DeclaringType;
74126
holder.DeclaringInstance = declaringInstance;
75-
76-
if (memberInfo.MemberType == MemberTypes.Field)
77-
{
78-
holder.MemberInfoType = ReflectionWindow.MemberInfoType.Field;
79-
}
80-
else if (memberInfo.MemberType == MemberTypes.Property)
81-
{
82-
holder.MemberInfoType = ReflectionWindow.MemberInfoType.Property;
83-
}
84-
else if (memberInfo.MemberType == MemberTypes.Method)
85-
{
86-
holder.MemberInfoType = ReflectionWindow.MemberInfoType.Method;
87-
}
88127
}
89128

90-
holder.Value = obj;
91-
holder.ValueType = type.FullName;
129+
holder.UpdateValue();
130+
holder.Init();
92131

93132
return holder;
94133
}
95134

96-
public void Draw(Rect window, float labelWidth = 180f)
135+
public void Draw(Rect window, float labelWidth = 215f)
97136
{
98137
if (MemberInfo != null)
99138
{
100-
GUILayout.Label("<color=cyan>" + FullName + ":</color>", new GUILayoutOption[] { GUILayout.Width(labelWidth) });
139+
GUILayout.Label("<color=cyan>" + FullName + "</color>", new GUILayoutOption[] { GUILayout.Width(labelWidth) });
101140
}
102141
else
103142
{
@@ -110,7 +149,7 @@ public void Draw(Rect window, float labelWidth = 180f)
110149
}
111150
else if (Value == null)
112151
{
113-
GUILayout.Label("<i>null (" + this.ValueType + ")</i>", null);
152+
GUILayout.Label("<i>null (" + ValueType + ")</i>", null);
114153
}
115154
else
116155
{
@@ -147,25 +186,19 @@ public virtual void UpdateValue()
147186
}
148187
}
149188

150-
public void SetValue(object value, MemberInfo memberInfo, object declaringInstance)
189+
public void SetValue()
151190
{
152191
try
153192
{
154-
if (memberInfo.MemberType == MemberTypes.Field)
193+
if (MemberInfo.MemberType == MemberTypes.Field)
155194
{
156-
var fi = memberInfo as FieldInfo;
157-
if (!(fi.IsLiteral && !fi.IsInitOnly))
158-
{
159-
fi.SetValue(fi.IsStatic ? null : declaringInstance, value);
160-
}
195+
var fi = MemberInfo as FieldInfo;
196+
fi.SetValue(fi.IsStatic ? null : DeclaringInstance, Value);
161197
}
162-
else if (memberInfo.MemberType == MemberTypes.Property)
198+
else if (MemberInfo.MemberType == MemberTypes.Property)
163199
{
164-
var pi = memberInfo as PropertyInfo;
165-
if (pi.CanWrite)
166-
{
167-
pi.SetValue(pi.GetAccessors()[0].IsStatic ? null : declaringInstance, value);
168-
}
200+
var pi = MemberInfo as PropertyInfo;
201+
pi.SetValue(pi.GetAccessors()[0].IsStatic ? null : DeclaringInstance, Value);
169202
}
170203
}
171204
catch (Exception e)

0 commit comments

Comments
 (0)