| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- using System;
- using EFT.CameraControl;
- using PerformanceScope.Core;
- using UnityEngine;
- namespace PerformanceScope
- {
- /// <summary>
- /// 镜内 PiP 分辨率的决策与应用。每帧由插件调用 <see cref="Tick"/>,
- /// 根据配置与当前游戏状态决定何时调用 <see cref="OpticCameraManager.SetResolution"/>。
- /// 普通类,由插件在 Awake 中创建并注册到 <see cref="Instance"/>。
- /// Decision and application of the in-scope PiP resolution. The plugin calls <see cref="Tick"/> every frame,
- /// which decides when to call <see cref="OpticCameraManager.SetResolution"/> based on config and game state.
- /// A plain class, created by the plugin in Awake and assigned to <see cref="Instance"/>.
- /// </summary>
- internal sealed class OpticResolutionService
- {
- private bool _loggedNoCamera;
- private int _pendingTarget = ResolutionMath.GameDefaultResolution;
- private int _conflictTarget = int.MinValue;
- /// <summary>
- /// 当前服务实例;插件未加载时为 null,补丁据此透传原值。
- /// Current service instance; null when the plugin is not loaded, in which case patches pass the original value through.
- /// </summary>
- public static OpticResolutionService Instance { get; set; }
- /// <summary>
- /// 最近一次解析出的目标边长,供叠加显示。
- /// Most recently resolved target edge length, used for overlay display.
- /// </summary>
- public int LastTarget { get; private set; } = ResolutionMath.GameDefaultResolution;
- /// <summary>
- /// 是否有因瞄准中而推迟应用的目标。
- /// Whether a target is currently deferred because the player is scoped.
- /// </summary>
- public bool PendingApply { get; private set; }
- /// <summary>
- /// 是否因第三方改写导致目标无法生效而暂停重试。
- /// Whether retries are paused because a third-party rewrite kept the target from taking effect.
- /// </summary>
- public bool ConflictLocked { get; private set; }
- /// <summary>
- /// 计算当前配置对应的目标边长。模组关闭时返回游戏默认值,
- /// 从而让游戏恢复其原生 1024。
- /// Computes the target edge length for the current config. Returns the game default when the mod is off,
- /// so the game falls back to its native 1024.
- /// </summary>
- public int ComputeTarget()
- {
- if (PluginConfig.EnableMod?.Value != true)
- {
- return ResolutionMath.GameDefaultResolution;
- }
- return ResolutionMath.Resolve(
- PluginConfig.Mode.Value,
- Screen.height,
- PluginConfig.ScreenHeightRatio.Value,
- PluginConfig.AbsolutePixels.Value);
- }
- /// <summary>
- /// 每帧调用:在相机就绪时把配置分辨率同步给游戏。
- /// Called every frame: syncs the configured resolution to the game once the camera is ready.
- /// </summary>
- public void Tick()
- {
- OpticCameraManager ocm = GetOpticCameraManager();
- if (ocm == null || ocm.Camera == null)
- {
- if (!_loggedNoCamera)
- {
- _loggedNoCamera = true;
- Log.Verbose("镜内相机尚未就绪,暂不应用 | Optic camera not ready, skipping.");
- }
- return;
- }
- _loggedNoCamera = false;
- int target = ComputeTarget();
- LastTarget = target;
- if (ConflictLocked)
- {
- // 仅在目标变化(例如玩家改了配置)时解除锁定,避免每帧重建。
- // Unlock only when the target changes (e.g. the player edits config) to avoid per-frame rebuilds.
- if (target == _conflictTarget)
- {
- return;
- }
- ConflictLocked = false;
- }
- OpticApplyAction action = OpticResolutionDecision.Decide(
- ocm.OpticFinalResolution,
- target,
- ocm.IsAnyOpticCameraRendering,
- PluginConfig.ApplyWhileScoped?.Value == true);
- switch (action)
- {
- case OpticApplyAction.Apply:
- Apply(ocm, target);
- break;
- case OpticApplyAction.Defer:
- if (!PendingApply || _pendingTarget != target)
- {
- _pendingTarget = target;
- PendingApply = true;
- Log.Info($"镜内瞄准中,推迟应用分辨率 | scoped, deferring resolution {ocm.OpticFinalResolution} → {target}");
- }
- break;
- default:
- PendingApply = false;
- break;
- }
- }
- /// <summary>
- /// 安全地调用游戏接口重建镜内 RenderTexture。
- /// Safely calls the game API to rebuild the scope RenderTexture.
- /// </summary>
- private void Apply(OpticCameraManager ocm, int target)
- {
- int old = ocm.OpticFinalResolution;
- if (old == target)
- {
- PendingApply = false;
- LastTarget = target;
- return;
- }
- try
- {
- ocm.SetResolution(target);
- }
- catch (Exception e)
- {
- Log.Error($"设置镜内分辨率失败({old} → {target}) | Failed to set scope resolution: {e}");
- return;
- }
- PendingApply = false;
- LastTarget = target;
- Log.Info($"镜内分辨率 | scope resolution {old} → {target}");
- if (ocm.OpticFinalResolution != target)
- {
- // 第三方 prefix/postfix 可能改写了结果;锁定以避免每帧重建 RenderTexture。
- // A third-party prefix/postfix may have rewritten the result; lock to avoid rebuilding the RenderTexture every frame.
- ConflictLocked = true;
- _conflictTarget = target;
- Log.Warn($"镜内分辨率未生效(仍为 {ocm.OpticFinalResolution}),可能存在冲突模组;已暂停重试直至目标变化 | Scope resolution did not take effect (still {ocm.OpticFinalResolution}); a conflicting mod may exist. Retrying paused until target changes.");
- }
- }
- private static OpticCameraManager GetOpticCameraManager()
- {
- // 读取静态字段而非 Instance 属性:避免在菜单等场景中触发 CameraManager 的懒创建。
- // Read the static field instead of the Instance property to avoid triggering CameraManager's lazy creation in menus.
- CameraManager manager = CameraManager.instance;
- return manager == null ? null : manager.OpticCameraManager;
- }
- }
- }
|