From a87821f2ad7b7675fe45073513f1387f2f4355d2 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 23 Aug 2024 12:16:07 +0200 Subject: [PATCH] Implement auto pricer Sets prices to price * multiplier automatically --- .../SupermarketTogether/AutoPricer.cs | 137 ++++++++++++++++-- .../SupermarketTogether/Class1.cs | 2 + 2 files changed, 125 insertions(+), 14 deletions(-) diff --git a/Projects/SupermarketTogether/SupermarketTogether/AutoPricer.cs b/Projects/SupermarketTogether/SupermarketTogether/AutoPricer.cs index 06de78f..8059184 100644 --- a/Projects/SupermarketTogether/SupermarketTogether/AutoPricer.cs +++ b/Projects/SupermarketTogether/SupermarketTogether/AutoPricer.cs @@ -1,25 +1,134 @@ using System; -using System.Collections.Generic; -using System.Linq; using HarmonyLib; +using UnityEngine; namespace DavesPhatStore { [HarmonyPatch] public class AutoPricer { - [HarmonyTranspiler] - [HarmonyPatch(typeof(PlayerNetwork), "Update")] - static IEnumerable Transpiler(IEnumerable instructions) { - var codes = new List(instructions); + // This does not work as I would like it to + // We'll have to do it differently... + // [HarmonyTranspiler] + // [HarmonyPatch(typeof(PlayerNetwork), "Update")] + // static IEnumerable Transpiler(IEnumerable instructions) { + // Console.WriteLine("Transpiling PlayerNetwork.Update"); + // var codes = new List(instructions); - for (int i = 0; i < codes.Count; i++) { - var codeInstruction = codes[i]; - if (codeInstruction.operand != null) { - Console.WriteLine( - $"'{codeInstruction.opcode}': '{codeInstruction.operand}'/'{codeInstruction.operand.GetType()}'"); - } - } + // for (int i = 0; i < codes.Count; i++) { + // var codeInstruction = codes[i]; + // if (codeInstruction.operand == null) { + // Console.WriteLine($"{i}: '{codeInstruction.opcode}' '{codeInstruction.operand}'"); + // continue; + // } + // Console.WriteLine($"{i}: '{codeInstruction.opcode}' '{codeInstruction.operand}' ('{codeInstruction.operand.GetType()}')"); + // } - return codes.AsEnumerable(); + // int beqLoc = -1; + // int startMove = -1; + // int endMove = -1; + // bool part1 = false; + + // for (int i = 0; i < codes.Count; i++) { + // if (codes[i].opcode == OpCodes.Beq) { + // beqLoc = i; + // break; + // } + // } + + // for (int i = 0; i < codes.Count; i++) { + // if (codes[i].opcode == OpCodes.Ldfld + // && codes[i].operand.ToString() == "System.Single[] tierInflation") { + // startMove = i - 1; + // } + // if (startMove != -1 + // && codes[i].opcode == OpCodes.Stloc_S + // && codes[i].operand.ToString() == "System.Single (24)" + // && codes[i - 1].opcode == OpCodes.Div) { + // endMove = i; + // break; + // } + // } + + // if (beqLoc != -1 && startMove != -1 && endMove != -1) { + // Console.WriteLine($"Moving {endMove - startMove + 1} instructions from {startMove} to {beqLoc}"); + // List blockToMove = codes.GetRange(startMove, endMove - startMove + 1); + // codes.RemoveRange(startMove, endMove - startMove + 1); + // codes.InsertRange(beqLoc, blockToMove); + // part1 = true; + // } + + // if (!part1) { + // Console.WriteLine("Failed mvoing local variables above BEQ, bailing"); + // return new List(instructions); + // } + + // CodeInstruction savedStfld = null; + // CodeInstruction savedLdloc = null; + + // for (int rep = 0; rep < 2; rep++) { + // int start = -1, end = -1; + // for (int i = 2; i < codes.Count; i++) { + // var codeInstruction = codes[i]; + + // if (savedLdloc == null + // && codeInstruction.opcode == OpCodes.Ldloc_S + // && codeInstruction.operand.ToString() == "System.Single (24)") { + // savedLdloc = codeInstruction; + // } + + // if (codeInstruction.opcode == OpCodes.Stfld + // && codeInstruction.operand.ToString() == "System.Single pPrice" + // && codes[i - 1].opcode != OpCodes.Call + // && codes[i - 2].opcode == OpCodes.Ldc_R4) { + // savedStfld = codeInstruction; + // end = i; + // for (int j = i; j > Math.Max(0, i - 20); j--) { + // if (codes[j].opcode == OpCodes.Ldarg_0 + // && codes[j + 1].opcode == OpCodes.Ldarg_0) { + // start = j; + // break; + // } + // } + // } + + // if (start != -1 && end != -1) { + // Console.WriteLine("Start: " + start + ", End: " + end); + // if (savedStfld == null) { + // Console.WriteLine("SavedStfld is null"); + // continue; + // } + // codes.RemoveRange(start, end - start + 1); + // codes.Insert(start, new CodeInstruction(OpCodes.Ldarg_0)); + // codes.Insert(start + 1, savedLdloc); + // codes.Insert(start + 2, new CodeInstruction(OpCodes.Ldc_R4, 2f)); + // codes.Insert(start + 3, new CodeInstruction(OpCodes.Mul)); + // codes.Insert(start + 4, savedStfld); + // Console.WriteLine($"Patched set price to 2x at {start}"); + // break; + // } + // } + // } + + // for (int i = 0; i < codes.Count; i++) { + // var codeInstruction = codes[i]; + // if (codeInstruction.operand == null) { + // Console.WriteLine($"{i}: '{codeInstruction.opcode}' '{codeInstruction.operand}'"); + // continue; + // } + // Console.WriteLine($"{i}: '{codeInstruction.opcode}' '{codeInstruction.operand}' ('{codeInstruction.operand.GetType()}')"); + // } + + // return codes.AsEnumerable(); + // } + + [HarmonyPrefix] + [HarmonyPatch(typeof(ProductListing), nameof(ProductListing.CmdUpdateProductPrice))] + public static void CmdUpdateProductPricePrefix(ref int productID, ref float newPrice) { + Console.WriteLine($"Called CmdUpdateProductPricePrefix with {productID} and {newPrice}"); + var inflationPrice = ProductListing.Instance.tierInflation[ProductListing.Instance.productPrefabs[productID].GetComponent().productTier]; + float realPricePerUnit = ProductListing.Instance.productPrefabs[productID].GetComponent().basePricePerUnit * inflationPrice; + realPricePerUnit = Mathf.Round(realPricePerUnit * 100f) / 100f; + newPrice = realPricePerUnit * Main.setPriceMultiplier.Value; + Console.WriteLine($"Adjusted price to {newPrice} for product {productID}"); } } } \ No newline at end of file diff --git a/Projects/SupermarketTogether/SupermarketTogether/Class1.cs b/Projects/SupermarketTogether/SupermarketTogether/Class1.cs index 5bb3672..e09f8c1 100644 --- a/Projects/SupermarketTogether/SupermarketTogether/Class1.cs +++ b/Projects/SupermarketTogether/SupermarketTogether/Class1.cs @@ -22,6 +22,7 @@ namespace DavesPhatStore { public static ConfigEntry productItemPlaceWait; public static ConfigEntry employeeItemPlaceWait; public static ConfigEntry experienceMultiplier; + public static ConfigEntry setPriceMultiplier; public void Awake() { playersAdded = Config.Bind("General", "PlayersAdded", 0); @@ -32,6 +33,7 @@ namespace DavesPhatStore { productItemPlaceWait = Config.Bind("General", "ProductItemPlaceWait", 0.5f); employeeItemPlaceWait = Config.Bind("General", "EmployeeItemPlaceWait", 0.2f); experienceMultiplier = Config.Bind("General", "ExperienceMultiplier", 0.2f); + setPriceMultiplier = Config.Bind("General", "SetPriceMultiplier", 2f); WaitTimes wt = gameObject.AddComponent(); wt.init();