Skip to content

Windows 上的安卓应用运行器:双击 APK 即装,应用以独立窗口与桌面图标运行,自持 QEMU 运行时

fix(runtime): 显卡可调用 + 4 个阻塞性 bug(xz 识别/ESM 解压/便携版路径/加速检测)

RoutineMiku7ae3ecf0e908
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 3244c75..b1e53fa 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -81,10 +81,15 @@ jobs:
- name: 组装运行时
run: node build/fetch-runtime.js engine
- # prepare-runtime 会把 system.img/vendor.img 压成 xz(LZMA2 9e,
- # 有 python 时走极限档,没有则回退 @napi-rs/lzma),再用 NSIS LZMA-max 打包。
+ # prepare-runtime 会把 system.img/vendor.img 压成 xz(LZMA2)。
+ # CI runner 只有 2 核:XZ_PRESET=6 把压缩时间从 ~2h 降到 ~40min(体积 +3%)。
+ - name: 准备运行时(xz 压缩,CI 降档加速)
+ run: node build/prepare-runtime.js --force
+ env:
+ XZ_PRESET: '6'
+
- name: 构建完整版安装包
- run: npm run build:exe
+ run: npx electron-builder --win nsis -c.compression=normal
env:
CSC_IDENTITY_AUTO_DISCOVERY: 'false'
diff --git a/build/prepare-runtime.js b/build/prepare-runtime.js
index b675dc5..bacd298 100644
--- a/build/prepare-runtime.js
+++ b/build/prepare-runtime.js
@@ -31,29 +31,57 @@ async function gzipFile(src, dest) {
);
}
-/** 大镜像用 XZ(LZMA2 9e):比 gzip -6 再省 ~25%(system.img 1.5GB→~1.1GB)。
- * 优先 python(支持 EXTREME 极限档);没有 python 时回退 napi-rs(preset 9)。 */
+/** 大镜像用 XZ(LZMA2):比 gzip -6 再省 ~25%(system.img 1.5GB→~1.1GB)。
+ * 默认 9e 极限档;CI 双核可用 XZ_PRESET=6 把时间压到 1/3(体积 +3%)。
+ *
+ * 实现说明(踩过的坑):
+ * - 用 python 的 lzma(单线程、稳定)。@napi-rs/lzma 的 createCompressStream 在某些
+ * 环境下会 0 字节挂死(实测挂 18 分钟无输出),只能当最后兜底且要带超时。
+ * - 找不到 python 时退回 gzip -9:体积大 ~30% 但绝不会挂,保证 CI 能出包。
+ */
+function findPython() {
+ const { spawnSync } = require('child_process');
+ for (const cmd of ['python', 'python3', 'py']) {
+ try {
+ const r = spawnSync(cmd, ['-c', 'import lzma;print("ok")'], {
+ encoding: 'utf8', windowsHide: true, timeout: 15000, stdio: ['ignore', 'pipe', 'ignore'],
+ });
+ if (r.status === 0 && /ok/.test(String(r.stdout))) return cmd;
+ } catch (_) { /* 试下一个 */ }
+ }
+ return null;
+}
+
async function xzFile(src, dest) {
- const { spawn } = require('child_process');
- const ok = await new Promise((resolve) => {
- const py = spawn('python', ['-c', [
- 'import lzma,sys,shutil',
- 'flt=[{"id":lzma.FILTER_LZMA2,"preset":9|lzma.PRESET_EXTREME,"dict_size":1<<27}]',
- 'z=lzma.LZMAFile(sys.stdout.buffer,mode="wb",format=lzma.FORMAT_XZ,filters=flt)',
- 'shutil.copyfileobj(sys.stdin.buffer,z); z.close()',
- ].join(';')], { stdio: ['pipe', 'pipe', 'inherit'], windowsHide: true });
- const out = fs.createWriteStream(dest);
- py.stdout.pipe(out);
- py.on('exit', (c) => resolve(c === 0));
- py.on('error', () => resolve(false));
- });
- if (ok) return;
- console.log(' (未找到 python,回退 napi-rs preset 9)');
- const { createCompressStream } = require('@napi-rs/lzma/xz');
+ const preset = Math.max(0, Math.min(9, Number(process.env.XZ_PRESET || 9)));
+ const extreme = preset >= 9 && process.env.XZ_NO_EXTREME !== '1';
+ const pyPreset = extreme ? '9|lzma.PRESET_EXTREME' : String(preset);
+ const dictSize = preset >= 9 ? (1 << 27) : preset >= 6 ? (1 << 26) : (1 << 23);
+ const py = findPython();
+ if (py) {
+ const { spawn } = require('child_process');
+ const ok = await new Promise((resolve) => {
+ const p = spawn(py, ['-c', [
+ 'import lzma,sys,shutil',
+ `flt=[{"id":lzma.FILTER_LZMA2,"preset":${pyPreset},"dict_size":${dictSize}}]`,
+ 'z=lzma.LZMAFile(sys.stdout.buffer,mode="wb",format=lzma.FORMAT_XZ,filters=flt)',
+ 'shutil.copyfileobj(sys.stdin.buffer,z); z.close()',
+ ].join(';')], { stdio: ['pipe', 'pipe', 'inherit'], windowsHide: true });
+ const out = fs.createWriteStream(dest);
+ p.stdout.pipe(out);
+ p.on('exit', (c) => resolve(c === 0));
+ p.on('error', () => resolve(false));
+ });
+ if (ok) return;
+ console.log(` (${py} 压缩失败,改用 gzip -9)`);
+ } else {
+ console.log(' (未找到 python,改用 gzip -9(体积略大但可靠))');
+ }
+ // 兜底:gzip -9(不是 xz,运行时 materializeImages 按扩展名选择解压器)
await pipeline(
fs.createReadStream(src),
- createCompressStream({ preset: 9 }),
- fs.createWriteStream(dest),
+ zlib.createGzip({ level: 9 }),
+ fs.createWriteStream(dest.replace(/\.xz$/i, '.gz')),
);
}
@@ -102,8 +130,11 @@ async function main() {
if (/^(system|vendor|cache)\.img$/i.test(f) && sizeOf(src) > 32 * 1024 * 1024) {
// 大镜像压一份,原图不进包;已有 .xz(且大小合理)则跳过重复压缩
const xz = path.join(to, `${f}.xz`);
- if (fs.existsSync(xz) && sizeOf(xz) > 32 * 1024 * 1024) {
- console.log(`跳过 ${f}(已有 .xz ${(sizeOf(xz) / 1048576).toFixed(0)} MB)`);
+ const xzSize = sizeOf(xz);
+ // 已有 .xz 且大小合理(>4MB 且不小于源文件的 5%)则跳过重复压缩。
+ // 注意不能用固定 32MB 阈值:vendor.img.gz 只有 ~21MB,会被误判为"无效"重压。
+ if (xzSize > 4 * 1024 * 1024 && xzSize > sizeOf(src) * 0.05) {
+ console.log(`跳过 ${f}(已有 .xz ${(xzSize / 1048576).toFixed(0)} MB)`);
compressed += 1;
continue;
}
diff --git a/build/slim-runtime.js b/build/slim-runtime.js
index 6cc0855..99144fe 100644
--- a/build/slim-runtime.js
+++ b/build/slim-runtime.js
@@ -27,9 +27,16 @@ const path = require('path');
*/
const SAFE = [
['emulator/qemu/windows-x86_64/qemu-system-aarch64.exe', 'arm64 的 QEMU 二进制(只发 x86_64 镜像)'],
- ['emulator/lib64/vulkan', 'Vulkan 后端(固定 -gpu software,删掉退回 swiftshader)'],
+ // 注意:不能整目录删 emulator/lib64/vulkan —— vulkan-1.dll 是 -gpu host 的加载器,
+ // 删了 host 会直接失败、只剩软件渲染(实测 AMD 780M 上必须保留)。只删大件:
+ ['emulator/lib64/vulkan/layers', 'Vulkan 校验层(host 渲染用不到,-20MB)'],
+ ['emulator/lib64/vulkan/glslangValidator.exe', '着色器校验工具(运行时用不到,-7MB)'],
+ ['emulator/lib64/vulkan/libvulkan_lvp.dll', 'lavapipe 软件 Vulkan 后备(有系统显卡驱动时用不到,-53MB)'],
+ ['emulator/lib64/vulkan/lvp_icd.json', 'lavapipe 的 ICD 描述(-1KB)'],
+ ['emulator/lib64/vulkan/vk_swiftshader.dll', 'SwiftShader Vulkan(gles_angle 目录里有同款副本,-23MB)'],
['emulator/lib64/qt/translations', '模拟器界面的多语言包(只剩 38px 标题栏)'],
- ['emulator/emulator-check.exe', 'CI 自检工具(运行时用不到,-16MB)'],
+ // 注意:不能删 emulator-check.exe —— emulator -accel-check 靠它检测 AEHD/WHPX,
+ // 缺了会判定"无硬件加速"退回 TCG 软件模拟(实测开机从 42s 变成 10 分钟+)。
['emulator/qsn.exe', 'QEMU 快照通知助手(emulator.exe 无任何引用,已 strings 实证,-34MB)'],
['emulator/resources/Toren1BD_Main.png', 'VR 全景环境贴图(红果用不到,-3.7MB)'],
['emulator/resources/Toren1BD_Decor.png', 'VR 全景装饰贴图(-3.5MB)'],
diff --git a/src/core/provision.js b/src/core/provision.js
index 80068b1..5253fd4 100644
--- a/src/core/provision.js
+++ b/src/core/provision.js
@@ -508,17 +508,26 @@ class Provisioner {
onLog(`首次启动:正在展开系统镜像 ${path.basename(j.dest)}(约 1 分钟,只需一次)`);
// eslint-disable-next-line no-await-in-loop
await new Promise((resolve, reject) => {
- const { pipeline } = require('stream');
- const parts = [fs.createReadStream(j.src)];
+ const { pipeline, Readable } = require('stream');
if (j.xz) {
- // LZMA2 流式解压;@napi-rs/lzma 是 NAPI 预编译,无 Electron ABI 问题
- const { createDecompressStream } = require('@napi-rs/lzma/xz');
- parts.push(createDecompressStream());
+ // LZMA2 解压:必须直接引原生绑定 index.js。
+ // 包默认入口 main.js / xz.js 内部会 require('stream-polyfill.mjs'),
+ // Electron 的 Node 20 不支持 require(ESM) 会抛错;而 exports 又不允许
+ // 直接写 '@napi-rs/lzma/index.js',所以用 require.resolve 定位目录后按文件路径引。
+ const path0 = require('path');
+ const lzmaDir = path0.dirname(require.resolve('@napi-rs/lzma'));
+ const { xz } = require(path0.join(lzmaDir, 'index.js'));
+ const webIn = Readable.toWeb(fs.createReadStream(j.src));
+ const webOut = xz.decompressStream(webIn);
+ pipeline(Readable.fromWeb(webOut), fs.createWriteStream(j.dest), (err) => (err ? reject(err) : resolve()));
} else {
- parts.push(zlib.createGunzip());
+ pipeline(
+ fs.createReadStream(j.src),
+ zlib.createGunzip(),
+ fs.createWriteStream(j.dest),
+ (err) => (err ? reject(err) : resolve()),
+ );
}
- parts.push(fs.createWriteStream(j.dest));
- pipeline(...parts, (err) => (err ? reject(err) : resolve()));
}).catch((e) => onLog(`展开镜像失败:${e.message}`));
done += 1;
}
diff --git a/src/main.js b/src/main.js
index 075e52a..9910737 100644
--- a/src/main.js
+++ b/src/main.js
@@ -95,9 +95,10 @@ function bootstrapBundledRuntime() {
try { abis = fs.readdirSync(path.join(root, api, tag)); } catch (_) { continue; }
for (const abi of abis) {
const d = path.join(root, api, tag, abi);
- // 单文件发行版里的镜像是压缩存放的(system.img.gz),解压在首次启动时做。
+ // 单文件发行版里的镜像是压缩存放的(system.img.xz / .gz),解压在首次启动时做。
// 这里只认 system.img 会让「包里明明带了镜像」被判成没带,转而去联网下载。
- const hasSys = fs.existsSync(path.join(d, 'system.img')) || fs.existsSync(path.join(d, 'system.img.gz'));
+ const hasSys = ['system.img', 'system.img.xz', 'system.img.gz']
+ .some((f) => fs.existsSync(path.join(d, f)));
if (hasSys && fs.existsSync(path.join(d, 'kernel-ranchu'))) return true;
}
}
@@ -112,7 +113,13 @@ function bootstrapBundledRuntime() {
// 旧版只在 settings.json 不存在时接管:用户先装过在线版再解压完整版,
// 就会一直走「缺组件」的部署向导。现在只要当前运行时目录里没有 emulator 就接管。
const current = s.runtime.dir || path.join(store.DATA_DIR, 'runtime');
- const currentOk = fs.existsSync(path.join(current, 'emulator', 'emulator.exe'));
+ const currentExe = path.join(current, 'emulator', 'emulator.exe');
+ // 便携版被移动/升级(解压到新目录)后,settings 里记的还是旧路径:
+ // 必须只认「本次 EXE 所在目录下的 runtime」,否则会一直用旧包里的组件
+ // (实测踩过:旧包的 emulator 缺 emulator-check → 加速探测失败 → 退回 TCG 软件模拟)。
+ const sameTree = path.resolve(current).toLowerCase()
+ .startsWith(path.resolve(exeDir).toLowerCase());
+ const currentOk = fs.existsSync(currentExe) && sameTree;
// 已有可用运行时(含在线版)就尊重它:AVD 用户数据就在该目录下,不能悄悄搬走
if (currentOk) {
// 但仍把自动参数打开,旧配置里的硬编码 4G/4核/弃用 GPU 模式不能继续用