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

Commit 153ad22

Browse files
committed
Cleanup project structure
Restructured the project somewhat and cleaned up classes so that things are where they belong. Created "Helpers" folder and put appropriate helper classes in there. Important things: - The "GameObject path" methods are now extension methods on UnityEngine.Transform - Removed AccessTools (Reflection helpers) as there was no use of it. Replaced with ReflectionHelpers class. - Some improvements to the "Object Reflection" window, should be a bit faster now. Code cleaned up significantly.
1 parent 1ba9b2e commit 153ad22

18 files changed

+522
-539
lines changed

src/CppExplorer.cs

Lines changed: 8 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Linq;
5-
using System.Text;
65
using UnityEngine;
76
using MelonLoader;
8-
using UnhollowerBaseLib;
9-
using UnhollowerRuntimeLib;
10-
using Harmony;
11-
using Il2CppSystem.Runtime.InteropServices;
127

138
namespace Explorer
149
{
@@ -17,53 +12,23 @@ public class CppExplorer : MelonMod
1712
// consts
1813

1914
public const string ID = "com.sinai.cppexplorer";
20-
public const string VERSION = "1.3.3";
15+
public const string VERSION = "1.3.5";
2116
public const string AUTHOR = "Sinai";
2217

18+
public const string NAME = "IL2CPP Runtime Explorer"
2319
#if Release_Unity2018
24-
public const string NAME = "IL2CPP Runtime Explorer (Unity 2018)";
25-
#else
26-
public const string NAME = "IL2CPP Runtime Explorer";
20+
+ " (Unity 2018)"
2721
#endif
22+
;
2823

2924
// fields
3025

3126
public static CppExplorer Instance;
32-
private string m_objUnderMouseName = "";
33-
private Camera m_main;
3427

3528
// props
3629

3730
public static bool ShowMenu { get; set; } = false;
3831
public static int ArrayLimit { get; set; } = 20;
39-
public bool MouseInspect { get; set; } = false;
40-
41-
// prop helpers
42-
43-
public static Il2CppSystem.Type GameObjectType => Il2CppType.Of<GameObject>();
44-
public static Il2CppSystem.Type TransformType => Il2CppType.Of<Transform>();
45-
public static Il2CppSystem.Type ObjectType => Il2CppType.Of<UnityEngine.Object>();
46-
public static Il2CppSystem.Type ComponentType => Il2CppType.Of<Component>();
47-
48-
public static string ActiveSceneName
49-
{
50-
get
51-
{
52-
return UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
53-
}
54-
}
55-
56-
public Camera MainCamera
57-
{
58-
get
59-
{
60-
if (m_main == null)
61-
{
62-
m_main = Camera.main;
63-
}
64-
return m_main;
65-
}
66-
}
6732

6833
// methods
6934

@@ -76,21 +41,15 @@ public override void OnApplicationStart()
7641
new MainMenu();
7742
new WindowManager();
7843

79-
// done init
8044
ShowMenu = true;
8145
}
8246

83-
// On scene change
8447
public override void OnLevelWasLoaded(int level)
8548
{
86-
if (ScenePage.Instance != null)
87-
{
88-
ScenePage.Instance.OnSceneChange();
89-
SearchPage.Instance.OnSceneChange();
90-
}
49+
ScenePage.Instance?.OnSceneChange();
50+
SearchPage.Instance?.OnSceneChange();
9151
}
9252

93-
// Update
9453
public override void OnUpdate()
9554
{
9655
if (Input.GetKeyDown(KeyCode.F7))
@@ -109,43 +68,7 @@ public override void OnUpdate()
10968
MainMenu.Instance.Update();
11069
WindowManager.Instance.Update();
11170

112-
if (Input.GetKey(KeyCode.LeftShift) && Input.GetMouseButtonDown(1))
113-
{
114-
MouseInspect = !MouseInspect;
115-
}
116-
117-
if (MouseInspect)
118-
{
119-
InspectUnderMouse();
120-
}
121-
}
122-
else if (MouseInspect)
123-
{
124-
MouseInspect = false;
125-
}
126-
}
127-
128-
private void InspectUnderMouse()
129-
{
130-
Ray ray = MainCamera.ScreenPointToRay(Input.mousePosition);
131-
132-
if (Physics.Raycast(ray, out RaycastHit hit, 1000f))
133-
{
134-
var obj = hit.transform.gameObject;
135-
136-
m_objUnderMouseName = GetGameObjectPath(obj.transform);
137-
138-
if (Input.GetMouseButtonDown(0))
139-
{
140-
MouseInspect = false;
141-
m_objUnderMouseName = "";
142-
143-
WindowManager.InspectObject(obj, out _);
144-
}
145-
}
146-
else
147-
{
148-
m_objUnderMouseName = "";
71+
InspectUnderMouse.Update();
14972
}
15073
}
15174

@@ -156,79 +79,7 @@ public override void OnGUI()
15679
MainMenu.Instance.OnGUI();
15780
WindowManager.Instance.OnGUI();
15881

159-
if (MouseInspect)
160-
{
161-
if (m_objUnderMouseName != "")
162-
{
163-
var pos = Input.mousePosition;
164-
var rect = new Rect(
165-
pos.x - (Screen.width / 2), // x
166-
Screen.height - pos.y - 50, // y
167-
Screen.width, // w
168-
50 // h
169-
);
170-
171-
var origAlign = GUI.skin.label.alignment;
172-
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
173-
174-
//shadow text
175-
GUI.Label(rect, $"<color=black>{m_objUnderMouseName}</color>");
176-
//white text
177-
GUI.Label(new Rect(rect.x - 1, rect.y + 1, rect.width, rect.height), m_objUnderMouseName);
178-
179-
GUI.skin.label.alignment = origAlign;
180-
}
181-
}
182-
}
183-
184-
// ************** public helpers **************
185-
186-
public static object Il2CppCast(object obj, Type castTo)
187-
{
188-
var method = typeof(Il2CppObjectBase).GetMethod("TryCast");
189-
var generic = method.MakeGenericMethod(castTo);
190-
return generic.Invoke(obj, null);
191-
}
192-
193-
public static string GetGameObjectPath(Transform _transform)
194-
{
195-
return GetGameObjectPath(_transform, true);
196-
}
197-
198-
public static string GetGameObjectPath(Transform _transform, bool _includeItemName)
199-
{
200-
string text = _includeItemName ? ("/" + _transform.name) : "";
201-
GameObject gameObject = _transform.gameObject;
202-
while (gameObject.transform.parent != null)
203-
{
204-
gameObject = gameObject.transform.parent.gameObject;
205-
text = "/" + gameObject.name + text;
206-
}
207-
return text;
208-
}
209-
210-
public static Type GetType(string _type)
211-
{
212-
try
213-
{
214-
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
215-
{
216-
try
217-
{
218-
if (asm.GetType(_type) is Type type)
219-
{
220-
return type;
221-
}
222-
}
223-
catch { }
224-
}
225-
226-
return null;
227-
}
228-
catch
229-
{
230-
return null;
231-
}
82+
InspectUnderMouse.OnGUI();
23283
}
23384
}
23485
}

src/CppExplorer.csproj

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,26 @@
137137
</ItemGroup>
138138
<ItemGroup>
139139
<Compile Include="CppExplorer.cs" />
140-
<Compile Include="Inspectors\Reflection\FieldInfoHolder.cs" />
141-
<Compile Include="Inspectors\Reflection\MemberInfoHolder.cs" />
142-
<Compile Include="Inspectors\Reflection\PropertyInfoHolder.cs" />
143-
<Compile Include="Inspectors\UIWindow.cs" />
140+
<Compile Include="Helpers\ReflectionHelpers.cs" />
141+
<Compile Include="Helpers\UIHelpers.cs" />
142+
<Compile Include="Helpers\UnityHelpers.cs" />
143+
<Compile Include="MainMenu\InspectUnderMouse.cs" />
144+
<Compile Include="Windows\Reflection\FieldInfoHolder.cs" />
145+
<Compile Include="Windows\Reflection\MemberInfoHolder.cs" />
146+
<Compile Include="Windows\Reflection\PropertyInfoHolder.cs" />
147+
<Compile Include="Windows\UIWindow.cs" />
144148
<Compile Include="MainMenu\Pages\ConsolePage.cs" />
145149
<Compile Include="MainMenu\Pages\Console\REPL.cs" />
146150
<Compile Include="MainMenu\Pages\Console\REPLHelper.cs" />
147151
<Compile Include="MainMenu\Pages\WindowPage.cs" />
148-
<Compile Include="WindowManager.cs" />
152+
<Compile Include="Windows\WindowManager.cs" />
149153
<Compile Include="MainMenu\MainMenu.cs" />
150-
<Compile Include="Inspectors\GameObjectWindow.cs" />
151-
<Compile Include="Inspectors\ReflectionWindow.cs" />
154+
<Compile Include="Windows\GameObjectWindow.cs" />
155+
<Compile Include="Windows\ReflectionWindow.cs" />
152156
<Compile Include="MainMenu\Pages\ScenePage.cs" />
153157
<Compile Include="MainMenu\Pages\SearchPage.cs" />
154-
<Compile Include="UIStyles.cs" />
158+
<Compile Include="Helpers\UIStyles.cs" />
155159
<Compile Include="Properties\AssemblyInfo.cs" />
156-
<Compile Include="utils\AccessTools.cs" />
157160
</ItemGroup>
158161
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
159162
</Project>

src/Helpers/ReflectionHelpers.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Reflection;
7+
using UnhollowerBaseLib;
8+
using UnhollowerRuntimeLib;
9+
using UnityEngine;
10+
using BF = System.Reflection.BindingFlags;
11+
using ILBF = Il2CppSystem.Reflection.BindingFlags;
12+
13+
namespace Explorer
14+
{
15+
public class ReflectionHelpers
16+
{
17+
public static BF CommonFlags = BF.Public | BF.Instance | BF.NonPublic | BF.Static;
18+
public static ILBF CommonFlags_IL = ILBF.Public | ILBF.NonPublic | ILBF.Instance | ILBF.Static;
19+
20+
public static Il2CppSystem.Type GameObjectType => Il2CppType.Of<GameObject>();
21+
public static Il2CppSystem.Type TransformType => Il2CppType.Of<Transform>();
22+
public static Il2CppSystem.Type ObjectType => Il2CppType.Of<UnityEngine.Object>();
23+
public static Il2CppSystem.Type ComponentType => Il2CppType.Of<Component>();
24+
25+
private static readonly MethodInfo m_tryCastMethodInfo = typeof(Il2CppObjectBase).GetMethod("TryCast");
26+
27+
public static object Il2CppCast(object obj, Type castTo)
28+
{
29+
var generic = m_tryCastMethodInfo.MakeGenericMethod(castTo);
30+
return generic.Invoke(obj, null);
31+
}
32+
33+
public static bool IsList(Type t)
34+
{
35+
return t.IsGenericType
36+
&& t.GetGenericTypeDefinition() is Type typeDef
37+
&& (typeDef.IsAssignableFrom(typeof(List<>)) || typeDef.IsAssignableFrom(typeof(Il2CppSystem.Collections.Generic.List<>)));
38+
}
39+
40+
public static Type GetTypeByName(string typeName)
41+
{
42+
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
43+
{
44+
try
45+
{
46+
if (asm.GetType(typeName) is Type type)
47+
{
48+
return type;
49+
}
50+
}
51+
catch { }
52+
}
53+
54+
return null;
55+
}
56+
57+
public static Type GetActualType(object m_object)
58+
{
59+
if (m_object is Il2CppSystem.Object ilObject)
60+
{
61+
var iltype = ilObject.GetIl2CppType();
62+
return Type.GetType(iltype.AssemblyQualifiedName);
63+
}
64+
else
65+
{
66+
return m_object.GetType();
67+
}
68+
}
69+
70+
public static Type[] GetAllBaseTypes(object m_object)
71+
{
72+
var list = new List<Type>();
73+
74+
if (m_object is Il2CppSystem.Object ilObject)
75+
{
76+
var ilType = ilObject.GetIl2CppType();
77+
if (Type.GetType(ilType.AssemblyQualifiedName) is Type ilTypeToManaged)
78+
{
79+
list.Add(ilTypeToManaged);
80+
81+
while (ilType.BaseType != null)
82+
{
83+
ilType = ilType.BaseType;
84+
if (Type.GetType(ilType.AssemblyQualifiedName) is Type ilBaseTypeToManaged)
85+
{
86+
list.Add(ilBaseTypeToManaged);
87+
}
88+
}
89+
}
90+
}
91+
else
92+
{
93+
var type = m_object.GetType();
94+
list.Add(type);
95+
while (type.BaseType != null)
96+
{
97+
type = type.BaseType;
98+
list.Add(type);
99+
}
100+
}
101+
102+
return list.ToArray();
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)