2024-05-21 19:10:17 +01:00
using SPT.Common.Http ;
using SPT.Reflection.Patching ;
using SPT.Reflection.Utils ;
using SPT.SinglePlayer.Models.Progression ;
using SPT.SinglePlayer.Utils.Progression ;
2023-03-03 18:52:31 +00:00
using Comfort.Common ;
using EFT ;
using HarmonyLib ;
using Newtonsoft.Json ;
using System ;
using System.Linq ;
using System.Reflection ;
2024-05-21 19:10:17 +01:00
namespace SPT.SinglePlayer.Patches.Progression
2023-03-03 18:52:31 +00:00
{
2023-11-22 15:44:13 +00:00
/// <summary>
2024-01-13 22:08:29 +00:00
/// After a raid, the client doesn't update the server on what occurred during the raid. To persist loot/quest changes etc we
2023-11-22 15:44:13 +00:00
/// make the client send the active profile to a spt-specific endpoint `/raid/profile/save` where we can update the players profile json
/// </summary>
2023-03-03 18:52:31 +00:00
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
2024-01-13 22:08:29 +00:00
var desiredType = typeof ( TarkovApplication ) ;
var desiredMethod = Array . Find ( desiredType . GetMethods ( PatchConstants . PublicDeclaredFlags ) , IsTargetMethod ) ;
2023-03-03 18:52:31 +00:00
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
2023-08-05 17:26:09 +00:00
{
2023-11-22 15:44:13 +00:00
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
2024-01-13 22:08:29 +00:00
Health = Utils . Healing . HealthListener . Instance . CurrentHealth , // Specific limb/effect damage data the player has at end of raid
2023-11-22 15:44:13 +00:00
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)
2023-03-03 18:52:31 +00:00
IsPlayerScav = ____raidSettings . IsScav
} ;
2023-10-10 10:58:33 +00:00
RequestHandler . PutJson ( "/raid/profile/save" , request . ToJson ( _defaultJsonConverters . AddItem ( new NotesJsonConverter ( ) ) . ToArray ( ) ) ) ;
2023-03-03 18:52:31 +00:00
}
}
}