0
0
mirror of https://github.com/sp-tarkov/modules.git synced 2025-02-13 09:50:43 -05:00
modules/project/Aki.Custom/Patches/SetLocationIdOnRaidStartPatch.cs
DrakiaXYZ a31f19755f 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: SPT-AKI/Modules#49
Co-authored-by: DrakiaXYZ <drakiaxyz@noreply.dev.sp-tarkov.com>
Co-committed-by: DrakiaXYZ <drakiaxyz@noreply.dev.sp-tarkov.com>
2024-01-03 08:57:57 +00:00

47 lines
1.7 KiB
C#

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
{
/// <summary>
/// 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
/// </summary>
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<GameWorld>.Instance;
Location location = _locationProperty.GetValue(__instance) as Location;
gameWorld.LocationId = location.Id;
}
}
}