From 9e49f09a796f86df887960122dcbedd04d8757a5 Mon Sep 17 00:00:00 2001 From: Sinai <49360850+sinai-dev@users.noreply.github.com> Date: Fri, 4 Feb 2022 20:34:05 +1100 Subject: [PATCH] Bump UniverseLib, fix C# Console issues, add Stop helper --- src/CSConsole/ConsoleController.cs | 80 +++++++++++++++++++----------- src/CSConsole/ScriptInteraction.cs | 5 +- src/UnityExplorer.csproj | 4 +- src/packages.config | 2 +- 4 files changed, 58 insertions(+), 33 deletions(-) diff --git a/src/CSConsole/ConsoleController.cs b/src/CSConsole/ConsoleController.cs index 9c0cfe6..4014cb2 100644 --- a/src/CSConsole/ConsoleController.cs +++ b/src/CSConsole/ConsoleController.cs @@ -19,6 +19,7 @@ using UniverseLib; using UniverseLib.UI.Models; using UniverseLib.Utility; using HarmonyLib; +using UniverseLib.Runtime; namespace UnityExplorer.CSConsole { @@ -61,6 +62,8 @@ namespace UnityExplorer.CSConsole public static void Init() { + InitEventSystemPropertyHandlers(); + // Make sure console is supported on this platform try { @@ -384,37 +387,55 @@ namespace UnityExplorer.CSConsole RuntimeHelper.StartCoroutine(SetCaretCoroutine(caretPosition)); } - internal static MemberInfo selectionGuardMemberInfo; - - internal static MemberInfo GetSelectionGuardMemberInfo() + static void InitEventSystemPropertyHandlers() { - if (selectionGuardMemberInfo != null) - return selectionGuardMemberInfo; + try + { + foreach (var member in typeof(EventSystem).GetMembers(AccessTools.all)) + { + if (member.Name == "m_CurrentSelected") + { + Type backingType; + if (member.MemberType == MemberTypes.Property) + backingType = (member as PropertyInfo).PropertyType; + else + backingType = (member as FieldInfo).FieldType; - if (AccessTools.Property(typeof(EventSystem), "m_SelectionGuard") is PropertyInfo pi_m_SelectionGuard) - return selectionGuardMemberInfo = pi_m_SelectionGuard; - - if (AccessTools.Property(typeof(EventSystem), "m_selectionGuard") is PropertyInfo pi_m_selectionGuard) - return selectionGuardMemberInfo = pi_m_selectionGuard; - - if (AccessTools.Field(typeof(EventSystem), "m_SelectionGuard") is FieldInfo fi_m_SelectionGuard) - return selectionGuardMemberInfo = fi_m_SelectionGuard; - - return selectionGuardMemberInfo = AccessTools.Field(typeof(EventSystem), "m_selectionGuard"); + usingEventSystemDictionaryMembers = ReflectionUtility.IsDictionary(backingType); + break; + } + } + } + catch (Exception ex) + { + ExplorerCore.LogWarning($"Exception checking EventSystem property backing type: {ex}"); + } } - internal static void SetSelectionGuard(EventSystem instance, bool value) + static bool usingEventSystemDictionaryMembers; + + static readonly AmbiguousMemberHandler m_CurrentSelected_Handler_Normal = new("m_CurrentSelected", "m_currentSelected"); + static readonly AmbiguousMemberHandler> m_CurrentSelected_Handler_Dictionary = new("m_CurrentSelected", "m_currentSelected"); + + static readonly AmbiguousMemberHandler m_SelectionGuard_Handler_Normal = new("m_SelectionGuard", "m_selectionGuard"); + static readonly AmbiguousMemberHandler> m_SelectionGuard_Handler_Dictionary = new("m_SelectionGuard", "m_selectionGuard"); + + static void SetCurrentSelectedGameObject(EventSystem instance, GameObject value) { - var member = GetSelectionGuardMemberInfo(); - if (member == null) - return; - if (member is PropertyInfo pi) - { - pi.SetValue(instance, value, null); - return; - } - var fi = member as FieldInfo; - fi.SetValue(instance, value); + instance.SetSelectedGameObject(value); + + if (usingEventSystemDictionaryMembers) + m_CurrentSelected_Handler_Dictionary.GetValue(instance)[0] = value; + else + m_CurrentSelected_Handler_Normal.SetValue(instance, value); + } + + static void SetSelectionGuard(EventSystem instance, bool value) + { + if (usingEventSystemDictionaryMembers) + m_SelectionGuard_Handler_Dictionary.GetValue(instance)[0] = value; + else + m_SelectionGuard_Handler_Normal.SetValue(instance, value); } private static IEnumerator SetCaretCoroutine(int caretPosition) @@ -423,7 +444,7 @@ namespace UnityExplorer.CSConsole color.a = 0f; Input.Component.selectionColor = color; - try { CursorUnlocker.CurrentEventSystem.m_CurrentSelected = null; } + try { SetCurrentSelectedGameObject(CursorUnlocker.CurrentEventSystem, null); } catch (Exception ex) { ExplorerCore.Log($"Failed removing selected object: {ex}"); } yield return null; // ~~~~~~~ YIELD FRAME ~~~~~~~~~ @@ -431,7 +452,7 @@ namespace UnityExplorer.CSConsole try { SetSelectionGuard(CursorUnlocker.CurrentEventSystem, false); } catch (Exception ex) { ExplorerCore.Log($"Failed setting selection guard: {ex}"); } - try { CursorUnlocker.CurrentEventSystem.SetSelectedGameObject(Input.GameObject, null); } + try { SetCurrentSelectedGameObject(CursorUnlocker.CurrentEventSystem, Input.GameObject); } catch (Exception ex) { ExplorerCore.Log($"Failed setting selected gameobject: {ex}"); } yield return null; // ~~~~~~~ YIELD FRAME ~~~~~~~~~ @@ -695,7 +716,8 @@ var x = 5; * Log(obj); - prints a message to the console log * Inspect(obj); - inspect the object with the Inspector * Inspect(someType); - inspect a Type with static reflection - * Start(enumerator); - starts the IEnumerator as a Coroutine + * Start(enumerator); - Coroutine, starts the IEnumerator as a Coroutine, and returns the Coroutine. + * Stop(coroutine); - stop the Coroutine ONLY if it was started with Start(ienumerator). * Copy(obj); - copies the object to the UnityExplorer Clipboard * Paste(); - System.Object, the contents of the Clipboard. * GetUsing(); - prints the current using directives to the console log diff --git a/src/CSConsole/ScriptInteraction.cs b/src/CSConsole/ScriptInteraction.cs index 8580d1a..b13cc24 100644 --- a/src/CSConsole/ScriptInteraction.cs +++ b/src/CSConsole/ScriptInteraction.cs @@ -29,9 +29,12 @@ namespace UnityExplorer.CSConsole public static void Inspect(Type type) => InspectorManager.Inspect(type); - public static void Start(IEnumerator ienumerator) + public static Coroutine Start(IEnumerator ienumerator) => RuntimeHelper.StartCoroutine(ienumerator); + public static void Stop(Coroutine coro) + => RuntimeHelper.StopCoroutine(coro); + public static void Copy(object obj) => ClipboardPanel.Copy(obj); diff --git a/src/UnityExplorer.csproj b/src/UnityExplorer.csproj index df2d969..b7557b0 100644 --- a/src/UnityExplorer.csproj +++ b/src/UnityExplorer.csproj @@ -175,13 +175,13 @@ False - packages\UniverseLib.1.2.5\lib\net35\UniverseLib.Mono.dll + packages\UniverseLib.1.2.6\lib\net35\UniverseLib.Mono.dll - packages\UniverseLib.1.2.5\lib\net472\UniverseLib.IL2CPP.dll + packages\UniverseLib.1.2.6\lib\net472\UniverseLib.IL2CPP.dll packages\Il2CppAssemblyUnhollower.BaseLib.0.4.22\lib\net472\UnhollowerBaseLib.dll diff --git a/src/packages.config b/src/packages.config index df67002..024d6d6 100644 --- a/src/packages.config +++ b/src/packages.config @@ -6,6 +6,6 @@ - + \ No newline at end of file