From b9a3ab7439cade61bf98b00237c7398a0242c350 Mon Sep 17 00:00:00 2001 From: Sinai <49360850+sinai-dev@users.noreply.github.com> Date: Fri, 4 Feb 2022 18:07:34 +1100 Subject: [PATCH] Fix CSConsole not re-selecting properly after Escape is pressed --- src/CSConsole/ConsoleController.cs | 54 +++++++++++++++++++++++------- src/UnityExplorer.csproj | 4 +-- src/packages.config | 2 +- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/CSConsole/ConsoleController.cs b/src/CSConsole/ConsoleController.cs index b9a7cb3..9c0cfe6 100644 --- a/src/CSConsole/ConsoleController.cs +++ b/src/CSConsole/ConsoleController.cs @@ -18,6 +18,7 @@ using UniverseLib.UI; using UniverseLib; using UniverseLib.UI.Models; using UniverseLib.Utility; +using HarmonyLib; namespace UnityExplorer.CSConsole { @@ -383,30 +384,59 @@ namespace UnityExplorer.CSConsole RuntimeHelper.StartCoroutine(SetCaretCoroutine(caretPosition)); } - internal static PropertyInfo SelectionGuardProperty => selectionGuardPropInfo ?? GetSelectionGuardPropInfo(); + internal static MemberInfo selectionGuardMemberInfo; - private static PropertyInfo GetSelectionGuardPropInfo() + internal static MemberInfo GetSelectionGuardMemberInfo() { - selectionGuardPropInfo = typeof(EventSystem).GetProperty("m_SelectionGuard"); - if (selectionGuardPropInfo == null) - selectionGuardPropInfo = typeof(EventSystem).GetProperty("m_selectionGuard"); - return selectionGuardPropInfo; + if (selectionGuardMemberInfo != null) + return selectionGuardMemberInfo; + + 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"); } - private static PropertyInfo selectionGuardPropInfo; + internal static void SetSelectionGuard(EventSystem instance, bool 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); + } private static IEnumerator SetCaretCoroutine(int caretPosition) { var color = Input.Component.selectionColor; color.a = 0f; Input.Component.selectionColor = color; - try { EventSystem.current.SetSelectedGameObject(null, null); } catch { } - yield return null; - try { SelectionGuardProperty.SetValue(EventSystem.current, false, null); } catch { } - try { EventSystem.current.SetSelectedGameObject(Input.UIRoot, null); } catch { } + try { CursorUnlocker.CurrentEventSystem.m_CurrentSelected = null; } + catch (Exception ex) { ExplorerCore.Log($"Failed removing selected object: {ex}"); } + + yield return null; // ~~~~~~~ YIELD FRAME ~~~~~~~~~ + + try { SetSelectionGuard(CursorUnlocker.CurrentEventSystem, false); } + catch (Exception ex) { ExplorerCore.Log($"Failed setting selection guard: {ex}"); } + + try { CursorUnlocker.CurrentEventSystem.SetSelectedGameObject(Input.GameObject, null); } + catch (Exception ex) { ExplorerCore.Log($"Failed setting selected gameobject: {ex}"); } + + yield return null; // ~~~~~~~ YIELD FRAME ~~~~~~~~~ + Input.Component.Select(); - yield return null; Input.Component.caretPosition = caretPosition; Input.Component.selectionFocusPosition = caretPosition; diff --git a/src/UnityExplorer.csproj b/src/UnityExplorer.csproj index fd4de01..df2d969 100644 --- a/src/UnityExplorer.csproj +++ b/src/UnityExplorer.csproj @@ -175,13 +175,13 @@ False - packages\UniverseLib.1.2.4\lib\net35\UniverseLib.Mono.dll + packages\UniverseLib.1.2.5\lib\net35\UniverseLib.Mono.dll - packages\UniverseLib.1.2.4\lib\net472\UniverseLib.IL2CPP.dll + packages\UniverseLib.1.2.5\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 699c5f8..df67002 100644 --- a/src/packages.config +++ b/src/packages.config @@ -6,6 +6,6 @@ - + \ No newline at end of file