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

Commit b9a3ab7

Browse files
committed
Fix CSConsole not re-selecting properly after Escape is pressed
1 parent 03661cd commit b9a3ab7

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

src/CSConsole/ConsoleController.cs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using UniverseLib;
1919
using UniverseLib.UI.Models;
2020
using UniverseLib.Utility;
21+
using HarmonyLib;
2122

2223
namespace UnityExplorer.CSConsole
2324
{
@@ -383,30 +384,59 @@ private static void SetCaretPosition(int caretPosition)
383384
RuntimeHelper.StartCoroutine(SetCaretCoroutine(caretPosition));
384385
}
385386

386-
internal static PropertyInfo SelectionGuardProperty => selectionGuardPropInfo ?? GetSelectionGuardPropInfo();
387+
internal static MemberInfo selectionGuardMemberInfo;
387388

388-
private static PropertyInfo GetSelectionGuardPropInfo()
389+
internal static MemberInfo GetSelectionGuardMemberInfo()
389390
{
390-
selectionGuardPropInfo = typeof(EventSystem).GetProperty("m_SelectionGuard");
391-
if (selectionGuardPropInfo == null)
392-
selectionGuardPropInfo = typeof(EventSystem).GetProperty("m_selectionGuard");
393-
return selectionGuardPropInfo;
391+
if (selectionGuardMemberInfo != null)
392+
return selectionGuardMemberInfo;
393+
394+
if (AccessTools.Property(typeof(EventSystem), "m_SelectionGuard") is PropertyInfo pi_m_SelectionGuard)
395+
return selectionGuardMemberInfo = pi_m_SelectionGuard;
396+
397+
if (AccessTools.Property(typeof(EventSystem), "m_selectionGuard") is PropertyInfo pi_m_selectionGuard)
398+
return selectionGuardMemberInfo = pi_m_selectionGuard;
399+
400+
if (AccessTools.Field(typeof(EventSystem), "m_SelectionGuard") is FieldInfo fi_m_SelectionGuard)
401+
return selectionGuardMemberInfo = fi_m_SelectionGuard;
402+
403+
return selectionGuardMemberInfo = AccessTools.Field(typeof(EventSystem), "m_selectionGuard");
394404
}
395405

396-
private static PropertyInfo selectionGuardPropInfo;
406+
internal static void SetSelectionGuard(EventSystem instance, bool value)
407+
{
408+
var member = GetSelectionGuardMemberInfo();
409+
if (member == null)
410+
return;
411+
if (member is PropertyInfo pi)
412+
{
413+
pi.SetValue(instance, value, null);
414+
return;
415+
}
416+
var fi = member as FieldInfo;
417+
fi.SetValue(instance, value);
418+
}
397419

398420
private static IEnumerator SetCaretCoroutine(int caretPosition)
399421
{
400422
var color = Input.Component.selectionColor;
401423
color.a = 0f;
402424
Input.Component.selectionColor = color;
403-
try { EventSystem.current.SetSelectedGameObject(null, null); } catch { }
404-
yield return null;
405425

406-
try { SelectionGuardProperty.SetValue(EventSystem.current, false, null); } catch { }
407-
try { EventSystem.current.SetSelectedGameObject(Input.UIRoot, null); } catch { }
426+
try { CursorUnlocker.CurrentEventSystem.m_CurrentSelected = null; }
427+
catch (Exception ex) { ExplorerCore.Log($"Failed removing selected object: {ex}"); }
428+
429+
yield return null; // ~~~~~~~ YIELD FRAME ~~~~~~~~~
430+
431+
try { SetSelectionGuard(CursorUnlocker.CurrentEventSystem, false); }
432+
catch (Exception ex) { ExplorerCore.Log($"Failed setting selection guard: {ex}"); }
433+
434+
try { CursorUnlocker.CurrentEventSystem.SetSelectedGameObject(Input.GameObject, null); }
435+
catch (Exception ex) { ExplorerCore.Log($"Failed setting selected gameobject: {ex}"); }
436+
437+
yield return null; // ~~~~~~~ YIELD FRAME ~~~~~~~~~
438+
408439
Input.Component.Select();
409-
yield return null;
410440

411441
Input.Component.caretPosition = caretPosition;
412442
Input.Component.selectionFocusPosition = caretPosition;

src/UnityExplorer.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@
175175
<Private>False</Private>
176176
</Reference>
177177
<Reference Include="UniverseLib.Mono">
178-
<HintPath>packages\UniverseLib.1.2.4\lib\net35\UniverseLib.Mono.dll</HintPath>
178+
<HintPath>packages\UniverseLib.1.2.5\lib\net35\UniverseLib.Mono.dll</HintPath>
179179
</Reference>
180180
</ItemGroup>
181181
<!-- Il2Cpp refs -->
182182
<ItemGroup Condition="'$(IsCpp)'=='true'">
183183
<Reference Include="UniverseLib.IL2CPP">
184-
<HintPath>packages\UniverseLib.1.2.4\lib\net472\UniverseLib.IL2CPP.dll</HintPath>
184+
<HintPath>packages\UniverseLib.1.2.5\lib\net472\UniverseLib.IL2CPP.dll</HintPath>
185185
</Reference>
186186
<Reference Include="UnhollowerBaseLib, Version=0.4.22.0, Culture=neutral, processorArchitecture=MSIL">
187187
<HintPath>packages\Il2CppAssemblyUnhollower.BaseLib.0.4.22\lib\net472\UnhollowerBaseLib.dll</HintPath>

src/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
<package id="ILRepack.Lib.MSBuild.Task" version="2.0.18.2" targetFramework="net35" />
77
<package id="Mono.Cecil" version="0.10.4" targetFramework="net35" />
88
<package id="Samboy063.Tomlet" version="3.1.3" targetFramework="net472" />
9-
<package id="UniverseLib" version="1.2.4" targetFramework="net35" />
9+
<package id="UniverseLib" version="1.2.5" targetFramework="net35" />
1010
<package id="UniverseLib.Analyzers" version="1.0.3" targetFramework="net35" developmentDependency="true" />
1111
</packages>

0 commit comments

Comments
 (0)