File size: 2,752 Bytes
10695bc 082058f 10695bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | # eu4-han-convert
与 ParaTranz 当前 EU4 转换器兼容的 Node.js 编解码包,处理双字节补丁使用的 UTF-8 BOM 汉化文本。
## 原理
EU4 汉化文本并不是 UTF-16。“EU4 双字节 BOM 编码”由两层组成:
1. 文件层使用 UTF-8,并以 `EF BB BF` 开头。
2. 每个码点不小于 `U+0100` 的 BMP 字符拆成低、高两个字节,在前面加入 `0x10` 至 `0x13` 标记。
3. 如果高、低字节会与 EU4 文本语法或引擎内部字符冲突,标记会记录需要还原的字节,EU4 1.26+ 对低字节使用 `+14` 偏移。
4. CP1252 的 `0x80` 至 `0x9F` 特殊字节先映射到对应 Unicode 字符,再由 UTF-8 保存,避免控制区在文件中损坏。
ParaTranz 存在三套可见的保留字节表:
- `current`:2026-07-30 在线实时转换器部署代码使用的表,包含 `0x21`。
- `legacy`:Ab Integro 等现有 ParaTranz 编码文件实际使用的表,额外包含 `#`、`/`、`:`,可用于字节完全一致的重建。
- `compatible`:两者并集,也是默认值。多转义已知保留字节不改变解码结果,并避免这些字节被游戏语法误读。
公开 Gist 又与上述两套表略有不同,但核心标记、偏移和 CP1252 规则相同。
参考:
- [ParaTranz 编码转换器](https://paratranz.cn/utilities/converter)
- [ParaTranz 公开的 special-escape.js](https://gist.github.com/bruceCzK/96ad6e054111f929ed67291552d36334)
- [EU4SpecialEscape 原始实现](https://github.com/matanki-saito/EU4SpecialEscape)
## 命令行
```powershell
eu4-han-convert encode .\text_l_english.yml
eu4-han-convert decode .\text_l_english.eu4.yml
eu4hc encode .\source.yml .\game.yml
eu4hc decode .\game.yml .\readable.yml
eu4hc inspect .\game.yml
```
默认编码生成 `*.eu4.yml`,解码生成 `*.decoded.yml`。两种输出都带 UTF-8 BOM。已有输出不会被静默覆盖:
```powershell
eu4hc encode .\source.yml -o .\game.yml --force
eu4hc encode .\game.yml --in-place
eu4hc encode .\gbk.yml --source gbk
eu4hc encode .\source.yml --profile current
eu4hc encode .\source.yml --profile legacy
eu4hc decode .\game.yml --plain-utf8
```
## Node.js API
```js
import {
decodeBuffer,
decodeFile,
decodeText,
encodeBuffer,
encodeFile,
encodeText,
} from 'eu4-han-convert';
const encodedText = encodeText('大明');
const readableText = decodeText(encodedText);
const gameFile = encodeBuffer('l_english:\n MNG:0 "大明"\n');
const source = decodeBuffer(gameFile);
await encodeFile('source.yml', 'game.yml');
await decodeFile('game.yml', 'readable.yml');
```
该双字节格式只表示 BMP(`U+0000` 至 `U+FFFF`)。扩展 B 区等非 BMP 字符会直接报错,因为它们无法由该格式的两个字节表示。
|