From 214f9d7f4b4495d758196ad43d231b7c5182830d Mon Sep 17 00:00:00 2001 From: N0VAN0M4LY <224130499+N0VAN0M4LY@users.noreply.github.com> Date: Thu, 3 Dec 2026 07:18:14 +0800 Subject: [PATCH] fix(launcher): use original-fs in portable updater and drop default_app.asar Electron patches node:fs to treat .asar archives as virtual directories, so portable update verification failed on staged resources/default_app.asar and recursive cleanup could leave staging folders behind. Switch staging lstat and cleanup rm in portable-update.ts to original-fs (falling back to node:fs outside Electron), strip unused resources/default_app.asar during packaging so already-released 0.1.2/0.1.3 portable builds can update cleanly to future releases, and include portable-update.log in exported diagnostics. --- .../services/app-update/portable-update.ts | 23 +++++++++++++++---- .../main/services/diagnostics/diagnostics.ts | 6 +++++ scripts/build-windows-installer.ts | 7 ++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/apps/launcher/src/main/services/app-update/portable-update.ts b/apps/launcher/src/main/services/app-update/portable-update.ts index 5eea063..d25f7e4 100644 --- a/apps/launcher/src/main/services/app-update/portable-update.ts +++ b/apps/launcher/src/main/services/app-update/portable-update.ts @@ -9,19 +9,33 @@ */ import { execFile, spawn } from 'node:child_process'; import { randomUUID } from 'node:crypto'; +import * as nodeFs from 'node:fs'; import { existsSync } from 'node:fs'; -import { lstat, mkdir, readFile, rm, writeFile } from 'node:fs/promises'; +import { mkdir, readFile, rm, writeFile } from 'node:fs/promises'; +import { createRequire } from 'node:module'; import { dirname, isAbsolute, join, relative, resolve } from 'node:path'; import { promisify } from 'node:util'; import { z } from 'zod'; const execute = promisify(execFile); +const require = createRequire(import.meta.url); export const PORTABLE_MARKER_FILE = 'portable-mode'; export const PORTABLE_FILES_MANIFEST = 'portable-files.json'; +export const PORTABLE_UPDATE_LOG_FILENAME = 'portable-update.log'; const LAUNCHER_EXECUTABLE = 'WTOffline Launcher.exe'; const UNINSTALLER_EXECUTABLE = 'Uninstall WTOffline Launcher.exe'; +// Electron patches node:fs to expose .asar archives as virtual directories, which makes +// lstat().isFile() return false and recursive rm() fail on staged .asar files. +function rawFsPromises(): typeof nodeFs.promises { + try { + return (require('original-fs') as typeof nodeFs).promises; + } catch { + return nodeFs.promises; + } +} + const PortableInventorySchema = z.object({ format: z.literal(1), version: z.string().min(1), @@ -216,6 +230,7 @@ export async function preparePortableInstall( ): Promise { await assertWritableFolder(installDir); await mkdir(updatesRoot, { recursive: true }); + const rawFs = rawFsPromises(); const stagingDir = join(updatesRoot, `portable-staging-${randomUUID()}`); await mkdir(stagingDir, { recursive: true }); try { @@ -238,7 +253,7 @@ export async function preparePortableInstall( } for (const rel of newFiles) { const stagedPath = resolveInside(stagingDir, rel); - const stats = await lstat(stagedPath); + const stats = await rawFs.lstat(stagedPath); if (!stats.isFile()) throw new Error(`Staged portable entry is not a file: ${rel}`); } @@ -254,7 +269,7 @@ export async function preparePortableInstall( installDir: resolve(installDir), stagingDir, archivePath, - logPath: join(updatesRoot, 'portable-update.log'), + logPath: join(updatesRoot, PORTABLE_UPDATE_LOG_FILENAME), executable: LAUNCHER_EXECUTABLE, newFiles, staleFiles, @@ -263,7 +278,7 @@ export async function preparePortableInstall( await writeFile(scriptPath, `\uFEFF${buildPortableApplyScript()}\r\n`, 'utf8'); return { scriptPath, planPath, stagingDir }; } catch (error) { - await rm(stagingDir, { recursive: true, force: true }).catch(() => undefined); + await rawFs.rm(stagingDir, { recursive: true, force: true }).catch(() => undefined); throw error; } } diff --git a/apps/launcher/src/main/services/diagnostics/diagnostics.ts b/apps/launcher/src/main/services/diagnostics/diagnostics.ts index 97c4482..40933f1 100644 --- a/apps/launcher/src/main/services/diagnostics/diagnostics.ts +++ b/apps/launcher/src/main/services/diagnostics/diagnostics.ts @@ -7,6 +7,7 @@ import { zipSync } from 'fflate'; import { GAME_FILES } from '@wto/patcher'; import { AppError } from '@shared/errors.ts'; import type { InstallRecord } from '@shared/schemas/install.ts'; +import { PORTABLE_UPDATE_LOG_FILENAME } from '../app-update/portable-update.ts'; import { getSettings } from '../config/settings-store.ts'; import { COPIES_FILENAME, readInstallRecord } from '../install/install-record.ts'; import { LAUNCHER_LOG_FILES, launcherLogFolder } from './launcher-log.ts'; @@ -58,6 +59,11 @@ export async function exportDiagnostics(window: BrowserWindow | null): Promise<{ await addFile(files, `launcher-logs/${name}`, join(launcherLogs, name)); } } + await addFile( + files, + `launcher-logs/${PORTABLE_UPDATE_LOG_FILENAME}`, + join(userData, 'launcher-updates', PORTABLE_UPDATE_LOG_FILENAME), + ); const [record, settings] = await Promise.all([readInstallRecord(), getSettings()]); const dataPath = (settings.developerMode ? settings.labDataPath : null) ?? record?.dataPath; if (dataPath) await addServerLogs(files, dataPath); diff --git a/scripts/build-windows-installer.ts b/scripts/build-windows-installer.ts index befcd3c..80de38f 100644 --- a/scripts/build-windows-installer.ts +++ b/scripts/build-windows-installer.ts @@ -163,6 +163,12 @@ async function main(): Promise { uninstallerIcon: join(stage, 'build', 'icon.ico'), installerHeaderIcon: join(stage, 'build', 'icon.ico'), }, + // Drop Electron's unused fallback welcome archive so portable builds never ship it and + // already-released portable launchers (0.1.2 / 0.1.3) can update without tripping over + // Electron's .asar virtual-directory check. + afterPack: async (context) => { + await rm(join(context.appOutDir, 'resources', 'default_app.asar'), { force: true }); + }, }, }); for (const artifact of artifacts) console.info(artifact); @@ -200,6 +206,7 @@ async function packagePortableArchive(outputFolder: string, version: string): Pr const inventoryPath = join(resources, 'portable-files.json'); const portable = join(outputFolder, portableLauncherReleaseFiles(version).archive); try { + await rm(join(resources, 'default_app.asar'), { force: true }); await writeFile(markerPath, 'portable\n', 'utf8'); await writeFile(inventoryPath, '', 'utf8'); const ownedFiles = await listRelativeFiles(unpacked);