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.
96 lines
4.0 KiB
TypeScript
96 lines
4.0 KiB
TypeScript
import { constants } from 'node:fs';
|
|
import { access, copyFile, mkdir, readFile } from 'node:fs/promises';
|
|
import { dirname, join, relative } from 'node:path';
|
|
import { executableVersion, GAME_FILES } from '@wto/patcher';
|
|
import { AppError } from '@shared/errors.ts';
|
|
import { changeCopyFiles } from '../copies/change-copy-files.ts';
|
|
import { canonicalCopyPath } from './install-record.ts';
|
|
|
|
/**
|
|
* The stock files every build starts from, kept inside the game copy: the three data packs and
|
|
* the executable the build replaces. After installation the launcher never reads the player's
|
|
* online installation again, so a Steam update of it cannot change the offline copy unnoticed;
|
|
* for an in-place installation these are also the files to put back to undo it.
|
|
*/
|
|
export const STOCK_BACKUP_FOLDER = join('wtoffline', 'stock');
|
|
|
|
export function stockBackupPath(copyFolder: string): string {
|
|
return join(copyFolder, STOCK_BACKUP_FOLDER);
|
|
}
|
|
|
|
export async function requireStockBackupLocation(copyFolder: string): Promise<string> {
|
|
const backup = stockBackupPath(await canonicalCopyPath(copyFolder));
|
|
for (const file of Object.values(GAME_FILES)) {
|
|
const path = join(backup, file);
|
|
if (relative(path, await canonicalCopyPath(path)) !== '') throw new AppError('launch_location');
|
|
}
|
|
return backup;
|
|
}
|
|
|
|
/**
|
|
* Copies each stock file from the copy into the backup folder, unless the backup already has it:
|
|
* a backed-up file is never replaced, because the copy's own file may be a patched one by then.
|
|
*/
|
|
export async function backUpStockFiles(copyFolder: string): Promise<string> {
|
|
return backUpStockFilesFrom(copyFolder, copyFolder);
|
|
}
|
|
|
|
/**
|
|
* Copies the four stock files from `sourceFolder` (read-only) into `<copyFolder>/wtoffline/stock`,
|
|
* keeping any file that is already backed up.
|
|
*/
|
|
export async function backUpStockFilesFrom(copyFolder: string, sourceFolder: string): Promise<string> {
|
|
const backup = await requireStockBackupLocation(copyFolder);
|
|
for (const file of Object.values(GAME_FILES)) {
|
|
const kept = join(backup, file);
|
|
if (await exists(kept)) continue;
|
|
await mkdir(dirname(kept), { recursive: true });
|
|
await copyFile(join(sourceFolder, file), kept, constants.COPYFILE_EXCL);
|
|
}
|
|
return backup;
|
|
}
|
|
|
|
/** A new game version or damaged backup must pass the patch checks before replacing wtoffline/stock. */
|
|
export async function installStockSource(copyFolder: string, version: string | null) {
|
|
const backup = await requireStockBackupLocation(copyFolder);
|
|
const backupExe = join(backup, GAME_FILES.executable);
|
|
if (!await exists(backupExe)) return { path: copyFolder, refresh: await exists(backup) };
|
|
let backedUpVersion: string | null = null;
|
|
try {
|
|
backedUpVersion = executableVersion(await readFile(backupExe));
|
|
} catch (error) {
|
|
console.warn('[stock-backup] unreadable backed-up executable, will refresh:', backupExe, error);
|
|
return { path: copyFolder, refresh: true };
|
|
}
|
|
return backedUpVersion === null || (version !== null && backedUpVersion !== version)
|
|
? { path: copyFolder, refresh: true }
|
|
: { path: backup, refresh: false };
|
|
}
|
|
|
|
export async function refreshStockBackup(copyFolder: string): Promise<string> {
|
|
return refreshStockBackupFrom(copyFolder, copyFolder);
|
|
}
|
|
|
|
/** Replace wtoffline/stock atomically with the four stock files from `sourceFolder`. */
|
|
export async function refreshStockBackupFrom(copyFolder: string, sourceFolder: string): Promise<string> {
|
|
const backup = await requireStockBackupLocation(copyFolder);
|
|
return await changeCopyFiles(copyFolder, async (incoming) => {
|
|
const replace = Object.values(GAME_FILES).map((file) => join(STOCK_BACKUP_FOLDER, file));
|
|
for (const file of Object.values(GAME_FILES)) {
|
|
const target = join(incoming, STOCK_BACKUP_FOLDER, file);
|
|
await mkdir(dirname(target), { recursive: true });
|
|
await copyFile(join(sourceFolder, file), target);
|
|
}
|
|
return { replace, remove: [] };
|
|
}, async () => backup);
|
|
}
|
|
|
|
async function exists(path: string): Promise<boolean> {
|
|
try {
|
|
await access(path);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|