Files
WT-Offline/apps/launcher/src/main/services/game-folder/inspect-game-folder.ts
T
N0VAN0M4LY c55ec4ed57 fix(launcher): detect stale stock backups and distinguish missing backups on launch
Read the live executable version in inspectGameFolder instead of masking externally updated folders behind an older wtoffline/stock backup, and atomically refresh stale or unreadable backups during install, external stock import, and relocation. Log exact copy/stock/recorded versions on launch mismatches and report a dedicated launch_stock_missing error when wtoffline/stock/win64/aces.exe is missing or unreadable.

Checked: root typecheck, launcher typecheck, zh-CN/en locale key parity (616/616), and launcher electron-vite build.
2026-12-02 09:06:28 +08:00

115 lines
4.4 KiB
TypeScript

import { access, readFile, realpath, stat } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { executableVersion, GAME_FILES, isInSteamLibrary, recordedGameVersions } from '@wto/patcher';
import { AppError } from '@shared/errors.ts';
import type { GameFolderInspection } from '@shared/schemas/install.ts';
import { STOCK_BACKUP_FOLDER } from '../install/stock-backup.ts';
import { resourcePaths } from '../resources/resource-paths.ts';
const PATCH_MARKER_FILES = ['wto_ws.dll', 'wto_ws.cfg'] as const;
const PATCHED_EXE_MARKER = Buffer.from('wto_ws.dll', 'ascii');
/**
* Look at a folder the player chose as their War Thunder installation: is it one, which version,
* whether it already has an offline stock backup, whether it is already patched without one,
* and whether it is inside a Steam library. Only reads.
*/
export async function inspectGameFolder(path: string): Promise<GameFolderInspection> {
await requireFolder(path);
const resolved = await realpath(path);
const required = Object.values(GAME_FILES);
const missingFiles: string[] = [];
for (const file of required) {
if (!(await exists(join(resolved, file)))) missingFiles.push(file);
}
const stockRoot = join(resolved, STOCK_BACKUP_FOLDER);
let hasStockFiles = true;
for (const file of required) {
if (!(await exists(join(stockRoot, file)))) {
hasStockFiles = false;
break;
}
}
const resources = resourcePaths();
const liveExeBytes = missingFiles.includes(GAME_FILES.executable)
? null
: await readBytes(join(resolved, GAME_FILES.executable));
const liveVersion = liveExeBytes ? readVersionFromBytes(liveExeBytes) : null;
const backupExeBytes = hasStockFiles
? await readBytes(join(stockRoot, GAME_FILES.executable))
: null;
const backupVersion = backupExeBytes ? readVersionFromBytes(backupExeBytes) : null;
// Always report the live executable's version when readable so an externally updated client
// (for example, an in-place folder updated by the official launcher) is never masked by a stale
// wtoffline/stock backup.
const version = liveVersion ?? backupVersion;
const hasOfflineBackup = hasStockFiles && backupVersion !== null && (liveVersion === null || backupVersion === liveVersion);
const liveExePatched = liveExeBytes !== null && liveExeBytes.includes(PATCHED_EXE_MARKER);
let needsStockSource = false;
if (!hasOfflineBackup && missingFiles.length === 0) {
if (hasStockFiles && backupVersion !== null && liveVersion !== null && backupVersion !== liveVersion) {
// A stale stock backup only needs an external stock source if the live executable is already
// patched; an unpatched live executable can refresh wtoffline/stock directly during install.
needsStockSource = liveExePatched;
} else {
const exeDir = join(resolved, dirname(GAME_FILES.executable));
const hasMarkerFile = (await Promise.all(
PATCH_MARKER_FILES.map((file) => exists(join(exeDir, file))),
)).some(Boolean);
needsStockSource = hasMarkerFile || liveExePatched;
}
}
return {
path,
resolvedPath: resolved,
missingFiles,
version,
verifiedVersions: recordedGameVersions(resources.patches),
inSteamLibrary: isInSteamLibrary(path) || isInSteamLibrary(resolved),
hasOfflineBackup,
needsStockSource,
};
}
async function readBytes(filePath: string): Promise<Buffer | null> {
try {
return await readFile(filePath);
} catch (error) {
console.warn('[game-folder] reading the executable failed:', error);
return null;
}
}
function readVersionFromBytes(bytes: Buffer): string | null {
try {
return executableVersion(bytes);
} catch (error) {
// Not a PE file, or unreadable: the folder is then not a usable installation, which the
// missing version already says.
console.warn('[game-folder] reading the executable version failed:', error);
return null;
}
}
/** Throws folder_unreadable unless `path` is a folder that can be read. */
export async function requireFolder(path: string): Promise<void> {
let isFolder = false;
try {
isFolder = (await stat(path)).isDirectory();
} catch (error) {
console.warn('[game-folder] cannot read', path, error);
}
if (!isFolder) throw new AppError('folder_unreadable', { path });
}
async function exists(path: string): Promise<boolean> {
try {
await access(path);
return true;
} catch {
return false;
}
}