Files
WT-Offline/apps/launcher/src/main/services/patch-pack/pack-update.ts
T
N0VAN0M4LY 3e2acc5a17 Release adaptation packs and let the launcher install them
An adaptation pack is the patch set (exe, GUI, native definitions and
version records) released on its own, so a new game build can be adapted
without a new offline version. It is a zip with a manifest, signed with
Ed25519, published as a GitHub release tagged patches-<YYYY.MM.DD[.N]>.

- packages/patcher/src/patch-pack: manifest and version numbers, zip
  writing and reading with path and size checks, signatures, a check that
  a patch set reads as a whole; tuning/patch-pack.jsonc holds the
  repository and the limits.
- scripts/generate-patch-signing-key.ts writes the public key for the
  repository and the private key outside it; scripts/build-patch-pack.ts
  packs only committed files and signs with PATCH_SIGNING_KEY.
- .github/workflows/patch-pack.yml packs, signs and publishes on a
  patches-* tag or a manual run.
- Launcher main process: find the newest pack release, download, verify,
  unpack into user data and make it active; resource paths use the
  active pack when it is usable (a repository checkout only with
  WTO_USE_PATCH_PACK=1). The pages that show and start this come later.
2026-11-29 16:08:37 +08:00

127 lines
5.4 KiB
TypeScript

/**
* 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<PackCheck> {
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<PackApplyResult> {
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<ActivePatchSet> {
await useBundledPatchSet();
return activePatchSet();
}
interface FoundRelease {
readonly files: PackReleaseFiles;
readonly manifest: PatchPackManifest;
}
async function newestRelease(): Promise<FoundRelease | null> {
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';
}