From eb443aaa5e01bf06ca4f6efbd23e8cd6abd411cb Mon Sep 17 00:00:00 2001 From: Arys Date: Tue, 30 Jan 2024 09:37:33 +0000 Subject: [PATCH] Fixed BTR movement speed being tied to a static framerate (!73) By multiplying `moveSpeed` by `Time.deltaTime` in `BTRVehicle.Update()`, it prevents the BTR movement from desyncing, and possibly going faster or slower than expected. Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Modules/pulls/73 Co-authored-by: Arys Co-committed-by: Arys --- project/Aki.Custom/AkiCustomPlugin.cs | 3 +- .../BTR/Patches/BTRAppFrameratePatches.cs | 60 ------------------- .../Aki.Custom/BTR/Patches/BTRBotInitPatch.cs | 6 +- .../BTR/Patches/BTRTurretCanShootPatch.cs | 6 +- .../Patches/BTRVehicleMovementSpeedPatch.cs | 22 +++++++ 5 files changed, 27 insertions(+), 70 deletions(-) delete mode 100644 project/Aki.Custom/BTR/Patches/BTRAppFrameratePatches.cs create mode 100644 project/Aki.Custom/BTR/Patches/BTRVehicleMovementSpeedPatch.cs diff --git a/project/Aki.Custom/AkiCustomPlugin.cs b/project/Aki.Custom/AkiCustomPlugin.cs index 954dd88..578096c 100644 --- a/project/Aki.Custom/AkiCustomPlugin.cs +++ b/project/Aki.Custom/AkiCustomPlugin.cs @@ -64,8 +64,7 @@ namespace Aki.Custom new BTRTransferItemsPatch().Enable(); new BTREndRaidItemDeliveryPatch().Enable(); new BTRDestroyAtRaidEndPatch().Enable(); - new BTRAppFrameratePatches.VehicleBaseInitFpsPatch().Enable(); - new BTRAppFrameratePatches.VehicleBaseResetFpsPatch().Enable(); + new BTRVehicleMovementSpeedPatch().Enable(); } catch (Exception ex) { diff --git a/project/Aki.Custom/BTR/Patches/BTRAppFrameratePatches.cs b/project/Aki.Custom/BTR/Patches/BTRAppFrameratePatches.cs deleted file mode 100644 index 61a14d9..0000000 --- a/project/Aki.Custom/BTR/Patches/BTRAppFrameratePatches.cs +++ /dev/null @@ -1,60 +0,0 @@ -using Aki.Reflection.Patching; -using HarmonyLib; -using System.Reflection; -using UnityEngine; - -namespace Aki.Custom.BTR.Patches -{ - /** - * This class contains two patches because it's the two places that Application.targetFrameRate are used - * and I wanted to keep the patches together - * - * These patches are used to set the target framerate used by vehicle movement calculations to a static - * value, avoiding issues caused by enabling NVidia Reflex. These values are then set back after the methods - * complete - */ - public static class BTRAppFrameratePatches - { - public class VehicleBaseInitFpsPatch : ModulePatch - { - protected override MethodBase GetTargetMethod() - { - return AccessTools.Method(typeof(VehicleBase), nameof(VehicleBase.Initialization)); - } - - [PatchPrefix] - private static void PrefixPatch(out int __state) - { - __state = Application.targetFrameRate; - Application.targetFrameRate = 60; - } - - [PatchPostfix] - private static void PostfixPatch(int __state) - { - Application.targetFrameRate = __state; - } - } - - public class VehicleBaseResetFpsPatch : ModulePatch - { - protected override MethodBase GetTargetMethod() - { - return AccessTools.Method(typeof(VehicleBase), nameof(VehicleBase.Reset)); - } - - [PatchPrefix] - private static void PrefixPatch(out int __state) - { - __state = Application.targetFrameRate; - Application.targetFrameRate = 60; - } - - [PatchPostfix] - private static void PostfixPatch(int __state) - { - Application.targetFrameRate = __state; - } - } - } -} diff --git a/project/Aki.Custom/BTR/Patches/BTRBotInitPatch.cs b/project/Aki.Custom/BTR/Patches/BTRBotInitPatch.cs index 06b0779..61d51a6 100644 --- a/project/Aki.Custom/BTR/Patches/BTRBotInitPatch.cs +++ b/project/Aki.Custom/BTR/Patches/BTRBotInitPatch.cs @@ -26,7 +26,7 @@ namespace Aki.Custom.BTR.Patches } [PatchPrefix] - private static bool PatchPrefix(BTRTurretView __instance, int btrBotId, ref bool __result) + private static bool PatchPrefix(ref ValueTuple ___valueTuple_0, int btrBotId, ref bool __result) { var gameWorld = Singleton.Instance; if (gameWorld == null) @@ -75,9 +75,7 @@ namespace Aki.Custom.BTR.Patches } } - var tuple = new ValueTuple(new ObservedPlayerView(), true); - var btrTurretViewTupleField = AccessTools.Field(__instance.GetType(), "valueTuple_0"); - btrTurretViewTupleField.SetValue(__instance, tuple); + ___valueTuple_0 = new ValueTuple(new ObservedPlayerView(), true); __result = true; return false; diff --git a/project/Aki.Custom/BTR/Patches/BTRTurretCanShootPatch.cs b/project/Aki.Custom/BTR/Patches/BTRTurretCanShootPatch.cs index 80ab3bf..12c8580 100644 --- a/project/Aki.Custom/BTR/Patches/BTRTurretCanShootPatch.cs +++ b/project/Aki.Custom/BTR/Patches/BTRTurretCanShootPatch.cs @@ -14,11 +14,9 @@ namespace Aki.Custom.BTR.Patches } [PatchPrefix] - private static bool PatchPrefix(BTRTurretServer __instance) + private static bool PatchPrefix(BTRTurretServer __instance, Transform ___defaultTargetTransform) { - Transform defaultTargetTransform = (Transform)AccessTools.Field(__instance.GetType(), "defaultTargetTransform").GetValue(__instance); - - bool flag = __instance.targetTransform != null && __instance.targetTransform != defaultTargetTransform; + bool flag = __instance.targetTransform != null && __instance.targetTransform != ___defaultTargetTransform; bool flag2 = __instance.method_2(); bool flag3 = __instance.targetPosition != __instance.defaultAimingPosition; diff --git a/project/Aki.Custom/BTR/Patches/BTRVehicleMovementSpeedPatch.cs b/project/Aki.Custom/BTR/Patches/BTRVehicleMovementSpeedPatch.cs new file mode 100644 index 0000000..0bf2b59 --- /dev/null +++ b/project/Aki.Custom/BTR/Patches/BTRVehicleMovementSpeedPatch.cs @@ -0,0 +1,22 @@ +using Aki.Reflection.Patching; +using EFT.Vehicle; +using HarmonyLib; +using System.Reflection; +using UnityEngine; + +namespace Aki.Custom.BTR.Patches +{ + public class BTRVehicleMovementSpeedPatch : ModulePatch + { + protected override MethodBase GetTargetMethod() + { + return AccessTools.Method(typeof(BTRVehicle), nameof(BTRVehicle.Update)); + } + + [PatchPrefix] + private static void PatchPrefix(ref float ___float_10, float ___moveSpeed) + { + ___float_10 = ___moveSpeed * Time.deltaTime; + } + } +}