100 lines
3.5 KiB
TypeScript
100 lines
3.5 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 hasOfflineBackup = true;
|
|
for (const file of required) {
|
|
if (!(await exists(join(stockRoot, file)))) {
|
|
hasOfflineBackup = false;
|
|
break;
|
|
}
|
|
}
|
|
const resources = resourcePaths();
|
|
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,
|
|
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;
|
|
}
|
|
}
|