0
0
mirror of https://github.com/sp-tarkov/modules.git synced 2025-02-13 09:50:43 -05:00
modules/project/Aki.SinglePlayer/Patches/Progression/OfflineSaveProfilePatch.cs
Terkoiz 337a0733ae Publicized assembly refactor (!58)
Depends on SPT-AKI/SPT-AssemblyTool#3

* Refactored Modules for better consistency and general readability, along with preparing the code for a publicized assembly
* Added `PublicDeclaredFlags` to `PatchConstants` to cover a set of commonly used flags to get methods post-publicizing
* Added a replacement to LINQ's `.Single()` - `.SingleCustom()` which has improved logging to help with debugging Module code. Replaced all `.Single()` usages where applicable
* Replaced most method info fetching with `AccessTools` for consistency and better readability, especially in places where methods were being retrieved by their name anyways

**NOTE:**
As a side effect of publicizing all properties, some property access code such as `Player.Position` will now show "ambiguous reference" errors during compile, due to there being multiple interfaces with the Property name being defined on the class. The way to get around this is to use a cast to an explicit interface
Example:
```cs
Singleton<GameWorld>.Instance.MainPlayer.Position
```
will now need to be
```cs
((IPlayer)Singleton<GameWorld>.Instance.MainPlayer).Position
```

Co-authored-by: Terkoiz <terkoiz@spt.dev>
Reviewed-on: SPT-AKI/Modules#58
Co-authored-by: Terkoiz <terkoiz@noreply.dev.sp-tarkov.com>
Co-committed-by: Terkoiz <terkoiz@noreply.dev.sp-tarkov.com>
2024-01-13 22:08:29 +00:00

78 lines
3.3 KiB
C#

using Aki.Common.Http;
using Aki.Reflection.Patching;
using Aki.Reflection.Utils;
using Aki.SinglePlayer.Models.Progression;
using Aki.SinglePlayer.Utils.Progression;
using Comfort.Common;
using EFT;
using HarmonyLib;
using Newtonsoft.Json;
using System;
using System.Linq;
using System.Reflection;
namespace Aki.SinglePlayer.Patches.Progression
{
/// <summary>
/// After a raid, the client doesn't update the server on what occurred during the raid. To persist loot/quest changes etc we
/// make the client send the active profile to a spt-specific endpoint `/raid/profile/save` where we can update the players profile json
/// </summary>
public class OfflineSaveProfilePatch : ModulePatch
{
private static readonly JsonConverter[] _defaultJsonConverters;
static OfflineSaveProfilePatch()
{
_ = nameof(ClientMetrics.Metrics);
var converterClass = typeof(AbstractGame).Assembly.GetTypes()
.First(t => t.GetField("Converters", BindingFlags.Static | BindingFlags.Public) != null);
_defaultJsonConverters = Traverse.Create(converterClass).Field<JsonConverter[]>("Converters").Value;
}
protected override MethodBase GetTargetMethod()
{
// method_45 - as of 16432
// method_43 - as of 18876
var desiredType = typeof(TarkovApplication);
var desiredMethod = Array.Find(desiredType.GetMethods(PatchConstants.PublicDeclaredFlags), IsTargetMethod);
Logger.LogDebug($"{this.GetType().Name} Type: {desiredType?.Name}");
Logger.LogDebug($"{this.GetType().Name} Method: {desiredMethod?.Name}");
return desiredMethod;
}
private bool IsTargetMethod(MethodInfo arg)
{
var parameters = arg.GetParameters();
return parameters.Length > 4
&& parameters[0]?.Name == "profileId"
&& parameters[1]?.Name == "savageProfile"
&& parameters[2]?.Name == "location"
&& arg.ReturnType == typeof(void);
}
[PatchPrefix]
private static void PatchPrefix(string profileId, RaidSettings ____raidSettings, TarkovApplication __instance, Result<ExitStatus, TimeSpan, ClientMetrics> result)
{
// Get scav or pmc profile based on IsScav value
var profile = (____raidSettings.IsScav)
? PatchConstants.BackEndSession.ProfileOfPet
: PatchConstants.BackEndSession.Profile;
SaveProfileRequest request = new SaveProfileRequest
{
Exit = result.Value0.ToString().ToLowerInvariant(), // Exit player used to leave raid
Profile = profile, // players scav or pmc profile, depending on type of raid they did
Health = Utils.Healing.HealthListener.Instance.CurrentHealth, // Specific limb/effect damage data the player has at end of raid
Insurance = Utils.Insurance.InsuredItemManager.Instance.GetTrackedItems(), // A copy of items insured by player with durability values as of raid end (if item is returned, send it back with correct durability values)
IsPlayerScav = ____raidSettings.IsScav
};
RequestHandler.PutJson("/raid/profile/save", request.ToJson(_defaultJsonConverters.AddItem(new NotesJsonConverter()).ToArray()));
}
}
}