Fix CSConsole not re-selecting properly after Escape is pressed

This commit is contained in:
Sinai 2022-02-04 18:07:34 +11:00
parent 03661cdd0b
commit b9a3ab7439
3 changed files with 45 additions and 15 deletions

View File

@ -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;

View File

@ -175,13 +175,13 @@
<Private>False</Private>
</Reference>
<Reference Include="UniverseLib.Mono">
<HintPath>packages\UniverseLib.1.2.4\lib\net35\UniverseLib.Mono.dll</HintPath>
<HintPath>packages\UniverseLib.1.2.5\lib\net35\UniverseLib.Mono.dll</HintPath>
</Reference>
</ItemGroup>
<!-- Il2Cpp refs -->
<ItemGroup Condition="'$(IsCpp)'=='true'">
<Reference Include="UniverseLib.IL2CPP">
<HintPath>packages\UniverseLib.1.2.4\lib\net472\UniverseLib.IL2CPP.dll</HintPath>
<HintPath>packages\UniverseLib.1.2.5\lib\net472\UniverseLib.IL2CPP.dll</HintPath>
</Reference>
<Reference Include="UnhollowerBaseLib, Version=0.4.22.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\Il2CppAssemblyUnhollower.BaseLib.0.4.22\lib\net472\UnhollowerBaseLib.dll</HintPath>

View File

@ -6,6 +6,6 @@
<package id="ILRepack.Lib.MSBuild.Task" version="2.0.18.2" targetFramework="net35" />
<package id="Mono.Cecil" version="0.10.4" targetFramework="net35" />
<package id="Samboy063.Tomlet" version="3.1.3" targetFramework="net472" />
<package id="UniverseLib" version="1.2.4" targetFramework="net35" />
<package id="UniverseLib" version="1.2.5" targetFramework="net35" />
<package id="UniverseLib.Analyzers" version="1.0.3" targetFramework="net35" developmentDependency="true" />
</packages>