principle.md 5.4 KB

原理与钩子设计

本文记录判定点与拦截钩子的实际实现。

总体思路

限制是会话级的:只需在内存里记录“本次 SPT 运行是否已进入过场景”,并在“开始战局”的入口查询该标记。数据流如下:

进入战局/藏身处  →  置位标记 (SessionGate.MarkEnteredScene)
试图开始战局    →  查询 (SessionGate.ShouldBlockStart)  →  阻止或放行

OneShot.Core.SessionGate 为纯逻辑状态机,不依赖游戏程序集,因此可在公共 CI 上直接单测。

判定点:进入

  • 战斗战局:Patches/EnterRaidPatch.cs 挂钩 EFT.GameWorld.OnGameStarted()([HarmonyPostfix])。该方法在离线/在线战局加载完成时触发一次。
  • 藏身处:Patches/EnterHideoutPatch.cs 挂钩 EFT.Hideout.HideoutController.HideoutAwake()。藏身处不经过 GameWorld.OnGameStarted,故必须单独挂钩 HideoutController(注意不要与嵌套类 EFT.TarkovApplication.HideoutController 混淆)。
  • 两者命中后均调用 SessionGate.MarkEnteredScene();置位幂等:重复进入只保持标记为真。
  • 各补丁用 [HarmonyPrepare] + AccessTools.Method 做目标方法存在性检查,缺失时安全跳过、保持游戏原行为。

拦截点:开始战局

  • 流程拦截:Patches/BlockStartPatch.cs 以 [HarmonyPrefix] 挂钩 EFT.MainMenuShowOperation.ShowScreen(EFT.UI.EMenuType screen, bool turnOn)。当 screen 为 Play(0) 或 Reconnect(12) 且 ShouldBlockStart(PluginConfig.Enabled.Value) 为真时 return false,静默跳过整段界面切换;Hideout(11) 等其它界面一律放行,不弹窗。
  • 视图反馈:Patches/PlayButtonStatePatch.cs 以 [HarmonyPostfix] 挂钩 EFT.UI.MenuScreen.Show(Profile, MatchmakerPlayersController, ESessionMode) 与 MenuScreen.OnMatchingTypeUpdate,在它们之后调用原生 _playButton.SetDisabledTooltip(...) 并置 Interactable = false:原生实现会把按钮 alpha 降到 0.3 且禁用点击,悬停显示提示。非限制态不干预按钮,交由游戏原生逻辑(如组队匹配禁用态)决定。
  • 文案取自 OneShotText.Get(...),按 LocalizationManager.Instance.Culture 经 LocaleSelector.Resolve 选择中/英文。

复位

  • 手动:配置中的“重置已进局标记”触发 SessionGate.Reset()。
  • 自动:SPT 进程结束,内存标记随之消失,无需持久化。

界面说明

  • 配置界面由 BepInEx ConfigurationManager 提供(SPT 默认 F12)。
  • ResetRequested 为一次性操作项:勾选后由 PluginConfig 复位为 false 并触发事件,避免界面停留在“已勾选”。

===============================================================

Design: Detection and Hook Points

This document records the detection points and interception hooks as implemented.

Approach

The restriction is session-scoped: keep an in-memory flag for "has a scene been entered this SPT run" and consult it at the raid-start entry. Data flow:

enter raid/hideout  →  set flag (SessionGate.MarkEnteredScene)
try to start raid   →  query (SessionGate.ShouldBlockStart)  →  block or allow

OneShot.Core.SessionGate is a pure-logic state machine with no game-assembly dependency, so it can be unit-tested on public CI.

Detection: entering

  • Combat raid: Patches/EnterRaidPatch.cs hooks EFT.GameWorld.OnGameStarted() ([HarmonyPostfix]), which fires once when an offline/online raid finishes loading.
  • Hideout: Patches/EnterHideoutPatch.cs hooks EFT.Hideout.HideoutController.HideoutAwake(). The hideout does not go through GameWorld.OnGameStarted, so it must be hooked separately on HideoutController (do not confuse it with the nested EFT.TarkovApplication.HideoutController).
  • Both call SessionGate.MarkEnteredScene() on hit; setting is idempotent.
  • Each patch guards with [HarmonyPrepare] + AccessTools.Method and skips safely (keeping vanilla behavior) when the target method is missing.

Interception: starting a raid

  • Flow interception: Patches/BlockStartPatch.cs hooks EFT.MainMenuShowOperation.ShowScreen(EFT.UI.EMenuType screen, bool turnOn) via [HarmonyPrefix]. When screen is Play (0) or Reconnect (12) and ShouldBlockStart(PluginConfig.Enabled.Value) is true, it returns false to silently skip the whole screen-switch flow; Hideout (11) and other screens pass through with no popup.
  • View feedback: Patches/PlayButtonStatePatch.cs hooks EFT.UI.MenuScreen.Show(Profile, MatchmakerPlayersController, ESessionMode) and MenuScreen.OnMatchingTypeUpdate via [HarmonyPostfix], then calls native _playButton.SetDisabledTooltip(...) and sets Interactable = false: the native implementation dims the button to alpha 0.3, disables clicks, and shows the tooltip on hover. When not blocked it leaves the button untouched so native logic (e.g. group-match disabling) decides.
  • Text comes from OneShotText.Get(...), selected by LocalizationManager.Instance.Culture via LocaleSelector.Resolve.

Reset

  • Manual: the "Reset Entered Flag" config fires SessionGate.Reset().
  • Automatic: the in-memory flag disappears when the SPT process ends; no persistence.

UI notes

  • The config UI is provided by BepInEx ConfigurationManager (F12 by default in SPT).
  • ResetRequested is a one-shot action item: on check, PluginConfig resets it to false and raises the event so the UI does not stay on.