Procházet zdrojové kódy

【修复】配置变更加防抖避免拖动滑块卡顿

ConfigurationManager 的 DrawRangeField 在滑块变化超过区间千分之一时几乎每帧
写值,每次写值都会触发 ConfigFile.SettingChanged;原先逐次应用会让体积光、
GPUInstancer、DistantShadow 等消费方连续重建 RenderTexture,战局内拖动滑块
会连续卡顿。

- 配置订阅移到 Plugin,变更后重启 250 ms 实时协程,停手后再应用一次。
- OpticCameraManager.OnOpticDisabled 路径保持立即应用。
- 服务层移除 HookSettings 与配置订阅,只保留镜内管理器订阅。
- docs/principle.md 与 docs/configuration.md 补充防抖说明。
PJ568 před 1 měsícem
rodič
revize
b6927a9835

+ 2 - 2
docs/configuration.md

@@ -2,7 +2,7 @@
 
 配置文件:`BepInEx/config/com.pj568.performancescope.cfg`(也可用 BepInEx ConfigurationManager 修改)。
 
-本插件不自带任何自定义界面或热键。安装 [BepInEx ConfigurationManager](https://github.com/BepInEx/BepInEx.ConfigurationManager) 后,游戏内按 **F12**(SPT 默认;上游默认 F1,可在其配置文件中改键)打开菜单,在 `PerformanceScope` 分区调整;改动即时生效(事件驱动,无每帧轮询)。
+本插件不自带任何自定义界面或热键。安装 [BepInEx ConfigurationManager](https://github.com/BepInEx/BepInEx.ConfigurationManager) 后,游戏内按 **F12**(SPT 默认;上游默认 F1,可在其配置文件中改键)打开菜单,在 `PerformanceScope` 分区调整;改动即时生效(事件驱动,无每帧轮询;为避免拖动滑块时反复重建 RenderTexture,变更在停止操作约 250 ms 后应用)。
 
 ## 配置表
 
@@ -38,7 +38,7 @@
 
 Config file: `BepInEx/config/com.pj568.performancescope.cfg` (or edit through BepInEx ConfigurationManager).
 
-The plugin ships no custom UI or hotkeys. Install [BepInEx ConfigurationManager](https://github.com/BepInEx/BepInEx.ConfigurationManager), then press **F12** in game (the SPT default; upstream defaults to F1, rebindable in its config) and adjust the `PerformanceScope` sections; changes apply immediately (event-driven, no per-frame polling).
+The plugin ships no custom UI or hotkeys. Install [BepInEx ConfigurationManager](https://github.com/BepInEx/BepInEx.ConfigurationManager), then press **F12** in game (the SPT default; upstream defaults to F1, rebindable in its config) and adjust the `PerformanceScope` sections; changes apply immediately (event-driven, no per-frame polling; to avoid repeated RenderTexture rebuilds while dragging a slider, a change is applied about 250 ms after you stop).
 
 ## Config Table
 

+ 2 - 2
docs/principle.md

@@ -67,7 +67,7 @@
 插件不做任何每帧轮询,只在三类时机工作:
 
 1. **战局开始**:游戏的 `Init()` 调用 `SetResolution`,prefix 直接把入参改成目标值,RT 一次建成,无需运行时补偿。
-2. **配置变更**:订阅 `ConfigFile.SettingChanged`;当前未瞄准则立即应用,正在瞄准且「瞄准中立即应用」为真时也立即应用。
+2. **配置变更**:插件订阅 `ConfigFile.SettingChanged`,防抖 250 ms 后应用一次(ConfigurationManager 拖动滑块时几乎每帧写值,逐次应用会连续重建);未瞄准则应用,正在瞄准且「瞄准中立即应用」为真时也应用。
 3. **退出镜内**:订阅 `OpticCameraManager.OnOpticDisabled`(在 `CurrentOpticSight` 置空且镜内相机停用之后触发,是安全的重建窗口),在此补齐被推迟的改动。
 
 订阅按 `OpticCameraManager` 实例幂等:战局切换导致管理器重建时,prefix 会把新实例交给服务并退订旧实例。
@@ -162,7 +162,7 @@ The plugin therefore uses a Harmony **prefix** to override the argument of `SetR
 The plugin does no per-frame polling; it only acts on three triggers:
 
 1. **Raid start**: the game's `Init()` calls `SetResolution`, and the prefix replaces the argument with the target value, so the RT is created once at the right size with no runtime compensation.
-2. **Config change**: it subscribes to `ConfigFile.SettingChanged`; the change applies immediately when not scoped, and also when scoped if "Apply While Scoped" is on.
+2. **Config change**: the plugin subscribes to `ConfigFile.SettingChanged` and applies once after a 250 ms debounce (dragging a ConfigurationManager slider writes the value on almost every frame, and applying each time would rebuild repeatedly); it applies when not scoped, and also when scoped if "Apply While Scoped" is on.
 3. **Scope exit**: it subscribes to `OpticCameraManager.OnOpticDisabled` (fired after `CurrentOpticSight` is cleared and the optic camera is deactivated — a safe rebuild window), where deferred changes are applied.
 
 Subscriptions are idempotent per `OpticCameraManager` instance: when a raid change rebuilds the manager, the prefix hands the new instance to the service and unsubscribes the old one.

+ 5 - 30
src/PerformanceScope.Plugin/OpticResolutionService.cs

@@ -1,5 +1,4 @@
 using System;
-using BepInEx.Configuration;
 using EFT.CameraControl;
 using PerformanceScope.Core;
 using UnityEngine;
@@ -8,11 +7,11 @@ namespace PerformanceScope
 {
     /// <summary>
     /// 镜内 PiP 分辨率的决策与应用,完全事件驱动,不做任何每帧轮询:
-    /// 配置变更来自 ConfigFile.SettingChanged,瞄具退出来自 OpticCameraManager.OnOpticDisabled,
+    /// 配置变更由插件防抖后调用 <see cref="TryApply"/>,瞄具退出来自 OpticCameraManager.OnOpticDisabled,
     /// 战局开始则由 SetResolution 的 prefix 直接覆盖(无需运行时补偿)。
     /// Decision and application of the in-scope PiP resolution, fully event-driven with no per-frame polling:
-    /// config changes come from ConfigFile.SettingChanged, scope exit from OpticCameraManager.OnOpticDisabled,
-    /// and raid start is covered directly by the SetResolution prefix.
+    /// config changes are debounced by the plugin which then calls <see cref="TryApply"/>, scope exit comes from
+    /// OpticCameraManager.OnOpticDisabled, and raid start is covered directly by the SetResolution prefix.
     /// </summary>
     internal sealed class OpticResolutionService
     {
@@ -47,30 +46,11 @@ namespace PerformanceScope
         }
 
         /// <summary>
-        /// 订阅配置变更(ConfigFile 级事件)。
-        /// Subscribe to config changes (ConfigFile-level event).
-        /// </summary>
-        public void HookSettings()
-        {
-            if (PluginConfig.File == null)
-            {
-                return;
-            }
-
-            PluginConfig.File.SettingChanged += OnSettingChanged;
-        }
-
-        /// <summary>
-        /// 取消全部订阅(配置与镜内管理器)。
-        /// Remove every subscription (config and optic camera manager).
+        /// 取消镜内管理器的订阅。
+        /// Remove the optic camera manager subscription.
         /// </summary>
         public void Unhook()
         {
-            if (PluginConfig.File != null)
-            {
-                PluginConfig.File.SettingChanged -= OnSettingChanged;
-            }
-
             if (_hookedManager != null)
             {
                 try
@@ -145,11 +125,6 @@ namespace PerformanceScope
             Apply(manager, target);
         }
 
-        private void OnSettingChanged(object sender, SettingChangedEventArgs e)
-        {
-            TryApply();
-        }
-
         private void OnOpticDisabled()
         {
             TryApply();

+ 59 - 6
src/PerformanceScope.Plugin/Plugin.cs

@@ -1,41 +1,72 @@
 using System;
+using System.Collections;
 using BepInEx;
+using BepInEx.Configuration;
 using HarmonyLib;
+using UnityEngine;
 
 namespace PerformanceScope
 {
     /// <summary>
     /// 插件入口:初始化配置、日志、Harmony 补丁与分辨率服务。
-    /// 所有调参通过 BepInEx ConfigurationManager 在游戏内完成(SPT 中默认 F12,可在其配置中改键),
-    /// 服务层事件驱动地响应配置变更并即时生效;本插件不提供自定义 UI 或热键。
+    /// 所有调参通过 BepInEx ConfigurationManager 在游戏内完成(SPT 中默认 F12,可在其配置中改键);
+    /// 配置变更在此防抖后交给服务应用,瞄具退出等事件由服务自行订阅。本插件不提供自定义 UI 或热键。
     /// Plugin entry point: initializes config, logging, Harmony patches and the resolution service.
     /// All tweaking is done in-game via BepInEx ConfigurationManager (F12 by default in SPT; rebindable in its config);
-    /// the service reacts to config changes in an event-driven way and applies them immediately. This plugin offers no custom UI or hotkey.
+    /// config changes are debounced here before being handed to the service, while events such as scope exit are
+    /// subscribed by the service itself. This plugin offers no custom UI or hotkey.
     /// </summary>
     [BepInPlugin("com.pj568.performancescope", "PerformanceScope", "0.1.0")]
     public class PerformanceScopePlugin : BaseUnityPlugin
     {
         private const string HarmonyId = "com.pj568.performancescope";
 
+        /// <summary>
+        /// 配置变更的防抖时长(秒)。拖动 ConfigurationManager 滑块时几乎每帧都在写值,
+        /// 若每次都重建 RenderTexture 会连续卡顿,因此等停手后再应用一次。
+        /// Debounce window (seconds) for config changes. Dragging a ConfigurationManager slider writes the value on
+        /// almost every frame, and rebuilding the RenderTexture each time would stutter, so apply once after it settles.
+        /// </summary>
+        private const float ApplyDebounceSeconds = 0.25f;
+
+        private readonly WaitForSecondsRealtime _debounceDelay = new WaitForSecondsRealtime(ApplyDebounceSeconds);
+
         private Harmony _harmony;
 
+        private Coroutine _pendingApply;
+
         private void Awake()
         {
             Log.Init(Logger);
             PluginConfig.Init(Config);
             ApplyHarmonyPatches();
 
-            // 事件驱动:创建服务并订阅配置变更,不再每帧轮询。
-            // Event-driven: create the service and subscribe to config changes; no per-frame polling.
             OpticResolutionService service = new OpticResolutionService();
             OpticResolutionService.Instance = service;
-            service.HookSettings();
+
+            // 事件驱动:订阅配置变更(防抖后应用),不做任何每帧轮询。
+            // Event-driven: subscribe to config changes (applied after a debounce); no per-frame polling.
+            if (PluginConfig.File != null)
+            {
+                PluginConfig.File.SettingChanged += OnSettingChanged;
+            }
 
             Log.Info("PerformanceScope 已加载 | loaded(在 ConfigurationManager 菜单中调参 / configure via ConfigurationManager)");
         }
 
         private void OnDestroy()
         {
+            if (PluginConfig.File != null)
+            {
+                PluginConfig.File.SettingChanged -= OnSettingChanged;
+            }
+
+            if (_pendingApply != null)
+            {
+                StopCoroutine(_pendingApply);
+                _pendingApply = null;
+            }
+
             OpticResolutionService.Instance?.Unhook();
 
             if (_harmony == null)
@@ -53,6 +84,28 @@ namespace PerformanceScope
             }
         }
 
+        /// <summary>
+        /// 配置变更回调:重启防抖计时,停手后再应用一次,避免拖动滑块期间反复重建 RenderTexture。
+        /// Config change callback: restart the debounce timer and apply once after the changes settle, avoiding
+        /// repeated RenderTexture rebuilds while a slider is being dragged.
+        /// </summary>
+        private void OnSettingChanged(object sender, SettingChangedEventArgs e)
+        {
+            if (_pendingApply != null)
+            {
+                StopCoroutine(_pendingApply);
+            }
+
+            _pendingApply = StartCoroutine(ApplyAfterDebounce());
+        }
+
+        private IEnumerator ApplyAfterDebounce()
+        {
+            yield return _debounceDelay;
+            _pendingApply = null;
+            OpticResolutionService.Instance?.TryApply();
+        }
+
         private void ApplyHarmonyPatches()
         {
             try

+ 2 - 2
src/PerformanceScope.Plugin/PluginConfig.cs

@@ -16,8 +16,8 @@ namespace PerformanceScope
         private const string SectionResolution = "2. 镜内分辨率 · Scope Resolution";
 
         /// <summary>
-        /// 底层配置文件,供服务层订阅 <c>SettingChanged</c> 事件。
-        /// The underlying config file, used by the service layer to subscribe to <c>SettingChanged</c>.
+        /// 底层配置文件,供插件订阅 <c>SettingChanged</c> 事件。
+        /// The underlying config file, used by the plugin to subscribe to <c>SettingChanged</c>.
         /// </summary>
         public static ConfigFile File { get; private set; }