1
0

Plugin.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections;
  3. using BepInEx;
  4. using BepInEx.Configuration;
  5. using HarmonyLib;
  6. using UnityEngine;
  7. namespace PerformanceScope
  8. {
  9. /// <summary>
  10. /// 插件入口:初始化配置、日志、Harmony 补丁与分辨率服务。
  11. /// 所有调参通过 BepInEx ConfigurationManager 在游戏内完成(SPT 中默认 F12,可在其配置中改键);
  12. /// 配置变更在此防抖后交给服务应用,瞄具退出等事件由服务自行订阅。本插件不提供自定义 UI 或热键。
  13. /// Plugin entry point: initializes config, logging, Harmony patches and the resolution service.
  14. /// All tweaking is done in-game via BepInEx ConfigurationManager (F12 by default in SPT; rebindable in its config);
  15. /// config changes are debounced here before being handed to the service, while events such as scope exit are
  16. /// subscribed by the service itself. This plugin offers no custom UI or hotkey.
  17. /// </summary>
  18. [BepInPlugin("com.pj568.performancescope", "PerformanceScope", "0.1.1")]
  19. public class PerformanceScopePlugin : BaseUnityPlugin
  20. {
  21. private const string HarmonyId = "com.pj568.performancescope";
  22. /// <summary>
  23. /// 配置变更的防抖时长(秒)。拖动 ConfigurationManager 滑块时几乎每帧都在写值,
  24. /// 若每次都重建 RenderTexture 会连续卡顿,因此等停手后再应用一次。
  25. /// Debounce window (seconds) for config changes. Dragging a ConfigurationManager slider writes the value on
  26. /// almost every frame, and rebuilding the RenderTexture each time would stutter, so apply once after it settles.
  27. /// </summary>
  28. private const float ApplyDebounceSeconds = 0.25f;
  29. private readonly WaitForSecondsRealtime _debounceDelay = new WaitForSecondsRealtime(ApplyDebounceSeconds);
  30. private Harmony _harmony;
  31. private Coroutine _pendingApply;
  32. private void Awake()
  33. {
  34. Log.Init(Logger);
  35. PluginConfig.Init(Config);
  36. ApplyHarmonyPatches();
  37. OpticQualityService service = new OpticQualityService();
  38. OpticQualityService.Instance = service;
  39. // 事件驱动:订阅配置变更(防抖后应用),不做任何每帧轮询。
  40. // Event-driven: subscribe to config changes (applied after a debounce); no per-frame polling.
  41. if (PluginConfig.File != null)
  42. {
  43. PluginConfig.File.SettingChanged += OnSettingChanged;
  44. }
  45. Log.Info("PerformanceScope 已加载 | loaded(在 ConfigurationManager 菜单中调参 / configure via ConfigurationManager)");
  46. }
  47. private void OnDestroy()
  48. {
  49. if (PluginConfig.File != null)
  50. {
  51. PluginConfig.File.SettingChanged -= OnSettingChanged;
  52. }
  53. if (_pendingApply != null)
  54. {
  55. StopCoroutine(_pendingApply);
  56. _pendingApply = null;
  57. }
  58. OpticQualityService.Instance?.Unhook();
  59. if (_harmony == null)
  60. {
  61. return;
  62. }
  63. try
  64. {
  65. _harmony.UnpatchSelf();
  66. }
  67. catch (Exception e)
  68. {
  69. Log.Error("卸载 Harmony 补丁失败 | Failed to unpatch Harmony patches: " + e);
  70. }
  71. }
  72. /// <summary>
  73. /// 配置变更回调:重启防抖计时,停手后再应用一次,避免拖动滑块期间反复重建 RenderTexture。
  74. /// Config change callback: restart the debounce timer and apply once after the changes settle, avoiding
  75. /// repeated RenderTexture rebuilds while a slider is being dragged.
  76. /// </summary>
  77. private void OnSettingChanged(object sender, SettingChangedEventArgs e)
  78. {
  79. if (_pendingApply != null)
  80. {
  81. StopCoroutine(_pendingApply);
  82. }
  83. _pendingApply = StartCoroutine(ApplyAfterDebounce());
  84. }
  85. private IEnumerator ApplyAfterDebounce()
  86. {
  87. yield return _debounceDelay;
  88. _pendingApply = null;
  89. OpticQualityService.Instance?.TryApply();
  90. }
  91. private void ApplyHarmonyPatches()
  92. {
  93. try
  94. {
  95. _harmony = new Harmony(HarmonyId);
  96. _harmony.PatchAll(typeof(SetResolutionPatch));
  97. }
  98. catch (Exception e)
  99. {
  100. Log.Error("应用 Harmony 补丁失败 | Failed to apply Harmony patches: " + e);
  101. }
  102. }
  103. }
  104. }