Refactor player faking

This commit is contained in:
2024-08-23 09:12:28 +02:00
parent 59e94959da
commit a0edd9edec
3 changed files with 77 additions and 67 deletions

View File

@@ -143,72 +143,5 @@ namespace DavesPhatStore {
} }
// region player faking // region player faking
[HarmonyPatch(typeof(GameData), "UserCode_CmdOpenSupermarket")]
public static class PlayersAddedTranspilePatch {
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();
}
}
[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 // endregion
} }

View File

@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using HarmonyLib;
namespace DavesPhatStore {
[HarmonyPatch]
public class PlayerFaking {
[HarmonyTranspiler]
[HarmonyPatch(typeof(GameData), "UserCode_CmdOpenSupermarket")]
static IEnumerable<CodeInstruction> TranspilerOpenMarket(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();
}
[HarmonyTranspiler]
[HarmonyPatch(typeof(NPC_Manager), "SpawnCustomerNCP")]
static IEnumerable<CodeInstruction> TranspilerSpawnNPC(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();
}
}
}

View File

@@ -36,6 +36,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Class1.cs"/> <Compile Include="Class1.cs"/>
<Compile Include="PlayerFaking.cs" />
<Compile Include="Properties\AssemblyInfo.cs"/> <Compile Include="Properties\AssemblyInfo.cs"/>
<Compile Include="WaitTimes.cs" /> <Compile Include="WaitTimes.cs" />
</ItemGroup> </ItemGroup>