0
0
mirror of https://github.com/sp-tarkov/modules.git synced 2025-02-13 05:10:45 -05:00

scavprofileloadpatch reworked tobe prefix instead of IL

This commit is contained in:
CWX 2024-07-12 14:49:25 +01:00
parent f90ca3f780
commit cef94b5c40
2 changed files with 17 additions and 64 deletions

View File

@ -14,6 +14,7 @@ namespace SPT.SinglePlayer.Patches.ScavMode
{ {
protected override MethodBase GetTargetMethod() protected override MethodBase GetTargetMethod()
{ {
// Struct324 - 3.10.0
var desiredType = typeof(TarkovApplication) var desiredType = typeof(TarkovApplication)
.GetNestedTypes(PatchConstants.PublicDeclaredFlags) .GetNestedTypes(PatchConstants.PublicDeclaredFlags)
.SingleCustom(x => x.GetField("timeAndWeather") != null .SingleCustom(x => x.GetField("timeAndWeather") != null

View File

@ -13,78 +13,30 @@ namespace SPT.SinglePlayer.Patches.ScavMode
{ {
public class ScavProfileLoadPatch : ModulePatch public class ScavProfileLoadPatch : ModulePatch
{ {
/// <summary>
/// This was changed from an IL Patch,
/// aim is just to replace loaded profile with the Scav profile when creating a game
/// </summary>
/// <returns></returns>
protected override MethodBase GetTargetMethod() protected override MethodBase GetTargetMethod()
{ {
// Struct225 - 20575 return AccessTools.Method(typeof(LocalGame), nameof(LocalGame.smethod_6));
var desiredType = typeof(TarkovApplication)
.GetNestedTypes(PatchConstants.PublicDeclaredFlags)
.SingleCustom(x => x.GetField("timeAndWeather") != null
&& x.GetField("timeHasComeScreenController") != null
&& x.Name.Contains("Struct"));
var desiredMethod = desiredType.GetMethods(PatchConstants.PublicDeclaredFlags)
.FirstOrDefault(x => x.Name == "MoveNext");
Logger.LogDebug($"{this.GetType().Name} Type: {desiredType?.Name}");
Logger.LogDebug($"{this.GetType().Name} Method: {desiredMethod?.Name}");
return desiredMethod;
} }
[PatchTranspiler] [PatchPrefix]
private static IEnumerable<CodeInstruction> PatchTranspile(ILGenerator generator, IEnumerable<CodeInstruction> instructions) public static void PatchPrefix(ref Profile profile, LocalRaidSettings raidSettings)
{ {
var codes = new List<CodeInstruction>(instructions); // check raidsettings to see if its a pmc raid
if (raidSettings.playerSide == ESideType.Pmc)
// Search for code where backend.Session.getProfile() is called.
var searchCode = new CodeInstruction(OpCodes.Callvirt, AccessTools.Method(PatchConstants.BackendProfileInterfaceType, "get_Profile"));
var searchIndex = -1;
for (var i = 0; i < codes.Count; i++)
{ {
if (codes[i].opcode == searchCode.opcode && codes[i].operand == searchCode.operand) Logger.LogInfo("Side was PMC, returning");
{ return;
searchIndex = i;
break;
}
} }
// Patch failed. // if not get scav profile
if (searchIndex == -1) // load that into the profile param
{ Logger.LogInfo("Side was Scav, setting profile");
Logger.LogError($"Patch {MethodBase.GetCurrentMethod()} failed: Could not find reference code."); profile = PatchConstants.BackEndSession.ProfileOfPet;
return instructions;
}
// Move back by 2. This is the start of this method call.
searchIndex -= 2;
var brFalseLabel = generator.DefineLabel();
var brLabel = generator.DefineLabel();
var newCodes = CodeGenerator.GenerateInstructions(new List<Code>()
{
new Code(OpCodes.Ldloc_1),
new Code(OpCodes.Call, typeof(ClientApplication<ISession>), "get_Session"),
new Code(OpCodes.Ldloc_1),
new Code(OpCodes.Ldfld, typeof(TarkovApplication), "_raidSettings"),
new Code(OpCodes.Callvirt, typeof(RaidSettings), "get_IsPmc"),
new Code(OpCodes.Brfalse, brFalseLabel),
new Code(OpCodes.Callvirt, PatchConstants.BackendProfileInterfaceType, "get_Profile"),
new Code(OpCodes.Br, brLabel),
new CodeWithLabel(OpCodes.Callvirt, brFalseLabel, PatchConstants.BackendProfileInterfaceType, "get_ProfileOfPet"),
new CodeWithLabel(OpCodes.Stfld, brLabel, typeof(TarkovApplication).GetNestedTypes(BindingFlags.Public).SingleCustom(IsTargetNestedType), "profile")
});
codes.RemoveRange(searchIndex, 4);
codes.InsertRange(searchIndex, newCodes);
return codes.AsEnumerable();
}
private static bool IsTargetNestedType(System.Type nestedType)
{
return nestedType.GetMethods(PatchConstants.PublicDeclaredFlags)
.Count(x => x.GetParameters().Length == 1 && x.GetParameters()[0].ParameterType == typeof(IResult)) > 0 && nestedType.GetField("savageProfile") != null;
} }
} }
} }