From 31b209397dfe0fa4dc55df347985570ea0a2a610 Mon Sep 17 00:00:00 2001 From: Sn1p3rr3c0n Date: Sun, 28 Jun 2026 12:31:59 +0200 Subject: [PATCH] Now storing the Recipes correctly again --- .../Patch_ITab_Bills_FillTab.cs | 85 +++++++++++++++++++ .../Assemblers/Building_DynamicBillGiver.cs | 10 +-- .../Assemblers/Building_SmartAssembler.cs | 4 +- 3 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 Source/ProjectRimFactory/Common/HarmonyPatches/Patch_ITab_Bills_FillTab.cs diff --git a/Source/ProjectRimFactory/Common/HarmonyPatches/Patch_ITab_Bills_FillTab.cs b/Source/ProjectRimFactory/Common/HarmonyPatches/Patch_ITab_Bills_FillTab.cs new file mode 100644 index 00000000..d1798424 --- /dev/null +++ b/Source/ProjectRimFactory/Common/HarmonyPatches/Patch_ITab_Bills_FillTab.cs @@ -0,0 +1,85 @@ +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Reflection.Emit; +using HarmonyLib; +using ProjectRimFactory.SAL3.Things.Assemblers; +using RimWorld; +using Verse; + +namespace ProjectRimFactory.Common.HarmonyPatches; + +[HarmonyPatch] +public class Patch_ITab_Bills_FillTab +{ + + static IEnumerable TargetMethods() + { + yield return typeof(ITab_Bills).GetMethods(AccessTools.all).First(m => m.Name == "g__OptionsMaker|10_0"); + yield return AccessTools.Method(typeof(ITab_Bills), "FillTab"); + } + + static IEnumerable Transpiler(IEnumerable instructions) + { + bool foundGetTabel = false; + bool foundDef =false; + CodeInstruction defInstruction = null; + + foreach (var instruction in instructions) + { + // Log.Message($"{instruction.opcode} {instruction.operand}"); + if (instruction.opcode == OpCodes.Call && instruction.operand.ToString() == "RimWorld.Building_WorkTable get_SelTable()") + { + foundGetTabel = true; + yield return instruction; + continue; + } + + if (foundGetTabel && instruction.opcode == OpCodes.Ldfld && + instruction.operand.ToString() == "Verse.ThingDef def") + { + // We Have SelTable().def + foundDef = true; + defInstruction = instruction; + continue; // We will add it back if this is a different case + } + + // Only if SelTable() is directly followed by .def + if (foundGetTabel && !foundDef) + { + foundGetTabel = false; + } + + if (foundDef && foundGetTabel && instruction.opcode == OpCodes.Callvirt + && instruction.operand.ToString() == "System.Collections.Generic.List`1[Verse.RecipeDef] get_AllRecipes()"){ + instruction.operand = AccessTools.Method(typeof(Patch_ITab_Bills_FillTab), "GetAllRecipes", [typeof(Thing)]); + foundDef = false; + foundGetTabel = false; + Log.Message($"Updated: {instruction.opcode} {instruction.operand}"); + yield return instruction; + continue; + } + if (foundDef && foundGetTabel) + { + foundDef = false; + foundGetTabel = false; + yield return defInstruction; + } + + yield return instruction; + } + } + + + public static List GetAllRecipes(Thing thing) + { + if (thing is Building_DynamicBillGiver prfBuilding) + { + return prfBuilding.Recipes; + } + return thing.def.AllRecipes; + } + + + +} \ No newline at end of file diff --git a/Source/ProjectRimFactory/SAL3/Things/Assemblers/Building_DynamicBillGiver.cs b/Source/ProjectRimFactory/SAL3/Things/Assemblers/Building_DynamicBillGiver.cs index aaf1b715..b5726708 100644 --- a/Source/ProjectRimFactory/SAL3/Things/Assemblers/Building_DynamicBillGiver.cs +++ b/Source/ProjectRimFactory/SAL3/Things/Assemblers/Building_DynamicBillGiver.cs @@ -1,7 +1,6 @@ using ProjectRimFactory.Common; using System.Collections.Generic; using System.Linq; -using ProjectRimFactory.SAL3.Tools; using Verse; namespace ProjectRimFactory.SAL3.Things.Assemblers @@ -18,19 +17,20 @@ public override void SpawnSetup(Map map, bool respawningAfterLoad) base.SpawnSetup(map, respawningAfterLoad); RegisterRecipes(); } + + public List Recipes = []; private void RegisterRecipes() { - def.recipes ??= []; + Recipes ??= []; var newRecipes = GetImportedRecipes(); foreach (var recipe in newRecipes) { - if (def.recipes.Contains(recipe)) continue; - def.recipes.Add(recipe); + if (Recipes.Contains(recipe)) continue; + Recipes.Add(recipe); } - ReflectionUtility.AllRecipesCached.SetValue(def, null); } public new bool CurrentlyUsableForBills() => false; diff --git a/Source/ProjectRimFactory/SAL3/Things/Assemblers/Building_SmartAssembler.cs b/Source/ProjectRimFactory/SAL3/Things/Assemblers/Building_SmartAssembler.cs index d7ef0115..2cef69c7 100644 --- a/Source/ProjectRimFactory/SAL3/Things/Assemblers/Building_SmartAssembler.cs +++ b/Source/ProjectRimFactory/SAL3/Things/Assemblers/Building_SmartAssembler.cs @@ -95,7 +95,7 @@ private void UpdateBills() if (removed.Count > 0) { - def.recipes.RemoveAll(recipe => removed.Contains(recipe)); + Recipes.RemoveAll(recipe => removed.Contains(recipe)); // ALl Bills + possible current BillStack.Bills.RemoveAll(b => removed.Contains(b.recipe)); @@ -113,7 +113,7 @@ private void UpdateBills() if (added.Count > 0) { - def.recipes.AddRange(added); + Recipes.AddRange(added); } if (added.Count > 0 || removed.Count > 0)