Files
WT-Offline/packages/patcher/src/patch-pack/manifest.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

146 lines
5.9 KiB
TypeScript

/**
* The manifest of an adaptation pack (`manifest.json` at the root of the zip), and the version
* numbers it carries.
*
* An adaptation pack is a patch set (patch-set.ts) released on its own, so that a new game build
* can be adapted without a new release of the offline version. It is published as a GitHub
* release tagged `patches-<pack version>` with two files: the zip and its Ed25519 signature
* (signature.ts).
*
* format layout of the pack; a launcher uses only formats it knows
* version pack version, `YYYY.MM.DD` or `YYYY.MM.DD.N` for a second pack on a day
* min_app_version the oldest offline version (launcher version) whose patch engine reads it
* game_versions the game builds with a version record in the pack
* created_at when the pack was built (ISO 8601)
*/
import { PatchPackError } from './patch-pack-error.ts';
/** The pack layout this code writes and reads. */
export const PATCH_PACK_FORMAT = 1;
export const PATCH_PACK_TAG_PREFIX = 'patches-';
export const MANIFEST_FILE = 'manifest.json';
export interface PatchPackManifest {
readonly format: number;
readonly version: string;
readonly minAppVersion: string;
readonly gameVersions: readonly string[];
readonly createdAt: string;
}
export interface PatchPackFileNames {
readonly zip: string;
readonly signature: string;
/**
* A copy of the manifest beside the zip, so a launcher can tell what a release holds before it
* downloads the zip. It is not signed and only informs; the manifest inside the signed zip is
* the one that counts.
*/
readonly manifest: string;
}
export function packFileNames(version: string): PatchPackFileNames {
const base = `wtoffline-patches-${version}`;
return { zip: `${base}.zip`, signature: `${base}.zip.sig`, manifest: `${base}.manifest.json` };
}
/** The pack version of a release tag, or null when the tag is not a pack's. */
export function packVersionOfTag(tag: string): string | null {
if (!tag.startsWith(PATCH_PACK_TAG_PREFIX)) return null;
const version = tag.slice(PATCH_PACK_TAG_PREFIX.length);
return isPackVersion(version) ? version : null;
}
export function isPackVersion(text: string): boolean {
return /^\d{4}\.\d{2}\.\d{2}(\.[1-9]\d*)?$/.test(text);
}
/** Negative when a is older than b. Both must be pack versions. */
export function comparePackVersions(a: string, b: string): number {
return compareNumberLists(packVersionParts(a), packVersionParts(b));
}
function packVersionParts(version: string): number[] {
const parts = version.split('.').map(Number);
// `2026.11.29` is the first pack of the day, the same as `2026.11.29.1` would be.
if (parts.length === 3) parts.push(1);
return parts;
}
/** Negative when a is older than b. Versions are `x.y.z`, a pre-release suffix is ignored. */
export function compareAppVersions(a: string, b: string): number {
return compareNumberLists(appVersionParts(a), appVersionParts(b));
}
export function isAppVersion(text: string): boolean {
return /^\d+\.\d+\.\d+([-+].*)?$/.test(text);
}
function appVersionParts(version: string): number[] {
const match = /^(\d+)\.(\d+)\.(\d+)/.exec(version);
if (!match) throw new PatchPackError('contents', `not a version number: ${version}`);
return [Number(match[1]), Number(match[2]), Number(match[3])];
}
function compareNumberLists(a: readonly number[], b: readonly number[]): number {
for (let i = 0; i < Math.max(a.length, b.length); i++) {
const difference = (a[i] ?? 0) - (b[i] ?? 0);
if (difference !== 0) return difference;
}
return 0;
}
/** Why this launcher cannot use a pack, or null when it can. */
export type ManifestIncompatibility = 'format' | 'app_version';
export function manifestIncompatibility(manifest: PatchPackManifest, appVersion: string): ManifestIncompatibility | null {
if (manifest.format > PATCH_PACK_FORMAT) return 'format';
if (compareAppVersions(appVersion, manifest.minAppVersion) < 0) return 'app_version';
return null;
}
export function formatManifest(manifest: PatchPackManifest): string {
const json = {
format: manifest.format,
version: manifest.version,
min_app_version: manifest.minAppVersion,
game_versions: manifest.gameVersions,
created_at: manifest.createdAt,
};
return `${JSON.stringify(json, null, 2)}\n`;
}
export function parseManifest(text: string): PatchPackManifest {
let value: unknown;
try {
value = JSON.parse(text);
} catch {
throw manifestError('not valid JSON');
}
if (typeof value !== 'object' || value === null || Array.isArray(value)) throw manifestError('not an object');
const fields = value as Record<string, unknown>;
const format = fields['format'];
if (typeof format !== 'number' || !Number.isInteger(format) || format < 1) throw manifestError('"format" must be a positive integer');
const version = stringField(fields, 'version');
if (!isPackVersion(version)) throw manifestError(`"version" is not a pack version: ${version}`);
const minAppVersion = stringField(fields, 'min_app_version');
if (!isAppVersion(minAppVersion)) throw manifestError(`"min_app_version" is not a version number: ${minAppVersion}`);
const gameVersions = fields['game_versions'];
if (!Array.isArray(gameVersions) || !gameVersions.every((entry) => typeof entry === 'string' && /^\d+(\.\d+){3}$/.test(entry))) {
throw manifestError('"game_versions" must list game versions such as "2.61.0.37"');
}
const createdAt = stringField(fields, 'created_at');
if (Number.isNaN(Date.parse(createdAt))) throw manifestError('"created_at" is not a date');
return { format, version, minAppVersion, gameVersions: [...gameVersions] as string[], createdAt };
}
function stringField(fields: Record<string, unknown>, key: string): string {
const value = fields[key];
if (typeof value !== 'string' || value.length === 0) throw manifestError(`"${key}" must be a string`);
return value;
}
function manifestError(detail: string): PatchPackError {
return new PatchPackError('contents', `${MANIFEST_FILE}: ${detail}`);
}