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

Commit 97dbeca

Browse files
committed
Made instance inspector helper (owner gameobject/name), force loading unhollowed Assembly-CSharp on game start
1 parent e77e4cc commit 97dbeca

File tree

5 files changed

+127
-38
lines changed

5 files changed

+127
-38
lines changed

src/ExplorerCore.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
using System.IO;
99
using UnityExplorer.Unstrip;
1010
using UnityEngine.SceneManagement;
11+
using UnityExplorer.Helpers;
1112

1213
namespace UnityExplorer
1314
{
1415
public class ExplorerCore
1516
{
1617
public const string NAME = "UnityExplorer";
17-
public const string VERSION = "3.0.1";
18+
public const string VERSION = "3.0.2";
1819
public const string AUTHOR = "Sinai";
1920
public const string GUID = "com.sinai.unityexplorer";
2021
public const string EXPLORER_FOLDER = @"Mods\UnityExplorer";
@@ -41,6 +42,10 @@ public ExplorerCore()
4142

4243
Instance = this;
4344

45+
#if CPP
46+
ReflectionHelpers.TryLoadGameModules();
47+
#endif
48+
4449
if (!Directory.Exists(EXPLORER_FOLDER))
4550
Directory.CreateDirectory(EXPLORER_FOLDER);
4651

src/Helpers/ReflectionHelpers.cs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,19 @@ public static Type GetMonoType(CppType cppType)
105105
return getType;
106106
}
107107

108-
private static readonly Dictionary<Type, IntPtr> ClassPointers = new Dictionary<Type, IntPtr>();
108+
private static readonly Dictionary<Type, IntPtr> CppClassPointers = new Dictionary<Type, IntPtr>();
109109

110110
public static object Il2CppCast(this object obj, Type castTo)
111111
{
112112
if (!(obj is Il2CppSystem.Object ilObj))
113-
{
114113
return obj;
115-
}
116114

117115
if (!Il2CppTypeNotNull(castTo, out IntPtr castToPtr))
118116
return obj;
119117

120-
IntPtr classPtr = il2cpp_object_get_class(ilObj.Pointer);
118+
IntPtr castFromPtr = il2cpp_object_get_class(ilObj.Pointer);
121119

122-
if (!il2cpp_class_is_assignable_from(castToPtr, classPtr))
120+
if (!il2cpp_class_is_assignable_from(castToPtr, castFromPtr))
123121
return obj;
124122

125123
if (RuntimeSpecificsStore.IsInjected(castToPtr))
@@ -128,24 +126,21 @@ public static object Il2CppCast(this object obj, Type castTo)
128126
return Activator.CreateInstance(castTo, ilObj.Pointer);
129127
}
130128

131-
public static bool Il2CppTypeNotNull(Type type)
132-
{
133-
return Il2CppTypeNotNull(type, out _);
134-
}
129+
public static bool Il2CppTypeNotNull(Type type) => Il2CppTypeNotNull(type, out _);
135130

136131
public static bool Il2CppTypeNotNull(Type type, out IntPtr il2cppPtr)
137132
{
138-
if (!ClassPointers.ContainsKey(type))
133+
if (!CppClassPointers.ContainsKey(type))
139134
{
140135
il2cppPtr = (IntPtr)typeof(Il2CppClassPointerStore<>)
141136
.MakeGenericType(new Type[] { type })
142137
.GetField("NativeClassPtr", BF.Public | BF.Static)
143138
.GetValue(null);
144139

145-
ClassPointers.Add(type, il2cppPtr);
140+
CppClassPointers.Add(type, il2cppPtr);
146141
}
147142
else
148-
il2cppPtr = ClassPointers[type];
143+
il2cppPtr = CppClassPointers[type];
149144

150145
return il2cppPtr != IntPtr.Zero;
151146
}
@@ -181,31 +176,42 @@ public static IEnumerable<Type> TryGetTypes(this Assembly asm)
181176
}
182177
}
183178

179+
#if CPP
180+
internal static void TryLoadGameModules()
181+
{
182+
LoadModule("Assembly-CSharp");
183+
LoadModule("Assembly-CSharp-firstpass");
184+
}
185+
184186
public static bool LoadModule(string module)
185187
{
186-
#if CPP
187188
#if ML
188-
string path = $@"MelonLoader\Managed\{module}.dll";
189+
var path = $@"MelonLoader\Managed\{module}.dll";
189190
#else
190191
var path = $@"BepInEx\unhollowed\{module}.dll";
191192
#endif
192-
if (!File.Exists(path))
193-
{
193+
194+
return LoadModuleInternal(path);
195+
}
196+
197+
internal static bool LoadModuleInternal(string fullPath)
198+
{
199+
if (!File.Exists(fullPath))
194200
return false;
195-
}
196201

197202
try
198203
{
199-
Assembly.Load(File.ReadAllBytes(path));
204+
Assembly.Load(File.ReadAllBytes(fullPath));
200205
return true;
201206
}
202207
catch (Exception e)
203208
{
204-
ExplorerCore.Log(e.GetType() + ", " + e.Message);
209+
Console.WriteLine(e.GetType() + ", " + e.Message);
205210
}
206-
#endif
211+
207212
return false;
208213
}
214+
#endif
209215

210216
public static bool IsEnumerable(Type t)
211217
{

src/Input/InputManager.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@ public static class InputManager
1212
{
1313
private static IHandleInput m_inputModule;
1414

15+
public static Vector3 MousePosition => m_inputModule.MousePosition;
16+
17+
public static bool GetKeyDown(KeyCode key) => m_inputModule.GetKeyDown(key);
18+
public static bool GetKey(KeyCode key) => m_inputModule.GetKey(key);
19+
20+
public static bool GetMouseButtonDown(int btn) => m_inputModule.GetMouseButtonDown(btn);
21+
public static bool GetMouseButton(int btn) => m_inputModule.GetMouseButton(btn);
22+
1523
public static void Init()
1624
{
25+
#if CPP
1726
if (InputSystem.TKeyboard != null || (ReflectionHelpers.LoadModule("Unity.InputSystem") && InputSystem.TKeyboard != null))
1827
{
1928
m_inputModule = new InputSystem();
@@ -22,20 +31,13 @@ public static void Init()
2231
{
2332
m_inputModule = new LegacyInput();
2433
}
34+
#endif
2535

2636
if (m_inputModule == null)
2737
{
2838
ExplorerCore.LogWarning("Could not find any Input module!");
2939
m_inputModule = new NoInput();
3040
}
3141
}
32-
33-
public static Vector3 MousePosition => m_inputModule.MousePosition;
34-
35-
public static bool GetKeyDown(KeyCode key) => m_inputModule.GetKeyDown(key);
36-
public static bool GetKey(KeyCode key) => m_inputModule.GetKey(key);
37-
38-
public static bool GetMouseButtonDown(int btn) => m_inputModule.GetMouseButtonDown(btn);
39-
public static bool GetMouseButton(int btn) => m_inputModule.GetMouseButton(btn);
4042
}
4143
}

src/Inspectors/Reflection/InstanceInspector.cs

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,95 @@ private void OnScopeFilterClicked(MemberScopes type, Button button)
4545

4646
public void ConstructInstanceHelpers()
4747
{
48+
if (!typeof(Component).IsAssignableFrom(m_targetType) && !typeof(UnityEngine.Object).IsAssignableFrom(m_targetType))
49+
return;
50+
51+
var rowObj = UIFactory.CreateHorizontalGroup(Content, new Color(0.1f, 0.1f, 0.1f));
52+
var rowGroup = rowObj.GetComponent<HorizontalLayoutGroup>();
53+
rowGroup.childForceExpandWidth = true;
54+
rowGroup.childControlWidth = true;
55+
rowGroup.spacing = 5;
56+
rowGroup.padding.top = 2;
57+
rowGroup.padding.bottom = 2;
58+
rowGroup.padding.right = 2;
59+
rowGroup.padding.left = 2;
60+
var rowLayout = rowObj.AddComponent<LayoutElement>();
61+
rowLayout.minHeight = 25;
62+
rowLayout.flexibleWidth = 5000;
63+
64+
if (typeof(Component).IsAssignableFrom(m_targetType))
65+
{
66+
ConstructCompHelper(rowObj);
67+
}
68+
69+
ConstructUObjHelper(rowObj);
70+
4871
// WIP
4972

5073
//if (m_targetType == typeof(Texture2D))
5174
// ConstructTextureHelper();
75+
}
5276

53-
// todo other helpers
77+
internal void ConstructCompHelper(GameObject rowObj)
78+
{
79+
var labelObj = UIFactory.CreateLabel(rowObj, TextAnchor.MiddleLeft);
80+
var labelLayout = labelObj.AddComponent<LayoutElement>();
81+
labelLayout.minWidth = 90;
82+
labelLayout.minHeight = 25;
83+
labelLayout.flexibleWidth = 0;
84+
var labelText = labelObj.GetComponent<Text>();
85+
labelText.text = "GameObject:";
86+
87+
#if MONO
88+
var comp = Target as Component;
89+
#else
90+
var comp = (Target as Il2CppSystem.Object).TryCast<Component>();
91+
#endif
92+
93+
var goBtnObj = UIFactory.CreateButton(rowObj, new Color(0.2f, 0.5f, 0.2f));
94+
var goBtnLayout = goBtnObj.AddComponent<LayoutElement>();
95+
goBtnLayout.minHeight = 25;
96+
goBtnLayout.minWidth = 200;
97+
goBtnLayout.flexibleWidth = 0;
98+
var text = goBtnObj.GetComponentInChildren<Text>();
99+
text.text = comp.name;
100+
var btn = goBtnObj.GetComponent<Button>();
101+
btn.onClick.AddListener(() => { InspectorManager.Instance.Inspect(comp.gameObject); });
102+
}
54103

55-
//if (typeof(Component).IsAssignableFrom(m_targetType))
56-
//{
57-
//}
58-
//else if (typeof(UnityEngine.Object).IsAssignableFrom(m_targetType))
59-
//{
60-
//}
104+
internal void ConstructUObjHelper(GameObject rowObj)
105+
{
106+
var labelObj = UIFactory.CreateLabel(rowObj, TextAnchor.MiddleLeft);
107+
var labelLayout = labelObj.AddComponent<LayoutElement>();
108+
labelLayout.minWidth = 60;
109+
labelLayout.minHeight = 25;
110+
labelLayout.flexibleWidth = 0;
111+
var labelText = labelObj.GetComponent<Text>();
112+
labelText.text = "Name:";
113+
114+
#if MONO
115+
var uObj = Target as UnityEngine.Object;
116+
#else
117+
var uObj = (Target as Il2CppSystem.Object).TryCast<UnityEngine.Object>();
118+
#endif
119+
120+
var inputObj = UIFactory.CreateInputField(rowObj, 14, 3, 1);
121+
var inputLayout = inputObj.AddComponent<LayoutElement>();
122+
inputLayout.minHeight = 25;
123+
inputLayout.flexibleWidth = 2000;
124+
var inputField = inputObj.GetComponent<InputField>();
125+
inputField.readOnly = true;
126+
inputField.text = uObj.name;
127+
128+
//var goBtnObj = UIFactory.CreateButton(rowObj, new Color(0.2f, 0.5f, 0.2f));
129+
//var goBtnLayout = goBtnObj.AddComponent<LayoutElement>();
130+
//goBtnLayout.minHeight = 25;
131+
//goBtnLayout.minWidth = 200;
132+
//goBtnLayout.flexibleWidth = 0;
133+
//var text = goBtnObj.GetComponentInChildren<Text>();
134+
//text.text = comp.name;
135+
//var btn = goBtnObj.GetComponent<Button>();
136+
//btn.onClick.AddListener(() => { InspectorManager.Instance.Inspect(comp.gameObject); });
61137
}
62138

63139
//internal bool showingTextureHelper;

src/Inspectors/Reflection/ReflectionInspector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public void CacheMembers(Type type)
179179
var pi = member as PropertyInfo;
180180
var fi = member as FieldInfo;
181181

182-
if (IsBlacklisted(sig) || mi != null && IsBlacklisted(mi))
182+
if (IsBlacklisted(sig) || (mi != null && IsBlacklisted(mi)))
183183
continue;
184184

185185
var args = mi?.GetParameters() ?? pi?.GetIndexParameters();
@@ -581,7 +581,7 @@ internal void ConstructOptionsArea()
581581

582582
internal void ConstructMemberList()
583583
{
584-
var scrollobj = UIFactory.CreateScrollView(Content, out m_scrollContent, out m_sliderScroller, new Color(0.08f, 0.08f, 0.08f));
584+
var scrollobj = UIFactory.CreateScrollView(Content, out m_scrollContent, out m_sliderScroller, new Color(0.05f, 0.05f, 0.05f));
585585

586586
m_scrollContentRect = m_scrollContent.GetComponent<RectTransform>();
587587

0 commit comments

Comments
 (0)