把官方 SideQuest 桌面版(基线 1.4.1)汉化为简体中文,并产出可覆盖安装的 deb。 不修改任何上游源码:所有改动集中在 build.js 的补丁层,作用于已编译产物 (dist-electron 与 dist/sidequest-desktop),因此本工具包可独立于上游代码工作。 主要内容: - 运行时汉化引擎 engine.js + 主进程统一注入 hook tail.js (应用有 7 个 data:text/html overlay 页且带严格 CSP,故用主进程 hook 一次覆盖全部窗口) - 词典 1806 条(dict-a…i.json)+ 动态规则 137 条(rules.json) - 纯 Node 实现的 asar 打包/解包 asar.js,零外部依赖 - 构建/试运行/安装/校验脚本 覆盖范围:主页、侧载、文件、应用、串流、设备设置、调试(Logcat)、无线、 全部弹窗、命令面板、日期与相对时间。内嵌商店为远程服务端渲染,不在范围内。
212 lines
7.9 KiB
JavaScript
212 lines
7.9 KiB
JavaScript
/* ============================================================================
|
|
* 纯 Node 实现的 asar 读/写(无任何外部依赖)
|
|
*
|
|
* 为什么要自己实现:Electron 的 asar 格式非常简单且稳定(头部 Pickle + JSON
|
|
* 目录表 + 连续文件数据),自己实现可以让整个汉化工具链在离线、无 node_modules
|
|
* 的环境下继续可用,避免以后升级时还要联网装 @electron/asar。
|
|
*
|
|
* 格式(与 Electron/@electron/asar 完全一致):
|
|
* [0..4) u32 = 4 —— sizePickle 的载荷长度
|
|
* [4..8) u32 = headerPickleLen —— 头部 Pickle 总长度
|
|
* [8..12) u32 = payloadLen —— headerPickle 内层载荷长度
|
|
* [12..16) u32 = jsonLen —— JSON 目录表长度
|
|
* [16..) JSON 目录表 + 对齐填充
|
|
* [8 + headerPickleLen ..) —— 文件数据区
|
|
*
|
|
* 目录表节点:
|
|
* 目录: { files: { 名称: 节点 } }
|
|
* 文件: { size, offset(字符串,相对数据区起点), integrity:{algorithm,hash} }
|
|
* 软链: { link: 目标 }
|
|
* ==========================================================================*/
|
|
'use strict';
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const crypto = require('crypto');
|
|
|
|
const HEADER_OFFSET = 8; /* 文件数据区起点 = HEADER_OFFSET + headerPickleLen */
|
|
|
|
/* ----------------------------------------------------------------- 读取头部 */
|
|
function readHeader(fd) {
|
|
const head = Buffer.alloc(16);
|
|
fs.readSync(fd, head, 0, 16, 0);
|
|
const headerPickleLen = head.readUInt32LE(4);
|
|
const jsonLen = head.readUInt32LE(12);
|
|
const jsonBuf = Buffer.alloc(jsonLen);
|
|
fs.readSync(fd, jsonBuf, 0, jsonLen, 16);
|
|
return {
|
|
header: JSON.parse(jsonBuf.toString('utf8')),
|
|
dataStart: HEADER_OFFSET + headerPickleLen,
|
|
};
|
|
}
|
|
|
|
/* ------------------------------------------------------------------- 解包 */
|
|
/**
|
|
* 把 asar 解包到 outDir。
|
|
* 注意:只处理普通文件、目录与软链;忽略 asar 的 unpacked 约定(本项目的 app
|
|
* 未使用 unpacked 资源,build.asarUnpack 在旧架构下才用到)。
|
|
*/
|
|
function extract(asarPath, outDir) {
|
|
const fd = fs.openSync(asarPath, 'r');
|
|
try {
|
|
const { header, dataStart } = readHeader(fd);
|
|
let count = 0;
|
|
const walk = (node, cur) => {
|
|
for (const name of Object.keys(node.files || {})) {
|
|
const child = node.files[name];
|
|
const abs = path.join(cur, name);
|
|
if (child.files) {
|
|
fs.mkdirSync(abs, { recursive: true });
|
|
walk(child, abs);
|
|
} else if (typeof child.link === 'string') {
|
|
try {
|
|
fs.symlinkSync(child.link, abs);
|
|
} catch (e) {
|
|
/* 已存在等情况忽略 */
|
|
}
|
|
} else {
|
|
fs.mkdirSync(path.dirname(abs), { recursive: true });
|
|
const buf = Buffer.alloc(child.size);
|
|
fs.readSync(fd, buf, 0, child.size, dataStart + parseInt(child.offset, 10));
|
|
fs.writeFileSync(abs, buf);
|
|
count++;
|
|
}
|
|
}
|
|
};
|
|
walk(header, outDir);
|
|
return { files: count };
|
|
} finally {
|
|
fs.closeSync(fd);
|
|
}
|
|
}
|
|
|
|
/* ------------------------------------------------------------------- 打包 */
|
|
function sha256hex(buf) {
|
|
return crypto.createHash('sha256').update(buf).digest('hex');
|
|
}
|
|
|
|
/**
|
|
* 把 srcDir 打包为 asar。
|
|
* 遍历顺序固定(按名称排序),保证同一输入产出同一字节序列 —— 便于校验与复现。
|
|
*/
|
|
function pack(srcDir, outFile) {
|
|
const files = []; /* { rel, abs, size, buf } */
|
|
const dirs = new Set();
|
|
|
|
(function walk(cur, rel) {
|
|
const entries = fs.readdirSync(cur, { withFileTypes: true }).sort((a, b) =>
|
|
a.name < b.name ? -1 : a.name > b.name ? 1 : 0
|
|
);
|
|
if (entries.length === 0 && rel !== '') dirs.add(rel);
|
|
for (const e of entries) {
|
|
const abs = path.join(cur, e.name);
|
|
const childRel = rel === '' ? e.name : rel + '/' + e.name;
|
|
if (e.isDirectory()) {
|
|
dirs.add(childRel);
|
|
walk(abs, childRel);
|
|
} else if (e.isSymbolicLink()) {
|
|
files.push({ rel: childRel, abs, link: fs.readlinkSync(abs) });
|
|
} else if (e.isFile()) {
|
|
files.push({ rel: childRel, abs, buf: fs.readFileSync(abs) });
|
|
}
|
|
}
|
|
})(srcDir, '');
|
|
|
|
/* 数据区顺序:文件按 rel 排序,依次追加 */
|
|
const ordered = files.filter(f => f.buf).sort((a, b) => (a.rel < b.rel ? -1 : 1));
|
|
let offset = 0;
|
|
for (const f of ordered) {
|
|
f.offset = offset;
|
|
offset += f.buf.length;
|
|
}
|
|
|
|
/* 构造 JSON 目录表 */
|
|
const root = { files: {} };
|
|
const ensureDir = rel => {
|
|
const parts = rel.split('/');
|
|
let node = root;
|
|
for (const p of parts) {
|
|
if (!node.files[p]) node.files[p] = { files: {} };
|
|
node = node.files[p];
|
|
}
|
|
return node;
|
|
};
|
|
for (const d of [...dirs].sort()) ensureDir(d);
|
|
for (const f of files) {
|
|
const parts = f.rel.split('/');
|
|
const name = parts.pop();
|
|
const parent = parts.length ? ensureDir(parts.join('/')) : root;
|
|
if (f.link !== undefined) {
|
|
parent.files[name] = { link: f.link };
|
|
} else {
|
|
parent.files[name] = {
|
|
size: f.buf.length,
|
|
offset: String(f.offset),
|
|
integrity: { algorithm: 'SHA256', hash: sha256hex(f.buf) },
|
|
};
|
|
}
|
|
}
|
|
|
|
const json = Buffer.from(JSON.stringify(root), 'utf8');
|
|
/* 对齐:令 headerPickleLen = 8 + jsonLen + pad 为 4 的倍数 */
|
|
let pad = (4 - ((HEADER_OFFSET + json.length) % 4)) % 4;
|
|
const payloadLen = 4 + json.length + pad;
|
|
const headerPickleLen = 4 + payloadLen;
|
|
|
|
const head = Buffer.alloc(16);
|
|
head.writeUInt32LE(4, 0);
|
|
head.writeUInt32LE(headerPickleLen, 4);
|
|
head.writeUInt32LE(payloadLen, 8);
|
|
head.writeUInt32LE(json.length, 12);
|
|
|
|
const out = fs.openSync(outFile, 'w');
|
|
try {
|
|
fs.writeSync(out, head);
|
|
fs.writeSync(out, json);
|
|
if (pad) fs.writeSync(out, Buffer.alloc(pad));
|
|
for (const f of ordered) fs.writeSync(out, f.buf);
|
|
} finally {
|
|
fs.closeSync(out);
|
|
}
|
|
return { files: ordered.length, bytes: offset };
|
|
}
|
|
|
|
/* --------------------------------------------------------------- 单文件读取 */
|
|
function readFile(asarPath, innerPath) {
|
|
const fd = fs.openSync(asarPath, 'r');
|
|
try {
|
|
const { header, dataStart } = readHeader(fd);
|
|
let node = header;
|
|
for (const p of innerPath.split('/')) {
|
|
if (!node.files || !node.files[p]) return null;
|
|
node = node.files[p];
|
|
}
|
|
if (!node || node.files) return null;
|
|
const buf = Buffer.alloc(node.size);
|
|
fs.readSync(fd, buf, 0, node.size, dataStart + parseInt(node.offset, 10));
|
|
return buf;
|
|
} finally {
|
|
fs.closeSync(fd);
|
|
}
|
|
}
|
|
|
|
module.exports = { extract, pack, readFile, readHeader };
|
|
|
|
/* -------------------------------------------------------------- CLI(可选) */
|
|
if (require.main === module) {
|
|
const [cmd, a, b] = process.argv.slice(2);
|
|
if (cmd === 'extract') {
|
|
const r = extract(a, b);
|
|
console.log('已解包 ' + r.files + ' 个文件 -> ' + b);
|
|
} else if (cmd === 'pack') {
|
|
const r = pack(a, b);
|
|
console.log('已打包 ' + r.files + ' 个文件 (' + r.bytes + ' 字节数据) -> ' + b);
|
|
} else if (cmd === 'ls') {
|
|
const { header } = readHeader(fs.openSync(a, 'r'));
|
|
console.log(JSON.stringify(header, null, 1).slice(0, 2000));
|
|
} else {
|
|
console.log('用法: node asar.js <extract|pack|ls> <输入> [输出]');
|
|
process.exit(1);
|
|
}
|
|
}
|