From a8b97607e165c0bcfde7e99273c62d611beddd97 Mon Sep 17 00:00:00 2001 From: NotAKidoS <37721153+NotAKidOnSteam@users.noreply.github.com> Date: Thu, 23 Feb 2023 03:08:30 -0600 Subject: [PATCH] dancing around issues --- TrackedControllerFix/HarmonyPatches.cs | 41 +++------------ TrackedControllerFix/TrackedControllerFix.cs | 54 ++++++++++++++++++++ 2 files changed, 60 insertions(+), 35 deletions(-) create mode 100644 TrackedControllerFix/TrackedControllerFix.cs diff --git a/TrackedControllerFix/HarmonyPatches.cs b/TrackedControllerFix/HarmonyPatches.cs index 72434c4..ce8110e 100644 --- a/TrackedControllerFix/HarmonyPatches.cs +++ b/TrackedControllerFix/HarmonyPatches.cs @@ -1,5 +1,4 @@ -using ABI_RC.Core.Base; -using ABI_RC.Core.Player; +using ABI_RC.Core.Player; using HarmonyLib; using Valve.VR; @@ -7,42 +6,14 @@ namespace NAK.Melons.TrackedControllerFix.HarmonyPatches; 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] [HarmonyPatch(typeof(PlayerSetup), "Start")] private static void Post_PlayerSetup_Start(ref PlayerSetup __instance) { - // Add SteamVR_TrackedObject and get SteamVR_Behaviour_Pose - vrLeftHandTracker = __instance.vrLeftHandTracker.AddComponent(); - vrRightHandTracker = __instance.vrRightHandTracker.AddComponent(); - vrLeftHandPose = __instance.vrLeftHandTracker.GetComponent(); - vrRightHandPose = __instance.vrRightHandTracker.GetComponent(); - 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; - } + // Add TrackedControllerFix + var vrLeftHandTracker = __instance.vrLeftHandTracker.AddComponent(); + vrLeftHandTracker.inputSource = SteamVR_Input_Sources.LeftHand; + var vrRightHandTracker = __instance.vrRightHandTracker.AddComponent(); + vrRightHandTracker.inputSource = SteamVR_Input_Sources.RightHand; } } \ No newline at end of file diff --git a/TrackedControllerFix/TrackedControllerFix.cs b/TrackedControllerFix/TrackedControllerFix.cs new file mode 100644 index 0000000..0078e3c --- /dev/null +++ b/TrackedControllerFix/TrackedControllerFix.cs @@ -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("Pose", false); + + private void Start() + { + trackedObject = gameObject.AddComponent(); + oldBehaviourPose = gameObject.GetComponent(); + 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); + } + } + } +} \ No newline at end of file