From 337134d2b51f62063326806e5720b5c6fb0b81b1 Mon Sep 17 00:00:00 2001 From: NotAKidoS <37721153+NotAKidOnSteam@users.noreply.github.com> Date: Tue, 20 Jun 2023 15:15:42 -0500 Subject: [PATCH] [DesktopVRSwitch] Prevent SteamVR_Behaviour from closing game. Was going to use my own event and switch before quit, but SteamVR_Behaviour was the one doing it the entire time. Very convenient for me. --- DesktopVRSwitch/HarmonyPatches.cs | 18 ++++++++++++++++++ DesktopVRSwitch/Main.cs | 8 +++++++- DesktopVRSwitch/VRModeSwitchDebugger.cs | 15 +++++++++------ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/DesktopVRSwitch/HarmonyPatches.cs b/DesktopVRSwitch/HarmonyPatches.cs index e8bd48f..0e341ff 100644 --- a/DesktopVRSwitch/HarmonyPatches.cs +++ b/DesktopVRSwitch/HarmonyPatches.cs @@ -8,6 +8,7 @@ using HarmonyLib; using NAK.DesktopVRSwitch.Patches; using NAK.DesktopVRSwitch.VRModeTrackers; using UnityEngine; +using Valve.VR; namespace NAK.DesktopVRSwitch.HarmonyPatches; @@ -110,4 +111,21 @@ class CohtmlUISystemPatches // dont return false; } +} + +class SteamVRBehaviourPatches +{ + [HarmonyPrefix] + [HarmonyPatch(typeof(SteamVR_Behaviour), nameof(SteamVR_Behaviour.OnQuit))] + static bool Prefix_SteamVR_Behaviour_OnQuit() + { + if (DesktopVRSwitch.EntrySwitchToDesktopOnExit.Value) + { + // If we don't switch fast enough, SteamVR will force close. + // World Transition might cause issues. Might need to override. + VRModeSwitchManager.Instance?.AttemptSwitch(); + return false; + } + return true; + } } \ No newline at end of file diff --git a/DesktopVRSwitch/Main.cs b/DesktopVRSwitch/Main.cs index d098656..7faab89 100644 --- a/DesktopVRSwitch/Main.cs +++ b/DesktopVRSwitch/Main.cs @@ -32,7 +32,10 @@ public class DesktopVRSwitch : MelonMod Category.CreateEntry("Use Transition on Switch", true, description: "Should the world transition play on VRMode switch?"); public static readonly MelonPreferences_Entry EntryRenderVRGameView = - Category.CreateEntry("Render VR Game View", true, description: "Should the VR view be displayed in the game window?"); + Category.CreateEntry("Render VR Game View", true, description: "Should the VR view be displayed in the game window after VRMode switch?"); + + public static readonly MelonPreferences_Entry EntrySwitchToDesktopOnExit = + Category.CreateEntry("Switch to Desktop on SteamVR Exit", true, description: "Should the game switch to Desktop when SteamVR quits?"); public override void OnInitializeMelon() { @@ -52,6 +55,9 @@ public class DesktopVRSwitch : MelonMod ApplyPatches(typeof(HarmonyPatches.CVRWorldPatches)); // cohtml gamepad handling nuke ApplyPatches(typeof(HarmonyPatches.CohtmlUISystemPatches)); + + // prevent steamvr behaviour from closing game + ApplyPatches(typeof(HarmonyPatches.SteamVRBehaviourPatches)); } public override void OnUpdate() diff --git a/DesktopVRSwitch/VRModeSwitchDebugger.cs b/DesktopVRSwitch/VRModeSwitchDebugger.cs index 72383b0..5f2a7b8 100644 --- a/DesktopVRSwitch/VRModeSwitchDebugger.cs +++ b/DesktopVRSwitch/VRModeSwitchDebugger.cs @@ -5,25 +5,29 @@ namespace NAK.DesktopVRSwitch; class VRModeSwitchDebugger : MonoBehaviour { - private Coroutine _switchCoroutine; - private WaitForSeconds _sleep = new WaitForSeconds(2.5f); + Coroutine _switchCoroutine; + WaitForSeconds _sleep; - private void OnEnable() + void OnEnable() { if (_switchCoroutine == null) + { _switchCoroutine = StartCoroutine(SwitchLoop()); + _sleep = new WaitForSeconds(2f); + } } - private void OnDisable() + void OnDisable() { if (_switchCoroutine != null) { StopCoroutine(_switchCoroutine); _switchCoroutine = null; + _sleep = null; } } - private IEnumerator SwitchLoop() + IEnumerator SwitchLoop() { while (true) { @@ -32,7 +36,6 @@ class VRModeSwitchDebugger : MonoBehaviour VRModeSwitchManager.Instance.AttemptSwitch(); yield return _sleep; } - yield return null; } }