Files
WT-Offline/dev-docs/contributing.md
T
N0VAN0M4LY e79866b96e docs(dev-docs): rewrite bilingual developer handbook with real code and byte walkthroughs
Expand dev-docs with a plain-language glossary, accurate single-port server flow, real apps/server directory map and cln_set_researchable trace, un-elided 57-byte EXE patch anatomy, 6-file Native C wiring guide, real Good vs. Bad code comparisons, AI constraint prompt, and traffic-log debugging steps at 1:1 zh-CN/en parity.
2026-12-03 11:25:53 +08:00

292 lines
22 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 环境搭建、流量调试与提交指南 / Contributing Guide
<a id="zh-cn"></a>
简体中文 · [English](#english) · [返回 Dev Docs](README.md#zh-cn)
这篇指南带你从零搭好本地开发环境、用内置的自动解码流量日志快速定位机库与对局问题,并在发起 Pull Request 前跑通对应的真实工程检查。
---
## 1. 开发环境准备
你需要一台 **Windows x64** 电脑,并安装以下基础工具:
1. **Node.js 24+**:仓库内的 CLI 脚本与 `apps/server` 直接以 Node 24 原生类型剥离模式运行 `.ts` 源码(Node 20/22 旧版本会因 `.ts` 扩展名或模块解析报错)。
2. **Visual Studio 2022 或 Build Tools(勾选「使用 C++ 的桌面开发」x64 工具链与 CMake)**:用于通过 `cl.exe` 编译 `native/wto-net/`(生成 `wto_ws.dll`),以及一次性编译 Quirrel 字节码检查器 `sq.exe`。
3. **Python 3.12+**:用于运行补丁校验器 `wtotools`、静态逆向工具 `tools/re/cli.py` 与客户端 `.clog` 日志解密命令。
4. **一份玩家本人合法持有的官方原版客户端(严格保持只读)**,以及在 Steam 游戏库目录之外单独准备的**离线副本目录**与**本地服务器数据目录**。
首次克隆仓库后,在仓库根目录依次执行:
```powershell
# 1. 安装工作区依赖
npm install
# 2. 编译进程内网络重定向与 LocalMP 桥接库 -> build\native\wto_ws.dll
powershell -NoProfile -File .\native\wto-net\build.ps1
# 3. 下载桌面启动器所需的 Electron 二进制运行时
node node_modules/electron/install.js
# 4. 初始化 Python 虚拟环境并安装本地补丁/逆向工具包 wtotools
python -m venv .venv
.\.venv\Scripts\python.exe -m pip install -e ./tools
```
如果你想脱离桌面启动器,直接从命令行快速构建一份离线副本并以前台模式启动本地服务器查看实时输出(注意:**`--game` 在构建时传只读原版目录、启动服务器时传离线副本目录,两条命令的 `--data` 与 `--port` 必须完全一致**):
```powershell
node scripts/build-game-copy.ts --game "<原版目录>" --out "<离线副本目录>" --data "<独立数据目录>" --port 27015
node apps/server/src/cli.ts --game "<离线副本目录>" --data "<独立数据目录>" --port 27015
```
---
<a id="debugging-zh"></a>
## 2. 怎么调试与查日志(排查 Bug 最快的路径)
排查机库动作报错、购买失败、匹配卡住或对局计分异常时,**千万不要靠盲猜客户端发了什么包**。WTOffline 在四处关键节点都留有可直接阅读的诊断日志:
### 2.1 本地服务器流量日志:`<数据目录>/logs/<启动时间戳>/`(第一站!)
每次启动 `apps/server`,`TrafficLog`(`apps/server/src/traffic-log/traffic-log.ts`)都会在 `<数据目录>/logs/` 下新建一个带 ISO 时间戳的会话文件夹,里面包含两部分:
1. **`requests.jsonl`(全量请求索引流)**
每行是一条 JSON 记录,按全局递增序号 `id` 记录每一次 HTTP 交互(`kind: "http"`)、每一条匹配服双向消息(`kind: "matching"`)、被拒绝的 WebSocket 升级请求(`kind: "websocket"`)以及连接开关事件:
- **一秒定位未实现接口**:直接在 `requests.jsonl` 中搜索 `"unhandled":true`(或看终端输出末尾带 `[UNHANDLED]` 标记的行),就能立刻抓出客户端刚发了哪个尚未挂载处理器的 `char` 动作、`yuplay2` 路径或 `matching` 消息!
- 每条记录中的 `files` 字段直接指明该次请求与响应对应的负载文件名。
2. **`bodies/` 子目录(自动解码的可读明文负载!)**
每个序号 `id`(补零至 6 位,如 `000042`)按需生成以下文件:
- `000042.req.bin` / `000042.resp.bin`:原始线上传输字节(登录表单中的密码字段会自动脱敏);
- **`000042.req.txt` / `000042.resp.txt`**:**自动解包并格式化好的可读明文 BLK 文本**(无论是带 `0x69` / zlib / zstd 标记的 `char` 二进制 BLK,还是 `cln_bulk_set_gblk` 上传的 `PBBD` 多段批处理流,都会自动转成缩进清晰的文本 BLK!)或 UTF-8 JSON 文本——你无需手写任何解包脚本,直接用编辑器打开 `.txt` 就能看清每一个字段与类型;
- **`000042.matching-in.json` / `000042.matching-out.json`**:解密并解压后的匹配长连接双向 RPC 消息(`in` 为客户端发来,`out` 为本地服务器下发)。
### 2.2 其余三类配套诊断日志
- **进程内钩子与 `LocalMP` 日志:`<离线副本目录>/win64/wto_ws.log`**
由 `wto_ws.dll` 每次启动游戏时重写。记录本地服务器端口号、每一次域名拦截与 Socket 连接重定向结果、`wto_ws.cfg` 与当前 `aces.exe` 的 PE 头校验结果,以及 **11 个 `local_mp_*` 模块中哪些成功安装、哪些因缺少 `wto_ws.cfg` 键值被优雅跳过**(并打印缺失的具体键名)。排查局内不刷 AI、选车无响应、记分牌为 0 或连杀飞机异常时先看这里。
- **游戏引擎客户端日志:`<离线副本目录>/.game_logs/*.clog`**
Dagor 引擎原生日志,记录 Quirrel 脚本编译/运行期报错、进机库卡死在「正在加载…」的具体栈信息、任务脚本报错与资源缺失。由于 `.clog` 是二进制异或加密格式,直接用 `tools/re/cli.py` 解密成纯文本再查阅:
```powershell
.\.venv\Scripts\python.exe tools\re\cli.py decrypt-clog "<离线副本目录>\.game_logs\<最新文件>.clog" --out "<临时目录>\game.clog.txt"
```
- **桌面启动器日志:`launcher.log`**
在启动器界面打开「设置 → 诊断 → 打开日志目录」即可查看,记录副本构建、补丁包校验、子进程启停与更新器状态。
---
<a id="checks"></a>
## 3. 提交前必跑的工程检查命令(按改动范围对照执行)
WTOffline **不要求也不提倡编写庞大的模拟(Mock)单元测试套件**,而是要求你按实际触碰的代码目录,跑通下面对应的**真实编译器、补丁字节核对与 Quirrel 字节码检查**;最终交互体验则在独立隔离副本中进游戏实机验收。
| 你修改了哪里 | 提交前必须跑通的检查 |
| --- | --- |
| 共享库 `packages/*`、本地服务器 `apps/server` 或 `scripts/*` | 根目录 `npm run typecheck`;若动了服务端启动逻辑或 `tuning/*.jsonc`,用临时数据目录启动一次 `apps/server/src/cli.ts` 确认数据集提取与配置校验零报错 |
| 桌面启动器 `apps/launcher` | 启动器 `typecheck` + `electron-vite build`;若增改了界面多语言词条,确认 `en.json` 与 `zh-CN.json` 键名完全对齐 |
| `patches/exe/`(任何 `patch.jsonc`) | `wtotools check-patches`(验证特征码唯一命中、`expect_original` 匹配,且 `asm`/`data` 编译结果与 `bytes` 逐字节一致) |
| `patches/gui/`(任何 `patch.jsonc` 或 `.nut` 片段) | **完整** `wtotools check-gui`(调用 `sq.exe` 执行 `compilestring` 词法作用域与字节码编译检查) + 临时副本构建确认 `gui-patch-definition.ts` Schema 无未知键报错 |
| `native/wto-net/` 或 `patches/native/` | MSVC `native\wto-net\build.ps1`(`/W4` **零警告**)+ 临时目录 `scripts/build-game-copy.ts` 确认所有原生站点全部解析就绪 |
| 适配客户端新版本(`patches/versions/`) | `scripts/record-game-version.ts` + 上述全部适用检查 + **在隔离副本中实机登录进机库并完成 `LocalMP` 对局验收后**,方可提交 `patches/versions/<版本号>.jsonc` |
常用检查命令汇总(均在仓库根目录执行,将尖括号替换为你的本地路径):
```powershell
# 1. TypeScript 类型与语法剥离约束检查(校验 .ts 后缀、import type、erasableSyntaxOnly 等)
npm run typecheck
npm run typecheck --workspace "@wto/launcher"
npm run build --workspace "@wto/launcher"
# 2. EXE 与 GUI 补丁核对(输入源必须是只读原版客户端目录!)
.\.venv\Scripts\python.exe -m wtotools.cli check-patches --exe "<原版目录>\win64\aces.exe"
.\.venv\Scripts\python.exe -m wtotools.cli check-gui --gui "<原版目录>\gui.vromfs.bin" --sq "build\quirrel-cli\bin\Release\sq.exe"
# 3. Native DLL 编译(MSVC /W4 零警告)与完整离线副本试构建
powershell -NoProfile -File .\native\wto-net\build.ps1
node scripts/build-game-copy.ts --game "<原版目录>" --out "<临时副本目录>" --data "<临时数据目录>" --port 27015
# 4. 客户端版本适配记录核对
node scripts/record-game-version.ts --game "<原版目录>"
```
<a id="quirrel"></a>
### 一次性编译 Quirrel 字节码检查器 `sq.exe`(注意 `--sq` 路径参数!)
`check-gui` 需要调用与《战争雷霆》客户端同代的官方 Quirrel 编译器(固定在 [Quirrel 仓库提交 `19f17df9b6ee43c91ef5c635fd45970ec76c5dbe`](https://github.com/GaijinEntertainment/quirrel/tree/19f17df9b6ee43c91ef5c635fd45970ec76c5dbe))。使用 Visual Studio 2022 C++ 工具链与 CMake 编译一次即可长期复用:
```powershell
git clone https://github.com/GaijinEntertainment/quirrel.git build/quirrel-source
git -C build/quirrel-source checkout --detach 19f17df9b6ee43c91ef5c635fd45970ec76c5dbe
cmake -S build/quirrel-source -B build/quirrel-cli -G "Visual Studio 17 2022" -A x64
cmake --build build/quirrel-cli --config Release --target sq
```
> **关于 `--sq` 参数的重要说明**:`wtotools check-gui` 在不传 `--sq` 时的默认查找路径是 `build/quirrel-check/sq.exe`(见 `tools/src/wtotools/cli/gui_commands.py`),而上面这条 Visual Studio CMake 多配置构建命令会把可执行文件生成在 **`build\quirrel-cli\bin\Release\sq.exe`**。因此运行 `check-gui` 时请像上方命令一样显式传入 `--sq "build\quirrel-cli\bin\Release\sq.exe"`(或者手动把生成的 `sq.exe` 复制一份到 `build\quirrel-check\sq.exe`)。
---
## 4. 提交消息规范与 PR 自查清单
- **一笔 PR 只聚焦一个清晰目标**:不要在修某个结算 Bug 的 PR 里顺手重排无关目录或大面积格式化其他文件。
- **全英文约定式提交(Conventional Commits)与英文注释**:
- 提交标题格式:`type(scope): concise summary`(例如 `fix(battle): credit Ground Arcade air event kills from AI victims`、`feat(launcher): show missing optional patch features`;仓库级工程或文档改动用 `repo` 或 `docs`);
- 若包含破坏性变更,在 `scope` 后加 `!` 并在正文末尾附上 `BREAKING CHANGE:` 说明;
- 代码注释、补丁顶部三行说明、补丁 `title` 与 Git 提交消息正文统一使用英文;
- `Co-authored-by` 尾注仅用于署名实际参与协作的人类开发者,不要填写 AI 模型或编程工具。
- **发起 PR 前最后 5 项自检**:
1. 对照 [开发规范第 4 节](development-rules.md#good-vs-bad-zh):零硬编码 RVA/结构体偏移/`_ReturnAddress()`、零手抄游戏静态数值、零越层 I/O;
2. 核对语法与 Schema 细节:TS 相对导入全部带 `.ts` 后缀且遵守 `erasableSyntaxOnly`;补丁 JSONC 无多余未知键(特别是 `patches/gui/**/patch.jsonc` 未误写 `requires`);带尾随立即数的 `rel32` 捕获已显式填写 `"end"`;
3. 涉及货币、研发、库存或存档改动时,确认所有修改均通过 `PlayerProfiles` / `SaveStore` **先完成原子写盘再向客户端返回响应**,且多账号目录严格隔离;
4. 通读一遍 `git diff`,确认没有带入任何本机个人路径、原版游戏文件、解包产物、测试存档或未脱敏日志;
5. 完整填写 [PR 模板](templates/pull-request.md),如实列出实际运行过的命令与输出结论,并把「已在隔离副本实机验收的界面步骤」与「尚待维护者实机验收的步骤」明确区分开。
---
<a id="english"></a>
[简体中文](#zh-cn) · English · [Back to Dev Docs](README.md#english)
This guide walks you through setting up a Windows x64 development environment, diagnosing bugs rapidly with auto-decoded local traffic logs, and running the required verification checks before opening a Pull Request.
---
## 1. Environment Setup
You need a **Windows x64** machine with:
1. **Node.js 24+**: Repository CLI scripts and `apps/server` execute `.ts` files directly via Node 24's native type-stripping (older Node 20/22 releases will fail on `.ts` import specifiers).
2. **Visual Studio 2022 or Build Tools (with "Desktop development with C++" x64 tools & CMake)**: Used by `native/wto-net/build.ps1` (`cl.exe`) to build `wto_ws.dll` and once to compile the Quirrel bytecode checker `sq.exe`.
3. **Python 3.12+**: Used for the patch verifier (`wtotools`), static disassembler (`tools/re/cli.py`), and `.clog` log decryptor.
4. **Your own legitimate stock War Thunder installation (kept strictly read-only)**, plus a separate **offline copy folder** and **server data folder** located outside any Steam library directory.
After cloning the repository, bootstrap everything from the repo root:
```powershell
# 1. Install npm workspace dependencies
npm install
# 2. Compile the Winsock redirector & LocalMP host bridge -> build\native\wto_ws.dll
powershell -NoProfile -File .\native\wto-net\build.ps1
# 3. Download the Electron runtime binary for the desktop launcher
node node_modules/electron/install.js
# 4. Create the Python virtual environment and install `wtotools` in editable mode
python -m venv .venv
.\.venv\Scripts\python.exe -m pip install -e ./tools
```
To build an offline game copy and run the local server directly from the terminal without the desktop launcher (note: pass the **read-only stock folder** to `build-game-copy.ts --game`, pass the **offline copy folder** to `cli.ts --game`, and keep `--data` and `--port` identical across both commands):
```powershell
node scripts/build-game-copy.ts --game "<stock-dir>" --out "<copy-dir>" --data "<data-dir>" --port 27015
node apps/server/src/cli.ts --game "<copy-dir>" --data "<data-dir>" --port 27015
```
---
<a id="debugging-en"></a>
## 2. Traffic Log Debugging & Diagnostic Logs
Never guess what payload the game client sent or why a hangar action failed—WTOffline records human-readable diagnostics across four log locations:
### 2.1 Local Server Traffic Logs: `<data-dir>/logs/<ISO-timestamp>/` (Check Here First!)
Every time `apps/server` starts, `TrafficLog` (`apps/server/src/traffic-log/traffic-log.ts`) creates a timestamped session folder under `<data-dir>/logs/` containing:
1. **`requests.jsonl` (Indexed Exchange Log)**
One JSON line per HTTP request/response (`kind: "http"`), matching message (`kind: "matching"`), refused WebSocket upgrade (`kind: "websocket"`), or TCP connection open/close, numbered sequentially by `id`:
- **Spot missing endpoints instantly**: Search `requests.jsonl` for `"unhandled":true` (or look for `[UNHANDLED]` in the server console) to immediately see any unimplemented `char` action, `yuplay2` endpoint, or `matching` RPC!
- Each entry's `files` object points directly to its corresponding payload files inside `bodies/`.
2. **`bodies/` Subdirectory (Auto-Decoded Readable BLK & JSON Payloads!)**
Numbered by zero-padded sequence ID (e.g., `000042.*`):
- `000042.req.bin` / `000042.resp.bin`: Raw wire bytes (with sign-in passwords automatically redacted);
- **`000042.req.txt` / `000042.resp.txt`**: **Automatically unpacked and formatted readable BLK text** (covering `0x69`/zlib/zstd-framed `char` binary BLK payloads as well as multi-chunk `PBBD` streams on `cln_bulk_set_gblk`!) or UTF-8 JSON/form text—open any `.txt` file directly in your editor to inspect exact block trees and parameter types without manual decoding;
- **`000042.matching-in.json` / `000042.matching-out.json`**: Decrypted, LZ4-decompressed inbound (`in`) and outbound (`out`) `matching` binary RPC messages.
### 2.2 The Other Three Diagnostic Logs
- **Native Hook & `LocalMP` Log: `<copy-dir>/win64/wto_ws.log`**
Rewritten on every client launch by `wto_ws.dll`. Logs the active local port, every locally answered hostname and redirected/refused socket, whether `wto_ws.cfg` matched the running `aces.exe` PE `TimeDateStamp` / `SizeOfImage`, and **which of the 11 `local_mp_*` modules installed vs. were skipped** (naming the exact missing `wto_ws.cfg` key when skipped), plus live battle referee events.
- **Dagor Client Log: `<copy-dir>/.game_logs/*.clog`**
Records Quirrel script compilation/runtime errors, exact call stacks behind `"Loading..."` login hangs, mission errors, and missing assets. Because `.clog` files are XOR-encrypted binary logs, decrypt them to plain text with `tools/re/cli.py`:
```powershell
.\.venv\Scripts\python.exe tools\re\cli.py decrypt-clog "<copy-dir>\.game_logs\<latest>.clog" --out "<temp-dir>\game.clog.txt"
```
- **Desktop Launcher Log: `launcher.log`**
Accessible from Launcher **Settings -> Diagnostics -> Open log folder**, covering copy builds, patch-pack checks, server process lifecycle, and updater activity.
---
<a id="checks-en"></a>
## 3. Required Checks Before Submitting
WTOffline **does not require or encourage large mock unit test suites**. Instead, run the real compiler, binary-patch, and Quirrel bytecode verifiers matching the files you touched, followed by live acceptance in an isolated game copy:
| What you changed | Required checks before opening a PR |
| --- | --- |
| `packages/*`, `apps/server`, or `scripts/*` | Root `npm run typecheck`; if you touched server startup or `tuning/*.jsonc`, boot `apps/server/src/cli.ts` against a temporary data folder to confirm clean dataset extraction and tuning validation |
| `apps/launcher` | Launcher `typecheck` + `electron-vite build`; if you edited UI strings, verify `en.json` and `zh-CN.json` locale key parity |
| `patches/exe/` (any `patch.jsonc`) | `wtotools check-patches` (verifies unique signature hits, `expect_original` bytes, and exact byte-for-byte agreement between `asm`/`data` and `bytes`) |
| `patches/gui/` (any `patch.jsonc` or `.nut` fragment) | **Full** `wtotools check-gui` using `sq.exe` `compilestring` (required) + a temporary copy build to confirm zero unknown schema keys in `gui-patch-definition.ts` |
| `native/wto-net/` or `patches/native/` | MSVC `native\wto-net\build.ps1` (`/W4`, **zero warnings**) + temporary `scripts/build-game-copy.ts` run confirming every native module resolves cleanly |
| Adapting a new client build (`patches/versions/`) | `scripts/record-game-version.ts` + all applicable checks above + **live in-game verification in an isolated copy (hangar login + `LocalMP` battle)** before committing `patches/versions/<version>.jsonc` |
Complete command reference (run from the repository root, substituting bracketed paths):
```powershell
# 1. TypeScript typecheck & Node 24 erasable-syntax checks (.ts extensions, import type, etc.)
npm run typecheck
npm run typecheck --workspace "@wto/launcher"
npm run build --workspace "@wto/launcher"
# 2. EXE & GUI patch verification (always point at your READ-ONLY stock folder!)
.\.venv\Scripts\python.exe -m wtotools.cli check-patches --exe "<stock-dir>\win64\aces.exe"
.\.venv\Scripts\python.exe -m wtotools.cli check-gui --gui "<stock-dir>\gui.vromfs.bin" --sq "build\quirrel-cli\bin\Release\sq.exe"
# 3. Native DLL build (MSVC /W4 zero warnings) & temporary offline copy build
powershell -NoProfile -File .\native\wto-net\build.ps1
node scripts/build-game-copy.ts --game "<stock-dir>" --out "<temp-copy-dir>" --data "<temp-data-dir>" --port 27015
# 4. Client build compatibility record
node scripts/record-game-version.ts --game "<stock-dir>"
```
<a id="quirrel-en"></a>
### One-Time Build of the Quirrel Bytecode Compiler `sq.exe` (Note the `--sq` Flag!)
`check-gui` requires the official Quirrel compiler pinned to the revision matching War Thunder's client VM ([Quirrel commit `19f17df9b6ee43c91ef5c635fd45970ec76c5dbe`](https://github.com/GaijinEntertainment/quirrel/tree/19f17df9b6ee43c91ef5c635fd45970ec76c5dbe)). Build it once with Visual Studio 2022 C++ tools and CMake:
```powershell
git clone https://github.com/GaijinEntertainment/quirrel.git build/quirrel-source
git -C build/quirrel-source checkout --detach 19f17df9b6ee43c91ef5c635fd45970ec76c5dbe
cmake -S build/quirrel-source -B build/quirrel-cli -G "Visual Studio 17 2022" -A x64
cmake --build build/quirrel-cli --config Release --target sq
```
> **Important note on `--sq`**: `wtotools check-gui` defaults `--sq` to `build/quirrel-check/sq.exe` (`tools/src/wtotools/cli/gui_commands.py`), whereas the multi-config Visual Studio CMake build above outputs **`build\quirrel-cli\bin\Release\sq.exe`**. Always pass `--sq "build\quirrel-cli\bin\Release\sq.exe"` explicitly as shown in the command block above (or copy `sq.exe` to `build\quirrel-check\sq.exe`).
---
## 4. Commit Conventions & Pre-PR Checklist
- **One focused goal per PR**: Avoid mixing unrelated refactors or repo-wide formatting into a bugfix or feature PR.
- **English Conventional Commits & English code comments**:
- Format commit titles as `type(scope): concise summary` (e.g., `fix(battle): credit Ground Arcade air event kills from AI victims`, `feat(launcher): show missing optional patch features`; use `repo` or `docs` for repository-level or documentation changes);
- Mark breaking changes with `!` after the scope and a `BREAKING CHANGE:` footer;
- Write all code comments, 3-line patch header comments, patch `title` fields, and commit bodies in English;
- Reserve `Co-authored-by` trailers strictly for human collaborators—never list AI models or coding tools.
- **Final 5-point self-check before opening a PR**:
1. Check against [Development Rules — Section 4](development-rules.md#good-vs-bad-en): zero hardcoded RVAs, struct offsets, or `_ReturnAddress()` checks; zero hardcoded stock game numbers; zero cross-layer I/O;
2. Verify syntax & schema discipline: explicit `.ts` extensions on relative imports, `import type` for types, no `enum`/`namespace`/parameter properties; strict patch JSONC keys (no `requires` in `patches/gui/**/patch.jsonc`, explicit `"end"` on `rel32` captures with trailing immediates);
3. For currency, research, inventory, or profile mutations, verify changes persist atomically via `PlayerProfiles` / `SaveStore` **before** replying to the client, with strict per-account isolation;
4. Review `git diff` to ensure zero personal machine paths, copyrighted game files, extracted stock scripts, personal saves, or unredacted logs slipped in;
5. Fill out the [PR template](templates/pull-request.md) completely, recording exact commands run and clearly separating observed in-game checks from pending in-game acceptance steps.