From 0769b7ef23473d3e4654f2439013b260c4c5c716 Mon Sep 17 00:00:00 2001 From: sinaioutlander <49360850+sinaioutlander@users.noreply.github.com> Date: Tue, 8 Sep 2020 23:47:17 +1000 Subject: [PATCH] 1.6.5 * Add expander to Unity struct inspectors, collapsed by default * `UnityEngine.Color` labels in Reflection Inspector are now the same color as the value for convenience * Cleaned up InputHelper --- src/CachedObjects/Struct/CacheColor.cs | 27 ++++- src/CachedObjects/Struct/CacheQuaternion.cs | 22 +++- src/CachedObjects/Struct/CacheRect.cs | 22 +++- src/CachedObjects/Struct/CacheVector.cs | 22 +++- src/CppExplorer.cs | 4 +- src/CppExplorer.csproj | 4 +- src/CppExplorer.sln | 4 +- src/Helpers/InputHelper.cs | 119 +++++++++++--------- 8 files changed, 160 insertions(+), 64 deletions(-) diff --git a/src/CachedObjects/Struct/CacheColor.cs b/src/CachedObjects/Struct/CacheColor.cs index 0e1525c..936d2d2 100644 --- a/src/CachedObjects/Struct/CacheColor.cs +++ b/src/CachedObjects/Struct/CacheColor.cs @@ -9,6 +9,8 @@ namespace Explorer { public class CacheColor : CacheObjectBase { + private bool IsExpanded; + private string r = "0"; private string g = "0"; private string b = "0"; @@ -28,9 +30,30 @@ namespace Explorer public override void DrawValue(Rect window, float width) { - GUILayout.Label($"Color: {((Color)Value).ToString()}", null); - if (CanWrite) + { + if (!IsExpanded) + { + if (GUILayout.Button("v", new GUILayoutOption[] { GUILayout.Width(25) })) + { + IsExpanded = true; + } + } + else + { + if (GUILayout.Button("^", new GUILayoutOption[] { GUILayout.Width(25) })) + { + IsExpanded = false; + } + } + } + + var c = (Color)Value; + GUI.color = c; + GUILayout.Label($"Color: {c.ToString()}", null); + GUI.color = Color.white; + + if (CanWrite && IsExpanded) { GUILayout.EndHorizontal(); var whitespace = window.width - width - 90; diff --git a/src/CachedObjects/Struct/CacheQuaternion.cs b/src/CachedObjects/Struct/CacheQuaternion.cs index dcfcf75..37b7fee 100644 --- a/src/CachedObjects/Struct/CacheQuaternion.cs +++ b/src/CachedObjects/Struct/CacheQuaternion.cs @@ -9,6 +9,8 @@ namespace Explorer { public class CacheQuaternion : CacheObjectBase { + private bool IsExpanded; + private string x = "0"; private string y = "0"; private string z = "0"; @@ -26,9 +28,27 @@ namespace Explorer public override void DrawValue(Rect window, float width) { + if (CanWrite) + { + if (!IsExpanded) + { + if (GUILayout.Button("v", new GUILayoutOption[] { GUILayout.Width(25) })) + { + IsExpanded = true; + } + } + else + { + if (GUILayout.Button("^", new GUILayoutOption[] { GUILayout.Width(25) })) + { + IsExpanded = false; + } + } + } + GUILayout.Label($"Quaternion: {((Quaternion)Value).eulerAngles.ToString()}", null); - if (CanWrite) + if (CanWrite && IsExpanded) { GUILayout.EndHorizontal(); var whitespace = window.width - width - 90; diff --git a/src/CachedObjects/Struct/CacheRect.cs b/src/CachedObjects/Struct/CacheRect.cs index 5488432..cbb2a46 100644 --- a/src/CachedObjects/Struct/CacheRect.cs +++ b/src/CachedObjects/Struct/CacheRect.cs @@ -9,6 +9,8 @@ namespace Explorer { public class CacheRect : CacheObjectBase { + private bool IsExpanded; + private string x = "0"; private string y = "0"; private string w = "0"; @@ -28,9 +30,27 @@ namespace Explorer public override void DrawValue(Rect window, float width) { + if (CanWrite) + { + if (!IsExpanded) + { + if (GUILayout.Button("v", new GUILayoutOption[] { GUILayout.Width(25) })) + { + IsExpanded = true; + } + } + else + { + if (GUILayout.Button("^", new GUILayoutOption[] { GUILayout.Width(25) })) + { + IsExpanded = false; + } + } + } + GUILayout.Label($"Rect: {((Rect)Value).ToString()}", null); - if (CanWrite) + if (CanWrite && IsExpanded) { GUILayout.EndHorizontal(); var whitespace = window.width - width - 90; diff --git a/src/CachedObjects/Struct/CacheVector.cs b/src/CachedObjects/Struct/CacheVector.cs index c57c903..9c4e781 100644 --- a/src/CachedObjects/Struct/CacheVector.cs +++ b/src/CachedObjects/Struct/CacheVector.cs @@ -10,6 +10,8 @@ namespace Explorer { public class CacheVector : CacheObjectBase { + private bool IsExpanded; + public int VectorSize = 2; private string x = "0"; @@ -63,9 +65,27 @@ namespace Explorer public override void DrawValue(Rect window, float width) { + if (CanWrite) + { + if (!IsExpanded) + { + if (GUILayout.Button("v", new GUILayoutOption[] { GUILayout.Width(25) })) + { + IsExpanded = true; + } + } + else + { + if (GUILayout.Button("^", new GUILayoutOption[] { GUILayout.Width(25) })) + { + IsExpanded = false; + } + } + } + GUILayout.Label($"Vector{VectorSize}: {(string)m_toStringMethod.Invoke(Value, new object[0])}", null); - if (CanWrite) + if (CanWrite && IsExpanded) { GUILayout.EndHorizontal(); var whitespace = window.width - width - 90; diff --git a/src/CppExplorer.cs b/src/CppExplorer.cs index 25714f4..02e7654 100644 --- a/src/CppExplorer.cs +++ b/src/CppExplorer.cs @@ -13,7 +13,7 @@ namespace Explorer public class CppExplorer : MelonMod { public const string NAME = "CppExplorer"; - public const string VERSION = "1.6.4"; + public const string VERSION = "1.6.5"; public const string AUTHOR = "Sinai"; public const string GUID = "com.sinai.cppexplorer"; @@ -50,7 +50,7 @@ namespace Explorer { Instance = this; - InputHelper.CheckInput(); + InputHelper.Init(); new MainMenu(); new WindowManager(); diff --git a/src/CppExplorer.csproj b/src/CppExplorer.csproj index 6866327..62262cd 100644 --- a/src/CppExplorer.csproj +++ b/src/CppExplorer.csproj @@ -2,7 +2,7 @@ - Release + Debug AnyCPU {B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D} Library @@ -24,6 +24,7 @@ false + ..\..\..\..\..\Steam\steamapps\common\Hellpoint\MelonLoader\Managed\Il2Cppmscorlib.dll False @@ -46,7 +47,6 @@ ..\..\..\..\..\Steam\steamapps\common\Hellpoint\MelonLoader\Managed\UnhollowerBaseLib.dll False - ..\..\..\Steam\steamapps\common\Hellpoint\MelonLoader\Managed\UnityEngine.dll False diff --git a/src/CppExplorer.sln b/src/CppExplorer.sln index fd5cdd3..67f89ef 100644 --- a/src/CppExplorer.sln +++ b/src/CppExplorer.sln @@ -10,8 +10,8 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release|Any CPU.Build.0 = Release|Any CPU + {B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release|Any CPU.ActiveCfg = Debug|Any CPU + {B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release|Any CPU.Build.0 = Debug|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/Helpers/InputHelper.cs b/src/Helpers/InputHelper.cs index e38a83b..c38e43c 100644 --- a/src/Helpers/InputHelper.cs +++ b/src/Helpers/InputHelper.cs @@ -15,93 +15,106 @@ namespace Explorer /// public static class InputHelper { - public static void CheckInput() - { - if (Input == null) - { - MelonLogger.Log("UnityEngine.Input is null, trying to load manually...."); + // If Input module failed to load at all + public static bool NO_INPUT; - if ((TryLoad("UnityEngine.InputLegacyModule.dll") || TryLoad("UnityEngine.CoreModule.dll")) && Input != null) - { - MelonLogger.Log("Ok!"); - } - else - { - MelonLogger.Log("Could not load Input module!"); - } - - bool TryLoad(string module) - { - var path = $@"MelonLoader\Managed\{module}"; - if (!File.Exists(path)) return false; - - try - { - Assembly.Load(File.ReadAllBytes(path)); - return true; - } - catch (Exception e) - { - MelonLogger.Log(e.GetType() + ", " + e.Message); - return false; - } - } - } - } - - public static Type Input => _input ?? (_input = ReflectionHelpers.GetTypeByName("UnityEngine.Input")); + // Base UnityEngine.Input class + private static Type Input => _input ?? (_input = ReflectionHelpers.GetTypeByName("UnityEngine.Input")); private static Type _input; - private static PropertyInfo MousePosInfo => _mousePosition ?? (_mousePosition = Input?.GetProperty("mousePosition")); + // Cached member infos private static PropertyInfo _mousePosition; - - private static MethodInfo GetKeyInfo => _getKey ?? (_getKey = Input?.GetMethod("GetKey", new Type[] { typeof(KeyCode) })); private static MethodInfo _getKey; - - private static MethodInfo GetKeyDownInfo => _getKeyDown ?? (_getKeyDown = Input?.GetMethod("GetKeyDown", new Type[] { typeof(KeyCode) })); private static MethodInfo _getKeyDown; - - private static MethodInfo GetMouseButtonInfo => _getMouseButton ?? (_getMouseButton = Input?.GetMethod("GetMouseButton", new Type[] { typeof(int) })); private static MethodInfo _getMouseButton; - - private static MethodInfo GetMouseButtonDownInfo => _getMouseButtonDown ?? (_getMouseButtonDown = Input?.GetMethod("GetMouseButtonDown", new Type[] { typeof(int) })); private static MethodInfo _getMouseButtonDown; + public static void Init() + { + if (Input == null && !TryManuallyLoadInput()) + { + NO_INPUT = true; + return; + } + + // Cache reflection now that we know Input is loaded + + _mousePosition = Input.GetProperty("mousePosition"); + + _getKey = Input.GetMethod("GetKey", new Type[] { typeof(KeyCode) }); + _getKeyDown = Input.GetMethod("GetKeyDown", new Type[] { typeof(KeyCode) }); + _getMouseButton = Input.GetMethod("GetMouseButton", new Type[] { typeof(int) }); + _getMouseButtonDown = Input.GetMethod("GetMouseButtonDown", new Type[] { typeof(int) }); + } + #pragma warning disable IDE1006 // Camel-case property (Unity style) public static Vector3 mousePosition { get { - if (Input == null) return Vector3.zero; - return (Vector3)MousePosInfo.GetValue(null); + if (NO_INPUT) return Vector3.zero; + return (Vector3)_mousePosition.GetValue(null); } } #pragma warning restore IDE1006 public static bool GetKeyDown(KeyCode key) { - if (Input == null) return false; - return (bool)GetKeyDownInfo.Invoke(null, new object[] { key }); + if (NO_INPUT) return false; + return (bool)_getKeyDown.Invoke(null, new object[] { key }); } public static bool GetKey(KeyCode key) { - if (Input == null) return false; - return (bool)GetKeyInfo.Invoke(null, new object[] { key }); + if (NO_INPUT) return false; + return (bool)_getKey.Invoke(null, new object[] { key }); } /// 1 = left, 2 = middle, 3 = right, etc public static bool GetMouseButtonDown(int btn) { - if (Input == null) return false; - return (bool)GetMouseButtonDownInfo.Invoke(null, new object[] { btn }); + if (NO_INPUT) return false; + return (bool)_getMouseButtonDown.Invoke(null, new object[] { btn }); } /// 1 = left, 2 = middle, 3 = right, etc public static bool GetMouseButton(int btn) { - if (Input == null) return false; - return (bool)GetMouseButtonInfo.Invoke(null, new object[] { btn }); + if (NO_INPUT) return false; + return (bool)_getMouseButton.Invoke(null, new object[] { btn }); + } + + private static bool TryManuallyLoadInput() + { + MelonLogger.Log("UnityEngine.Input is null, trying to load manually...."); + + if ((TryLoad("UnityEngine.InputLegacyModule.dll") || TryLoad("UnityEngine.CoreModule.dll")) && Input != null) + { + MelonLogger.Log("Ok!"); + return true; + } + else + { + MelonLogger.Log("Could not load Input module!"); + return false; + } + + bool TryLoad(string module) + { + var path = $@"MelonLoader\Managed\{module}"; + if (!File.Exists(path)) return false; + + try + { + Assembly.Load(File.ReadAllBytes(path)); + return true; + } + catch (Exception e) + { + MelonLogger.Log(e.GetType() + ", " + e.Message); + return false; + } + } } } }