From 97dbecaa2a0d7dae6e00a3eb39299772c6cf299a Mon Sep 17 00:00:00 2001 From: sinaioutlander <49360850+sinaioutlander@users.noreply.github.com> Date: Thu, 19 Nov 2020 16:47:18 +1100 Subject: [PATCH] Made instance inspector helper (owner gameobject/name), force loading unhollowed Assembly-CSharp on game start --- src/ExplorerCore.cs | 7 +- src/Helpers/ReflectionHelpers.cs | 46 +++++----- src/Input/InputManager.cs | 18 ++-- .../Reflection/InstanceInspector.cs | 90 +++++++++++++++++-- .../Reflection/ReflectionInspector.cs | 4 +- 5 files changed, 127 insertions(+), 38 deletions(-) diff --git a/src/ExplorerCore.cs b/src/ExplorerCore.cs index 17ff219..8845d65 100644 --- a/src/ExplorerCore.cs +++ b/src/ExplorerCore.cs @@ -8,13 +8,14 @@ using UnityExplorer.Inspectors; using System.IO; using UnityExplorer.Unstrip; using UnityEngine.SceneManagement; +using UnityExplorer.Helpers; namespace UnityExplorer { public class ExplorerCore { public const string NAME = "UnityExplorer"; - public const string VERSION = "3.0.1"; + public const string VERSION = "3.0.2"; public const string AUTHOR = "Sinai"; public const string GUID = "com.sinai.unityexplorer"; public const string EXPLORER_FOLDER = @"Mods\UnityExplorer"; @@ -41,6 +42,10 @@ namespace UnityExplorer Instance = this; +#if CPP + ReflectionHelpers.TryLoadGameModules(); +#endif + if (!Directory.Exists(EXPLORER_FOLDER)) Directory.CreateDirectory(EXPLORER_FOLDER); diff --git a/src/Helpers/ReflectionHelpers.cs b/src/Helpers/ReflectionHelpers.cs index d47920b..8eb4f28 100644 --- a/src/Helpers/ReflectionHelpers.cs +++ b/src/Helpers/ReflectionHelpers.cs @@ -105,21 +105,19 @@ namespace UnityExplorer.Helpers return getType; } - private static readonly Dictionary ClassPointers = new Dictionary(); + private static readonly Dictionary CppClassPointers = new Dictionary(); public static object Il2CppCast(this object obj, Type castTo) { if (!(obj is Il2CppSystem.Object ilObj)) - { return obj; - } if (!Il2CppTypeNotNull(castTo, out IntPtr castToPtr)) return obj; - IntPtr classPtr = il2cpp_object_get_class(ilObj.Pointer); + IntPtr castFromPtr = il2cpp_object_get_class(ilObj.Pointer); - if (!il2cpp_class_is_assignable_from(castToPtr, classPtr)) + if (!il2cpp_class_is_assignable_from(castToPtr, castFromPtr)) return obj; if (RuntimeSpecificsStore.IsInjected(castToPtr)) @@ -128,24 +126,21 @@ namespace UnityExplorer.Helpers return Activator.CreateInstance(castTo, ilObj.Pointer); } - public static bool Il2CppTypeNotNull(Type type) - { - return Il2CppTypeNotNull(type, out _); - } + public static bool Il2CppTypeNotNull(Type type) => Il2CppTypeNotNull(type, out _); public static bool Il2CppTypeNotNull(Type type, out IntPtr il2cppPtr) { - if (!ClassPointers.ContainsKey(type)) + if (!CppClassPointers.ContainsKey(type)) { il2cppPtr = (IntPtr)typeof(Il2CppClassPointerStore<>) .MakeGenericType(new Type[] { type }) .GetField("NativeClassPtr", BF.Public | BF.Static) .GetValue(null); - ClassPointers.Add(type, il2cppPtr); + CppClassPointers.Add(type, il2cppPtr); } else - il2cppPtr = ClassPointers[type]; + il2cppPtr = CppClassPointers[type]; return il2cppPtr != IntPtr.Zero; } @@ -181,31 +176,42 @@ namespace UnityExplorer.Helpers } } +#if CPP + internal static void TryLoadGameModules() + { + LoadModule("Assembly-CSharp"); + LoadModule("Assembly-CSharp-firstpass"); + } + public static bool LoadModule(string module) { -#if CPP #if ML - string path = $@"MelonLoader\Managed\{module}.dll"; + var path = $@"MelonLoader\Managed\{module}.dll"; #else var path = $@"BepInEx\unhollowed\{module}.dll"; #endif - if (!File.Exists(path)) - { + + return LoadModuleInternal(path); + } + + internal static bool LoadModuleInternal(string fullPath) + { + if (!File.Exists(fullPath)) return false; - } try { - Assembly.Load(File.ReadAllBytes(path)); + Assembly.Load(File.ReadAllBytes(fullPath)); return true; } catch (Exception e) { - ExplorerCore.Log(e.GetType() + ", " + e.Message); + Console.WriteLine(e.GetType() + ", " + e.Message); } -#endif + return false; } +#endif public static bool IsEnumerable(Type t) { diff --git a/src/Input/InputManager.cs b/src/Input/InputManager.cs index 127d495..4578aea 100644 --- a/src/Input/InputManager.cs +++ b/src/Input/InputManager.cs @@ -12,8 +12,17 @@ namespace UnityExplorer.Input { private static IHandleInput m_inputModule; + public static Vector3 MousePosition => m_inputModule.MousePosition; + + public static bool GetKeyDown(KeyCode key) => m_inputModule.GetKeyDown(key); + public static bool GetKey(KeyCode key) => m_inputModule.GetKey(key); + + public static bool GetMouseButtonDown(int btn) => m_inputModule.GetMouseButtonDown(btn); + public static bool GetMouseButton(int btn) => m_inputModule.GetMouseButton(btn); + public static void Init() { +#if CPP if (InputSystem.TKeyboard != null || (ReflectionHelpers.LoadModule("Unity.InputSystem") && InputSystem.TKeyboard != null)) { m_inputModule = new InputSystem(); @@ -22,6 +31,7 @@ namespace UnityExplorer.Input { m_inputModule = new LegacyInput(); } +#endif if (m_inputModule == null) { @@ -29,13 +39,5 @@ namespace UnityExplorer.Input m_inputModule = new NoInput(); } } - - public static Vector3 MousePosition => m_inputModule.MousePosition; - - public static bool GetKeyDown(KeyCode key) => m_inputModule.GetKeyDown(key); - public static bool GetKey(KeyCode key) => m_inputModule.GetKey(key); - - public static bool GetMouseButtonDown(int btn) => m_inputModule.GetMouseButtonDown(btn); - public static bool GetMouseButton(int btn) => m_inputModule.GetMouseButton(btn); } } \ No newline at end of file diff --git a/src/Inspectors/Reflection/InstanceInspector.cs b/src/Inspectors/Reflection/InstanceInspector.cs index f129dee..bf43e1b 100644 --- a/src/Inspectors/Reflection/InstanceInspector.cs +++ b/src/Inspectors/Reflection/InstanceInspector.cs @@ -45,19 +45,95 @@ namespace UnityExplorer.Inspectors.Reflection public void ConstructInstanceHelpers() { + if (!typeof(Component).IsAssignableFrom(m_targetType) && !typeof(UnityEngine.Object).IsAssignableFrom(m_targetType)) + return; + + var rowObj = UIFactory.CreateHorizontalGroup(Content, new Color(0.1f, 0.1f, 0.1f)); + var rowGroup = rowObj.GetComponent(); + rowGroup.childForceExpandWidth = true; + rowGroup.childControlWidth = true; + rowGroup.spacing = 5; + rowGroup.padding.top = 2; + rowGroup.padding.bottom = 2; + rowGroup.padding.right = 2; + rowGroup.padding.left = 2; + var rowLayout = rowObj.AddComponent(); + rowLayout.minHeight = 25; + rowLayout.flexibleWidth = 5000; + + if (typeof(Component).IsAssignableFrom(m_targetType)) + { + ConstructCompHelper(rowObj); + } + + ConstructUObjHelper(rowObj); + // WIP //if (m_targetType == typeof(Texture2D)) // ConstructTextureHelper(); + } - // todo other helpers + internal void ConstructCompHelper(GameObject rowObj) + { + var labelObj = UIFactory.CreateLabel(rowObj, TextAnchor.MiddleLeft); + var labelLayout = labelObj.AddComponent(); + labelLayout.minWidth = 90; + labelLayout.minHeight = 25; + labelLayout.flexibleWidth = 0; + var labelText = labelObj.GetComponent(); + labelText.text = "GameObject:"; - //if (typeof(Component).IsAssignableFrom(m_targetType)) - //{ - //} - //else if (typeof(UnityEngine.Object).IsAssignableFrom(m_targetType)) - //{ - //} +#if MONO + var comp = Target as Component; +#else + var comp = (Target as Il2CppSystem.Object).TryCast(); +#endif + + var goBtnObj = UIFactory.CreateButton(rowObj, new Color(0.2f, 0.5f, 0.2f)); + var goBtnLayout = goBtnObj.AddComponent(); + goBtnLayout.minHeight = 25; + goBtnLayout.minWidth = 200; + goBtnLayout.flexibleWidth = 0; + var text = goBtnObj.GetComponentInChildren(); + text.text = comp.name; + var btn = goBtnObj.GetComponent