From d67c45023f9b8d24aa7bd98483e3bab803e18b83 Mon Sep 17 00:00:00 2001 From: NotAKidoS <37721153+NotAKidOnSteam@users.noreply.github.com> Date: Fri, 28 Jul 2023 22:29:24 -0500 Subject: [PATCH] [SmoothRay] Fixes for 2023r171 --- SmoothRay/HarmonyPatches.cs | 4 +- SmoothRay/Main.cs | 2 +- SmoothRay/Properties/AssemblyInfo.cs | 5 +- SmoothRay/SmoothRay.cs | 162 ++++++++++++++++----------- SmoothRay/format.json | 14 +-- 5 files changed, 110 insertions(+), 77 deletions(-) diff --git a/SmoothRay/HarmonyPatches.cs b/SmoothRay/HarmonyPatches.cs index 18a4964..858304b 100644 --- a/SmoothRay/HarmonyPatches.cs +++ b/SmoothRay/HarmonyPatches.cs @@ -3,11 +3,11 @@ using HarmonyLib; namespace NAK.SmoothRay.HarmonyPatches; -class PlayerSetupPatches +internal class PlayerSetupPatches { [HarmonyPostfix] [HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.Start))] - static void Post_PlayerSetup_Start(ref PlayerSetup __instance) + private static void Post_PlayerSetup_Start(ref PlayerSetup __instance) { __instance.vrLeftHandTracker.gameObject.AddComponent().ray = __instance.leftRay; __instance.vrRightHandTracker.gameObject.AddComponent().ray = __instance.rightRay; diff --git a/SmoothRay/Main.cs b/SmoothRay/Main.cs index 0637bb5..e3d431d 100644 --- a/SmoothRay/Main.cs +++ b/SmoothRay/Main.cs @@ -36,7 +36,7 @@ public class SmoothRay : MelonMod ApplyPatches(typeof(HarmonyPatches.PlayerSetupPatches)); } - void ApplyPatches(Type type) + private void ApplyPatches(Type type) { try { diff --git a/SmoothRay/Properties/AssemblyInfo.cs b/SmoothRay/Properties/AssemblyInfo.cs index 4a7903c..a4f4227 100644 --- a/SmoothRay/Properties/AssemblyInfo.cs +++ b/SmoothRay/Properties/AssemblyInfo.cs @@ -20,11 +20,14 @@ using System.Reflection; [assembly: MelonGame("Alpha Blend Interactive", "ChilloutVR")] [assembly: MelonPlatform(MelonPlatformAttribute.CompatiblePlatforms.WINDOWS_X64)] [assembly: MelonPlatformDomain(MelonPlatformDomainAttribute.CompatibleDomains.MONO)] +[assembly: MelonColor(255, 220, 130, 5)] +[assembly: MelonAuthorColor(255, 158, 21, 32)] [assembly: HarmonyDontPatchAll] namespace NAK.SmoothRay.Properties; + internal static class AssemblyInfoParams { - public const string Version = "1.0.1"; + public const string Version = "1.0.2"; public const string Author = "NotAKidoS"; } \ No newline at end of file diff --git a/SmoothRay/SmoothRay.cs b/SmoothRay/SmoothRay.cs index 4bc610f..4bd2b4d 100644 --- a/SmoothRay/SmoothRay.cs +++ b/SmoothRay/SmoothRay.cs @@ -23,6 +23,8 @@ **/ using ABI_RC.Core.InteractionSystem; +using ABI_RC.Core.Savior; +using MelonLoader; using UnityEngine; using UnityEngine.Events; using Valve.VR; @@ -31,118 +33,146 @@ namespace NAK.SmoothRay; public class SmoothRayer : MonoBehaviour { + #region Variables + public ControllerRay ray; //settings - bool isEnabled; - bool menuOnly; - float positionSmoothingValue; - float rotationSmoothingValue; - float smallMovementThresholdAngle; + private bool _isEnabled; + private bool _menuOnly; + private float _positionSmoothingValue; + private float _rotationSmoothingValue; + private float _smallMovementThresholdAngle; //internal - Vector3 smoothedPosition = Vector3.zero; - Quaternion smoothedRotation = Quaternion.identity; - float angleVelocitySnap = 1f; + private Vector3 _smoothedPosition = Vector3.zero; + private Quaternion _smoothedRotation = Quaternion.identity; + private float _angleVelocitySnap = 1f; //native & trackedcontrollerfix stuff - SteamVR_Behaviour_Pose pose; - SteamVR_TrackedObject tracked; - SteamVR_Events.Action newPosesAction = null; - - void Start() + private SteamVR_Behaviour_Pose _behaviourPose; + private SteamVR_TrackedObject _trackedObject; + private SteamVR_Events.Action _newPosesAction; + + #endregion + + #region Unity Methods + + private void Start() { - // native CVR - pose = GetComponent(); - if (pose != null) - pose.onTransformUpdatedEvent += OnTransformUpdated; + // Native ChilloutVR - OpenVR + if (TryGetComponent(out _behaviourPose)) + UpdateTransformUpdatedEvent(true); - // trackedcontrollerfix support - tracked = GetComponent(); - if (tracked != null) + // TrackedControllerFix support - OpenVR + if (TryGetComponent(out _trackedObject)) { - newPosesAction = SteamVR_Events.NewPosesAppliedAction(new UnityAction(OnAppliedPoses)); - newPosesAction.enabled = true; + _newPosesAction = SteamVR_Events.NewPosesAppliedAction(new UnityAction(OnAppliedPoses)); + UpdatePosesAction(true); } - foreach (var setting in SmoothRay.Category.Entries) - { + foreach (MelonPreferences_Entry setting in SmoothRay.Category.Entries) setting.OnEntryValueChangedUntyped.Subscribe(OnUpdateSettings); - } OnUpdateSettings(null, null); } - - void OnEnable() + + private void OnEnable() { - smoothedPosition = transform.localPosition; - smoothedRotation = transform.localRotation; + _smoothedPosition = transform.localPosition; + _smoothedRotation = transform.localRotation; // desktopvrswitch support, start handles this for normal use - if (pose != null) - pose.onTransformUpdatedEvent += OnTransformUpdated; - if (tracked != null && newPosesAction != null) - newPosesAction.enabled = true; + UpdateTransformUpdatedEvent(true); + UpdatePosesAction(true); } - void OnDisable() + private void OnDisable() { - smoothedPosition = transform.localPosition; - smoothedRotation = transform.localRotation; - + _smoothedPosition = transform.localPosition; + _smoothedRotation = transform.localRotation; + // desktopvrswitch support, normal use wont run this - if (pose != null) - pose.onTransformUpdatedEvent -= OnTransformUpdated; - if (tracked != null && newPosesAction != null) - newPosesAction.enabled = false; + UpdateTransformUpdatedEvent(false); + UpdatePosesAction(false); } - void OnUpdateSettings(object arg1, object arg2) + #endregion + + #region Private Methods + + private void UpdatePosesAction(bool enable) { - isEnabled = SmoothRay.EntryEnabled.Value; - menuOnly = SmoothRay.EntryMenuOnly.Value; - smallMovementThresholdAngle = SmoothRay.EntrySmallMovementThresholdAngle.Value; + if (enable && CheckVR.Instance.forceOpenXr) + return; + + if (_trackedObject != null && _newPosesAction != null) + _newPosesAction.enabled = enable; + } + + private void UpdateTransformUpdatedEvent(bool enable) + { + if (enable && CheckVR.Instance.forceOpenXr) + return; + + if (_behaviourPose == null) + return; + + if (enable) + _behaviourPose.onTransformUpdatedEvent += OnTransformUpdated; + else + _behaviourPose.onTransformUpdatedEvent -= OnTransformUpdated; + } + + private void OnUpdateSettings(object arg1, object arg2) + { + _isEnabled = SmoothRay.EntryEnabled.Value; + _menuOnly = SmoothRay.EntryMenuOnly.Value; + _smallMovementThresholdAngle = SmoothRay.EntrySmallMovementThresholdAngle.Value; // dont let value hit 0, itll freeze controllers - positionSmoothingValue = Math.Max(20f - Mathf.Clamp(SmoothRay.EntryPositionSmoothing.Value, 0f, 20f), 0.1f); - rotationSmoothingValue = Math.Max(20f - Mathf.Clamp(SmoothRay.EntryRotationSmoothing.Value, 0f, 20f), 0.1f); + _positionSmoothingValue = Math.Max(20f - Mathf.Clamp(SmoothRay.EntryPositionSmoothing.Value, 0f, 20f), 0.1f); + _rotationSmoothingValue = Math.Max(20f - Mathf.Clamp(SmoothRay.EntryRotationSmoothing.Value, 0f, 20f), 0.1f); } - void OnAppliedPoses() => SmoothTransform(); - void OnTransformUpdated(SteamVR_Behaviour_Pose pose, SteamVR_Input_Sources inputSource) => SmoothTransform(); - - void SmoothTransform() + private void OnAppliedPoses() => SmoothTransform(); + + private void OnTransformUpdated(SteamVR_Behaviour_Pose pose, SteamVR_Input_Sources inputSource) => SmoothTransform(); + + private void SmoothTransform() { - if (isEnabled && ray.lineRenderer != null && ray.lineRenderer.enabled) + if (_isEnabled && ray.lineRenderer != null && ray.lineRenderer.enabled) { - if (menuOnly && (!ray.uiActive || (ray.hitTransform != ViewManager.Instance.transform && ray.hitTransform != CVR_MenuManager.Instance.quickMenu.transform))) + if (_menuOnly && (!ray.uiActive || (ray.hitTransform != ViewManager.Instance.transform && ray.hitTransform != CVR_MenuManager.Instance.quickMenu.transform))) { return; } - var angDiff = Quaternion.Angle(smoothedRotation, transform.localRotation); - angleVelocitySnap = Mathf.Min(angleVelocitySnap + angDiff, 90f); + var angDiff = Quaternion.Angle(_smoothedRotation, transform.localRotation); + _angleVelocitySnap = Mathf.Min(_angleVelocitySnap + angDiff, 90f); - var snapMulti = Mathf.Clamp(angleVelocitySnap / smallMovementThresholdAngle, 0.1f, 2.5f); + var snapMulti = Mathf.Clamp(_angleVelocitySnap / _smallMovementThresholdAngle, 0.1f, 2.5f); - if (angleVelocitySnap > 0.1f) - angleVelocitySnap -= Mathf.Max(0.4f, angleVelocitySnap / 1.7f); + if (_angleVelocitySnap > 0.1f) + _angleVelocitySnap -= Mathf.Max(0.4f, _angleVelocitySnap / 1.7f); - if (positionSmoothingValue < 20f) + if (_positionSmoothingValue < 20f) { - smoothedPosition = Vector3.Lerp(smoothedPosition, transform.localPosition, positionSmoothingValue * Time.deltaTime * snapMulti); - transform.localPosition = smoothedPosition; + _smoothedPosition = Vector3.Lerp(_smoothedPosition, transform.localPosition, _positionSmoothingValue * Time.deltaTime * snapMulti); + transform.localPosition = _smoothedPosition; } - if (rotationSmoothingValue < 20f) + if (_rotationSmoothingValue < 20f) { - smoothedRotation = Quaternion.Lerp(smoothedRotation, transform.localRotation, rotationSmoothingValue * Time.deltaTime * snapMulti); - transform.localRotation = smoothedRotation; + _smoothedRotation = Quaternion.Lerp(_smoothedRotation, transform.localRotation, _rotationSmoothingValue * Time.deltaTime * snapMulti); + transform.localRotation = _smoothedRotation; } } else { - smoothedPosition = transform.localPosition; - smoothedRotation = transform.localRotation; + _smoothedPosition = transform.localPosition; + _smoothedRotation = transform.localRotation; } } + + #endregion } \ No newline at end of file diff --git a/SmoothRay/format.json b/SmoothRay/format.json index 57b3017..1168caf 100644 --- a/SmoothRay/format.json +++ b/SmoothRay/format.json @@ -1,12 +1,12 @@ { - "_id": -1, + "_id": 162, "name": "SmoothRay", - "modversion": "1.0.1", - "gameversion": "2022r170p1", + "modversion": "1.0.2", + "gameversion": "2023r171", "loaderversion": "0.6.1", "modtype": "Mod", "author": "NotAKidoS", - "description": "Smoothes your controller while using the Quick and Main menus to make it easier to navigate.\nThis is a CVR adaptation of a Beat Saber mod: [BeatSaber_SmoothedController](https://github.com/kinsi55/BeatSaber_SmoothedController)\n\nSupport for TrackedControllerFix & DesktopVRSwitch.", + "description": "Smoothes your controller while using the Quick and Main menus to make it easier to navigate.\nThis is a CVR adaptation of a Beat Saber mod: [BeatSaber_SmoothedController](https://github.com/kinsi55/BeatSaber_SmoothedController)\n\nSupport for TrackedControllerFix & DesktopVRSwitch.\n**Only supports OpenVR, not OpenXR.**", "searchtags": [ "vr", "ray", @@ -16,8 +16,8 @@ "requirements": [ "None" ], - "downloadlink": "https://github.com/NotAKidOnSteam/NAK_CVR_Mods/releases/download/r9/SmoothRay.dll", + "downloadlink": "https://github.com/NotAKidOnSteam/NAK_CVR_Mods/releases/download/r16/SmoothRay.dll", "sourcelink": "https://github.com/NotAKidOnSteam/NAK_CVR_Mods/tree/main/SmoothRay/", - "changelog": "Initial CVRMG Release", - "embedcolor": "dc8105" + "changelog": "- Fixes for 2023r171.\n- Prevented from initializing when launching with OpenXR.", + "embedcolor": "#dc8105" } \ No newline at end of file