Refactor wait times into their own class

This commit is contained in:
2024-08-23 09:10:45 +02:00
parent 454939d0eb
commit 59e94959da
3 changed files with 102 additions and 81 deletions

View File

@@ -21,9 +21,8 @@ namespace DavesPhatStore {
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 static ConfigEntry<float> 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<WaitTimes>();
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<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_CmdAlterFunds__Single")]
public static class ExperiencePatch {
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.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();
}
}

View File

@@ -37,6 +37,7 @@
<ItemGroup>
<Compile Include="Class1.cs"/>
<Compile Include="Properties\AssemblyInfo.cs"/>
<Compile Include="WaitTimes.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">

View File

@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using HarmonyLib;
using UnityEngine;
namespace DavesPhatStore {
[HarmonyPatch]
public class WaitTimes : MonoBehaviour {
public static List<WeakReference<NPC_Info>> NpcInfos = new List<WeakReference<NPC_Info>>();
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<NPC_Info>(__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");
}
}
}