Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<MethodBase> TargetMethods()
{
yield return typeof(ITab_Bills).GetMethods(AccessTools.all).First(m => m.Name == "<FillTab>g__OptionsMaker|10_0");
yield return AccessTools.Method(typeof(ITab_Bills), "FillTab");
}

static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> 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<RecipeDef> GetAllRecipes(Thing thing)
{
if (thing is Building_DynamicBillGiver prfBuilding)
{
return prfBuilding.Recipes;
}
return thing.def.AllRecipes;
}



}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,19 +17,20 @@ public override void SpawnSetup(Map map, bool respawningAfterLoad)
base.SpawnSetup(map, respawningAfterLoad);
RegisterRecipes();
}

public List<RecipeDef> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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)
Expand Down