Files
WT-Offline/mod-dev-docs/zh-CN/04-client-gui-and-exe.md
T

126 lines
5.3 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. 客户端界面扩展的两种组合做法(推荐最佳实践)
如果你要给游戏加一个新界面(例如自带的 `official-map-selector` 地图池弹窗),最佳实践是**“大块逻辑独立新建 + 极简增量补丁挂入口”**:
1. **新增独立的 `.nut` / `.tpl` / `.css` 文件**:直接放进 `client/packs/gui.vromfs.bin/` 下(例如 `scripts/missions/wtoMapSelectorModal.nut`、`gui/missions/wtoMapSelectorModal.tpl`、`gui/missions/wtoMapSelector.css`)。独立文件零冲突、极易维护;
2. **用最小 GUI 增量补丁挂载入口按钮**:把修改原版脚本的几行放进 `client/gui-patches/<补丁名>/`。
> 注意:`gui.vromfs.bin` 内部路径为**全小写**。虽然 Quirrel 的 `import` 路径大小写不敏感,但放进 `client/packs/gui.vromfs.bin/` 的文件名建议保持小写或与原版一致。
---
## 2. `client/gui-patches/` 补丁格式
每个补丁一个子目录,内含 `patch.jsonc` 和引用的原文/替换片段文件:
```text
client/gui-patches/
└─ my-button-patch/
├─ patch.jsonc
├─ find.nut
└─ replace.nut
```
`patch.jsonc` 示例:
```jsonc
// Adds a custom entry button to the mode selection panel.
// Needed because the stock UI has no hook for custom modal windows.
// Without this patch, the custom map selector button is not shown.
{
"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.nut",
"replace": "replace.nut"
}
]
}
]
}
```
### 规则与注意事项
- **执行顺序**:离线版核心 GUI 补丁永远先应用,各模组的 `gui-patches` 在其后按启动器排序依次应用;
- **唯一命中保护**:每条 `edit` 的 `find` 文本必须在目标脚本中**恰好出现 1 次**,否则该补丁整条跳过并写入日志,绝不半应用写坏脚本;
- 替换文本里可以使用占位符 `{{LOCAL_ENDPOINT}}`,构建器会自动替换为当前本地服务器地址(`127.0.0.1:<端口>`);
- **多版本适配**:当游戏版本更新导致原文变化时,只需在 `variants` 数组里追加一个新的写法条目,构建器会自动选取恰好命中的那一条。
### 必做检查:用 Quirrel `compilestring` 做完整字节码检查
战雷使用的 Quirrel 脚本在登录阶段预加载。如果 `.nut` 存在**词法作用域冲突**(例如局部变量和顶部 `from ... import *` 常量重名)、**前向引用未声明函数**、或误用 **Quirrel 保留字(如 `base`)**,会导致登录一直卡在「正在加载」。
只做 AST 语法检查会漏掉这些错误,**必须**用项目匹配的 Quirrel 编译器(`sq.exe`)通过 `compilestring` 跑完整字节码编译:
```powershell
# 示例:用 sq.exe 验证单个 .nut 文件能否通过完整字节码编译
& "build\quirrel-cli\bin\Release\sq.exe" -e "local f = compilestring(readfile('your_script.nut'), 'your_script.nut'); println('OK')"
```
---
## 3. 声明式 `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": []
}
}
]
}
```
### 定位方式(`locate.kind`)
- `signature`:字节特征码扫描(支持 `??` 通配与 `captures` 捕获偏移/相对跳转目标);
- `string_ref`:按代码引用的固定 ASCII 字符串定位,可附加局部指令特征校验;
- `within`:在依赖补丁或锚点所在的函数范围(按 PE 异常表边界)内搜索局部特征。
### 安全红线与核心补丁锁(不可绕过)
1. **核心离线与隔离补丁永远最先规划并锁定字节区间**(包括不连 Gaijin、不加载反作弊、目录与注册表隔离、禁止拉起官方启动器、本地 TLS/匹配公钥等);
2. **任何模组 `exe` 补丁如果与核心补丁字节重叠,会被构建器硬拒绝**并在日志中报错 `overlaps protected core patch`;
3. 如果两个模组的 `exe` 补丁写入同一段字节,后序补丁会被拒绝并标注冲突模组 ID;
4. 如果当前游戏版本找不到特征码或原字节不匹配,该补丁会被跳过并写入日志,不会导致核心构建崩溃。