Sfoglia il codice sorgente

【新增】置灰主菜单开局按钮并显示限制提示

PJ568 1 mese fa
parent
commit
c099d4ded3

+ 9 - 0
src/OneShot.Plugin/OneShot.Plugin.csproj

@@ -62,6 +62,15 @@
       <HintPath>$(SPT_MANAGED)/Newtonsoft.Json.dll</HintPath>
       <Private>false</Private>
     </Reference>
+    <!-- MenuScreen 的基类链依赖 Sirenix(SerializedMonoBehaviour),补丁签名编译需要。 -->
+    <Reference Include="Sirenix.Serialization">
+      <HintPath>$(SPT_MANAGED)/Sirenix.Serialization.dll</HintPath>
+      <Private>false</Private>
+    </Reference>
+    <Reference Include="Sirenix.Utilities">
+      <HintPath>$(SPT_MANAGED)/Sirenix.Utilities.dll</HintPath>
+      <Private>false</Private>
+    </Reference>
   </ItemGroup>
 
   <!-- 本地 Release 构建后自动部署到游戏插件目录(可用 -p:DeployToGame=false 关闭) -->

+ 92 - 0
src/OneShot.Plugin/Patches/PlayButtonStatePatch.cs

@@ -0,0 +1,92 @@
+using System;
+using EFT;
+using EFT.UI;
+using EFT.UI.Matchmaker;
+using HarmonyLib;
+using OneShot.Core;
+
+namespace OneShot.Patches
+{
+    /// <summary>
+    /// 主菜单“开始游戏”按钮状态补丁:限制生效时置灰并显示本地化悬停提示,未生效时不干预、交由游戏原生逻辑决定。
+    /// 仅通过原生 <see cref="DefaultUIButton.Interactable"/> 置灰(原生 alpha 0.3 且禁用点击),不自行改色、改布局或新建 UI。
+    /// Patches the main menu "play" button: greys it out with a localized tooltip while the restriction is
+    /// active, and leaves it untouched otherwise so native game logic decides. Greying only relies on the native
+    /// <see cref="DefaultUIButton.Interactable"/> (native alpha 0.3 and click disabled); no recoloring,
+    /// re-layout or new UI elements are introduced.
+    /// </summary>
+    [HarmonyPatch(typeof(MenuScreen))]
+    internal static class PlayButtonStatePatch
+    {
+        /// <summary>
+        /// 在 <see cref="MenuScreen.Show(Profile, MatchmakerPlayersController, ESessionMode)"/> 之后施加状态。
+        /// 该方法内部会先把按钮设为可用,故禁用态必须在其后施加。
+        /// Applies the state after <see cref="MenuScreen.Show(Profile, MatchmakerPlayersController, ESessionMode)"/>,
+        /// which internally re-enables the button first, so the disabled state must be applied afterwards.
+        /// </summary>
+        [HarmonyPostfix]
+        [HarmonyWrapSafe]
+        [HarmonyPatch("Show", new Type[] { typeof(Profile), typeof(MatchmakerPlayersController), typeof(ESessionMode) })]
+        private static void ShowPostfix(MenuScreen __instance)
+        {
+            ApplyState(__instance);
+        }
+
+        /// <summary>
+        /// 在 <see cref="MenuScreen.OnMatchingTypeUpdate"/> 之后重新施加状态。
+        /// 该方法会按组队匹配状态重设 <c>_playButton.Interactable</c>,可能覆盖禁用态,故需在其后复算。
+        /// Re-applies the state after <see cref="MenuScreen.OnMatchingTypeUpdate"/>, which resets
+        /// <c>_playButton.Interactable</c> according to the group matching status and may override the disabled state.
+        /// </summary>
+        [HarmonyPostfix]
+        [HarmonyWrapSafe]
+        [HarmonyPatch("OnMatchingTypeUpdate")]
+        private static void OnMatchingTypeUpdatePostfix(MenuScreen __instance)
+        {
+            ApplyState(__instance);
+        }
+
+        /// <summary>
+        /// 计算并施加“开始游戏”按钮状态:仅限制生效时置灰并挂提示,否则直接返回、不触碰按钮。幂等:重复调用不会叠加副作用。
+        /// Computes and applies the play button state: only greys it out with a tooltip when blocked, otherwise returns without
+        /// touching the button. Idempotent: repeated calls accumulate no side effects.
+        /// </summary>
+        private static void ApplyState(MenuScreen menuScreen)
+        {
+            // 空引用保护:游戏版本缺成员时安全降级。
+            // Null guard: degrade safely when a game member is missing.
+            if (menuScreen == null || menuScreen._playButton == null)
+            {
+                return;
+            }
+
+            bool enabled = PluginConfig.Enabled != null && PluginConfig.Enabled.Value;
+            bool blocked = OneShotPlugin.Gate != null && OneShotPlugin.Gate.ShouldBlockStart(enabled);
+
+            if (!blocked)
+            {
+                // 非限制态不干预:交由游戏原生逻辑决定按钮状态。
+                // 原生 OnMatchingTypeUpdate(GroupPlayer) 会在特定情形禁用按钮并挂组队提示,此处若强置 true 会抹掉该禁用态。
+                // When not blocked, leave the button alone so native game logic decides its state.
+                // Native OnMatchingTypeUpdate(GroupPlayer) may disable the button and attach a group tooltip; forcing true here would erase that state.
+                return;
+            }
+
+            // 先登记禁用态悬停提示,再置灰;Interactable 的 setter 会据当前状态刷新提示。
+            // Register the disabled tooltip first, then grey out; the Interactable setter refreshes the tooltip for the new state.
+            menuScreen._playButton.SetDisabledTooltip(GetBlockedTooltip);
+            menuScreen._playButton.Interactable = false;
+        }
+
+        /// <summary>
+        /// 读取游戏当前语言并返回“已被限制”的本地化提示;语言管理器缺失时回退英文。
+        /// Reads the current game culture and returns the localized "blocked" message; falls back to English when the localization manager is missing.
+        /// </summary>
+        private static string GetBlockedTooltip()
+        {
+            LocalizationManager manager = LocalizationManager.Instance;
+            string culture = manager == null ? null : manager.Culture;
+            return OneShotText.Get(LocaleSelector.Resolve(culture));
+        }
+    }
+}