From 59e94959da6dbe5470adff1f2477492769b82f67 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 23 Aug 2024 09:10:45 +0200 Subject: [PATCH] Refactor wait times into their own class --- .../SupermarketTogether/Class1.cs | 119 ++++++------------ .../SupermarketTogether.csproj | 1 + .../SupermarketTogether/WaitTimes.cs | 63 ++++++++++ 3 files changed, 102 insertions(+), 81 deletions(-) create mode 100644 Projects/SupermarketTogether/SupermarketTogether/WaitTimes.cs diff --git a/Projects/SupermarketTogether/SupermarketTogether/Class1.cs b/Projects/SupermarketTogether/SupermarketTogether/Class1.cs index 47f2aa6..a801034 100644 --- a/Projects/SupermarketTogether/SupermarketTogether/Class1.cs +++ b/Projects/SupermarketTogether/SupermarketTogether/Class1.cs @@ -21,9 +21,8 @@ namespace DavesPhatStore { public static ConfigEntry productCheckoutWait; public static ConfigEntry productItemPlaceWait; public static ConfigEntry employeeItemPlaceWait; - - public static List> NpcInfos = new List>(); - + public static ConfigEntry experienceMultiplier; + public void Awake() { playersAdded = Config.Bind("General", "PlayersAdded", 0); employeePerPerk = Config.Bind("General", "EmployeePerPerk", 1); @@ -31,65 +30,11 @@ namespace DavesPhatStore { 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); + employeeItemPlaceWait = Config.Bind("General", "EmployeeItemPlaceWait", 0.2f); + experienceMultiplier = Config.Bind("General", "ExperienceMultiplier", 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}"); - }; + WaitTimes wt = gameObject.AddComponent(); + wt.init(); Logger.LogInfo("Cyka mod loaded"); HarmonyFileLog.Enabled = true; @@ -99,28 +44,40 @@ namespace DavesPhatStore { 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(__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_CmdAlterFunds__Single")] + public static class ExperiencePatch { + static IEnumerable Transpiler(IEnumerable instructions) { + var codes = new List(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.Mul && codes[i].opcode == OpCodes.Sub) { + Console.WriteLine("Found Mul and Sub:"); + Console.WriteLine(previous); + Console.WriteLine(codes[i]); + + codes.Insert(i + 1, + new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(Main), "experienceMultiplier"))); + codes.Insert(i + 2, + new CodeInstruction(OpCodes.Call, + AccessTools.PropertyGetter(typeof(Main).GetField("experienceMultiplier").FieldType, "Value"))); + codes.Insert(i + 3, new CodeInstruction(OpCodes.Add)); + } + + previous = codes[i]; + } + + // foreach (var codeInstruction in codes) { + // Console.WriteLine(codeInstruction); + // } + + return codes.AsEnumerable(); } } diff --git a/Projects/SupermarketTogether/SupermarketTogether/SupermarketTogether.csproj b/Projects/SupermarketTogether/SupermarketTogether/SupermarketTogether.csproj index 5569fe7..b0286bc 100644 --- a/Projects/SupermarketTogether/SupermarketTogether/SupermarketTogether.csproj +++ b/Projects/SupermarketTogether/SupermarketTogether/SupermarketTogether.csproj @@ -37,6 +37,7 @@ + diff --git a/Projects/SupermarketTogether/SupermarketTogether/WaitTimes.cs b/Projects/SupermarketTogether/SupermarketTogether/WaitTimes.cs new file mode 100644 index 0000000..93f374b --- /dev/null +++ b/Projects/SupermarketTogether/SupermarketTogether/WaitTimes.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using HarmonyLib; +using UnityEngine; + +namespace DavesPhatStore { + [HarmonyPatch] + public class WaitTimes : MonoBehaviour { + public static List> NpcInfos = new List>(); + + public void init() { + InvokeRepeating(nameof(UpdateNpcs), 1f, 0.5f); + } + + 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; + } + + [HarmonyPostfix] + [HarmonyPatch(typeof(NPC_Info), MethodType.Constructor)] + static void Postfix(NPC_Info __instance) { + NpcInfos.Add(new WeakReference(__instance)); + // This does not work for some reason + // I'm assuming times are updated by the game whenever the npc decides to do something + // __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}"); + } + + public void UpdateNpcs() { + Main.productCheckoutWait.Value = Math.Max(Main.productCheckoutWait.Value, 0.02f); + Main.productItemPlaceWait.Value = Math.Max(Main.productItemPlaceWait.Value, 0.02f); + Main.employeeItemPlaceWait.Value = Math.Max(Main.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.productCheckoutWait = Main.productCheckoutWait.Value; + npcInfo.productItemPlaceWait = Main.productItemPlaceWait.Value; + npcInfo.employeeItemPlaceWait = Main.employeeItemPlaceWait.Value; + } + + Console.WriteLine($"Updated {n} npc infos"); + } + } +} \ No newline at end of file