Handling of "VR <-> Desktop" switch, attempt one

This commit is contained in:
SDraw 2024-01-29 23:11:01 +03:00
parent c2a219dfa3
commit 66b448a2c3
No known key found for this signature in database
GPG key ID: BB95B4DAB2BB8BB5
20 changed files with 356 additions and 77 deletions

View file

@ -1,5 +1,6 @@
using ABI_RC.Core.Networking.IO.Social;
using ABI_RC.Core.Player;
using ABI_RC.Systems.IK;
using ABI_RC.Systems.Movement;
using System;
using System.Collections.Generic;
@ -33,6 +34,11 @@ namespace ml_pmc
null,
new HarmonyLib.HarmonyMethod(typeof(PlayerMovementCopycat).GetMethod(nameof(OnSetupAvatar_Postfix), BindingFlags.Static | BindingFlags.NonPublic))
);
HarmonyInstance.Patch(
typeof(IKSystem).GetMethod(nameof(IKSystem.ReinitializeAvatar), BindingFlags.Instance | BindingFlags.Public),
null,
new HarmonyLib.HarmonyMethod(typeof(PlayerMovementCopycat).GetMethod(nameof(OnAvatarReinitialize_Postfix), BindingFlags.Static | BindingFlags.NonPublic))
);
MelonLoader.MelonCoroutines.Start(WaitForLocalPlayer());
}
@ -133,5 +139,19 @@ namespace ml_pmc
MelonLoader.MelonLogger.Error(e);
}
}
static void OnAvatarReinitialize_Postfix() => ms_instance?.OnAvatarReinitialize();
void OnAvatarReinitialize()
{
try
{
if(m_localCopycat != null)
m_localCopycat.OnAvatarReinitialize();
}
catch(System.Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}
}
}

View file

@ -3,6 +3,7 @@ using ABI_RC.Systems.IK;
using ABI_RC.Systems.IK.SubSystems;
using ABI_RC.Systems.InputManagement;
using ABI_RC.Systems.Movement;
using ABI_RC.Systems.VRModeSwitch;
using RootMotion.FinalIK;
using UnityEngine;
@ -36,6 +37,9 @@ namespace ml_pmc
{
if(Instance == null)
Instance = this;
VRModeSwitchEvents.OnInitializeXR.AddListener(this.OnSwitchToVR);
VRModeSwitchEvents.OnDeinitializeXR.AddListener(this.OnSwitchToDesktop);
}
void OnDestroy()
{
@ -52,6 +56,9 @@ namespace ml_pmc
m_animator = null;
m_vrIk = null;
m_lookAtIk = null;
VRModeSwitchEvents.OnInitializeXR.RemoveListener(this.OnSwitchToVR);
VRModeSwitchEvents.OnDeinitializeXR.RemoveListener(this.OnSwitchToDesktop);
}
// Unity events
@ -177,8 +184,6 @@ namespace ml_pmc
}
m_active = false;
m_inVr = Utils.IsInVR();
if(m_puppetParser != null)
Object.Destroy(m_puppetParser);
m_puppetParser = null;
@ -220,6 +225,33 @@ namespace ml_pmc
else
m_animator = null;
}
internal void OnAvatarReinitialize()
{
// Old VRIK and LookAtIK are destroyed by game
m_vrIk = PlayerSetup.Instance._avatar.GetComponent<VRIK>();
m_lookAtIk = PlayerSetup.Instance._avatar.GetComponent<LookAtIK>();
if(m_vrIk != null)
{
m_vrIk.onPreSolverUpdate.AddListener(this.OnVRIKPreUpdate);
m_vrIk.onPostSolverUpdate.AddListener(this.OnVRIKPostUpdate);
}
if(m_lookAtIk != null)
{
m_lookAtIk.onPreSolverUpdate.AddListener(this.OnLookAtIKPreUpdate);
m_lookAtIk.onPostSolverUpdate.AddListener(this.OnLookAtIKPostUpdate);
}
}
void OnSwitchToVR()
{
m_inVr = true;
}
void OnSwitchToDesktop()
{
m_inVr = false;
}
// IK updates
void OnVRIKPreUpdate()

View file

@ -16,7 +16,7 @@ namespace ml_pmc
};
static readonly int[] ms_centralMuscles = new int[] { 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 18, 20 };
public static bool IsInVR() => ((CheckVR.Instance != null) && CheckVR.Instance.hasVrDeviceLoaded);
public static bool IsInVR() => ((MetaPort.Instance != null) && MetaPort.Instance.isUsingVr);
public static bool AreKnucklesInUse() => ((CVRInputManager.Instance._leftController == ABI_RC.Systems.InputManagement.XR.eXRControllerType.Index) || (CVRInputManager.Instance._rightController == ABI_RC.Systems.InputManagement.XR.eXRControllerType.Index));
public static bool IsWorldSafe() => ((CVRWorld.Instance != null) && CVRWorld.Instance.allowFlying);