From 2745fa36dbe5b6bf31f026121283312f3d47f0c9 Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Fri, 10 Feb 2023 03:04:51 +0800 Subject: [PATCH] WIP for MechaDronesTweaks --- MechaDronesTweaks/MechaDronesTweaks.cs | 135 ++++++++++++++++++++++++- 1 file changed, 132 insertions(+), 3 deletions(-) diff --git a/MechaDronesTweaks/MechaDronesTweaks.cs b/MechaDronesTweaks/MechaDronesTweaks.cs index 4d7e42d..ad01d3a 100644 --- a/MechaDronesTweaks/MechaDronesTweaks.cs +++ b/MechaDronesTweaks/MechaDronesTweaks.cs @@ -1,5 +1,8 @@ using System; +using System.Collections.Generic; +using System.Reflection.Emit; using BepInEx; +using BepInEx.Configuration; using HarmonyLib; namespace MechaDronesTweaks; @@ -8,12 +11,17 @@ namespace MechaDronesTweaks; [BepInDependency(FastDronesRemover.FastDronesGuid, BepInDependency.DependencyFlags.SoftDependency)] public class MechaDronesTweaksPlugin : BaseUnityPlugin { + public new static readonly BepInEx.Logging.ManualLogSource Logger = + BepInEx.Logging.Logger.CreateLogSource(PluginInfo.PLUGIN_NAME); + + private readonly Harmony _harmony = new(PluginInfo.PLUGIN_GUID); + public MechaDronesTweaksPlugin() { - var harmony = new Harmony(PluginInfo.PLUGIN_GUID); /* Remove FastDrones MOD if loaded */ - try { - if (FastDronesRemover.Run(harmony)) + try + { + if (FastDronesRemover.Run(_harmony)) { Logger.LogInfo("Unpatch FastDrones - OK"); } @@ -23,4 +31,125 @@ public class MechaDronesTweaksPlugin : BaseUnityPlugin Logger.LogWarning($"Failed to unpatch FastDrones: {e}"); } } + + public void Awake() + { + MechaDronesTweaks.UseFixedSpeed = Config.Bind("MechaDrones", "UseFixedSpeed", MechaDronesTweaks.UseFixedSpeed, + "Use fixed speed for mecha drones").Value; + MechaDronesTweaks.RemoveSpeedLimitForStage1 = Config.Bind("MechaDrones", "RemoveSpeedLimitForStage1", + MechaDronesTweaks.RemoveSpeedLimitForStage1, + "Remove speed limit for 1st stage (flying away from mecha in ~1/3 speed for several frames)").Value; + MechaDronesTweaks.FixedSpeed = Config.Bind("MechaDrones", "FixedSpeed", MechaDronesTweaks.FixedSpeed, + new ConfigDescription("Fixed speed for mecha drones, working only when UseFixedSpeed is enabled", + new AcceptableValueRange(6f, 1000f))).Value; + MechaDronesTweaks.SpeedMultiplier = Config.Bind("MechaDrones", "SpeedMultiplier", + MechaDronesTweaks.SpeedMultiplier, + new ConfigDescription("Speed multiplier for mecha drones, working only when UseFixedSpeed is disabled", + new AcceptableValueRange(1f, 10f))).Value; + MechaDronesTweaks.EnergyMultiplier = Config.Bind("MechaDrones", "EnergyMultiplier", + MechaDronesTweaks.EnergyMultiplier, + new ConfigDescription("Energy consumption multiplier for mecha drones", + new AcceptableValueRange(0.01f, 1f))).Value; + + _harmony.PatchAll(typeof(MechaDronesTweaks)); + } } + +[HarmonyPatch] +public static class MechaDronesTweaks +{ + public static bool UseFixedSpeed = false; + public static bool RemoveSpeedLimitForStage1 = true; + public static float FixedSpeed = 500f; + public static float SpeedMultiplier = 5f; + public static float EnergyMultiplier = 0.1f; + + [HarmonyTranspiler, HarmonyPatch(typeof(MechaDroneLogic), "UpdateDrones")] + private static IEnumerable MechaUpdateDrones_Transpiler(IEnumerable instructions) + { + foreach (var instr in instructions) + { + yield return instr; + if (instr.opcode == OpCodes.Ldfld && instr.OperandIs(AccessTools.Field(typeof(Mecha), "droneEnergyPerMeter"))) + { + yield return new CodeInstruction(OpCodes.Ldc_R8, (double)EnergyMultiplier); + yield return new CodeInstruction(OpCodes.Mul); + } + } + } + + [HarmonyTranspiler, HarmonyPatch(typeof(MechaDrone), "Update")] + private static IEnumerable MechaDroneUpdate_Transpiler(IEnumerable instructions) + { + var lastIsLdarg0 = false; + CodeInstruction lastInstr = null; + foreach (var instr in instructions) + { + if (instr.opcode == OpCodes.Ldarg_0) + { + if (lastIsLdarg0) + { + yield return lastInstr; + continue; + } + + lastIsLdarg0 = true; + lastInstr = instr; + continue; + } + + if (lastIsLdarg0) + { + lastIsLdarg0 = false; + if (instr.opcode == OpCodes.Ldfld && + instr.OperandIs(AccessTools.Field(typeof(MechaDrone), "speed"))) + { + if (UseFixedSpeed) + { + lastInstr.opcode = OpCodes.Ldc_R4; + lastInstr.operand = FixedSpeed; + yield return lastInstr; + } + else + { + yield return lastInstr; + yield return instr; + yield return new CodeInstruction(OpCodes.Ldc_R4, SpeedMultiplier); + yield return new CodeInstruction(OpCodes.Mul); + } + + continue; + } + + yield return lastInstr; + yield return instr; + continue; + } + + if (instr.opcode == OpCodes.Ldc_R4) + { + if (instr.OperandIs(0.5f)) + { + if (FixedSpeed > 59f) + { + instr.operand = 0.5f * FixedSpeed / 50f; + yield return instr; + continue; + } + } + else if (instr.OperandIs(3f)) + { + if (RemoveSpeedLimitForStage1) + { + instr.operand = 10000f; + } + + yield return instr; + continue; + } + } + + yield return instr; + } + } +} \ No newline at end of file