mirror of
https://github.com/NotAKidoS/NAK_CVR_Mods.git
synced 2025-09-02 14:29:25 +00:00
i dont rememebr
This commit is contained in:
parent
374ab6c11e
commit
86828a94e2
48 changed files with 1637 additions and 841 deletions
|
@ -1,4 +1,8 @@
|
|||
using System.Collections;
|
||||
#if !PLATFORM_ANDROID
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using ABI_RC.Core.Savior;
|
||||
using Unity.XR.OpenVR;
|
||||
|
@ -6,62 +10,76 @@ using UnityEngine;
|
|||
using UnityEngine.XR.Management;
|
||||
using UnityEngine.XR.OpenXR;
|
||||
using Valve.VR;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace NAK.DesktopVRSwitch;
|
||||
|
||||
internal static class XRHandler
|
||||
namespace ABI_RC.Systems.VRModeSwitch
|
||||
{
|
||||
internal static IEnumerator StartXR()
|
||||
internal static class XRHandler
|
||||
{
|
||||
yield return XRGeneralSettings.Instance.Manager.InitializeLoader();
|
||||
|
||||
if (XRGeneralSettings.Instance.Manager.activeLoader != null)
|
||||
XRGeneralSettings.Instance.Manager.StartSubsystems();
|
||||
else
|
||||
yield return StopXR();
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
internal static IEnumerator StopXR()
|
||||
{
|
||||
if (!XRGeneralSettings.Instance.Manager.isInitializationComplete)
|
||||
yield break;
|
||||
|
||||
// Forces SteamVR to reinitialize SteamVR_Input next switch
|
||||
SteamVR_ActionSet_Manager.DisableAllActionSets();
|
||||
SteamVR_Input.initialized = false;
|
||||
|
||||
// Remove SteamVR behaviour & render
|
||||
UnityEngine.Object.DestroyImmediate(SteamVR_Behaviour.instance.gameObject);
|
||||
SteamVR.enabled = false; // disposes SteamVR
|
||||
Patches.SteamVRNullReferencePatch.DestroySteamVRInstancesImmediate();
|
||||
|
||||
// Disable UnityXR
|
||||
XRGeneralSettings.Instance.Manager.StopSubsystems();
|
||||
XRGeneralSettings.Instance.Manager.DeinitializeLoader();
|
||||
|
||||
// We don't really need to wait a frame on Stop()
|
||||
yield return null;
|
||||
}
|
||||
|
||||
internal static void SwitchLoader()
|
||||
{
|
||||
XRLoader item;
|
||||
|
||||
if (!CheckVR.Instance.forceOpenXr)
|
||||
private static async Task InitializeXRLoader()
|
||||
{
|
||||
item = ScriptableObject.CreateInstance<OpenVRLoader>();
|
||||
DesktopVRSwitch.Logger.Msg("Using XR Loader: SteamVR");
|
||||
EnsureXRLoader();
|
||||
XRGeneralSettings.Instance.Manager.InitializeLoaderSync();
|
||||
await Task.Yield();
|
||||
}
|
||||
else
|
||||
|
||||
internal static async Task StartXR()
|
||||
{
|
||||
item = ScriptableObject.CreateInstance<OpenXRLoader>();
|
||||
DesktopVRSwitch.Logger.Msg("Using XR Loader: OpenXR");
|
||||
await InitializeXRLoader();
|
||||
|
||||
if (XRGeneralSettings.Instance.Manager.activeLoader != null)
|
||||
XRGeneralSettings.Instance.Manager.StartSubsystems();
|
||||
else
|
||||
await StopXR(); // assuming StopXR is now an async method.
|
||||
|
||||
// Await a delay or equivalent method to wait for a frame.
|
||||
await Task.Yield(); // This line is to simulate "waiting for the next frame" in an async way.
|
||||
}
|
||||
|
||||
typeof(XRManagerSettings)
|
||||
.GetField("m_Loaders", BindingFlags.Instance | BindingFlags.NonPublic)
|
||||
?.SetValue(XRGeneralSettings.Instance.Manager, new List<XRLoader> { item });
|
||||
internal static Task StopXR()
|
||||
{
|
||||
if (!XRGeneralSettings.Instance.Manager.isInitializationComplete)
|
||||
return Task.CompletedTask;
|
||||
|
||||
// Forces SteamVR to reinitialize SteamVR_Input next switch
|
||||
SteamVR_ActionSet_Manager.DisableAllActionSets();
|
||||
SteamVR_Input.initialized = false;
|
||||
|
||||
// Remove SteamVR behaviour & render
|
||||
Object.DestroyImmediate(SteamVR_Behaviour.instance.gameObject);
|
||||
SteamVR.enabled = false; // disposes SteamVR
|
||||
|
||||
// Disable UnityXR
|
||||
XRGeneralSettings.Instance.Manager.StopSubsystems();
|
||||
XRGeneralSettings.Instance.Manager.DeinitializeLoader();
|
||||
return Task.CompletedTask;
|
||||
|
||||
// If we need to wait for something specific (like a frame), we use Task.Delay or equivalent.
|
||||
// In this case, it seems like you don't need to wait after stopping XR,
|
||||
// so we don't necessarily need an equivalent to 'yield return null' here.
|
||||
}
|
||||
|
||||
private static void EnsureXRLoader()
|
||||
{
|
||||
Type selectedLoaderType = !CheckVR.Instance.forceOpenXr ? typeof(OpenVRLoader) : typeof(OpenXRLoader);
|
||||
|
||||
// dont do anything if we already have the loader selected
|
||||
if (XRGeneralSettings.Instance.Manager.activeLoaders.Count > 0
|
||||
&& XRGeneralSettings.Instance.Manager.activeLoaders[0].GetType() == selectedLoaderType)
|
||||
return;
|
||||
|
||||
XRLoader newLoaderInstance = (XRLoader)ScriptableObject.CreateInstance(selectedLoaderType);
|
||||
FieldInfo field = typeof(XRManagerSettings).GetField("m_Loaders",
|
||||
BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
if (field == null) return;
|
||||
|
||||
// destroy old loaders, set the new laoder
|
||||
// this should not happen normally, but changing loader during runtime sounds funni
|
||||
if (field.GetValue(XRGeneralSettings.Instance.Manager) is List<XRLoader> currentLoaders)
|
||||
foreach (XRLoader loader in currentLoaders.Where(loader => loader != null)) Object.Destroy(loader);
|
||||
|
||||
field.SetValue(XRGeneralSettings.Instance.Manager, new List<XRLoader> { newLoaderInstance });
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue