OpticResolutionService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using EFT.CameraControl;
  3. using PerformanceScope.Core;
  4. using UnityEngine;
  5. namespace PerformanceScope
  6. {
  7. /// <summary>
  8. /// 镜内 PiP 分辨率的决策与应用。每帧由插件调用 <see cref="Tick"/>,
  9. /// 根据配置与当前游戏状态决定何时调用 <see cref="OpticCameraManager.SetResolution"/>。
  10. /// 普通类,由插件在 Awake 中创建并注册到 <see cref="Instance"/>。
  11. /// Decision and application of the in-scope PiP resolution. The plugin calls <see cref="Tick"/> every frame,
  12. /// which decides when to call <see cref="OpticCameraManager.SetResolution"/> based on config and game state.
  13. /// A plain class, created by the plugin in Awake and assigned to <see cref="Instance"/>.
  14. /// </summary>
  15. internal sealed class OpticResolutionService
  16. {
  17. private bool _loggedNoCamera;
  18. private int _pendingTarget = ResolutionMath.GameDefaultResolution;
  19. private int _conflictTarget = int.MinValue;
  20. /// <summary>
  21. /// 当前服务实例;插件未加载时为 null,补丁据此透传原值。
  22. /// Current service instance; null when the plugin is not loaded, in which case patches pass the original value through.
  23. /// </summary>
  24. public static OpticResolutionService Instance { get; set; }
  25. /// <summary>
  26. /// 最近一次解析出的目标边长,供叠加显示。
  27. /// Most recently resolved target edge length, used for overlay display.
  28. /// </summary>
  29. public int LastTarget { get; private set; } = ResolutionMath.GameDefaultResolution;
  30. /// <summary>
  31. /// 是否有因瞄准中而推迟应用的目标。
  32. /// Whether a target is currently deferred because the player is scoped.
  33. /// </summary>
  34. public bool PendingApply { get; private set; }
  35. /// <summary>
  36. /// 是否因第三方改写导致目标无法生效而暂停重试。
  37. /// Whether retries are paused because a third-party rewrite kept the target from taking effect.
  38. /// </summary>
  39. public bool ConflictLocked { get; private set; }
  40. /// <summary>
  41. /// 计算当前配置对应的目标边长。模组关闭时返回游戏默认值,
  42. /// 从而让游戏恢复其原生 1024。
  43. /// Computes the target edge length for the current config. Returns the game default when the mod is off,
  44. /// so the game falls back to its native 1024.
  45. /// </summary>
  46. public int ComputeTarget()
  47. {
  48. if (PluginConfig.EnableMod?.Value != true)
  49. {
  50. return ResolutionMath.GameDefaultResolution;
  51. }
  52. return ResolutionMath.Resolve(
  53. PluginConfig.Mode.Value,
  54. Screen.height,
  55. PluginConfig.ScreenHeightRatio.Value,
  56. PluginConfig.AbsolutePixels.Value);
  57. }
  58. /// <summary>
  59. /// 每帧调用:在相机就绪时把配置分辨率同步给游戏。
  60. /// Called every frame: syncs the configured resolution to the game once the camera is ready.
  61. /// </summary>
  62. public void Tick()
  63. {
  64. OpticCameraManager ocm = GetOpticCameraManager();
  65. if (ocm == null || ocm.Camera == null)
  66. {
  67. if (!_loggedNoCamera)
  68. {
  69. _loggedNoCamera = true;
  70. Log.Verbose("镜内相机尚未就绪,暂不应用 | Optic camera not ready, skipping.");
  71. }
  72. return;
  73. }
  74. _loggedNoCamera = false;
  75. int target = ComputeTarget();
  76. LastTarget = target;
  77. if (ConflictLocked)
  78. {
  79. // 仅在目标变化(例如玩家改了配置)时解除锁定,避免每帧重建。
  80. // Unlock only when the target changes (e.g. the player edits config) to avoid per-frame rebuilds.
  81. if (target == _conflictTarget)
  82. {
  83. return;
  84. }
  85. ConflictLocked = false;
  86. }
  87. OpticApplyAction action = OpticResolutionDecision.Decide(
  88. ocm.OpticFinalResolution,
  89. target,
  90. ocm.IsAnyOpticCameraRendering,
  91. PluginConfig.ApplyWhileScoped?.Value == true);
  92. switch (action)
  93. {
  94. case OpticApplyAction.Apply:
  95. Apply(ocm, target);
  96. break;
  97. case OpticApplyAction.Defer:
  98. if (!PendingApply || _pendingTarget != target)
  99. {
  100. _pendingTarget = target;
  101. PendingApply = true;
  102. Log.Info($"镜内瞄准中,推迟应用分辨率 | scoped, deferring resolution {ocm.OpticFinalResolution} → {target}");
  103. }
  104. break;
  105. default:
  106. PendingApply = false;
  107. break;
  108. }
  109. }
  110. /// <summary>
  111. /// 安全地调用游戏接口重建镜内 RenderTexture。
  112. /// Safely calls the game API to rebuild the scope RenderTexture.
  113. /// </summary>
  114. private void Apply(OpticCameraManager ocm, int target)
  115. {
  116. int old = ocm.OpticFinalResolution;
  117. if (old == target)
  118. {
  119. PendingApply = false;
  120. LastTarget = target;
  121. return;
  122. }
  123. try
  124. {
  125. ocm.SetResolution(target);
  126. }
  127. catch (Exception e)
  128. {
  129. Log.Error($"设置镜内分辨率失败({old} → {target}) | Failed to set scope resolution: {e}");
  130. return;
  131. }
  132. PendingApply = false;
  133. LastTarget = target;
  134. Log.Info($"镜内分辨率 | scope resolution {old} → {target}");
  135. if (ocm.OpticFinalResolution != target)
  136. {
  137. // 第三方 prefix/postfix 可能改写了结果;锁定以避免每帧重建 RenderTexture。
  138. // A third-party prefix/postfix may have rewritten the result; lock to avoid rebuilding the RenderTexture every frame.
  139. ConflictLocked = true;
  140. _conflictTarget = target;
  141. Log.Warn($"镜内分辨率未生效(仍为 {ocm.OpticFinalResolution}),可能存在冲突模组;已暂停重试直至目标变化 | Scope resolution did not take effect (still {ocm.OpticFinalResolution}); a conflicting mod may exist. Retrying paused until target changes.");
  142. }
  143. }
  144. private static OpticCameraManager GetOpticCameraManager()
  145. {
  146. // 读取静态字段而非 Instance 属性:避免在菜单等场景中触发 CameraManager 的懒创建。
  147. // Read the static field instead of the Instance property to avoid triggering CameraManager's lazy creation in menus.
  148. CameraManager manager = CameraManager.instance;
  149. return manager == null ? null : manager.OpticCameraManager;
  150. }
  151. }
  152. }