Refactor wait times into their own class
This commit is contained in:
@@ -21,8 +21,7 @@ namespace DavesPhatStore {
|
|||||||
public static ConfigEntry<float> productCheckoutWait;
|
public static ConfigEntry<float> productCheckoutWait;
|
||||||
public static ConfigEntry<float> productItemPlaceWait;
|
public static ConfigEntry<float> productItemPlaceWait;
|
||||||
public static ConfigEntry<float> employeeItemPlaceWait;
|
public static ConfigEntry<float> employeeItemPlaceWait;
|
||||||
|
public static ConfigEntry<float> experienceMultiplier;
|
||||||
public static List<WeakReference<NPC_Info>> NpcInfos = new List<WeakReference<NPC_Info>>();
|
|
||||||
|
|
||||||
public void Awake() {
|
public void Awake() {
|
||||||
playersAdded = Config.Bind("General", "PlayersAdded", 0);
|
playersAdded = Config.Bind("General", "PlayersAdded", 0);
|
||||||
@@ -32,64 +31,10 @@ namespace DavesPhatStore {
|
|||||||
productCheckoutWait = Config.Bind("General", "ProductCheckoutWait", 0.75f);
|
productCheckoutWait = Config.Bind("General", "ProductCheckoutWait", 0.75f);
|
||||||
productItemPlaceWait = Config.Bind("General", "ProductItemPlaceWait", 0.5f);
|
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) => {
|
WaitTimes wt = gameObject.AddComponent<WaitTimes>();
|
||||||
Console.WriteLine($"ProductCheckoutWait changed to {productCheckoutWait.Value}");
|
wt.init();
|
||||||
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");
|
Logger.LogInfo("Cyka mod loaded");
|
||||||
HarmonyFileLog.Enabled = true;
|
HarmonyFileLog.Enabled = true;
|
||||||
@@ -99,28 +44,40 @@ namespace DavesPhatStore {
|
|||||||
Logger.LogInfo("Patched " + originalMethods.Count() + " methods");
|
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(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));
|
||||||
}
|
}
|
||||||
|
|
||||||
[HarmonyPatch(typeof(NPC_Info), MethodType.Constructor)]
|
previous = codes[i];
|
||||||
public static class NpcInfoConstructorPatch {
|
}
|
||||||
static void Postfix(NPC_Info __instance) {
|
|
||||||
Main.NpcInfos.Add(new WeakReference<NPC_Info>(__instance));
|
// foreach (var codeInstruction in codes) {
|
||||||
__instance.productItemPlaceWait = Main.productItemPlaceWait.Value;
|
// Console.WriteLine(codeInstruction);
|
||||||
__instance.productCheckoutWait = Main.productCheckoutWait.Value;
|
// }
|
||||||
__instance.employeeItemPlaceWait = Main.employeeItemPlaceWait.Value;
|
|
||||||
Console.WriteLine(
|
return codes.AsEnumerable();
|
||||||
$"Updated npc info with values: {__instance.productItemPlaceWait}, {__instance.productCheckoutWait}, {__instance.employeeItemPlaceWait}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -37,6 +37,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Class1.cs"/>
|
<Compile Include="Class1.cs"/>
|
||||||
<Compile Include="Properties\AssemblyInfo.cs"/>
|
<Compile Include="Properties\AssemblyInfo.cs"/>
|
||||||
|
<Compile Include="WaitTimes.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="0Harmony">
|
<Reference Include="0Harmony">
|
||||||
|
|||||||
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user