From 118e1b85e3df4febe4be3115b97f1387af9559ee Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Wed, 23 Aug 2023 21:45:20 +0800 Subject: [PATCH] move some functions into MechaDroneTweaks --- MechaDronesTweaks/MechaDronesTweaks.cs | 138 ++++++++++++++++++--- OverclockEverything/OverclockEverything.cs | 89 ------------- 2 files changed, 121 insertions(+), 106 deletions(-) diff --git a/MechaDronesTweaks/MechaDronesTweaks.cs b/MechaDronesTweaks/MechaDronesTweaks.cs index 0e668b4..042e455 100644 --- a/MechaDronesTweaks/MechaDronesTweaks.cs +++ b/MechaDronesTweaks/MechaDronesTweaks.cs @@ -58,17 +58,25 @@ public class MechaDronesTweaksPlugin : BaseUnityPlugin MechaDronesTweaks.EnergyMultiplier, new ConfigDescription("Energy consumption multiplier for mecha drones", new AcceptableValueRange(0f, 1f))).Value; - MechaDronesTweaks.removeBuildRangeLimit = Config.Bind("MechaBuild", "RemoveBuildRangeLimit", - MechaDronesTweaks.removeBuildRangeLimit, + MechaDronesTweaks.RemoveBuildRangeLimit = Config.Bind("MechaBuild", "RemoveBuildRangeLimit", + MechaDronesTweaks.RemoveBuildRangeLimit, "Remove limit for build range and maximum count of drag building belts/buildings\nNote: this does not affect range limit for mecha drones' action") .Value; - MechaDronesTweaks.largerAreaForUpgradeAndDismantle = Config.Bind("MechaBuild", - "LargerAreaForUpgradeAndDismantle", MechaDronesTweaks.largerAreaForUpgradeAndDismantle, + MechaDronesTweaks.LargerAreaForUpgradeAndDismantle = Config.Bind("MechaBuild", + "LargerAreaForUpgradeAndDismantle", MechaDronesTweaks.LargerAreaForUpgradeAndDismantle, "Increase maximum area size for upgrade and dismantle to 31x31 (from 11x11)").Value; - MechaDronesTweaks.largerAreaForTerraform = Config.Bind("MechaBuild", "LargerAreaForTerraform", - MechaDronesTweaks.largerAreaForTerraform, + MechaDronesTweaks.LargerAreaForTerraform = Config.Bind("MechaBuild", "LargerAreaForTerraform", + MechaDronesTweaks.LargerAreaForTerraform, "Increase maximum area size for terraform to 30x30 (from 10x10)\nNote: this may impact game performance while using large area") .Value; + MechaDronesTweaks.EnhancedMechaForgeCountControl = Config.Bind("", "EnhancedMechaForgeCountControl", + MechaDronesTweaks.EnhancedMechaForgeCountControl, + "Enhanced count control for hand-make, increases maximum of count to 1000, and you can hold Ctrl/Shift/Alt to change the count rapidly") + .Value; + MechaDronesTweaks.InventoryStackMultiplier = Config.Bind("Storage", "InventoryStackMultiplier", + MechaDronesTweaks.InventoryStackMultiplier, + "Stack count multiplier for inventory items, this also affects stack count for storage boxes") + .Value; _harmony.PatchAll(typeof(MechaDronesTweaks)); } @@ -82,9 +90,11 @@ public static class MechaDronesTweaks public static float FixedSpeed = 300f; public static float SpeedMultiplier = 4f; public static float EnergyMultiplier = 0.1f; - public static bool removeBuildRangeLimit = true; - public static bool largerAreaForUpgradeAndDismantle = true; - public static bool largerAreaForTerraform = true; + public static bool RemoveBuildRangeLimit = true; + public static bool LargerAreaForUpgradeAndDismantle = true; + public static bool LargerAreaForTerraform = true; + public static bool EnhancedMechaForgeCountControl = true; + public static int InventoryStackMultiplier = 1; [HarmonyTranspiler, HarmonyPatch(typeof(UITechTree), "RefreshDataValueText")] private static IEnumerable UITechTreeRefreshDataValueText_Transpiler( @@ -276,7 +286,7 @@ public static class MechaDronesTweaks private static IEnumerable BuildTools_CursorSizePatch_Transpiler( IEnumerable instructions) { - if (!largerAreaForUpgradeAndDismantle) + if (!LargerAreaForUpgradeAndDismantle) { foreach (var instr in instructions) { @@ -303,7 +313,7 @@ public static class MechaDronesTweaks private static IEnumerable BuildTool_Reform_Constructor_Transpiler( IEnumerable instructions) { - if (!largerAreaForTerraform) + if (!LargerAreaForTerraform) { foreach (var instr in instructions) { @@ -330,7 +340,7 @@ public static class MechaDronesTweaks private static IEnumerable BuildTool_Reform_ReformAction_Transpiler( IEnumerable instructions) { - if (!largerAreaForTerraform) + if (!LargerAreaForTerraform) { foreach (var instr in instructions) { @@ -365,7 +375,7 @@ public static class MechaDronesTweaks private static IEnumerable ConnGizmoGraph_Constructor_Transpiler( IEnumerable instructions) { - if (!removeBuildRangeLimit) + if (!RemoveBuildRangeLimit) { foreach (var instr in instructions) { @@ -392,7 +402,7 @@ public static class MechaDronesTweaks private static IEnumerable ConnGizmoGraph_SetPointCount_Transpiler( IEnumerable instructions) { - if (!removeBuildRangeLimit) + if (!RemoveBuildRangeLimit) { foreach (var instr in instructions) { @@ -419,7 +429,7 @@ public static class MechaDronesTweaks private static IEnumerable BuildTool_Path__OnInit_Transpiler( IEnumerable instructions) { - if (!removeBuildRangeLimit) + if (!RemoveBuildRangeLimit) { foreach (var instr in instructions) { @@ -444,7 +454,7 @@ public static class MechaDronesTweaks private static IEnumerable BuildTool_Click__OnInit_Transpiler( IEnumerable instructions) { - if (!removeBuildRangeLimit) + if (!RemoveBuildRangeLimit) { foreach (var instr in instructions) { @@ -480,7 +490,7 @@ public static class MechaDronesTweaks private static IEnumerable BuildAreaLimitRemoval_Transpiler( IEnumerable instructions) { - if (!removeBuildRangeLimit) + if (!RemoveBuildRangeLimit) { foreach (var instr in instructions) { @@ -567,4 +577,98 @@ public static class MechaDronesTweaks } } } + + [HarmonyTranspiler] + [HarmonyPatch(typeof(UIReplicatorWindow), "OnOkButtonClick")] + private static IEnumerable UIReplicatorWindow_OnOkButtonClick_Transpiler(IEnumerable instructions, ILGenerator generator) + { + if (!EnhancedMechaForgeCountControl) return instructions; + var matcher = new CodeMatcher(instructions, generator); + matcher.MatchForward(false, + new CodeMatch(OpCodes.Ldc_I4_S, 10) + ).Set(OpCodes.Ldc_I4, 1000); + return matcher.InstructionEnumeration(); + } + + [HarmonyTranspiler] + [HarmonyPatch(typeof(UIReplicatorWindow), "OnPlusButtonClick")] + [HarmonyPatch(typeof(UIReplicatorWindow), "OnMinusButtonClick")] + private static IEnumerable UIReplicatorWindow_OnPlusButtonClick_Transpiler(IEnumerable instructions, ILGenerator generator) + { + if (!EnhancedMechaForgeCountControl) return instructions; + var label1 = generator.DefineLabel(); + var label2 = generator.DefineLabel(); + var label3 = generator.DefineLabel(); + var label4 = generator.DefineLabel(); + var matcher = new CodeMatcher(instructions, generator); + matcher.MatchForward(false, + new CodeMatch(OpCodes.Ldloc_0), + new CodeMatch(OpCodes.Ldc_I4_1), + new CodeMatch(o => o.opcode == OpCodes.Add || o.opcode == OpCodes.Sub) + ).Advance(1).RemoveInstruction().InsertAndAdvance( + new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(VFInput), "control")), + new CodeInstruction(OpCodes.Brfalse_S, label1), + new CodeInstruction(OpCodes.Ldc_I4_S, 10), + new CodeInstruction(OpCodes.Br_S, label4), + new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(VFInput), "shift")).WithLabels(label1), + new CodeInstruction(OpCodes.Brfalse_S, label2), + new CodeInstruction(OpCodes.Ldc_I4_S, 100), + new CodeInstruction(OpCodes.Br_S, label4), + new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(VFInput), "alt")).WithLabels(label2), + new CodeInstruction(OpCodes.Brfalse_S, label3), + new CodeInstruction(OpCodes.Ldc_I4, 1000), + new CodeInstruction(OpCodes.Br_S, label4), + new CodeInstruction(OpCodes.Ldc_I4_1).WithLabels(label3) + ).Labels.Add(label4); + return matcher.InstructionEnumeration(); + } + + [HarmonyPrefix, HarmonyPatch(typeof(StorageComponent), "LoadStatic")] + private static bool StorageComponent_LoadStatic_Prefix() + { + if (StorageComponent.staticLoaded) return false; + if (InventoryStackMultiplier <= 1) return true; + foreach (var proto in LDB.items.dataArray) + { + proto.StackSize *= InventoryStackMultiplier; + } + return true; + } + + [HarmonyTranspiler] + [HarmonyPatch(typeof(Mecha), nameof(Mecha.SetForNewGame))] + private static IEnumerable Mecha_SetForNewGame_Transpiler(IEnumerable instructions, ILGenerator generator) + { + if (InventoryStackMultiplier <= 1) return instructions; + var matcher = new CodeMatcher(instructions, generator); + matcher.MatchForward(false, + new CodeMatch(OpCodes.Ldc_I4, 1210), + new CodeMatch(OpCodes.Ldc_I4_S, 20) + ).Advance(1).Set(OpCodes.Ldc_I4_S, 20 * InventoryStackMultiplier); + return matcher.InstructionEnumeration(); + } + + [HarmonyPostfix, HarmonyPatch(typeof(StorageComponent), "Import")] + private static void StorageComponent_Import_Postfix(StorageComponent __instance) + { + if (InventoryStackMultiplier <= 1) return; + var size = __instance.size; + if (size <= 0) return; + if (__instance.type == EStorageType.Filtered) + { + if (__instance.grids[0].itemId == 1210) + { + __instance.grids[0].stackSize = 20 * InventoryStackMultiplier; + } + return; + } + for (var i = 0; i < size; i++) + { + var itemId = __instance.grids[i].itemId; + if (itemId != 0) + { + __instance.grids[i].stackSize = StorageComponent.itemStackCount[itemId]; + } + } + } } \ No newline at end of file diff --git a/OverclockEverything/OverclockEverything.cs b/OverclockEverything/OverclockEverything.cs index 303d029..be59442 100644 --- a/OverclockEverything/OverclockEverything.cs +++ b/OverclockEverything/OverclockEverything.cs @@ -60,8 +60,6 @@ public class Patch : BaseUnityPlugin, IModCanSave new ConfigDescription("Speed multiplier for EM-Rail Ejectors", new AcceptableValueRange(1, 10))).Value; Cfg.SiloMultiplier = Config.Bind("DysonSphere", "SiloMultiplier", Cfg.SiloMultiplier, new ConfigDescription("Speed multiplier for Rocket Silos", new AcceptableValueRange(1, 10))).Value; - Cfg.InventoryStackMultiplier = Config.Bind("Inventory", "StackMultiplier", Cfg.InventoryStackMultiplier, - new ConfigDescription("Stack count multiplier for inventory items", new AcceptableValueRange(1, 10))).Value; Harmony.CreateAndPatchAll(typeof(Patch)); Harmony.CreateAndPatchAll(typeof(BeltFix)); } @@ -124,66 +122,6 @@ public class Patch : BaseUnityPlugin, IModCanSave } */ - [HarmonyTranspiler] - [HarmonyPatch(typeof(UIReplicatorWindow), "OnOkButtonClick")] - private static IEnumerable UIReplicatorWindow_OnOkButtonClick_Transpiler(IEnumerable instructions) - { - foreach (var instr in instructions) - { - if (instr.opcode == OpCodes.Ldc_I4_S && instr.OperandIs(10)) - { - instr.opcode = OpCodes.Ldc_I4; - instr.operand = 1000; - } - - yield return instr; - } - } - - [HarmonyTranspiler] - [HarmonyPatch(typeof(UIReplicatorWindow), "OnPlusButtonClick")] - [HarmonyPatch(typeof(UIReplicatorWindow), "OnMinusButtonClick")] - private static IEnumerable UIReplicatorWindow_OnPlusButtonClick_Transpiler(IEnumerable instructions, ILGenerator generator) - { - var ilist = instructions.ToList(); - for (var i = 0; i < ilist.Count; i++) - { - var instr = ilist[i]; - if (instr.opcode == OpCodes.Ldloc_0 && ilist[i + 1].opcode == OpCodes.Ldc_I4_1 && (ilist[i + 2].opcode == OpCodes.Add || ilist[i + 2].opcode == OpCodes.Sub)) - { - var label1 = generator.DefineLabel(); - var label2 = generator.DefineLabel(); - var label3 = generator.DefineLabel(); - var label4 = generator.DefineLabel(); - yield return instr; - yield return new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(VFInput), "control")); - yield return new CodeInstruction(OpCodes.Brfalse_S, label1); - yield return new CodeInstruction(OpCodes.Ldc_I4_S, 10); - yield return new CodeInstruction(OpCodes.Br_S, label4); - yield return new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(VFInput), "shift")).WithLabels(label1); - yield return new CodeInstruction(OpCodes.Brfalse_S, label2); - yield return new CodeInstruction(OpCodes.Ldc_I4_S, 100); - yield return new CodeInstruction(OpCodes.Br_S, label4); - yield return new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(VFInput), "alt")).WithLabels(label2); - yield return new CodeInstruction(OpCodes.Brfalse_S, label3); - yield return new CodeInstruction(OpCodes.Ldc_I4, 1000); - yield return new CodeInstruction(OpCodes.Br_S, label4); - yield return new CodeInstruction(OpCodes.Ldc_I4_1).WithLabels(label3); - ilist[i + 2] = ilist[i + 2].WithLabels(label4); - i++; - continue; - } - - if (instr.opcode == OpCodes.Ldc_I4_S && instr.OperandIs(10)) - { - instr.opcode = OpCodes.Ldc_I4; - instr.operand = 1000; - } - - yield return instr; - } - } - [HarmonyPostfix, HarmonyPatch(typeof(LabComponent), "SetFunction")] private static void LabComponent_SetFunction_Postfix(ref LabComponent __instance) { @@ -208,33 +146,6 @@ public class Patch : BaseUnityPlugin, IModCanSave } } - [HarmonyPrefix, HarmonyPatch(typeof(StorageComponent), "LoadStatic")] - private static bool StorageComponent_LoadStatic_Prefix() - { - if (StorageComponent.staticLoaded) return false; - foreach (var proto in LDB.items.dataArray) - { - proto.StackSize *= Cfg.InventoryStackMultiplier; - } - - return true; - } - - [HarmonyPostfix, HarmonyPatch(typeof(StorageComponent), "Import")] - private static void StorageComponent_Import_Postfix(StorageComponent __instance) - { - if (Cfg.InventoryStackMultiplier <= 1) return; - var size = __instance.size; - for (var i = 0; i < size; i++) - { - var itemId = __instance.grids[i].itemId; - if (itemId != 0) - { - __instance.grids[i].stackSize = StorageComponent.itemStackCount[itemId]; - } - } - } - [HarmonyPrefix] [HarmonyPatch(typeof(GameSave), nameof(GameSave.LoadCurrentGame))] private static void GameSave_LoadCurrentGame_Prefix()