1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2025-12-09 02:53:29 +08:00
Files
DSP_Mods/CheatEnabler/WaterPumpPatch.cs
2023-09-28 03:21:46 +08:00

55 lines
1.6 KiB
C#

using System.Collections.Generic;
using System.Reflection.Emit;
using BepInEx.Configuration;
using HarmonyLib;
namespace CheatEnabler;
public static class WaterPumperPatch
{
public static ConfigEntry<bool> Enabled;
private static Harmony _patch;
public static void Init()
{
Enabled.SettingChanged += (_, _) => ValueChanged();
ValueChanged();
}
public static void Uninit()
{
_patch?.UnpatchSelf();
_patch = null;
}
private static void ValueChanged()
{
if (Enabled.Value)
{
_patch ??= Harmony.CreateAndPatchAll(typeof(WaterPumperPatch));
}
else
{
_patch?.UnpatchSelf();
_patch = null;
}
}
[HarmonyTranspiler]
[HarmonyPatch(typeof(BuildTool_BlueprintPaste), "CheckBuildConditions")]
[HarmonyPatch(typeof(BuildTool_Click), "CheckBuildConditions")]
private static IEnumerable<CodeInstruction> BuildTool_CheckBuildConditions_Transpiler(
IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
var matcher = new CodeMatcher(instructions, generator);
matcher.MatchForward(false,
new CodeMatch(instr => instr.opcode == OpCodes.Ldc_I4_S && instr.OperandIs(22))
).Advance(1).MatchForward(false,
new CodeMatch(instr => instr.opcode == OpCodes.Ldc_I4_S && instr.OperandIs(22))
);
matcher.Repeat(codeMatcher =>
{
codeMatcher.SetAndAdvance(OpCodes.Ldc_I4_S, 0);
});
return matcher.InstructionEnumeration();
}
}