diff --git a/CheatEnabler/CHANGELOG.md b/CheatEnabler/CHANGELOG.md index 5f08743..2468b6f 100644 --- a/CheatEnabler/CHANGELOG.md +++ b/CheatEnabler/CHANGELOG.md @@ -4,13 +4,9 @@ ## Changlog * 2.4.7 - * `Unlock Tech with Key-Modifiers` becomes `Quickly upgrade/downgrade tech with key-modifiers`. - * Holding `Shift` / `Ctrl` / `Ctrl + Shift` / `Alt` while clicking a tech adds 1 / 10 / 100 / all levels; all direct prerequisites are unlocked as well. - * `Caps Lock` toggles downgrade mode; the same modifiers then subtract 1 / 10 / 100 levels, or revert the tech to locked. - * Downgrading reverts the tech's upgrade values as well, including multiplicative, assignment, and boolean tech functions, so repeated up/down cycles do not drift. - * Toggling downgrade mode reports itself like the other toggles: a realtime tip popup plus a critical warning banner. The banner shows on its own, without requiring `Build without condition` or `No collision` to be on, and outranks both of them while they are. - * The `?` button in the tech tree window shows the full upgrade and downgrade key reference, using the same title and text as the config panel's tip button. - * While the option is off, neither the `?` button nor the `Caps Lock` toggle is active, so the key cannot collide with other bindings. + * Extend `Unlock Tech with Key-Modifiers` with a downgrade mode. + * Press `Caps Lock` to toggle downgrade mode. Modifier clicks lower tech levels and revert their bonuses and dependent tech levels; `Alt` returns the tech to locked. + * The current mode is shown by a popup and warning banner. A help button in the tech tree explains the key combinations. * 2.4.6 * `Architect mode`: Restore unlimited use of buildings, Logistics Bots, Logistics Drones, and Logistics Vessels, and increase the default displayed count from 100 to 999. * `Wind Turbines do global power coverage`: Fix a possible startup crash when enabled. @@ -205,13 +201,9 @@ ## 更新日志 * 2.4.7 - * `使用组合键点击解锁科技` 改为 `使用组合键快速升降级科技`。 - * 按住 `Shift` / `Ctrl` / `Ctrl + Shift` / `Alt` 点击科技,分别提升 1 / 10 / 100 / 全部等级;所有直接前置科技也会被解锁。 - * `Caps Lock` 切换降级模式,开启后同样的组合键变为降低 1 / 10 / 100 等级,或将科技回退到未解锁。 - * 降级同时回退科技带来的升级数值,覆盖乘法型、赋值型和布尔型科技函数,因此反复升降级不会产生数值漂移。 - * 切换降级模式会像其它开关一样提示:实时弹窗 + 严重警告横幅。横幅可独立显示,不需要开启`无条件建造`或`无碰撞`;这两个开启时,降级模式的提示优先显示。 - * 科技树界面的 `?` 按钮显示完整的升降级组合键说明,标题与文本同 MOD 设置面板的提示按钮一致。 - * 关闭该选项时,科技界面不显示 `?` 按钮,`Caps Lock` 也不生效,避免与原版其它键位冲突。 + * `使用组合键点击解锁科技` 新增降级模式。 + * 按 `Caps Lock` 切换降级模式。按住组合键点击可降低科技等级,同时回退升级效果及后继科技等级;`Alt` 将科技回退到未解锁。 + * 弹窗和警告横幅显示当前模式,科技树中的帮助按钮提供组合键说明。 * 2.4.6 * `建筑师模式`:修复建筑、配送运输机、物流运输机和星际物流运输船无法无限使用的问题,默认显示数量由 100 提高至 999。 * `风力涡轮机供电覆盖全球`:修复启用后可能导致启动崩溃的问题。 diff --git a/CheatEnabler/CheatEnabler.cs b/CheatEnabler/CheatEnabler.cs index 1f5604d..a6491ab 100644 --- a/CheatEnabler/CheatEnabler.cs +++ b/CheatEnabler/CheatEnabler.cs @@ -24,7 +24,7 @@ public class CheatEnabler : BaseUnityPlugin GamePatch.AbnormalDisablerEnabled = Config.Bind("General", "DisableAbnormalChecks", false, "disable all abnormal checks"); TechTweaksPatch.Enabled = Config.Bind("General", "UnlockTech", false, - "Unlock clicked tech by holding key-modifilers(Shift/Alt/Ctrl), and downgrade them by holding the " + + "Upgrade clicked tech by holding key modifiers (Shift/Alt/Ctrl), and downgrade it by holding the " + "same modifiers while downgrade mode is on (toggle it with Caps Lock)"); FactoryPatch.ImmediateEnabled = Config.Bind("Build", "ImmediateBuild", false, "Build immediately"); @@ -110,4 +110,4 @@ public class CheatEnabler : BaseUnityPlugin I18N.Apply(); } -} \ No newline at end of file +} diff --git a/CheatEnabler/Localization.cs b/CheatEnabler/Localization.cs index 4fe2461..7a24730 100644 --- a/CheatEnabler/Localization.cs +++ b/CheatEnabler/Localization.cs @@ -58,6 +58,10 @@ Press Caps Lock to toggle Tech Downgrade Mode: the key-modifiers above then decr The features above come from the mod: CheatEnabler """; public const string PackageResizeSkippedOccupiedCells = "Package resize skipped: occupied cells would be removed"; + public const string TechDowngradeDeliveryOccupied = "Cannot downgrade tech: empty the delivery slots being removed and wait for deliveries to finish"; + public const string TechDowngradeDronesBusy = "Cannot downgrade tech: wait for construction drones to return"; + public const string TechDowngradeFleetsOccupied = "Cannot downgrade tech: recall and empty the fleets being removed"; + public const string TechDowngradeUnsupportedFunction = "Cannot downgrade tech: an unsupported tech function requires a mod update"; public const string DevShortcuts = "Dev Shortcuts"; public const string DevShortcutsTips = "Dev Shortcuts Tips"; public const string UnlockTechWithKeyModifiersTips = "Unlock Tech with Key-Modifiers Tips"; @@ -207,8 +211,17 @@ The features above come from the mod: CheatEnabler 以上功能来源于MOD:CheatEnabler """); - I18N.Add(PackageResizeSkippedOccupiedCells, "Package resize skipped: occupied cells would be removed", - "背包扩容回退已跳过:有物品的格子会被移除,未缩小背包"); + I18N.Add(PackageResizeSkippedOccupiedCells, + "Cannot downgrade tech: empty the inventory cells being removed and retry", + "无法降级科技:请先清空缩容将移除的机舱格子,再重试"); + I18N.Add(TechDowngradeDeliveryOccupied, TechDowngradeDeliveryOccupied, + "无法降级科技:请先清空缩容将移除的物流清单格子,并等待配送完成"); + I18N.Add(TechDowngradeDronesBusy, TechDowngradeDronesBusy, + "无法降级科技:请等待建设无人机返回"); + I18N.Add(TechDowngradeFleetsOccupied, TechDowngradeFleetsOccupied, + "无法降级科技:请先召回并清空缩容将移除的编队"); + I18N.Add(TechDowngradeUnsupportedFunction, TechDowngradeUnsupportedFunction, + "无法降级科技:存在尚未支持的科技效果,请更新 MOD 后重试"); I18N.Add(DevShortcuts, "Dev Shortcuts", "开发模式快捷键"); I18N.Add(DevShortcutsTips, """ Caution: Some function may trigger abnormal check! diff --git a/CheatEnabler/Patches/Factory/FactoryPatch.cs b/CheatEnabler/Patches/Factory/FactoryPatch.cs index 5832e89..583e3a1 100644 --- a/CheatEnabler/Patches/Factory/FactoryPatch.cs +++ b/CheatEnabler/Patches/Factory/FactoryPatch.cs @@ -8,6 +8,7 @@ using HarmonyLib; using UnityEngine; using UXAssist.Common; using UXAssist.Common.ModFeatures; +using UXAssist.Common.Patching; using GameLogicProc = UXAssist.Common.GameLogic; namespace CheatEnabler.Patches.Factory; @@ -202,95 +203,91 @@ public class FactoryPatch : PatchImpl } GameMain.data?.warningSystem?.UpdateCriticalWarningText(); } + + private static bool HasCustomCriticalWarning() => + Tech.DowngradeMode.IsOn || NoConditionEnabled?.Value == true || NoCollisionEnabled?.Value == true; + + private static string GetCustomCriticalWarningText() + { + if (Tech.DowngradeMode.IsOn) return Localization.TechDowngradeModeWarning.Translate(); + if (NoConditionEnabled?.Value == true) return Localization.BuildWithoutConditionIsEnabled.Translate(); + if (NoCollisionEnabled?.Value == true) return Localization.NoCollisionIsEnabled.Translate(); + return null; + } + + private static void ApplyCustomCriticalWarningText(WarningSystem warning) + { + string text = GetCustomCriticalWarningText(); + if (text != null) warning.criticalWarningTexts = text + "\r\n"; + } + // Harmony transpiler: WarningSystem_hasCriticalWarning_Transpiler // Target: WarningSystem.hasCriticalWarning (getter) - // Fallback: None — patch will fail loudly if the target method body changes. + // Fallback: TranspilerGuard returns original instructions and logs a warning. [HarmonyTranspiler] [HarmonyPatch(typeof(WarningSystem), nameof(WarningSystem.hasCriticalWarning), MethodType.Getter)] - private static IEnumerable WarningSystem_hasCriticalWarning_Transpiler(IEnumerable instructions, ILGenerator generator) + private static IEnumerable WarningSystem_hasCriticalWarning_Transpiler( + IEnumerable instructions, ILGenerator generator) { - // The original body is `criticalWarnings.Count > 0`. The chain appended here ORs the feature - // switches on top of it, in display priority order: whichever one matches first owns the banner, - // because UpdateCriticalWarningText bails out before assigning any custom text when this getter is - // false. Tech downgrade mode therefore has to come first: it must show on its own, without either - // build toggle being on. var matcher = new CodeMatcher(instructions, generator); - var downgrade = generator.DefineLabel(); - var noCondition = generator.DefineLabel(); - var noCollision = generator.DefineLabel(); - matcher.End().MatchBack(false, - new CodeMatch(OpCodes.Ret) - ).RemoveInstructions(1); - matcher.InsertAndAdvance( - new CodeInstruction(OpCodes.Brfalse, downgrade), - new CodeInstruction(OpCodes.Ldc_I4_1), - new CodeInstruction(OpCodes.Ret), - new CodeInstruction(OpCodes.Call, AccessTools.PropertyGetter(typeof(Tech.DowngradeMode), nameof(Tech.DowngradeMode.IsOn))).WithLabels(downgrade), - new CodeInstruction(OpCodes.Brfalse, noCondition), - new CodeInstruction(OpCodes.Ldc_I4_1), - new CodeInstruction(OpCodes.Ret), - new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(FactoryPatch), nameof(NoConditionEnabled))).WithLabels(noCondition), - new CodeInstruction(OpCodes.Call, AccessTools.PropertyGetter(typeof(ConfigEntry), nameof(ConfigEntry.Value))), - new CodeInstruction(OpCodes.Brfalse, noCollision), - new CodeInstruction(OpCodes.Ldc_I4_1), - new CodeInstruction(OpCodes.Ret), - new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(FactoryPatch), nameof(NoCollisionEnabled))).WithLabels(noCollision), - new CodeInstruction(OpCodes.Call, AccessTools.PropertyGetter(typeof(ConfigEntry), nameof(ConfigEntry.Value))), - new CodeInstruction(OpCodes.Ret) - ); - return matcher.InstructionEnumeration(); + matcher.End().MatchBack(false, new CodeMatch(OpCodes.Ret)); + if (matcher.IsInvalid) + { + return matcher.Finish(instructions, global::CheatEnabler.CheatEnabler.Logger, + nameof(WarningSystem_hasCriticalWarning_Transpiler)); + } + + var labels = matcher.Labels; + matcher.Labels = new List