import { join } from 'node:path'; import { utilityProcess } from 'electron'; import { AppError } from '@shared/errors.ts'; import type { PatchCheck } from '@shared/schemas/install.ts'; import type { PatchCheckReply, PatchCheckRequest } from '../../workers/patch-check-protocol.ts'; import { resourcePaths } from '../resources/resource-paths.ts'; /** * Which patches fit the stock files in `stockFolder`, worked out in a utility process * (workers/patch-check-worker.ts). Nothing is written. */ export function checkPatches(stockFolder: string, dataFolder: string): Promise { const resources = resourcePaths(); const request: PatchCheckRequest = { stockFolder, dataFolder, patches: resources.patches, networkHookLibrary: resources.networkHookLibrary, }; return new Promise((resolve, reject) => { const worker = utilityProcess.fork(join(import.meta.dirname, 'patch-check-worker.js'), [], { serviceName: 'WTOffline patch check' }); let answered = false; worker.once('message', (reply: PatchCheckReply) => { answered = true; worker.kill(); if (reply.ok) { resolve(reply.check); } else { console.error('[install] the patch check failed:', reply.error); reject(new AppError('patch_check_failed')); } }); worker.once('exit', (code) => { if (answered) return; console.error('[install] the patch check process ended without an answer, exit code', code); reject(new AppError('patch_check_failed')); }); worker.postMessage(request); }); }