From c01bd92c7871567a81633a9ab568d0b367527d60 Mon Sep 17 00:00:00 2001 From: DrakiaXYZ Date: Mon, 29 Jan 2024 19:08:38 +0000 Subject: [PATCH] Fix BTR when NVidia Reflex is enabled (!71) - Before `Application.targetFrameRate` is used in the BTR code, set it to 60. Then set it back afterwards - Remove an unnecessary error from BTRExtractPassengersPatch, not actually an error state All credit goes to Ngst for figuring out that NVidia Reflex was causing the BTR issues Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Modules/pulls/71 Co-authored-by: DrakiaXYZ Co-committed-by: DrakiaXYZ --- project/Aki.Custom/AkiCustomPlugin.cs | 2 + .../BTR/Patches/BTRAppFrameratePatches.cs | 60 +++++++++++++++++++ .../BTR/Patches/BTRExtractPassengersPatch.cs | 1 - 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 project/Aki.Custom/BTR/Patches/BTRAppFrameratePatches.cs diff --git a/project/Aki.Custom/AkiCustomPlugin.cs b/project/Aki.Custom/AkiCustomPlugin.cs index 20b240f..954dd88 100644 --- a/project/Aki.Custom/AkiCustomPlugin.cs +++ b/project/Aki.Custom/AkiCustomPlugin.cs @@ -64,6 +64,8 @@ namespace Aki.Custom new BTRTransferItemsPatch().Enable(); new BTREndRaidItemDeliveryPatch().Enable(); new BTRDestroyAtRaidEndPatch().Enable(); + new BTRAppFrameratePatches.VehicleBaseInitFpsPatch().Enable(); + new BTRAppFrameratePatches.VehicleBaseResetFpsPatch().Enable(); } catch (Exception ex) { diff --git a/project/Aki.Custom/BTR/Patches/BTRAppFrameratePatches.cs b/project/Aki.Custom/BTR/Patches/BTRAppFrameratePatches.cs new file mode 100644 index 0000000..61a14d9 --- /dev/null +++ b/project/Aki.Custom/BTR/Patches/BTRAppFrameratePatches.cs @@ -0,0 +1,60 @@ +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/BTRExtractPassengersPatch.cs b/project/Aki.Custom/BTR/Patches/BTRExtractPassengersPatch.cs index b90bb0b..68e5744 100644 --- a/project/Aki.Custom/BTR/Patches/BTRExtractPassengersPatch.cs +++ b/project/Aki.Custom/BTR/Patches/BTRExtractPassengersPatch.cs @@ -24,7 +24,6 @@ namespace Aki.Custom.BTR.Patches var btrSide = btrManager.LastInteractedBtrSide; if (btrSide == null) { - Logger.LogError($"[AKI-BTR] BTRExtractPassengersPatch - btrSide is null"); return; }