Files
WT-Offline/mod-dev-docs/zh-CN/04-client-gui-and-exe.md
T
N0VAN0M4LY a2fdcc1f15 docs(mods): fix schema errors and expand bilingual mod handbook to full parity
Fix store.jsonc vehiclePacks nesting and gjn field name, matchingApiFunc callback argument order, and DaGui CSS @import/lowercase rules; add stock archive inspection steps, BR-to-mrank table, cloned vehicle walkthrough, tuning field reference, and official-map-selector full-stack trace while bringing en/01..07.md to full 1:1 parity with zh-CN/.
2026-12-03 11:25:54 +08:00

217 lines
11 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.
# 04 · 客户端 GUI 补丁与声明式 EXE 补丁
> **实验期提示**:界面脚本补丁(`gui-patches`)与声明式 `exe` 补丁(`exe-patches`)直接依赖目标游戏客户端的代码结构,游戏版本更新或离线核心补丁演进时可能需要同步维护。
---
## 1. 先懂概念:战雷原生 DaGui 界面三件套(`.tpl` + `.css` + `.nut`)
《战争雷霆》机库与大厅界面由 Dagor 自研的 **DaGui** 框架驱动(全部存放在 `gui.vromfs.bin` 包内)。一个完整的界面弹窗通常由三类文本文件配合完成:
| 文件后缀 | 角色定位 | 通俗解释 |
| --- | --- | --- |
| **`.tpl`** | **Handyman 布局模板** | 类似 HTML + Mustache 模板(如 `gui/missions/wtomapselectormodal.tpl`),声明窗口骨架、按钮节点、`id` 与数据占位符(如 `<<wndTitle>>`、`{{#cards}}...{{/cards}}`)。 |
| **`.css`** | **DaGui 样式表** | 定义节点宽高、边距、背景色、字体大小以及自定义状态属性选择器(如 `[wtoSelected=yes]`)。 |
| **`.nut`** | **Quirrel 界面逻辑脚本** | 战雷使用的类 JS/Lua 强作用域脚本语言(Squirrel 衍生版),继承 `BaseGuiHandlerWT` 处理按钮点击、渲染 `.tpl` 并与本地服务器通信。 |
### 客户端界面扩展黄金准则:「大块逻辑独立新建 + 极简增量补丁挂入口」
像官方模板 `templates/official-map-selector` 一样,给游戏做新功能界面的最佳做法是:
1. **把你自己的弹窗 `.nut`、`.tpl`、`.css` 作为新文件放进 `client/packs/gui.vromfs.bin/`**:独立文件与其他模组零冲突、极易维护;
2. **只用几行 `client/gui-patches/` 增量补丁修改原版文件**:挂一个入口按钮,并在全局样式表入口 `gui/css.css` 里注册你的新 `.css`!
> **必看高频踩坑:新增的独立 `.css` 不会被 DaGui 自动加载!**
> 仅仅把 `gui/missions/wtomapselector.css` 放进 `client/packs/gui.vromfs.bin/` 是**不会生效**的!DaGui 只从根入口 `gui/css.css` 加载样式表,因此你**必须同时在 `client/gui-patches/` 里给 `gui/css.css` 打一条补丁**,追加一行 `@import "missions\wtomapselector.css"`(注意:DaGui 的 `gui/css.css` 路径相对于 `gui/` 目录,使用反斜杠 `\` 且行尾**不写分号**,详见下方 2.2 节真实示例)。
---
## 2. `client/gui-patches/` 增量补丁格式与匹配规则
`client/gui-patches/` 不仅能改 `.nut` 脚本,**对 `gui.vromfs.bin` 里的 `.nut`、`.tpl` 和 `.css` 文本文件完全通用**!每个补丁放在一个含 `patch.jsonc` 的子目录(支持多层子目录组织):
```text
client/gui-patches/
└─ mode-select-map-selector-button/
├─ css/
│ ├─ patch.jsonc
│ ├─ find-css.css
│ └─ replace-css.css
├─ tpl/
│ ├─ patch.jsonc
│ ├─ find-nav-right.tpl
│ └─ replace-nav-right.tpl
└─ script/
├─ patch.jsonc
├─ find-imports.nut
├─ replace-imports.nut
├─ find-buttons.nut
└─ replace-buttons.nut
```
### 2.1 `.nut` 脚本补丁示例
```jsonc
// Adds a custom entry button to the mode selection panel.
{
"id": "my-mod/add-button",
"title": "Add custom button to mode selector",
"features": [],
"variants": [
{
"written_for": "2.61.0.58",
"file": "scripts/gamemodes/gamemodeselect.nut",
"edits": [
{
"find": "find-imports.nut",
"replace": "replace-imports.nut"
},
{
"find": "find-buttons.nut",
"replace": "replace-buttons.nut"
}
]
}
]
}
```
### 2.2 注册独立 `.css` 到 `gui/css.css` 的真实补丁示例
```jsonc
// client/gui-patches/my-style/patch.jsonc
{
"id": "my-mod/register-css",
"title": "Include custom stylesheet in gui/css.css",
"features": [],
"variants": [
{
"written_for": "2.61.0.58",
"file": "gui/css.css",
"edits": [
{
"find": "find-css.css",
"replace": "replace-css.css"
}
]
}
]
}
```
`find-css.css`:
```css
@import "missions\mapPreferences.css"
```
`replace-css.css`:
```css
@import "missions\mapPreferences.css"
@import "missions\wtomapselector.css"
```
### 2.3 匹配规则与大小写注意事项
1. **`variant.file` 必须全小写、精确区分大小写!**
原版 `gui.vromfs.bin` 归档内的所有文件路径均为**全小写**(例如 `scripts/gamemodes/gamemodeselect.nut`、`gui/gamemodeselect/gamemodeselect.tpl`、`gui/css.css`)。虽然 Quirrel 代码里的 `from "%scripts/gameModes/..."` 导入语句不区分大小写,但 `gui-patches` 构建器在包内查找 `variant.file` 时做的是**严格区分大小写的精确键匹配**——如果写成驼峰 `scripts/gameModes/gameModeSelect.nut`,会直接报错 `the data pack has no script ...`!放进 `client/packs/gui.vromfs.bin/` 的新增文件也请统一使用全小写路径。
2. **每条 `find` 必须唯一命中 1 次**:
每条 `edit` 的 `find` 原文片段在目标文件中需要**恰好出现 1 次**(自动兼容 `\r\n` 与 `\n` 换行差异);若出现 0 次或 ≥2 次,该补丁会整条跳过并记入 `launcher.log`,避免半应用写坏脚本。
3. **`variants` 与 `written_for` 的真实选择机制**:
`written_for` 只是便于阅读和报错排查的版本标注字符串,构建器**不会**按版本号盲选——它会对当前客户端文件逐一测试 `variants` 数组里的**全部候选条目**,且要求**有且仅有 1 个 `variant` 能够完整命中**(0 个命中 = 视为版本不匹配安全跳过;≥2 个同时命中 = 视为变体定义歧义直接拒绝)。
4. **本地服务器地址占位符 `{{LOCAL_ENDPOINT}}`**:
在任何 `replace` 文本中写入 `{{LOCAL_ENDPOINT}}`,构建器都会自动将其替换为当前客户端绑定的本地服务器 `127.0.0.1:<端口>`(例如拼接内置浏览器 URL `http://{{LOCAL_ENDPOINT}}/mods/demo.my-mod/panel`)。离线版核心 GUI 补丁永远最先应用,各模组的 `gui-patches` 在其后按加载顺序依次叠加。
---
## 3. 必做检查:编译 `sq.exe` 并对完整 `.nut` 做字节码校验
战雷的 Quirrel 脚本在登录阶段预编译加载。如果 `.nut` 存在以下错误,游戏会在进入机库前**一直卡死在「正在加载」界面**:
- **词法作用域重名冲突**:局部变量名(`let` / `local`)与文件顶部 `from "..." import *` 导入的常量或原生函数重名;
- **前向引用未声明函数**或误用 **Quirrel 保留字**(如 `base`);
- **误用反引号字符串 `` `...` ``**:战雷内置 Quirrel VM 不支持 JS 风格反引号模板字符串,请统一使用 Quirrel 原生插值字符串 **`$"hello {name}"`**!
注意:`sq.exe --syntax-check` 只跑语法树解析,**会漏掉作用域重名和未声明标识符错误**;必须通过 `compilestring(...)` 触发完整字节码生成检查!
### 3.1 外部模组作者如何自行获取匹配的 `sq.exe`?
克隆 Gaijin 官方开源的 **[GaijinEntertainment/quirrel](https://github.com/GaijinEntertainment/quirrel)** 仓库,检出与 2.61 客户端匹配的提交 `19f17df9b6ee43c91ef5c635fd45970ec76c5dbe`,用 CMake + Visual Studio(C++17)即可在数秒内编译出 `sq.exe`:
```powershell
git clone https://github.com/GaijinEntertainment/quirrel.git quirrel-src
git -C quirrel-src checkout 19f17df9b6ee43c91ef5c635fd45970ec76c5dbe
cmake -S quirrel-src -B quirrel-build
cmake --build quirrel-build --config Release --target sq
# 产物位于 quirrel-build\bin\Release\sq.exe
```
### 3.2 正确用法:必须对「完整 `.nut` 文件」执行 `compilestring`
> **切勿拿半截 `replace.nut` 片段直接喂给 `compilestring`!**
> `replace.nut` 只是补丁上下文中的几行代码片段,单独编译必然报语法或未定义变量错误。你必须针对**完整的 `.nut` 文件**(即你新建的独立 `.nut` 全文,或者将 `find`/`replace` 应用到原版 `.nut` 之后的完整结果文件)运行检查:
- **如果克隆了 WTOffline 源码仓库**:仓库内的 `tools/src/wtotools/gui_check.py` 已经封装好全自动导入符号解析与 `compilestring` 沙箱,可一键对解包的 `gui.vromfs.bin` 应用补丁并完成全量字节码校验;
- **单文件快速自检示例**(将文件内顶部 `import` 绑定名作为第 3 参数占位表传入 `compilestring`):
```powershell
& "quirrel-build\bin\Release\sq.exe" -e "let io = require('io'); let f = io.file('your_complete_script.nut', 'rb'); let src = f.readblob(f.len()).as_string(); f.close(); compilestring(src, 'your_complete_script.nut'); print('OK\n')"
```
---
## 4. 声明式 `exe` 补丁(`client/exe-patches/`)
高级模组可以直接复用 WTOffline 本体的 `@wto/exepatch` 引擎对 `win64/aces.exe` 做特征码定位补丁。请使用声明式补丁文件,不要直接打包或分发修改后的 `aces.exe` 可执行文件。
```text
client/exe-patches/
└─ my-tweak/
└─ patch.jsonc
```
`patch.jsonc` 结构示例:
```jsonc
// Brief description of what this binary patch changes.
// Why the local server cannot do this alone.
// What happens if this patch is skipped.
{
"id": "my-tweak",
"title": "Example declarative exe patch",
"features": [],
"requires": [],
"variants": [
{
"written_for": "2.61.0.58",
"locate": {
"kind": "signature",
"pattern": "48 8B ?? ?? ?? ?? ?? 84 C0 74 ??",
"captures": []
},
"edit": {
"offset": 9,
"asm": "nop; nop",
"bytes": "90 90",
"expect_original": "74 ??",
"fixups": []
}
}
]
}
```
### 支持的 3 种定位器(`locate.kind`)
- `signature`:字节特征码扫描(支持 `??` 通配字节与 `captures` 捕获相对偏移/跳转目标);
- `string_ref`:按代码引用的固定 ASCII 字符串定位,可附加局部指令特征校验;
- `within`:在依赖补丁或锚点所在的函数边界(基于 PE `.pdata` 异常表起止范围)内搜索局部特征。
### 核心隔离保护与冲突处理
1. **核心离线与隔离补丁永远最先规划并锁定字节区间**(涵盖断网隔离不连 Gaijin、不加载反作弊、存档目录与注册表隔离、禁止拉起官方启动器、本地 TLS/匹配公钥替换等);
2. **任何模组 `exe` 补丁只要写入范围与核心补丁重叠 1 个字节,就会被构建器硬拒绝**,并在日志中报错 `overlaps protected core patch`;
3. 若两个不同模组的 `exe` 补丁写入同一段字节,后加载的补丁会被拒绝并明确报出冲突模组 ID;
4. 若客户端更新后特征码未命中或 `expect_original` 不匹配,该模组补丁会安全跳过并记入 `launcher.log`,不会导致离线副本构建崩溃。