Add patch for manipulating npc wait timers

This commit is contained in:
2024-08-22 20:03:49 +02:00
parent ed67c49c04
commit 92cb8b7ca4

View File

@@ -18,12 +18,78 @@ namespace CykaMod {
public static ConfigEntry<int> employeePerPerk;
public static ConfigEntry<float> employeeSpeedPerk;
public static ConfigEntry<float> employeeExtraMoneyPerk;
public static ConfigEntry<float> productCheckoutWait;
public static ConfigEntry<float> productItemPlaceWait;
public static ConfigEntry<float> employeeItemPlaceWait;
public static List<WeakReference<NPC_Info>> NpcInfos = new List<WeakReference<NPC_Info>>();
public void Awake() {
playersAdded = Config.Bind("General", "PlayersAdded", 0);
employeePerPerk = Config.Bind("General", "EmployeePerPerk", 1);
employeeSpeedPerk = Config.Bind("General", "EmployeeSpeedPerk", 0.2f);
employeeExtraMoneyPerk = Config.Bind("General", "EmployeeExtraMoneyPerk", 0.1f);
productCheckoutWait = Config.Bind("General", "ProductCheckoutWait", 0.75f);
productItemPlaceWait = Config.Bind("General", "ProductItemPlaceWait", 0.5f);
employeeItemPlaceWait = Config.Bind("General", "EmployeeItemPlaceWait", 0.2f);
productCheckoutWait.SettingChanged += (sender, args) => {
Console.WriteLine($"ProductCheckoutWait changed to {productCheckoutWait.Value}");
productCheckoutWait.Value = Math.Max(productCheckoutWait.Value, 0.02f);
int n = 0;
for (var i = 0; i < NpcInfos.Count; i++) {
var npcInfo = GetNpcInfo(i);
if (npcInfo == null) {
NpcInfos.RemoveAt(i);
i--;
continue;
}
n++;
npcInfo.productCheckoutWait = productCheckoutWait.Value;
}
Console.WriteLine($"Updated {n} npc infos productCheckoutWait to {productCheckoutWait.Value}");
};
productItemPlaceWait.SettingChanged += (sender, args) => {
Console.WriteLine($"ProductItemPlaceWait changed to {productItemPlaceWait.Value}");
productItemPlaceWait.Value = Math.Max(productItemPlaceWait.Value, 0.02f);
int n = 0;
for (var i = 0; i < NpcInfos.Count; i++) {
var npcInfo = GetNpcInfo(i);
if (npcInfo == null) {
NpcInfos.RemoveAt(i);
i--;
continue;
}
n++;
npcInfo.productItemPlaceWait = productItemPlaceWait.Value;
}
Console.WriteLine($"Updated {n} npc infos productItemPlaceWait to {productItemPlaceWait.Value}");
};
employeeItemPlaceWait.SettingChanged += (sender, args) => {
Console.WriteLine($"EmployeeItemPlaceWait changed to {employeeItemPlaceWait.Value}");
employeeItemPlaceWait.Value = Math.Max(employeeItemPlaceWait.Value, 0.02f);
int n = 0;
for (var i = 0; i < NpcInfos.Count; i++) {
var npcInfo = GetNpcInfo(i);
if (npcInfo == null) {
NpcInfos.RemoveAt(i);
i--;
continue;
}
n++;
npcInfo.employeeItemPlaceWait = employeeItemPlaceWait.Value;
}
Console.WriteLine($"Updated {n} npc infos employeeItemPlaceWait to {employeeItemPlaceWait.Value}");
};
Logger.LogInfo("Cyka mod loaded");
HarmonyFileLog.Enabled = true;
@@ -32,6 +98,30 @@ namespace CykaMod {
var originalMethods = harmony.GetPatchedMethods();
Logger.LogInfo("Patched " + originalMethods.Count() + " methods");
}
private static NPC_Info GetNpcInfo(int index) {
if (index < 0 || index >= NpcInfos.Count)
return null;
NPC_Info npcInfo;
if (NpcInfos[index].TryGetTarget(out npcInfo)) {
return npcInfo;
}
return null;
}
}
[HarmonyPatch(typeof(NPC_Info), MethodType.Constructor)]
public static class NpcInfoConstructorPatch {
static void Postfix(NPC_Info __instance) {
Main.NpcInfos.Add(new WeakReference<NPC_Info>(__instance));
__instance.productItemPlaceWait = Main.productItemPlaceWait.Value;
__instance.productCheckoutWait = Main.productCheckoutWait.Value;
__instance.employeeItemPlaceWait = Main.employeeItemPlaceWait.Value;
Console.WriteLine(
$"Updated npc info with values: {__instance.productItemPlaceWait}, {__instance.productCheckoutWait}, {__instance.employeeItemPlaceWait}");
}
}
[HarmonyPatch(typeof(GameData), "UserCode_CmdOpenSupermarket")]
@@ -56,7 +146,8 @@ namespace CykaMod {
// Console.WriteLine(codeInstruction.operand.ToString() == "NPC_Manager::extraEmployeeSpeedFactor");
// }
if (codeInstruction.opcode == OpCodes.Ldfld && codeInstruction.operand?.ToString() == "System.Int32 maxEmployees") {
if (codeInstruction.opcode == OpCodes.Ldfld &&
codeInstruction.operand?.ToString() == "System.Int32 maxEmployees") {
Console.WriteLine($"Found {codeInstruction.operand}");
codes[i + 1].opcode = OpCodes.Nop;
codes.Insert(i + 2,
@@ -66,7 +157,8 @@ namespace CykaMod {
AccessTools.PropertyGetter(typeof(Main).GetField("employeePerPerk").FieldType, "Value")));
}
if (codeInstruction.opcode == OpCodes.Ldfld && codeInstruction.operand?.ToString() == "System.Single extraEmployeeSpeedFactor") {
if (codeInstruction.opcode == OpCodes.Ldfld &&
codeInstruction.operand?.ToString() == "System.Single extraEmployeeSpeedFactor") {
Console.WriteLine($"Found {codeInstruction.operand}");
codes[i + 1].opcode = OpCodes.Nop;
codes.Insert(i + 2,
@@ -76,14 +168,16 @@ namespace CykaMod {
AccessTools.PropertyGetter(typeof(Main).GetField("employeeSpeedPerk").FieldType, "Value")));
}
if (codeInstruction.opcode == OpCodes.Ldfld && codeInstruction.operand?.ToString() == "System.Single extraCheckoutMoney") {
if (codeInstruction.opcode == OpCodes.Ldfld &&
codeInstruction.operand?.ToString() == "System.Single extraCheckoutMoney") {
Console.WriteLine($"Found {codeInstruction.operand}");
codes[i + 1].opcode = OpCodes.Nop;
codes.Insert(i + 2,
new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(Main), "employeeExtraMoneyPerk")));
codes.Insert(i + 3,
new CodeInstruction(OpCodes.Call,
AccessTools.PropertyGetter(typeof(Main).GetField("employeeExtraMoneyPerk").FieldType, "Value")));
AccessTools.PropertyGetter(typeof(Main).GetField("employeeExtraMoneyPerk").FieldType,
"Value")));
}
}