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:
N0VAN0M4LY committed 2026-11-30 15:23:17 +08:00
1 parent 84158f719d
commit 5a78b1fdf8
28 files changed
+813 -459

No files matched your search

@@ -5,15 +5,17 @@ import { app, BrowserWindow, shell } from 'electron';
import { AccountStore, LOOPBACK } from '@wto/server';
import { executableVersion, GAME_FILES, isInSteamLibrary } from '@wto/patcher';
import { AppError } from '@shared/errors.ts';
import type { InstallRecord } from '@shared/schemas/install.ts';
import type { GameExit, LaunchState } from '@shared/schemas/launch.ts';
import type { Settings } from '@shared/schemas/settings.ts';
import { getSettings } from '../config/settings-store.ts';
import { decalCachePath } from '../install/decals.ts';
import { copyPathContains, readInstallRecord } from '../install/install-record.ts';
import { canonicalCopyPath, copyPathContains, readInstallRecord } from '../install/install-record.ts';
import { resourcePaths } from '../resources/resource-paths.ts';
import { saveBackup } from '../saves/save-archive.ts';
import { buildCopy } from './build-copy.ts';
import { gameProcessRunning } from './game-process.ts';
import { readInstalledLab, runLab } from './lab-handoff.ts';
import { readInstalledLab, runLab, type Lab } from './lab-handoff.ts';
import { localServerStatusUrl } from './local-server.ts';
import { ordinaryLaunchPaths, runOrdinaryLaunch } from './ordinary-launch.ts';
@@ -104,61 +106,10 @@ async function launch(job: LaunchJob): Promise<void> {
const [record, settings] = await Promise.all([readInstallRecord(), getSettings()]);
if (!record) throw new AppError('launch_no_copy');
job.copyId = record.id;
const useLab = !app.isPackaged && settings.developerMode;
if (isInSteamLibrary(record.copyPath)) throw new AppError('launch_copy_in_steam');
const currentVersion = await readVersion(record.copyPath, signal);
if (!currentVersion || currentVersion !== record.gameVersion) {
update(job, { phase: 'version-changed', actualVersion: currentVersion });
return;
}
const lab = useLab ? await readInstalledLab() : null;
const { copyPath, stockPath, dataPath } = lab ? {
copyPath: lab.copyPath,
stockPath: settings.labStockPath ?? record.stockBackupPath,
dataPath: settings.labDataPath ?? record.dataPath,
} : await ordinaryLaunchPaths(record);
if (!isAbsolute(stockPath) || !isAbsolute(dataPath)) {
throw new AppError('folder_unreadable', { path: !isAbsolute(stockPath) ? stockPath : dataPath });
}
if (isInSteamLibrary(dataPath) || copyPathContains(stockPath, dataPath)) {
throw new AppError('launch_copy_in_steam');
}
const requestedPort = lab?.port ?? (settings.launchPortMode === 'fixed' ? settings.launchPort : 0);
if (requestedPort) update(job, { phase: 'checking', port: requestedPort });
const [copyVersion, stockVersion, games] = await Promise.all([
lab ? readVersion(copyPath, signal) : currentVersion,
readVersion(stockPath, signal),
gameProcessRunning(signal),
]);
signal.throwIfAborted();
if (!copyVersion || copyVersion !== stockVersion) {
throw new AppError(lab ? 'launch_lab_version' : 'launch_stock_version');
}
if (games) throw new AppError('launch_game_running');
const port = await selectPort(requestedPort);
if (settings.autoSaveBackups) {
for (const { userId } of new AccountStore(dataPath).all()) {
signal.throwIfAborted();
await saveBackup({ ...record, dataPath }, userId, 'launch');
}
}
signal.throwIfAborted();
update(job, { phase: 'building', port });
await buildCopy({
kind: 'build',
stockPath,
copyPath,
dataPath,
port,
...resourcePaths(),
...(record.decalCacheId ? { decalCache: decalCachePath(record.dataPath, record.decalCacheId) } : {}),
acceptedUnavailable: record.unavailablePatches.map(({ patchId }) => patchId),
}, signal, (filesWritten, filesTotal, file) => {
if (!signal.aborted) update(job, {
phase: 'building', port, filesWritten, filesTotal, ...(file === undefined ? {} : { file }),
});
});
const plan = await planLaunch(job, record, settings);
if (plan === null) return;
const { lab, copyPath, stockPath, dataPath, port } = plan;
await buildForLaunch(job, record, plan, settings.autoSaveBackups);
const publish = (phase: LaunchState['phase'], gameExit?: GameExit): void => {
if (signal.aborted) return;
@@ -176,6 +127,85 @@ async function launch(job: LaunchJob): Promise<void> {
update(job, { phase: 'game-exited', gameExit });
}
interface LaunchPlan {
readonly lab: Lab | null;
readonly copyPath: string;
readonly stockPath: string;
readonly dataPath: string;
readonly steamLibraryConfirmed: boolean;
readonly port: number;
}
async function planLaunch(job: LaunchJob, record: InstallRecord, settings: Settings): Promise<LaunchPlan | null> {
const signal = job.stop.signal;
const useLab = !app.isPackaged && settings.developerMode;
if (useLab && (isInSteamLibrary(record.copyPath) || isInSteamLibrary(await canonicalCopyPath(record.copyPath)))) {
throw new AppError('launch_copy_in_steam');
}
const currentVersion = await readVersion(record.copyPath, signal);
if (!currentVersion || currentVersion !== record.gameVersion) {
update(job, { phase: 'version-changed', actualVersion: currentVersion });
return null;
}
const lab = useLab ? await readInstalledLab() : null;
const { copyPath, stockPath, dataPath, steamLibraryConfirmed } = lab ? {
copyPath: lab.copyPath,
stockPath: settings.labStockPath ?? record.stockBackupPath,
dataPath: settings.labDataPath ?? record.dataPath,
steamLibraryConfirmed: false,
} : await ordinaryLaunchPaths(record);
if (!isAbsolute(stockPath) || !isAbsolute(dataPath)) {
throw new AppError('folder_unreadable', { path: !isAbsolute(stockPath) ? stockPath : dataPath });
}
if (lab && (isInSteamLibrary(copyPath) || isInSteamLibrary(dataPath) || isInSteamLibrary(await canonicalCopyPath(dataPath)))) {
throw new AppError('launch_copy_in_steam');
}
if (copyPathContains(stockPath, dataPath)) throw new AppError('launch_location');
const requestedPort = lab?.port ?? (settings.launchPortMode === 'fixed' ? settings.launchPort : 0);
if (requestedPort) update(job, { phase: 'checking', port: requestedPort });
const [copyVersion, stockVersion, games] = await Promise.all([
lab ? readVersion(copyPath, signal) : currentVersion,
readVersion(stockPath, signal),
gameProcessRunning(signal),
]);
signal.throwIfAborted();
if (!copyVersion || copyVersion !== stockVersion) {
throw new AppError(lab ? 'launch_lab_version' : 'launch_stock_version');
}
if (games) throw new AppError('launch_game_running');
const port = await selectPort(requestedPort);
return { lab, copyPath, stockPath, dataPath, steamLibraryConfirmed, port };
}
async function buildForLaunch(job: LaunchJob, record: InstallRecord, plan: LaunchPlan, autoSaveBackups: boolean): Promise<void> {
const signal = job.stop.signal;
const { lab, copyPath, stockPath, dataPath, port, steamLibraryConfirmed } = plan;
if (autoSaveBackups) {
for (const { userId } of new AccountStore(dataPath).all()) {
signal.throwIfAborted();
await saveBackup({ ...record, dataPath }, userId, 'launch');
}
}
signal.throwIfAborted();
update(job, { phase: 'building', port });
await buildCopy({
kind: 'build',
stockPath,
copyPath,
dataPath,
port,
steamLibraryConfirmed,
...resourcePaths(),
...(record.decalCacheId ? { decalCache: decalCachePath(lab ? record.dataPath : dataPath, record.decalCacheId) } : {}),
acceptedUnavailable: record.unavailablePatches.map(({ patchId }) => patchId),
}, signal, (filesWritten, filesTotal, file) => {
if (!signal.aborted) update(job, {
phase: 'building', port, filesWritten, filesTotal, ...(file === undefined ? {} : { file }),
});
});
}
async function readVersion(folder: string, signal: AbortSignal): Promise<string | null> {
try {
return executableVersion(await readFile(join(folder, GAME_FILES.executable), { signal }));
@@ -6,8 +6,10 @@ import { parseLaunchArguments } from '@shared/launch-arguments.ts';
import type { InstallRecord } from '@shared/schemas/install.ts';
import type { GameExit } from '@shared/schemas/launch.ts';
import type { Settings } from '@shared/schemas/settings.ts';
import { canonicalCopyPath, copyPathContains } from '../install/install-record.ts';
import { STOCK_BACKUP_FOLDER } from '../install/stock-backup.ts';
import { requireUnredirectedData } from '../install/data-location.ts';
import { PLAYER_DATA_FOLDERS } from '../install/copy-game.ts';
import { canonicalCopyPath, copyPathContains, steamLibraryNeedsConfirmation } from '../install/install-record.ts';
import { requireStockBackupLocation, stockBackupPath } from '../install/stock-backup.ts';
import { runGameProcess } from './game-process.ts';
import { startLocalServer } from './local-server.ts';
@@ -17,16 +19,25 @@ export async function ordinaryLaunchPaths(record: InstallRecord) {
const [copyPath, stockPath, dataPath] = await Promise.all([
canonicalCopyPath(record.copyPath), canonicalCopyPath(record.stockBackupPath), canonicalCopyPath(record.dataPath),
]);
const executable = await canonicalCopyPath(join(copyPath, GAME_FILES.executable));
const source = record.sourcePath;
if (relative(copyPath, stockPath).toLowerCase() !== STOCK_BACKUP_FOLDER.toLowerCase() ||
relative(copyPath, executable).toLowerCase() !== GAME_FILES.executable.toLowerCase() ||
const source = record.sourcePath === null ? null : await canonicalCopyPath(record.sourcePath);
if (relative(stockBackupPath(copyPath), stockPath) !== '' ||
copyPathContains(stockPath, dataPath) ||
(source !== null && [copyPath, dataPath].some((path) => copyPathContains(source, path)))) {
throw new AppError('launch_location');
}
if (isInSteamLibrary(copyPath) || isInSteamLibrary(dataPath)) throw new AppError('launch_copy_in_steam');
return { copyPath: copyPath, stockPath: stockPath, dataPath: dataPath };
await Promise.all([requireUnredirectedGamePaths(copyPath), requireStockBackupLocation(copyPath), requireUnredirectedData(dataPath)]);
if (steamLibraryNeedsConfirmation(record, copyPath, dataPath)) throw new AppError('launch_steam_confirmation_required');
return { copyPath, stockPath, dataPath, steamLibraryConfirmed: isInSteamLibrary(record.copyPath) || isInSteamLibrary(copyPath) };
}
/** The game and build write to these copy-relative locations. */
async function requireUnredirectedGamePaths(copyPath: string): Promise<void> {
const paths = ['config.blk', join('cache', 'content'), ...PLAYER_DATA_FOLDERS, ...Object.values(GAME_FILES)];
const checks = await Promise.all(paths.map(async (name) => {
const intended = join(copyPath, name);
return relative(intended, await canonicalCopyPath(intended)) === '';
}));
if (checks.includes(false)) throw new AppError('launch_location');
}
type OrdinaryPhase = 'starting-server' | 'starting-game' | 'running' | 'server-running';