feat(launcher): prompt for stock source when adding patched client without backup
This commit is contained in:
1 parent
39228c6e75
commit
bfedc3d27e
13 files changed
+195
-41
No files matched your search
@@ -1,14 +1,18 @@
|
||||
import { access, readFile, realpath, stat } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
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, and whether it is inside a Steam library. Only reads.
|
||||
* 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);
|
||||
@@ -30,20 +34,42 @@ export async function inspectGameFolder(path: string): Promise<GameFolderInspect
|
||||
const executablePath = hasOfflineBackup
|
||||
? join(stockRoot, GAME_FILES.executable)
|
||||
: join(resolved, GAME_FILES.executable);
|
||||
const executableBytes = (!hasOfflineBackup && missingFiles.includes(GAME_FILES.executable))
|
||||
? null
|
||||
: await readBytes(executablePath);
|
||||
const version = executableBytes ? readVersionFromBytes(executableBytes) : null;
|
||||
let needsStockSource = false;
|
||||
if (!hasOfflineBackup && missingFiles.length === 0) {
|
||||
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 || (executableBytes !== null && executableBytes.includes(PATCHED_EXE_MARKER));
|
||||
}
|
||||
return {
|
||||
path,
|
||||
resolvedPath: resolved,
|
||||
missingFiles,
|
||||
version: !hasOfflineBackup && missingFiles.includes(GAME_FILES.executable) ? null : await readVersion(executablePath),
|
||||
version,
|
||||
verifiedVersions: recordedGameVersions(resources.patches),
|
||||
inSteamLibrary: isInSteamLibrary(path) || isInSteamLibrary(resolved),
|
||||
hasOfflineBackup,
|
||||
needsStockSource,
|
||||
};
|
||||
}
|
||||
|
||||
async function readVersion(executablePath: string): Promise<string | null> {
|
||||
async function readBytes(filePath: string): Promise<Buffer | null> {
|
||||
try {
|
||||
return executableVersion(await readFile(executablePath));
|
||||
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.
|
||||
|
||||
@@ -12,7 +12,7 @@ import { discardDecalCache, importDecalCache } from './decals.ts';
|
||||
import { hardlinkResource, hasHardlinkEvidence, verifyHardlinkResource } from './hardlink-files.ts';
|
||||
import { canonicalCopyPath, copyPathContains, requireAvailableCopy, writeInstallRecord } from './install-record.ts';
|
||||
import { checkPatches } from './patch-check.ts';
|
||||
import { backUpStockFiles, installStockSource, refreshStockBackup, requireStockBackupLocation, stockBackupPath } from './stock-backup.ts';
|
||||
import { backUpStockFiles, backUpStockFilesFrom, installStockSource, refreshStockBackup, requireStockBackupLocation, stockBackupPath } from './stock-backup.ts';
|
||||
|
||||
/**
|
||||
* Install the offline version: check the patches against the player's game,
|
||||
@@ -57,19 +57,25 @@ export function cancelInstall(): boolean {
|
||||
}
|
||||
|
||||
/** Which patches fit the game in `gameFolder`, for the window to show before installing. */
|
||||
export async function checkGameFolder(gameFolder: string, dataPath: string): Promise<PatchCheck> {
|
||||
const { resolvedPath, version } = await requireGameFolder(gameFolder);
|
||||
const stock = await installStockSource(resolvedPath, version);
|
||||
export async function checkGameFolder(gameFolder: string, dataPath: string, stockSourcePath?: string): Promise<PatchCheck> {
|
||||
const game = await requireGameFolder(gameFolder);
|
||||
const stock = stockSourcePath
|
||||
? await resolveExternalStockSource(stockSourcePath, game.version)
|
||||
: game.needsStockSource
|
||||
? (() => { throw new AppError('stock_source_invalid'); })()
|
||||
: await installStockSource(game.resolvedPath, game.version);
|
||||
const data = await canonicalCopyPath(dataPath);
|
||||
await requireUnredirectedData(data);
|
||||
return checkPatches(stock.path, data);
|
||||
}
|
||||
|
||||
async function install(request: InstallRequest, signal: AbortSignal, report: (progress: InstallProgress) => void): Promise<InstallRecord> {
|
||||
const { version, requestedCopyPath, copyPath, dataPath, sourcePath, target, useHardLinks, copyInSteam, dataInSteam } =
|
||||
const { version, requestedCopyPath, copyPath, dataPath, sourcePath, externalStockPath, target, useHardLinks, copyInSteam, dataInSteam } =
|
||||
await inspectInstallation(request);
|
||||
report({ phase: 'checking', doneBytes: 0, totalBytes: 0, doneFiles: 0, totalFiles: 0 });
|
||||
const stock = await installStockSource(sourcePath, version);
|
||||
const stock = externalStockPath
|
||||
? { path: externalStockPath, refresh: false }
|
||||
: await installStockSource(sourcePath, version);
|
||||
await mkdir(dataPath, { recursive: true });
|
||||
const check = await checkPatches(stock.path, dataPath);
|
||||
const essential = check.unavailable.filter((patch) => patch.essential);
|
||||
@@ -88,9 +94,11 @@ async function install(request: InstallRequest, signal: AbortSignal, report: (pr
|
||||
: undefined;
|
||||
|
||||
report({ phase: 'backing-up', doneBytes: 0, totalBytes: 0, doneFiles: 0, totalFiles: 0 });
|
||||
const stockBackup = (request.method === 'in-place' || request.method === 'existing') && stock.refresh
|
||||
? await refreshStockBackup(copyPath)
|
||||
: await backUpStockFiles(copyPath);
|
||||
const stockBackup = externalStockPath
|
||||
? await backUpStockFilesFrom(copyPath, externalStockPath)
|
||||
: (request.method === 'in-place' || request.method === 'existing') && stock.refresh
|
||||
? await refreshStockBackup(copyPath)
|
||||
: await backUpStockFiles(copyPath);
|
||||
|
||||
report({ phase: 'recording', doneBytes: 0, totalBytes: 0, doneFiles: 0, totalFiles: 0 });
|
||||
const record: InstallRecord = {
|
||||
@@ -120,13 +128,22 @@ async function install(request: InstallRequest, signal: AbortSignal, report: (pr
|
||||
async function inspectInstallation(request: InstallRequest) {
|
||||
const gameFolder = request.method === 'copy' ? request.sourcePath : request.gamePath;
|
||||
const game = await requireGameFolder(gameFolder);
|
||||
let externalStockPath: string | null = null;
|
||||
if (request.method === 'copy') {
|
||||
if (game.needsStockSource) throw new AppError('stock_source_invalid');
|
||||
} else if (request.stockSourcePath) {
|
||||
externalStockPath = (await resolveExternalStockSource(request.stockSourcePath, game.version)).path;
|
||||
} else if (game.needsStockSource) {
|
||||
throw new AppError('stock_source_invalid');
|
||||
}
|
||||
const requestedCopyPath = request.method === 'copy' ? request.copyPath : request.gamePath;
|
||||
const [copyPath, dataPath, sourcePath] = await Promise.all([
|
||||
canonicalCopyPath(requestedCopyPath), canonicalCopyPath(request.dataPath), canonicalCopyPath(gameFolder),
|
||||
]);
|
||||
await requireAvailableCopy(request.id, copyPath, dataPath);
|
||||
if (!isAbsolute(request.dataPath) || copyPathContains(stockBackupPath(copyPath), dataPath) ||
|
||||
(request.method === 'copy' && copyPathContains(sourcePath, dataPath))) {
|
||||
(request.method === 'copy' && copyPathContains(sourcePath, dataPath)) ||
|
||||
(externalStockPath && copyPathContains(externalStockPath, dataPath))) {
|
||||
throw new AppError('target_not_usable', { problem: 'overlaps_stock' });
|
||||
}
|
||||
|
||||
@@ -145,7 +162,16 @@ async function inspectInstallation(request: InstallRequest) {
|
||||
throw new AppError('steam_library_not_confirmed');
|
||||
}
|
||||
await Promise.all([requireStockBackupLocation(copyPath), requireUnredirectedData(dataPath)]);
|
||||
return { version: game.version, requestedCopyPath, copyPath, dataPath, sourcePath, target, useHardLinks, copyInSteam, dataInSteam };
|
||||
return { version: game.version, requestedCopyPath, copyPath, dataPath, sourcePath, externalStockPath, target, useHardLinks, copyInSteam, dataInSteam };
|
||||
}
|
||||
|
||||
async function resolveExternalStockSource(stockSourcePath: string, expectedVersion: string | null) {
|
||||
const stockGame = await requireGameFolder(stockSourcePath);
|
||||
if (stockGame.needsStockSource) throw new AppError('stock_source_invalid');
|
||||
if (expectedVersion !== null && stockGame.version !== null && expectedVersion !== stockGame.version) {
|
||||
throw new AppError('stock_source_version_mismatch', { expected: expectedVersion, actual: stockGame.version });
|
||||
}
|
||||
return installStockSource(stockGame.resolvedPath, stockGame.version);
|
||||
}
|
||||
|
||||
async function copyGame(
|
||||
|
||||
@@ -32,12 +32,20 @@ export async function requireStockBackupLocation(copyFolder: string): Promise<st
|
||||
* 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(copyFolder, file), kept, constants.COPYFILE_EXCL);
|
||||
await copyFile(join(sourceFolder, file), kept, constants.COPYFILE_EXCL);
|
||||
}
|
||||
return backup;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user