dancing around issues

This commit is contained in:
NotAKidoS 2023-02-23 03:08:30 -06:00
parent 7403b6c3e7
commit a8b97607e1
2 changed files with 60 additions and 35 deletions

View file

@ -1,5 +1,4 @@
using ABI_RC.Core.Base; using ABI_RC.Core.Player;
using ABI_RC.Core.Player;
using HarmonyLib; using HarmonyLib;
using Valve.VR; using Valve.VR;
@ -7,42 +6,14 @@ namespace NAK.Melons.TrackedControllerFix.HarmonyPatches;
internal class PlayerSetupPatches internal class PlayerSetupPatches
{ {
public static SteamVR_Behaviour_Pose vrLeftHandPose;
public static SteamVR_Behaviour_Pose vrRightHandPose;
public static SteamVR_TrackedObject vrLeftHandTracker;
public static SteamVR_TrackedObject vrRightHandTracker;
[HarmonyPostfix] [HarmonyPostfix]
[HarmonyPatch(typeof(PlayerSetup), "Start")] [HarmonyPatch(typeof(PlayerSetup), "Start")]
private static void Post_PlayerSetup_Start(ref PlayerSetup __instance) private static void Post_PlayerSetup_Start(ref PlayerSetup __instance)
{ {
// Add SteamVR_TrackedObject and get SteamVR_Behaviour_Pose // Add TrackedControllerFix
vrLeftHandTracker = __instance.vrLeftHandTracker.AddComponent<SteamVR_TrackedObject>(); var vrLeftHandTracker = __instance.vrLeftHandTracker.AddComponent<TrackedControllerFix>();
vrRightHandTracker = __instance.vrRightHandTracker.AddComponent<SteamVR_TrackedObject>(); vrLeftHandTracker.inputSource = SteamVR_Input_Sources.LeftHand;
vrLeftHandPose = __instance.vrLeftHandTracker.GetComponent<SteamVR_Behaviour_Pose>(); var vrRightHandTracker = __instance.vrRightHandTracker.AddComponent<TrackedControllerFix>();
vrRightHandPose = __instance.vrRightHandTracker.GetComponent<SteamVR_Behaviour_Pose>(); vrRightHandTracker.inputSource = SteamVR_Input_Sources.RightHand;
vrLeftHandPose.enabled = false;
vrRightHandPose.enabled = false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(PlayerSetup), "SetupAvatarVr")]
private static void Prefix_PlayerSetup_SetupAvatarVr()
{
// This is a super lazy way of doing this...
// but this is the best way to support DesktopVRSwitch & not redo the controller inputs
if (vrLeftHandTracker != null)
{
vrLeftHandPose.enabled = true;
vrLeftHandTracker.SetDeviceIndex(vrLeftHandPose.GetDeviceIndex());
vrLeftHandPose.enabled = false;
}
if (vrRightHandTracker != null)
{
vrRightHandPose.enabled = true;
vrRightHandTracker.SetDeviceIndex(vrRightHandPose.GetDeviceIndex());
vrRightHandPose.enabled = false;
}
} }
} }

View file

@ -0,0 +1,54 @@
using UnityEngine;
using Valve.VR;
namespace NAK.Melons.TrackedControllerFix;
public class TrackedControllerFix : MonoBehaviour
{
public SteamVR_Input_Sources inputSource;
public int deviceIndex;
private SteamVR_TrackedObject trackedObject;
private SteamVR_Behaviour_Pose oldBehaviourPose;
private SteamVR_Action_Pose actionPose = SteamVR_Input.GetAction<SteamVR_Action_Pose>("Pose", false);
private void Start()
{
trackedObject = gameObject.AddComponent<SteamVR_TrackedObject>();
oldBehaviourPose = gameObject.GetComponent<SteamVR_Behaviour_Pose>();
oldBehaviourPose.broadcastDeviceChanges = false; //this fucks us
if (actionPose != null) CheckDeviceIndex();
}
private void OnEnable()
{
if (actionPose != null) actionPose[inputSource].onDeviceConnectedChanged += OnDeviceConnectedChanged;
oldBehaviourPose.enabled = false;
}
private void OnDisable()
{
if (actionPose != null) actionPose[inputSource].onDeviceConnectedChanged -= OnDeviceConnectedChanged;
oldBehaviourPose.enabled = true;
}
private void OnDeviceConnectedChanged(SteamVR_Action_Pose changedAction, SteamVR_Input_Sources changedSource, bool connected)
{
if (actionPose != changedAction) actionPose = changedAction;
if (changedSource != inputSource) return;
CheckDeviceIndex();
}
private void CheckDeviceIndex()
{
if (actionPose[inputSource].active && actionPose[inputSource].deviceIsConnected)
{
int trackedDeviceIndex = (int)actionPose[inputSource].trackedDeviceIndex;
if (deviceIndex != trackedDeviceIndex)
{
deviceIndex = trackedDeviceIndex;
trackedObject.SetDeviceIndex(deviceIndex);
}
}
}
}