Update count faker to reflect on NCP spawning

This commit is contained in:
2024-08-22 11:30:29 +02:00
parent ee2c825d04
commit 925d27696c

View File

@@ -54,6 +54,7 @@ namespace CykaMod {
} }
} }
// region player faking
[HarmonyPatch(typeof(GameData), "UserCode_CmdOpenSupermarket")] [HarmonyPatch(typeof(GameData), "UserCode_CmdOpenSupermarket")]
public static class PlayersAddedTranspilePatch { public static class PlayersAddedTranspilePatch {
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) { static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) {
@@ -67,6 +68,8 @@ namespace CykaMod {
for (int i = 1; i < codes.Count; i++) { for (int i = 1; i < codes.Count; i++) {
if (previous.opcode == OpCodes.Ldsfld && codes[i].opcode == OpCodes.Callvirt) { if (previous.opcode == OpCodes.Ldsfld && codes[i].opcode == OpCodes.Callvirt) {
Console.WriteLine("Found Ldsfld and Callvirt"); Console.WriteLine("Found Ldsfld and Callvirt");
Console.WriteLine(previous);
Console.WriteLine(codes[i]);
codes.Insert(i + 1, codes.Insert(i + 1,
new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(Main), "playersAdded"))); new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(Main), "playersAdded")));
codes.Insert(i + 2, codes.Insert(i + 2,
@@ -85,4 +88,39 @@ namespace CykaMod {
return codes.AsEnumerable(); return codes.AsEnumerable();
} }
} }
[HarmonyPatch(typeof(NPC_Manager), "SpawnCustomerNCP")]
public static class PlayersAddedTranspilePatch2 {
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) {
var codes = new List<CodeInstruction>(instructions);
// foreach (var codeInstruction in codes) {
// Console.WriteLine(codeInstruction);
// }
CodeInstruction previous = codes[0];
for (int i = 1; i < codes.Count; i++) {
if (previous.opcode == OpCodes.Ldsfld && codes[i].opcode == OpCodes.Callvirt) {
Console.WriteLine("Found Ldsfld and Callvirt:");
Console.WriteLine(previous);
Console.WriteLine(codes[i]);
codes.Insert(i + 1,
new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(Main), "playersAdded")));
codes.Insert(i + 2,
new CodeInstruction(OpCodes.Call,
AccessTools.PropertyGetter(typeof(Main).GetField("playersAdded").FieldType, "Value")));
codes.Insert(i + 3, new CodeInstruction(OpCodes.Add));
}
previous = codes[i];
}
// foreach (var codeInstruction in codes) {
// Console.WriteLine(codeInstruction);
// }
return codes.AsEnumerable();
}
}
// endregion
} }