diff --git a/UXAssist/Common/PatchImpl.cs b/UXAssist/Common/PatchImpl.cs index 206bc26..34a6c21 100644 --- a/UXAssist/Common/PatchImpl.cs +++ b/UXAssist/Common/PatchImpl.cs @@ -10,22 +10,18 @@ public class PatchImplGuidAttribute(string guid) : Attribute public string Guid { get; } = guid; } -public class PatchImpl where T : new() +public class PatchImpl where T : PatchImpl, new() { private static T Instance { get; } = new(); private Harmony _patch; - + public static void Enable(bool enable) { - if (Instance is not PatchImpl thisInstance) - { - UXAssist.Logger.LogError($"PatchImpl<{typeof(T).Name}> is not inherited correctly"); - return; - } + var thisInstance = Instance; if (enable) { - var guid = typeof(T).GetCustomAttribute()?.Guid; + var guid = typeof(T).GetCustomAttribute()?.Guid ?? $"PatchImpl.{typeof(T).FullName ?? typeof(T).ToString()}"; thisInstance._patch ??= Harmony.CreateAndPatchAll(typeof(T), guid); thisInstance.OnEnable(); return; @@ -35,7 +31,7 @@ public class PatchImpl where T : new() thisInstance._patch = null; } - public static Harmony GetHarmony() => (Instance as PatchImpl)?._patch; + public static Harmony GetHarmony() => Instance._patch; protected virtual void OnEnable() { } protected virtual void OnDisable() { } diff --git a/UXAssist/ModsCompat/BulletTimeWrapper.cs b/UXAssist/ModsCompat/BulletTimeWrapper.cs index b6f482c..03cd618 100644 --- a/UXAssist/ModsCompat/BulletTimeWrapper.cs +++ b/UXAssist/ModsCompat/BulletTimeWrapper.cs @@ -2,6 +2,7 @@ using System.Reflection.Emit; using BepInEx.Configuration; using HarmonyLib; +using UXAssist.Common; namespace UXAssist.ModsCompat; @@ -14,6 +15,7 @@ public static class BulletTimeWrapper { HasBulletTime = BepInEx.Bootstrap.Chainloader.PluginInfos.TryGetValue(BulletTimeGuid, out var pluginInfo); if (!HasBulletTime) return; + I18N.Add("Increase game speed (max 10x)", "Increase game speed (max 10x)", "提升游戏速度(最高10倍)"); var assembly = pluginInfo.Instance.GetType().Assembly; try { diff --git a/UXAssist/Patches/DysonSpherePatch.cs b/UXAssist/Patches/DysonSpherePatch.cs index d7493ee..7a251ca 100644 --- a/UXAssist/Patches/DysonSpherePatch.cs +++ b/UXAssist/Patches/DysonSpherePatch.cs @@ -21,13 +21,17 @@ public class DysonSpherePatch: PatchImpl Enable(true); StopEjectOnNodeCompleteEnabled.SettingChanged += (_, _) => StopEjectOnNodeComplete.Enable(StopEjectOnNodeCompleteEnabled.Value); OnlyConstructNodesEnabled.SettingChanged += (_, _) => OnlyConstructNodes.Enable(OnlyConstructNodesEnabled.Value); - StopEjectOnNodeComplete.Enable(StopEjectOnNodeCompleteEnabled.Value); - OnlyConstructNodes.Enable(OnlyConstructNodesEnabled.Value); _totalNodeSpInfo = AccessTools.Field(typeof(DysonSphereLayer), "totalNodeSP"); _totalFrameSpInfo = AccessTools.Field(typeof(DysonSphereLayer), "totalFrameSP"); _totalCpInfo = AccessTools.Field(typeof(DysonSphereLayer), "totalCP"); } + public static void Start() + { + StopEjectOnNodeComplete.Enable(StopEjectOnNodeCompleteEnabled.Value); + OnlyConstructNodes.Enable(OnlyConstructNodesEnabled.Value); + } + public static void Uninit() { StopEjectOnNodeComplete.Enable(false); diff --git a/UXAssist/Patches/FactoryPatch.cs b/UXAssist/Patches/FactoryPatch.cs index d11b9ea..c6b3fb2 100644 --- a/UXAssist/Patches/FactoryPatch.cs +++ b/UXAssist/Patches/FactoryPatch.cs @@ -55,6 +55,10 @@ public class FactoryPatch: PatchImpl DoNotRenderEntitiesEnabled.SettingChanged += (_, _) => DoNotRenderEntities.Enable(DoNotRenderEntitiesEnabled.Value); DragBuildPowerPolesEnabled.SettingChanged += (_, _) => DragBuildPowerPoles.Enable(DragBuildPowerPolesEnabled.Value); BeltSignalsForBuyOutEnabled.SettingChanged += (_, _) => BeltSignalsForBuyOut.Enable(BeltSignalsForBuyOutEnabled.Value); + } + + public static void Start() + { UnlimitInteractive.Enable(UnlimitInteractiveEnabled.Value); RemoveSomeConditionBuild.Enable(RemoveSomeConditionEnabled.Value); NightLight.Enable(NightLightEnabled.Value); diff --git a/UXAssist/Patches/GamePatch.cs b/UXAssist/Patches/GamePatch.cs index 226f4d1..bfb5bab 100644 --- a/UXAssist/Patches/GamePatch.cs +++ b/UXAssist/Patches/GamePatch.cs @@ -96,6 +96,10 @@ public class GamePatch: PatchImpl } FPSController.SetFixUPS(GameMain.tickPerSec * GameUpsFactor.Value); }; + } + + public static void Start() + { EnableWindowResize.Enable(EnableWindowResizeEnabled.Value); LoadLastWindowRect.Enable(LoadLastWindowRectEnabled.Value); MouseCursorScaleUp.reload = false; diff --git a/UXAssist/Patches/LogisticsPatch.cs b/UXAssist/Patches/LogisticsPatch.cs index b7f9b3f..752cf99 100644 --- a/UXAssist/Patches/LogisticsPatch.cs +++ b/UXAssist/Patches/LogisticsPatch.cs @@ -27,14 +27,19 @@ 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() + { LogisticsCapacityTweaks.Enable(LogisticsCapacityTweaksEnabled.Value); AllowOverflowInLogistics.Enable(AllowOverflowInLogisticsEnabled.Value); LogisticsConstrolPanelImprovement.Enable(LogisticsConstrolPanelImprovementEnabled.Value); RealtimeLogisticsInfoPanel.Enable(RealtimeLogisticsInfoPanelEnabled.Value); RealtimeLogisticsInfoPanel.EnableBars(RealtimeLogisticsInfoPanelBarsEnabled.Value); - - GameLogic.OnGameBegin += RealtimeLogisticsInfoPanel.OnGameBegin; - GameLogic.OnDataLoaded += RealtimeLogisticsInfoPanel.OnDataLoaded; + RealtimeLogisticsInfoPanel.InitGUI(); } public static void Uninit() @@ -48,11 +53,6 @@ public static class LogisticsPatch RealtimeLogisticsInfoPanel.Enable(false); } - public static void Start() - { - RealtimeLogisticsInfoPanel.InitGUI(); - } - public static void OnUpdate() { if (RealtimeLogisticsInfoPanelEnabled.Value) diff --git a/UXAssist/Patches/PersistPatch.cs b/UXAssist/Patches/PersistPatch.cs new file mode 100644 index 0000000..ebdb8ea --- /dev/null +++ b/UXAssist/Patches/PersistPatch.cs @@ -0,0 +1,201 @@ +using System; +using System.Collections.Generic; +using System.Reflection.Emit; +using HarmonyLib; +using UnityEngine; +using UXAssist.Common; + +namespace UXAssist.Patches; + +public class PersistPatch : PatchImpl +{ + public static void Start() + { + Enable(true); + } + + public static void Uninit() + { + Enable(false); + } + + // Check for noModifier while pressing hotkeys on build bar + [HarmonyTranspiler] + [HarmonyPatch(typeof(UIBuildMenu), nameof(UIBuildMenu._OnUpdate))] + private static IEnumerable UIBuildMenu__OnUpdate_Transpiler(IEnumerable instructions, ILGenerator generator) + { + var matcher = new CodeMatcher(instructions, generator); + matcher.MatchForward(false, + new CodeMatch(OpCodes.Ldsfld, AccessTools.Field(typeof(VFInput), nameof(VFInput.inScreen))) + ); + matcher.Repeat(codeMatcher => + { + var jumpPos = codeMatcher.Advance(1).Operand; + codeMatcher.Advance(-1).InsertAndAdvance( + new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(VFInput), nameof(VFInput.noModifier))), + new CodeInstruction(OpCodes.Brfalse_S, jumpPos) + ).Advance(2); + }); + return matcher.InstructionEnumeration(); + } + + // Patch to fix the issue that warning popup on VeinUtil upgraded to level 8000+ + [HarmonyTranspiler] + [HarmonyPatch(typeof(ABN_VeinsUtil), nameof(ABN_VeinsUtil.CheckValue))] + private static IEnumerable ABN_VeinsUtil_CheckValue_Transpiler(IEnumerable instructions, ILGenerator generator) + { + var matcher = new CodeMatcher(instructions, generator); + matcher.MatchForward(false, + new CodeMatch(OpCodes.Ldelem_R8), + new CodeMatch(OpCodes.Conv_R4), + new CodeMatch(OpCodes.Add), + new CodeMatch(OpCodes.Stloc_1) + ); + // loc1 = Mathf.Round(n * 1000f) / 1000f; + matcher.Advance(3).Insert( + new CodeInstruction(OpCodes.Ldc_R4, 1000f), + new CodeInstruction(OpCodes.Mul), + new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Mathf), nameof(Mathf.Round))), + new CodeInstruction(OpCodes.Ldc_R4, 1000f), + new CodeInstruction(OpCodes.Div) + ); + return matcher.InstructionEnumeration(); + } + + // Bring popup tip window to top layer + [HarmonyTranspiler] + [HarmonyPatch(typeof(UIButton), nameof(UIButton.LateUpdate))] + private static IEnumerable UIButton_LateUpdate_Transpiler(IEnumerable instructions, ILGenerator generator) + { + var matcher = new CodeMatcher(instructions, generator); + matcher.MatchForward(false, + new CodeMatch(OpCodes.Ldloc_2), + new CodeMatch(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(Component), nameof(Component.gameObject))), + new CodeMatch(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(GameObject), nameof(GameObject.activeSelf))) + ); + var labels = matcher.Labels; + matcher.Labels = null; + matcher.Insert( + new CodeInstruction(OpCodes.Ldloc_2).WithLabels(labels), + new CodeInstruction(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(Component), nameof(Component.transform))), + new CodeInstruction(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(Transform), nameof(Transform.parent))), + new CodeInstruction(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(Transform), nameof(Transform.parent))), + new CodeInstruction(OpCodes.Callvirt, AccessTools.Method(typeof(Transform), nameof(Transform.SetAsLastSibling))) + ); + return matcher.InstructionEnumeration(); + } + + // Sort blueprint structures by item id, model index, recipe id, area index, and position before saving + [HarmonyPostfix] + [HarmonyPatch(typeof(BlueprintUtils), nameof(BlueprintUtils.GenerateBlueprintData))] + private static void BlueprintUtils_GenerateBlueprintData_Postfix(BlueprintData _blueprintData) + { + var buildings = _blueprintData.buildings; + Array.Sort(buildings, (a, b) => + { + var tmpItemId = a.itemId - b.itemId; + if (tmpItemId != 0) + return tmpItemId; + var tmpModelIndex = a.modelIndex - b.modelIndex; + if (tmpModelIndex != 0) + return tmpModelIndex; + var tmpRecipeId = a.recipeId - b.recipeId; + if (tmpRecipeId != 0) + return tmpRecipeId; + var tmpAreaIndex = a.areaIndex - b.areaIndex; + if (tmpAreaIndex != 0) + return tmpAreaIndex; + const double ky = 256.0; + const double kx = 1024.0; + var scorePosA = (a.localOffset_y * ky + a.localOffset_x) * kx + a.localOffset_z; + var scorePosB = (b.localOffset_y * ky + b.localOffset_x) * kx + b.localOffset_z; + return scorePosA < scorePosB ? 1 : -1; + }); + for (var i = buildings.Length - 1; i >= 0; i--) + { + buildings[i].index = i; + } + } + + // Increase maximum value of property realizing, 2000 -> 20000 + [HarmonyTranspiler] + [HarmonyPatch(typeof(UIPropertyEntry), nameof(UIPropertyEntry.UpdateUIElements))] + [HarmonyPatch(typeof(UIPropertyEntry), nameof(UIPropertyEntry.OnRealizeButtonClick))] + [HarmonyPatch(typeof(UIPropertyEntry), nameof(UIPropertyEntry.OnInputValueEnd))] + private static IEnumerable UIProductEntry_UpdateUIElements_Transpiler(IEnumerable instructions, ILGenerator generator) + { + var matcher = new CodeMatcher(instructions, generator); + matcher.MatchForward(false, + new CodeMatch(OpCodes.Ldc_I4, 2000) + ); + matcher.Repeat(m => { m.SetAndAdvance(OpCodes.Ldc_I4, 20000); }); + return matcher.InstructionEnumeration(); + } + + [HarmonyTranspiler] + [HarmonyPatch(typeof(UIPropertyEntry), nameof(UIPropertyEntry.OnInputValueEnd))] + private static IEnumerable UIProductEntry_OnInputValueEnd_Transpiler(IEnumerable instructions, ILGenerator generator) + { + var matcher = new CodeMatcher(instructions, generator); + matcher.MatchForward(false, + new CodeMatch(ci => ci.opcode == OpCodes.Ldc_R4 && ci.OperandIs(2000f)) + ); + matcher.Repeat(m => { m.SetAndAdvance(OpCodes.Ldc_R4, 20000f); }); + return matcher.InstructionEnumeration(); + } + + // Increase capacity of player order queue, 16 -> 128 + [HarmonyTranspiler] + [HarmonyPatch(typeof(PlayerOrder), MethodType.Constructor, typeof(Player))] + private static IEnumerable PlayerOrder_Constructor_Transpiler(IEnumerable instructions, ILGenerator generator) + { + var matcher = new CodeMatcher(instructions, generator); + matcher.MatchForward(false, + new CodeMatch(ci => (ci.opcode == OpCodes.Ldc_I4_S || ci.opcode == OpCodes.Ldc_I4) && ci.OperandIs(16)) + ); + matcher.Repeat(m => { m.SetAndAdvance(OpCodes.Ldc_I4, 128); }); + return matcher.InstructionEnumeration(); + } + + // Increase Player Command Queue from 16 to 128 + [HarmonyTranspiler] + [HarmonyPatch(typeof(PlayerOrder), nameof(PlayerOrder._trimEnd))] + [HarmonyPatch(typeof(PlayerOrder), nameof(PlayerOrder.Enqueue))] + private static IEnumerable PlayerOrder_ExtendCount_Transpiler(IEnumerable instructions, ILGenerator generator) + { + var matcher = new CodeMatcher(instructions, generator); + matcher.MatchForward(false, + new CodeMatch(ci => (ci.opcode == OpCodes.Ldc_I4_S || ci.opcode == OpCodes.Ldc_I4) && ci.OperandIs(16)) + ); + matcher.Repeat(m => { m.SetAndAdvance(OpCodes.Ldc_I4, 128); }); + return matcher.InstructionEnumeration(); + } + + // Allow F11 in star map + [HarmonyTranspiler] + [HarmonyPatch(typeof(UIGame), nameof(UIGame._OnLateUpdate))] + private static IEnumerable UIGame__OnLateUpdate_Transpiler(IEnumerable instructions, ILGenerator generator) + { + var matcher = new CodeMatcher(instructions, generator); + matcher.MatchForward(false, + new CodeMatch(OpCodes.Ldsfld, AccessTools.Field(typeof(VFInput), nameof(VFInput.inFullscreenGUI))), + new CodeMatch(ci => ci.opcode == OpCodes.Brfalse || ci.opcode == OpCodes.Brfalse_S) + ); + var jumpPos = matcher.Advance(1).Operand; + matcher.Advance(-1).Insert( + new CodeInstruction(OpCodes.Ldarg_0), + new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(UIGame), nameof(UIGame.starmap))), + new CodeInstruction(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(ManualBehaviour), nameof(ManualBehaviour.active))), + new CodeInstruction(OpCodes.Brtrue_S, jumpPos) + ); + return matcher.InstructionEnumeration(); + } + + // Ignore UIDFCommunicatorWindow.Determine() + [HarmonyPrefix] + [HarmonyPatch(typeof(UIDFCommunicatorWindow), nameof(UIDFCommunicatorWindow.Determine))] + private static bool UIDFCommunicatorWindow_Determine_Prefix() + { + return false; + } +} diff --git a/UXAssist/Patches/PlanetPatch.cs b/UXAssist/Patches/PlanetPatch.cs index 9364675..59dfd6c 100644 --- a/UXAssist/Patches/PlanetPatch.cs +++ b/UXAssist/Patches/PlanetPatch.cs @@ -12,6 +12,10 @@ public static class PlanetPatch public static void Init() { PlayerActionsInGlobeViewEnabled.SettingChanged += (_, _) => PlayerActionsInGlobeView.Enable(PlayerActionsInGlobeViewEnabled.Value); + } + + public static void Start() + { PlayerActionsInGlobeView.Enable(PlayerActionsInGlobeViewEnabled.Value); } diff --git a/UXAssist/Patches/PlayerPatch.cs b/UXAssist/Patches/PlayerPatch.cs index f6d8eb6..61de7bf 100644 --- a/UXAssist/Patches/PlayerPatch.cs +++ b/UXAssist/Patches/PlayerPatch.cs @@ -20,12 +20,6 @@ public static class PlayerPatch public static void Init() { - EnhancedMechaForgeCountControlEnabled.SettingChanged += (_, _) => EnhancedMechaForgeCountControl.Enable(EnhancedMechaForgeCountControlEnabled.Value); - HideTipsForSandsChangesEnabled.SettingChanged += (_, _) => HideTipsForSandsChanges.Enable(HideTipsForSandsChangesEnabled.Value); - AutoNavigationEnabled.SettingChanged += (_, _) => AutoNavigation.Enable(AutoNavigationEnabled.Value); - EnhancedMechaForgeCountControl.Enable(EnhancedMechaForgeCountControlEnabled.Value); - HideTipsForSandsChanges.Enable(HideTipsForSandsChangesEnabled.Value); - AutoNavigation.Enable(AutoNavigationEnabled.Value); _autoDriveKey = KeyBindings.RegisterKeyBinding(new BuiltinKey { key = new CombineKey(0, 0, ECombineKeyAction.OnceClick, true), @@ -35,6 +29,16 @@ public static class PlayerPatch }); I18N.Add("AutoCruiseOn", "Auto-cruise enabled", "已启用自动巡航"); I18N.Add("AutoCruiseOff", "Auto-cruise disabled", "已禁用自动巡航"); + EnhancedMechaForgeCountControlEnabled.SettingChanged += (_, _) => EnhancedMechaForgeCountControl.Enable(EnhancedMechaForgeCountControlEnabled.Value); + HideTipsForSandsChangesEnabled.SettingChanged += (_, _) => HideTipsForSandsChanges.Enable(HideTipsForSandsChangesEnabled.Value); + AutoNavigationEnabled.SettingChanged += (_, _) => AutoNavigation.Enable(AutoNavigationEnabled.Value); + } + + public static void Start() + { + EnhancedMechaForgeCountControl.Enable(EnhancedMechaForgeCountControlEnabled.Value); + HideTipsForSandsChanges.Enable(HideTipsForSandsChangesEnabled.Value); + AutoNavigation.Enable(AutoNavigationEnabled.Value); } public static void OnUpdate() diff --git a/UXAssist/Patches/TechPatch.cs b/UXAssist/Patches/TechPatch.cs index 643164f..56a2766 100644 --- a/UXAssist/Patches/TechPatch.cs +++ b/UXAssist/Patches/TechPatch.cs @@ -18,10 +18,14 @@ public static class TechPatch I18N.Add("分拣器运货量", "Sorter Mk.III cargo stacking : ", "极速分拣器每次可运送 "); SorterCargoStackingEnabled.SettingChanged += (_, _) => SorterCargoStacking.Enable(SorterCargoStackingEnabled.Value); BatchBuyoutTechEnabled.SettingChanged += (_, _) => BatchBuyoutTech.Enable(BatchBuyoutTechEnabled.Value); + } + + public static void Start() + { SorterCargoStacking.Enable(SorterCargoStackingEnabled.Value); BatchBuyoutTech.Enable(BatchBuyoutTechEnabled.Value); } - + public static void Uninit() { BatchBuyoutTech.Enable(false); diff --git a/UXAssist/UIConfigWindow.cs b/UXAssist/UIConfigWindow.cs index 4d89937..bac42b1 100644 --- a/UXAssist/UIConfigWindow.cs +++ b/UXAssist/UIConfigWindow.cs @@ -238,8 +238,8 @@ public static class UIConfigWindow if (ModsCompat.AuxilaryfunctionWrapper.ShowStationInfo != null) { ModsCompat.AuxilaryfunctionWrapper.ShowStationInfo.SettingChanged += (_, _) => { OnAuxilaryInfoPanelChanged(); }; + OnAuxilaryInfoPanelChanged(); } - OnAuxilaryInfoPanelChanged(); var tab3 = wnd.AddTab(trans, "Player/Mecha"); x = 0f; diff --git a/UXAssist/UXAssist.cs b/UXAssist/UXAssist.cs index 737bed2..47e831d 100644 --- a/UXAssist/UXAssist.cs +++ b/UXAssist/UXAssist.cs @@ -158,8 +158,6 @@ public class UXAssist : BaseUnityPlugin, IModCanSave // UI Patches UIPatch.Enable(true); - // Persistant Patches - Persist.Enable(true); GameLogic.Enable(true); MyWindowManager.Init(); @@ -179,7 +177,6 @@ public class UXAssist : BaseUnityPlugin, IModCanSave private void Start() { _patches?.Do(type => type.GetMethod("Start")?.Invoke(null, null)); - LogisticsPatch.Start(); } private void OnDestroy() @@ -189,7 +186,6 @@ public class UXAssist : BaseUnityPlugin, IModCanSave MyWindowManager.Uninit(); GameLogic.Enable(false); - Persist.Enable(false); UIPatch.Enable(false); } @@ -325,187 +321,4 @@ public class UXAssist : BaseUnityPlugin, IModCanSave } } } - - private class Persist: PatchImpl - { - // Check for noModifier while pressing hotkeys on build bar - [HarmonyTranspiler] - [HarmonyPatch(typeof(UIBuildMenu), nameof(UIBuildMenu._OnUpdate))] - private static IEnumerable UIBuildMenu__OnUpdate_Transpiler(IEnumerable instructions, ILGenerator generator) - { - var matcher = new CodeMatcher(instructions, generator); - matcher.MatchForward(false, - new CodeMatch(OpCodes.Ldsfld, AccessTools.Field(typeof(VFInput), nameof(VFInput.inScreen))) - ); - matcher.Repeat(codeMatcher => - { - var jumpPos = codeMatcher.Advance(1).Operand; - codeMatcher.Advance(-1).InsertAndAdvance( - new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(VFInput), nameof(VFInput.noModifier))), - new CodeInstruction(OpCodes.Brfalse_S, jumpPos) - ).Advance(2); - }); - return matcher.InstructionEnumeration(); - } - - // Patch to fix the issue that warning popup on VeinUtil upgraded to level 8000+ - [HarmonyTranspiler] - [HarmonyPatch(typeof(ABN_VeinsUtil), nameof(ABN_VeinsUtil.CheckValue))] - private static IEnumerable ABN_VeinsUtil_CheckValue_Transpiler(IEnumerable instructions, ILGenerator generator) - { - var matcher = new CodeMatcher(instructions, generator); - matcher.MatchForward(false, - new CodeMatch(OpCodes.Ldelem_R8), - new CodeMatch(OpCodes.Conv_R4), - new CodeMatch(OpCodes.Add), - new CodeMatch(OpCodes.Stloc_1) - ); - // loc1 = Mathf.Round(n * 1000f) / 1000f; - matcher.Advance(3).Insert( - new CodeInstruction(OpCodes.Ldc_R4, 1000f), - new CodeInstruction(OpCodes.Mul), - new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Mathf), nameof(Mathf.Round))), - new CodeInstruction(OpCodes.Ldc_R4, 1000f), - new CodeInstruction(OpCodes.Div) - ); - return matcher.InstructionEnumeration(); - } - - // Bring popup tip window to top layer - [HarmonyTranspiler] - [HarmonyPatch(typeof(UIButton), nameof(UIButton.LateUpdate))] - private static IEnumerable UIButton_LateUpdate_Transpiler(IEnumerable instructions, ILGenerator generator) - { - var matcher = new CodeMatcher(instructions, generator); - matcher.MatchForward(false, - new CodeMatch(OpCodes.Ldloc_2), - new CodeMatch(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(Component), nameof(Component.gameObject))), - new CodeMatch(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(GameObject), nameof(GameObject.activeSelf))) - ); - var labels = matcher.Labels; - matcher.Labels = null; - matcher.Insert( - new CodeInstruction(OpCodes.Ldloc_2).WithLabels(labels), - new CodeInstruction(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(Component), nameof(Component.transform))), - new CodeInstruction(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(Transform), nameof(Transform.parent))), - new CodeInstruction(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(Transform), nameof(Transform.parent))), - new CodeInstruction(OpCodes.Callvirt, AccessTools.Method(typeof(Transform), nameof(Transform.SetAsLastSibling))) - ); - return matcher.InstructionEnumeration(); - } - - // Sort blueprint structures by item id, model index, recipe id, area index, and position before saving - [HarmonyPostfix] - [HarmonyPatch(typeof(BlueprintUtils), nameof(BlueprintUtils.GenerateBlueprintData))] - private static void BlueprintUtils_GenerateBlueprintData_Postfix(BlueprintData _blueprintData) - { - var buildings = _blueprintData.buildings; - Array.Sort(buildings, (a, b) => - { - var tmpItemId = a.itemId - b.itemId; - if (tmpItemId != 0) - return tmpItemId; - var tmpModelIndex = a.modelIndex - b.modelIndex; - if (tmpModelIndex != 0) - return tmpModelIndex; - var tmpRecipeId = a.recipeId - b.recipeId; - if (tmpRecipeId != 0) - return tmpRecipeId; - var tmpAreaIndex = a.areaIndex - b.areaIndex; - if (tmpAreaIndex != 0) - return tmpAreaIndex; - const double ky = 256.0; - const double kx = 1024.0; - var scorePosA = (a.localOffset_y * ky + a.localOffset_x) * kx + a.localOffset_z; - var scorePosB = (b.localOffset_y * ky + b.localOffset_x) * kx + b.localOffset_z; - return scorePosA < scorePosB ? 1 : -1; - }); - for (var i = buildings.Length - 1; i >= 0; i--) - { - buildings[i].index = i; - } - } - - // Increase maximum value of property realizing, 2000 -> 20000 - [HarmonyTranspiler] - [HarmonyPatch(typeof(UIPropertyEntry), nameof(UIPropertyEntry.UpdateUIElements))] - [HarmonyPatch(typeof(UIPropertyEntry), nameof(UIPropertyEntry.OnRealizeButtonClick))] - [HarmonyPatch(typeof(UIPropertyEntry), nameof(UIPropertyEntry.OnInputValueEnd))] - private static IEnumerable UIProductEntry_UpdateUIElements_Transpiler(IEnumerable instructions, ILGenerator generator) - { - var matcher = new CodeMatcher(instructions, generator); - matcher.MatchForward(false, - new CodeMatch(OpCodes.Ldc_I4, 2000) - ); - matcher.Repeat(m => { m.SetAndAdvance(OpCodes.Ldc_I4, 20000); }); - return matcher.InstructionEnumeration(); - } - - [HarmonyTranspiler] - [HarmonyPatch(typeof(UIPropertyEntry), nameof(UIPropertyEntry.OnInputValueEnd))] - private static IEnumerable UIProductEntry_OnInputValueEnd_Transpiler(IEnumerable instructions, ILGenerator generator) - { - var matcher = new CodeMatcher(instructions, generator); - matcher.MatchForward(false, - new CodeMatch(ci => ci.opcode == OpCodes.Ldc_R4 && ci.OperandIs(2000f)) - ); - matcher.Repeat(m => { m.SetAndAdvance(OpCodes.Ldc_R4, 20000f); }); - return matcher.InstructionEnumeration(); - } - - // Increase capacity of player order queue, 16 -> 128 - [HarmonyTranspiler] - [HarmonyPatch(typeof(PlayerOrder), MethodType.Constructor, typeof(Player))] - private static IEnumerable PlayerOrder_Constructor_Transpiler(IEnumerable instructions, ILGenerator generator) - { - var matcher = new CodeMatcher(instructions, generator); - matcher.MatchForward(false, - new CodeMatch(ci => (ci.opcode == OpCodes.Ldc_I4_S || ci.opcode == OpCodes.Ldc_I4) && ci.OperandIs(16)) - ); - matcher.Repeat(m => { m.SetAndAdvance(OpCodes.Ldc_I4, 128); }); - return matcher.InstructionEnumeration(); - } - - // Increase Player Command Queue from 16 to 128 - [HarmonyTranspiler] - [HarmonyPatch(typeof(PlayerOrder), nameof(PlayerOrder._trimEnd))] - [HarmonyPatch(typeof(PlayerOrder), nameof(PlayerOrder.Enqueue))] - private static IEnumerable PlayerOrder_ExtendCount_Transpiler(IEnumerable instructions, ILGenerator generator) - { - var matcher = new CodeMatcher(instructions, generator); - matcher.MatchForward(false, - new CodeMatch(ci => (ci.opcode == OpCodes.Ldc_I4_S || ci.opcode == OpCodes.Ldc_I4) && ci.OperandIs(16)) - ); - matcher.Repeat(m => { m.SetAndAdvance(OpCodes.Ldc_I4, 128); }); - return matcher.InstructionEnumeration(); - } - - // Allow F11 in star map - [HarmonyTranspiler] - [HarmonyPatch(typeof(UIGame), nameof(UIGame._OnLateUpdate))] - private static IEnumerable UIGame__OnLateUpdate_Transpiler(IEnumerable instructions, ILGenerator generator) - { - var matcher = new CodeMatcher(instructions, generator); - matcher.MatchForward(false, - new CodeMatch(OpCodes.Ldsfld, AccessTools.Field(typeof(VFInput), nameof(VFInput.inFullscreenGUI))), - new CodeMatch(ci => ci.opcode == OpCodes.Brfalse || ci.opcode == OpCodes.Brfalse_S) - ); - var jumpPos = matcher.Advance(1).Operand; - matcher.Advance(-1).Insert( - new CodeInstruction(OpCodes.Ldarg_0), - new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(UIGame), nameof(UIGame.starmap))), - new CodeInstruction(OpCodes.Callvirt, AccessTools.PropertyGetter(typeof(ManualBehaviour), nameof(ManualBehaviour.active))), - new CodeInstruction(OpCodes.Brtrue_S, jumpPos) - ); - return matcher.InstructionEnumeration(); - } - - // Ignore UIDFCommunicatorWindow.Determine() - [HarmonyPrefix] - [HarmonyPatch(typeof(UIDFCommunicatorWindow), nameof(UIDFCommunicatorWindow.Determine))] - private static bool UIDFCommunicatorWindow_Determine_Prefix() - { - return false; - } - } }