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:
N0VAN0M4LY committed 2026-11-30 09:59:14 +08:00
1 parent 10839f050f
commit 1e3843b7e9
18 files changed
+304 -53

No files matched your search

@@ -8,6 +8,7 @@ export interface BuildRequest {
readonly port: number;
readonly patches: string;
readonly networkHookLibrary: string;
readonly decalCache?: string;
readonly acceptedUnavailable: readonly string[];
}
+20 -6
View File
@@ -1,24 +1,37 @@
import { readFileSync } from 'node:fs';
import { readFileSync, realpathSync } from 'node:fs';
import { relative } from 'node:path';
import type { MessageEvent } from 'electron';
import { assertWritableCopy, buildGameCopy, readStockGameFiles, writeGameCopy, type GameCopyFile } from '@wto/patcher';
import { assertWritableCopy, buildGameCopy, copyPlayerDecalCache, playerDecalCacheProblem, readStockGameFiles, writeGameCopy, type GameCopyFile } from '@wto/patcher';
import { multiVehiclePackIds } from '@wto/server';
import type { BuildCommand, BuildReply, BuildRequest } from './build-protocol.ts';
let prepared: { readonly copyPath: string; readonly files: readonly GameCopyFile[] } | null = null;
let prepared: { readonly copyPath: string; readonly files: readonly GameCopyFile[]; readonly decalCache: string | undefined } | null = null;
function send(reply: BuildReply): void {
process.parentPort.postMessage(reply);
}
function importedDecals(path: string | undefined): string | undefined {
if (!path) return undefined;
try {
const imported = realpathSync(path);
if (relative(path, imported) === '' && !playerDecalCacheProblem(imported)) return imported;
} catch (error) {
console.warn('[build] the imported decal cache is unavailable:', error);
}
return undefined;
}
function build(request: BuildRequest): void {
assertWritableCopy(request.stockPath, request.copyPath);
const decalCache = importedDecals(request.decalCache);
const result = buildGameCopy({
stock: readStockGameFiles(request.stockPath),
patches: request.patches,
networkHookLibrary: readFileSync(request.networkHookLibrary),
port: request.port,
multiVehiclePackIds: multiVehiclePackIds(request.dataPath, console.info),
placeholderDecals: true,
placeholderDecals: decalCache === undefined,
});
const unavailable = [...result.exePatches.failures, ...result.guiPatches.failures, ...result.nativeModules.failures];
const accepted = new Set(request.acceptedUnavailable);
@@ -27,7 +40,7 @@ function build(request: BuildRequest): void {
send({ kind: 'failed', error: 'launch_build_unavailable' });
return;
}
prepared = { copyPath: request.copyPath, files: result.files };
prepared = { copyPath: request.copyPath, files: result.files, decalCache };
send({ kind: 'prepared', total: prepared.files.length });
}
@@ -39,8 +52,9 @@ process.parentPort.on('message', (event: MessageEvent) => {
return;
}
if (!prepared) throw new Error('No build is ready to write.');
const { copyPath, files } = prepared;
const { copyPath, files, decalCache } = prepared;
prepared = null;
if (decalCache) copyPlayerDecalCache(decalCache, copyPath);
for (const [index, file] of files.entries()) {
writeGameCopy(copyPath, [file]);
send({ kind: 'progress', written: index + 1, total: files.length, file: file.path });