1433 lines
264 KiB
HTML
1433 lines
264 KiB
HTML
<!doctype html>
|
||
<html lang="zh-CN" data-mode="dark">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>WTOffline · 开发者文档与 PR 规范</title>
|
||
<meta name="description" content="WTOffline 开发者手册:术语词典、整体架构与请求调用链、好坏代码对比与 AI 约束提示词、EXE 57 字节逐指令拆解、Native 6 文件完整接线、流量日志排错与 PR 规范。">
|
||
<link rel="icon" type="image/svg+xml" href="assets/icon.svg">
|
||
<link rel="stylesheet" href="site/tokens.css?v=0e5a4a4c">
|
||
<link rel="stylesheet" href="site/home.css?v=355a55fd">
|
||
</head>
|
||
<body data-lang="zh" data-page="dev-docs">
|
||
<div id="blink"></div>
|
||
<div class="bg"></div>
|
||
<div class="grid"></div>
|
||
|
||
<header class="nav">
|
||
<a class="brand" href="/">
|
||
<img class="logo" src="assets/icon.svg" alt="" width="28" height="28" draggable="false">
|
||
<span>WT<span class="brand-accent">OFFLINE</span></span>
|
||
</a>
|
||
<nav class="nav-links">
|
||
<a class="active" href="/dev-docs">
|
||
<span class="lang-zh">开发者文档</span>
|
||
<span class="lang-en">Dev Docs</span>
|
||
</a>
|
||
<a href="/mod-docs">
|
||
<span class="lang-zh">模组文档</span>
|
||
<span class="lang-en">Mod Docs</span>
|
||
</a>
|
||
<a href="/releases">
|
||
<span class="lang-zh">下载</span>
|
||
<span class="lang-en">Download</span>
|
||
</a>
|
||
<a class="hide-sm" href="/#community">
|
||
<span class="lang-zh">社区</span>
|
||
<span class="lang-en">Community</span>
|
||
</a>
|
||
<a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline" target="_blank" rel="noopener">Git ↗</a>
|
||
<button class="ui-btn" type="button" data-lang-toggle title="Switch Language / 切换语言" aria-label="Switch language">
|
||
<span class="lang-zh">EN</span>
|
||
<span class="lang-en">中文</span>
|
||
</button>
|
||
<button class="ui-btn ui-iconbtn" type="button" data-mode-toggle title="Toggle Light / Dark" aria-label="Toggle color theme">
|
||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
|
||
<circle cx="12" cy="12" r="4"/>
|
||
<path d="M12 2.5v2.2M12 19.3v2.2M21.5 12h-2.2M4.7 12H2.5M18.7 5.3l-1.5 1.5M6.8 17.2l-1.5 1.5M18.7 18.7l-1.5-1.5M6.8 6.8 5.3 5.3"/>
|
||
</svg>
|
||
</button>
|
||
</nav>
|
||
</header>
|
||
|
||
<main class="focus-in">
|
||
<div class="wrap wrap-doc">
|
||
<section class="page-head stagger">
|
||
<a class="back-link" href="/">
|
||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
|
||
<path d="M19 12H5M11 6l-6 6 6 6"/>
|
||
</svg>
|
||
<span class="lang-zh">返回 WTOffline 主页</span>
|
||
<span class="lang-en">Back to WTOffline Home</span>
|
||
</a>
|
||
<div class="eyebrow">DEVELOPER HANDBOOK</div>
|
||
<h1 class="page-title">
|
||
<span class="lang-zh">开发者手册(全 5 章)</span>
|
||
<span class="lang-en">Developer Handbook (5 Chapters)</span>
|
||
</h1>
|
||
<p class="lede">
|
||
<span class="lang-zh">与仓库 <code>dev-docs/</code> 1:1 同源生成的完整手册:从核心黑话翻译、服务端完整请求走读、EXE 逐字节拆解与 Native 6 文件接线,到 AI 辅助开发约束提示词与流量日志排错法。</span>
|
||
<span class="lang-en">Compiled 1:1 from repository <code>dev-docs/</code>: plain-language glossary, end-to-end server action trace, 57-byte EXE patch anatomy, 6-file Native C wiring, AI coding assistant prompt, and traffic-log debugging.</span>
|
||
</p>
|
||
</section>
|
||
|
||
<div class="doc-layout">
|
||
<aside class="doc-toc" aria-label="Handbook navigation">
|
||
<div class="doc-tools">
|
||
<div class="doc-search">
|
||
<input class="doc-search-input" type="search" data-doc-search placeholder="搜索全书关键词…" aria-label="Search handbook">
|
||
<div class="doc-search-results" role="listbox"></div>
|
||
</div>
|
||
<button class="doc-expand-btn" type="button" data-expand-all aria-pressed="false">
|
||
<span class="lang-zh">展开全部章节(支持 Ctrl+F)</span>
|
||
<span class="lang-en">Expand All Chapters (Ctrl+F)</span>
|
||
</button>
|
||
</div>
|
||
<div>
|
||
<p class="doc-toc-title">
|
||
<span class="lang-zh">章节目录(共 5 章)</span>
|
||
<span class="lang-en">Chapters (5)</span>
|
||
</p>
|
||
<nav>
|
||
<a href="#ch-01" data-chapter="ch-01" class="active" aria-current="page">
|
||
<span class="lang-zh">01 · 导览、术语词典与入口地图</span>
|
||
<span class="lang-en">01 · Overview, Glossary & Map</span>
|
||
</a>
|
||
<a href="#ch-02" data-chapter="ch-02">
|
||
<span class="lang-zh">02 · 架构原理、分层与好坏代码</span>
|
||
<span class="lang-en">02 · Architecture, Layers & Patterns</span>
|
||
</a>
|
||
<a href="#ch-03" data-chapter="ch-03">
|
||
<span class="lang-zh">03 · 补丁编写与逐字节真实拆解</span>
|
||
<span class="lang-en">03 · Patch Authoring & Byte Anatomy</span>
|
||
</a>
|
||
<a href="#ch-04" data-chapter="ch-04">
|
||
<span class="lang-zh">04 · 环境搭建、流量日志与检查</span>
|
||
<span class="lang-en">04 · Setup, Traffic Logs & Checks</span>
|
||
</a>
|
||
<a href="#ch-05" data-chapter="ch-05">
|
||
<span class="lang-zh">05 · PR 模板与提交前自查表</span>
|
||
<span class="lang-en">05 · PR Template & Self-Check</span>
|
||
</a>
|
||
</nav>
|
||
</div>
|
||
<div class="doc-toc-card">
|
||
<span class="lang-zh">需要现成的 EXE / GUI / Native 补丁脚手架模板?</span>
|
||
<span class="lang-en">Need ready-to-copy EXE, GUI, and Native patch templates?</span>
|
||
<br>
|
||
<a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/dev-docs/templates" target="_blank" rel="noopener">
|
||
<span class="lang-zh">打开补丁与 PR 模板目录 ↗</span>
|
||
<span class="lang-en">Open Patch & PR Templates ↗</span>
|
||
</a>
|
||
</div>
|
||
</aside>
|
||
|
||
<div class="doc-body">
|
||
<article class="doc-section doc-chapter active" id="ch-01">
|
||
<div class="lang-zh">
|
||
<h2>01 · 导览、核心术语词典与任务入口</h2>
|
||
<p>欢迎参与 WTOffline 开发。本目录面向<strong>项目贡献者、客户端新版本适配者,以及使用 AI 编程工具辅助逆向定位与编写代码的协作者</strong>。无论你之前是否接触过 Dagor 引擎或《战争雷霆》客户端内部结构,都可以按本指南一步步上手。</p>
|
||
<blockquote><p><strong>如果你只是想制作载具属性魔改、自定义地图池、全新 3D 载具模型或服务端脚本模组</strong>:你完全不需要修改 WTOffline 核心源码,请直接阅读 <a href="/mod-docs">模组开发文档(mod-dev-docs)</a> 或访问 <a href="https://wto-project.net/mod-docs" target="_blank" rel="noopener">在线模组文档</a>。</p></blockquote>
|
||
<hr>
|
||
<h3 id="ch-01-zh-第一次来-建议按什么顺序读">第一次来,建议按什么顺序读?</h3>
|
||
<p>不要一上来就啃二进制补丁字段或反汇编地址。按下面三步顺读,大约 15–20 分钟就能建立完整的全局认知:</p>
|
||
<ol><li><strong><a href="#ch-02">开发理念、架构分层与工程规范</a></strong>(<strong>必读第一站</strong>):搞清楚启动器、单端口本地模拟服务器(<code>apps/server</code>)与进程内网络/对局桥接库(<code>wto_ws.dll</code>)是如何协同工作的;顺着一个真实的 <code>cln_set_researchable</code> 动作看懂服务端四层代码怎么流转;掌握「三类数值放哪里」「好代码 vs 坏代码」真实对比,以及可直接复制给 AI 编程助手的工程约束提示词。</li><li><strong><a href="#ch-03">补丁编写与新版本适配指南</a></strong>:当确认本地服务器与原版配置确实无法解决问题、必须修改客户端时,按这篇手把手教程拆包查看原版文件、用 <code>tools/re/cli.py</code> 反汇编定位,并对照完整无省略的 GUI(<code>patches/gui/</code>)、EXE(<code>patches/exe/</code>)和 Native(<code>patches/native/</code> + <code>native/wto-net/</code>)真实案例编写跨版本自适应补丁。</li><li><strong><a href="#ch-04">环境搭建、流量调试与提交自查</a></strong>:配齐本地开发环境,学会直接阅读 <code><数据目录>/logs/</code> 下自动解码的明文 BLK 流量日志快速抓 Bug,按改动类型运行对应的必跑检查命令,并对照 <a href="#ch-05">PR 模板</a> 提交高质量改动。</li></ol>
|
||
<hr>
|
||
<h3 id="ch-01-zh-核心概念与术语词典">核心概念与术语词典</h3>
|
||
<p>在阅读后续文档或翻阅源码时,你会频繁遇到以下专有名词。这里用大白话一次性讲透:</p>
|
||
<h4 id="ch-01-zh-1-vromfs-bin-dagor-虚拟只读归档包">1. <code>vromfs.bin</code>(Dagor 虚拟只读归档包)</h4>
|
||
<p>《战争雷霆》不会把成千上万个零散脚本和配置文件直接摊在硬盘上,而是打包进若干个 <code>.vromfs.bin</code> 归档文件中。它在结构上分两层:外层是带简单异或混淆的 Zstandard(zstd)压缩容器、内部镜像 MD5 与末尾 256 字节签名;内层是文件表(全小写相对路径、偏移量、可选 SHA-1 摘要,以及可选的共享键名表 <code>nm</code> 与 <code>.dict</code> 压缩字典)。项目中的 <code>@wto/vromfs</code>(<code>readArchive</code> / <code>getFile</code> / <code>putFile</code> / <code>writeArchive</code>)负责无损读写这些归档包。</p>
|
||
<p>不同归档包各司其职,查数据时按下面这张表找最快:</p>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th>归档包路径(相对于游戏根目录)</th><th>里面装什么核心内容</th><th>WTOffline 哪里会用到它</th></tr></thead><tbody><tr><td><code>char.vromfs.bin</code></td><td>科技树排布(<code>config/shop.blk</code>)、载具价格/研发点/修车费/改装件(<code>config/wpcost.blk</code>)、全局经济与分房公式(<code>config/warpoints.blk</code>、<code>config/rank.blk</code>)、初始档案模板(<code>config/default_user.blk</code>)、任务列表与成就解锁(<code>config/mislist.blk</code>、<code>config/pve_awards.blk</code>、<code>config/unlocks.blk</code>)、涂装/贴花/挂件/道具定义(<code>config/skins.blk</code>、<code>config/decals.blk</code>、<code>config/attachable.blk</code>、<code>config/items.blk</code>)以及 <code>config/network.blk</code></td><td><code>packages/patcher</code> 构建副本时改写服务地址与隐藏载具可见性;<code>apps/server</code> 启动时从中提取绝大部分静态经济与科技树数据集</td></tr><tr><td><code>aces.vromfs.bin</code></td><td>局内玩法与连杀支援规则(<code>config/gameplay.blk</code>)、界面与礼包 GUID 映射(<code>config/gui.blk</code>)、工坊配方(<code>config/workshop.blk</code>)以及全部载具的气动/损伤/武器预设模型(<code>gamedata/flightmodels/…</code>、<code>gamedata/units/…</code>)</td><td><code>packages/patcher</code> 写入本地商店礼包 GUID;<code>apps/server</code> 提取对局出生规则、礼包映射与载具武器预设</td></tr><tr><td><code>gui.vromfs.bin</code></td><td>客户端全部机库、弹窗、商店、战后结算与 HUD 界面的 Quirrel 脚本(<code>scripts/**/*.nut</code>)与布局模板(<code>.blk</code> / <code>.css</code>)</td><td><code>patches/gui/</code> 在构建副本时对少数客户端界面脚本做精确片段替换</td></tr><tr><td><code>mis.vromfs.bin</code></td><td>多人对战与单人任务的地图逻辑、出生点区域、战区与胜利条件定义(<code>gamedata/missions/**/*.blk</code>)</td><td><code>apps/server</code> 启动时扫描提取多人任务目录(<code>multiplayer-missions</code> 数据集),用于匹配开局</td></tr><tr><td><code>lang.vromfs.bin</code></td><td>分号分隔的多语言本地化词条表(<code>lang/units.csv</code>、<code>lang/menu.csv</code>、<code>lang/unlocks_achievements.csv</code> 等)</td><td><code>apps/server</code> 提取载具、国家与物品的多语言显示名称,供本地网页商店、交易所和启动器展示</td></tr><tr><td><code>ui/atlases.vromfs.bin</code></td><td>科技树载具剪影与阵营国旗等矢量图标(<code>gameuiskin/*.svg</code>)</td><td><code>apps/server</code> 按需提取金币车/礼包车/市场车图标与国旗(<code>web-icons</code> 数据集)供游戏内嵌网页商店渲染</td></tr><tr><td><code>ui/images.vromfs.bin</code></td><td>界面位图插画、礼包封面与飞行员头像贴图(<code>images/**</code>)</td><td><code>apps/server</code> 为内嵌网页商店展示商品封面,并为启动器存档页渲染玩家头像</td></tr></tbody></table></div>
|
||
<h4 id="ch-01-zh-2-blk-datablock-dagor-树状键值与块数据格式">2. <code>BLK</code> / <code>DataBlock</code>(Dagor 树状键值与块数据格式)</h4>
|
||
<p><code>BLK</code>(引擎 C++ 类名为 <code>DataBlock</code>)是贯穿整个游戏的通用配置与通信载体,结构类似「带严格值类型的树状 JSON」:每个节点既可以挂载带类型的参数(如整数 <code>:i</code>、浮点 <code>:r</code>、字符串 <code>:t</code>、布尔 <code>:b</code>、三维向量 <code>:p3</code>、矩阵 <code>:m</code> 等),也可以嵌套任意多层同名或异名子块。在 WTOffline 中你会见到它的四种形态,全部由 <code>@wto/blk</code>(<code>decodeBlk</code>、<code>encodeBlk</code>、<code>formatBlkText</code>、<code>parseBlkText</code>)统一处理:</p>
|
||
<ul><li><strong>归档包内的紧凑二进制 BLK</strong>:<code>char.vromfs.bin</code> 与 <code>aces.vromfs.bin</code> 里的 <code>.blk</code> 通常将键名抽离到包内共享键名表 <code>nm</code>(甚至配合 <code>.dict</code> 字典做二次 zstd 压缩),解码时需传入 <code>SharedNameContext</code>;</li><li><strong><code>char</code> 协议单包二进制 BLK</strong>:HTTP 响应体首字节带压缩标记(例如 <code>0x69</code> 即 <code>'i'</code> 表示未压缩二进制 BLK,另有 zlib / zstd 标记),后面紧跟自包含二进制 BLK;</li><li><strong><code>PBBD</code> 批处理流</strong>:客户端上传云端键位与设置(<code>cln_bulk_set_gblk</code>)时使用的多段二进制容器(魔数 <code>"PBBD"</code>),每段内嵌独立 BLK 块;</li><li><strong>本地存档自包含二进制 BLK</strong>:<code><数据目录>/saves/<userId>/profile.blk</code> 直接用 <code>encodeBlk()</code> 落盘,完整保留每个字段的原始值类型,避免整数与浮点数混淆导致客户端解析异常。</li></ul>
|
||
<h4 id="ch-01-zh-3-本地模拟的四大-gaijin-后端服务">3. 本地模拟的四大 Gaijin 后端服务</h4>
|
||
<p>原版联网客户端并不是只跟一台服务器通信,而是同时访问多套职责不同的后端服务。<code>apps/server</code> 在本机 <code>127.0.0.1:<端口></code> 上把这四大核心服务全部接管并模拟:</p>
|
||
<ul><li><strong><code>yuplay2</code>(登录与权益服务,<code>login/</code>、<code>store/</code>)</strong>:处理客户端启动时的账号密码表单登录(<code>POST /login.php</code>),签发本地会话凭据与 <code>userId</code>,并提供商店礼包与激活码相关的 Web 接口;</li><li><strong><code>char</code>(角色档案服务器 Character Server,<code>char/</code>)</strong>:机库阶段最核心的请求入口(原域名 <code>char-*.warthunder.com</code>)。客户端通过 HTTP 请求头 <code>action</code> 指明动作名(如 <code>cln_get_user_stat</code>、<code>cln_buy_aircraft</code>、<code>cln_set_researchable</code>、<code>cln_update</code>),通过 <code>uidhint</code> 指明当前账号,请求与响应主体以二进制 BLK 为主,负责完整玩家档案下发、科技树研发、买车买改装、乘员组训练、联队、云端键位设置及战后经济结算;</li><li><strong><code>matching</code>(匹配与开局二进制长连接服务,<code>matching/</code>)</strong>:基于 TCP 的自定义二进制长连接协议(首字节固定为 <code>0x01</code>,9 字节包头 + LZ4 压缩负载,握手后启用流加密)。负责向客户端下发可用游戏模式列表、接收排队与小队状态,并在匹配成功时向客户端下发完整的对局启动参数(地图、天气、AI 名单、自定义计分参数等);</li><li><strong><code>inventory</code>(外部物品库与交易所服务,<code>inventory/</code>)</strong>:模拟 <code>inventory-proxy-*.gaijin.net/char</code> 接口(动作名如 <code>GetInventory</code>、<code>GetItemDefsClient</code>、<code>GetItemPrices</code>、<code>GetItemDefChances</code>、<code>ExchangeItems</code>)。与 <code>char</code> 不同,客户端 <code>charsq</code> 模块直接将响应解析为 UTF-8 JSON,且要求关键响应附带 RSA 内容签名;用于驱动补给箱开箱、载具兑换券、工坊合成与本地模拟交易所。</li></ul>
|
||
<h4 id="ch-01-zh-4-localmp-为什么既要有-apps-server-又必须有-wtows-dll">4. <code>LocalMP</code>(为什么既要有 <code>apps/server</code>,又必须有 <code>wto_ws.dll</code>?)</h4>
|
||
<p>初看代码库时最容易产生的疑问是:<strong>既然已经用 Node.js 写了本地服务器 <code>apps/server</code>,为什么还要用 C 写一个 <code>native/wto-net/</code>(编译为 <code>wto_ws.dll</code>)注入进游戏进程?</strong></p>
|
||
<p>原因在于《战争雷霆》客户端与专用对局服务器(Dedicated Server)的代码裁剪边界:</p>
|
||
<ul><li>原版零售客户端 <code>aces.exe</code> 内部其实保留了一套名为 <strong><code>LocalMP</code>(本地多人主机,<code>mp_is_host == 1 && mp_is_local_mp == 1</code>)</strong> 的局内会话模式,能够在本机直接跑完整的物理引擎、弹道解算、损伤模型与 AI 驾驶逻辑,因此 WTOffline 不需要重写整套游戏物理服务器;</li><li><strong>但是</strong>,Gaijin 在编译零售版 <code>aces.exe</code> 时,把原本只在云端专用对局服务器上运行的大量「对局裁判(Host Referee)」处理逻辑裁掉了——包括:<strong>响应玩家选车与挂载弹药并生成实体、允许 <code>BotSystem</code> 刷出 AI 载具、判定击杀/助攻/占点并往记分牌加分、陆战街机连杀空中支援事件生成、核弹落地直接判胜,以及过滤本不该发给受害者之外玩家的炮管/炮闩受损提示</strong>;</li><li>因此两套组件分工明确、缺一不可:<strong><code>apps/server</code> 负责对局之外的一切</strong>(登录、机库养成、商店市场、排队拼装任务配置、战后发银狮/研发点与存档持久化);而 <strong><code>wto_ws.dll</code> 在对局进行期间通过 MinHook 挂载在 <code>aces.exe</code> 进程内</strong>,一方面把所有硬编码外网 Socket 连接重定向回 <code>127.0.0.1</code>,另一方面通过 <code>local_mp_*</code> 模块把被客户端裁掉的局内主机裁判逻辑原原本本补齐。</li></ul>
|
||
<h4 id="ch-01-zh-5-quirrel-nut-与-sq-exe-字节码编译器">5. <code>Quirrel (.nut)</code> 与 <code>sq.exe</code> 字节码编译器</h4>
|
||
<p><code>gui.vromfs.bin</code> 中的 <code>.nut</code> 文件使用的是 Gaijin 基于 Squirrel 深度定制的强词法作用域脚本语言 <strong><a href="https://quirrel.io/" target="_blank" rel="noopener">Quirrel</a></strong>。客户端在玩家点击登录、进入机库前会一次性编译加载大批 <code>.nut</code> 脚本。</p>
|
||
<p>为什么修改 <code>patches/gui/</code> 后<strong>不能只靠肉眼或普通语法树(AST)检查</strong>,而需要用对应提交版本的原生编译器 <code>sq.exe</code> 跑 <code>wtotools check-gui</code>?因为 Quirrel 的 <code>compilestring</code> 字节码编译阶段会执行严格的词法绑定检查:</p>
|
||
<ul><li>如果你在 <code>replace.nut</code> 里声明了一个 <code>local</code> 变量,恰好与该文件顶部 <code>from ... import *</code> 通配导入的某个全局常量重名;</li><li>或者在函数尚未定义/绑定前做了前向调用;</li><li>或者把 Quirrel 保留字(最典型的坑是 <code>base</code>)当成了普通变量名;</li></ul>
|
||
<p>普通语法检查器都会放行,但真进游戏时 Quirrel 虚拟机就会直接中断模块加载,导致客户端<strong>永远卡死在登录后的「正在加载…」转圈界面</strong>。<code>wtotools check-gui</code> 会在内存中打上所有 GUI 补丁并逐个调用 <code>sq.exe</code> 编译字节码,把这类致命隐患在提交前 100% 拦住。</p>
|
||
<h4 id="ch-01-zh-6-rva-虚拟地址-va-与进程运行时指针">6. <code>RVA</code>、虚拟地址(<code>VA</code>)与进程运行时指针</h4>
|
||
<p>在看 <code>patches/exe/</code>、<code>patches/native/</code> 或使用逆向工具时,务必分清这三种地址表示:</p>
|
||
<ul><li><strong><code>VA</code>(Virtual Address,静态虚拟地址)</strong>:Ghidra 或 IDA 打开 64 位 Windows 程序 <code>aces.exe</code> 时,默认镜像基址(<code>ImageBase</code>)是 <code>0x140000000</code>,因此反编译器里显示的函数地址往往长这样:<code>0x1412657a0</code>;</li><li><strong><code>RVA</code>(Relative Virtual Address,相对虚拟地址)</strong>:减去镜像基址后的纯偏移,即 <strong><code>RVA = VA - ImageBase</code></strong>(例如 <code>0x1412657a0 - 0x140000000 = 0x12657a0</code>)。因为 Windows 的 ASLR(地址空间布局随机化)每次启动游戏都会把 <code>aces.exe</code> 加载到不同的内存基址,<strong>仓库内所有补丁注释、<code>tools/re/cli.py</code> 参数以及生成的 <code>win64/wto_ws.cfg</code> 一律使用 RVA</strong>;</li><li><strong>运行时绝对指针(Runtime Pointer)</strong>:游戏启动后,<code>wto_ws.dll</code>(见 <code>native/wto-net/src/local_mp_values.c</code>)通过 <code>(uint8_t *)GetModuleHandleW(NULL) + rva</code> 把 <code>wto_ws.cfg</code> 里的 RVA 换算成当次进程内的真实内存地址存入 <code>g_lmp</code>。</li></ul>
|
||
<h4 id="ch-01-zh-7-features-功能标签与三条-核心安全锁-essentialfeatures">7. <code>features</code> 功能标签与三条「核心安全锁」(<code>ESSENTIAL_FEATURES</code>)</h4>
|
||
<p>每个 <code>patches/exe/**/patch.jsonc</code>、<code>patches/gui/**/patch.jsonc</code> 和 <code>patches/native/*.jsonc</code> 都包含一个 <code>features</code> 字符串数组,标签以功能分组字母 <code>A-</code>..<code>K-</code> 开头(例如 <code>"G-Store"</code>、<code>"K-AI opponents and teammates"</code>、<code>"K-Kill, assist and capture scoring"</code>)。当游戏刚发布小版本更新、个别非关键补丁暂时失配时,启动器依靠这些标签清晰告知玩家「当前版本哪一项具体子功能暂不可用」,其余功能仍可正常游玩。</p>
|
||
<p>但其中有 <strong>3 个定义在 <code>packages/patcher/src/essential-features.ts</code>(<code>ESSENTIAL_FEATURES</code>)中的硬阻断安全标签</strong>例外:</p>
|
||
<ol><li><strong><code>A-Offline startup</code></strong>:离线副本能否启动的根基(例如跳过修改后 <code>.vromfs.bin</code> 的官方签名校验);</li><li><strong><code>A-Isolation from the official game</code></strong>:与官方正版客户端的完全隔离(将用户配置存档改到独立 <code>WTOffline</code> 目录、注册表改到 <code>Software\WTOffline</code>、剥离反作弊启动器与崩溃上报);</li><li><strong><code>A-No Gaijin connections</code></strong>:彻底阻断连向 Gaijin 官方服务器(例如将导入表 <code>WS2_32.dll</code> 替换为 <code>wto_ws.dll</code> 并在加载时强制挂载网络重定向钩子)。</li></ol>
|
||
<p>只要带有上述任一标签的补丁未能成功定位和应用,<code>blockingFailures()</code> 就会<strong>终止副本构建与启动</strong>,防止离线客户端触碰玩家的线上正版数据或直连官方服务器,从而保护玩家的线上账号安全。</p>
|
||
<hr>
|
||
<h3 id="ch-01-zh-按你想做的事-直接定位代码目录">按你想做的事,直接定位代码目录</h3>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th>你想做什么</th><th>具体源码子目录</th><th>对口参考章节</th></tr></thead><tbody><tr><td>新增或修复机库动作路由(买车、研发、乘员、联队、贴花、战后结算等 <code>char</code> 接口)</td><td><code>apps/server/src/char/actions/</code>(协议适配)+ <code>apps/server/src/<领域>/</code>(纯业务规则,如 <code>research/</code>、<code>modifications/</code>、<code>crew/</code>、<code>battle/</code>)</td><td><a href="#ch-02">架构分层与真实动作追踪</a></td></tr><tr><td>修改玩家档案字段读写、新号初始资产或存档落盘格式</td><td><code>apps/server/src/profile/</code>、<code>apps/server/src/saves/</code>、<code>apps/server/src/storage/</code></td><td><a href="#ch-02">存档层与原子写盘规范</a></td></tr><tr><td>从游戏原版数据包提取新的载具/经济/任务/本地化静态数据</td><td><code>apps/server/src/game-data/datasets/</code></td><td><a href="#ch-02">三类数值与缓存数据集</a></td></tr><tr><td>调整离线版默认倍率、商店定价、补给箱权重、AI 重生数或上限参数</td><td><code>apps/server/tuning/*.jsonc</code> + <code>apps/server/src/tuning/</code> + 各领域 <code>*-tuning.ts</code></td><td><a href="#ch-02">三类数值放哪里</a></td></tr><tr><td>修改本地内嵌网页商店、模拟交易所或服务器脚本模组运行时</td><td><code>apps/server/src/store/</code>、<code>apps/server/src/inventory/</code>、<code>apps/server/src/web-ui/</code>、<code>apps/server/src/mods/</code></td><td><a href="#ch-02">服务器目录全景图</a></td></tr><tr><td>修改桌面启动器界面、安装向导、存档管理页、模组管理页或更新器</td><td><code>apps/launcher/src/</code></td><td><a href="#ch-04">检查命令清单</a></td></tr><tr><td>修改离线副本构建管线、归档包改写、BLK 编解码、EXE 补丁引擎或模组加载器</td><td><code>packages/patcher/</code>、<code>packages/blk/</code>、<code>packages/vromfs/</code>、<code>packages/exepatch/</code>、<code>packages/mods/</code></td><td><a href="#ch-02">仓库级单向依赖约定</a></td></tr><tr><td>修改游戏内界面弹窗、按钮或结算页脚本(Quirrel <code>.nut</code>)</td><td><code>patches/gui/<领域>/<名称>/</code></td><td><a href="#ch-03">GUI 补丁完整范例</a></td></tr><tr><td>修改 <code>aces.exe</code> 机器码分支或只读常量字符串</td><td><code>patches/exe/<领域>/<名称>/</code></td><td><a href="#ch-03">EXE 补丁 57 字节逐指令拆解</a></td></tr><tr><td>编写或修复 <code>wto_ws.dll</code> 局内 <code>LocalMP</code> 原生裁判钩子</td><td><code>patches/native/<模块>.jsonc</code> + <code>native/wto-net/src/</code></td><td><a href="#ch-03">Native 模块 6 文件串联教程</a></td></tr><tr><td>协助适配刚发布的《战争雷霆》客户端小版本更新</td><td><code>patches/versions/</code>、<code>scripts/record-game-version.ts</code></td><td><a href="#ch-03">适配新版本客户端四步流程</a></td></tr></tbody></table></div>
|
||
</div>
|
||
<div class="lang-en">
|
||
<h2>01 · Overview, Core Glossary & Task Map</h2>
|
||
<p>Welcome to WTOffline development. This directory is written for <strong>project contributors, client build adapters, and collaborators using AI coding assistants to reverse-engineer, investigate, and fix issues</strong>. Even if you have never worked with the Dagor Engine or War Thunder internals before, these guides walk you through every layer from first principles.</p>
|
||
<blockquote><p><strong>Just want to create vehicle stat tweaks, custom map pools, standalone 3D vehicles, or server script mods?</strong> You do not need to modify WTOffline's core source code at all—head straight to the <a href="/mod-docs">Mod Development Docs (<code>mod-dev-docs</code>)</a> or the <a href="https://wto-project.net/mod-docs" target="_blank" rel="noopener">online Mod Docs</a>.</p></blockquote>
|
||
<hr>
|
||
<h3 id="ch-01-en-recommended-reading-order">Recommended Reading Order</h3>
|
||
<p>Do not jump straight into raw byte patterns or disassembly offsets on your first visit. Reading the three core guides in this order builds the full mental model in 15–20 minutes:</p>
|
||
<ol><li><strong><a href="#ch-02">Design Philosophy, Architecture & Engineering Rules</a></strong> (<strong>Start here</strong>): Understand how the launcher, single-port local server (<code>apps/server</code>), and in-process Winsock/LocalMP bridge (<code>wto_ws.dll</code>) fit together; trace one real <code>cln_set_researchable</code> request end-to-end across all four server layers; learn where the three categories of numbers belong; review four real Good vs. Bad code comparisons; and grab the copy-paste prompt block for AI coding assistants.</li><li><strong><a href="#ch-03">Patch Authoring & Version Adaptation Guide</a></strong>: Once you have confirmed that the local server and stock configuration genuinely cannot solve an issue, follow step-by-step instructions to unpack stock archives, disassemble <code>aces.exe</code> with <code>tools/re/cli.py</code>, and study un-elided real patches across <code>patches/gui/</code>, <code>patches/exe/</code>, and <code>patches/native/</code> + <code>native/wto-net/</code>.</li><li><strong><a href="#ch-04">Environment Setup, Traffic Debugging & PR Guide</a></strong>: Set up the Windows x64 toolchain, inspect auto-decoded readable BLK payloads under <code><data-dir>/logs/</code> to pinpoint bugs in minutes, run the required verification commands for your change type, and fill out the <a href="#ch-05">PR template</a>.</li></ol>
|
||
<hr>
|
||
<h3 id="ch-01-en-core-concepts-glossary">Core Concepts & Glossary</h3>
|
||
<p>You will encounter these War Thunder and WTOffline terms throughout the codebase and documentation. Here is what each one means in plain language:</p>
|
||
<h4 id="ch-01-en-1-vromfs-bin-dagor-virtual-read-only-file-system-archive">1. <code>vromfs.bin</code> (Dagor Virtual Read-Only File System Archive)</h4>
|
||
<p>Instead of shipping tens of thousands of loose scripts and config files on disk, War Thunder bundles them into <code>.vromfs.bin</code> archives. Each archive has two layers: an outer container (Zstandard compression with lightweight XOR obfuscation, an MD5 digest of the inner image, and a 256-byte signature tail) and an inner image (a lowercase relative path table, byte offsets, optional SHA-1 digests, plus an optional shared name map <code>nm</code> and <code>.dict</code> zstd dictionary). <code>@wto/vromfs</code> (<code>readArchive</code>, <code>getFile</code>, <code>putFile</code>, <code>writeArchive</code>) reads and repacks these archives losslessly.</p>
|
||
<p>Use this lookup table whenever you need to find where a stock script, config, or asset lives:</p>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th>Archive path (relative to game root)</th><th>What it stores</th><th>How WTOffline uses it</th></tr></thead><tbody><tr><td><code>char.vromfs.bin</code></td><td>Tech-tree layout (<code>config/shop.blk</code>), per-vehicle prices, RP, repair costs, and modifications (<code>config/wpcost.blk</code>), global economy and BR formulas (<code>config/warpoints.blk</code>, <code>config/rank.blk</code>), new-account profile template (<code>config/default_user.blk</code>), mission lists and unlock definitions (<code>config/mislist.blk</code>, <code>config/pve_awards.blk</code>, <code>config/unlocks.blk</code>), camos/decals/decorators/classic items (<code>config/skins.blk</code>, <code>config/decals.blk</code>, <code>config/attachable.blk</code>, <code>config/items.blk</code>), and <code>config/network.blk</code></td><td><code>packages/patcher</code> rewrites backend URLs and hidden-vehicle visibility during copy builds; <code>apps/server</code> extracts most economy, tech-tree, unlock, and mission datasets from it at startup</td></tr><tr><td><code>aces.vromfs.bin</code></td><td>Match gameplay and killstreak rules (<code>config/gameplay.blk</code>), UI config and store bundle GUID mappings (<code>config/gui.blk</code>), workshop recipes (<code>config/workshop.blk</code>), and all vehicle flight/damage/weapon preset files (<code>gamedata/flightmodels/…</code>, <code>gamedata/units/…</code>)</td><td><code>packages/patcher</code> injects offline store bundle GUIDs; <code>apps/server</code> extracts spawn rules, bundle mappings, and weapon presets</td></tr><tr><td><code>gui.vromfs.bin</code></td><td>All in-game hangar, modal, store, debriefing, and HUD Quirrel scripts (<code>scripts/**/*.nut</code>) and layout templates (<code>.blk</code> / <code>.css</code>)</td><td><code>patches/gui/</code> applies minimal surgical text replacements to UI scripts when building the offline copy</td></tr><tr><td><code>mis.vromfs.bin</code></td><td>Multiplayer and single-player mission logic, spawn zones, capture points, and victory rules (<code>gamedata/missions/**/*.blk</code>)</td><td><code>apps/server</code> scans it at startup (<code>multiplayer-missions</code> dataset) to build matchmaking mission definitions</td></tr><tr><td><code>lang.vromfs.bin</code></td><td>Semicolon-separated localization CSV tables (<code>lang/units.csv</code>, <code>lang/menu.csv</code>, <code>lang/unlocks_achievements.csv</code>, etc.)</td><td><code>apps/server</code> extracts localized vehicle, country, and item names for the in-game web store, marketplace, and launcher</td></tr><tr><td><code>ui/atlases.vromfs.bin</code></td><td>Tech-tree vehicle silhouettes and country flags in SVG format (<code>gameuiskin/*.svg</code>)</td><td><code>apps/server</code> extracts icons for gift/premium/market vehicles and flags (<code>web-icons</code> dataset) for the local web store and marketplace</td></tr><tr><td><code>ui/images.vromfs.bin</code></td><td>UI raster artwork, bundle cover images, and pilot avatar textures (<code>images/**</code>)</td><td><code>apps/server</code> serves store/marketplace item artwork and renders player profile icons for the launcher's saves page</td></tr></tbody></table></div>
|
||
<h4 id="ch-01-en-2-blk-datablock-dagor-typed-hierarchical-data-format">2. <code>BLK</code> / <code>DataBlock</code> (Dagor Typed Hierarchical Data Format)</h4>
|
||
<p><code>BLK</code> (named <code>DataBlock</code> in Dagor Engine C++ code) is the typed tree format used everywhere in War Thunder for configs, network payloads, and player profiles. Think of it as a tree of named blocks and typed key-value parameters (<code>:i</code> integers, <code>:r</code> floats, <code>:t</code> strings, <code>:b</code> booleans, <code>:p3</code> 3D vectors, <code>:m</code> matrices, etc.). You will encounter four binary/wire representations in WTOffline, all handled by <code>@wto/blk</code> (<code>decodeBlk</code>, <code>encodeBlk</code>, <code>formatBlkText</code>, <code>parseBlkText</code>):</p>
|
||
<ul><li><strong>Slimmed binary BLK inside <code>.vromfs.bin</code></strong>: <code>.blk</code> files inside <code>char.vromfs.bin</code> and <code>aces.vromfs.bin</code> strip repeated key names into the archive's shared name map <code>nm</code> (and may compress payloads with an archive <code>.dict</code> zstd dictionary), decoded by passing <code>SharedNameContext</code> to <code>decodeBlk</code>;</li><li><strong>Framed binary BLK on <code>char</code> HTTP exchanges</strong>: Single-document HTTP payloads prefixed with a 1-byte compression marker (e.g., <code>0x69</code> / <code>'i'</code> for uncompressed binary BLK, or zlib/zstd markers);</li><li><strong><code>PBBD</code> batch streams</strong>: Multi-chunk binary streams (magic <code>"PBBD"</code>) used by <code>online_storage</code> (<code>cln_bulk_set_gblk</code>) when uploading keybindings and cloud settings, where each chunk carries an embedded BLK block;</li><li><strong>Self-contained binary BLK on disk</strong>: Player profiles saved at <code><data>/saves/<userId>/profile.blk</code> via <code>encodeBlk()</code>, preserving every parameter's exact scalar/vector type so the game client never misinterprets ints as floats or vice versa.</li></ul>
|
||
<h4 id="ch-01-en-3-the-four-gaijin-backend-services-emulated-locally">3. The Four Gaijin Backend Services Emulated Locally</h4>
|
||
<p>A live War Thunder client talks to several distinct backend services rather than a single monolithic API. <code>apps/server</code> emulates all four locally on a single <code>127.0.0.1:<port></code> listener:</p>
|
||
<ul><li><strong><code>yuplay2</code> (Login & Entitlement APIs, <code>login/</code>, <code>store/</code>)</strong>: Handles form-encoded credential sign-in (<code>POST /login.php</code>), issues local session tokens and <code>userId</code>s, and serves store/redeem-code web endpoints;</li><li><strong><code>char</code> (Character Server, <code>char/</code>)</strong>: The primary hangar RPC service (<code>char-*.warthunder.com</code>). Requests specify the operation via the <code>action</code> HTTP header (such as <code>cln_get_user_stat</code>, <code>cln_buy_aircraft</code>, <code>cln_set_researchable</code>, <code>cln_update</code>) and identify the player via <code>uidhint</code>, exchanging binary BLK payloads for full profile sync, tech-tree research, vehicle/modification/crew purchases, squadrons, cloud settings, and post-battle settlement;</li><li><strong><code>matching</code> (Binary Matchmaking & Session RPC, <code>matching/</code>)</strong>: A persistent binary TCP protocol (first byte <code>0x01</code>, 9-byte header + LZ4-compressed frame, stream-encrypted after handshake) that serves game-mode lists, manages queues and squad state, and dispatches complete battle launch configurations (map, weather, AI bot rosters, <code>customScoreParams</code>, etc.);</li><li><strong><code>inventory</code> (External Inventory & Marketplace Service, <code>inventory/</code>)</strong>: Emulates <code>inventory-proxy-*.gaijin.net/char</code> (<code>GetInventory</code>, <code>GetItemDefsClient</code>, <code>GetItemPrices</code>, <code>GetItemDefChances</code>, <code>ExchangeItems</code>). Unlike <code>char</code>, the client's <code>charsq</code> subsystem parses raw HTTP bodies directly as UTF-8 JSON and verifies RSA content signatures—powering supply chests, vehicle coupons, workshop crafting, and the simulated marketplace.</li></ul>
|
||
<h4 id="ch-01-en-4-localmp-why-wtoffline-needs-both-apps-server-and-wtows-dll">4. <code>LocalMP</code> (Why WTOffline Needs Both <code>apps/server</code> AND <code>wto_ws.dll</code>)</h4>
|
||
<p>A natural question for newcomers is: <strong>If <code>apps/server</code> already emulates Gaijin's backend in TypeScript, why do we also compile <code>native/wto-net/</code> into <code>wto_ws.dll</code> and load it inside <code>aces.exe</code>?</strong></p>
|
||
<p>The answer comes down to what Gaijin kept vs. stripped in the retail game client:</p>
|
||
<ul><li>Retail <code>aces.exe</code> still contains an in-process <strong><code>LocalMP</code> session mode (<code>mp_is_host == 1 && mp_is_local_mp == 1</code>)</strong> capable of running full physics, ballistics, damage models, and AI piloting/driving locally—meaning WTOffline does not have to reimplement a Dedicated Server physics engine from scratch;</li><li><strong>However</strong>, when building retail <code>aces.exe</code>, Gaijin compiled out many <strong>Dedicated Server battle referee handlers</strong> that normally run only in the cloud during live matches: <strong>spawning player vehicles and weapon loadouts on selection, granting <code>BotSystem</code> permission to spawn AI vehicles, awarding kill/assist/capture scoreboard points, running Ground Arcade killstreak air events, triggering nuclear instant victories, and filtering barrel/breech/part-damage HUD notices so enemy hits are not misdelivered to the local player</strong>;</li><li>Consequently, the two halves have a crisp boundary: <strong><code>apps/server</code> owns everything before and after a battle</strong> (login, hangar progression, economy, store/market, matchmaking setup, post-battle Silver Lions/RP settlement, and atomic save persistence), while <strong><code>wto_ws.dll</code> hooks <code>aces.exe</code> in-process via MinHook</strong>—redirecting hardcoded outbound Winsock traffic to <code>127.0.0.1</code> and restoring stripped host-side referee logic <strong>during</strong> live <code>LocalMP</code> battles via modular <code>local_mp_*</code> bridges.</li></ul>
|
||
<h4 id="ch-01-en-5-quirrel-nut-the-sq-exe-bytecode-compiler">5. <code>Quirrel (.nut)</code> & the <code>sq.exe</code> Bytecode Compiler</h4>
|
||
<p>UI scripts inside <code>gui.vromfs.bin</code> (<code>.nut</code> files) are written in <strong><a href="https://quirrel.io/" target="_blank" rel="noopener">Quirrel</a></strong>, Gaijin's Squirrel-derived scripting language with strict lexical scoping. When logging into the hangar, the client preloads and compiles UI script modules en masse.</p>
|
||
<p>Why is running <code>wtotools check-gui</code> with the matching <code>sq.exe</code> compiler mandatory whenever you touch <code>patches/gui/</code>, instead of eyeballing diffs or running an AST-only syntax check? Because Quirrel's <code>compilestring</code> bytecode compiler enforces lexical binding rules that AST parsers ignore:</p>
|
||
<ul><li>Declaring a <code>local</code> identifier that collides with a constant imported via <code>from ... import *</code> at the top of the file;</li><li>Referencing an unbound local/module function before its declaration;</li><li>Using a Quirrel reserved keyword (most notoriously <code>base</code>) as a variable name.</li></ul>
|
||
<p>Any of those mistakes looks syntactically innocent on paper, yet aborts module compilation inside the game client and <strong>freezes login indefinitely on the <code>"Loading..."</code> spinner</strong>. <code>wtotools check-gui</code> applies every GUI patch in memory and compiles each modified <code>.nut</code> file to Quirrel bytecode via <code>sq.exe</code>, catching 100% of lexical and constant-binding errors before commit.</p>
|
||
<h4 id="ch-01-en-6-rva-vs-virtual-address-va-vs-runtime-pointer">6. <code>RVA</code> vs. Virtual Address (<code>VA</code>) vs. Runtime Pointer</h4>
|
||
<p>Keep these three address forms straight whenever you read disassembly or write <code>patches/</code>:</p>
|
||
<ul><li><strong><code>VA</code> (Virtual Address)</strong>: When you open 64-bit <code>aces.exe</code> in Ghidra or IDA, the default PE <code>ImageBase</code> is <code>0x140000000</code>, so addresses appear as <code>0x1412657a0</code>;</li><li><strong><code>RVA</code> (Relative Virtual Address)</strong>: Offset from the image base, i.e., <strong><code>RVA = VA - ImageBase</code></strong> (<code>0x1412657a0 - 0x140000000 = 0x12657a0</code>). Because Windows ASLR loads <code>aces.exe</code> at a fresh random base address on every launch, <strong>every <code>patches/</code> comment, <code>tools/re/cli.py</code> argument, and <code>win64/wto_ws.cfg</code> entry uses RVAs exclusively</strong>;</li><li><strong>Runtime Pointer</strong>: When the game starts, <code>wto_ws.dll</code> (<code>native/wto-net/src/local_mp_values.c</code>) adds the live module base <code>(uint8_t *)GetModuleHandleW(NULL) + rva</code> and stores the resulting process pointer in <code>g_lmp</code>.</li></ul>
|
||
<h4 id="ch-01-en-7-features-tags-the-three-essential-safety-locks-essentialfeatures">7. <code>features</code> Tags & the Three Essential Safety Locks (<code>ESSENTIAL_FEATURES</code>)</h4>
|
||
<p>Every definition in <code>patches/exe/**/patch.jsonc</code>, <code>patches/gui/**/patch.jsonc</code>, and <code>patches/native/*.jsonc</code> lists one or more <code>features</code> tags prefixed by inventory category (<code>A-</code> through <code>K-</code>, such as <code>"G-Store"</code>, <code>"K-AI opponents and teammates"</code>, or <code>"K-Kill, assist and capture scoring"</code>). If a minor client update breaks an optional patch, the launcher uses these tags to show the player exactly which non-essential gameplay feature is degraded while still letting them play.</p>
|
||
<p>Three tags defined in <code>packages/patcher/src/essential-features.ts</code> (<code>ESSENTIAL_FEATURES</code>) are <strong>hard-blocking safety locks</strong>:</p>
|
||
<ol><li><strong><code>A-Offline startup</code></strong>: Required for the offline copy to boot at all (e.g., bypassing <code>.vromfs.bin</code> pack signature checks so modified archives load);</li><li><strong><code>A-Isolation from the official game</code></strong>: Keeps the offline copy completely separate from the official client (redirecting user saves/settings to <code>WTOffline</code>, registry keys to <code>Software\WTOffline</code>, and disabling the anti-cheat launcher and crash reporter);</li><li><strong><code>A-No Gaijin connections</code></strong>: Guarantees zero traffic reaches Gaijin's live servers (e.g., rewriting the PE import descriptor from <code>WS2_32.dll</code> to <code>wto_ws.dll</code> and enforcing Winsock redirection hooks on attach).</li></ol>
|
||
<p>If <strong>any</strong> patch carrying one of these three tags fails to resolve, <code>blockingFailures()</code> immediately aborts copy installation and launch—protecting the player's official online installation and account from ever being touched.</p>
|
||
<hr>
|
||
<h3 id="ch-01-en-jump-straight-to-your-task">Jump Straight to Your Task</h3>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th>What you want to do</th><th>Specific codebase directories</th><th>Primary reference</th></tr></thead><tbody><tr><td>Add or fix hangar actions (research, purchases, crews, squadrons, decals, post-battle settlement, etc.)</td><td><code>apps/server/src/char/actions/</code> (wire handlers) + <code>apps/server/src/<domain>/</code> (pure rules: <code>research/</code>, <code>modifications/</code>, <code>crew/</code>, <code>battle/</code>, etc.)</td><td><a href="#ch-02">Server Layers & Action Walkthrough</a></td></tr><tr><td>Change player profile fields, new-account defaults, or save persistence / format upgrades</td><td><code>apps/server/src/profile/</code>, <code>apps/server/src/saves/</code>, <code>apps/server/src/storage/</code></td><td><a href="#ch-02">Persistence & Atomic Writes</a></td></tr><tr><td>Extract new static vehicle, economy, mission, or localization data from stock archives</td><td><code>apps/server/src/game-data/datasets/</code></td><td><a href="#ch-02">Three Categories of Values</a></td></tr><tr><td>Adjust offline reward multipliers, store prices, supply-chest weights, AI respawn counts, or limits</td><td><code>apps/server/tuning/*.jsonc</code> + <code>apps/server/src/tuning/</code> + <code><domain>/*-tuning.ts</code></td><td><a href="#ch-02">Three Categories of Values</a></td></tr><tr><td>Work on the in-game web store, simulated marketplace, or server script mod runtime</td><td><code>apps/server/src/store/</code>, <code>apps/server/src/inventory/</code>, <code>apps/server/src/web-ui/</code>, <code>apps/server/src/mods/</code></td><td><a href="#ch-02">Server Directory Map</a></td></tr><tr><td>Change the desktop launcher UI, install wizard, saves manager, mods page, or auto-updater</td><td><code>apps/launcher/src/</code></td><td><a href="#ch-04">Required Check Commands</a></td></tr><tr><td>Modify copy building, archive patching, BLK/vromfs codecs, EXE patch engine, or mod loader</td><td><code>packages/patcher/</code>, <code>packages/blk/</code>, <code>packages/vromfs/</code>, <code>packages/exepatch/</code>, <code>packages/mods/</code></td><td><a href="#ch-02">Workspace Dependency Rule</a></td></tr><tr><td>Patch in-game UI modals, buttons, or debriefing Quirrel scripts (<code>.nut</code>)</td><td><code>patches/gui/<domain>/<name>/</code></td><td><a href="#ch-03">GUI Patches Full Example</a></td></tr><tr><td>Patch machine instructions or constant <code>.rdata</code> strings in <code>aces.exe</code></td><td><code>patches/exe/<domain>/<name>/</code></td><td><a href="#ch-03">EXE Patch 57-Byte Anatomy</a></td></tr><tr><td>Write or fix <code>wto_ws.dll</code> in-process <code>LocalMP</code> battle host hooks</td><td><code>patches/native/<module>.jsonc</code> + <code>native/wto-net/src/</code></td><td><a href="#ch-03">Native Modules 6-File Wiring</a></td></tr><tr><td>Adapt WTOffline to a newly released minor War Thunder client build</td><td><code>patches/versions/</code>, <code>scripts/record-game-version.ts</code></td><td><a href="#ch-03">Adapting a New Client Build</a></td></tr></tbody></table></div>
|
||
</div>
|
||
<div class="doc-pager">
|
||
|
||
<a class="doc-pager-btn next" href="#ch-02" data-chapter="ch-02"><span class="doc-pager-label"><span class="lang-zh">下一章 →</span><span class="lang-en">Next →</span></span><span class="doc-pager-title"><span class="lang-zh">02 · 架构原理、分层与好坏代码</span><span class="lang-en">02 · Architecture, Layers & Patterns</span></span></a>
|
||
</div>
|
||
</article>
|
||
|
||
<article class="doc-section doc-chapter" id="ch-02">
|
||
<div class="lang-zh">
|
||
<h2>02 · 架构原理、真实目录分层与好坏代码对比</h2>
|
||
<p>这篇文档不只罗列「能做什么、不能做什么」,而是先把 <strong>WTOffline 为什么这样分层、客户端与本地服务器各自承担什么角色</strong> 彻底讲透。只要底层运行模型在脑子里立住了,项目里的每一条工程规范都是自然而然的推论。</p>
|
||
<hr>
|
||
<h3 id="ch-02-zh-1-先建立全景图-wtoffline-到底是怎么跑起来的">1. 先建立全景图:WTOffline 到底是怎么跑起来的?</h3>
|
||
<p>WTOffline 采用与《逃离塔科夫》单机版(SPT)一脉相承的本地模拟架构:<strong>不推翻重写游戏引擎,而是让原版客户端相信自己正连着完整的线上后端,把它的全部网络请求收口到本机 <code>127.0.0.1</code> 的模拟服务器,并仅在进程内补齐客户端被裁剪掉的局内主机裁判函数。</strong></p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>text</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>玩家在启动器点击「启动游戏」
|
||
│
|
||
├─ 1. 桌面启动器(apps/launcher)调用构建引擎(packages/patcher)
|
||
│ └─ 以「严格只读」方式读取玩家的官方原版客户端目录,向独立离线副本目录生成:
|
||
│ ├─ char.vromfs.bin / aces.vromfs.bin:改写 network.blk 服务地址 -> 127.0.0.1:<端口>,注入本地商店礼包 GUID
|
||
│ ├─ gui.vromfs.bin:应用 patches/gui/(仅修补本地服务器无法覆盖的极少数界面脚本点)
|
||
│ ├─ win64/aces.exe:应用 patches/exe/(跳过包验签、隔离存档与注册表、禁用反作弊、把导入表 WS2_32.dll 改为 wto_ws.dll)
|
||
│ └─ win64/wto_ws.dll + win64/wto_ws.cfg:按 patches/native/*.jsonc 动态解析函数 RVA 与结构体偏移写入配置文件
|
||
│
|
||
├─ 2. 本地模拟服务器(apps/server,仅绑定 127.0.0.1:<端口> 单端口监听)
|
||
│ ├─ 启动阶段:通过 DatasetCache 从游戏归档包提取静态事实(科技树、价格、改装件、任务、多语言词条),加载 tuning/*.jsonc
|
||
│ └─ 运行阶段:port-listener.ts 按每个 TCP 连接的首批字节自动分流多协议:
|
||
│ ├─ 首字节 0x01 ──▶ MatchingService(9 字节头 + LZ4 压缩帧,握手后流加密)
|
||
│ ├─ 明文 PASS/NICK/USER/CAP/PING ──▶ IrcServer(机库与小队聊天)
|
||
│ ├─ 首字节 0x16(TLS ClientHello) ──▶ 本地证书 TLSSocket 解密后再二次分流至 IrcServer 或 HTTP
|
||
│ └─ 其余全部明文连接 ──▶ HTTP 服务器(yuplay2 登录、char 二进制 BLK 档案、inventory 签名 JSON、内嵌网页商店)
|
||
│ (注:HTTP WebSocket upgrade 请求会被记录日志后直接 socket.destroy() 拒绝)
|
||
│
|
||
└─ 3. 隔离离线副本(win64/aces.exe + win64/wto_ws.dll)
|
||
├─ 进程加载时(DllMain):wto_ws.dll 通过 wto_ws.def 把全部 Winsock 导出(含序号导出)转发至系统 ws2_32.dll,
|
||
│ 同时用 MinHook 挂钩系统 ws2_32.dll 的域名解析与连接函数;若任一网络钩子安装失败,DllMain 直接返回 FALSE 终止启动!
|
||
├─ 机库阶段:登录、拉取档案、科技树研发、买车买改装、乘员训练、开箱、逛商店,全部走原版协议由 apps/server 处理并原子落盘
|
||
└─ 对局阶段:MatchingService 下发任务参数使客户端以 LocalMP 本地主机开局;
|
||
wto_ws.dll 校验 PE 指纹匹配后启用 local_mp_* 模块,在进程内补齐选车出生、AI 刷出、击杀助攻占点计分、
|
||
陆战街机空中支援、核弹判胜与受损提示过滤;战后由客户端经 cln_update 回传 apps/server 结算银狮/研发点并落盘</code></pre></div>
|
||
<h4 id="ch-02-zh-1-1-单端口多协议分流细节-apps-server-src-transport">1.1 单端口多协议分流细节(<code>apps/server/src/transport/</code>)</h4>
|
||
<p>客户端内部既有读取 <code>config/network.blk</code> 的模块,也有把域名、端口甚至 <code>https://</code> 写死在二进制常量里的模块。为了不占用一串本地端口,本地服务器在 <code>apps/server/src/transport/port-listener.ts</code> 中用<strong>单个 TCP 监听端口</strong>接住所有流量,在 <code>socket.once('data', ...)</code> 窥探首批字节后原样退回缓冲区(<code>socket.unshift</code>)并精准派发:</p>
|
||
<ol><li><strong>匹配二进制协议(首字节 <code>0x01</code>)</strong>:匹配 <code>startsMatchingPacket(first)</code> 后交由 <code>MatchingService</code> 处理(9 字节小端包头 + LZ4 数据块,见 <code>matching/matching-frame.ts</code>);</li><li><strong>明文 IRC 握手</strong>:首行以 <code>PASS</code>、<code>NICK</code>、<code>USER</code>、<code>CAP</code> 或 <code>PING</code> 开头时交由 <code>IrcServer</code> 处理;</li><li><strong>TLS 加密连接(首字节 <code>0x16</code>)</strong>:使用本地动态生成的自签名证书建立 <code>TLSSocket</code> 解密,再窥探解密后的首包字节——若是 TLS 承载的 IRC 握手则交由 <code>IrcServer</code>,否则派发给 HTTP 服务器;</li><li><strong>常规 HTTP 连接</strong>:其余连接直接进入 <code>http-listener.ts</code> 的 <code>createHttpServer</code>,由 <code>Router</code> 按原始目标域名(<code>originalHost</code>)和路径分发给 <code>login</code>、<code>char</code>、<code>inventory</code>、<code>store</code>、<code>downloadable-content</code> 等服务。注意:<strong>WTOffline 不使用 WebSocket</strong>——客户端偶然发起的 HTTP <code>upgrade</code> 请求会在 <code>http-listener.ts</code> 中记入流量日志(<code>WebSocket ... (refused)</code>)并立即调用 <code>socket.destroy()</code> 断开。</li></ol>
|
||
<h4 id="ch-02-zh-1-2-wtows-dll-是如何在零外联风险下接管网络的">1.2 <code>wto_ws.dll</code> 是如何在零外联风险下接管网络的?</h4>
|
||
<p>在 <code>patches/exe/online/network-hook-library/patch.jsonc</code> 中,补丁将 <code>aces.exe</code> 只读数据段(<code>.rdata</code>)导入描述符引用的字符串 <code>"WS2_32.dll\0"</code>(<code>57 53 32 5f 33 32 2e 64 6c 6c 00</code>,共 <strong>11 字节</strong>)原位替换为等长的 <code>"wto_ws.dll\0"</code>(<code>77 74 6f 5f 77 73 2e 64 6c 6c 00</code>,同样 <strong>11 字节</strong>):</p>
|
||
<ul><li>Windows 加载器启动 <code>aces.exe</code> 时会自动加载同目录下的 <code>win64/wto_ws.dll</code>,由 <code>native/wto-net/src/wto_ws.def</code> 将客户端导入的全部 Winsock API(包含 31 个按 <code>@序号</code> 导入的函数)无缝转发给系统 <code>ws2_32.dll</code>;</li><li>在 <code>native/wto-net/src/dll_main.c</code> 的 <code>DllMain(DLL_PROCESS_ATTACH)</code> 中,DLL 读取同目录 <code>wto_ws.cfg</code>,通过 MinHook 直接挂钩系统 <code>ws2_32.dll</code> 本身的域名解析与连接/数据报函数(这样进程内<strong>所有模块</strong>的网络调用都会经过重定向,而不只是主程序);</li><li><strong>安全熔断</strong>:如果 <code>MH_Initialize</code>、<code>install_resolver_hooks</code>、<code>install_connection_hooks</code> 或 <code>MH_ApplyQueued</code> 中任何一步失败,<code>DllMain</code> 会记录日志并直接返回 <code>FALSE</code>,让 Windows 加载器中止启动游戏进程,避免游戏在未挂钩状态下连接外网。</li></ul>
|
||
<h4 id="ch-02-zh-1-3-基本设计原则">1.3 基本设计原则</h4>
|
||
<blockquote><p><strong>遇到功能缺失或 Bug 时,先评估能否在 <code>apps/server</code> 或通过下发原版配置字段解决。</strong><br>能通过修正服务端协议响应、补齐下发给客户端的任务 BLK 字段、或读取原版静态配置解决的问题,优先在服务端解决,不新增 <code>.nut</code> 界面脚本或 <code>aces.exe</code> / <code>wto_ws.dll</code> 客户端补丁。客户端补丁留给原版客户端确实缺少对应通路的场景——因为游戏每次小版本更新都会重排二进制代码,而服务端协议与配置字段跨版本稳定得多。</p></blockquote>
|
||
<hr>
|
||
<h3 id="ch-02-zh-2-仓库依赖约定与-apps-server-真实代码地图">2. 仓库依赖约定与 <code>apps/server</code> 真实代码地图</h3>
|
||
<h4 id="ch-02-zh-2-1-仓库级单向依赖约定">2.1 仓库级单向依赖约定</h4>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>text</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>apps/launcher ──┐
|
||
├──▶ packages/* (@wto/blk · @wto/vromfs · @wto/jsonc · @wto/exepatch · @wto/patcher · @wto/mods)
|
||
apps/server ──┘</code></pre></div>
|
||
<ul><li><strong>依赖方向单向朝下</strong>:应用层 <code>apps/*</code> 可以导入共享包 <code>packages/*</code>(开发脚本 <code>scripts/*</code> 可组合两者),<code>packages/*</code> 不应反向导入、引用或硬编码读取 <code>apps/*</code> 下的代码或配置文件。</li><li>当共享包需要补丁集目录、数据目录、端口或礼包列表时,一律由上层调用方通过函数参数显式传入。</li></ul>
|
||
<h4 id="ch-02-zh-2-2-本地服务器-apps-server-src-四层目录全景图">2.2 本地服务器 <code>apps/server/src/</code> 四层目录全景图</h4>
|
||
<p>注意:<strong><code>apps/server/src/</code> 下并没有一个笼统的 <code>rules/</code> 文件夹</strong>——业务规则是按游戏业务领域拆分成独立目录的。动手写代码前请对照下面这张真实目录表放对位置:</p>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th>职责分层</th><th><code>apps/server/src/</code> 下的真实子目录</th><th>这一层负责什么</th><th>应避免放在这一层的内容</th></tr></thead><tbody><tr><td><strong>1. 传输与协议层</strong>(Transport & Protocol)</td><td><code>transport/</code>(单端口分流、HTTP 路由、TLS 证书)、<code>traffic-log/</code>(流量落盘)、<code>login/</code>(<code>yuplay2</code> 登录与会话凭据)、<code>char/</code>(含 <code>char-service.ts</code>、<code>pbbd-stream.ts</code>、<code>char/actions/*-actions.ts</code> 与路由总表 <code>char/actions/action-table.ts</code>)、<code>matching/</code>(LZ4 帧、流加密与排队 RPC)、<code>inventory/</code>(外部物品库与 RSA 内容签名)、<code>store/</code>(礼包与激活码路由)、<code>chat/</code>(IRC 服务端)、<code>cloud-settings/</code>、<code>downloadable-content/</code>、<code>web-ui/</code>(内嵌商店/市场页面渲染)、<code>mods/</code>(服务端模组运行时)</td><td>解码线上传输格式(表单、JSON、压缩标记二进制 BLK、<code>PBBD</code> 流、LZ4 帧),提取并校验请求参数,调用对应领域规则,再把结果编码回客户端期待的原版响应格式</td><td>避免在 <code>*-actions.ts</code> 里堆砌几百行扣钱、算研发点或改档案树的业务逻辑;不要绕过 <code>PlayerProfiles</code> / <code>SaveStore</code> 直接读写磁盘文件</td></tr><tr><td><strong>2. 领域规则层</strong>(Domain Rules)</td><td><code>battle/</code>(开局配置与战后经济结算)、<code>research/</code>(科技树状态、选定研发、自由研发点兑换)、<code>modifications/</code>(改装件、弹链、备用载具、自定义挂载)、<code>crew/</code>(乘员组槽位与技能加点)、<code>items/</code>(加成卡、赌注、折扣券)、<code>repair/</code>(战损折算与免费修车时间)、<code>clans/</code>(联队管理与联队载具研发)、<code>decor/</code>(涂装、贴花、3D 挂件)、<code>unlocks/</code>(成就、头衔、阶段解锁)、<code>player-card/</code>(头像、资料卡展柜)、<code>training/</code>(新手教程首通奖励)、<code>game-modes/</code></td><td><strong>纯业务逻辑</strong>:接收内存中的档案 <code>BlkBlock</code>、已加载的静态数据集与可调参数,校验前置条件,计算并更新档案状态,返回明确的成功或拒绝错误码(如 <code>RuleOutcome</code>)</td><td><strong>零网络、零磁盘 I/O</strong>:不导入 <code>node:http</code>、<code>node:net</code> 或 <code>node:fs</code>,也不要感知外层是 HTTP 还是匹配长连接</td></tr><tr><td><strong>3. 档案与持久化层</strong>(Profile & Persistence)</td><td><code>profile/</code>(<code>player-profiles.ts</code> 及按领域拆分的 <code>profile-*.ts</code> 档案节点读写器、派生状态刷新器)、<code>saves/</code>(<code>save-store.ts</code> 账号存档读写、<code>save-upgrades.ts</code> 版本演进)、<code>accounts/</code>(本地多账号密码与隔离)、<code>storage/</code>(<code>atomic-file.ts</code> 原子落盘)</td><td>封装档案 BLK 树的底层字段读写,管理多账号目录隔离(<code><data>/saves/<userId>/</code>),维护存档 <code>formatVersion</code>,并通过临时文件 + <code>fsync</code> + 原子重命名保证断电不坏档</td><td>不掺杂具体购买价格或研发公式;<strong>任何修改档案的动作必须在磁盘原子写入完成后才能向客户端返回成功响应</strong></td></tr><tr><td><strong>4. 静态数据集与可调配置层</strong>(Datasets & Tuning)</td><td><code>game-data/</code>(<code>game-files.ts</code>、<code>game-pack.ts</code>、<code>datasets/*.ts</code>、<code>datasets/dataset-cache.ts</code>)、仓库默认配置 <code>apps/server/tuning/*.jsonc</code> + 加载校验器 <code>src/tuning/</code>(<code>tuning-loader.ts</code>、<code>tuning-reader.ts</code>、<code>limits-tuning.ts</code>、<code>launcher-tuning.ts</code>)+ 各领域 <code><领域>/*-tuning.ts</code></td><td>启动时从玩家原版 <code>.vromfs.bin</code> 提取静态事实并按包时间戳 + 内部文件 SHA-1 缓存到 <code><data>/cache/datasets/</code>;按「仓库默认 → 已启用模组 → 玩家 <code><data>/tuning/</code> 覆盖」三层合并 JSONC 并做严格类型与范围校验</td><td>不要凭空编造原版数据;必填字段缺失时应明确报错停止启动,避免用隐式假默认值掩盖问题</td></tr></tbody></table></div>
|
||
<p><strong>模块粒度与复用准则</strong>:</p>
|
||
<ul><li>当单个源文件接近 <strong>300 行</strong> 或单个函数接近 <strong>50 行</strong> 时,主动检查是否把协议拆包、档案树遍历和业务公式塞进了同一个文件,及时按上表拆开;</li><li>动手前先搜现有公共基础设施(如 <code>char/actions/rule-answer.ts</code>、<code>profile/profile-*.ts</code>、<code>modifications/equipment-catalogue.ts</code>、<code>web-ui/</code> 公共组件、<code>native</code> 侧的 <code>local_mp_core</code>),尽量复用而不是重复实现。</li></ul>
|
||
<hr>
|
||
<h4 id="ch-02-zh-2-3-跟着真实代码走一遍-clnsetresearchable-是怎么穿透四层的">2.3 跟着真实代码走一遍:<code>cln_set_researchable</code> 是怎么穿透四层的?</h4>
|
||
<p>光看分层表还不够直观。下面我们顺着玩家在科技树点击「研发此载具」时触发的真实动作 <strong><code>cln_set_researchable</code></strong>,逐文件看一遍标准请求流(你在写任何新 <code>char</code> 动作时直接照这个模板写即可):</p>
|
||
<ol><li><strong>客户端发包 → 协议入口解码(<code>apps/server/src/char/char-service.ts</code>)</strong> 客户端向 <code>char-*.warthunder.com</code> 发送 HTTP POST,请求头带 <code>action: cln_set_researchable</code> 与 <code>uidhint: <userId></code>,请求体是带压缩标记的二进制 BLK(包含 <code>unit:t = "us_m4a1_1942_sherman"</code> 与 <code>type:i = 0</code>)。<code>CharService.#handleCore</code> 调用 <code>decodeRequestPayload(request.body)</code>(<code>char/char-payload.ts</code>)把二进制负载解码为 <code>BlkBlock</code>(同时生成可读文本供流量日志记录),然后从 <code>char/actions/action-table.ts</code> 注册表里查出对应的处理器 <code>setResearchable</code>。</li><li><strong>协议动作适配与参数提取(<code>apps/server/src/char/actions/research-actions.ts</code>)</strong> <code>setResearchable</code> 本身只有十几行:它使用公共包装器 <code>ruleAction</code>(<code>char/actions/rule-answer.ts</code>),通过 <code>stringParam(payload, 'unit')</code> 和 <code>intParam(payload, 'type')</code>(<code>char/actions/payload-params.ts</code>)做基础类型提取。如果缺字段就返回 <code>undefined</code>(由 <code>ruleAction</code> 统一回 <code>!ERROR:BAD_PARAMS</code>);参数齐全则返回一个闭包: <code>(profile) => chooseResearchTarget(profile, rules, vehicle, type)</code>。</li><li><strong>加载档案与执行纯领域规则(<code>apps/server/src/research/research-progress.ts</code>)</strong> <code>ruleAction</code> 校验完账号后调用 <code>profiles.updateWhen(account.userId, ...)</code>(<code>profile/player-profiles.ts</code>),由 <code>SaveStore.loadProfile</code> 从 <code><data>/saves/<userId>/profile.blk</code> 读出当前档案并交给纯规则函数 <code>chooseResearchTarget</code>:
|
||
<ul><li>先从启动期提取好的科技树数据集 <code>rules.techTree.vehicle(vehicleName)</code> 查找该载具并核对 <code>typeId</code>;</li><li>调用 <code>currentStatus(profile, rules, vehicleName)</code> 确认该载具在玩家当前档案下确实处于 <code>VehicleStatus.CAN_RESEARCH</code> 状态;若尚未解锁前置车或已拥有,则返回 <code>refused('NOT_AVAILABLE')</code>(<code>{ ok: false, refusal: 'NOT_AVAILABLE' }</code>),<code>updateWhen</code> 看到 <code>false</code> <strong>不触碰磁盘</strong>,<code>ruleAction</code> 向客户端回复纯文本 <code>!ERROR:NOT_AVAILABLE</code>;</li><li>校验通过后,调用 <code>profile/profile-research-targets.ts</code> 的访问器写入新研发目标、清空刚完成研发弹窗标记,返回 <code>ACCEPTED</code>(<code>{ ok: true }</code>)。</li></ul></li><li><strong>先原子落盘,再回包给客户端(<code>profile/player-profiles.ts</code> → <code>saves/save-store.ts</code> → <code>storage/atomic-file.ts</code>)</strong> <code>updateWhen</code> 收到 <code>true</code> 后,先运行 <code>#refresh(profile, userId)</code> 同步档案内的派生状态(解锁阶段、头衔、联队同步等),随即调用 <code>SaveStore.saveProfile(userId, profile)</code>。<code>saveProfile</code> 使用 <code>writeFileAtomic</code> 先在同目录写出临时文件 <code>profile.blk.<pid>.<rand>.tmp</code>、调用 <code>fsyncSync(fd)</code> 刷入物理磁盘、再原位 <code>renameSync</code> 覆盖目标文件并更新 <code>save.json</code>。<strong>只有在磁盘写入彻底成功后</strong>,<code>ruleAction</code> 才调用 <code>clientProfile(profile, account)</code> 组装客户端视图,由 <code>toResponse</code>(<code>char/char-answers.ts</code>)加上 <code>0x69</code> 标记编码成二进制 BLK 返回 HTTP 200。</li></ol>
|
||
<h4 id="ch-02-zh-2-4-存档结构演进规则-saves-save-upgrades-ts">2.4 存档结构演进规则(<code>saves/save-upgrades.ts</code>)</h4>
|
||
<ul><li><strong>领域规则只面向「当前最新版」存档结构编程</strong>:避免在 <code>research/</code>、<code>modifications/</code>、<code>battle/</code> 等业务代码里散落兼容旧测试字段的 <code>if/else</code> 分支;</li><li>当正式版本演进确实改变了 <code>profile.blk</code> 的存储结构时,统一在档案升级链(<code>saves/save-upgrades.ts</code> 的 <code>SaveUpgrades</code>)中追加一步升级函数(<code>steps[n]</code> 负责把 <code>formatVersion</code> 从 <code>n + 1</code> 原地升级到 <code>n + 2</code>)。<code>SaveStore.loadProfile</code> 打开旧版存档时会按顺序跑完升级步骤并立即以最新 <code>formatVersion</code> 原子回写磁盘。仍在同一分支开发调试、尚未发布的草稿改动无需写迁移代码。</li></ul>
|
||
<hr>
|
||
<h3 id="ch-02-zh-3-三类数值放哪里-保持边界清晰">3. 三类数值放哪里(保持边界清晰)</h3>
|
||
<p>在代码审查中,最常见的返工原因就是把不同性质的数字混写在了业务逻辑里。写下任何一个字面量前,先问它属于下面哪一类:</p>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th>数值类别</th><th>典型例子</th><th>唯一正确的存放位置</th><th>常见错误示范</th></tr></thead><tbody><tr><td><strong>① 协议与引擎固定常量</strong></td><td>匹配/局内消息号(如 <code>LMP_MPI_MSG_UNIT_RESPAWN = 0xf030u</code>)、<code>PBBD</code> 魔数(<code>0x44424250</code>)、Dagor 容器布局(<code>dag::Vector</code> 偏移、<code>FastNameMap</code> 移位)、状态位掩码</td><td>定义为具名常量放在对应协议或头文件中,并用<strong>英文注释写明一手来源</strong>(目标客户端版本号 + RVA,或 <a href="https://github.com/GaijinEntertainment/DagorEngine" target="_blank" rel="noopener">DagorEngine 公开源码</a> 文件路径)</td><td>在函数中间裸写 <code>if (msgId == 61488)</code> 或 <code>if (status == 64)</code> 且没有任何来源注释</td></tr><tr><td><strong>② 原版游戏包里本来就有的静态事实</strong></td><td>载具购买银狮/金鹰价、研发点需求、分房权重、维修费、改装件树、多人地图列表、新手教程首通奖励、连杀支援出生高度/距离/速度</td><td>在 <code>apps/server/src/game-data/datasets/</code> 编写 <code>Dataset<T></code>,服务启动时经 <code>DatasetCache</code> 从玩家原版 <code>.vromfs.bin</code> 动态读取并缓存在 <code><data>/cache/datasets/<id>.json</code>;若原版文件缺失必填块/字段,直接 <code>throw new Error(...)</code> 明确报错停止</td><td>把某一个客户端版本的载具表、奖励金额或出生坐标手抄成 TS/C 常量表;或者读不到原版字段时悄悄 <code>?? 10000</code> 填个假默认值蒙混过关</td></tr><tr><td><strong>③ 离线版自己决定的可调参数</strong></td><td>银狮/研发点收益倍率、新账号初始货币、本地商店礼包定价、补给箱掉落权重、AI 机器人重生次数、单次请求数量上限、玩法开关</td><td>仓库默认值放进 <code>apps/server/tuning/<领域>.jsonc</code>,在对应 <code><领域>/*-tuning.ts</code> 用 <code>TuningReader</code> 做启动期类型与上下界校验;运行期由 <code>tuning-loader.ts</code> 自动按「仓库默认 → 已启用模组覆盖 → 玩家 <code><data>/tuning/<领域>.jsonc</code> 覆盖」合并</td><td>把 <code>const RP_MULTIPLIER = 3.0</code> 或硬上限直接写死在结算函数内部,导致玩家和模组作者无法通过 <code>tuning/</code> 调参</td></tr></tbody></table></div>
|
||
<hr>
|
||
<h3 id="ch-02-zh-4-好代码-vs-坏代码-4-组来自真实仓库的代码对比">4. 好代码 vs 坏代码:4 组来自真实仓库的代码对比</h3>
|
||
<p>下面所有正面代码全部取自仓库内的真实文件与真实符号,提交前请逐条对照自查。</p>
|
||
<h4 id="ch-02-zh-对比-1-native-c-钩子-glmp-动态捕获与外层作用域标志-vs-硬编码偏移与-returnaddress">对比 1:Native C 钩子 —— <code>g_lmp</code> 动态捕获与外层作用域标志 vs 硬编码偏移与 <code>_ReturnAddress()</code></h4>
|
||
<p>客户端只要发一个小版本更新(例如 <code>2.61.0.37</code> → <code>2.61.0.58</code>),函数 RVA 会整体漂移,C++ 类成员偏移也会随着字段增删而移动(例如 <code>gameplay</code> 里的连杀规则表偏移就会跨版本变化)。</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>c</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// ❌ 坏写法:硬编码 RVA、硬编码结构体偏移 +0x3798、用 _ReturnAddress() 猜调用栈
|
||
// 只要游戏发一个小补丁,轻则功能静默失效,重则当场非法内存访问崩溃!
|
||
static void __fastcall bad_hook_send_unit_respawn(uint8_t *unit, const void *spawn)
|
||
{
|
||
uintptr_t base = (uintptr_t)GetModuleHandleW(NULL);
|
||
if ((uintptr_t)_ReturnAddress() != base + 0x1281280)
|
||
return;
|
||
const uint8_t *gameplay = *(const uint8_t *const *)(base + 0x7bad2f0);
|
||
const uint8_t *rules = *(const uint8_t *const *)(gameplay + 0x3798);
|
||
/* ... */
|
||
}</code></pre></div>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>c</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// ✅ 好写法(真实代码:native/wto-net/src/local_mp_air_events.c + local_mp_offsets.h + local_mp_values.h):
|
||
// 1. 所有函数地址与结构体偏移均在 patches/native/local_mp_air_events.jsonc 动态捕获写入 wto_ws.cfg,
|
||
// 运行时由 local_mp_values.c 填入全局 extern LmpValues g_lmp(LMP_GP_OFF_KS_RULES 展开为 g_lmp.gp_ks_rules);
|
||
// 2. 用外层函数钩子 + 线程局部标志(__declspec(thread))精确界定调用上下文,彻底告别脆弱的 _ReturnAddress()。
|
||
static SwitchPlayerUnitFn orig_switch_player_unit;
|
||
static __declspec(thread) int g_in_air_event_switch;
|
||
|
||
static void __fastcall hook_switch_player_unit(void *event_mgr, void *participant, uint8_t to_air)
|
||
{
|
||
int previous = g_in_air_event_switch;
|
||
|
||
if (to_air != 0 && ground_arcade_air_events_enabled())
|
||
g_in_air_event_switch = 1;
|
||
orig_switch_player_unit(event_mgr, participant, to_air);
|
||
g_in_air_event_switch = previous;
|
||
}
|
||
|
||
// 读取连杀规则表时全部走 g_lmp 动态解析值,跨小版本自动适配:
|
||
const uint8_t *gameplay = g_lmp.gameplay ? *(const uint8_t *const *)g_lmp.gameplay : NULL;
|
||
const uint8_t *rules = *(const uint8_t *const *)(gameplay + LMP_GP_OFF_KS_RULES);
|
||
uint32_t rule_count = *(const uint32_t *)(gameplay + LMP_GP_OFF_KS_RULE_COUNT);</code></pre></div>
|
||
<h4 id="ch-02-zh-对比-2-原版静态数据-datasetcache-提取与纯领域目录消费-vs-在业务里手抄常量表">对比 2:原版静态数据 —— <code>DatasetCache</code> 提取与纯领域目录消费 vs 在业务里手抄常量表</h4>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>ts</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// ❌ 坏写法:把某一个版本的新手教程首通银狮/金鹰奖励硬编码成服务端字典
|
||
const TUTORIAL_REWARDS: Record<string, { silverLions: number; goldenEagles: number }> = {
|
||
'tutorialB_fighter': { silverLions: 5000, goldenEagles: 100 },
|
||
'tutorialB_assaulter': { silverLions: 5000, goldenEagles: 0 },
|
||
};</code></pre></div>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>ts</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// ✅ 好写法(真实代码:apps/server/src/game-data/datasets/training-missions.ts
|
||
// + apps/server/src/training/training-completion.ts):
|
||
// 1. 在 datasets/ 定义带版本号的 Dataset,从玩家原版 char.vromfs.bin 的
|
||
// config/mislist.blk、config/pve_awards.blk、config/unlocks.blk 提取真实配置;缺失必填块立即抛错。
|
||
export const trainingMissions: Dataset<TrainingMissionsData> = {
|
||
id: 'training-missions',
|
||
version: 1,
|
||
extract(source: DatasetSource): TrainingMissionsData {
|
||
const misSection = blockAt(source.readBlk('char.vromfs.bin', 'config/mislist.blk'), 'training');
|
||
if (!misSection) throw new Error('config/mislist.blk has no "training" block');
|
||
const awardSection = blockAt(source.readBlk('char.vromfs.bin', 'config/pve_awards.blk'), 'training');
|
||
if (!awardSection) throw new Error('config/pve_awards.blk has no "training" block');
|
||
// ... 组装 missions 列表并返回纯数据对象
|
||
return { missions };
|
||
},
|
||
};
|
||
|
||
// 2. 启动时通过 DatasetCache 加载(包未变直接命中缓存),交由纯领域类 TrainingCatalogue 执行幂等发奖:
|
||
const trainingCatalogue = new TrainingCatalogue(datasets.load(trainingMissions));
|
||
trainingCatalogue.applyCompletion(profile, { chapter, mission, difficulty });</code></pre></div>
|
||
<h4 id="ch-02-zh-对比-3-找准修复层级与严格遵守工具链语法-msvc-c-与-node-24-原生-ts">对比 3:找准修复层级与严格遵守工具链语法(MSVC C 与 Node 24 原生 TS)</h4>
|
||
<p><strong>(A)先查原版读取逻辑,别急着动刀写客户端补丁</strong></p>
|
||
<ul><li>❌ <strong>坏思路</strong>:发现多载具出击模式下 AI 机器人死一次就不再重生,立刻去反汇编 <code>BotSystem</code> 准备写 <code>patches/exe/</code> 强改内存计数器。</li><li>✅ <strong>好思路</strong>:先看 <code>MissionSettings::load</code> 是怎么读任务配置的——结果发现原版客户端本来就会读取任务 BLK 根节点的 <code>numBotRespawns</code> 与 <code>numTankBotRespawns</code>,只是服务端组装匹配开局配置时漏填了这两个根字段。在 <code>apps/server</code> 补上几行配置下发即可解决,<strong>零客户端补丁、跨版本永久免疫失效</strong>。</li></ul>
|
||
<p><strong>(B)MSVC C 编译规范与 Node 24 原生 TypeScript 语法规范</strong></p>
|
||
<ul><li><strong>Native C(<code>native/wto-net/</code>)</strong>:由 <code>native/wto-net/build.ps1</code> 调用 Visual Studio <code>cl.exe</code> 以 <code>/W4</code> 编译,要求<strong>零编译警告</strong>。校验自定义结构体大小与成员偏移时使用 <code><windows.h></code> 的编译期断言 <code>C_ASSERT(sizeof(LocalMpAirSpawn) == 0x48);</code>,线程局部存储使用 <code>__declspec(thread)</code>,内存对齐使用 <code>__declspec(align(16))</code>;避免使用 <code>__attribute__((...))</code>、零长数组、可变长数组(VLA)或 <code>void *</code> 算术运算等 GCC/Clang 专属扩展。</li><li><strong>TypeScript(<code>tsconfig.json</code>)</strong>:Node.js 24 直接剥离类型运行 <code>.ts</code> 源码,仓库开启了 <code>erasableSyntaxOnly: true</code>、<code>verbatimModuleSyntax: true</code>、<code>allowImportingTsExtensions: true</code> 与 <code>exactOptionalPropertyTypes: true</code>:</li></ul>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>ts</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// ❌ 坏写法:省略 .ts 后缀、值导入混用纯类型、使用 enum 或构造函数参数属性(Node 24 类型剥离与 tsc 均会报错!)
|
||
import { SaveStore, SaveUpgrades } from '../saves/save-store';
|
||
export enum ResearchState { Locked = 0, Available = 1 }
|
||
export class Handler {
|
||
constructor(private readonly saves: SaveStore) {}
|
||
}</code></pre></div>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>ts</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// ✅ 好写法:相对路径一律写明 .ts 扩展名;纯类型必须用 import type;用 as const 对象或联合类型代替 enum;用 #私有字段
|
||
import { decodeBlk, encodeBlk, type BlkBlock } from '@wto/blk';
|
||
import { writeFileAtomic } from '../storage/atomic-file.ts';
|
||
import type { SaveUpgrades } from './save-upgrades.ts';
|
||
|
||
export const VehicleStatus = {
|
||
CAN_RESEARCH: 2,
|
||
OWNED: 4,
|
||
} as const;</code></pre></div>
|
||
<h4 id="ch-02-zh-对比-4-工程验证-跑真实工具链检查-vs-写几百行假-mock-单元测试">对比 4:工程验证 —— 跑真实工具链检查 vs 写几百行假 Mock 单元测试</h4>
|
||
<ul><li>❌ <strong>坏写法</strong>:改了 <code>patches/gui/</code> 只肉眼扫一眼 <code>.nut</code> 缩进,或者自己造一套模拟客户端行为的 300 行 Mock 单测跑绿就宣称「测试全部通过」——结果真进游戏时因为 Quirrel 词法作用域冲突卡死在登录界面,或者 <code>patch.jsonc</code> 多写了一个非法键在构建副本时直接抛 <code>DefinitionError</code>。</li><li>✅ <strong>好写法</strong>:按改动范围老老实实运行真实检查工具(<code>npm run typecheck</code>、<code>wtotools check-patches</code>、带 <code>sq.exe</code> 的完整 <code>wtotools check-gui</code>、<code>native\wto-net\build.ps1</code>、<code>scripts/build-game-copy.ts</code>),并在 PR 描述中<strong>如实把「已在命令行跑过的检查」与「尚待在隔离副本进游戏实机验收的步骤」分开写清楚</strong>。</li></ul>
|
||
<hr>
|
||
<h3 id="ch-02-zh-5-使用-ai-编程助手协作指南-含一键复制约束提示词">5. 使用 AI 编程助手协作指南(含一键复制约束提示词)</h3>
|
||
<p>我们非常鼓励你使用 Claude Code、Codex、Cursor 等 AI 工具协助阅读反汇编、梳理 <code>.nut</code> 界面调用链、核对 BLK 字段以及快速定位协议差异。</p>
|
||
<p>但 AI 如果事先不知道 WTOffline 的工程边界,很容易顺手写出硬编码 RVA、在 <code>patches/gui/**/patch.jsonc</code> 里误加 <code>requires</code> 字段、漏写 <code>.ts</code> 导入后缀、用 <code>enum</code> 语法或生成一大堆无意义的 Mock 测试。<strong>在让 AI 动手修改任何文件之前,请先把下面这段完整提示词发给它</strong>:</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>text</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>我正在为 WTOffline 仓库开发改动。在编写或修改代码前,请遵循以下项目规范(写任何符号、结构体字段、CLI 参数前先读取仓库真实源文件核实):
|
||
|
||
1. 修复层级优先顺序:先查原版一手依据(玩家只读原版 .vromfs.bin、gui.vromfs.bin 里的 .nut 脚本、DagorEngine 公开源码)。凡是能在本地服务器(apps/server)协议响应、任务配置字段或静态数据集里解决的问题,不要新增 patches/gui/、patches/exe/ 或 native/wto-net/ 客户端补丁。
|
||
2. 动态定位,不硬编码地址与偏移:不要在 C 或 TS 代码里写死 exe 函数地址、RVA、C++ 结构体偏移或 _ReturnAddress() 调用栈判断。所有原生地址与偏移通过 patches/exe/ 或 patches/native/*.jsonc 的 signature / string_ref / within 动态捕获写入 wto_ws.cfg,C 侧通过 extern LmpValues g_lmp(local_mp_values.h / local_mp_offsets.h)读取,并用外层钩子 + __declspec(thread) 标志替代返回地址检查。带尾随立即数的 rip-relative 指令捕获 rel32 时需显式指定 "end"(指令总字节长度)。
|
||
3. 三类数值严格归位:原版游戏自带数值(载具价格、研发点、奖励、任务、出生参数)必须通过 apps/server/src/game-data/datasets/(DatasetCache)从原版归档提取,缺必填字段直接抛错;离线版自定参数与开关统一放入 apps/server/tuning/*.jsonc 并在对应 *-tuning.ts 做启动校验;协议/引擎常量写英文注释注明客户端版本 RVA 或 DagorEngine 源码路径。
|
||
4. 分层与依赖方向:packages/* 不导入或读取 apps/*。apps/server/src/ 内区分传输协议层(char/actions/ 等)、领域规则层(research/、battle/、modifications/ 等纯逻辑目录,无 rules/ 目录,零网络零磁盘 I/O)、档案持久化层(profile/、saves/、storage/atomic-file.ts —— 改档动作先原子写盘成功再回包);存档兼容升级统一通过 saves/save-upgrades.ts 的 formatVersion 步骤处理,不在业务规则里散落旧版兼容分支。单文件接近 300 行或函数接近 50 行时主动拆分。
|
||
5. 严格语法与 Schema 约束:
|
||
- TypeScript 运行在 Node 24 原生类型剥离模式(erasableSyntaxOnly + verbatimModuleSyntax + allowImportingTsExtensions):所有相对导入必须带显式 `.ts` 后缀,纯类型必须用 `import type`,禁止使用 `enum`、`namespace` 和构造函数参数属性。
|
||
- Native C 必须通过 MSVC `native/wto-net/build.ps1`(/W4 零警告),使用 `C_ASSERT` 做编译期布局检查,禁止 GCC 扩展;新增 `.c` 文件需同步加入 `build.ps1` 的 `$sources`。
|
||
- 补丁 JSONC 严格校检未知键:特别是 `patches/gui/**/patch.jsonc` 顶层只允许 `id`、`title`、`features`、`variants`(不要写 `requires`,否则会抛出 DefinitionError)。
|
||
6. 真实工具验证:不要编写庞大的模拟(mock)单元测试套件。按改动运行真实检查命令(`npm run typecheck`、`wtotools check-patches`、带 `--sq` 的完整 `wtotools check-gui`、`native/wto-net/build.ps1`、`scripts/build-game-copy.ts`),并如实区分已跑过的命令与待隔离副本实机验收项。
|
||
7. 提交规范:一笔 PR 只聚焦单一目标;代码注释、补丁 `title`/头部三行说明与约定式提交消息统一使用英文;不把 AI 写入 Co-authored-by;在隔离副本实机进游戏验收前不提交 `patches/versions/*.jsonc`。</code></pre></div>
|
||
<hr>
|
||
<h3 id="ch-02-zh-6-隔离安全与资产边界">6. 隔离安全与资产边界</h3>
|
||
<ol><li><strong>官方原版客户端目录保持只读</strong>:无论是拆包查 <code>.blk</code>、跑 <code>tools/re/cli.py</code> 反汇编、<code>wtotools</code> 核对补丁还是构建游戏副本,原版目录都只作为只读输入源,不要向 Steam 或官方启动器安装目录写入、覆盖或残留文件。</li><li><strong>仅在独立隔离副本中运行测试</strong>:实机运行客户端必须在 Steam 库目录之外的独立离线副本中进行,并确保三大核心安全锁(<code>ESSENTIAL_FEATURES</code>)全部生效——所有联网请求经 <code>wto_ws.dll</code> 收口至 <code>127.0.0.1</code>、本地配置落入独立 <code>WTOffline</code> 用户目录与 <code>Software\WTOffline</code> 注册表路径、反作弊与崩溃上报已剥离,且只使用离线本地账号登录。</li><li><strong>代码仓库不包含受版权保护资产与隐私信息</strong>:不要将 <code>.vromfs.bin</code>、<code>aces.exe</code>、解包出的原版 <code>.nut</code> / <code>.blk</code> 完整文件、真实玩家存档、本机绝对路径或未脱敏日志提交进 Git 仓库。</li></ol>
|
||
</div>
|
||
<div class="lang-en">
|
||
<h2>02 · Architecture, Real Directory Map & Code Patterns</h2>
|
||
<p>Rather than handing you a dry list of dos and don'ts, this guide first explains <strong>how WTOffline actually works under the hood and why its layers are separated the way they are</strong>. Once that architecture clicks, every engineering rule follows naturally.</p>
|
||
<hr>
|
||
<h3 id="ch-02-en-1-end-to-end-architecture-how-wtoffline-works">1. End-to-End Architecture: How WTOffline Works</h3>
|
||
<p>WTOffline uses the same core philosophy as Single Player Tarkov (SPT): <strong>do not rewrite the game engine—keep the retail client running its native code paths, redirect all network traffic to a local emulation server on <code>127.0.0.1</code>, and hook <code>aces.exe</code> in-process only where retail builds stripped Dedicated Server battle handlers.</strong></p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>text</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>Player clicks "Launch" in the Launcher
|
||
│
|
||
├─ 1. Desktop Launcher (apps/launcher) invokes Copy Builder (packages/patcher)
|
||
│ └─ Reads the player's official stock install STRICTLY READ-ONLY and writes an isolated copy:
|
||
│ ├─ char.vromfs.bin / aces.vromfs.bin: rewrite network.blk endpoints -> 127.0.0.1:<port>, inject store bundle GUIDs
|
||
│ ├─ gui.vromfs.bin: apply patches/gui/ (surgical UI script edits only where server responses cannot suffice)
|
||
│ ├─ win64/aces.exe: apply patches/exe/ (bypass pack signature checks, isolate user dir & registry, disable anti-cheat,
|
||
│ │ rewrite PE import name "WS2_32.dll\0" -> "wto_ws.dll\0")
|
||
│ └─ win64/wto_ws.dll + win64/wto_ws.cfg: resolve patches/native/*.jsonc RVAs & struct offsets into wto_ws.cfg
|
||
│
|
||
├─ 2. Local Emulation Server (apps/server, bound strictly to 127.0.0.1:<port>)
|
||
│ ├─ Startup: DatasetCache extracts static facts from stock .vromfs.bin archives; tuning-loader merges tuning/*.jsonc
|
||
│ └─ Runtime: transport/port-listener.ts peeks at the first bytes of every TCP socket on that single port:
|
||
│ ├─ First byte 0x01 ──▶ MatchingService (9-byte header + LZ4 frames, encrypted after handshake)
|
||
│ ├─ Plaintext PASS/NICK/USER/CAP/PING ──▶ IrcServer (hangar & squad chat)
|
||
│ ├─ First byte 0x16 (TLS ClientHello) ──▶ Local-cert TLSSocket decrypts, then routes to IrcServer or HTTP
|
||
│ └─ Everything else ──▶ HTTP server (yuplay2 login, char binary BLK RPC, signed inventory JSON, web store)
|
||
│ (Note: HTTP WebSocket `upgrade` requests are logged and explicitly refused via socket.destroy()!)
|
||
│
|
||
└─ 3. Isolated Offline Client Copy (win64/aces.exe + win64/wto_ws.dll)
|
||
├─ Process attach (DllMain): wto_ws.dll forwards all Winsock exports (including 31 ordinal imports) via wto_ws.def
|
||
│ to system ws2_32.dll and installs MinHook detours on ws2_32's resolver/connect APIs; if ANY network hook fails,
|
||
│ DllMain returns FALSE so Windows immediately aborts launch before a single packet can go online!
|
||
├─ Hangar phase: sign-in, profile load, tech-tree research, purchases, crews, chests, and store all run over stock
|
||
│ protocols against apps/server with atomic disk persistence
|
||
└─ Battle phase: MatchingService launches a LocalMP host session; after verifying PE headers against wto_ws.cfg,
|
||
wto_ws.dll activates local_mp_* modules to restore stripped host handlers (vehicle/loadout spawn, AI bot spawn,
|
||
kill/assist/capture scoring, Ground Arcade air events, nuclear instant win, damage-notice filtering), and post-battle
|
||
results return to apps/server via cln_update for reward settlement and save persistence</code></pre></div>
|
||
<h4 id="ch-02-en-1-1-single-port-protocol-multiplexing-apps-server-src-transport">1.1 Single-Port Protocol Multiplexing (<code>apps/server/src/transport/</code>)</h4>
|
||
<p>Because some client subsystems read URLs from <code>config/network.blk</code> while others use compiled-in hostnames, ports, and <code>https://</code> URLs, <code>apps/server/src/transport/port-listener.ts</code> multiplexes every protocol over <strong>one <code>127.0.0.1</code> TCP port</strong>. On each incoming connection, <code>socket.once('data', ...)</code> inspects the initial bytes, pushes them back with <code>socket.unshift(first)</code>, and hands the socket to the matching protocol handler:</p>
|
||
<ol><li><strong>Matching binary RPC (<code>first[0] === 0x01</code>)</strong>: Matched by <code>startsMatchingPacket(first)</code> (<code>matching/matching-frame.ts</code> — 9-byte little-endian header + LZ4 block) and handed to <code>MatchingService</code>;</li><li><strong>Plaintext IRC handshake</strong>: Connections starting with <code>PASS</code>, <code>NICK</code>, <code>USER</code>, <code>CAP</code>, or <code>PING</code> (<code>startsIrcHandshake</code>) go straight to <code>IrcServer</code>;</li><li><strong>TLS ClientHello (<code>first[0] === 0x16</code>)</strong>: Wrapped in a <code>TLSSocket</code> using the local certificate, after which the first decrypted bytes are inspected again—routing TLS-wrapped IRC handshakes to <code>IrcServer</code> and all other decrypted streams to the HTTP server;</li><li><strong>Plaintext HTTP</strong>: All remaining connections go to <code>createHttpServer</code> (<code>transport/http-listener.ts</code>), where <code>Router</code> dispatches requests by <code>originalHost</code> and path across <code>login</code>, <code>char</code>, <code>inventory</code>, <code>store</code>, <code>downloadable-content</code>, etc. <strong>Note on WebSockets</strong>: WTOffline does <strong>not</strong> use WebSockets—any HTTP <code>upgrade</code> request hits <code>server.on('upgrade', ...)</code> in <code>http-listener.ts</code>, which logs <code>WebSocket <host><path> (refused)</code> in the traffic log and immediately closes the socket with <code>socket.destroy()</code>.</li></ol>
|
||
<h4 id="ch-02-en-1-2-how-wtows-dll-intercepts-all-outbound-traffic-safely">1.2 How <code>wto_ws.dll</code> Intercepts All Outbound Traffic Safely</h4>
|
||
<p><code>patches/exe/online/network-hook-library/patch.jsonc</code> locates the 11-byte import descriptor name <code>"WS2_32.dll\0"</code> (<code>57 53 32 5f 33 32 2e 64 6c 6c 00</code>) in <code>aces.exe</code>'s <code>.rdata</code> section and overwrites it in place with <code>"wto_ws.dll\0"</code> (<code>77 74 6f 5f 77 73 2e 64 6c 6c 00</code>, also <strong>11 bytes</strong>):</p>
|
||
<ul><li>When Windows loads <code>aces.exe</code>, it loads <code>win64/wto_ws.dll</code> instead of <code>WS2_32.dll</code>. <code>native/wto-net/src/wto_ws.def</code> forwards every imported Winsock symbol (preserving exact <code>@ordinal</code> numbers for all 31 ordinal-imported functions) to the operating system's <code>ws2_32.dll</code>;</li><li>Inside <code>DllMain</code> (<code>native/wto-net/src/dll_main.c</code>), <code>wto_ws.dll</code> loads <code>wto_ws.cfg</code> and uses MinHook to hook the name-resolution, <code>connect</code>, and datagram functions inside the system's <code>ws2_32.dll</code> itself—so network calls from <strong>every module</strong> in the process pass through local redirection, not just calls made directly by <code>aces.exe</code>;</li><li><strong>Hard fail-closed guarantee</strong>: If <code>MH_Initialize</code>, <code>install_resolver_hooks()</code>, <code>install_connection_hooks()</code>, or <code>MH_ApplyQueued()</code> fails for any reason, <code>DllMain</code> logs the failure and <strong>returns <code>FALSE</code></strong>, causing the Windows loader to abort process startup immediately rather than risking an unhooked online connection.</li></ul>
|
||
<h4 id="ch-02-en-1-3-the-golden-priority-rule">1.3 The Golden Priority Rule</h4>
|
||
<blockquote><p><strong>Before touching any client file, always ask: "Can <code>apps/server</code> or stock mission/config fields solve this?"</strong><br>If a bug or feature can be implemented by returning the right server payload, populating a stock BLK field in the mission launch config, or extracting stock archive data, <strong>never add a <code>.nut</code>, <code>aces.exe</code>, or <code>wto_ws.dll</code> patch</strong>. Client patches are strictly a last resort when retail <code>aces.exe</code> or <code>gui.vromfs.bin</code> genuinely lacks the path—because minor game updates frequently shift binary code while wire protocols and BLK schemas remain stable across builds.</p></blockquote>
|
||
<hr>
|
||
<h3 id="ch-02-en-2-workspace-dependency-rule-real-apps-server-directory-map">2. Workspace Dependency Rule & Real <code>apps/server</code> Directory Map</h3>
|
||
<h4 id="ch-02-en-2-1-one-way-workspace-dependency-rule">2.1 One-Way Workspace Dependency Rule</h4>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>text</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>apps/launcher ──┐
|
||
├──▶ packages/* (@wto/blk · @wto/vromfs · @wto/jsonc · @wto/exepatch · @wto/patcher · @wto/mods)
|
||
apps/server ──┘</code></pre></div>
|
||
<ul><li><strong>Dependencies flow strictly downward</strong>: <code>apps/*</code> may import from <code>packages/*</code> (and developer CLI scripts under <code>scripts/*</code> may compose both), <strong>but <code>packages/*</code> must never import from, reference, or hardcode paths into <code>apps/*</code></strong>.</li><li>Whenever a shared package needs a patch directory, data folder, port number, or bundle list, pass it explicitly as a function parameter from the caller.</li></ul>
|
||
<h4 id="ch-02-en-2-2-complete-apps-server-src-four-layer-directory-map">2.2 Complete <code>apps/server/src/</code> Four-Layer Directory Map</h4>
|
||
<p>Note that <strong>there is NO monolithic <code>rules/</code> folder in <code>apps/server/src/</code></strong>—domain rules live in dedicated per-domain directories. Use this map to place every file in the right layer:</p>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th>Layer</th><th>Real directories under <code>apps/server/src/</code></th><th>What belongs here</th><th>What must NOT go here</th></tr></thead><tbody><tr><td><strong>1. Transport & Protocol</strong></td><td><code>transport/</code> (single-port listener, HTTP router, TLS cert), <code>traffic-log/</code> (JSONL + decoded body logger), <code>login/</code> (<code>yuplay2</code> sign-in & session tokens), <code>char/</code> (<code>char-service.ts</code>, <code>pbbd-stream.ts</code>, <code>char/actions/*-actions.ts</code>, and <code>char/actions/action-table.ts</code>), <code>matching/</code> (LZ4 framing, stream cipher, queue/battle RPC), <code>inventory/</code> (external inventory & RSA content signing), <code>store/</code> (bundle & redeem-code endpoints), <code>chat/</code> (IRC server), <code>cloud-settings/</code>, <code>downloadable-content/</code>, <code>web-ui/</code> (in-game store & market HTML/CSS), <code>mods/</code> (server mod runtime)</td><td>Decode wire payloads (HTML forms, JSON, marker-prefixed binary BLK, <code>PBBD</code> streams, LZ4 packets), validate request parameters, invoke domain rules, and serialize results back into stock wire formats</td><td>Do not write multi-hundred-line currency, research, or profile-mutation logic inside <code>*-actions.ts</code>; never read or write save files directly bypassing <code>PlayerProfiles</code> / <code>SaveStore</code></td></tr><tr><td><strong>2. Domain Rules</strong></td><td><code>battle/</code> (mission setup & post-battle economy settlement), <code>research/</code> (tech-tree state, target selection, free-RP conversion), <code>modifications/</code> (modules, ammo belts, spares, custom weapon presets), <code>crew/</code> (slots & skill progression), <code>items/</code> (boosters, wagers, discounts), <code>repair/</code> (battle damage & timed free repair), <code>clans/</code> (squadrons & squadron vehicle RP), <code>decor/</code> (skins, decals, 3D attachables), <code>unlocks/</code> (streaks, titles, progression unlocks), <code>player-card/</code> (pilot icons & showcases), <code>training/</code> (tutorial completion rewards), <code>game-modes/</code></td><td><strong>Pure domain logic</strong>: take an in-memory profile <code>BlkBlock</code>, loaded datasets, and validated tuning; verify preconditions; mutate or compute the next state; return an explicit <code>RuleOutcome</code> (<code>ACCEPTED</code> or <code>refused('CODE')</code>)</td><td><strong>Zero network or filesystem I/O</strong>: never import <code>node:http</code>, <code>node:net</code>, or <code>node:fs</code>, and keep domain logic agnostic of HTTP headers or sockets</td></tr><tr><td><strong>3. Profile & Persistence</strong></td><td><code>profile/</code> (<code>player-profiles.ts</code> plus focused <code>profile-*.ts</code> BLK accessors and derived-state refresh), <code>saves/</code> (<code>save-store.ts</code> & <code>save-upgrades.ts</code> <code>formatVersion</code>), <code>accounts/</code> (multi-account credentials & isolation), <code>storage/</code> (<code>atomic-file.ts</code> <code>writeFileAtomic</code>)</td><td>Encapsulate typed <code>BlkBlock</code> profile accessors, isolate per-account saves under <code><data>/saves/<userId>/</code>, run ordered <code>formatVersion</code> upgrades, and guarantee crash-safe writes via temp file + <code>fsync</code> + atomic rename</td><td>Do not embed shop pricing or research formulas here; <strong>every profile mutation must finish writing to disk before replying to the client</strong></td></tr><tr><td><strong>4. Datasets & Tuning</strong></td><td><code>game-data/</code> (<code>game-files.ts</code>, <code>game-pack.ts</code>, <code>datasets/*.ts</code>, <code>datasets/dataset-cache.ts</code>), shipped defaults in <code>apps/server/tuning/*.jsonc</code> + loader/validators in <code>src/tuning/</code> (<code>tuning-loader.ts</code>, <code>tuning-reader.ts</code>, <code>limits-tuning.ts</code>, <code>launcher-tuning.ts</code>) + per-domain <code><domain>/*-tuning.ts</code></td><td>Extract static facts from stock <code>.vromfs.bin</code> archives at startup (cached in <code><data>/cache/datasets/<id>.json</code> by pack size/mtime and inner file SHA-1 digests), and merge/validate JSONC tuning across shipped defaults -> enabled mods -> player <code><data>/tuning/</code> overrides</td><td>Never invent stock game numbers; if a required stock block or field is missing, throw an explicit error at startup instead of silently substituting a fake default</td></tr></tbody></table></div>
|
||
<p><strong>File granularity & reuse</strong>:</p>
|
||
<ul><li>When a file approaches <strong>300 lines</strong> or a function approaches <strong>50 lines</strong>, check whether wire parsing, BLK tree traversal, and domain rules have been mixed together and split them cleanly along the table above;</li><li>Reuse existing building blocks (<code>char/actions/rule-answer.ts</code>, <code>profile/profile-*.ts</code>, <code>modifications/equipment-catalogue.ts</code>, shared <code>web-ui/</code> helpers, and <code>local_mp_core</code> in C) instead of duplicating helpers.</li></ul>
|
||
<hr>
|
||
<h4 id="ch-02-en-2-3-end-to-end-walkthrough-of-one-real-action-clnsetresearchable">2.3 End-to-End Walkthrough of One Real Action: <code>cln_set_researchable</code></h4>
|
||
<p>To see how those four layers collaborate in practice, let's trace what happens when a player clicks <strong>"Research"</strong> on a vehicle in the tech tree and the client sends <strong><code>cln_set_researchable</code></strong> (use this exact pattern whenever you add a new profile-mutating <code>char</code> action):</p>
|
||
<ol><li><strong>Wire Arrival & Payload Decoding (<code>apps/server/src/char/char-service.ts</code>)</strong> The client sends an HTTP POST to <code>char-*.warthunder.com</code> with headers <code>action: cln_set_researchable</code> and <code>uidhint: <userId></code>, carrying a marker-prefixed binary BLK body (<code>unit:t = "us_m4a1_1942_sherman"</code>, <code>type:i = 0</code>). <code>CharService.#handleCore</code> calls <code>decodeRequestPayload(request.body)</code> (<code>char/char-payload.ts</code>) to decode the <code>BlkBlock</code> (and format readable BLK text for the traffic log), then looks up <code>'cln_set_researchable'</code> in <code>char/actions/action-table.ts</code> to invoke <code>setResearchable</code>.</li><li><strong>Protocol Adapter & Parameter Extraction (<code>apps/server/src/char/actions/research-actions.ts</code>)</strong> <code>setResearchable</code> is a thin 14-line adapter built on <code>ruleAction</code> (<code>char/actions/rule-answer.ts</code>). It extracts <code>unit</code> and <code>type</code> (or <code>mod</code> for module research) using <code>stringParam</code> and <code>intParam</code> (<code>char/actions/payload-params.ts</code>). Missing parameters return <code>undefined</code> (which <code>ruleAction</code> turns into <code>!ERROR:BAD_PARAMS</code>); valid parameters return a pure rule closure: <code>(profile) => chooseResearchTarget(profile, rules, vehicle, type)</code>.</li><li><strong>Profile Load & Pure Domain Rule (<code>apps/server/src/research/research-progress.ts</code>)</strong> After resolving the account via <code>withAccount</code>, <code>ruleAction</code> calls <code>profiles.updateWhen(account.userId, ...)</code> (<code>profile/player-profiles.ts</code>), which loads <code><data>/saves/<userId>/profile.blk</code> through <code>SaveStore.loadProfile</code> and passes the <code>BlkBlock</code> into <code>chooseResearchTarget</code>:
|
||
<ul><li>Looks up the vehicle in the startup-extracted tech-tree dataset via <code>rules.techTree.vehicle(vehicleName)</code> and verifies <code>vehicleTypeById(typeId) === vehicle.type</code>;</li><li>Verifies <code>currentStatus(profile, rules, vehicleName) === VehicleStatus.CAN_RESEARCH</code>. If the vehicle is locked or already owned, it returns <code>refused('NOT_AVAILABLE')</code> (<code>{ ok: false, refusal: 'NOT_AVAILABLE' }</code>)—causing <code>updateWhen</code> to return <code>undefined</code> <strong>without touching the disk</strong> and <code>ruleAction</code> to reply <code>!ERROR:NOT_AVAILABLE</code>;</li><li>Otherwise, updates the active target and clears any pending finished-research prompt via <code>profile/profile-research-targets.ts</code>, returning <code>ACCEPTED</code> (<code>{ ok: true }</code>).</li></ul></li><li><strong>Atomic Disk Persistence BEFORE Replying (<code>profile/player-profiles.ts</code> -> <code>saves/save-store.ts</code> -> <code>storage/atomic-file.ts</code>)</strong> When the closure accepts, <code>PlayerProfiles.updateWhen</code> runs <code>#refresh(profile, userId)</code> to bring derived profile state (unlocked ranks, statuses, clan sync, earned unlocks) up to date, then calls <code>SaveStore.saveProfile(userId, profile)</code>. <code>saveProfile</code> calls <code>writeFileAtomic</code> (<code>storage/atomic-file.ts</code>), which writes <code>profile.blk.<pid>.<rand>.tmp</code>, flushes hardware buffers via <code>fsyncSync(fd)</code>, and atomically <code>renameSync</code>s it over <code>profile.blk</code> (with Windows transient-lock retries) before updating <code>save.json</code>. <strong>Only after both atomic writes succeed</strong> does <code>ruleAction</code> project <code>clientProfile(profile, account)</code> and <code>toResponse</code> (<code>char/char-answers.ts</code>) send HTTP 200 (<code>0x69</code> uncompressed marker + binary BLK) back to <code>aces.exe</code>.</li></ol>
|
||
<h4 id="ch-02-en-2-4-save-schema-upgrades-saves-save-upgrades-ts">2.4 Save Schema Upgrades (<code>saves/save-upgrades.ts</code>)</h4>
|
||
<ul><li><strong>Domain rules always target only the current profile schema</strong>: never scatter ad-hoc legacy compatibility branches (<code>if (oldFieldExists) ...</code>) across <code>research/</code>, <code>modifications/</code>, or <code>battle/</code>.</li><li>When a release changes what <code>profile.blk</code> stores, append an ordered <code>SaveUpgrade</code> step to <code>SaveUpgrades</code> (<code>saves/save-upgrades.ts</code>, where <code>steps[n]</code> transforms a <code>formatVersion</code> <code>n + 1</code> profile into <code>n + 2</code> in place, bumping <code>currentFormat</code>). When <code>SaveStore.loadProfile</code> opens a save from an earlier release, it runs the missing steps in order and immediately persists the upgraded profile at <code>currentFormat</code>. Unreleased work-in-progress structures on a development branch do not need migration shims.</li></ul>
|
||
<hr>
|
||
<h3 id="ch-02-en-3-three-categories-of-values-keep-boundaries-clear">3. Three Categories of Values (Keep Boundaries Clear)</h3>
|
||
<p>Every number or constant in WTOffline belongs to exactly one of three categories:</p>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th>Category</th><th>Concrete examples</th><th>Where it MUST live</th><th>Anti-pattern to avoid</th></tr></thead><tbody><tr><td><strong>1. Protocol & engine constants</strong></td><td>Wire/MPI message IDs (<code>LMP_MPI_MSG_UNIT_RESPAWN = 0xf030u</code>), <code>PBBD</code> magic (<code>0x44424250</code>), Dagor container layouts (<code>dag::Vector</code> offsets, <code>FastNameMap</code> shifts), bitmasks</td><td>Named constants in the relevant protocol module or header, with an <strong>English comment citing the primary source</strong> (client version + RVA, or <a href="https://github.com/GaijinEntertainment/DagorEngine" target="_blank" rel="noopener">DagorEngine open-source</a> file path)</td><td>Bare magic numbers like <code>if (msgId == 61488)</code> or <code>if (st == 64)</code> with zero provenance comment</td></tr><tr><td><strong>2. Stock game facts</strong></td><td>Vehicle SL/GE prices, RP requirements, battle ratings, repair costs, modification trees, mission lists, tutorial first-win rewards, killstreak spawn altitude/distance/speed</td><td>Extracted from the player's read-only <code>.vromfs.bin</code> archives at startup via <code>apps/server/src/game-data/datasets/</code> (<code>Dataset<T></code>) and cached in <code><data>/cache/datasets/<id>.json</code> by <code>DatasetCache</code>. Throw an explicit <code>Error</code> if a required block/field is missing</td><td>Copying a table of vehicle costs, tutorial rewards, or spawn coordinates from one client build into TS/C code, or silently falling back to <code>?? 10000</code> when a lookup fails</td></tr><tr><td><strong>3. Offline tuning & defaults</strong></td><td>SL/RP reward multipliers, new-profile starting balances, local store bundle prices, supply-chest drop weights, AI bot respawn counts, request caps, gameplay toggles</td><td>Shipped defaults in <code>apps/server/tuning/<domain>.jsonc</code>, validated for type and bounds at startup via <code>TuningReader</code> in <code><domain>/*-tuning.ts</code>, and merged by <code>tuning-loader.ts</code> across shipped defaults -> enabled mod overrides -> player <code><data>/tuning/<domain>.jsonc</code> overrides</td><td>Hardcoding <code>const RP_MULTIPLIER = 3.0</code> or arbitrary caps inside a settlement function where players and mods cannot override them</td></tr></tbody></table></div>
|
||
<hr>
|
||
<h3 id="ch-02-en-4-good-vs-bad-code-4-real-codebase-comparisons">4. Good vs. Bad Code: 4 Real Codebase Comparisons</h3>
|
||
<p>Every "✅ Good" snippet below uses the exact symbols and files from this repository. Check your changes against all four before opening a PR.</p>
|
||
<h4 id="ch-02-en-comparison-1-native-c-hooks-glmp-dynamic-captures-scoped-tls-flags-vs-hardcoded-offsets-returnaddress">Comparison 1: Native C Hooks — <code>g_lmp</code> Dynamic Captures & Scoped TLS Flags vs. Hardcoded Offsets & <code>_ReturnAddress()</code></h4>
|
||
<p>Whenever War Thunder ships a minor patch (e.g., <code>2.61.0.37</code> -> <code>2.61.0.58</code>), function RVAs shift across <code>.text</code> and C++ class member offsets move whenever fields are added or reordered.</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>c</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// ❌ BAD: Hardcoded RVAs, hardcoded struct offset (+0x3798), and brittle _ReturnAddress() caller check.
|
||
// The very next minor game update will silently break the feature or crash the client!
|
||
static void __fastcall bad_hook_send_unit_respawn(uint8_t *unit, const void *spawn)
|
||
{
|
||
uintptr_t base = (uintptr_t)GetModuleHandleW(NULL);
|
||
if ((uintptr_t)_ReturnAddress() != base + 0x1281280)
|
||
return;
|
||
const uint8_t *gameplay = *(const uint8_t *const *)(base + 0x7bad2f0);
|
||
const uint8_t *rules = *(const uint8_t *const *)(gameplay + 0x3798);
|
||
/* ... */
|
||
}</code></pre></div>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>c</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// ✅ GOOD (Real code: native/wto-net/src/local_mp_air_events.c + local_mp_offsets.h + local_mp_values.h):
|
||
// 1. Every RVA and struct offset is dynamically captured in patches/native/local_mp_air_events.jsonc,
|
||
// written to wto_ws.cfg, and loaded into extern LmpValues g_lmp (LMP_GP_OFF_KS_RULES -> g_lmp.gp_ks_rules);
|
||
// 2. An outer detour + thread-local flag (__declspec(thread)) scopes the call cleanly without _ReturnAddress().
|
||
static SwitchPlayerUnitFn orig_switch_player_unit;
|
||
static __declspec(thread) int g_in_air_event_switch;
|
||
|
||
static void __fastcall hook_switch_player_unit(void *event_mgr, void *participant, uint8_t to_air)
|
||
{
|
||
int previous = g_in_air_event_switch;
|
||
|
||
if (to_air != 0 && ground_arcade_air_events_enabled())
|
||
g_in_air_event_switch = 1;
|
||
orig_switch_player_unit(event_mgr, participant, to_air);
|
||
g_in_air_event_switch = previous;
|
||
}
|
||
|
||
// Struct offsets come from g_lmp, surviving minor client updates automatically:
|
||
const uint8_t *gameplay = g_lmp.gameplay ? *(const uint8_t *const *)g_lmp.gameplay : NULL;
|
||
const uint8_t *rules = *(const uint8_t *const *)(gameplay + LMP_GP_OFF_KS_RULES);
|
||
uint32_t rule_count = *(const uint32_t *)(gameplay + LMP_GP_OFF_KS_RULE_COUNT);</code></pre></div>
|
||
<h4 id="ch-02-en-comparison-2-stock-game-data-datasetcache-extraction-pure-catalogue-vs-hardcoded-ts-maps">Comparison 2: Stock Game Data — <code>DatasetCache</code> Extraction + Pure Catalogue vs. Hardcoded TS Maps</h4>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>ts</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// ❌ BAD: Hardcoding tutorial mission rewards from one client build directly in server code
|
||
const TUTORIAL_REWARDS: Record<string, { silverLions: number; goldenEagles: number }> = {
|
||
'tutorialB_fighter': { silverLions: 5000, goldenEagles: 100 },
|
||
'tutorialB_assaulter': { silverLions: 5000, goldenEagles: 0 },
|
||
};</code></pre></div>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>ts</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// ✅ GOOD (Real code: apps/server/src/game-data/datasets/training-missions.ts
|
||
// + apps/server/src/training/training-completion.ts):
|
||
// 1. Define a versioned Dataset<T> that extracts stock data from char.vromfs.bin
|
||
// (config/mislist.blk, config/pve_awards.blk, config/unlocks.blk) and throws if required blocks are missing.
|
||
export const trainingMissions: Dataset<TrainingMissionsData> = {
|
||
id: 'training-missions',
|
||
version: 1,
|
||
extract(source: DatasetSource): TrainingMissionsData {
|
||
const misSection = blockAt(source.readBlk('char.vromfs.bin', 'config/mislist.blk'), 'training');
|
||
if (!misSection) throw new Error('config/mislist.blk has no "training" block');
|
||
const awardSection = blockAt(source.readBlk('char.vromfs.bin', 'config/pve_awards.blk'), 'training');
|
||
if (!awardSection) throw new Error('config/pve_awards.blk has no "training" block');
|
||
// ... build and return { missions }
|
||
return { missions };
|
||
},
|
||
};
|
||
|
||
// 2. Load through DatasetCache at startup and pass pure data into TrainingCatalogue:
|
||
const trainingCatalogue = new TrainingCatalogue(datasets.load(trainingMissions));
|
||
trainingCatalogue.applyCompletion(profile, { chapter, mission, difficulty });</code></pre></div>
|
||
<h4 id="ch-02-en-comparison-3-fixing-at-the-right-layer-toolchain-discipline-msvc-c-node-24-typescript">Comparison 3: Fixing at the Right Layer & Toolchain Discipline (MSVC C & Node 24 TypeScript)</h4>
|
||
<p><strong>(A) Check stock loader fields before writing a client patch</strong></p>
|
||
<ul><li>❌ <strong>Bad instinct</strong>: Notice AI bots only respawn once in multi-slot Air Arcade battles and immediately start reversing <code>BotSystem</code> to write an <code>aces.exe</code> memory patch.</li><li>✅ <strong>Good engineering</strong>: Inspect <code>MissionSettings::load</code> first and discover that the stock client already reads <code>numBotRespawns</code> and <code>numTankBotRespawns</code> from the mission root <code>DataBlock</code>—the local server simply omitted those two root parameters when assembling multi-slot missions. Adding them in <code>apps/server</code> fixes the issue in 5 lines with <strong>zero client patches</strong> and permanent cross-version stability.</li></ul>
|
||
<p><strong>(B) MSVC C rules & Node 24 native TypeScript rules</strong></p>
|
||
<ul><li><strong>Native C (<code>native/wto-net/</code>)</strong>: Compiled by Visual Studio <code>cl.exe</code> via <code>native/wto-net/build.ps1</code> with <code>/W4</code> and <strong>zero warnings</strong>. Verify stack/wire struct layouts at compile time with <code>C_ASSERT(sizeof(LocalMpAirSpawn) == 0x48);</code>, use <code>__declspec(thread)</code> for TLS and <code>__declspec(align(16))</code> for alignment, and <strong>never use GCC/Clang extensions</strong> (<code>__attribute__</code>, VLAs, zero-length arrays, or <code>void *</code> pointer arithmetic).</li><li><strong>TypeScript (<code>tsconfig.json</code>)</strong>: Executed directly by Node.js 24 native type-stripping with <code>erasableSyntaxOnly: true</code>, <code>verbatimModuleSyntax: true</code>, <code>allowImportingTsExtensions: true</code>, and <code>exactOptionalPropertyTypes: true</code>:</li></ul>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>ts</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// ❌ BAD: Extensionless relative import, value-importing a type, using `enum`, and constructor parameter properties
|
||
import { SaveStore, SaveUpgrades } from '../saves/save-store';
|
||
export enum ResearchState { Locked = 0, Available = 1 }
|
||
export class Handler {
|
||
constructor(private readonly saves: SaveStore) {}
|
||
}</code></pre></div>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>ts</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// ✅ GOOD: Explicit `.ts` extension on all relative imports, `import type` for types, `as const` objects instead of `enum`, and `#private` fields
|
||
import { decodeBlk, encodeBlk, type BlkBlock } from '@wto/blk';
|
||
import { writeFileAtomic } from '../storage/atomic-file.ts';
|
||
import type { SaveUpgrades } from './save-upgrades.ts';
|
||
|
||
export const VehicleStatus = {
|
||
CAN_RESEARCH: 2,
|
||
OWNED: 4,
|
||
} as const;</code></pre></div>
|
||
<h4 id="ch-02-en-comparison-4-real-verification-vs-mock-test-suites">Comparison 4: Real Verification vs. Mock Test Suites</h4>
|
||
<ul><li>❌ <strong>Bad practice</strong>: Editing <code>patches/gui/</code> and only eyeballing indentation—or writing 300 lines of mock unit tests that simulate fake server/client calls—and declaring the change "fully tested", only for login to hang forever on a Quirrel <code>from ... import *</code> scope collision or <code>loadGuiPatches</code> to throw <code>DefinitionError</code> on an unknown JSONC key.</li><li>✅ <strong>Good practice</strong>: Run the real verification tools matching your change (<code>npm run typecheck</code>, <code>wtotools check-patches</code>, full <code>wtotools check-gui</code> with <code>sq.exe</code>, MSVC <code>native\wto-net\build.ps1</code>, <code>scripts/build-game-copy.ts</code>), and <strong>honestly separate executed CLI checks from pending in-game acceptance steps</strong> in your PR description.</li></ul>
|
||
<hr>
|
||
<h3 id="ch-02-en-5-working-effectively-with-ai-coding-assistants-copy-paste-prompt-block">5. Working Effectively with AI Coding Assistants (Copy-Paste Prompt Block)</h3>
|
||
<p>Using AI assistants (Claude Code, Codex, Cursor, etc.) to read disassembly, trace <code>.nut</code> UI call chains, inspect BLK schemas, and pinpoint protocol mismatches is warmly welcomed.</p>
|
||
<p>However, without explicit project context, AI tools frequently invent hardcoded RVAs, add an invalid <code>requires</code> field to <code>patches/gui/**/patch.jsonc</code>, omit <code>.ts</code> import extensions, write <code>enum</code> declarations, or generate bloated mock test suites. <strong>Paste the constraint block below into your AI session before asking it to write or edit code</strong>:</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>text</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>I am preparing a change for the WTOffline repository. Before writing any code, always read the real source files to verify symbols, struct fields, and CLI flags, and follow these engineering rules strictly:
|
||
|
||
1. Layer priority: Ground every change in primary sources (read-only stock .vromfs.bin archives, gui.vromfs.bin .nut scripts, DagorEngine open-source code). Solve issues in apps/server protocol answers, mission BLK fields, or stock datasets before adding any client patch under patches/gui/, patches/exe/, or native/wto-net/.
|
||
2. Zero hardcoded addresses or offsets: Never hardcode function RVAs, global addresses, C++ struct offsets, or _ReturnAddress() caller checks in C or TS. Capture all native symbols and offsets dynamically via patches/exe/ or patches/native/*.jsonc (signature / string_ref / within) into wto_ws.cfg, read them in C exclusively through `extern LmpValues g_lmp` (local_mp_values.h / local_mp_offsets.h), and replace caller checks with an outer hook + `__declspec(thread)` flag. When capturing a `rel32` from an instruction that has trailing immediate bytes after disp32, always pass `"end"` set to the full instruction byte length.
|
||
3. Three value categories: Extract stock game numbers (vehicle prices, RP, rewards, missions, spawn params) from stock archives via `apps/server/src/game-data/datasets/` (`DatasetCache`), throwing explicitly if required fields are missing. Put offline defaults/multipliers/toggles in `apps/server/tuning/*.jsonc` validated via `*-tuning.ts`. Keep protocol/engine constants in code with English comments citing the client build RVA or DagorEngine source path.
|
||
4. Layer & dependency rules: `packages/*` must never import or read from `apps/*`. Inside `apps/server/src/`, keep transport/protocol (`char/actions/`, etc.), pure domain rules (`research/`, `battle/`, `modifications/`, etc. — note there is NO `rules/` folder; zero network/disk I/O in domain modules), and profile/persistence (`profile/`, `saves/`, `storage/atomic-file.ts` — always persist atomically to disk BEFORE replying) cleanly separated. Handle save schema evolution only via `formatVersion` steps in `saves/save-upgrades.ts`, never ad-hoc legacy branches in domain rules. Split files near 300 lines or functions near 50 lines.
|
||
5. Strict toolchain & schema rules:
|
||
- TypeScript runs on Node 24 native type-stripping (`erasableSyntaxOnly`, `verbatimModuleSyntax`, `allowImportingTsExtensions`): include explicit `.ts` extensions on all relative imports, use `import type` for type-only imports, and never use `enum`, `namespace`, or constructor parameter properties.
|
||
- Native C must compile cleanly with MSVC `native/wto-net/build.ps1` (`/W4`, zero warnings), using `C_ASSERT` for layout checks and zero GCC extensions; register any new `.c` file in `build.ps1`'s `$sources` list.
|
||
- Patch JSONC files reject unknown keys: in particular, `patches/gui/**/patch.jsonc` allows ONLY `id`, `title`, `features`, and `variants` at top level (adding `requires` throws `DefinitionError`!).
|
||
6. Real verification: Do not add large mock unit test suites. Run the applicable real checks (`npm run typecheck`, `wtotools check-patches`, full `wtotools check-gui` with `--sq`, `native/wto-net/build.ps1`, `scripts/build-game-copy.ts`) and clearly separate executed CLI checks from pending isolated in-game acceptance steps.
|
||
7. PR hygiene: Keep one focused goal per PR; write code comments, patch `title`/3-line header comments, and conventional commit messages in English; never list AI tools in `Co-authored-by`; and never commit `patches/versions/*.jsonc` before live in-game verification in an isolated copy.</code></pre></div>
|
||
<hr>
|
||
<h3 id="ch-02-en-6-isolation-asset-boundaries-non-negotiable">6. Isolation & Asset Boundaries (Non-Negotiable)</h3>
|
||
<ol><li><strong>Keep the official stock installation strictly read-only</strong>: Unpacking archives, running <code>tools/re/cli.py</code>, checking patches with <code>wtotools</code>, and building game copies must only ever read the stock client folder—<strong>never write, modify, or drop files inside a Steam or official launcher installation</strong>.</li><li><strong>Test exclusively in an isolated offline copy</strong>: Run the patched client only from a separate directory outside your Steam library, with all three <code>ESSENTIAL_FEATURES</code> safety locks active—all network calls redirected to <code>127.0.0.1</code> by <code>wto_ws.dll</code>, user files isolated under <code>WTOffline</code>, registry state under <code>Software\WTOffline</code>, anti-cheat and crash reporting disabled, and signed in only with a local offline account.</li><li><strong>Zero copyrighted assets or private data in Git</strong>: Never commit <code>.vromfs.bin</code> packs, <code>aces.exe</code>, full extracted stock <code>.nut</code> / <code>.blk</code> files, personal player saves, machine-specific paths, or unredacted logs to the repository.</li></ol>
|
||
</div>
|
||
<div class="doc-pager">
|
||
<a class="doc-pager-btn" href="#ch-01" data-chapter="ch-01"><span class="doc-pager-label"><span class="lang-zh">← 上一章</span><span class="lang-en">← Previous</span></span><span class="doc-pager-title"><span class="lang-zh">01 · 导览、术语词典与入口地图</span><span class="lang-en">01 · Overview, Glossary & Map</span></span></a>
|
||
<a class="doc-pager-btn next" href="#ch-03" data-chapter="ch-03"><span class="doc-pager-label"><span class="lang-zh">下一章 →</span><span class="lang-en">Next →</span></span><span class="doc-pager-title"><span class="lang-zh">03 · 补丁编写与逐字节真实拆解</span><span class="lang-en">03 · Patch Authoring & Byte Anatomy</span></span></a>
|
||
</div>
|
||
</article>
|
||
|
||
<article class="doc-section doc-chapter" id="ch-03">
|
||
<div class="lang-zh">
|
||
<h2>03 · 补丁编写、逐字节拆解与 Native 完整接线</h2>
|
||
<p>这篇手册面向需要修改客户端(Quirrel 界面脚本、<code>aces.exe</code> 二进制指令、或 <code>wto_ws.dll</code> 局内 <code>LocalMP</code> 原生裁判模块)以及协助适配游戏新版本的开发者。</p>
|
||
<blockquote><p><strong>动手前的第一道自检</strong>:请先阅读 <a href="#ch-02">开发规范第 1 节</a>,确认问题<strong>确实无法</strong>通过修改 <code>apps/server</code> 协议响应或下发原版任务/配置 BLK 字段解决。只有原版客户端真正缺失对应通路时,才编写客户端补丁。</p></blockquote>
|
||
<hr>
|
||
<h3 id="ch-03-zh-1-第-0-步-只读拆包-查看二进制-blk-反汇编定位与选型">1. 第 0 步:只读拆包、查看二进制 BLK、反汇编定位与选型</h3>
|
||
<p>请将原版客户端目录视为只读,不要直接修改其中的文件。利用仓库自带的 <code>@wto/vromfs</code>、<code>@wto/blk</code> 和 Python 静态分析工具 <code>tools/re/cli.py</code>,你可以在内存中直接查看原版脚本、配置与反汇编代码。</p>
|
||
<h4 id="ch-03-zh-1-1-一行命令读取原版-vromfs-bin-中的-nut-脚本或解码二进制-blk">1.1 一行命令读取原版 <code>.vromfs.bin</code> 中的 <code>.nut</code> 脚本或解码二进制 <code>.blk</code></h4>
|
||
<p>无需安装第三方解包器,在仓库根目录运行下面这段 Node.js 脚本(把 <code><原版目录></code> 换成你的官方客户端路径,只读不写):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code># 示例 A:列出 gui.vromfs.bin 中匹配关键字的脚本路径,并直接打印某个 .nut 原文
|
||
node --input-type=module -e "
|
||
import { readFileSync } from 'node:fs';
|
||
import { getFile, readArchive } from '@wto/vromfs';
|
||
const archive = readArchive(readFileSync(String.raw`<原版目录>\gui.vromfs.bin`));
|
||
console.log([...archive.files.keys()].filter((p) => p.includes('tutorial')));
|
||
console.log(Buffer.from(getFile(archive, 'scripts/tutorials/nexttutorialhandler.nut')).toString('utf8'));
|
||
"
|
||
|
||
# 示例 B:读取 char.vromfs.bin(或 aces.vromfs.bin)里的紧凑二进制 .blk 并格式化为可读文本
|
||
# (自动加载包内共享键名表 nm 与可选的 .dict 压缩字典)
|
||
node --input-type=module -e "
|
||
import { readFileSync } from 'node:fs';
|
||
import { decodeBlk, formatBlkText, parseSharedNameMap } from '@wto/blk';
|
||
import { getFile, NAME_MAP_PATH, readArchive } from '@wto/vromfs';
|
||
const archive = readArchive(readFileSync(String.raw`<原版目录>\char.vromfs.bin`));
|
||
const dictionary = [...archive.files.entries()].find(([p]) => p.endsWith('.dict'))?.[1];
|
||
const names = archive.files.has(NAME_MAP_PATH) ? parseSharedNameMap(getFile(archive, NAME_MAP_PATH), dictionary).names : [];
|
||
const blk = decodeBlk(getFile(archive, 'config/pve_awards.blk'), { names, dictionary });
|
||
console.log(formatBlkText(blk).slice(0, 2000));
|
||
"</code></pre></div>
|
||
<h4 id="ch-03-zh-1-2-使用-tools-re-cli-py-对-aces-exe-做只读静态逆向定位">1.2 使用 <code>tools/re/cli.py</code> 对 <code>aces.exe</code> 做只读静态逆向定位</h4>
|
||
<p>先按 <a href="#ch-04">环境准备</a> 初始化 <code>.venv</code>(<code>pip install -e ./tools</code>)。随后即可直接在命令行查字符串交叉引用、查调用者、反汇编函数(<strong>注意:所有地址参数均为十六进制 RVA,且务必显式传入 <code>--exe "<原版目录>\win64\aces.exe"</code></strong>):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code># 1. 查找固定英文字符串在 .rdata 中的 RVA,以及所有通过 rip-relative lea 引用它的代码位置与所属函数
|
||
.\.venv\Scripts\python.exe tools\re\cli.py find-string "customScoreParams" --exe "<原版目录>\win64\aces.exe"
|
||
|
||
# 2. 查找所有以 rip-relative 方式读写某个全局变量 RVA 的指令
|
||
.\.venv\Scripts\python.exe tools\re\cli.py xref 0x7bad2f0 --exe "<原版目录>\win64\aces.exe"
|
||
|
||
# 3. 查找所有直接 call 或 jmp 到目标函数 RVA 的调用点
|
||
.\.venv\Scripts\python.exe tools\re\cli.py callers 0x5445270 --exe "<原版目录>\win64\aces.exe"
|
||
|
||
# 4. 反汇编指定 RVA(默认打印 40 条指令,带字符串和调用注释;可传 --count、--end 或 --function 反汇编整函数)
|
||
.\.venv\Scripts\python.exe tools\re\cli.py disasm 0x11b2c14 --count 20 --exe "<原版目录>\win64\aces.exe"
|
||
.\.venv\Scripts\python.exe tools\re\cli.py disasm 0x126d762 --function --exe "<原版目录>\win64\aces.exe"</code></pre></div>
|
||
<h4 id="ch-03-zh-1-3-选对客户端修改方式与通用编写规范">1.3 选对客户端修改方式与通用编写规范</h4>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th>你要改什么</th><th>代码放哪里</th><th>真实参考例子</th></tr></thead><tbody><tr><td>修改机库/弹窗/结算界面脚本(Quirrel <code>.nut</code>)</td><td><code>patches/gui/<领域>/<名称>/</code></td><td><a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/patches/gui/tutorials/immediate-save-skip-tutorial/patch.jsonc" target="_blank" rel="noopener"><code>patches/gui/tutorials/immediate-save-skip-tutorial/</code></a></td></tr><tr><td>改写 <code>aces.exe</code> 内寥寥几条跳转/比较指令或 <code>.rdata</code> 固定字符串</td><td><code>patches/exe/<领域>/<名称>/</code></td><td><a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/patches/exe/battle/allow-local-host-bot-spawns/patch.jsonc" target="_blank" rel="noopener"><code>patches/exe/battle/allow-local-host-bot-spawns/</code></a></td></tr><tr><td>用 C 语言写 MinHook 钩子补齐 <code>LocalMP</code> 局内裁判逻辑(<code>wto_ws.dll</code>)</td><td><code>patches/native/<模块>.jsonc</code> + <code>native/wto-net/src/<模块>.c</code></td><td><a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/patches/native/local_mp_scores.jsonc" target="_blank" rel="noopener"><code>patches/native/local_mp_scores.jsonc</code></a></td></tr><tr><td>改写 <code>network.blk</code>、<code>shop.blk</code>、<code>gui.blk</code> 等数据包静态配置</td><td><code>packages/patcher/src/</code>(<strong>不放 <code>patches/</code></strong>)</td><td><code>packages/patcher/src/char-pack.ts</code>、<code>main-pack.ts</code></td></tr></tbody></table></div>
|
||
<p><strong>所有 <code>patches/</code> 定义共享的四条基本约定</strong>:</p>
|
||
<ol><li><strong>一处独立行为对应一个目录/模块</strong>,不在同一个补丁里顺手塞两件不相关的改动;</li><li><strong><code>patch.jsonc</code> / <code><模块>.jsonc</code> 顶部必须写三行英文注释</strong>:
|
||
<ul><li>第 1 行:这个补丁具体改了什么;</li><li>第 2 行(<code>Why the client:</code> / <code>Why the exe:</code>):为什么本地服务器或原版配置解决不了;</li><li>第 3 行(<code>Without it:</code>):如果不打这个补丁,玩家会遇到什么具体现象。</li></ul></li><li><strong><code>title</code> 与 <code>features</code> 统一写英文</strong>(注:仓库内个别早期补丁保留了中文 <code>title</code> 历史遗留,<strong>所有新增补丁请一律使用清晰的英文 <code>title</code></strong>),并优先复用现有的 <code>A-</code>..<code>K-</code> 功能标签(见 <a href="#ch-01">核心概念词典第 7 节</a>);</li><li><strong>唯一命中校验</strong>:无论多少个 <code>variants</code>,对任意一个客户端版本<strong>必须且只能有恰好 1 个 <code>variant</code> 命中(且单次定位全局唯一命中 1 处)</strong>;0 命中或多处命中都会被补丁引擎安全拒绝,避免误写。</li></ol>
|
||
<hr>
|
||
<h3 id="ch-03-zh-2-gui-补丁-patches-gui-完整三文件实例与严格-schema-校验">2. GUI 补丁(<code>patches/gui/</code>)—— 完整三文件实例与严格 Schema 校验</h3>
|
||
<h4 id="ch-03-zh-2-1-看一个完整的真实-gui-补丁-tutorials-immediate-save-skip-tutorial">2.1 看一个完整的真实 GUI 补丁:<code>tutorials/immediate-save-skip-tutorial</code></h4>
|
||
<p>在原版 <code>scripts/tutorials/nexttutorialhandler.nut</code> 中,当玩家弹出「是否参加新手教程」询问框并勾选「不再询问」时,脚本调用 <code>saveLocalByAccount(skipTutorialBitmaskId, skipTutorial)</code> 时没传第 3 个参数,默认走延迟 60 秒防抖的 <code>saveProfile()</code>——如果玩家勾选后在 60 秒内重启或崩溃退出,设置根本没发到服务端落盘,下次进机库弹窗又会冒出来。服务端无法替客户端缩短前端自己的 60 秒定时器,因此需要补上第 3 个回调参数 <code>@() save_profile(false)</code> 立即保存。</p>
|
||
<p>该补丁目录 <code>patches/gui/tutorials/immediate-save-skip-tutorial/</code> 下共有 <strong>3 个文件</strong>,完整内容如下:</p>
|
||
<p><strong>① <code>patch.jsonc</code></strong>(注:此早期补丁保留了中文 <code>title</code>,你编写新补丁时请将 <code>title</code> 写为英文,如 <code>"Save tutorial opt-out immediately after ticking 'do not ask again'"</code>):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>jsonc</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// Save the tutorial opt-out immediately via save_profile(false).
|
||
// Why the client: the stock callback waits 60 s before writing or sending anything to the service.
|
||
// Without it: a crash within 60 s of ticking "do not ask again" brings the tutorial prompt back.
|
||
{
|
||
"id": "tutorials/immediate-save-skip-tutorial",
|
||
"title": "新手教程询问窗勾选「不再询问」后立即落盘保存",
|
||
"features": [
|
||
"A-Tutorials"
|
||
],
|
||
"variants": [
|
||
{
|
||
"written_for": "2.61.0.37",
|
||
"file": "scripts/tutorials/nexttutorialhandler.nut",
|
||
"edits": [
|
||
{
|
||
"find": "find.nut",
|
||
"replace": "replace.nut"
|
||
}
|
||
]
|
||
}
|
||
]
|
||
}</code></pre></div>
|
||
<p><strong>② <code>find.nut</code></strong>(原版脚本中<strong>最小且在目标文件内唯一出现 1 次</strong>的原文,保留原始 4 空格缩进):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>squirrel</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code> skipTutorial = stdMath.change_bit(skipTutorial, this.checkIdx, obj.getValue())
|
||
saveLocalByAccount(skipTutorialBitmaskId, skipTutorial)
|
||
}</code></pre></div>
|
||
<p><strong>③ <code>replace.nut</code></strong>(替换后的脚本片段):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>squirrel</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code> skipTutorial = stdMath.change_bit(skipTutorial, this.checkIdx, obj.getValue())
|
||
saveLocalByAccount(skipTutorialBitmaskId, skipTutorial, @() save_profile(false))
|
||
}</code></pre></div>
|
||
<h4 id="ch-03-zh-2-2-gui-patch-definition-ts-严格-schema-白名单与编写细节-极易踩坑">2.2 <code>gui-patch-definition.ts</code> 严格 Schema 白名单与编写细节(极易踩坑!)</h4>
|
||
<p><code>packages/patcher/src/gui-patch-definition.ts</code> 会调用 <code>rejectUnknownKeys</code> 对每一层字段做严格白名单检查,<strong>多写任何一个未定义字段都会直接抛出 <code>DefinitionError</code></strong>:</p>
|
||
<ul><li><strong>顶层只允许 4 个键</strong>:<code>"id"</code>、<code>"title"</code>、<code>"features"</code>、<code>"variants"</code>。
|
||
<blockquote><p><strong>特别警告:GUI 的 <code>patch.jsonc</code> 没有 <code>"requires"</code> 字段!</strong> 不要把 EXE 补丁里的 <code>"requires": []</code> 复制到 <code>patches/gui/</code> 里,否则加载器会当场抛错拒绝构建!</p></blockquote></li><li><strong><code>variants[]</code> 元素只允许 3 个键</strong>:<code>"written_for"</code>、<code>"file"</code>、<code>"edits"</code>。其中 <code>"file"</code> 必须是 <code>gui.vromfs.bin</code> 内部<strong>全小写、以 <code>/</code> 分隔且不带前导 <code>/</code> 的相对路径</strong>(例如 <code>"scripts/tutorials/nexttutorialhandler.nut"</code>,注意原版归档内文件名全被转为小写!)。</li><li><strong><code>edits[]</code> 元素只允许 2 个键</strong>:<code>"find"</code>、<code>"replace"</code>,值必须是<strong>同一补丁目录下的纯文件名</strong>(不允许包含 <code>/</code> 或 <code>\</code> 子路径)。</li><li><strong>换行与唯一匹配规则</strong>:读取 <code>.nut</code> 片段时会自动剥离 UTF-8 BOM、将 <code>\r\n</code> 归一化为 <code>\n</code>,并去掉文件末尾的<strong>单个终止换行符</strong>(因此 Git 自动换行转换不会破坏匹配);<code>find.nut</code> 的内容在目标脚本里必须<strong>精确出现 1 次</strong>(0 次或 ≥2 次均报错)。</li><li><strong>本地服务地址占位符</strong>:如果 <code>replace.nut</code> 里需要拼出本地服务器地址,直接写 <code>{{LOCAL_ENDPOINT}}</code>,构建副本时 <code>gui-pack.ts</code> 会自动替换成 <code>127.0.0.1:<实际端口></code>。</li><li><strong>跨版本演进</strong>:如果新版本游戏修改了附近代码导致 <code>find.nut</code> 不再命中,<strong>切勿直接覆盖旧片段破坏老版本兼容性</strong>——新建 <code>find-58.nut</code> 与 <code>replace-58.nut</code>,在 <code>variants</code> 数组里追加一条 <code>"written_for": "<新版本号>"</code> 的记录即可。</li><li><strong>必跑 <code>sq.exe</code> 字节码检查</strong>:只要改过任何 <code>patches/gui/</code> 文件,提交前<strong>必须运行</strong>:
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>.\.venv\Scripts\python.exe -m wtotools.cli check-gui --gui "<原版目录>\gui.vromfs.bin" --sq "build\quirrel-cli\bin\Release\sq.exe"</code></pre></div>
|
||
<p>它会把补丁打在内存里的原版脚本上并调用 <code>sq.exe</code> 执行 <code>compilestring</code> 字节码编译,精准拦截与顶部 <code>from ... import *</code> 词法重名、未绑定函数前向引用以及误用 <code>base</code> 等保留字导致的进机库无限卡加载 Bug。</p></li></ul>
|
||
<hr>
|
||
<h3 id="ch-03-zh-3-exe-补丁-patches-exe-57-字节完整逐指令解剖与进阶参考">3. EXE 补丁(<code>patches/exe/</code>)—— 57 字节完整逐指令解剖与进阶参考</h3>
|
||
<p>当需要改动 <code>aces.exe</code> 内部某条分支判断或常量字符串时,我们使用 <code>@wto/exepatch</code> 的声明式补丁格式,做到<strong>零硬编码地址、跨小版本自动重定位</strong>。</p>
|
||
<h4 id="ch-03-zh-3-1-完整无省略实例-patches-exe-battle-allow-local-host-bot-spawns-patch-jsonc">3.1 完整无省略实例:<code>patches/exe/battle/allow-local-host-bot-spawns/patch.jsonc</code></h4>
|
||
<p>在 <code>LocalMP</code> 对局中,原版 <code>BotSystem::changeUnit</code> 在通过出生点检查后,会检查全局变量 <code>g_multiplayer</code> 是否为空(<code>cmp qword ptr [rip + disp32], 0</code>):单机试驾时 <code>g_multiplayer == NULL</code>,直接 <code>jz</code> 跳去调用 <code>Unit::changeAircraft</code> 刷出 AI;一旦处于 <code>LocalMP</code> 多人会话(<code>g_multiplayer != NULL</code>),零售客户端因为裁掉了专用服务器的脚本刷怪许可,落入后续分支只会打印 <code>"spawn denied by scripts"</code>,导致整场对局一辆 AI 载具都刷不出来。</p>
|
||
<p>下面是仓库中 <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/patches/exe/battle/allow-local-host-bot-spawns/patch.jsonc" target="_blank" rel="noopener"><code>patches/exe/battle/allow-local-host-bot-spawns/patch.jsonc</code></a> 的<strong>一字不差完整原文</strong>(<code>pattern</code> 共 <strong>57 字节</strong>,无任何省略号):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>jsonc</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// Let BotSystem::changeUnit spawn the AI players' vehicles on a LocalMP host (0x11b2c3a: the g_multiplayer
|
||
// null guard becomes a jump to the Unit::changeAircraft call at 0x11b314e).
|
||
// Why the exe: the build drops the dedicated server's script spawn permission, so with a multiplayer
|
||
// session the function only logs "spawn denied by scripts"; non-host sessions already return at its start.
|
||
// Without it: no AI vehicle ever spawns in a LocalMP battle.
|
||
{
|
||
"id": "battle/allow-local-host-bot-spawns",
|
||
"title": "Let BotSystem spawn AI vehicles on a LocalMP host",
|
||
"features": [
|
||
"K-AI opponents and teammates"
|
||
],
|
||
"requires": [],
|
||
"variants": [
|
||
// Masked signature of BotSystem::changeUnit from the spawn point check (call 0x111a010)
|
||
// to the g_multiplayer null guard, 0x3c bytes; rip-relative and branch targets are wildcards.
|
||
// 2.61.0.37: unique hit at 0x11b2c14, edited instruction at +38 (0x11b2c3a).
|
||
// spawn_call: where the null guard's jz goes (the Unit::changeAircraft call); the edit jumps there
|
||
// unconditionally. The guard's rip displacement differs between builds, so it is a wildcard.
|
||
{
|
||
"written_for": "2.61.0.37",
|
||
"locate": {
|
||
"method": "signature",
|
||
"section": ".text",
|
||
"pattern": "48 8b 05 ?? ?? ?? ?? 48 8b 88 e0 03 00 00 48 8b 5c 24 20 48 89 da 4d 89 f0 e8 ?? ?? ?? ?? 84 c0 0f 84 ?? ?? ?? ?? 48 83 3d ?? ?? ?? ?? 00 0f 84 ?? ?? ?? ?? 48 8b 54 24 20",
|
||
"captures": {
|
||
"spawn_call": {
|
||
"offset": 48,
|
||
"type": "rel32"
|
||
}
|
||
}
|
||
},
|
||
"edit": {
|
||
"offset": 38,
|
||
"asm": [
|
||
"jmp {spawn_call}",
|
||
"nop",
|
||
"nop",
|
||
"nop",
|
||
"nop",
|
||
"nop",
|
||
"nop",
|
||
"nop",
|
||
"nop",
|
||
"nop"
|
||
],
|
||
"bytes": "e9 ?? ?? ?? ?? 90 90 90 90 90 90 90 90 90",
|
||
"expect_original": "48 83 3d ?? ?? ?? ?? 00 0f 84 ?? ?? ?? ??",
|
||
"fixups": [
|
||
{
|
||
"at": 1,
|
||
"type": "rel32",
|
||
"target": "spawn_call"
|
||
}
|
||
]
|
||
}
|
||
}
|
||
]
|
||
}</code></pre></div>
|
||
<h4 id="ch-03-zh-3-2-0-56-全部-57-字节逐指令拆解表">3.2 <code>+0..+56</code> 全部 57 字节逐指令拆解表</h4>
|
||
<p>为什么 <code>edit.offset</code> 是 <code>38</code>?为什么 <code>spawn_call</code> 的 <code>offset</code> 是 <code>48</code>?为什么 <code>fixups[0].at</code> 又是 <code>1</code>?把这 57 个字节按 x86-64 指令逐条摊开,一眼就彻底明白了(以 <code>2.61.0.37</code> 命中起点 <code>site = 0x11b2c14</code> 为例):</p>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th>相对 <code>site</code> 偏移</th><th>长度</th><th><code>pattern</code> 中的十六进制字节</th><th>对应的 x86-64 反汇编指令</th><th>在补丁中扮演的角色</th></tr></thead><tbody><tr><td><code>+0 .. +6</code></td><td>7 B</td><td><code>48 8b 05 ?? ?? ?? ??</code></td><td><code>mov rax, qword ptr [rip + disp32]</code></td><td>前置上下文锚点;<code>??</code> 通配随版本变化的全局变量 RIP 位移</td></tr><tr><td><code>+7 .. +13</code></td><td>7 B</td><td><code>48 8b 88 e0 03 00 00</code></td><td><code>mov rcx, qword ptr [rax + 0x3e0]</code></td><td>前置上下文锚点</td></tr><tr><td><code>+14 .. +18</code></td><td>5 B</td><td><code>48 8b 5c 24 20</code></td><td><code>mov rbx, qword ptr [rsp + 0x20]</code></td><td>前置上下文锚点</td></tr><tr><td><code>+19 .. +21</code></td><td>3 B</td><td><code>48 89 da</code></td><td><code>mov rdx, rbx</code></td><td>前置上下文锚点</td></tr><tr><td><code>+22 .. +24</code></td><td>3 B</td><td><code>4d 89 f0</code></td><td><code>mov r8, r14</code></td><td>前置上下文锚点</td></tr><tr><td><code>+25 .. +29</code></td><td>5 B</td><td><code>e8 ?? ?? ?? ??</code></td><td><code>call rel32</code>(出生点检查函数)</td><td>前置上下文锚点;<code>??</code> 通配被调函数相对距离</td></tr><tr><td><code>+30 .. +31</code></td><td>2 B</td><td><code>84 c0</code></td><td><code>test al, al</code></td><td>前置上下文锚点</td></tr><tr><td><code>+32 .. +37</code></td><td>6 B</td><td><code>0f 84 ?? ?? ?? ??</code></td><td><code>jz rel32</code>(无可用出生点则提前返回)</td><td>前置上下文锚点;确保我们不会覆盖到前面的合法返回分支</td></tr><tr><td><strong><code>+38 .. +45</code></strong></td><td><strong>8 B</strong></td><td><code>48 83 3d ?? ?? ?? ?? 00</code></td><td><code>cmp qword ptr [rip + disp32], 0</code></td><td><strong>被改写区第 1 条指令(起点 <code>edit.offset = 38</code>)</strong>:原版 <code>g_multiplayer == 0</code> 判空检查</td></tr><tr><td><strong><code>+46 .. +51</code></strong></td><td><strong>6 B</strong></td><td><code>0f 84 ?? ?? ?? ??</code></td><td><code>jz rel32</code>(跳向 <code>Unit::changeAircraft</code>)</td><td><strong>被改写区第 2 条指令</strong>:操作码 <code>0f 84</code> 占 <code>+46..+47</code>,4 字节 <code>rel32</code> 位移位于 <strong><code>+48..+51</code></strong>,由 <code>captures.spawn_call</code>(<code>offset: 48</code>)动态读出跳转目标 RVA!</td></tr><tr><td><code>+52 .. +56</code></td><td>5 B</td><td><code>48 8b 54 24 20</code></td><td><code>mov rdx, qword ptr [rsp + 0x20]</code></td><td>后置上下文锚点:锁死指令边界,确认 <code>+51</code> 之后紧跟预期的下一条指令</td></tr></tbody></table></div>
|
||
<p>从上面这张表可以提炼出编写任何 EXE 补丁的 <strong>5 个核心要点</strong>:</p>
|
||
<ol><li><strong>首尾锚点保证 <code>.text</code> 全局唯一命中</strong>:单靠 <code>48 83 3d ?? ?? ?? ?? 00 0f 84 ?? ?? ?? ??</code>(判空后条件跳转)在整个 <code>aces.exe</code> 里会撞上成百上千处;加上前后 <code>+0..+37</code> 与 <code>+52..+56</code> 的寄存器与调用序列后,80 MB 代码段里就只剩这唯一 1 处命中。</li><li><strong><code>edit.offset: 38</code> 精准对齐被替换指令首字节</strong>:只改写 <code>+38..+51</code> 这两条指令(<code>8 + 6 = 14</code> 字节),前后的上下文锚点纹丝不动。</li><li><strong><code>captures.spawn_call</code> 动态抠出跳转目的地</strong>:原 <code>jz rel32</code> 的 4 字节有符号位移位于 <code>site + 48..51</code>,未传 <code>end</code> 时默认指令结尾为 <code>offset + 4 = 52</code>,因此补丁引擎自动算出目标 RVA = <code>site + 52 + disp32</code>(在 <code>2.61.0.37</code> 上即 <code>0x11b2c14 + 52 + disp32 = 0x11b314e</code>)。哪怕下个版本目标函数挪到了别的地址,只要相对跳转还在,捕获出的目标就永远准确!</li><li><strong><code>expect_original</code>、<code>bytes</code> 与 <code>asm</code> 三重互锁</strong>:<code>expect_original</code>(<code>8 + 6 = 14</code> 字节)与写入的 <code>bytes</code>(5 字节 <code>e9 ?? ?? ?? ??</code> 无条件跳转 + 9 个 <code>90</code> <code>nop</code> = <strong>14 字节</strong>)长度必须严丝合缝相等;运行 <code>wtotools check-patches</code> 时还会调用汇编器把 <code>asm</code> 编译成机器码与 <code>bytes</code> 逐字节比对,杜绝手算机器码出错。</li><li><strong>务必分清两套偏移基准点(新手头号易混点!)</strong>:
|
||
<ul><li><code>edit.offset</code>(<code>38</code>)和 <code>locate.captures.*.offset</code>(<code>48</code>)都是<strong>从 <code>locate</code> 匹配起点 <code>site</code>(<code>+0</code>)开始算的</strong>;</li><li>而 <code>edit.fixups[].at</code>(<code>1</code>)和 <code>edit.fixups[].end</code>(默认 <code>at + 4 = 5</code>)是<strong>从 <code>edit.bytes</code> 的第 0 字节(即 <code>site + edit.offset = site + 38</code>)开始算的</strong>!因为 <code>bytes</code> 第 0 字节是 <code>e9</code>(<code>jmp</code> 操作码),紧跟其后的 4 个 <code>??</code> 洞口就在 <code>at: 1</code>,回填位移公式为 <code>spawn_call - ((site + 38) + 5)</code>。</li></ul></li></ol>
|
||
<h4 id="ch-03-zh-3-3-exe-与-native-定位进阶参考-必读防坑指南">3.3 EXE 与 Native 定位进阶参考(必读防坑指南)</h4>
|
||
<h5 id="ch-03-zh-1-rel32-捕获的-end-陷阱-带尾随立即数的-rip-相对寻址指令">(1)<code>rel32</code> 捕获的 <code>"end"</code> 陷阱:带尾随立即数的 RIP 相对寻址指令!</h5>
|
||
<p>x86-64 CPU 计算 <code>[rip + disp32]</code> 内存地址时,基准点永远是<strong>整条指令结束处(下一条指令的首地址)</strong>,而不是 <code>disp32</code> 这 4 个字节结束的位置!</p>
|
||
<ul><li>对于 <code>mov rax, [rip + disp32]</code>(<code>48 8b 05 ?? ?? ?? ??</code>,共 7 字节)或 <code>jz rel32</code>(<code>0f 84 ?? ?? ?? ??</code>,共 6 字节),<code>disp32</code> 恰好位于指令最末尾,所以默认的 <code>end = offset + 4</code> 刚好等于指令结尾,无需写 <code>"end"</code>。</li><li><strong>但如果指令在 <code>disp32</code> 后面还跟着立即数操作数</strong>,默认 <code>end</code> 就会算错地址!例如单条指令: <code>c6 05 ?? ?? ?? ?? 00</code> —— <code>mov byte ptr [rip + disp32], 0</code>(整条指令共 <strong>7 字节</strong>:<code>c6 05</code> 占 <code>0..1</code>,<code>disp32</code> 占 <code>2..5</code>,末尾立即数 <code>00</code> 占第 <code>6</code> 字节,指令在偏移 <strong><code>7</code></strong> 处结束)。如果你只写 <code>{ "offset": 2, "type": "rel32" }</code>,默认 <code>end</code> 会取 <code>2 + 4 = 6</code>,算出来的全局变量 RVA 就会<strong>偏小 1 个字节</strong>!此时<strong>必须显式加上 <code>"end": 7</code></strong>:
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>jsonc</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>"captures": {
|
||
"some_flag": { "offset": 2, "type": "rel32", "end": 7 }
|
||
}</code></pre></div>
|
||
<p>仓库真实范例见 <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/patches/native/local_mp_core.jsonc" target="_blank" rel="noopener"><code>patches/native/local_mp_core.jsonc</code></a> 的 <code>disabled_slots_mask</code> 站点:<code>31 c0</code>(<code>+0..+1</code>)后面紧跟 <code>48 83 3d ?? ?? ?? ?? 00</code>(<code>+2..+9</code>,<code>cmp qword ptr [rip + disp32], 0</code>),<code>disp32</code> 在 <code>offset: 5</code>,末尾还有 1 字节立即数 <code>00</code>(<code>+9</code>),因此显式声明了 <code>"multiplayer": { "offset": 5, "type": "rel32", "end": 10 }</code>。</p></li></ul>
|
||
<h5 id="ch-03-zh-2-captures-支持的全部-7-种数值类型-packages-exepatch-src-captures-ts">(2)<code>captures</code> 支持的全部 7 种数值类型(<code>packages/exepatch/src/captures.ts</code>)</h5>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th><code>type</code></th><th>字节宽度</th><th>读取语义</th><th>典型用途</th></tr></thead><tbody><tr><td><code>"rel32"</code></td><td>4 B(小端有符号)</td><td>计算并返回 <code>site + end + int32</code>(即目标 RVA)</td><td>捕获 <code>call</code>、<code>jmp</code>/<code>jcc</code> 跳转目标或 <code>[rip + disp32]</code> 全局变量地址</td></tr><tr><td><code>"u8"</code></td><td>1 B(无符号)</td><td>原样读取 <code>0..255</code></td><td>捕获短位移成员偏移(如 <code>[r8 + 0x61]</code> 中的 <code>0x61</code>)</td></tr><tr><td><code>"u16"</code></td><td>2 B(小端无符号)</td><td>原样读取 <code>0..65535</code></td><td>捕获 16 位消息号或协议常量(如 MPI 消息 ID)</td></tr><tr><td><code>"u32"</code></td><td>4 B(小端无符号)</td><td>原样读取 32 位无符号整数</td><td>捕获常规 C++ 结构体大偏移(如 <code>[rax + 0x6d8]</code> 中的 <code>0x6d8</code>)或元素步长</td></tr><tr><td><code>"i8"</code></td><td>1 B(有符号)</td><td>原样读取 <code>-128..127</code></td><td>捕获带负号的单字节指针偏置(如循环步进指针向回偏移 <code>-0x10</code>)</td></tr><tr><td><code>"i16"</code></td><td>2 B(小端有符号)</td><td>原样读取 <code>-32768..32767</code></td><td>捕获 16 位有符号位移</td></tr><tr><td><code>"i32"</code></td><td>4 B(小端有符号)</td><td>原样读取 32 位有符号整数</td><td>捕获可能为负数的 32 位有符号结构体/栈位移</td></tr></tbody></table></div>
|
||
<h5 id="ch-03-zh-3-改写-rdata-只读字符串的-data-写法">(3)改写 <code>.rdata</code> 只读字符串的 <code>"data"</code> 写法</h5>
|
||
<p>当你在 <code>.rdata</code> 数据段替换等长 ASCII 字符串(而不是改 <code>.text</code> 汇编指令)时,在 <code>edit</code> 中用 <code>"data"</code> 代替 <code>"asm"</code>(真实范例见 <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/patches/exe/online/network-hook-library/patch.jsonc" target="_blank" rel="noopener"><code>patches/exe/online/network-hook-library/patch.jsonc</code></a>):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>jsonc</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>"edit": {
|
||
"offset": 0,
|
||
"data": {
|
||
"encoding": "ascii",
|
||
"text": "wto_ws.dll"
|
||
},
|
||
"bytes": "77 74 6f 5f 77 73 2e 64 6c 6c 00",
|
||
"expect_original": "57 53 32 5f 33 32 2e 64 6c 6c 00"
|
||
}</code></pre></div>
|
||
<p>注意:<code>bytes</code> 与 <code>expect_original</code> 都包含末尾的 C 字符串终止零字节 <code>00</code>(共 11 字节),<code>wtotools check-patches</code> 会自动核对 <code>"text"</code> 的 ASCII 编码加终止零与 <code>"bytes"</code> 完全一致。</p>
|
||
<hr>
|
||
<h3 id="ch-03-zh-4-native-模块-patches-native-native-wto-net-三种定位器与-6-文件完整串联教程">4. Native 模块(<code>patches/native/</code> + <code>native/wto-net/</code>)—— 三种定位器与 6 文件完整串联教程</h3>
|
||
<p>当需要在 <code>wto_ws.dll</code> 中用 C 语言挂载 MinHook 钩子或调用引擎内部函数时,<strong>不要在 C 源码里写死十六进制地址或游戏结构体偏移常量</strong>——所有值都由 <code>patches/native/<模块>.jsonc</code> 定位,经 <code>wto_ws.cfg</code> 传入 C 侧全局结构体 <code>g_lmp</code>。</p>
|
||
<h4 id="ch-03-zh-4-1-三种代码定位方法详解-signature-stringref-within">4.1 三种代码定位方法详解(<code>signature</code> / <code>string_ref</code> / <code>within</code>)</h4>
|
||
<ol><li><strong><code>"method": "signature"</code></strong>:在指定段(默认 <code>".text"</code>,可通过 <code>"section"</code> 覆盖)搜索全局唯一命中的字节特征码,支持 <code>captures</code>。</li><li><strong><code>"method": "string_ref"</code></strong>(<code>packages/exepatch/src/locate/string-ref.ts</code>,<strong>最抗编译器重排与小版本更新的利器!</strong>):先在 <code>".rdata"</code>(或指定 <code>"section"</code>)中找到以 <code>\0</code> 结尾的 UTF-8 字符串,再检索 <code>.text</code> 中所有加载该字符串地址的 7 字节 RIP 相对 <code>lea</code> 指令(<code>48 8d</code> / <code>4c 8d</code>)。它共有 <strong>3 种工作模式</strong>:
|
||
<ul><li><strong>模式 A(默认 <code>"target": "reference"</code>,不带 <code>"pattern"</code>)</strong>:直接返回那条 7 字节 <code>lea</code> 指令本身的 RVA(可校验字节长度为 7);</li><li><strong>模式 B(<code>"target": "function"</code>)</strong>:通过 PE 异常表(<code>.pdata</code>)反查包含该 <code>lea</code> 引用的<strong>外层函数起始 RVA</strong> 并去重返回(注意:此模式可校验字节长度为 0,不能直接在它上面挂 <code>captures</code>,专门用来定位函数入口供 MinHook 挂钩或作为下方 <code>within</code> 的父锚点!);</li><li><strong>模式 C(<code>"pattern"</code> + <code>"lea_at"</code>,此时 <code>"target"</code> 必须保持默认 <code>"reference"</code>)</strong>:围绕字符串 <code>lea</code> 匹配一段局部指令序列,要求加载该字符串的 7 字节 <code>lea</code> 恰好位于 <code>pattern</code> 内部偏移 <code>lea_at</code> 处,返回 <code>pattern</code> 起点 RVA——这样就能顺手用 <code>captures</code> 把紧挨着字符串引用的结构体字段偏移抠出来!(例如通过字段名 <code>"kills"</code> 直接抠出 <code>MPlayer</code> 里的击杀计数字段偏移)。</li></ul></li><li><strong><code>"method": "within"</code></strong>(<code>packages/exepatch/src/locate/within.ts</code>):在<strong>同一个或被 <code>requires</code> 依赖的模块中已经先行定位好的站点 <code>"in"</code></strong> 所属函数的 <code>.pdata</code> <code>[begin, end)</code> 范围内搜索 <code>"pattern"</code>。因为在单个函数内部搜索,哪怕只有一两条指令的短特征码也能唯一命中,彻底避免全程序撞码。</li></ol>
|
||
<h4 id="ch-03-zh-4-2-跟着-localmpscores-走完从-jsonc-到-c-落地的全部-6-个文件">4.2 跟着 <code>local_mp_scores</code> 走完从 JSONC 到 C 落地的全部 6 个文件</h4>
|
||
<p>假设我们要实现 <code>LocalMP</code> 局内记分牌分值读取模块 <code>local_mp_scores</code>(本地服务器把各事件得分写进任务配置的 <code>customScoreParams</code> 子块,客户端 <code>MissionSettings::load</code> 会把它拷进 <code>MissionSettings</code> 结构体内部某偏移处,但客户端自己从不读它;我们的 C 模块需要调用引擎的 <code>DataBlock::getReal</code> 从该偏移处读出分值)。看看这 <strong>6 个真实文件</strong>是怎么严丝合缝串起来的:</p>
|
||
<h5 id="ch-03-zh-第-1-步-编写定位定义-patches-native-localmpscores-jsonc">第 1 步:编写定位定义 <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/patches/native/local_mp_scores.jsonc" target="_blank" rel="noopener"><code>patches/native/local_mp_scores.jsonc</code></a></h5>
|
||
<p>文件名必须与 <code>"module"</code> 字段完全同名。由于我们要复用 <code>local_mp_core.jsonc</code> 里已经用 <code>"string_ref"</code>(<code>"string": "customScoreParams", "target": "function"</code>)定位好的函数 <code>"mission_settings_load"</code>,在 <code>"requires"</code> 中声明 <code>"local_mp_core"</code>:</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>jsonc</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// Score table of a LocalMP battle (native/wto-net/src/local_mp_scores.c): the local server puts it
|
||
// in the mission's customScoreParams; the host reads it there.
|
||
// Without it: kills, assists and captures score nothing.
|
||
{
|
||
"module": "local_mp_scores",
|
||
"features": [
|
||
"K-Kill, assist and capture scoring"
|
||
],
|
||
"requires": [
|
||
"local_mp_core"
|
||
],
|
||
"sites": {
|
||
// DataBlock::getReal wrapper.
|
||
// 2.61.0.37: 0x5445270
|
||
"datablock_get_real": {
|
||
"variants": [
|
||
{
|
||
"written_for": "2.61.0.37",
|
||
"locate": {
|
||
"method": "signature",
|
||
"pattern": "48 83 ec 28 f3 0f 11 54 24 24 4c 8d 44 24 24 e8 ?? ?? ?? ?? 90 48 83 c4 28 c3 cc cc cc cc cc cc 56 57 53 48 83 ec 30 48 89 d7 48 89 ce 48 8d 15"
|
||
}
|
||
}
|
||
]
|
||
},
|
||
// MissionSettings customScoreParams copy.
|
||
// 2.61.0.37: 0x126d762 (lea rcx, [rax + 0x6d8]; mov eax, dword ptr [r13 + 0x10]; cmp eax, -1; ...)
|
||
"mission_score_params": {
|
||
"variants": [
|
||
{
|
||
"written_for": "2.61.0.37",
|
||
"locate": {
|
||
"method": "within",
|
||
"in": "mission_settings_load",
|
||
"pattern": "48 8d 88 ?? ?? ?? ?? 41 8b 45 10 83 f8 ff 74 ?? 45 0f b7 45 0e 46 8d 0c 00 31 d2 44 39 c8 0f 8d ?? ?? ?? ?? ff c7 4d 8b 4d 00 45 8b 51 5c 4d 01 d1 49 81 c1 90 00 00 00 41 bb ff ff ff 3f 45 31 d2 66 66 66 66 2e 0f 1f 84 00 00 00 00 00 42 8d 34 10 48 c1 e6 05 41 8b 5c 31 08 44 21 db 39 fb 74 ?? 41 ff c2 45 39 d0 75 ?? eb ?? 41 0f b7 45 0e 31 d2 85 c0 74 ?? ff c7 4d 8b 4d 18 4d 8b 01 45 8b 49 08 44 8d 14 c5 00 00 00 00 45 29 d1 41 ba ff ff ff 3f 45 31 db 66 0f 1f 44 00 00 4b 8b 34 08 8b 76 08 44 21 d6 39 fe 74 ?? 41 ff c3 49 83 c1 08 44 39 d8 75 ?? eb ?? 49 01 f1 31 d2 45 39 c2 49 0f 42 d1 eb ?? 41 39 c3 73 ?? 44 89 c8 49 8b 14 00 eb ?? 31 d2 48 85 d2 49 0f 44 d6 31 f6 45 31 c0 e8 ?? ?? ?? ?? 49 8b 5d 00 bf ff ff ff ff 48 85 db 74 ?? 48 8d 0d ?? ?? ?? ?? ba 16 00 00 00",
|
||
"captures": {
|
||
"custom_score_params": {
|
||
"offset": 3,
|
||
"type": "u32"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
]
|
||
}
|
||
}
|
||
}</code></pre></div>
|
||
<p>看 <code>mission_score_params</code> 首条指令 <code>48 8d 88 ?? ?? ?? ??</code>(<code>lea rcx, [rax + disp32]</code>):前 3 字节是 <code>48 8d 88</code>,第 <code>3..6</code> 字节就是 <code>MissionSettings</code> 里的结构体偏移,因此 <code>"offset": 3, "type": "u32"</code> 直接把 <code>0x6d8</code> 抠了出来!</p>
|
||
<h5 id="ch-03-zh-第-2-步-构建器自动生成-离线副本-win64-wtows-cfg-packages-patcher-src-native">第 2 步:构建器自动生成 <code><离线副本>/win64/wto_ws.cfg</code>(<code>packages/patcher/src/native/</code>)</h5>
|
||
<p>构建副本时,<code>native-resolve.ts</code> 与 <code>native-settings.ts</code> 会把当前 <code>aces.exe</code> 的 PE 头指纹连同所有已就绪模块的 <code><站点></code> 与 <code><站点>.<捕获名></code> 写入 <code>win64/wto_ws.cfg</code>:</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>ini</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>port=27015
|
||
exe.time_date_stamp=0x67e5a1b2
|
||
exe.size_of_image=0x83f4000
|
||
native.datablock_get_real=0x5445270
|
||
native.mission_score_params=0x126d762
|
||
native.mission_score_params.custom_score_params=0x6d8</code></pre></div>
|
||
<h5 id="ch-03-zh-第-3-步-在-native-wto-net-src-localmpvalues-h-声明-lmpvalues-字段">第 3 步:在 <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/native/wto-net/src/local_mp_values.h" target="_blank" rel="noopener"><code>native/wto-net/src/local_mp_values.h</code></a> 声明 <code>LmpValues</code> 字段</h5>
|
||
<p>地址一律声明为 <code>uint8_t *</code>,无符号偏移/大小声明为 <code>size_t</code>(有符号位移声明为 <code>ptrdiff_t</code>):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>c</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>typedef struct LmpValues {
|
||
/* ... 其他模块字段 ... */
|
||
|
||
/* local_mp_scores */
|
||
uint8_t *datablock_get_real;
|
||
size_t mis_custom_score_params;
|
||
} LmpValues;
|
||
|
||
extern LmpValues g_lmp;</code></pre></div>
|
||
<h5 id="ch-03-zh-第-4-步-在-native-wto-net-src-localmpvalues-c-绑定-specs-映射表">第 4 步:在 <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/native/wto-net/src/local_mp_values.c" target="_blank" rel="noopener"><code>native/wto-net/src/local_mp_values.c</code></a> 绑定 <code>SPECS[]</code> 映射表</h5>
|
||
<p><code>local_mp_values.c</code> 通过三个宏把 <code>wto_ws.cfg</code> 里的键名映射到 <code>g_lmp</code> 的成员:</p>
|
||
<ul><li><strong><code>A(module, name, field)</code></strong>(<code>LMP_ADDRESS</code>):读取 RVA 后自动加上当次进程基址 <code>(uint8_t *)GetModuleHandleW(NULL) + value</code>,存成可直接调用的运行时指针;</li><li><strong><code>O(module, name, field)</code></strong>(<code>LMP_OFFSET</code>):存为无符号 <code>size_t</code> 结构体偏移或步长;</li><li><strong><code>S(module, name, field)</code></strong>(<code>LMP_SIGNED</code>):存为有符号 <code>ptrdiff_t</code> 位移;</li><li>如果某个复合偏移需要由两个捕获值相加减(客户端没有单独访问它的指令),则写在同文件的 <code>derive_values()</code> 中。</li></ul>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>c</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>#define SCORES "local_mp_scores"
|
||
|
||
static const LmpValueSpec SPECS[] = {
|
||
/* ... */
|
||
A(SCORES, "datablock_get_real", datablock_get_real),
|
||
O(SCORES, "mission_score_params.custom_score_params", mis_custom_score_params),
|
||
/* ... */
|
||
};</code></pre></div>
|
||
<h5 id="ch-03-zh-第-5-步-在-native-wto-net-src-localmpoffsets-h-定义可读偏移宏">第 5 步:在 <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/native/wto-net/src/local_mp_offsets.h" target="_blank" rel="noopener"><code>native/wto-net/src/local_mp_offsets.h</code></a> 定义可读偏移宏</h5>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>c</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>#define LMP_MIS_OFF_CUSTOM_SCORE_PARAMS (g_lmp.mis_custom_score_params)</code></pre></div>
|
||
<h5 id="ch-03-zh-第-6-步-实现-localmpscores-c-注册到-localmpcore-c-并加入-build-ps1">第 6 步:实现 <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/native/wto-net/src/local_mp_scores.c" target="_blank" rel="noopener"><code>local_mp_scores.c</code></a>、注册到 <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/native/wto-net/src/local_mp_core.c" target="_blank" rel="noopener"><code>local_mp_core.c</code></a> 并加入 <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/native/wto-net/build.ps1" target="_blank" rel="noopener"><code>build.ps1</code></a></h5>
|
||
<ul><li>在 <code>local_mp_scores.c</code> 中只读取 <code>g_lmp</code> 与 <code>local_mp_core</code> 导出的公共指针:
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>c</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>typedef float (__fastcall *DataBlockGetRealFn)(const void *blk, const char *name, float def);
|
||
static DataBlockGetRealFn fn_datablock_get_real;
|
||
|
||
float local_mp_score_param(const char *key)
|
||
{
|
||
if (fn_datablock_get_real == NULL || g_local_mp_mission_settings == NULL || key == NULL)
|
||
return 0.0f;
|
||
return fn_datablock_get_real(g_local_mp_mission_settings + LMP_MIS_OFF_CUSTOM_SCORE_PARAMS, key, 0.0f);
|
||
}
|
||
|
||
int install_local_mp_score_reader(void)
|
||
{
|
||
fn_datablock_get_real = (DataBlockGetRealFn)g_lmp.datablock_get_real;
|
||
return 1;
|
||
}</code></pre></div></li><li>在 <code>local_mp_core.c</code> 中,启动时 <code>install_local_mp_hooks()</code> 会先用 <code>config_native_matches_executable()</code> 核对当前运行中 <code>aces.exe</code> 的 PE <code>TimeDateStamp</code> 与 <code>SizeOfImage</code> 是否与 <code>wto_ws.cfg</code> 完全一致(不一致则整套 <code>LocalMP</code> 桥接器安全关闭),然后按依赖顺序遍历 <code>MODULES[]</code> 数组,对每个模块先跑 <code>lmp_values_load(MODULES[i].name)</code> 确认值一个不少,再调用安装函数并在最后统一 <code>MH_ApplyQueued()</code>。</li><li><strong>如果新增了一个全新的 <code>.c</code> 源文件</strong>:除了在 <code>local_mp_internal.h</code> 声明安装函数并加入 <code>local_mp_core.c</code> 的 <code>MODULES[]</code>,还<strong>必须把该 <code>.c</code> 文件路径加进 <code>native/wto-net/build.ps1</code> 的 <code>$sources</code> 数组</strong>!</li><li><strong>多模块共享钩子</strong>:如果两个模块都需要拦截或调用同一个引擎函数(例如 <code>Unit::onEvent</code> 同时被 <code>local_mp_damage_notifications</code> 和 <code>local_mp_air_events</code> 使用),请把站点放进 <code>local_mp_core.jsonc</code> 统一解析分发,避免在两个模块里用 MinHook 重复挂钩同一个目标地址。</li></ul>
|
||
<hr>
|
||
<h3 id="ch-03-zh-5-适配新版本客户端-标准四步流程">5. 适配新版本客户端(标准四步流程)</h3>
|
||
<p>每当《战争雷霆》发布小版本更新(例如 <code>2.61.0.37</code> → <code>2.61.0.58</code>)时,按下面四步跟进适配:</p>
|
||
<ol><li><strong>对只读原版客户端运行版本核对脚本</strong>:
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code> node scripts/record-game-version.ts --game "<原版目录>"</code></pre></div>
|
||
<p>该脚本会只读扫描原版客户端,一次性跑完所有 <code>patches/exe/</code>、<code>patches/gui/</code> 和 <code>patches/native/</code> 的定位测试。如果三条核心安全锁(<code>ESSENTIAL_FEATURES</code>)中有补丁失效,脚本会直接拒绝生成版本记录并列出阻断项;若核心安全锁全部命中,则会写出 <code>patches/versions/<版本号>.jsonc</code> 并打印所有 <code>unavailable</code>(暂不可用)的非关键补丁/模块。</p></li><li><strong>排查失配项并追加新 <code>variant</code></strong>: 打开失配的补丁或原生站点,用第 1 节的 Node 拆包脚本或 <code>tools/re/cli.py</code>(优先用 <code>find-string</code> 找不变的字符串锚点!)查看新版本原版文件发生了什么变化。<strong>保留原有旧版本的 <code>variant</code> 不动</strong>,在 <code>variants</code> 数组中追加一条 <code>"written_for": "<新版本号>"</code> 的新写法。</li><li><strong>跑完完整工具链核对</strong>: 重新运行 <code>record-game-version.ts</code> 确认 <code>unavailable</code> 清零,并依次跑通:
|
||
<ul><li><code>.\.venv\Scripts\python.exe -m wtotools.cli check-patches --exe "<原版目录>\win64\aces.exe"</code></li><li><code>.\.venv\Scripts\python.exe -m wtotools.cli check-gui --gui "<原版目录>\gui.vromfs.bin" --sq "build\quirrel-cli\bin\Release\sq.exe"</code></li><li>若改了 <code>native/wto-net/</code>,运行 <code>powershell -NoProfile -File .\native\wto-net\build.ps1</code></li><li>用临时输出目录跑一遍 <code>node scripts/build-game-copy.ts</code> 确认副本完整生成。</li></ul></li><li><strong>隔离副本实机进游戏验收后再提交 <code>patches/versions/<版本号>.jsonc</code></strong>: 启动器依赖 <code>patches/versions/<版本号>.jsonc</code> 向玩家展示「该客户端版本已核对通过」。请在隔离副本里实际登录进机库并完成一局 <code>LocalMP</code> 对局验收后,再提交 <code>patches/versions/<版本号>.jsonc</code>,不要仅凭命令行脚本通过就直接入库。</li></ol>
|
||
<p>新建补丁时可直接复制空白脚手架模板:<a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/dev-docs/templates/gui/patch.jsonc" target="_blank" rel="noopener"><code>dev-docs/templates/gui/</code></a>、<a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/dev-docs/templates/exe/patch.jsonc" target="_blank" rel="noopener"><code>dev-docs/templates/exe/</code></a>、<a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/dev-docs/templates/native/local_mp_example.jsonc" target="_blank" rel="noopener"><code>dev-docs/templates/native/</code></a>。</p>
|
||
</div>
|
||
<div class="lang-en">
|
||
<h2>03 · Patch Authoring, Byte Anatomy & Full Native Wiring</h2>
|
||
<p>This handbook covers authoring client patches (Quirrel UI scripts, <code>aces.exe</code> machine-code/data patches, and <code>wto_ws.dll</code> <code>LocalMP</code> native modules) and adapting WTOffline to new War Thunder client builds.</p>
|
||
<blockquote><p><strong>Check first before touching the client</strong>: Read <a href="#ch-02">Development Rules — Section 1</a> and verify that the issue <strong>genuinely cannot</strong> be solved in <code>apps/server</code> or via stock mission/config BLK fields. Client patches are strictly for code paths that retail client builds omit or hardcode.</p></blockquote>
|
||
<hr>
|
||
<h3 id="ch-03-en-1-step-0-unpacking-stock-archives-reading-binary-blk-disassembling-aces-exe-and-choosing-a-patch-type">1. Step 0: Unpacking Stock Archives, Reading Binary BLK, Disassembling <code>aces.exe</code>, and Choosing a Patch Type</h3>
|
||
<p>Never modify any file inside your official stock installation. Using <code>@wto/vromfs</code>, <code>@wto/blk</code>, and <code>tools/re/cli.py</code>, you can inspect every stock script, binary <code>.blk</code>, and <code>aces.exe</code> function purely in memory.</p>
|
||
<h4 id="ch-03-en-1-1-inspect-nut-scripts-or-binary-blk-files-inside-stock-vromfs-bin-archives">1.1 Inspect <code>.nut</code> Scripts or Binary <code>.blk</code> Files Inside Stock <code>.vromfs.bin</code> Archives</h4>
|
||
<p>Run these one-liners from the repository root (replacing <code><stock-dir></code> with your read-only official War Thunder path):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code># Example A: List matching .nut paths inside gui.vromfs.bin and print a stock Quirrel script
|
||
node --input-type=module -e "
|
||
import { readFileSync } from 'node:fs';
|
||
import { getFile, readArchive } from '@wto/vromfs';
|
||
const archive = readArchive(readFileSync(String.raw`<stock-dir>\gui.vromfs.bin`));
|
||
console.log([...archive.files.keys()].filter((p) => p.includes('tutorial')));
|
||
console.log(Buffer.from(getFile(archive, 'scripts/tutorials/nexttutorialhandler.nut')).toString('utf8'));
|
||
"
|
||
|
||
# Example B: Decode a slimmed binary .blk from char.vromfs.bin (or aces.vromfs.bin) as readable text
|
||
# (automatically loads the archive's shared name map `nm` and optional `.dict` zstd dictionary)
|
||
node --input-type=module -e "
|
||
import { readFileSync } from 'node:fs';
|
||
import { decodeBlk, formatBlkText, parseSharedNameMap } from '@wto/blk';
|
||
import { getFile, NAME_MAP_PATH, readArchive } from '@wto/vromfs';
|
||
const archive = readArchive(readFileSync(String.raw`<stock-dir>\char.vromfs.bin`));
|
||
const dictionary = [...archive.files.entries()].find(([p]) => p.endsWith('.dict'))?.[1];
|
||
const names = archive.files.has(NAME_MAP_PATH) ? parseSharedNameMap(getFile(archive, NAME_MAP_PATH), dictionary).names : [];
|
||
const blk = decodeBlk(getFile(archive, 'config/pve_awards.blk'), { names, dictionary });
|
||
console.log(formatBlkText(blk).slice(0, 2000));
|
||
"</code></pre></div>
|
||
<h4 id="ch-03-en-1-2-static-reverse-engineering-on-aces-exe-with-tools-re-cli-py">1.2 Static Reverse Engineering on <code>aces.exe</code> with <code>tools/re/cli.py</code></h4>
|
||
<p>Once your <code>.venv</code> is installed (<code>pip install -e ./tools</code>; see <a href="#ch-04">Environment Setup</a>), use <code>tools/re/cli.py</code> to locate strings, cross-references, callers, and disassembly (<strong>all addresses are hex RVAs, and always pass <code>--exe "<stock-dir>\win64\aces.exe"</code> explicitly</strong>):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code># 1. Find where a C string lives in .rdata and every rip-relative `lea` instruction + function referencing it
|
||
.\.venv\Scripts\python.exe tools\re\cli.py find-string "customScoreParams" --exe "<stock-dir>\win64\aces.exe"
|
||
|
||
# 2. Find every instruction with a rip-relative reference to a target RVA (e.g., a global pointer)
|
||
.\.venv\Scripts\python.exe tools\re\cli.py xref 0x7bad2f0 --exe "<stock-dir>\win64\aces.exe"
|
||
|
||
# 3. Find every direct `call` or `jmp` targeting a function RVA
|
||
.\.venv\Scripts\python.exe tools\re\cli.py callers 0x5445270 --exe "<stock-dir>\win64\aces.exe"
|
||
|
||
# 4. Disassemble an RVA with string and call annotations (defaults to 40 instructions; supports --count, --end, or --function)
|
||
.\.venv\Scripts\python.exe tools\re\cli.py disasm 0x11b2c14 --count 20 --exe "<stock-dir>\win64\aces.exe"
|
||
.\.venv\Scripts\python.exe tools\re\cli.py disasm 0x126d762 --function --exe "<stock-dir>\win64\aces.exe"</code></pre></div>
|
||
<h4 id="ch-03-en-1-3-choosing-the-right-patch-type-universal-conventions">1.3 Choosing the Right Patch Type & Universal Conventions</h4>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th>What needs changing</th><th>Where it belongs</th><th>Real codebase example</th></tr></thead><tbody><tr><td>Hangar, modal, store, or debriefing UI scripts (Quirrel <code>.nut</code>)</td><td><code>patches/gui/<domain>/<name>/</code></td><td><a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/patches/gui/tutorials/immediate-save-skip-tutorial/patch.jsonc" target="_blank" rel="noopener"><code>patches/gui/tutorials/immediate-save-skip-tutorial/</code></a></td></tr><tr><td>A few machine instructions or a constant <code>.rdata</code> string in <code>aces.exe</code></td><td><code>patches/exe/<domain>/<name>/</code></td><td><a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/patches/exe/battle/allow-local-host-bot-spawns/patch.jsonc" target="_blank" rel="noopener"><code>patches/exe/battle/allow-local-host-bot-spawns/</code></a></td></tr><tr><td>In-process C hooks restoring <code>LocalMP</code> battle host logic (<code>wto_ws.dll</code>)</td><td><code>patches/native/<module>.jsonc</code> + <code>native/wto-net/src/<module>.c</code></td><td><a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/patches/native/local_mp_scores.jsonc" target="_blank" rel="noopener"><code>patches/native/local_mp_scores.jsonc</code></a></td></tr><tr><td>Static archive config rewrites (<code>network.blk</code>, <code>shop.blk</code>, <code>gui.blk</code>)</td><td><code>packages/patcher/src/</code> (<strong>not</strong> under <code>patches/</code>)</td><td><code>packages/patcher/src/char-pack.ts</code>, <code>main-pack.ts</code></td></tr></tbody></table></div>
|
||
<p><strong>Four universal rules across all <code>patches/</code> definitions</strong>:</p>
|
||
<ol><li><strong>One behavior per directory/module</strong>—never bundle unrelated fixes into a single patch definition.</li><li><strong>Start every <code>patch.jsonc</code> / <code><module>.jsonc</code> with a 3-line English comment</strong>:
|
||
<ul><li>Line 1: What the patch changes;</li><li>Line 2 (<code>Why the client:</code> / <code>Why the exe:</code>): Why the local server or stock config cannot provide it;</li><li>Line 3 (<code>Without it:</code>): What breaks for the player if this patch is missing.</li></ul></li><li><strong>Write <code>title</code> and <code>features</code> in English</strong> (note: a few early historical patches still carry Chinese <code>title</code> strings, <strong>but all new patches must write <code>title</code> in English</strong>), reusing existing <code>A-</code>..<code>K-</code> inventory tags (<a href="#ch-01">Glossary — Section 7</a>).</li><li><strong>Exact single-variant, single-site match</strong>: On any given client build, <strong>exactly one <code>variant</code> must apply, and its locator must hit exactly one site</strong>; zero hits or multiple hits fail safely rather than corrupting the output copy.</li></ol>
|
||
<hr>
|
||
<h3 id="ch-03-en-2-gui-patches-patches-gui-full-3-file-example-strict-schema-rules">2. GUI Patches (<code>patches/gui/</code>) — Full 3-File Example & Strict Schema Rules</h3>
|
||
<h4 id="ch-03-en-2-1-complete-real-example-tutorials-immediate-save-skip-tutorial">2.1 Complete Real Example: <code>tutorials/immediate-save-skip-tutorial</code></h4>
|
||
<p>In stock <code>scripts/tutorials/nexttutorialhandler.nut</code>, when a player ticks <strong>"Do not ask me again"</strong> on the tutorial prompt, the script calls <code>saveLocalByAccount(skipTutorialBitmaskId, skipTutorial)</code> with no third argument. That falls back to <code>saveProfile()</code>, which waits on a <strong>60-second debounce timer</strong> before sending anything to the server—so closing or crashing within 60 seconds loses the opt-out and brings the prompt back on next login. Because <code>apps/server</code> cannot speed up a client-side timer before a request is ever sent, this patch passes <code>@() save_profile(false)</code> as the immediate-save callback.</p>
|
||
<p>Here are <strong>all three files</strong> in <code>patches/gui/tutorials/immediate-save-skip-tutorial/</code> verbatim (note: this early patch kept a Chinese <code>title</code>, whereas new patches should use an English <code>title</code> such as <code>"Save tutorial opt-out immediately after ticking 'do not ask again'"</code>):</p>
|
||
<p><strong>1. <code>patch.jsonc</code></strong>:</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>jsonc</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// Save the tutorial opt-out immediately via save_profile(false).
|
||
// Why the client: the stock callback waits 60 s before writing or sending anything to the service.
|
||
// Without it: a crash within 60 s of ticking "do not ask again" brings the tutorial prompt back.
|
||
{
|
||
"id": "tutorials/immediate-save-skip-tutorial",
|
||
"title": "新手教程询问窗勾选「不再询问」后立即落盘保存",
|
||
"features": [
|
||
"A-Tutorials"
|
||
],
|
||
"variants": [
|
||
{
|
||
"written_for": "2.61.0.37",
|
||
"file": "scripts/tutorials/nexttutorialhandler.nut",
|
||
"edits": [
|
||
{
|
||
"find": "find.nut",
|
||
"replace": "replace.nut"
|
||
}
|
||
]
|
||
}
|
||
]
|
||
}</code></pre></div>
|
||
<p><strong>2. <code>find.nut</code></strong> (the smallest verbatim stock snippet occurring <strong>exactly once</strong> in the target <code>.nut</code> file, preserving exact indentation):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>squirrel</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code> skipTutorial = stdMath.change_bit(skipTutorial, this.checkIdx, obj.getValue())
|
||
saveLocalByAccount(skipTutorialBitmaskId, skipTutorial)
|
||
}</code></pre></div>
|
||
<p><strong>3. <code>replace.nut</code></strong> (the replacement text):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>squirrel</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code> skipTutorial = stdMath.change_bit(skipTutorial, this.checkIdx, obj.getValue())
|
||
saveLocalByAccount(skipTutorialBitmaskId, skipTutorial, @() save_profile(false))
|
||
}</code></pre></div>
|
||
<h4 id="ch-03-en-2-2-strict-gui-patch-definition-ts-schema-whitelist-authoring-rules">2.2 Strict <code>gui-patch-definition.ts</code> Schema Whitelist & Authoring Rules</h4>
|
||
<p><code>packages/patcher/src/gui-patch-definition.ts</code> calls <code>rejectUnknownKeys</code> at every level—<strong>any extra property immediately throws <code>DefinitionError</code></strong>:</p>
|
||
<ul><li><strong>Top-level keys are strictly <code>id</code>, <code>title</code>, <code>features</code>, <code>variants</code></strong>.
|
||
<blockquote><p><strong>Watch out: GUI <code>patch.jsonc</code> does NOT have a <code>requires</code> key!</strong> Do not copy <code>"requires": []</code> from an EXE patch into <code>patches/gui/</code>, or <code>loadGuiPatches</code> will throw <code>DefinitionError</code>!</p></blockquote></li><li><strong><code>variants[]</code> keys are strictly <code>written_for</code>, <code>file</code>, <code>edits</code></strong>. <code>file</code> must be the <strong>exact lowercase, <code>/</code>-separated relative path</strong> inside <code>gui.vromfs.bin</code> without a leading slash (e.g., <code>"scripts/tutorials/nexttutorialhandler.nut"</code>—archive paths are all lowercased!).</li><li><strong><code>edits[]</code> keys are strictly <code>find</code>, <code>replace</code></strong>, naming plain filenames directly inside the patch folder (no <code>/</code> or <code>\</code> subdirectories).</li><li><strong>Line endings & exact-once matching</strong>: <code>readFragment</code> strips any UTF-8 BOM, normalizes <code>\r\n</code> to <code>\n</code>, and removes a single trailing newline at EOF (so Git <code>autocrlf</code> never breaks patches). <code>find.nut</code> must match <strong>exactly once</strong> in the target script.</li><li><strong><code>{{LOCAL_ENDPOINT}}</code> placeholder</strong>: Whenever replacement Quirrel code needs the local server address, write <code>{{LOCAL_ENDPOINT}}</code>; <code>gui-pack.ts</code> expands it to <code>127.0.0.1:<port></code> during copy builds.</li><li><strong>Adding a variant for a newer build</strong>: Never edit an existing <code>find.nut</code> in a way that breaks older supported builds—create <code>find-58.nut</code> / <code>replace-58.nut</code> and append a new entry to <code>variants</code>.</li><li><strong>Mandatory <code>sq.exe</code> bytecode check</strong>: Every change under <code>patches/gui/</code> <strong>must</strong> pass:
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>.\.venv\Scripts\python.exe -m wtotools.cli check-gui --gui "<stock-dir>\gui.vromfs.bin" --sq "build\quirrel-cli\bin\Release\sq.exe"</code></pre></div>
|
||
<p><code>check-gui</code> applies all GUI patches in memory and runs <code>sq.exe</code> (<code>compilestring</code>) on every patched <code>.nut</code> script, catching lexical binding clashes with top-of-file <code>from ... import *</code> imports, unbound forward calls, and reserved keywords like <code>base</code> that otherwise hang the client forever on <code>"Loading..."</code>.</p></li></ul>
|
||
<hr>
|
||
<h3 id="ch-03-en-3-exe-patches-patches-exe-full-57-byte-anatomy-reference">3. EXE Patches (<code>patches/exe/</code>) — Full 57-Byte Anatomy & Reference</h3>
|
||
<p>When modifying instructions or constant data inside <code>aces.exe</code>, <code>@wto/exepatch</code> lets you express edits with <strong>zero hardcoded addresses</strong> so patches survive minor game updates automatically.</p>
|
||
<h4 id="ch-03-en-3-1-un-elided-real-example-patches-exe-battle-allow-local-host-bot-spawns-patch-jsonc">3.1 Un-Elided Real Example: <code>patches/exe/battle/allow-local-host-bot-spawns/patch.jsonc</code></h4>
|
||
<p>In <code>BotSystem::changeUnit</code>, after verifying a valid spawn point, stock <code>aces.exe</code> checks whether <code>g_multiplayer</code> is null (<code>cmp qword ptr [rip + disp32], 0</code>): in single-player test flights (<code>g_multiplayer == NULL</code>), <code>jz</code> jumps straight to the <code>Unit::changeAircraft</code> call to spawn the AI unit; in a <code>LocalMP</code> session (<code>g_multiplayer != NULL</code>), because retail <code>aces.exe</code> strips the Dedicated Server's script spawn permission, execution falls through to <code>"spawn denied by scripts"</code> and no AI vehicle ever spawns.</p>
|
||
<p>Here is the <strong>complete, un-elided</strong> <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/patches/exe/battle/allow-local-host-bot-spawns/patch.jsonc" target="_blank" rel="noopener"><code>patches/exe/battle/allow-local-host-bot-spawns/patch.jsonc</code></a> (<strong>all 57 pattern bytes</strong>, zero <code>...</code>):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>jsonc</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// Let BotSystem::changeUnit spawn the AI players' vehicles on a LocalMP host (0x11b2c3a: the g_multiplayer
|
||
// null guard becomes a jump to the Unit::changeAircraft call at 0x11b314e).
|
||
// Why the exe: the build drops the dedicated server's script spawn permission, so with a multiplayer
|
||
// session the function only logs "spawn denied by scripts"; non-host sessions already return at its start.
|
||
// Without it: no AI vehicle ever spawns in a LocalMP battle.
|
||
{
|
||
"id": "battle/allow-local-host-bot-spawns",
|
||
"title": "Let BotSystem spawn AI vehicles on a LocalMP host",
|
||
"features": [
|
||
"K-AI opponents and teammates"
|
||
],
|
||
"requires": [],
|
||
"variants": [
|
||
// Masked signature of BotSystem::changeUnit from the spawn point check (call 0x111a010)
|
||
// to the g_multiplayer null guard, 0x3c bytes; rip-relative and branch targets are wildcards.
|
||
// 2.61.0.37: unique hit at 0x11b2c14, edited instruction at +38 (0x11b2c3a).
|
||
// spawn_call: where the null guard's jz goes (the Unit::changeAircraft call); the edit jumps there
|
||
// unconditionally. The guard's rip displacement differs between builds, so it is a wildcard.
|
||
{
|
||
"written_for": "2.61.0.37",
|
||
"locate": {
|
||
"method": "signature",
|
||
"section": ".text",
|
||
"pattern": "48 8b 05 ?? ?? ?? ?? 48 8b 88 e0 03 00 00 48 8b 5c 24 20 48 89 da 4d 89 f0 e8 ?? ?? ?? ?? 84 c0 0f 84 ?? ?? ?? ?? 48 83 3d ?? ?? ?? ?? 00 0f 84 ?? ?? ?? ?? 48 8b 54 24 20",
|
||
"captures": {
|
||
"spawn_call": {
|
||
"offset": 48,
|
||
"type": "rel32"
|
||
}
|
||
}
|
||
},
|
||
"edit": {
|
||
"offset": 38,
|
||
"asm": [
|
||
"jmp {spawn_call}",
|
||
"nop",
|
||
"nop",
|
||
"nop",
|
||
"nop",
|
||
"nop",
|
||
"nop",
|
||
"nop",
|
||
"nop",
|
||
"nop"
|
||
],
|
||
"bytes": "e9 ?? ?? ?? ?? 90 90 90 90 90 90 90 90 90",
|
||
"expect_original": "48 83 3d ?? ?? ?? ?? 00 0f 84 ?? ?? ?? ??",
|
||
"fixups": [
|
||
{
|
||
"at": 1,
|
||
"type": "rel32",
|
||
"target": "spawn_call"
|
||
}
|
||
]
|
||
}
|
||
}
|
||
]
|
||
}</code></pre></div>
|
||
<h4 id="ch-03-en-3-2-instruction-by-instruction-breakdown-0-56-all-57-bytes">3.2 Instruction-by-Instruction Breakdown (<code>+0..+56</code>, All 57 Bytes)</h4>
|
||
<p>Breaking down every byte from the match start <code>site</code> (<code>0x11b2c14</code> on <code>2.61.0.37</code>) makes every offset number self-evident:</p>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th>Offset from <code>site</code></th><th>Size</th><th>Hex bytes in <code>pattern</code></th><th>x86-64 instruction</th><th>Role in this patch</th></tr></thead><tbody><tr><td><code>+0 .. +6</code></td><td>7 B</td><td><code>48 8b 05 ?? ?? ?? ??</code></td><td><code>mov rax, qword ptr [rip + disp32]</code></td><td>Leading context anchor (<code>??</code> masks build-varying RIP displacement)</td></tr><tr><td><code>+7 .. +13</code></td><td>7 B</td><td><code>48 8b 88 e0 03 00 00</code></td><td><code>mov rcx, qword ptr [rax + 0x3e0]</code></td><td>Leading context anchor</td></tr><tr><td><code>+14 .. +18</code></td><td>5 B</td><td><code>48 8b 5c 24 20</code></td><td><code>mov rbx, qword ptr [rsp + 0x20]</code></td><td>Leading context anchor</td></tr><tr><td><code>+19 .. +21</code></td><td>3 B</td><td><code>48 89 da</code></td><td><code>mov rdx, rbx</code></td><td>Leading context anchor</td></tr><tr><td><code>+22 .. +24</code></td><td>3 B</td><td><code>4d 89 f0</code></td><td><code>mov r8, r14</code></td><td>Leading context anchor</td></tr><tr><td><code>+25 .. +29</code></td><td>5 B</td><td><code>e8 ?? ?? ?? ??</code></td><td><code>call rel32</code> (spawn-point check)</td><td>Leading context anchor (<code>??</code> masks call displacement)</td></tr><tr><td><code>+30 .. +31</code></td><td>2 B</td><td><code>84 c0</code></td><td><code>test al, al</code></td><td>Leading context anchor</td></tr><tr><td><code>+32 .. +37</code></td><td>6 B</td><td><code>0f 84 ?? ?? ?? ??</code></td><td><code>jz rel32</code> (early-out if spawn check failed)</td><td>Leading context anchor (kept intact!)</td></tr><tr><td><strong><code>+38 .. +45</code></strong></td><td><strong>8 B</strong></td><td><code>48 83 3d ?? ?? ?? ?? 00</code></td><td><code>cmp qword ptr [rip + disp32], 0</code></td><td><strong>Edited instruction #1 (<code>edit.offset = 38</code>)</strong>: the <code>g_multiplayer</code> null guard</td></tr><tr><td><strong><code>+46 .. +51</code></strong></td><td><strong>6 B</strong></td><td><code>0f 84 ?? ?? ?? ??</code></td><td><code>jz rel32</code> (to <code>Unit::changeAircraft</code> call)</td><td><strong>Edited instruction #2</strong>: 2-byte opcode <code>0f 84</code> (<code>+46..+47</code>) + 4-byte <code>rel32</code> at <strong><code>+48..+51</code></strong>, captured as <code>spawn_call</code> (<code>offset: 48</code>)!</td></tr><tr><td><code>+52 .. +56</code></td><td>5 B</td><td><code>48 8b 54 24 20</code></td><td><code>mov rdx, qword ptr [rsp + 0x20]</code></td><td>Trailing context anchor verifying exact instruction boundary after <code>+51</code></td></tr></tbody></table></div>
|
||
<p>Notice the <strong>five key mechanics</strong> working together here:</p>
|
||
<ol><li><strong>Surrounding anchors guarantee a unique hit</strong>: <code>48 83 3d ?? ?? ?? ?? 00 0f 84 ?? ?? ?? ??</code> alone appears hundreds of times across <code>.text</code>, whereas anchoring <code>+0..+37</code> before it and <code>+52..+56</code> after it narrows 80 MB of code down to a single unambiguous hit.</li><li><strong><code>edit.offset: 38</code> leaves the anchors untouched</strong>: Only the 14 bytes at <code>+38..+51</code> are overwritten.</li><li><strong><code>captures.spawn_call</code> (<code>offset: 48, type: "rel32"</code>) resolves the jump target dynamically</strong>: The <code>jz</code> instruction's 4-byte displacement sits at <code>+48..+51</code>, so default <code>end = 48 + 4 = 52</code>, yielding target RVA = <code>site + 52 + disp32</code> (<code>0x11b314e</code> on <code>2.61.0.37</code>). Even when future builds shift <code>Unit::changeAircraft</code>, the captured RVA updates automatically.</li><li><strong>Exact 14-byte parity & <code>asm</code> verification</strong>: <code>expect_original</code> is <code>8 + 6 = 14</code> bytes and <code>bytes</code> is <code>5</code> (<code>e9 ?? ?? ?? ??</code>) + <code>9</code> (<code>90</code> NOPs) = <strong>14 bytes</strong>. Running <code>wtotools check-patches</code> assembles <code>asm</code> and verifies byte-for-byte agreement with <code>bytes</code>.</li><li><strong>Crucial offset base distinction (most common beginner mistake!)</strong>:
|
||
<ul><li><code>edit.offset</code> (<code>38</code>) and <code>locate.captures.*.offset</code> (<code>48</code>) count from <strong><code>site</code> (<code>+0</code> of <code>locate.pattern</code>)</strong>;</li><li><code>edit.fixups[].at</code> (<code>1</code>) and <code>edit.fixups[].end</code> (default <code>at + 4 = 5</code>) count from <strong>byte <code>0</code> of <code>edit.bytes</code> (<code>site + 38</code>)</strong>, right after the <code>e9</code> opcode!</li></ul></li></ol>
|
||
<h4 id="ch-03-en-3-3-reference-the-rel32-end-trap-all-7-capture-types-rdata-edits">3.3 Reference: The <code>rel32</code> <code>"end"</code> Trap, All 7 Capture Types & <code>.rdata</code> Edits</h4>
|
||
<h5 id="ch-03-en-1-the-rel32-end-trap-instructions-with-trailing-immediate-bytes">(1) The <code>rel32</code> <code>"end"</code> Trap (Instructions with Trailing Immediate Bytes!)</h5>
|
||
<p>On x86-64, RIP-relative addressing always adds <code>disp32</code> to the <strong>address of the next instruction (where the current instruction ends)</strong>, NOT where the 4-byte <code>disp32</code> ends!</p>
|
||
<ul><li>For <code>mov rax, [rip + disp32]</code> (<code>48 8b 05 ?? ?? ?? ??</code>, 7 B) or <code>jz rel32</code> (<code>0f 84 ?? ?? ?? ??</code>, 6 B), <code>disp32</code> is at the very end of the instruction, so the default <code>end = offset + 4</code> matches the instruction end automatically.</li><li><strong>However, when an instruction has trailing immediate byte(s) after <code>disp32</code></strong>, omitting <code>"end"</code> resolves the wrong address! Take <code>c6 05 ?? ?? ?? ?? 00</code> (<code>mov byte ptr [rip + disp32], 0</code>): the instruction is <strong>7 bytes</strong> total (<code>c6 05</code> at <code>0..1</code>, <code>disp32</code> at <code>2..5</code>, trailing immediate <code>00</code> at <code>6</code>, ending at <strong><code>7</code></strong>). Writing <code>{ "offset": 2, "type": "rel32" }</code> defaults <code>end</code> to <code>2 + 4 = 6</code>, landing <strong>1 byte before</strong> the real global variable! You <strong>must</strong> pass <code>"end": 7</code>:
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>jsonc</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>"captures": {
|
||
"some_flag": { "offset": 2, "type": "rel32", "end": 7 }
|
||
}</code></pre></div>
|
||
<p>For a real example in the repo, see <code>disabled_slots_mask.multiplayer</code> in <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/patches/native/local_mp_core.jsonc" target="_blank" rel="noopener"><code>patches/native/local_mp_core.jsonc</code></a>: after <code>31 c0</code> (<code>+0..+1</code>), <code>48 83 3d ?? ?? ?? ?? 00</code> (<code>cmp qword ptr [rip + disp32], 0</code>) spans <code>+2..+9</code> with <code>disp32</code> at <code>offset: 5</code> and trailing <code>00</code> at <code>+9</code>, so it specifies <code>"end": 10</code>.</p></li></ul>
|
||
<h5 id="ch-03-en-2-all-7-capture-types-packages-exepatch-src-captures-ts">(2) All 7 Capture Types (<code>packages/exepatch/src/captures.ts</code>)</h5>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th><code>type</code></th><th>Width</th><th>How it is read</th><th>Typical use</th></tr></thead><tbody><tr><td><code>"rel32"</code></td><td>4 B (signed LE)</td><td>Computes target RVA = <code>site + end + int32</code></td><td>Target of <code>call</code>, <code>jmp</code>/<code>jcc</code>, or <code>[rip + disp32]</code> global variable</td></tr><tr><td><code>"u8"</code></td><td>1 B (unsigned)</td><td>Raw <code>0..255</code></td><td>Compact 8-bit struct field offset (e.g., <code>[r8 + 0x61]</code>)</td></tr><tr><td><code>"u16"</code></td><td>2 B (unsigned LE)</td><td>Raw <code>0..65535</code></td><td>16-bit wire/MPI message IDs</td></tr><tr><td><code>"u32"</code></td><td>4 B (unsigned LE)</td><td>Raw 32-bit unsigned integer</td><td>Standard C++ struct member offsets (e.g., <code>[rax + 0x6d8]</code>) or array strides</td></tr><tr><td><code>"i8"</code></td><td>1 B (signed)</td><td>Raw <code>-128..127</code></td><td>Signed 1-byte pointer biases in loop iterators</td></tr><tr><td><code>"i16"</code></td><td>2 B (signed LE)</td><td>Raw <code>-32768..32767</code></td><td>Signed 16-bit displacements</td></tr><tr><td><code>"i32"</code></td><td>4 B (signed LE)</td><td>Raw 32-bit signed integer</td><td>Signed 32-bit stack or struct displacements</td></tr></tbody></table></div>
|
||
<h5 id="ch-03-en-3-constant-string-data-edits-in-rdata">(3) Constant String / Data Edits in <code>.rdata</code></h5>
|
||
<p>When replacing a fixed-length string in <code>.rdata</code> instead of assembly instructions, use <code>"data"</code> in place of <code>"asm"</code> (real example: <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/patches/exe/online/network-hook-library/patch.jsonc" target="_blank" rel="noopener"><code>patches/exe/online/network-hook-library/patch.jsonc</code></a>):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>jsonc</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>"edit": {
|
||
"offset": 0,
|
||
"data": {
|
||
"encoding": "ascii",
|
||
"text": "wto_ws.dll"
|
||
},
|
||
"bytes": "77 74 6f 5f 77 73 2e 64 6c 6c 00",
|
||
"expect_original": "57 53 32 5f 33 32 2e 64 6c 6c 00"
|
||
}</code></pre></div>
|
||
<p>Both <code>bytes</code> and <code>expect_original</code> include the terminating <code>00</code> byte (11 bytes total), and <code>wtotools check-patches</code> verifies that <code>data</code> encodes identically to <code>bytes</code>.</p>
|
||
<hr>
|
||
<h3 id="ch-03-en-4-native-modules-patches-native-native-wto-net-locators-complete-6-file-wiring-walkthrough">4. Native Modules (<code>patches/native/</code> + <code>native/wto-net/</code>) — Locators & Complete 6-File Wiring Walkthrough</h3>
|
||
<p>When writing MinHook detours or calling engine functions in <code>wto_ws.dll</code>, <strong>never put a single build-specific RVA or game struct offset in C code</strong>. Instead, declare sites in <code>patches/native/<module>.jsonc</code>, let the patcher emit resolved hex values into <code>win64/wto_ws.cfg</code>, and read them in C from <code>extern LmpValues g_lmp</code>.</p>
|
||
<h4 id="ch-03-en-4-1-all-three-locate-methods-signature-stringref-within">4.1 All Three Locate Methods (<code>signature</code>, <code>string_ref</code>, <code>within</code>)</h4>
|
||
<ol><li><strong><code>"method": "signature"</code></strong>: Scans <code>".text"</code> (or a specified <code>"section"</code>) for a unique byte pattern, supporting <code>captures</code>.</li><li><strong><code>"method": "string_ref"</code></strong> (<code>packages/exepatch/src/locate/string-ref.ts</code> — <strong>most resilient across compiler codegen shifts!</strong>): Finds UTF-8 <code>string + \0</code> in <code>".rdata"</code> (or <code>"section"</code>) and locates 7-byte RIP-relative <code>lea</code> (<code>48 8d</code> / <code>4c 8d</code>) instructions loading that string in <code>.text</code>. Supports <strong>three modes</strong>:
|
||
<ul><li><strong>Mode A (<code>"target": "reference"</code>, default without <code>"pattern"</code>)</strong>: Returns the RVA of the 7-byte <code>lea</code> instruction itself (checked length = 7 bytes);</li><li><strong>Mode B (<code>"target": "function"</code>)</strong>: Looks up the enclosing function's start RVA in the PE <code>.pdata</code> exception directory (checked length = 0, so no direct <code>captures</code>—ideal for locating function entry points to hook or use as <code>within</code> anchors);</li><li><strong>Mode C (<code>"pattern"</code> + <code>"lea_at"</code>, with default <code>"target": "reference"</code>)</strong>: Matches <code>pattern</code> around the string reference where the 7-byte <code>lea</code> sits at byte offset <code>lea_at</code>, returning the <code>pattern</code> start RVA so <code>captures</code> can read adjacent struct offsets right next to the string reference!</li></ul></li><li><strong><code>"method": "within"</code></strong> (<code>packages/exepatch/src/locate/within.ts</code>): Searches <code>pattern</code> strictly within the <code>[begin, end)</code> PE <code>.pdata</code> bounds of an already-resolved site named in <code>"in"</code> (from an earlier site in the same module or a <code>requires</code> module), allowing short field-access patterns to match uniquely inside one function.</li></ol>
|
||
<h4 id="ch-03-en-4-2-complete-6-file-wiring-walkthrough-localmpscores">4.2 Complete 6-File Wiring Walkthrough: <code>local_mp_scores</code></h4>
|
||
<p>Here is how <code>local_mp_scores</code> (which reads per-event scoreboard points placed by <code>apps/server</code> into <code>MissionSettings.customScoreParams</code>) connects end-to-end across <strong>all 6 real files</strong>:</p>
|
||
<h5 id="ch-03-en-file-1-patches-native-localmpscores-jsonc">File 1: <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/patches/native/local_mp_scores.jsonc" target="_blank" rel="noopener"><code>patches/native/local_mp_scores.jsonc</code></a></h5>
|
||
<p>The filename must match <code>"module"</code>. Because <code>"mission_settings_load"</code> is already resolved via <code>string_ref</code> (<code>"string": "customScoreParams", "target": "function"</code>) in <code>local_mp_core.jsonc</code>, <code>"local_mp_scores"</code> lists <code>"local_mp_core"</code> in <code>"requires"</code> and searches inside <code>"mission_settings_load"</code> using <code>"within"</code>:</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>jsonc</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>// Score table of a LocalMP battle (native/wto-net/src/local_mp_scores.c): the local server puts it
|
||
// in the mission's customScoreParams; the host reads it there.
|
||
// Without it: kills, assists and captures score nothing.
|
||
{
|
||
"module": "local_mp_scores",
|
||
"features": [
|
||
"K-Kill, assist and capture scoring"
|
||
],
|
||
"requires": [
|
||
"local_mp_core"
|
||
],
|
||
"sites": {
|
||
// DataBlock::getReal wrapper.
|
||
// 2.61.0.37: 0x5445270
|
||
"datablock_get_real": {
|
||
"variants": [
|
||
{
|
||
"written_for": "2.61.0.37",
|
||
"locate": {
|
||
"method": "signature",
|
||
"pattern": "48 83 ec 28 f3 0f 11 54 24 24 4c 8d 44 24 24 e8 ?? ?? ?? ?? 90 48 83 c4 28 c3 cc cc cc cc cc cc 56 57 53 48 83 ec 30 48 89 d7 48 89 ce 48 8d 15"
|
||
}
|
||
}
|
||
]
|
||
},
|
||
// MissionSettings customScoreParams copy.
|
||
// 2.61.0.37: 0x126d762 (lea rcx, [rax + 0x6d8]; mov eax, dword ptr [r13 + 0x10]; cmp eax, -1; ...)
|
||
"mission_score_params": {
|
||
"variants": [
|
||
{
|
||
"written_for": "2.61.0.37",
|
||
"locate": {
|
||
"method": "within",
|
||
"in": "mission_settings_load",
|
||
"pattern": "48 8d 88 ?? ?? ?? ?? 41 8b 45 10 83 f8 ff 74 ?? 45 0f b7 45 0e 46 8d 0c 00 31 d2 44 39 c8 0f 8d ?? ?? ?? ?? ff c7 4d 8b 4d 00 45 8b 51 5c 4d 01 d1 49 81 c1 90 00 00 00 41 bb ff ff ff 3f 45 31 d2 66 66 66 66 2e 0f 1f 84 00 00 00 00 00 42 8d 34 10 48 c1 e6 05 41 8b 5c 31 08 44 21 db 39 fb 74 ?? 41 ff c2 45 39 d0 75 ?? eb ?? 41 0f b7 45 0e 31 d2 85 c0 74 ?? ff c7 4d 8b 4d 18 4d 8b 01 45 8b 49 08 44 8d 14 c5 00 00 00 00 45 29 d1 41 ba ff ff ff 3f 45 31 db 66 0f 1f 44 00 00 4b 8b 34 08 8b 76 08 44 21 d6 39 fe 74 ?? 41 ff c3 49 83 c1 08 44 39 d8 75 ?? eb ?? 49 01 f1 31 d2 45 39 c2 49 0f 42 d1 eb ?? 41 39 c3 73 ?? 44 89 c8 49 8b 14 00 eb ?? 31 d2 48 85 d2 49 0f 44 d6 31 f6 45 31 c0 e8 ?? ?? ?? ?? 49 8b 5d 00 bf ff ff ff ff 48 85 db 74 ?? 48 8d 0d ?? ?? ?? ?? ba 16 00 00 00",
|
||
"captures": {
|
||
"custom_score_params": {
|
||
"offset": 3,
|
||
"type": "u32"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
]
|
||
}
|
||
}
|
||
}</code></pre></div>
|
||
<p>In <code>48 8d 88 ?? ?? ?? ??</code> (<code>lea rcx, [rax + disp32]</code>), bytes <code>0..2</code> are <code>48 8d 88</code> and bytes <code>3..6</code> hold the struct offset (<code>0x6d8</code> on <code>2.61.0.37</code>), captured via <code>"offset": 3, "type": "u32"</code>.</p>
|
||
<h5 id="ch-03-en-file-2-generated-copy-win64-wtows-cfg-packages-patcher-src-native-native-settings-ts">File 2: Generated <code><copy>/win64/wto_ws.cfg</code> (<code>packages/patcher/src/native/native-settings.ts</code>)</h5>
|
||
<p>During copy builds, the patcher writes the executable's PE identity stamps alongside <code><site></code> and <code><site>.<capture></code> entries for all completely resolved modules:</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>ini</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>port=27015
|
||
exe.time_date_stamp=0x67e5a1b2
|
||
exe.size_of_image=0x83f4000
|
||
native.datablock_get_real=0x5445270
|
||
native.mission_score_params=0x126d762
|
||
native.mission_score_params.custom_score_params=0x6d8</code></pre></div>
|
||
<h5 id="ch-03-en-file-3-native-wto-net-src-localmpvalues-h">File 3: <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/native/wto-net/src/local_mp_values.h" target="_blank" rel="noopener"><code>native/wto-net/src/local_mp_values.h</code></a></h5>
|
||
<p>Add pointer (<code>uint8_t *</code>), unsigned offset (<code>size_t</code>), or signed displacement (<code>ptrdiff_t</code>) fields to <code>struct LmpValues</code>:</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>c</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>typedef struct LmpValues {
|
||
/* ... other modules ... */
|
||
|
||
/* local_mp_scores */
|
||
uint8_t *datablock_get_real;
|
||
size_t mis_custom_score_params;
|
||
} LmpValues;
|
||
|
||
extern LmpValues g_lmp;</code></pre></div>
|
||
<h5 id="ch-03-en-file-4-native-wto-net-src-localmpvalues-c">File 4: <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/native/wto-net/src/local_mp_values.c" target="_blank" rel="noopener"><code>native/wto-net/src/local_mp_values.c</code></a></h5>
|
||
<p>Wire the <code>wto_ws.cfg</code> keys into <code>SPECS[]</code> using the three storage macros:</p>
|
||
<ul><li><strong><code>A(module, name, field)</code></strong> (<code>LMP_ADDRESS</code>): computes <code>(uint8_t *)GetModuleHandleW(NULL) + rva</code> and stores the live process pointer;</li><li><strong><code>O(module, name, field)</code></strong> (<code>LMP_OFFSET</code>): stores an unsigned <code>size_t</code> struct offset or stride;</li><li><strong><code>S(module, name, field)</code></strong> (<code>LMP_SIGNED</code>): stores a signed <code>ptrdiff_t</code> displacement;</li><li>Use <code>derive_values()</code> in the same file when a relative offset must be computed from two captured values (e.g., subtracting <code>econ_change_req</code> from an outer <code>econ_req_*</code> offset).</li></ul>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>c</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>#define SCORES "local_mp_scores"
|
||
|
||
static const LmpValueSpec SPECS[] = {
|
||
/* ... */
|
||
A(SCORES, "datablock_get_real", datablock_get_real),
|
||
O(SCORES, "mission_score_params.custom_score_params", mis_custom_score_params),
|
||
/* ... */
|
||
};</code></pre></div>
|
||
<h5 id="ch-03-en-file-5-native-wto-net-src-localmpoffsets-h">File 5: <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/native/wto-net/src/local_mp_offsets.h" target="_blank" rel="noopener"><code>native/wto-net/src/local_mp_offsets.h</code></a></h5>
|
||
<p>Expose a descriptive macro backed by <code>g_lmp</code>:</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>c</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>#define LMP_MIS_OFF_CUSTOM_SCORE_PARAMS (g_lmp.mis_custom_score_params)</code></pre></div>
|
||
<h5 id="ch-03-en-file-6-localmpscores-c-localmpcore-c-native-wto-net-build-ps1">File 6: <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/native/wto-net/src/local_mp_scores.c" target="_blank" rel="noopener"><code>local_mp_scores.c</code></a>, <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/native/wto-net/src/local_mp_core.c" target="_blank" rel="noopener"><code>local_mp_core.c</code></a> & <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/native/wto-net/build.ps1" target="_blank" rel="noopener"><code>native/wto-net/build.ps1</code></a></h5>
|
||
<ul><li>Consume the resolved function and offset in <code>local_mp_scores.c</code>:
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>c</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>typedef float (__fastcall *DataBlockGetRealFn)(const void *blk, const char *name, float def);
|
||
static DataBlockGetRealFn fn_datablock_get_real;
|
||
|
||
float local_mp_score_param(const char *key)
|
||
{
|
||
if (fn_datablock_get_real == NULL || g_local_mp_mission_settings == NULL || key == NULL)
|
||
return 0.0f;
|
||
return fn_datablock_get_real(g_local_mp_mission_settings + LMP_MIS_OFF_CUSTOM_SCORE_PARAMS, key, 0.0f);
|
||
}
|
||
|
||
int install_local_mp_score_reader(void)
|
||
{
|
||
fn_datablock_get_real = (DataBlockGetRealFn)g_lmp.datablock_get_real;
|
||
return 1;
|
||
}</code></pre></div></li><li>In <code>local_mp_core.c</code>, <code>install_local_mp_hooks()</code> first calls <code>config_native_matches_executable()</code> to verify that the running <code>aces.exe</code>'s PE <code>TimeDateStamp</code> and <code>SizeOfImage</code> match <code>wto_ws.cfg</code> (disabling the entire <code>LocalMP</code> bridge if <code>wto_ws.cfg</code> came from a different build), loads <code>local_mp_core</code>, iterates <code>MODULES[]</code> in dependency order (<code>{ "local_mp_scores", install_local_mp_score_reader }</code>) calling <code>lmp_values_load(MODULES[i].name)</code> before <code>install()</code>, and enables all queued hooks in one pass via <code>MH_ApplyQueued()</code>.</li><li><strong>Adding a brand-new <code>.c</code> file</strong>: Declare its installer in <code>local_mp_internal.h</code>, register it in <code>MODULES[]</code> (<code>local_mp_core.c</code>), <strong>and add the <code>.c</code> file to <code>$sources</code> in <code>native/wto-net/build.ps1</code></strong>.</li><li><strong>Sharing common engine hooks</strong>: When two modules need the same target function (e.g., <code>unit_on_event</code> shared by <code>local_mp_damage_notifications</code> and <code>local_mp_air_events</code>), define and dispatch it once in <code>local_mp_core</code> rather than calling <code>MH_CreateHook</code> twice on the same address.</li></ul>
|
||
<hr>
|
||
<h3 id="ch-03-en-5-adapting-a-new-client-build-4-step-workflow">5. Adapting a New Client Build (4-Step Workflow)</h3>
|
||
<p>When War Thunder releases a minor client update (e.g., <code>2.61.0.37</code> -> <code>2.61.0.58</code>), follow these four steps:</p>
|
||
<ol><li><strong>Run the build recorder against your read-only stock client</strong>:
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code> node scripts/record-game-version.ts --game "<stock-dir>"</code></pre></div>
|
||
<p><code>record-game-version.ts</code> tests every <code>patches/exe/</code>, <code>patches/gui/</code>, and <code>patches/native/</code> definition without modifying the stock folder. If any essential safety patch (<code>ESSENTIAL_FEATURES</code>) fails, it refuses to write a record and prints the blocking failures; otherwise it writes <code>patches/versions/<version>.jsonc</code> and lists any <code>unavailable</code> optional patches/modules.</p></li><li><strong>Add new variants only for failing patches/sites</strong>: Inspect the updated <code>.nut</code> script or <code>aces.exe</code> function using the Step 0 Node snippet or <code>tools/re/cli.py</code> (prefer <code>find-string</code> anchors!). <strong>Keep existing <code>variants</code> untouched</strong> so earlier builds continue working, and append a new variant with <code>"written_for": "<new-version>"</code>.</li><li><strong>Run the full verification suite</strong>: Re-run <code>record-game-version.ts</code> to confirm zero <code>unavailable</code> items, then run:
|
||
<ul><li><code>.\.venv\Scripts\python.exe -m wtotools.cli check-patches --exe "<stock-dir>\win64\aces.exe"</code></li><li><code>.\.venv\Scripts\python.exe -m wtotools.cli check-gui --gui "<stock-dir>\gui.vromfs.bin" --sq "build\quirrel-cli\bin\Release\sq.exe"</code></li><li><code>powershell -NoProfile -File .\native\wto-net\build.ps1</code> (if any C file changed)</li><li><code>node scripts/build-game-copy.ts</code> into a temporary copy directory to confirm clean assembly.</li></ul></li><li><strong>Verify live in-game inside an isolated copy BEFORE committing <code>patches/versions/<version>.jsonc</code></strong>: The launcher relies on <code>patches/versions/<version>.jsonc</code> to badge a client build as verified. Boot the isolated copy, sign into the hangar, and play through a <code>LocalMP</code> battle before committing <code>patches/versions/<version>.jsonc</code> rather than relying on CLI checks alone.</li></ol>
|
||
<p>Starter templates for new patches live under <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/dev-docs/templates/gui/patch.jsonc" target="_blank" rel="noopener"><code>dev-docs/templates/gui/</code></a>, <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/dev-docs/templates/exe/patch.jsonc" target="_blank" rel="noopener"><code>dev-docs/templates/exe/</code></a>, and <a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/dev-docs/templates/native/local_mp_example.jsonc" target="_blank" rel="noopener"><code>dev-docs/templates/native/</code></a>.</p>
|
||
</div>
|
||
<div class="doc-pager">
|
||
<a class="doc-pager-btn" href="#ch-02" data-chapter="ch-02"><span class="doc-pager-label"><span class="lang-zh">← 上一章</span><span class="lang-en">← Previous</span></span><span class="doc-pager-title"><span class="lang-zh">02 · 架构原理、分层与好坏代码</span><span class="lang-en">02 · Architecture, Layers & Patterns</span></span></a>
|
||
<a class="doc-pager-btn next" href="#ch-04" data-chapter="ch-04"><span class="doc-pager-label"><span class="lang-zh">下一章 →</span><span class="lang-en">Next →</span></span><span class="doc-pager-title"><span class="lang-zh">04 · 环境搭建、流量日志与检查</span><span class="lang-en">04 · Setup, Traffic Logs & Checks</span></span></a>
|
||
</div>
|
||
</article>
|
||
|
||
<article class="doc-section doc-chapter" id="ch-04">
|
||
<div class="lang-zh">
|
||
<h2>04 · 环境搭建、本地流量日志排错与检查命令</h2>
|
||
<p>这篇指南带你从零搭好本地开发环境、用内置的自动解码流量日志快速定位机库与对局问题,并在发起 Pull Request 前跑通对应的真实工程检查。</p>
|
||
<hr>
|
||
<h3 id="ch-04-zh-1-开发环境准备">1. 开发环境准备</h3>
|
||
<p>你需要一台 <strong>Windows x64</strong> 电脑,并安装以下基础工具:</p>
|
||
<ol><li><strong>Node.js 24+</strong>:仓库内的 CLI 脚本与 <code>apps/server</code> 直接以 Node 24 原生类型剥离模式运行 <code>.ts</code> 源码(Node 20/22 旧版本会因 <code>.ts</code> 扩展名或模块解析报错)。</li><li><strong>Visual Studio 2022 或 Build Tools(勾选「使用 C++ 的桌面开发」x64 工具链与 CMake)</strong>:用于通过 <code>cl.exe</code> 编译 <code>native/wto-net/</code>(生成 <code>wto_ws.dll</code>),以及一次性编译 Quirrel 字节码检查器 <code>sq.exe</code>。</li><li><strong>Python 3.12+</strong>:用于运行补丁校验器 <code>wtotools</code>、静态逆向工具 <code>tools/re/cli.py</code> 与客户端 <code>.clog</code> 日志解密命令。</li><li><strong>一份玩家本人合法持有的官方原版客户端(严格保持只读)</strong>,以及在 Steam 游戏库目录之外单独准备的<strong>离线副本目录</strong>与<strong>本地服务器数据目录</strong>。</li></ol>
|
||
<p>首次克隆仓库后,在仓库根目录依次执行:</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code># 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</code></pre></div>
|
||
<p>如果你想脱离桌面启动器,直接从命令行快速构建一份离线副本并以前台模式启动本地服务器查看实时输出(注意:<strong><code>--game</code> 在构建时传只读原版目录、启动服务器时传离线副本目录,两条命令的 <code>--data</code> 与 <code>--port</code> 必须完全一致</strong>):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>node scripts/build-game-copy.ts --game "<原版目录>" --out "<离线副本目录>" --data "<独立数据目录>" --port 27015
|
||
node apps/server/src/cli.ts --game "<离线副本目录>" --data "<独立数据目录>" --port 27015</code></pre></div>
|
||
<hr>
|
||
<h3 id="ch-04-zh-2-怎么调试与查日志-排查-bug-最快的路径">2. 怎么调试与查日志(排查 Bug 最快的路径)</h3>
|
||
<p>排查机库动作报错、购买失败、匹配卡住或对局计分异常时,<strong>千万不要靠盲猜客户端发了什么包</strong>。WTOffline 在四处关键节点都留有可直接阅读的诊断日志:</p>
|
||
<h4 id="ch-04-zh-2-1-本地服务器流量日志-数据目录-logs-启动时间戳-第一站">2.1 本地服务器流量日志:<code><数据目录>/logs/<启动时间戳>/</code>(第一站!)</h4>
|
||
<p>每次启动 <code>apps/server</code>,<code>TrafficLog</code>(<code>apps/server/src/traffic-log/traffic-log.ts</code>)都会在 <code><数据目录>/logs/</code> 下新建一个带 ISO 时间戳的会话文件夹,里面包含两部分:</p>
|
||
<ol><li><strong><code>requests.jsonl</code>(全量请求索引流)</strong> 每行是一条 JSON 记录,按全局递增序号 <code>id</code> 记录每一次 HTTP 交互(<code>kind: "http"</code>)、每一条匹配服双向消息(<code>kind: "matching"</code>)、被拒绝的 WebSocket 升级请求(<code>kind: "websocket"</code>)以及连接开关事件:
|
||
<ul><li><strong>一秒定位未实现接口</strong>:直接在 <code>requests.jsonl</code> 中搜索 <code>"unhandled":true</code>(或看终端输出末尾带 <code>[UNHANDLED]</code> 标记的行),就能立刻抓出客户端刚发了哪个尚未挂载处理器的 <code>char</code> 动作、<code>yuplay2</code> 路径或 <code>matching</code> 消息!</li><li>每条记录中的 <code>files</code> 字段直接指明该次请求与响应对应的负载文件名。</li></ul></li><li><strong><code>bodies/</code> 子目录(自动解码的可读明文负载!)</strong> 每个序号 <code>id</code>(补零至 6 位,如 <code>000042</code>)按需生成以下文件:
|
||
<ul><li><code>000042.req.bin</code> / <code>000042.resp.bin</code>:原始线上传输字节(登录表单中的密码字段会自动脱敏);</li><li><strong><code>000042.req.txt</code> / <code>000042.resp.txt</code></strong>:<strong>自动解包并格式化好的可读明文 BLK 文本</strong>(无论是带 <code>0x69</code> / zlib / zstd 标记的 <code>char</code> 二进制 BLK,还是 <code>cln_bulk_set_gblk</code> 上传的 <code>PBBD</code> 多段批处理流,都会自动转成缩进清晰的文本 BLK!)或 UTF-8 JSON 文本——你无需手写任何解包脚本,直接用编辑器打开 <code>.txt</code> 就能看清每一个字段与类型;</li><li><strong><code>000042.matching-in.json</code> / <code>000042.matching-out.json</code></strong>:解密并解压后的匹配长连接双向 RPC 消息(<code>in</code> 为客户端发来,<code>out</code> 为本地服务器下发)。</li></ul></li></ol>
|
||
<h4 id="ch-04-zh-2-2-其余三类配套诊断日志">2.2 其余三类配套诊断日志</h4>
|
||
<ul><li><strong>进程内钩子与 <code>LocalMP</code> 日志:<code><离线副本目录>/win64/wto_ws.log</code></strong> 由 <code>wto_ws.dll</code> 每次启动游戏时重写。记录本地服务器端口号、每一次域名拦截与 Socket 连接重定向结果、<code>wto_ws.cfg</code> 与当前 <code>aces.exe</code> 的 PE 头校验结果,以及 <strong>11 个 <code>local_mp_*</code> 模块中哪些成功安装、哪些因缺少 <code>wto_ws.cfg</code> 键值被优雅跳过</strong>(并打印缺失的具体键名)。排查局内不刷 AI、选车无响应、记分牌为 0 或连杀飞机异常时先看这里。</li><li><strong>游戏引擎客户端日志:<code><离线副本目录>/.game_logs/*.clog</code></strong> Dagor 引擎原生日志,记录 Quirrel 脚本编译/运行期报错、进机库卡死在「正在加载…」的具体栈信息、任务脚本报错与资源缺失。由于 <code>.clog</code> 是二进制异或加密格式,直接用 <code>tools/re/cli.py</code> 解密成纯文本再查阅:
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>.\.venv\Scripts\python.exe tools\re\cli.py decrypt-clog "<离线副本目录>\.game_logs\<最新文件>.clog" --out "<临时目录>\game.clog.txt"</code></pre></div></li><li><strong>桌面启动器日志:<code>launcher.log</code></strong> 在启动器界面打开「设置 → 诊断 → 打开日志目录」即可查看,记录副本构建、补丁包校验、子进程启停与更新器状态。</li></ul>
|
||
<hr>
|
||
<h3 id="ch-04-zh-3-提交前必跑的工程检查命令-按改动范围对照执行">3. 提交前必跑的工程检查命令(按改动范围对照执行)</h3>
|
||
<p>WTOffline <strong>不要求也不提倡编写庞大的模拟(Mock)单元测试套件</strong>,而是要求你按实际触碰的代码目录,跑通下面对应的<strong>真实编译器、补丁字节核对与 Quirrel 字节码检查</strong>;最终交互体验则在独立隔离副本中进游戏实机验收。</p>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th>你修改了哪里</th><th>提交前必须跑通的检查</th></tr></thead><tbody><tr><td>共享库 <code>packages/*</code>、本地服务器 <code>apps/server</code> 或 <code>scripts/*</code></td><td>根目录 <code>npm run typecheck</code>;若动了服务端启动逻辑或 <code>tuning/*.jsonc</code>,用临时数据目录启动一次 <code>apps/server/src/cli.ts</code> 确认数据集提取与配置校验零报错</td></tr><tr><td>桌面启动器 <code>apps/launcher</code></td><td>启动器 <code>typecheck</code> + <code>electron-vite build</code>;若增改了界面多语言词条,确认 <code>en.json</code> 与 <code>zh-CN.json</code> 键名完全对齐</td></tr><tr><td><code>patches/exe/</code>(任何 <code>patch.jsonc</code>)</td><td><code>wtotools check-patches</code>(验证特征码唯一命中、<code>expect_original</code> 匹配,且 <code>asm</code>/<code>data</code> 编译结果与 <code>bytes</code> 逐字节一致)</td></tr><tr><td><code>patches/gui/</code>(任何 <code>patch.jsonc</code> 或 <code>.nut</code> 片段)</td><td><strong>完整</strong> <code>wtotools check-gui</code>(调用 <code>sq.exe</code> 执行 <code>compilestring</code> 词法作用域与字节码编译检查) + 临时副本构建确认 <code>gui-patch-definition.ts</code> Schema 无未知键报错</td></tr><tr><td><code>native/wto-net/</code> 或 <code>patches/native/</code></td><td>MSVC <code>nativewto-netbuild.ps1</code>(<code>/W4</code> <strong>零警告</strong>)+ 临时目录 <code>scripts/build-game-copy.ts</code> 确认所有原生站点全部解析就绪</td></tr><tr><td>适配客户端新版本(<code>patches/versions/</code>)</td><td><code>scripts/record-game-version.ts</code> + 上述全部适用检查 + <strong>在隔离副本中实机登录进机库并完成 <code>LocalMP</code> 对局验收后</strong>,方可提交 <code>patches/versions/<版本号>.jsonc</code></td></tr></tbody></table></div>
|
||
<p>常用检查命令汇总(均在仓库根目录执行,将尖括号替换为你的本地路径):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code># 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 "<原版目录>"</code></pre></div>
|
||
<h4 id="ch-04-zh-一次性编译-quirrel-字节码检查器-sq-exe-注意-sq-路径参数">一次性编译 Quirrel 字节码检查器 <code>sq.exe</code>(注意 <code>--sq</code> 路径参数!)</h4>
|
||
<p><code>check-gui</code> 需要调用与《战争雷霆》客户端同代的官方 Quirrel 编译器(固定在 <a href="https://github.com/GaijinEntertainment/quirrel/tree/19f17df9b6ee43c91ef5c635fd45970ec76c5dbe" target="_blank" rel="noopener">Quirrel 仓库提交 <code>19f17df9b6ee43c91ef5c635fd45970ec76c5dbe</code></a>)。使用 Visual Studio 2022 C++ 工具链与 CMake 编译一次即可长期复用:</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>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</code></pre></div>
|
||
<blockquote><p><strong>关于 <code>--sq</code> 参数的重要说明</strong>:<code>wtotools check-gui</code> 在不传 <code>--sq</code> 时的默认查找路径是 <code>build/quirrel-check/sq.exe</code>(见 <code>tools/src/wtotools/cli/gui_commands.py</code>),而上面这条 Visual Studio CMake 多配置构建命令会把可执行文件生成在 <strong><code>build\quirrel-cli\bin\Release\sq.exe</code></strong>。因此运行 <code>check-gui</code> 时请像上方命令一样显式传入 <code>--sq "build\quirrel-cli\bin\Release\sq.exe"</code>(或者手动把生成的 <code>sq.exe</code> 复制一份到 <code>build\quirrel-check\sq.exe</code>)。</p></blockquote>
|
||
<hr>
|
||
<h3 id="ch-04-zh-4-提交消息规范与-pr-自查清单">4. 提交消息规范与 PR 自查清单</h3>
|
||
<ul><li><strong>一笔 PR 只聚焦一个清晰目标</strong>:不要在修某个结算 Bug 的 PR 里顺手重排无关目录或大面积格式化其他文件。</li><li><strong>全英文约定式提交(Conventional Commits)与英文注释</strong>:
|
||
<ul><li>提交标题格式:<code>type(scope): concise summary</code>(例如 <code>fix(battle): credit Ground Arcade air event kills from AI victims</code>、<code>feat(launcher): show missing optional patch features</code>;仓库级工程或文档改动用 <code>repo</code> 或 <code>docs</code>);</li><li>若包含破坏性变更,在 <code>scope</code> 后加 <code>!</code> 并在正文末尾附上 <code>BREAKING CHANGE:</code> 说明;</li><li>代码注释、补丁顶部三行说明、补丁 <code>title</code> 与 Git 提交消息正文统一使用英文;</li><li><code>Co-authored-by</code> 尾注仅用于署名实际参与协作的人类开发者,不要填写 AI 模型或编程工具。</li></ul></li><li><strong>发起 PR 前最后 5 项自检</strong>:
|
||
<ol><li>对照 <a href="#ch-02">开发规范第 4 节</a>:零硬编码 RVA/结构体偏移/<code>_ReturnAddress()</code>、零手抄游戏静态数值、零越层 I/O;</li><li>核对语法与 Schema 细节:TS 相对导入全部带 <code>.ts</code> 后缀且遵守 <code>erasableSyntaxOnly</code>;补丁 JSONC 无多余未知键(特别是 <code>patches/gui/**/patch.jsonc</code> 未误写 <code>requires</code>);带尾随立即数的 <code>rel32</code> 捕获已显式填写 <code>"end"</code>;</li><li>涉及货币、研发、库存或存档改动时,确认所有修改均通过 <code>PlayerProfiles</code> / <code>SaveStore</code> <strong>先完成原子写盘再向客户端返回响应</strong>,且多账号目录严格隔离;</li><li>通读一遍 <code>git diff</code>,确认没有带入任何本机个人路径、原版游戏文件、解包产物、测试存档或未脱敏日志;</li><li>完整填写 <a href="#ch-05">PR 模板</a>,如实列出实际运行过的命令与输出结论,并把「已在隔离副本实机验收的界面步骤」与「尚待维护者实机验收的步骤」明确区分开。</li></ol></li></ul>
|
||
</div>
|
||
<div class="lang-en">
|
||
<h2>04 · Setup, Traffic Log Debugging & Check Commands</h2>
|
||
<p>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.</p>
|
||
<hr>
|
||
<h3 id="ch-04-en-1-environment-setup">1. Environment Setup</h3>
|
||
<p>You need a <strong>Windows x64</strong> machine with:</p>
|
||
<ol><li><strong>Node.js 24+</strong>: Repository CLI scripts and <code>apps/server</code> execute <code>.ts</code> files directly via Node 24's native type-stripping (older Node 20/22 releases will fail on <code>.ts</code> import specifiers).</li><li><strong>Visual Studio 2022 or Build Tools (with "Desktop development with C++" x64 tools & CMake)</strong>: Used by <code>native/wto-net/build.ps1</code> (<code>cl.exe</code>) to build <code>wto_ws.dll</code> and once to compile the Quirrel bytecode checker <code>sq.exe</code>.</li><li><strong>Python 3.12+</strong>: Used for the patch verifier (<code>wtotools</code>), static disassembler (<code>tools/re/cli.py</code>), and <code>.clog</code> log decryptor.</li><li><strong>Your own legitimate stock War Thunder installation (kept strictly read-only)</strong>, plus a separate <strong>offline copy folder</strong> and <strong>server data folder</strong> located outside any Steam library directory.</li></ol>
|
||
<p>After cloning the repository, bootstrap everything from the repo root:</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code># 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</code></pre></div>
|
||
<p>To build an offline game copy and run the local server directly from the terminal without the desktop launcher (note: pass the <strong>read-only stock folder</strong> to <code>build-game-copy.ts --game</code>, pass the <strong>offline copy folder</strong> to <code>cli.ts --game</code>, and keep <code>--data</code> and <code>--port</code> identical across both commands):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>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</code></pre></div>
|
||
<hr>
|
||
<h3 id="ch-04-en-2-traffic-log-debugging-diagnostic-logs">2. Traffic Log Debugging & Diagnostic Logs</h3>
|
||
<p>Never guess what payload the game client sent or why a hangar action failed—WTOffline records human-readable diagnostics across four log locations:</p>
|
||
<h4 id="ch-04-en-2-1-local-server-traffic-logs-data-dir-logs-iso-timestamp-check-here-first">2.1 Local Server Traffic Logs: <code><data-dir>/logs/<ISO-timestamp>/</code> (Check Here First!)</h4>
|
||
<p>Every time <code>apps/server</code> starts, <code>TrafficLog</code> (<code>apps/server/src/traffic-log/traffic-log.ts</code>) creates a timestamped session folder under <code><data-dir>/logs/</code> containing:</p>
|
||
<ol><li><strong><code>requests.jsonl</code> (Indexed Exchange Log)</strong> One JSON line per HTTP request/response (<code>kind: "http"</code>), matching message (<code>kind: "matching"</code>), refused WebSocket upgrade (<code>kind: "websocket"</code>), or TCP connection open/close, numbered sequentially by <code>id</code>:
|
||
<ul><li><strong>Spot missing endpoints instantly</strong>: Search <code>requests.jsonl</code> for <code>"unhandled":true</code> (or look for <code>[UNHANDLED]</code> in the server console) to immediately see any unimplemented <code>char</code> action, <code>yuplay2</code> endpoint, or <code>matching</code> RPC!</li><li>Each entry's <code>files</code> object points directly to its corresponding payload files inside <code>bodies/</code>.</li></ul></li><li><strong><code>bodies/</code> Subdirectory (Auto-Decoded Readable BLK & JSON Payloads!)</strong> Numbered by zero-padded sequence ID (e.g., <code>000042.*</code>):
|
||
<ul><li><code>000042.req.bin</code> / <code>000042.resp.bin</code>: Raw wire bytes (with sign-in passwords automatically redacted);</li><li><strong><code>000042.req.txt</code> / <code>000042.resp.txt</code></strong>: <strong>Automatically unpacked and formatted readable BLK text</strong> (covering <code>0x69</code>/zlib/zstd-framed <code>char</code> binary BLK payloads as well as multi-chunk <code>PBBD</code> streams on <code>cln_bulk_set_gblk</code>!) or UTF-8 JSON/form text—open any <code>.txt</code> file directly in your editor to inspect exact block trees and parameter types without manual decoding;</li><li><strong><code>000042.matching-in.json</code> / <code>000042.matching-out.json</code></strong>: Decrypted, LZ4-decompressed inbound (<code>in</code>) and outbound (<code>out</code>) <code>matching</code> binary RPC messages.</li></ul></li></ol>
|
||
<h4 id="ch-04-en-2-2-the-other-three-diagnostic-logs">2.2 The Other Three Diagnostic Logs</h4>
|
||
<ul><li><strong>Native Hook & <code>LocalMP</code> Log: <code><copy-dir>/win64/wto_ws.log</code></strong> Rewritten on every client launch by <code>wto_ws.dll</code>. Logs the active local port, every locally answered hostname and redirected/refused socket, whether <code>wto_ws.cfg</code> matched the running <code>aces.exe</code> PE <code>TimeDateStamp</code> / <code>SizeOfImage</code>, and <strong>which of the 11 <code>local_mp_*</code> modules installed vs. were skipped</strong> (naming the exact missing <code>wto_ws.cfg</code> key when skipped), plus live battle referee events.</li><li><strong>Dagor Client Log: <code><copy-dir>/.game_logs/*.clog</code></strong> Records Quirrel script compilation/runtime errors, exact call stacks behind <code>"Loading..."</code> login hangs, mission errors, and missing assets. Because <code>.clog</code> files are XOR-encrypted binary logs, decrypt them to plain text with <code>tools/re/cli.py</code>:
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>.\.venv\Scripts\python.exe tools\re\cli.py decrypt-clog "<copy-dir>\.game_logs\<latest>.clog" --out "<temp-dir>\game.clog.txt"</code></pre></div></li><li><strong>Desktop Launcher Log: <code>launcher.log</code></strong> Accessible from Launcher <strong>Settings -> Diagnostics -> Open log folder</strong>, covering copy builds, patch-pack checks, server process lifecycle, and updater activity.</li></ul>
|
||
<hr>
|
||
<h3 id="ch-04-en-3-required-checks-before-submitting">3. Required Checks Before Submitting</h3>
|
||
<p>WTOffline <strong>does not require or encourage large mock unit test suites</strong>. 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:</p>
|
||
<div class="doc-table-wrap"><table class="doc-table"><thead><tr><th>What you changed</th><th>Required checks before opening a PR</th></tr></thead><tbody><tr><td><code>packages/*</code>, <code>apps/server</code>, or <code>scripts/*</code></td><td>Root <code>npm run typecheck</code>; if you touched server startup or <code>tuning/*.jsonc</code>, boot <code>apps/server/src/cli.ts</code> against a temporary data folder to confirm clean dataset extraction and tuning validation</td></tr><tr><td><code>apps/launcher</code></td><td>Launcher <code>typecheck</code> + <code>electron-vite build</code>; if you edited UI strings, verify <code>en.json</code> and <code>zh-CN.json</code> locale key parity</td></tr><tr><td><code>patches/exe/</code> (any <code>patch.jsonc</code>)</td><td><code>wtotools check-patches</code> (verifies unique signature hits, <code>expect_original</code> bytes, and exact byte-for-byte agreement between <code>asm</code>/<code>data</code> and <code>bytes</code>)</td></tr><tr><td><code>patches/gui/</code> (any <code>patch.jsonc</code> or <code>.nut</code> fragment)</td><td><strong>Full</strong> <code>wtotools check-gui</code> using <code>sq.exe</code> <code>compilestring</code> (required) + a temporary copy build to confirm zero unknown schema keys in <code>gui-patch-definition.ts</code></td></tr><tr><td><code>native/wto-net/</code> or <code>patches/native/</code></td><td>MSVC <code>nativewto-netbuild.ps1</code> (<code>/W4</code>, <strong>zero warnings</strong>) + temporary <code>scripts/build-game-copy.ts</code> run confirming every native module resolves cleanly</td></tr><tr><td>Adapting a new client build (<code>patches/versions/</code>)</td><td><code>scripts/record-game-version.ts</code> + all applicable checks above + <strong>live in-game verification in an isolated copy (hangar login + <code>LocalMP</code> battle)</strong> before committing <code>patches/versions/<version>.jsonc</code></td></tr></tbody></table></div>
|
||
<p>Complete command reference (run from the repository root, substituting bracketed paths):</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code># 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>"</code></pre></div>
|
||
<h4 id="ch-04-en-one-time-build-of-the-quirrel-bytecode-compiler-sq-exe-note-the-sq-flag">One-Time Build of the Quirrel Bytecode Compiler <code>sq.exe</code> (Note the <code>--sq</code> Flag!)</h4>
|
||
<p><code>check-gui</code> requires the official Quirrel compiler pinned to the revision matching War Thunder's client VM (<a href="https://github.com/GaijinEntertainment/quirrel/tree/19f17df9b6ee43c91ef5c635fd45970ec76c5dbe" target="_blank" rel="noopener">Quirrel commit <code>19f17df9b6ee43c91ef5c635fd45970ec76c5dbe</code></a>). Build it once with Visual Studio 2022 C++ tools and CMake:</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>powershell</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>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</code></pre></div>
|
||
<blockquote><p><strong>Important note on <code>--sq</code></strong>: <code>wtotools check-gui</code> defaults <code>--sq</code> to <code>build/quirrel-check/sq.exe</code> (<code>tools/src/wtotools/cli/gui_commands.py</code>), whereas the multi-config Visual Studio CMake build above outputs <strong><code>build\quirrel-cli\bin\Release\sq.exe</code></strong>. Always pass <code>--sq "build\quirrel-cli\bin\Release\sq.exe"</code> explicitly as shown in the command block above (or copy <code>sq.exe</code> to <code>build\quirrel-check\sq.exe</code>).</p></blockquote>
|
||
<hr>
|
||
<h3 id="ch-04-en-4-commit-conventions-pre-pr-checklist">4. Commit Conventions & Pre-PR Checklist</h3>
|
||
<ul><li><strong>One focused goal per PR</strong>: Avoid mixing unrelated refactors or repo-wide formatting into a bugfix or feature PR.</li><li><strong>English Conventional Commits & English code comments</strong>:
|
||
<ul><li>Format commit titles as <code>type(scope): concise summary</code> (e.g., <code>fix(battle): credit Ground Arcade air event kills from AI victims</code>, <code>feat(launcher): show missing optional patch features</code>; use <code>repo</code> or <code>docs</code> for repository-level or documentation changes);</li><li>Mark breaking changes with <code>!</code> after the scope and a <code>BREAKING CHANGE:</code> footer;</li><li>Write all code comments, 3-line patch header comments, patch <code>title</code> fields, and commit bodies in English;</li><li>Reserve <code>Co-authored-by</code> trailers strictly for human collaborators—never list AI models or coding tools.</li></ul></li><li><strong>Final 5-point self-check before opening a PR</strong>:
|
||
<ol><li>Check against <a href="#ch-02">Development Rules — Section 4</a>: zero hardcoded RVAs, struct offsets, or <code>_ReturnAddress()</code> checks; zero hardcoded stock game numbers; zero cross-layer I/O;</li><li>Verify syntax & schema discipline: explicit <code>.ts</code> extensions on relative imports, <code>import type</code> for types, no <code>enum</code>/<code>namespace</code>/parameter properties; strict patch JSONC keys (no <code>requires</code> in <code>patches/gui/**/patch.jsonc</code>, explicit <code>"end"</code> on <code>rel32</code> captures with trailing immediates);</li><li>For currency, research, inventory, or profile mutations, verify changes persist atomically via <code>PlayerProfiles</code> / <code>SaveStore</code> <strong>before</strong> replying to the client, with strict per-account isolation;</li><li>Review <code>git diff</code> to ensure zero personal machine paths, copyrighted game files, extracted stock scripts, personal saves, or unredacted logs slipped in;</li><li>Fill out the <a href="#ch-05">PR template</a> completely, recording exact commands run and clearly separating observed in-game checks from pending in-game acceptance steps.</li></ol></li></ul>
|
||
</div>
|
||
<div class="doc-pager">
|
||
<a class="doc-pager-btn" href="#ch-03" data-chapter="ch-03"><span class="doc-pager-label"><span class="lang-zh">← 上一章</span><span class="lang-en">← Previous</span></span><span class="doc-pager-title"><span class="lang-zh">03 · 补丁编写与逐字节真实拆解</span><span class="lang-en">03 · Patch Authoring & Byte Anatomy</span></span></a>
|
||
<a class="doc-pager-btn next" href="#ch-05" data-chapter="ch-05"><span class="doc-pager-label"><span class="lang-zh">下一章 →</span><span class="lang-en">Next →</span></span><span class="doc-pager-title"><span class="lang-zh">05 · PR 模板与提交前自查表</span><span class="lang-en">05 · PR Template & Self-Check</span></span></a>
|
||
</div>
|
||
</article>
|
||
|
||
<article class="doc-section doc-chapter" id="ch-05">
|
||
<div class="lang-zh">
|
||
<h2>05 · Pull Request 标准模板与自查清单</h2>
|
||
<p>提交 PR 时请直接复制下方模板,逐项勾选 <code>Self-check</code> 并如实填写实际运行过的检查命令与待实机验收步骤:</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>markdown</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>## Change
|
||
|
||
<!-- Describe the player-visible result and the single focused goal of this PR. -->
|
||
|
||
## Reason and evidence
|
||
|
||
<!-- Give the target client build (e.g. 2.61.0.37) and cite the primary source: stock archive file, .nut script, RVA, or DagorEngine source path. -->
|
||
<!-- For any client patch (patches/gui, patches/exe, native/wto-net), explain why apps/server or stock BLK configuration cannot solve it. -->
|
||
<!-- For tuning changes (apps/server/tuning/*.jsonc), record the rationale, bounds, and units of the new defaults. -->
|
||
|
||
## Client compatibility
|
||
|
||
<!-- List the tested/affected client builds and any added patch variants or patches/versions/<version>.jsonc updates. -->
|
||
<!-- Reminder: never commit patches/versions/<version>.jsonc until live isolated in-game acceptance is complete. -->
|
||
|
||
## Self-check
|
||
|
||
- [ ] No hardcoded addresses, RVAs, C++ struct offsets, or `_ReturnAddress()` checks (all native values resolved dynamically into `g_lmp` via `patches/`, with explicit `"end"` on `rel32` captures that have trailing immediate bytes)
|
||
- [ ] No hardcoded stock game numbers or silent fallback defaults (extracted from stock archives via `game-data/datasets/` or validated in `tuning/*.jsonc`)
|
||
- [ ] Layer & persistence rules preserved (`packages/*` never imports `apps/*`; domain modules stay free of network/disk I/O; profile mutations persist atomically via `PlayerProfiles` / `SaveStore` before replying)
|
||
- [ ] Toolchain & schema rules met (explicit `.ts` extensions on relative imports, `import type` for types, no `enum`/`namespace`/parameter properties; strict patch JSONC keys—e.g., no `requires` in `patches/gui/**/patch.jsonc`; new `.c` files added to `native/wto-net/build.ps1`)
|
||
- [ ] Focused on one goal, English comments / patch `title` / conventional commit message used, no AI `Co-authored-by` trailers, and no personal paths, game assets, or unredacted logs in `git diff`
|
||
- [ ] Every command listed in **Checks run** was actually executed (including `wtotools check-gui --sq ...` for `patches/gui/` and MSVC `native/wto-net/build.ps1` with zero warnings for `native/`)
|
||
|
||
## Checks run
|
||
|
||
| Command or check | Client build (if relevant) | Result |
|
||
| --- | --- | --- |
|
||
| | | |
|
||
|
||
## In-game acceptance
|
||
|
||
<!-- Give the exact screen or entry point, the action to perform, and the expected result in an isolated offline copy. -->
|
||
<!-- Clearly state what was observed in-game, or explicitly mark steps that are still pending live verification. --></code></pre></div>
|
||
</div>
|
||
<div class="lang-en">
|
||
<h2>05 · Pull Request Template & Self-Check</h2>
|
||
<p>Copy the template below when opening a PR, check every <code>Self-check</code> box, and honestly record executed checks alongside pending in-game verification steps:</p>
|
||
<div class="doc-pre"><div class="doc-pre-head"><span>markdown</span><button class="hash-copy" type="button" data-copy-code><span class="lang-zh">复制</span><span class="lang-en">Copy</span></button></div><pre><code>## Change
|
||
|
||
<!-- Describe the player-visible result and the single focused goal of this PR. -->
|
||
|
||
## Reason and evidence
|
||
|
||
<!-- Give the target client build (e.g. 2.61.0.37) and cite the primary source: stock archive file, .nut script, RVA, or DagorEngine source path. -->
|
||
<!-- For any client patch (patches/gui, patches/exe, native/wto-net), explain why apps/server or stock BLK configuration cannot solve it. -->
|
||
<!-- For tuning changes (apps/server/tuning/*.jsonc), record the rationale, bounds, and units of the new defaults. -->
|
||
|
||
## Client compatibility
|
||
|
||
<!-- List the tested/affected client builds and any added patch variants or patches/versions/<version>.jsonc updates. -->
|
||
<!-- Reminder: never commit patches/versions/<version>.jsonc until live isolated in-game acceptance is complete. -->
|
||
|
||
## Self-check
|
||
|
||
- [ ] No hardcoded addresses, RVAs, C++ struct offsets, or `_ReturnAddress()` checks (all native values resolved dynamically into `g_lmp` via `patches/`, with explicit `"end"` on `rel32` captures that have trailing immediate bytes)
|
||
- [ ] No hardcoded stock game numbers or silent fallback defaults (extracted from stock archives via `game-data/datasets/` or validated in `tuning/*.jsonc`)
|
||
- [ ] Layer & persistence rules preserved (`packages/*` never imports `apps/*`; domain modules stay free of network/disk I/O; profile mutations persist atomically via `PlayerProfiles` / `SaveStore` before replying)
|
||
- [ ] Toolchain & schema rules met (explicit `.ts` extensions on relative imports, `import type` for types, no `enum`/`namespace`/parameter properties; strict patch JSONC keys—e.g., no `requires` in `patches/gui/**/patch.jsonc`; new `.c` files added to `native/wto-net/build.ps1`)
|
||
- [ ] Focused on one goal, English comments / patch `title` / conventional commit message used, no AI `Co-authored-by` trailers, and no personal paths, game assets, or unredacted logs in `git diff`
|
||
- [ ] Every command listed in **Checks run** was actually executed (including `wtotools check-gui --sq ...` for `patches/gui/` and MSVC `native/wto-net/build.ps1` with zero warnings for `native/`)
|
||
|
||
## Checks run
|
||
|
||
| Command or check | Client build (if relevant) | Result |
|
||
| --- | --- | --- |
|
||
| | | |
|
||
|
||
## In-game acceptance
|
||
|
||
<!-- Give the exact screen or entry point, the action to perform, and the expected result in an isolated offline copy. -->
|
||
<!-- Clearly state what was observed in-game, or explicitly mark steps that are still pending live verification. --></code></pre></div>
|
||
</div>
|
||
<hr>
|
||
<div class="doc-links">
|
||
<a class="btn btn-primary" href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline/src/branch/main/dev-docs/templates" target="_blank" rel="noopener">
|
||
<span class="lang-zh">浏览仓库补丁与 PR 模板 ↗</span>
|
||
<span class="lang-en">Browse Patch & PR Templates ↗</span>
|
||
</a>
|
||
<a class="btn btn-ghost" href="/mod-docs">
|
||
<span class="lang-zh">阅读完整模组制作手册 →</span>
|
||
<span class="lang-en">Read Mod Authoring Handbook →</span>
|
||
</a>
|
||
</div>
|
||
<div class="doc-pager">
|
||
<a class="doc-pager-btn" href="#ch-04" data-chapter="ch-04"><span class="doc-pager-label"><span class="lang-zh">← 上一章</span><span class="lang-en">← Previous</span></span><span class="doc-pager-title"><span class="lang-zh">04 · 环境搭建、流量日志与检查</span><span class="lang-en">04 · Setup, Traffic Logs & Checks</span></span></a>
|
||
|
||
</div>
|
||
</article>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<footer>
|
||
<div class="wrap">
|
||
<div class="foot">
|
||
<span class="foot-brand">
|
||
<img src="assets/icon.svg" alt="" width="20" height="20" draggable="false">
|
||
WTOffline · AGPL-3.0-or-later
|
||
</span>
|
||
<span class="right">
|
||
<a href="/"><span class="lang-zh">主页</span><span class="lang-en">Home</span></a>
|
||
<a href="/releases"><span class="lang-zh">全部版本</span><span class="lang-en">All Releases</span></a>
|
||
<a href="/mod-docs"><span class="lang-zh">模组文档</span><span class="lang-en">Mod Docs</span></a>
|
||
<a href="https://git.wto-project.net/Sh3ll_in_Sh3ll/WT-Offline" target="_blank" rel="noopener">Gitea</a>
|
||
<a href="https://github.com/N0VAN0M4LY/WT-Offline" target="_blank" rel="noopener">GitHub</a>
|
||
<a href="https://discord.gg/P566xWNpEs" target="_blank" rel="noopener">Discord</a>
|
||
</span>
|
||
</div>
|
||
<p class="disclaimer">
|
||
<span class="lang-zh">声明:本项目是非商业性质的同人开源项目,与 Gaijin Entertainment 没有任何关联,也未获其认可。War Thunder 及相关名称与素材归其各自所有者所有。本项目不分发任何游戏文件,使用者须自行拥有合法的游戏客户端。</span>
|
||
<span class="lang-en">Disclaimer: This is a non-commercial open-source fan project and is not affiliated with or endorsed by Gaijin Entertainment. War Thunder and related names and assets belong to their respective owners. No game files are distributed; you need your own legitimate copy of the client.</span>
|
||
</p>
|
||
</div>
|
||
</footer>
|
||
</main>
|
||
|
||
<script src="site/home.js?v=e267aee3"></script>
|
||
</body>
|
||
</html>
|