diff --git a/apps/launcher/src/main/services/patch-pack/pack-store.ts b/apps/launcher/src/main/services/patch-pack/pack-store.ts index a9f783d..b95b5fa 100644 --- a/apps/launcher/src/main/services/patch-pack/pack-store.ts +++ b/apps/launcher/src/main/services/patch-pack/pack-store.ts @@ -11,12 +11,13 @@ * Only the active pack is kept. */ import { randomBytes } from 'node:crypto'; -import { readFileSync } from 'node:fs'; +import { existsSync, readFileSync } from 'node:fs'; import { mkdir, readdir, rename, rm, writeFile } from 'node:fs/promises'; import { dirname, join } from 'node:path'; import { app } from 'electron'; import { checkPatchSet, + comparePackVersions, formatManifest, isPackVersion, manifestIncompatibility, @@ -44,17 +45,31 @@ function activeFile(): string { return join(storeFolder(), 'active.json'); } +/** The version of a bundled set that carries a manifest (a packaged launcher's), else null. */ +export function bundledPatchVersion(bundledRoot: string): string | null { + const path = join(bundledRoot, 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; + } +} + /** - * The active downloaded pack, when there is one this launcher can use. Synchronous, because the - * resource paths are; it reads two small files. + * The active downloaded pack, when there is one this launcher can use and it is newer than the + * bundled patch set. Synchronous, because the resource paths are; it reads two small files. */ -export function activeDownloadedPack(): DownloadedPack | null { +export function activeDownloadedPack(bundledRoot?: string): DownloadedPack | null { try { const active = JSON.parse(readFileSync(activeFile(), 'utf8')) as { version?: unknown }; if (typeof active.version !== 'string' || !isPackVersion(active.version)) return null; const root = join(storeFolder(), active.version); const manifest = parseManifest(readFileSync(join(root, MANIFEST_FILE), 'utf8')); if (manifest.version !== active.version || manifestIncompatibility(manifest, app.getVersion())) return null; + const bundled = bundledRoot ? bundledPatchVersion(bundledRoot) : null; + if (bundled && comparePackVersions(manifest.version, bundled) <= 0) return null; return { root, manifest }; } catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') console.warn('[patch-pack] the active pack is unusable:', error); diff --git a/apps/launcher/src/main/services/patch-pack/pack-update.ts b/apps/launcher/src/main/services/patch-pack/pack-update.ts index 0fa60f4..472ad6f 100644 --- a/apps/launcher/src/main/services/patch-pack/pack-update.ts +++ b/apps/launcher/src/main/services/patch-pack/pack-update.ts @@ -6,12 +6,9 @@ * 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, @@ -22,7 +19,7 @@ import { } 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 { activeDownloadedPack, bundledPatchVersion, installPack, useBundledPatchSet } from './pack-store.ts'; import { downloadReleaseFile, FeedUnreachableError, newestProjectRelease, ReleasePageLimitError, type PackReleaseFiles } from './release-feed.ts'; /** The manifest copy beside a release is a few hundred bytes. */ @@ -31,10 +28,10 @@ const MANIFEST_COPY_MAX_BYTES = 64 * 1024; 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) }; + const pack = usesDownloadedPacks() ? activeDownloadedPack(root) : null; + if (pack) return { source: 'downloaded', version: pack.manifest.version, gameVersions: [...pack.manifest.gameVersions] }; + return { source: 'bundled', version: bundledPatchVersion(root), gameVersions: recordedGameVersions(root) }; } export async function checkPackUpdate(): Promise { @@ -103,18 +100,6 @@ 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 ReleasePageLimitError) return 'page_limit'; if (error instanceof FeedUnreachableError) return 'network'; diff --git a/apps/launcher/src/main/services/resources/resource-paths.ts b/apps/launcher/src/main/services/resources/resource-paths.ts index bedc7cd..70f9857 100644 --- a/apps/launcher/src/main/services/resources/resource-paths.ts +++ b/apps/launcher/src/main/services/resources/resource-paths.ts @@ -19,7 +19,7 @@ export interface ResourcePaths { export function resourcePaths(): ResourcePaths { const bundled = bundledResourcePaths(); - const pack = usesDownloadedPacks() ? activeDownloadedPack() : null; + const pack = usesDownloadedPacks() ? activeDownloadedPack(bundled.patches) : null; return pack ? { ...bundled, patches: pack.root } : bundled; } diff --git a/scripts/build-windows-installer.ts b/scripts/build-windows-installer.ts index fd06f06..ea38ca8 100644 --- a/scripts/build-windows-installer.ts +++ b/scripts/build-windows-installer.ts @@ -4,6 +4,7 @@ import { tmpdir } from 'node:os'; import { dirname, join, relative, resolve, sep } from 'node:path'; import { Arch, build, Platform } from 'electron-builder'; import { launcherReleaseFiles } from '../apps/launcher/src/shared/schemas/app-update.ts'; +import { formatManifest, MANIFEST_FILE, PATCH_PACK_FORMAT, recordedGameVersions } from '../packages/patcher/src/index.ts'; const repository = resolve(import.meta.dirname, '..'); const launcher = join(repository, 'apps', 'launcher'); @@ -68,6 +69,15 @@ async function stageInstaller(stage: string, manifest: LauncherManifest): Promis for (const folder of ['exe', 'gui', 'native', 'versions']) { await cp(join(repository, 'patches', folder), join(resources, 'patches', folder), { recursive: true }); } + const now = new Date(); + const bundledPackVersion = `${now.getUTCFullYear()}.${String(now.getUTCMonth() + 1).padStart(2, '0')}.${String(now.getUTCDate()).padStart(2, '0')}`; + await writeFile(join(resources, 'patches', MANIFEST_FILE), formatManifest({ + format: PATCH_PACK_FORMAT, + version: bundledPackVersion, + minAppVersion: version, + gameVersions: recordedGameVersions(join(resources, 'patches')), + createdAt: now.toISOString(), + }), 'utf8'); const runtimeModules = join(stage, 'installer-modules'); for (const folder of workspacePackages) await stageWorkspacePackage(folder, resources, runtimeModules); for (const name of ['fflate', 'zod']) {