feat(launcher): detect missing client folders and support relocating while keeping saves
This commit is contained in:
1 parent
2820d40d87
commit
a0515c4ed3
12 files changed
+199
-20
No files matched your search
@@ -0,0 +1,22 @@
|
||||
import { AppError } from '@shared/errors.ts';
|
||||
import type { Copies } from '@shared/schemas/install.ts';
|
||||
import { inspectGameFolder } from '../game-folder/inspect-game-folder.ts';
|
||||
import { hydrateCopiesStatus, relocateCopyRecord } from '../install/install-record.ts';
|
||||
import { backUpStockFiles } from '../install/stock-backup.ts';
|
||||
|
||||
/**
|
||||
* Re-point an existing registered client to its moved folder while keeping its id, name, saves,
|
||||
* and data folder intact.
|
||||
*/
|
||||
export async function relocateCopy(id: string, newPath: string): Promise<Copies> {
|
||||
const game = await inspectGameFolder(newPath);
|
||||
if (game.missingFiles.length) throw new AppError('not_a_game_folder', { path: newPath });
|
||||
if (game.needsStockSource) throw new AppError('stock_source_invalid');
|
||||
const stockBackupPath = await backUpStockFiles(game.resolvedPath);
|
||||
const updated = await relocateCopyRecord(id, {
|
||||
copyPath: newPath,
|
||||
stockBackupPath,
|
||||
gameVersion: game.version,
|
||||
});
|
||||
return hydrateCopiesStatus(updated);
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { realpath } from 'node:fs/promises';
|
||||
import { access, 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 { GAME_FILES, 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';
|
||||
@@ -75,11 +75,72 @@ export function renameCopy(id: string, name: string): Promise<Copies> {
|
||||
return updateCopyRecord(id, (record) => ({ ...record, name }));
|
||||
}
|
||||
|
||||
export async function copyFolderMissing(copyPath: string): Promise<boolean> {
|
||||
for (const file of Object.values(GAME_FILES)) {
|
||||
try {
|
||||
await access(join(copyPath, file));
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Attach live `folderMissing` status to each registered copy without modifying `copies.json`. */
|
||||
export async function hydrateCopiesStatus(registry: Copies): Promise<Copies> {
|
||||
const copies = await Promise.all(registry.copies.map(async (copy) => {
|
||||
const missing = await copyFolderMissing(copy.copyPath);
|
||||
const { folderMissing: _old, ...base } = copy;
|
||||
return missing ? { ...base, folderMissing: true } : base;
|
||||
}));
|
||||
return { ...registry, copies };
|
||||
}
|
||||
|
||||
export function relocateCopyRecord(
|
||||
id: string,
|
||||
next: { readonly copyPath: string; readonly stockBackupPath: string; readonly gameVersion: string | null },
|
||||
): Promise<Copies> {
|
||||
return changeRegistry(async (registry) => {
|
||||
const current = registry.copies.find((copy) => copy.id === id);
|
||||
if (!current) throw new AppError('copy_not_found');
|
||||
const resolvedCopy = await canonicalCopyPath(next.copyPath);
|
||||
const resolvedData = await canonicalCopyPath(current.dataPath);
|
||||
if (overlaps(resolvedCopy, resolvedData) && copyPathContains(next.stockBackupPath, resolvedData)) {
|
||||
throw new AppError('copy_location_in_use');
|
||||
}
|
||||
for (const existing of registry.copies) {
|
||||
if (existing.id === id) continue;
|
||||
const used = await Promise.all([canonicalCopyPath(existing.copyPath), canonicalCopyPath(existing.dataPath)]);
|
||||
if (used.some((other) => overlaps(resolvedCopy, other))) {
|
||||
throw new AppError('copy_location_in_use');
|
||||
}
|
||||
}
|
||||
return {
|
||||
...registry,
|
||||
copies: registry.copies.map((record) => {
|
||||
if (record.id !== id) return record;
|
||||
const { steamLibraryConfirmation: _staleSteam, folderMissing: _staleMissing, ...rest } = record;
|
||||
return {
|
||||
...rest,
|
||||
copyPath: next.copyPath,
|
||||
stockBackupPath: next.stockBackupPath,
|
||||
gameVersion: next.gameVersion,
|
||||
};
|
||||
}),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/** Serialize writes so switching, renaming and completing an installation preserve each other. */
|
||||
function changeRegistry(change: (registry: Copies) => Promise<Copies> | Copies): Promise<Copies> {
|
||||
const operation = pending.then(async () => {
|
||||
try {
|
||||
const updated = CopiesSchema.parse(await change(await readRegistry()));
|
||||
const next = await change(await readRegistry());
|
||||
const sanitized: Copies = {
|
||||
...next,
|
||||
copies: next.copies.map(({ folderMissing: _ignored, ...rest }) => rest),
|
||||
};
|
||||
const updated = CopiesSchema.parse(sanitized);
|
||||
await writeJsonAtomic(recordPath(), updated);
|
||||
return updated;
|
||||
} catch (error) {
|
||||
|
||||
Reference in new issue
Block a user