From 26df57f9115867247863625315e667532adcf31c Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Sat, 12 Oct 2024 20:53:25 +0800 Subject: [PATCH] WIP --- CheatEnabler/Patches/FactoryPatch.cs | 5 +- UXAssist/Functions/WindowFunctions.cs | 17 +++- UXAssist/Patches/FactoryPatch.cs | 108 +++++++++++++++++++++++--- UXAssist/Patches/LogisticsPatch.cs | 6 +- UXAssist/UIConfigWindow.cs | 16 ++++ UXAssist/UXAssist.cs | 3 + 6 files changed, 138 insertions(+), 17 deletions(-) diff --git a/CheatEnabler/Patches/FactoryPatch.cs b/CheatEnabler/Patches/FactoryPatch.cs index 05dd0e1..ba4f46d 100644 --- a/CheatEnabler/Patches/FactoryPatch.cs +++ b/CheatEnabler/Patches/FactoryPatch.cs @@ -92,11 +92,12 @@ public class FactoryPatch: PatchImpl ControlPanelRemoteLogistics.Enable(ControlPanelRemoteLogisticsEnabled.Value); Enable(true); GameLogic.OnGameBegin += GameMain_Begin_Postfix_For_ImmBuild; - GameLogic.OnDataLoaded += () => WindTurbinesPowerGlobalCoverage.Enable(WindTurbinesPowerGlobalCoverageEnabled.Value); + GameLogic.OnDataLoaded += OnDataLoaded; } public static void Uninit() { + GameLogic.OnDataLoaded -= OnDataLoaded; GameLogic.OnGameBegin -= GameMain_Begin_Postfix_For_ImmBuild; Enable(false); ImmediateBuild.Enable(false); @@ -113,6 +114,8 @@ public class FactoryPatch: PatchImpl ControlPanelRemoteLogistics.Enable(false); } + private static void OnDataLoaded() => WindTurbinesPowerGlobalCoverage.Enable(WindTurbinesPowerGlobalCoverageEnabled.Value); + public static void OnUpdate() { if (_noConditionKey.keyValue) diff --git a/UXAssist/Functions/WindowFunctions.cs b/UXAssist/Functions/WindowFunctions.cs index 7b941d4..53fb9f4 100644 --- a/UXAssist/Functions/WindowFunctions.cs +++ b/UXAssist/Functions/WindowFunctions.cs @@ -23,7 +23,8 @@ public static class WindowFunctions public static ConfigEntry ProcessPriority; public static ConfigEntry ProcessAffinity; - private static readonly int[] ProrityFlags = [ + private static readonly int[] ProrityFlags = + [ WinApi.HIGH_PRIORITY_CLASS, WinApi.ABOVE_NORMAL_PRIORITY_CLASS, WinApi.NORMAL_PRIORITY_CLASS, @@ -44,13 +45,14 @@ public static class WindowFunctions public static void Start() { - GameLogic.OnDataLoaded += () => { _gameLoaded = true; }; + GameLogic.OnDataLoaded += OnDataLoaded; var wndProc = new WinApi.WndProc(GameWndProc); var gameWnd = FindGameWindow(); if (gameWnd != IntPtr.Zero) { _oldWndProc = WinApi.SetWindowLongPtr(gameWnd, WinApi.GWLP_WNDPROC, Marshal.GetFunctionPointerForDelegate(wndProc)); } + if (GamePatch.LoadLastWindowRectEnabled.Value) GamePatch.LoadLastWindowRect.MoveWindowPosition(true); @@ -67,6 +69,7 @@ public static class WindowFunctions { systemMask = ulong.MaxValue; } + switch (ProcessAffinity.Value) { case 0: @@ -88,6 +91,16 @@ public static class WindowFunctions } } + public static void Uninit() + { + GameLogic.OnDataLoaded -= OnDataLoaded; + } + + private static void OnDataLoaded() + { + _gameLoaded = true; + } + private static IntPtr GameWndProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam) { switch (uMsg) diff --git a/UXAssist/Patches/FactoryPatch.cs b/UXAssist/Patches/FactoryPatch.cs index d6ee016..1709c51 100644 --- a/UXAssist/Patches/FactoryPatch.cs +++ b/UXAssist/Patches/FactoryPatch.cs @@ -31,9 +31,13 @@ public class FactoryPatch : PatchImpl public static ConfigEntry DragBuildPowerPolesEnabled; public static ConfigEntry DragBuildPowerPolesAlternatelyEnabled; public static ConfigEntry BeltSignalsForBuyOutEnabled; + public static ConfigEntry TankFastFillInAndTakeOutEnabled; + public static ConfigEntry TankFastFillInAndTakeOutMultiplier; private static PressKeyBind _doNotRenderEntitiesKey; private static PressKeyBind _offgridfForPathsKey; + private static int _tankFastFillInAndTakeOutMultiplierRealValue = 2; + public static void Init() { _doNotRenderEntitiesKey = KeyBindings.RegisterKeyBinding(new BuiltinKey @@ -72,6 +76,8 @@ public class FactoryPatch : PatchImpl DragBuildPowerPolesEnabled.SettingChanged += (_, _) => DragBuildPowerPoles.Enable(DragBuildPowerPolesEnabled.Value); DragBuildPowerPolesAlternatelyEnabled.SettingChanged += (_, _) => DragBuildPowerPoles.AlternatelyChanged(); BeltSignalsForBuyOutEnabled.SettingChanged += (_, _) => BeltSignalsForBuyOut.Enable(BeltSignalsForBuyOutEnabled.Value); + TankFastFillInAndTakeOutEnabled.SettingChanged += (_, _) => TankFastFillInAndTakeOut.Enable(TankFastFillInAndTakeOutEnabled.Value); + TankFastFillInAndTakeOutMultiplier.SettingChanged += (_, _) => { UpdateTankFastFillInAndTakeOutMultiplierRealValue(); }; } public static void Start() @@ -89,30 +95,39 @@ public class FactoryPatch : PatchImpl DoNotRenderEntities.Enable(DoNotRenderEntitiesEnabled.Value); DragBuildPowerPoles.Enable(DragBuildPowerPolesEnabled.Value); BeltSignalsForBuyOut.Enable(BeltSignalsForBuyOutEnabled.Value); + TankFastFillInAndTakeOut.Enable(TankFastFillInAndTakeOutEnabled.Value); Enable(true); + UpdateTankFastFillInAndTakeOutMultiplierRealValue(); } public static void Uninit() { Enable(false); + TankFastFillInAndTakeOut.Enable(false); + BeltSignalsForBuyOut.Enable(false); + DragBuildPowerPoles.Enable(false); + DoNotRenderEntities.Enable(false); + ProtectVeinsFromExhaustion.Enable(false); + QuickBuildAndDismantleLab.Enable(false); + TreatStackingAsSingle.Enable(false); + OffGridBuilding.Enable(false); + LargerAreaForTerraform.Enable(false); + LargerAreaForUpgradeAndDismantle.Enable(false); + RemoveBuildRangeLimit.Enable(false); + NightLight.Enable(false); RemoveSomeConditionBuild.Enable(false); UnlimitInteractive.Enable(false); - NightLight.Enable(false); - RemoveBuildRangeLimit.Enable(false); - LargerAreaForUpgradeAndDismantle.Enable(false); - LargerAreaForTerraform.Enable(false); - OffGridBuilding.Enable(false); - TreatStackingAsSingle.Enable(false); - QuickBuildAndDismantleLab.Enable(false); - ProtectVeinsFromExhaustion.Enable(false); - DoNotRenderEntities.Enable(false); - DragBuildPowerPoles.Enable(false); - BeltSignalsForBuyOut.Enable(false); + BeltSignalsForBuyOut.UninitPersist(); } + private static void UpdateTankFastFillInAndTakeOutMultiplierRealValue() + { + _tankFastFillInAndTakeOutMultiplierRealValue = Mathf.Max(1, TankFastFillInAndTakeOutMultiplier.Value) * 2; + } + public static void OnUpdate() { if (_doNotRenderEntitiesKey.keyValue) @@ -1757,4 +1772,75 @@ public class FactoryPatch : PatchImpl return matcher.InstructionEnumeration(); } } + + private class TankFastFillInAndTakeOut : PatchImpl + { + private static int GetRealCount() + { + return _tankFastFillInAndTakeOutMultiplierRealValue; + } + + private static int MultiplierWithCountCheck(int count) + { + return Math.Min(count, _tankFastFillInAndTakeOutMultiplierRealValue); + } + + [HarmonyTranspiler] + [HarmonyPatch(typeof(PlanetFactory), nameof(PlanetFactory.EntityFastFillIn))] + private static IEnumerable PlanetFactory_EntityFastFillIn_Transpiler(IEnumerable instructions, ILGenerator generator) + { + var matcher = new CodeMatcher(instructions, generator); + matcher.MatchForward(false, + new CodeMatch(ci => ci.IsStloc()), + new CodeMatch(OpCodes.Ldc_I4_2), + new CodeMatch(ci => ci.IsStloc()) + ).Advance(1).RemoveInstruction().InsertAndAdvance(Transpilers.EmitDelegate(GetRealCount)).MatchForward(false, + new CodeMatch(OpCodes.Ldc_I4_1), + new CodeMatch(ci => ci.Branches(out _)), + new CodeMatch(OpCodes.Ldc_I4_1), + new CodeMatch(ci => ci.Branches(out _)), + new CodeMatch(OpCodes.Ldc_I4_2), + new CodeMatch(ci => ci.IsStloc()) + ).RemoveInstructions(5).Insert(Transpilers.EmitDelegate(MultiplierWithCountCheck)); + return matcher.InstructionEnumeration(); + } + + [HarmonyTranspiler] + [HarmonyPatch(typeof(PlanetFactory), nameof(PlanetFactory.EntityFastTakeOut))] + private static IEnumerable PlanetFactory_EntityFastTakeOut_Transpiler(IEnumerable instructions, ILGenerator generator) + { + var matcher = new CodeMatcher(instructions, generator); + matcher.MatchForward(false, + new CodeMatch(ci => ci.IsLdloc()), + new CodeMatch(OpCodes.Ldc_I4_2), + new CodeMatch(OpCodes.Ldc_I4_0), + new CodeMatch(ci => ci.opcode == OpCodes.Ldloca || ci.opcode == OpCodes.Ldloca_S) + ).Advance(1).RemoveInstruction().InsertAndAdvance(Transpilers.EmitDelegate(GetRealCount)).MatchForward(false, + new CodeMatch(OpCodes.Ldc_I4_1), + new CodeMatch(ci => ci.opcode == OpCodes.Bgt || ci.opcode == OpCodes.Bgt_S), + new CodeMatch(OpCodes.Ldc_I4_1), + new CodeMatch(ci => ci.opcode == OpCodes.Br || ci.opcode == OpCodes.Br_S), + new CodeMatch(OpCodes.Ldc_I4_2), + new CodeMatch(ci => ci.IsLdloc()) + ).RemoveInstructions(5).Insert(Transpilers.EmitDelegate(MultiplierWithCountCheck)); + return matcher.InstructionEnumeration(); + } + + [HarmonyTranspiler] + [HarmonyPatch(typeof(UITankWindow), nameof(UITankWindow._OnUpdate))] + private static IEnumerable UITankWindow__OnUpdate_Transpiler(IEnumerable instructions, ILGenerator generator) + { + var matcher = new CodeMatcher(instructions, generator); + matcher.MatchForward(false, + new CodeMatch(OpCodes.Ldc_I4_1), + new CodeMatch(ci => ci.opcode == OpCodes.Bgt || ci.opcode == OpCodes.Bgt_S), + new CodeMatch(OpCodes.Ldc_I4_1), + new CodeMatch(ci => ci.opcode == OpCodes.Br || ci.opcode == OpCodes.Br_S), + new CodeMatch(OpCodes.Ldc_I4_2), + new CodeMatch(ci => ci.IsStloc()) + ); + matcher.Repeat(m => m.RemoveInstructions(5).InsertAndAdvance(Transpilers.EmitDelegate(MultiplierWithCountCheck))); + return matcher.InstructionEnumeration(); + } + } } \ No newline at end of file diff --git a/UXAssist/Patches/LogisticsPatch.cs b/UXAssist/Patches/LogisticsPatch.cs index 3085965..a06bb6c 100644 --- a/UXAssist/Patches/LogisticsPatch.cs +++ b/UXAssist/Patches/LogisticsPatch.cs @@ -28,9 +28,6 @@ public static class LogisticsPatch LogisticsConstrolPanelImprovementEnabled.SettingChanged += (_, _) => LogisticsConstrolPanelImprovement.Enable(LogisticsConstrolPanelImprovementEnabled.Value); RealtimeLogisticsInfoPanelEnabled.SettingChanged += (_, _) => RealtimeLogisticsInfoPanel.Enable(RealtimeLogisticsInfoPanelEnabled.Value); RealtimeLogisticsInfoPanelBarsEnabled.SettingChanged += (_, _) => RealtimeLogisticsInfoPanel.EnableBars(RealtimeLogisticsInfoPanelBarsEnabled.Value); - - GameLogic.OnGameBegin += RealtimeLogisticsInfoPanel.OnGameBegin; - GameLogic.OnDataLoaded += RealtimeLogisticsInfoPanel.OnDataLoaded; } public static void Start() @@ -41,6 +38,9 @@ public static class LogisticsPatch RealtimeLogisticsInfoPanel.Enable(RealtimeLogisticsInfoPanelEnabled.Value); RealtimeLogisticsInfoPanel.EnableBars(RealtimeLogisticsInfoPanelBarsEnabled.Value); RealtimeLogisticsInfoPanel.InitGUI(); + + GameLogic.OnGameBegin += RealtimeLogisticsInfoPanel.OnGameBegin; + GameLogic.OnDataLoaded += RealtimeLogisticsInfoPanel.OnDataLoaded; } public static void Uninit() diff --git a/UXAssist/UIConfigWindow.cs b/UXAssist/UIConfigWindow.cs index 9f25acc..d50fed7 100644 --- a/UXAssist/UIConfigWindow.cs +++ b/UXAssist/UIConfigWindow.cs @@ -67,6 +67,8 @@ public static class UIConfigWindow I18N.Add("Enhanced count control for hand-make", "Enhanced count control for hand-make", "手动制造物品的数量控制改进"); I18N.Add("Enhanced count control for hand-make tips", "Maximum count is increased to 1000.\nHold Ctrl/Shift/Alt to change the count rapidly.", "最大数量提升至1000\n按住Ctrl/Shift/Alt可快速改变数量"); I18N.Add("Quick build and dismantle stacking labs", "Quick build and dismantle stacking labs/storages/tanks(hold shift)", "快速建造和拆除堆叠研究站/储物仓/储液罐(按住shift)"); + I18N.Add("Fast fill in to and take out from tanks", "Fast fill in to and take out from tanks", "储液罐快速注入和抽取液体"); + I18N.Add("Speed Ratio", "Speed Ratio", "速度倍率"); I18N.Add("Protect veins from exhaustion", "Protect veins from exhaustion", "保护矿脉不会耗尽"); I18N.Add("Protect veins from exhaustion tips", "By default, the vein amount is protected at 100, and oil speed is protected at 1.0/s, you can set them yourself in config file.\nWhen reach the protection value, veins/oils steeps will not be mined/extracted any longer.\nClose this function to resume mining and pumping, usually when you have enough level on `Veins Utilization`", @@ -241,6 +243,20 @@ public static class UIConfigWindow y += 36f; wnd.AddCheckBox(x, y, tab2, FactoryPatch.QuickBuildAndDismantleLabsEnabled, "Quick build and dismantle stacking labs"); y += 36f; + var cb = wnd.AddCheckBox(x, y, tab2, FactoryPatch.TankFastFillInAndTakeOutEnabled, "Fast fill in to and take out from tanks"); + x += cb.Width + 5f; + txt = wnd.AddText2(x, y + 2f, tab2, "Speed Ratio", 13, "text-tank-fast-fill-speed-ratio"); + var tankSlider = wnd.AddSlider(x + txt.preferredWidth + 5f, y + 7f, tab2, FactoryPatch.TankFastFillInAndTakeOutMultiplier, [2, 5, 10, 20, 50, 100, 500, 1000], "G", 100f).WithSmallerHandle(); + EventHandler tankSettingChanged = (_, _) => + { + tankSlider.SetEnable(FactoryPatch.TankFastFillInAndTakeOutEnabled.Value); + }; + FactoryPatch.TankFastFillInAndTakeOutEnabled.SettingChanged += tankSettingChanged; + wnd.OnFree += () => { FactoryPatch.TankFastFillInAndTakeOutEnabled.SettingChanged -= tankSettingChanged; }; + tankSettingChanged(null, null); + + x = 0; + y += 36f; checkBoxForMeasureTextWidth = wnd.AddCheckBox(x, y, tab2, FactoryPatch.ProtectVeinsFromExhaustionEnabled, "Protect veins from exhaustion"); wnd.AddTipsButton2(x + checkBoxForMeasureTextWidth.Width + 5f, y + 6f, tab2, "Protect veins from exhaustion", "Protect veins from exhaustion tips", "protect-veins-tips"); y += 36f; diff --git a/UXAssist/UXAssist.cs b/UXAssist/UXAssist.cs index 6bb8e98..9811387 100644 --- a/UXAssist/UXAssist.cs +++ b/UXAssist/UXAssist.cs @@ -131,6 +131,9 @@ public class UXAssist : BaseUnityPlugin, IModCanSave "Build Tesla Tower and Wireless Power Tower alternately"); FactoryPatch.BeltSignalsForBuyOutEnabled = Config.Bind("Factory", "BeltSignalsForBuyOut", false, "Belt signals for buy out dark fog items automatically"); + FactoryPatch.TankFastFillInAndTakeOutEnabled = Config.Bind("Factory", "TankFastFillInAndTakeOut", false, + "Fast fill in to and take out from tanks"); + FactoryPatch.TankFastFillInAndTakeOutMultiplier = Config.Bind("Factory", "TankFastFillInAndTakeOutMultiplier", 1000, "Speed multiplier for fast filling in to and takeing out from tanks"); LogisticsPatch.LogisticsCapacityTweaksEnabled = Config.Bind("Factory", "LogisticsCapacityTweaks", true, "Logistics capacity related tweaks"); LogisticsPatch.AllowOverflowInLogisticsEnabled = Config.Bind("Factory", "AllowOverflowInLogistics", false,