/** * Adaptation pack updates: which patch set the builds use, whether GitHub has a newer pack, and * installing it. The launcher checks at start (when the setting allows) and from the About page; * a pack is installed only when the player confirms. * * A pack is newer when its version is above the active set's. A bundled set without a version * (a repository checkout) is older than every pack. */ import { existsSync, readFileSync } from 'node:fs'; import { join } from 'node:path'; import { app } from 'electron'; import { comparePackVersions, MANIFEST_FILE, manifestIncompatibility, parseManifest, PatchPackError, patchPackTuning, packSigningPublicKey, recordedGameVersions, type PatchPackManifest, } from '@wto/patcher'; import type { ActivePatchSet, PackApplyResult, PackCheck, PackProblem, PackRelease } from '@shared/schemas/patch-pack.ts'; import { bundledResourcePaths, usesDownloadedPacks } from '../resources/resource-paths.ts'; import { activeDownloadedPack, installPack, useBundledPatchSet } from './pack-store.ts'; import { downloadReleaseFile, FeedUnreachableError, newestPackRelease, type PackReleaseFiles } from './release-feed.ts'; /** The manifest copy beside a release is a few hundred bytes. */ const MANIFEST_COPY_MAX_BYTES = 64 * 1024; /** A signature file is 89 bytes. */ const SIGNATURE_MAX_BYTES = 1024; export function activePatchSet(): ActivePatchSet { const pack = usesDownloadedPacks() ? activeDownloadedPack() : null; if (pack) return { source: 'downloaded', version: pack.manifest.version, gameVersions: [...pack.manifest.gameVersions] }; const root = bundledResourcePaths().patches; return { source: 'bundled', version: bundledVersion(root), gameVersions: recordedGameVersions(root) }; } export async function checkPackUpdate(): Promise { const active = activePatchSet(); if (!packSigningPublicKey()) return { status: 'failed', active, latest: null, problem: 'no_signing_key' }; try { const found = await newestRelease(); if (!found || !isNewer(found.manifest.version, active.version)) return { status: 'up_to_date', active, latest: found ? release(found) : null, problem: null }; const status = manifestIncompatibility(found.manifest, app.getVersion()) ? 'needs_app_update' : 'available'; return { status, active, latest: release(found), problem: null }; } catch (error) { console.warn('[patch-pack] update check failed:', error); return { status: 'failed', active, latest: null, problem: problemOf(error) }; } } /** Download and activate the newest pack, when it is newer than the active set and usable. */ export async function applyPackUpdate(): Promise { if (!packSigningPublicKey()) return { ok: false, problem: 'no_signing_key' }; try { const found = await newestRelease(); if (!found || !isNewer(found.manifest.version, activePatchSet().version)) return { ok: false, problem: 'up_to_date' }; if (manifestIncompatibility(found.manifest, app.getVersion())) return { ok: false, problem: 'needs_app_update' }; const tuning = patchPackTuning(); const zip = await downloadReleaseFile(found.files.zipUrl, tuning.maxPackBytes); const signature = new TextDecoder().decode(await downloadReleaseFile(found.files.signatureUrl, SIGNATURE_MAX_BYTES)); await installPack(found.files.version, zip, signature); console.info(`[patch-pack] pack ${found.files.version} installed and active`); return { ok: true, active: activePatchSet() }; } catch (error) { console.warn('[patch-pack] update failed:', error); return { ok: false, problem: problemOf(error) }; } } /** Stop using downloaded packs. */ export async function revertToBundledPatchSet(): Promise { await useBundledPatchSet(); return activePatchSet(); } interface FoundRelease { readonly files: PackReleaseFiles; readonly manifest: PatchPackManifest; } async function newestRelease(): Promise { const files = await newestPackRelease(); if (!files) return null; const text = new TextDecoder().decode(await downloadReleaseFile(files.manifestUrl, MANIFEST_COPY_MAX_BYTES)); const manifest = parseManifest(text); if (manifest.version !== files.version) throw new PatchPackError('contents', `release ${files.version} carries the manifest of ${manifest.version}`); return { files, manifest }; } function release(found: FoundRelease): PackRelease { return { version: found.manifest.version, gameVersions: [...found.manifest.gameVersions], minAppVersion: found.manifest.minAppVersion, publishedAt: found.files.publishedAt, }; } function isNewer(candidate: string, active: string | null): boolean { return active === null || comparePackVersions(candidate, active) > 0; } /** The version of a bundled set that carries a manifest (a packaged launcher's), else null. */ function bundledVersion(root: string): string | null { const path = join(root, MANIFEST_FILE); if (!existsSync(path)) return null; try { return parseManifest(readFileSync(path, 'utf8')).version; } catch (error) { console.warn('[patch-pack] the bundled manifest is unreadable:', error); return null; } } function problemOf(error: unknown): PackProblem { if (error instanceof FeedUnreachableError) return 'network'; if (error instanceof PatchPackError) { if (error.problem === 'signature') return 'signature'; if (error.problem === 'size') return 'size'; if (error.problem === 'key') return 'no_signing_key'; } return 'contents'; }