From a31f19755f1ac604452843cc1ab63987ead77ccc Mon Sep 17 00:00:00 2001 From: DrakiaXYZ Date: Wed, 3 Jan 2024 08:57:57 +0000 Subject: [PATCH] Move setting of gameWorld.LocationId to its own patch (!49) Since gameWorld.LocationId can be used by multiple things (And may be used by more in the future), move it to its own patch instead of as part of the BTR patch. Target `method_3` in `BaseLocalGame` to assign it as close to where a live game would set it Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Modules/pulls/49 Co-authored-by: DrakiaXYZ Co-committed-by: DrakiaXYZ --- project/Aki.Custom/AkiCustomPlugin.cs | 1 + .../Patches/SetLocationIdOnRaidStartPatch.cs | 46 +++++++++++++++++++ project/Aki.Debugging/Patches/BtrTestPatch.cs | 1 - 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 project/Aki.Custom/Patches/SetLocationIdOnRaidStartPatch.cs diff --git a/project/Aki.Custom/AkiCustomPlugin.cs b/project/Aki.Custom/AkiCustomPlugin.cs index 41b7e45..ca0a108 100644 --- a/project/Aki.Custom/AkiCustomPlugin.cs +++ b/project/Aki.Custom/AkiCustomPlugin.cs @@ -45,6 +45,7 @@ namespace Aki.Custom new QTEPatch().Enable(); new PmcFirstAidPatch().Enable(); new SettingsLocationPatch().Enable(); + new SetLocationIdOnRaidStartPatch().Enable(); //new RankPanelPatch().Enable(); new RagfairFeePatch().Enable(); new ScavQuestPatch().Enable(); diff --git a/project/Aki.Custom/Patches/SetLocationIdOnRaidStartPatch.cs b/project/Aki.Custom/Patches/SetLocationIdOnRaidStartPatch.cs new file mode 100644 index 0000000..b979ac4 --- /dev/null +++ b/project/Aki.Custom/Patches/SetLocationIdOnRaidStartPatch.cs @@ -0,0 +1,46 @@ +using Aki.Reflection.Patching; +using Aki.Reflection.Utils; +using EFT; +using System.Linq; +using System.Reflection; +using Comfort.Common; +using System; +using static LocationSettingsClass; + +namespace Aki.Custom.Patches +{ + /// + /// Local games do not set the locationId property like a network game does, `LocationId` is used by various bsg systems + /// e.g. btr/lightkeeper services + /// + public class SetLocationIdOnRaidStartPatch : ModulePatch + { + private static PropertyInfo _locationProperty; + protected override MethodBase GetTargetMethod() + { + Type localGameBaseType = PatchConstants.LocalGameType.BaseType; + + // At this point, gameWorld.MainPlayer isn't set, so we need to use the LocalGame's `Location_0` property + _locationProperty = localGameBaseType.GetProperties(PatchConstants.PrivateFlags).Single(x => x.PropertyType == typeof(Location)); + + // Find the TimeAndWeatherSettings handling method + return localGameBaseType.GetMethods(PatchConstants.PrivateFlags).SingleOrDefault(m => IsTargetMethod(m)); + } + + private static bool IsTargetMethod(MethodInfo mi) + { + // Find method_3(TimeAndWeatherSettings timeAndWeather) + var parameters = mi.GetParameters(); + return (parameters.Length == 1 + && parameters[0].ParameterType == typeof(TimeAndWeatherSettings)); + } + + [PatchPostfix] + private static void PatchPostfix(AbstractGame __instance) + { + var gameWorld = Singleton.Instance; + Location location = _locationProperty.GetValue(__instance) as Location; + gameWorld.LocationId = location.Id; + } + } +} diff --git a/project/Aki.Debugging/Patches/BtrTestPatch.cs b/project/Aki.Debugging/Patches/BtrTestPatch.cs index 4bf1eea..9cb233f 100644 --- a/project/Aki.Debugging/Patches/BtrTestPatch.cs +++ b/project/Aki.Debugging/Patches/BtrTestPatch.cs @@ -26,7 +26,6 @@ namespace Aki.Debugging.Patches } var botGame = Singleton.Instance; - gameWorld.LocationId = gameWorld.MainPlayer.Location; if (gameWorld.BtrController == null) {