feat(decals): import a decal cache for each copy
Store the chosen cache in the copy data directory and use it for future builds. Restore the placeholder index when the copy has no available cache. Verified type checks, the launcher build, a temporary .43 build, and the import and replacement flow from the Windows package.
This commit is contained in:
1 parent
10839f050f
commit
1e3843b7e9
18 files changed
+304
-53
No files matched your search
@@ -0,0 +1,103 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { cp, lstat, mkdir, realpath, rm } from 'node:fs/promises';
|
||||
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';
|
||||
|
||||
const CACHE_FOLDER = 'decal-cache';
|
||||
let changing = false;
|
||||
|
||||
export function decalImportInProgress(): boolean {
|
||||
return changing;
|
||||
}
|
||||
|
||||
export function decalCachePath(dataPath: string, cacheId: string): string {
|
||||
return join(dataPath, CACHE_FOLDER, cacheId);
|
||||
}
|
||||
|
||||
async function cacheRoot(dataPath: string): 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)) !== '') {
|
||||
throw new AppError('decal_import_failed');
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
/** 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,
|
||||
): 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);
|
||||
if (copyPathContains(source, root) || copyPathContains(root, source)) {
|
||||
throw new AppError('decal_source_invalid');
|
||||
}
|
||||
const id = randomUUID();
|
||||
const target = join(root, id);
|
||||
try {
|
||||
await mkdir(root, { recursive: true });
|
||||
await cp(source, target, {
|
||||
recursive: true,
|
||||
force: false,
|
||||
errorOnExist: true,
|
||||
filter: async (path) => {
|
||||
signal?.throwIfAborted();
|
||||
const entry = await lstat(path);
|
||||
if (!entry.isDirectory() && !entry.isFile()) throw new AppError('decal_source_invalid');
|
||||
return true;
|
||||
},
|
||||
});
|
||||
signal?.throwIfAborted();
|
||||
if (playerDecalCacheProblem(target)) throw new AppError('decal_source_invalid');
|
||||
return id;
|
||||
} catch (error) {
|
||||
await discardDecalCache(dataPath, id);
|
||||
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> {
|
||||
try {
|
||||
const root = await cacheRoot(dataPath);
|
||||
const target = decalCachePath(dataPath, id);
|
||||
const actual = await realpath(target).catch(() => null);
|
||||
if (actual && relative(root, dirname(actual)) === '' && relative(join(root, id), actual) === '') {
|
||||
await rm(actual, { recursive: true, force: true });
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('[decals] could not remove the replaced cache:', error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function changeDecalSource(id: string, source: string | null): Promise<Copies> {
|
||||
if (changing) throw new AppError('maintenance_busy');
|
||||
changing = true;
|
||||
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);
|
||||
let copies: Copies;
|
||||
try {
|
||||
copies = await updateCopyRecord(id, (current) => {
|
||||
const { decalCacheId, ...other } = current;
|
||||
return cacheId === undefined ? other : { ...other, decalCacheId: cacheId };
|
||||
});
|
||||
} catch (error) {
|
||||
if (cacheId) await discardDecalCache(record.dataPath, cacheId);
|
||||
throw error;
|
||||
}
|
||||
if (record.decalCacheId) await discardDecalCache(record.dataPath, record.decalCacheId);
|
||||
return copies;
|
||||
} finally {
|
||||
changing = false;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
import type { BigIntStats } from 'node:fs';
|
||||
import { access, mkdir } from 'node:fs/promises';
|
||||
import { join } 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 { 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';
|
||||
@@ -91,6 +93,14 @@ async function install(request: InstallRequest, signal: AbortSignal, report: (pr
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -99,30 +109,41 @@ async function install(request: InstallRequest, signal: AbortSignal, report: (pr
|
||||
if (check.unavailable.length && !request.missingFeaturesAccepted) throw new AppError('missing_features_not_accepted');
|
||||
signal.throwIfAborted();
|
||||
|
||||
const copied = request.method === 'copy'
|
||||
? await copyGame(request.sourcePath, copyPath, target!.freeBytes, useHardLinks ? game.version : null, signal, report)
|
||||
: undefined;
|
||||
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);
|
||||
}
|
||||
try {
|
||||
const copied = request.method === 'copy'
|
||||
? await copyGame(request.sourcePath, copyPath, target!.freeBytes, useHardLinks ? game.version : null, signal, report)
|
||||
: undefined;
|
||||
|
||||
report({ phase: 'backing-up', doneBytes: 0, totalBytes: 0, doneFiles: 0, totalFiles: 0 });
|
||||
const stockBackup = await backUpStockFiles(copyPath);
|
||||
report({ phase: 'backing-up', doneBytes: 0, totalBytes: 0, doneFiles: 0, totalFiles: 0 });
|
||||
const stockBackup = 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,
|
||||
method: request.method,
|
||||
copyPath,
|
||||
dataPath: request.dataPath,
|
||||
stockBackupPath: stockBackup,
|
||||
gameVersion: game.version,
|
||||
unavailablePatches: check.unavailable,
|
||||
installedAt: new Date().toISOString(),
|
||||
lastCheckedAt: new Date().toISOString(),
|
||||
...copied,
|
||||
};
|
||||
await writeInstallRecord(record);
|
||||
return record;
|
||||
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,
|
||||
method: request.method,
|
||||
copyPath,
|
||||
dataPath: request.dataPath,
|
||||
stockBackupPath: stockBackup,
|
||||
gameVersion: game.version,
|
||||
unavailablePatches: check.unavailable,
|
||||
installedAt: new Date().toISOString(),
|
||||
lastCheckedAt: new Date().toISOString(),
|
||||
...(decalCacheId ? { decalCacheId } : {}),
|
||||
...copied,
|
||||
};
|
||||
await writeInstallRecord(record);
|
||||
return record;
|
||||
} catch (error) {
|
||||
if (decalCacheId) await discardDecalCache(request.dataPath, decalCacheId);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function copyGame(
|
||||
|
||||
@@ -7,6 +7,7 @@ import { executableVersion, GAME_FILES, isInSteamLibrary } from '@wto/patcher';
|
||||
import { AppError } from '@shared/errors.ts';
|
||||
import { ORDINARY_LAUNCH_ENABLED, type LaunchState } from '@shared/schemas/launch.ts';
|
||||
import { getSettings } from '../config/settings-store.ts';
|
||||
import { decalCachePath } from '../install/decals.ts';
|
||||
import { copyPathContains, readInstallRecord } from '../install/install-record.ts';
|
||||
import { resourcePaths } from '../resources/resource-paths.ts';
|
||||
import { saveBackup } from '../saves/save-archive.ts';
|
||||
@@ -153,6 +154,7 @@ async function launch(job: LaunchJob): Promise<void> {
|
||||
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, {
|
||||
|
||||
Reference in new issue
Block a user