Files
se-launcher/CringeLauncher/Patches/PbInstantiatePatch.cs
pas2704 bd626f7a2b
All checks were successful
Build / Compute Version (push) Successful in 6s
Build / Build Nuget package (CringeBootstrap.Abstractions) (push) Successful in 1m30s
Build / Build Nuget package (SharedCringe) (push) Successful in 1m45s
Build / Build Nuget package (NuGet) (push) Successful in 1m47s
Build / Build Nuget package (CringePlugins) (push) Successful in 1m58s
Build / Build Launcher (push) Successful in 2m24s
Fix init when pasting in a programmable block
Improvements for imgui input handling
2025-05-16 22:52:15 -04:00

37 lines
1.2 KiB
C#

using HarmonyLib;
using Sandbox.Game.Entities.Blocks;
using System.Reflection.Emit;
namespace CringeLauncher.Patches;
[HarmonyPatch(typeof(MyProgrammableBlock), "UpdateBeforeSimulation")]
internal static class PbInstantiatePatch
{
[HarmonyTranspiler]
public static List<CodeInstruction> UpdateBeforeSimulationTranspiler(IEnumerable<CodeInstruction> instructions)
{
var list = instructions.ToList();
var index = list.FindIndex(b => b.opcode == OpCodes.Brfalse);
//get second brfase
index = list.FindIndex(index, b => b.opcode == OpCodes.Brfalse);
var jump = new CodeInstruction(list[index])
{
opcode = OpCodes.Brtrue
};
index++;
list.Insert(index++, new CodeInstruction(OpCodes.Ldarg_0));
list.Insert(index++, CodeInstruction.CallClosure(PveMethod));
list.Insert(index, jump);
return list;
}
//IMPORTANT: if you reference CompilingPbs in the transpiler, ModScriptCompilerPatch will cause static init of MyScriptWhitelist, crashing the game
private static bool PveMethod(MyProgrammableBlock block) => ModScriptCompilerPatch.CompilingPbs.Contains(block);
}