fix(copies): honor confirmed Steam locations
Bind confirmation to the selected copy and data paths throughout installation, launch, and maintenance. Validate redirected runtime data paths and recheck original files before refreshing an in-place backup. Verified by type checking and build. The acceptance checklist covers directory links and Steam confirmation.
This commit is contained in:
1 parent
84158f719d
commit
5a78b1fdf8
28 files changed
+813
-459
No files matched your search
@@ -17,7 +17,7 @@ import { linkVerifiedResource, sameResource } from './hardlink-files.ts';
|
||||
* own missions. The offline copy does not take the online game's data (working rules 18); the
|
||||
* game creates these folders again when it needs them.
|
||||
*/
|
||||
const PLAYER_DATA_FOLDERS = new Set(['.game_logs', 'cache', 'replays', 'screenshots', 'usermissions']);
|
||||
export const PLAYER_DATA_FOLDERS = new Set(['.game_logs', 'cache', 'replays', 'screenshots', 'usermissions']);
|
||||
|
||||
interface CopyEntry {
|
||||
readonly relativePath: string;
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { readdir } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
import { AppError } from '@shared/errors.ts';
|
||||
|
||||
/** A data tree must keep the server's saves, logs and caches inside the selected folder. */
|
||||
export async function requireUnredirectedData(root: string): Promise<void> {
|
||||
const folders = [root];
|
||||
for (let folder = folders.pop(); folder !== undefined; folder = folders.pop()) {
|
||||
const entries = await readdir(folder, { withFileTypes: true }).catch((error: NodeJS.ErrnoException) => {
|
||||
if (folder === root && error.code === 'ENOENT') return [];
|
||||
throw error;
|
||||
});
|
||||
for (const entry of entries) {
|
||||
if (entry.isSymbolicLink()) throw new AppError('launch_location');
|
||||
if (entry.isDirectory()) folders.push(join(folder, entry.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import { dirname, isAbsolute, join, relative } from 'node:path';
|
||||
import { isInSteamLibrary, playerDecalCacheProblem } from '@wto/patcher';
|
||||
import { AppError } from '@shared/errors.ts';
|
||||
import type { Copies } from '@shared/schemas/install.ts';
|
||||
import { canonicalCopyPath, copyPathContains, listCopies, updateCopyRecord } from './install-record.ts';
|
||||
import { canonicalCopyPath, copyPathContains, listCopies, steamLibraryNeedsConfirmation, updateCopyRecord } from './install-record.ts';
|
||||
|
||||
const CACHE_FOLDER = 'decal-cache';
|
||||
let changing = false;
|
||||
@@ -17,11 +17,12 @@ export function decalCachePath(dataPath: string, cacheId: string): string {
|
||||
return join(dataPath, CACHE_FOLDER, cacheId);
|
||||
}
|
||||
|
||||
async function cacheRoot(dataPath: string): Promise<string> {
|
||||
async function cacheRoot(dataPath: string, steamDataConfirmed: boolean): Promise<string> {
|
||||
if (!isAbsolute(dataPath)) throw new AppError('decal_import_failed');
|
||||
const data = await canonicalCopyPath(dataPath);
|
||||
const root = join(data, CACHE_FOLDER);
|
||||
if (isInSteamLibrary(data) || relative(root, await canonicalCopyPath(root)) !== '') {
|
||||
if ((isInSteamLibrary(data) && (!steamDataConfirmed || relative(dataPath, data) !== '')) ||
|
||||
relative(root, await canonicalCopyPath(root)) !== '') {
|
||||
throw new AppError('decal_import_failed');
|
||||
}
|
||||
return root;
|
||||
@@ -29,13 +30,13 @@ async function cacheRoot(dataPath: string): Promise<string> {
|
||||
|
||||
/** Import the selected cache as its own snapshot so a later build can use the copy's data alone. */
|
||||
export async function importDecalCache(
|
||||
dataPath: string, sourcePath: string, signal?: AbortSignal,
|
||||
dataPath: string, sourcePath: string, signal?: AbortSignal, steamDataConfirmed = false,
|
||||
): Promise<string> {
|
||||
if (!isAbsolute(sourcePath)) throw new AppError('decal_source_invalid');
|
||||
const source = await realpath(sourcePath).catch(() => null);
|
||||
if (!source || playerDecalCacheProblem(source)) throw new AppError('decal_source_invalid');
|
||||
|
||||
const root = await cacheRoot(dataPath);
|
||||
const root = await cacheRoot(dataPath, steamDataConfirmed);
|
||||
if (copyPathContains(source, root) || copyPathContains(root, source)) {
|
||||
throw new AppError('decal_source_invalid');
|
||||
}
|
||||
@@ -58,16 +59,16 @@ export async function importDecalCache(
|
||||
if (playerDecalCacheProblem(target)) throw new AppError('decal_source_invalid');
|
||||
return id;
|
||||
} catch (error) {
|
||||
await discardDecalCache(dataPath, id);
|
||||
await discardDecalCache(dataPath, id, steamDataConfirmed);
|
||||
if (signal?.aborted || error instanceof AppError) throw error;
|
||||
console.error('[decals] could not import the cache:', error);
|
||||
throw new AppError('decal_import_failed');
|
||||
}
|
||||
}
|
||||
|
||||
export async function discardDecalCache(dataPath: string, id: string): Promise<void> {
|
||||
export async function discardDecalCache(dataPath: string, id: string, steamDataConfirmed = false): Promise<void> {
|
||||
try {
|
||||
const root = await cacheRoot(dataPath);
|
||||
const root = await cacheRoot(dataPath, steamDataConfirmed);
|
||||
const target = decalCachePath(dataPath, id);
|
||||
const actual = await realpath(target).catch(() => null);
|
||||
if (actual && relative(root, dirname(actual)) === '' && relative(join(root, id), actual) === '') {
|
||||
@@ -84,7 +85,10 @@ export async function changeDecalSource(id: string, source: string | null): Prom
|
||||
try {
|
||||
const record = (await listCopies()).copies.find((copy) => copy.id === id);
|
||||
if (!record) throw new AppError('copy_not_found');
|
||||
const cacheId = source === null ? undefined : await importDecalCache(record.dataPath, source);
|
||||
const [copyPath, dataPath] = await Promise.all([canonicalCopyPath(record.copyPath), canonicalCopyPath(record.dataPath)]);
|
||||
if (steamLibraryNeedsConfirmation(record, copyPath, dataPath)) throw new AppError('launch_steam_confirmation_required');
|
||||
const dataInSteam = isInSteamLibrary(dataPath);
|
||||
const cacheId = source === null ? undefined : await importDecalCache(dataPath, source, undefined, dataInSteam);
|
||||
let copies: Copies;
|
||||
try {
|
||||
copies = await updateCopyRecord(id, (current) => {
|
||||
@@ -92,10 +96,10 @@ export async function changeDecalSource(id: string, source: string | null): Prom
|
||||
return cacheId === undefined ? other : { ...other, decalCacheId: cacheId };
|
||||
});
|
||||
} catch (error) {
|
||||
if (cacheId) await discardDecalCache(record.dataPath, cacheId);
|
||||
if (cacheId) await discardDecalCache(dataPath, cacheId, dataInSteam);
|
||||
throw error;
|
||||
}
|
||||
if (record.decalCacheId) await discardDecalCache(record.dataPath, record.decalCacheId);
|
||||
if (record.decalCacheId) await discardDecalCache(dataPath, record.decalCacheId, dataInSteam);
|
||||
return copies;
|
||||
} finally {
|
||||
changing = false;
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
import type { BigIntStats } from 'node:fs';
|
||||
import { access, mkdir } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
import { isAbsolute, join, relative } from 'node:path';
|
||||
import { isInSteamLibrary } from '@wto/patcher';
|
||||
import { AppError } from '@shared/errors.ts';
|
||||
import type { InstallProgress, InstallRecord, InstallRequest, PatchCheck } from '@shared/schemas/install.ts';
|
||||
import { inspectGameFolder } from '../game-folder/inspect-game-folder.ts';
|
||||
import { freeBytes, inspectTargetFolder, sameVolume } from '../game-folder/inspect-target-folder.ts';
|
||||
import { copyPlannedFiles, planCopy, removeInterruptedCopy, type CopyPlan } from './copy-game.ts';
|
||||
import { requireUnredirectedData } from './data-location.ts';
|
||||
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, stockBackupPath } from './stock-backup.ts';
|
||||
import { backUpStockFiles, installStockSource, refreshStockBackup, requireStockBackupLocation, stockBackupPath } from './stock-backup.ts';
|
||||
|
||||
/**
|
||||
* Install the offline version: check the patches against the player's game,
|
||||
@@ -55,55 +56,22 @@ export function cancelInstall(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The stock files a check of `gameFolder` starts from: its backup folder when it already has one
|
||||
* (a folder installed before, whose own files may be patched), otherwise its own files.
|
||||
*/
|
||||
export async function stockFolderOf(gameFolder: string): Promise<string> {
|
||||
const backup = stockBackupPath(gameFolder);
|
||||
try {
|
||||
await access(backup);
|
||||
return backup;
|
||||
} catch {
|
||||
return gameFolder;
|
||||
}
|
||||
}
|
||||
|
||||
/** Which patches fit the game in `gameFolder`, for the window to show before installing. */
|
||||
export async function checkGameFolder(gameFolder: string, dataPath: string): Promise<PatchCheck> {
|
||||
await requireGameFolder(gameFolder);
|
||||
return checkPatches(await stockFolderOf(gameFolder), dataPath);
|
||||
const { resolvedPath, version } = await requireGameFolder(gameFolder);
|
||||
const stock = await installStockSource(resolvedPath, 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 gameFolder = request.method === 'copy' ? request.sourcePath : request.gamePath;
|
||||
const game = await requireGameFolder(gameFolder);
|
||||
const copyPath = request.method === 'copy' ? request.copyPath : request.gamePath;
|
||||
await requireAvailableCopy(request.id, copyPath, request.dataPath);
|
||||
|
||||
const target = request.method === 'copy' ? await inspectTargetFolder(copyPath, gameFolder) : null;
|
||||
if (target?.problem) throw new AppError('target_not_usable', { problem: target.problem });
|
||||
const useHardLinks = request.method === 'copy' && request.useHardLinks === true;
|
||||
if (useHardLinks) {
|
||||
if (!target?.sameDriveAsStock || !hasHardlinkEvidence(game.version)) throw new AppError('hardlink_unavailable');
|
||||
if (copyPathContains(await canonicalCopyPath(gameFolder), await canonicalCopyPath(request.dataPath))) {
|
||||
throw new AppError('target_not_usable', { problem: 'overlaps_stock' });
|
||||
}
|
||||
}
|
||||
const inSteamLibrary = target ? target.inSteamLibrary : game.inSteamLibrary;
|
||||
if (inSteamLibrary && !request.steamLibraryConfirmed) throw new AppError('steam_library_not_confirmed');
|
||||
|
||||
if (request.decalSourcePath) {
|
||||
const dataPath = await canonicalCopyPath(request.dataPath);
|
||||
if (isInSteamLibrary(dataPath) ||
|
||||
(request.method === 'copy' && copyPathContains(await canonicalCopyPath(gameFolder), dataPath))) {
|
||||
throw new AppError('decal_import_failed');
|
||||
}
|
||||
}
|
||||
|
||||
const { version, requestedCopyPath, copyPath, dataPath, sourcePath, target, useHardLinks, copyInSteam, dataInSteam } =
|
||||
await inspectInstallation(request);
|
||||
report({ phase: 'checking', doneBytes: 0, totalBytes: 0, doneFiles: 0, totalFiles: 0 });
|
||||
await mkdir(request.dataPath, { recursive: true });
|
||||
const check = await checkPatches(await stockFolderOf(gameFolder), request.dataPath);
|
||||
const stock = await installStockSource(sourcePath, version);
|
||||
await mkdir(dataPath, { recursive: true });
|
||||
const check = await checkPatches(stock.path, dataPath);
|
||||
const essential = check.unavailable.filter((patch) => patch.essential);
|
||||
if (essential.length) throw new AppError('essential_patches_unavailable', { patches: essential.map((patch) => patch.patchId).join(', ') });
|
||||
if (check.unavailable.length && !request.missingFeaturesAccepted) throw new AppError('missing_features_not_accepted');
|
||||
@@ -112,26 +80,29 @@ async function install(request: InstallRequest, signal: AbortSignal, report: (pr
|
||||
let decalCacheId: string | undefined;
|
||||
if (request.decalSourcePath) {
|
||||
report({ phase: 'importing-decals', doneBytes: 0, totalBytes: 0, doneFiles: 0, totalFiles: 0 });
|
||||
decalCacheId = await importDecalCache(request.dataPath, request.decalSourcePath, signal);
|
||||
decalCacheId = await importDecalCache(dataPath, request.decalSourcePath, signal, dataInSteam);
|
||||
}
|
||||
try {
|
||||
const copied = request.method === 'copy'
|
||||
? await copyGame(request.sourcePath, copyPath, target!.freeBytes, useHardLinks ? game.version : null, signal, report)
|
||||
? await copyGame(sourcePath, copyPath, target!.freeBytes, useHardLinks ? version : null, signal, report)
|
||||
: undefined;
|
||||
|
||||
report({ phase: 'backing-up', doneBytes: 0, totalBytes: 0, doneFiles: 0, totalFiles: 0 });
|
||||
const stockBackup = await backUpStockFiles(copyPath);
|
||||
const stockBackup = request.method === 'in-place' && stock.refresh
|
||||
? await refreshStockBackup(copyPath)
|
||||
: await backUpStockFiles(copyPath);
|
||||
|
||||
report({ phase: 'recording', doneBytes: 0, totalBytes: 0, doneFiles: 0, totalFiles: 0 });
|
||||
const record: InstallRecord = {
|
||||
id: request.id,
|
||||
name: request.name,
|
||||
sourcePath: request.method === 'copy' ? request.sourcePath : null,
|
||||
sourcePath: request.method === 'copy' ? sourcePath : null,
|
||||
method: request.method,
|
||||
copyPath,
|
||||
copyPath: requestedCopyPath,
|
||||
dataPath: request.dataPath,
|
||||
...(copyInSteam || dataInSteam ? { steamLibraryConfirmation: { copyPath, dataPath } } : {}),
|
||||
stockBackupPath: stockBackup,
|
||||
gameVersion: game.version,
|
||||
gameVersion: version,
|
||||
unavailablePatches: check.unavailable,
|
||||
installedAt: new Date().toISOString(),
|
||||
lastCheckedAt: new Date().toISOString(),
|
||||
@@ -141,11 +112,42 @@ async function install(request: InstallRequest, signal: AbortSignal, report: (pr
|
||||
await writeInstallRecord(record);
|
||||
return record;
|
||||
} catch (error) {
|
||||
if (decalCacheId) await discardDecalCache(request.dataPath, decalCacheId);
|
||||
if (decalCacheId) await discardDecalCache(dataPath, decalCacheId, dataInSteam);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function inspectInstallation(request: InstallRequest) {
|
||||
const gameFolder = request.method === 'copy' ? request.sourcePath : request.gamePath;
|
||||
const game = await requireGameFolder(gameFolder);
|
||||
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))) {
|
||||
throw new AppError('target_not_usable', { problem: 'overlaps_stock' });
|
||||
}
|
||||
|
||||
const target = request.method === 'copy' ? await inspectTargetFolder(copyPath, sourcePath) : null;
|
||||
if (target?.problem) throw new AppError('target_not_usable', { problem: target.problem });
|
||||
const useHardLinks = request.method === 'copy' && request.useHardLinks === true;
|
||||
if (useHardLinks && (!target?.sameDriveAsStock || !hasHardlinkEvidence(game.version))) {
|
||||
throw new AppError('hardlink_unavailable');
|
||||
}
|
||||
const copyInSteam = isInSteamLibrary(requestedCopyPath) || isInSteamLibrary(copyPath);
|
||||
const dataInSteam = isInSteamLibrary(request.dataPath) || isInSteamLibrary(dataPath);
|
||||
const { confirmedSteamCopyPath, confirmedSteamDataPath } = request;
|
||||
const copyConfirmed = confirmedSteamCopyPath !== undefined && relative(copyPath, confirmedSteamCopyPath) === '';
|
||||
const dataConfirmed = confirmedSteamDataPath !== undefined && relative(dataPath, confirmedSteamDataPath) === '';
|
||||
if ((copyInSteam && !copyConfirmed) || (dataInSteam && !dataConfirmed)) {
|
||||
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 };
|
||||
}
|
||||
|
||||
async function copyGame(
|
||||
source: string,
|
||||
target: string,
|
||||
|
||||
@@ -2,6 +2,7 @@ import { randomUUID } from 'node:crypto';
|
||||
import { realpath } from 'node:fs/promises';
|
||||
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
||||
import { app } from 'electron';
|
||||
import { isInSteamLibrary } from '@wto/patcher';
|
||||
import { AppError } from '@shared/errors.ts';
|
||||
import { CopiesSchema, type Copies, type InstallRecord } from '@shared/schemas/install.ts';
|
||||
import { readJsonOr, writeJsonAtomic } from '../../util/atomic-json.ts';
|
||||
@@ -106,6 +107,12 @@ export function copyPathContains(parent: string, child: string): boolean {
|
||||
return !isAbsolute(remainder) && remainder !== '..' && !remainder.startsWith('..' + sep);
|
||||
}
|
||||
|
||||
export function steamLibraryNeedsConfirmation(record: InstallRecord, copyPath: string, dataPath: string): boolean {
|
||||
if (![record.copyPath, record.dataPath, copyPath, dataPath].some(isInSteamLibrary)) return false;
|
||||
const confirmed = record.steamLibraryConfirmation;
|
||||
return !confirmed || relative(confirmed.copyPath, copyPath) !== '' || relative(confirmed.dataPath, dataPath) !== '';
|
||||
}
|
||||
|
||||
function overlaps(left: string, right: string): boolean {
|
||||
return copyPathContains(left, right) || copyPathContains(right, left);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { constants } from 'node:fs';
|
||||
import { access, copyFile, mkdir } from 'node:fs/promises';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { GAME_FILES } from '@wto/patcher';
|
||||
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
|
||||
@@ -15,12 +18,21 @@ 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> {
|
||||
const backup = stockBackupPath(copyFolder);
|
||||
const backup = await requireStockBackupLocation(copyFolder);
|
||||
for (const file of Object.values(GAME_FILES)) {
|
||||
const kept = join(backup, file);
|
||||
if (await exists(kept)) continue;
|
||||
@@ -30,6 +42,27 @@ export async function backUpStockFiles(copyFolder: string): Promise<string> {
|
||||
return backup;
|
||||
}
|
||||
|
||||
/** A new game version must pass the patch checks before replacing an existing stock backup. */
|
||||
export async function installStockSource(copyFolder: string, version: string | null) {
|
||||
const backup = await requireStockBackupLocation(copyFolder);
|
||||
if (!await exists(join(backup, GAME_FILES.executable))) return { path: copyFolder, refresh: await exists(backup) };
|
||||
const backedUpVersion = executableVersion(await readFile(join(backup, GAME_FILES.executable)));
|
||||
return version !== null && backedUpVersion !== version ? { path: copyFolder, refresh: true } : { path: backup, refresh: false };
|
||||
}
|
||||
|
||||
export async function refreshStockBackup(copyFolder: 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(copyFolder, file), target);
|
||||
}
|
||||
return { replace, remove: [] };
|
||||
}, async () => backup);
|
||||
}
|
||||
|
||||
async function exists(path: string): Promise<boolean> {
|
||||
try {
|
||||
await access(path);
|
||||
|
||||
Reference in new issue
Block a user