Files
WT-Offline/dev-docs/mods.md
T

144 lines
9.9 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.
# 模组开发指南 / Mod Authoring Guide
<a id="zh-cn"></a>
简体中文 · [English](#english) · [返回 Dev Docs](README.md#zh-cn)
> **实验期免责声明(API `0.1-experimental`)**
> WTOffline 仍处于极初期开发阶段。服务端脚本接口、内部数据结构、钩子签名与声明式 `exe` 补丁格式均可能随本体开发随时调整,**当前阶段不承诺向后兼容**。涉及服务端脚本或客户端二进制补丁的模组,请作者做好跟随本体更新维护的准备。
## 1. 设计理念与目录位置
WTOffline 采用**添加式、可叠加**模组架构,不支持整包覆盖 `.vromfs.bin`。多个模组启用时按启动器「模组」页的顺序依次合并(越靠后优先级越高),关闭模组或切换预设即可干净还原。
- **全局模组仓库**:所有客户端共享一份模组目录(默认 `<启动器用户数据>/mods`,可在启动器「模组 › 预设与仓库」改为任意硬盘位置)。
- **按客户端独立配置**:每个离线客户端单独保存自己的启用开关、加载顺序与命名预设(支持 JSON 导入/导出)。
- **原生资源边界**:独立的涂装(`UserSkins/`)、瞄具(`UserSights/`)与单人任务(`UserMissions/`)继续沿用游戏原生放置方式;启动器绝不干涉或覆盖玩家原生目录中的文件。
## 2. 标准模组目录结构
在全局 `mods/` 目录下新建一个子文件夹,根目录放置 `mod.jsonc`,其余子目录按需创建:
```text
my-mod/
├─ mod.jsonc # 必需:清单(ID、名称、版本、作者、依赖、互斥、排序)
├─ server/ # 可选:服务端扩展
│ ├─ tuning/ # 服务端数值增量覆盖(*.jsonc,支持 $listOps)
│ └─ index.ts # 服务端 JS/TS 脚本入口(支持 .ts / .mjs / .js)
└─ client/ # 可选:客户端扩展
├─ packs/ # .vromfs.bin 增量叠加(按目标包名分目录)
│ ├─ char.vromfs.bin/ # 例:config/shop.blk.patch.jsonc, config/wpcost.blk.patch.jsonc
│ ├─ aces.vromfs.bin/ # 例:gamedata/units/tankmodels/my_tank.blk(支持文本 BLK)
│ ├─ lang.vromfs.bin/ # 例:lang/units.csv.patch.csv
│ ├─ mis.vromfs.bin/ # 例:自定义任务 .blk
│ ├─ ui/atlases.vromfs.bin/ # 例:gameuiskin/my_tank_ico.svg
│ └─ ui/images.vromfs.bin/ # 例:units/my_tank.ddsx
├─ addon/ # 独立 3D 模型与贴图扩展包(res/*.grp, res/*.dxp.bin)
├─ gui-patches/ # GUI 界面脚本补丁(复用 patches/gui/<id>/patch.jsonc 格式)
├─ exe-patches/ # 声明式 exe 补丁(复用 patches/exe/<id>/patch.jsonc 格式)
├─ files/ # 可选:大型整合包附带的配套涂装/瞄具/音效/任务
└─ config-blk.patch.jsonc # 可选:游戏根目录 config.blk 的增量补丁
```
## 3. 清单文件(`mod.jsonc`)
```jsonc
{
"format": 1,
"id": "author.my-mod",
"name": { "zh-CN": "我的载具模组", "en": "My Vehicle Mod" },
"version": "0.1.0",
"author": "YourName",
"description": { "zh-CN": "示例说明。", "en": "Example description." },
"api_version": "0.1-experimental",
"game_versions": ["2.61.0.*"],
"requires": [],
"conflicts": [],
"load_after": [],
"load_before": []
}
```
## 4. 魔改载具与添加全新 3D 模型载具
### 4.1 增量 BLK 补丁(`*.blk.patch.jsonc`)
对公共大表(如 `char.vromfs.bin/config/shop.blk`、`config/wpcost.blk` 或原版载具 `.blk`),使用 `.patch.jsonc` 描述操作,保留精确 BLK 数据类型:
- `setParam` / `addParam` / `removeParam`:修改、追加或删除参数;
- `upsertBlock` / `addBlock` / `removeBlock`:创建、合并或删除子块(路径支持 `range[2]` 索引定位同名块);
- `cloneBlock`:从同文件复制已有载具块为新名称并嵌套 `ops` 修改字段(衍生魔改车的最快方式)。
在 `client/packs/aces.vromfs.bin/` 中直接放入文本格式的 `.blk` 文件(如新建载具物理或炮弹定义)时,构建器会自动编译为游戏需要的二进制 BLK。
### 4.2 添加全新 3D 模型与贴图(跨盘零拷贝)
将 CDK 导出的 `.grp` 3D 模型和 `.dxp.bin` 贴图包放入 `client/addon/res/`:
- 不含 `res/grp_hdr.vromfs.bin` 的散装模型/贴图包,启动器会通过 **Windows NTFS 目录联接点(Junction)** 零拷贝跨盘挂载到 `<客户端>/content/pkg_user/<modId>/`,并自动注册进 `aces.vromfs.bin` `config/settings.blk`,引擎会自动扫描 `res/` 下的所有 `.grp` 和 `.dxp.bin`;
- 自带 `res/grp_hdr.vromfs.bin` 的正式包会挂载到 `content/pkg_wto_<modId>/`;
- 配合 `char.vromfs.bin/config/shop.blk.patch.jsonc`、`config/wpcost.blk.patch.jsonc`、`lang.vromfs.bin/lang/units.csv.patch.csv` 以及 `ui/atlases.vromfs.bin` / `ui/images.vromfs.bin` 图标,本地服务器启动时会自动从叠加后的数据包识别新载具的价格、研发、改装件与商城礼包。
## 5. 服务端数值叠加与 JS/TS 脚本接口
### 5.1 `server/tuning/*.jsonc` 与 `$listOps`
合并顺序:`内置默认 → Mod 1 → Mod 2 → … → 玩家在启动器「数值」页的覆盖`。
列表型配置支持 `$listOps` 增量操作(`append`、`prepend`、`upsertBy`、`removeBy`),避免多个模组互相覆盖礼包或兑换码列表:
```jsonc
{
"codes": {
"$listOps": [
{ "op": "append", "items": [{ "code": "MY-MOD-GIFT", "silverLions": 500000 }] }
]
}
}
```
### 5.2 服务端 JS/TS 脚本(`server/index.ts`)
启动器内置 Node 24 原生支持可擦除类型语法的 `.ts`(不使用 `enum`、`namespace` 或构造函数参数属性),也可自行编译为 `.mjs`/`.js` 分发。完整类型定义见 `packages/mods/src/server-api.ts`:
- `ctx.hooks.onDatasetsLoaded(datasets)`:修改或追加已提取的载具、经济、任务数据集;
- `ctx.hooks.onCataloguesReady(catalogues)`:访问商店、交易所、箱子、游戏模式、档案与匹配推送;
- `ctx.hooks.profile.onRefresh(profile, userId)`:每次档案刷新时运行;
- `ctx.hooks.battle.onBeforeMatchmake / onLiveFact / onAfterSettle`:介入匹配、局内实时事件与战后结算;
- `ctx.storage.forPlayer(userId)` / `ctx.storage.shared()`:按账号隔离的独立 JSON 存档区(不污染原版 `profile.blk`);
- `ctx.routes.char / matching / http`:拦截或注册自定义 `cln_*` 动作、匹配服 RPC,或挂载 `/mods/<modId>/*` 本地网页路由。
## 6. 声明式 `exe` 与 `gui` 补丁
`client/exe-patches/` 与 `client/gui-patches/` 直接复用项目本体的补丁格式(见 [补丁指南](patches.md#zh-cn))。
离线版核心隔离补丁永远最先规划并锁定字节区间:**任何与核心补丁字节重叠的模组 `exe` 补丁会被直接拒绝并记录在日志中**。
参考模板见 `dev-docs/templates/mods/` 下的两份可直接运行的示例模组。
---
<a id="english"></a>
[简体中文](#zh-cn) · English · [Back to Dev Docs](README.md#english)
> **Experimental Notice (API `0.1-experimental`)**
> WTOffline is in early development. Server script APIs, internal server structures, hook signatures, and declarative `exe` patch contracts may change at any time without backwards compatibility. Mod authors using server scripts or binary patches should be prepared to update their mods alongside core releases.
## 1. Architecture & Repository Layout
WTOffline uses an **additive, stackable** mod system and does not support whole-pack `.vromfs.bin` replacements. Enabled mods merge in the load order configured on the launcher's Mods page (later entries take higher priority) and roll back cleanly when disabled.
- **Global mod repository**: shared across all clients (default `<launcher userData>/mods`, configurable to any drive from Mods › Presets & repository).
- **Per-client configuration**: toggles, load order, and named presets (with JSON import/export) are stored separately for each offline client.
- **Native content boundary**: standalone skins (`UserSkins/`), sights (`UserSights/`), and missions (`UserMissions/`) continue to use War Thunder's native folders; the launcher never overwrites player-managed native files.
## 2. Standard Mod Folder Layout
Place each mod in its own folder under `mods/` with a `mod.jsonc` manifest at its root:
- `server/tuning/*.jsonc`: multi-layer tuning overlays supporting `$listOps` (`append`, `prepend`, `upsertBy`, `removeBy`).
- `server/index.ts` (or `.mjs` / `.js`): server script entry exporting `default { setup(ctx) { ... } }`. Runs directly on Electron's bundled Node 24 (erasable TypeScript syntax supported natively).
- `client/packs/<pack>/`: additive overlays for `char.vromfs.bin`, `aces.vromfs.bin`, `gui.vromfs.bin`, `lang.vromfs.bin`, `mis.vromfs.bin`, `game.vromfs.bin`, `ui/atlases.vromfs.bin`, and `ui/images.vromfs.bin` (`*.blk.patch.jsonc`, `*.csv.patch.csv`, or added files; text `.blk` files compile to binary BLK automatically).
- `client/addon/`: 3D models and textures (`res/*.grp`, `res/*.dxp.bin`). Mounted zero-copy across drives via NTFS Directory Junctions into `content/pkg_user/<modId>` (or `content/pkg_wto_<modId>` when a compiled `res/grp_hdr.vromfs.bin` is included) and registered in `aces.vromfs.bin` `config/settings.blk`.
- `client/gui-patches/` & `client/exe-patches/`: declarative patches using the same format as `patches/gui/` and `patches/exe/` (see [Patch guide](patches.md#english)). Core offline isolation exe patches remain locked; any overlapping mod exe patch is rejected.
- `client/files/` & `client/config-blk.patch.jsonc`: optional companion assets and `config.blk` patches.
See the runnable templates in `dev-docs/templates/mods/` and full TypeScript API definitions in `packages/mods/src/server-api.ts`.