From 944a4d794879e0caf807188d48b10b5bd0d2a6b1 Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Sun, 25 May 2025 14:33:51 +0800 Subject: [PATCH] CheatEnabler: fix a known issue in Immediate Build --- CheatEnabler/Patches/FactoryPatch.cs | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/CheatEnabler/Patches/FactoryPatch.cs b/CheatEnabler/Patches/FactoryPatch.cs index 5ca122a..2428b16 100644 --- a/CheatEnabler/Patches/FactoryPatch.cs +++ b/CheatEnabler/Patches/FactoryPatch.cs @@ -418,6 +418,38 @@ public class FactoryPatch : PatchImpl return matcher.InstructionEnumeration(); } + [HarmonyTranspiler] + [HarmonyPatch(typeof(ConstructionSystem), nameof(ConstructionSystem.AddBuildTargetToModules))] + private static IEnumerable ConstructionSystem_AddBuildTargetToModules_Transpiler(IEnumerable instructions, ILGenerator generator) + { + var matcher = new CodeMatcher(instructions, generator); + // 13 0035 ldarg.0 + // 14 0036 ldfld class Player ConstructionSystem::player + // 15 003B callvirt instance class Mecha Player::get_mecha() + // 16 0040 ldfld float32 Mecha::buildArea + // 17 0045 stloc.0 + matcher.MatchForward(false, + new CodeMatch(OpCodes.Ldarg_0), + new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(ConstructionSystem), nameof(ConstructionSystem.player))), + new CodeMatch(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(Player), nameof(Player.mecha))), + new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(Mecha), nameof(Mecha.buildArea))), + new CodeMatch(ci => ci.IsStloc()) + ); + var labels = matcher.Labels; + matcher.Labels = []; + matcher.Insert( + new CodeInstruction(OpCodes.Ldarg_0).WithLabels(labels), + new CodeInstruction(OpCodes.Ldarg_1), + Transpilers.EmitDelegate((ConstructionSystem constructionSystem, int objId) => + { + var player = constructionSystem.player; + player.factory.BuildFinally(player, objId); + }), + new CodeInstruction(OpCodes.Ret) + ); + return matcher.InstructionEnumeration(); + } + [HarmonyPostfix] [HarmonyPatch(typeof(UXAssist.Functions.PlanetFunctions), nameof(UXAssist.Functions.PlanetFunctions.BuildOrbitalCollectors))] private static void UXAssist_PlanetFunctions_BuildOrbitalCollectors_Postfix()