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.
This commit is contained in:
N0VAN0M4LY committed 2026-11-29 16:08:37 +08:00
1 parent 19990fc9de
commit 3e2acc5a17
18 files changed
+1097 -2

No files matched your search

+131
View File
@@ -0,0 +1,131 @@
/**
* Build and sign an adaptation pack from this repository's patches/ (packages/patcher/src/patch-pack/).
*
* node scripts/build-patch-pack.ts --version <YYYY.MM.DD[.N]> [--out <folder>] [--min-app-version <x.y.z>] [--unsigned]
*
* The private key comes from the environment variable PATCH_SIGNING_KEY (PEM text), as the release
* workflow .github/workflows/patch-pack.yml passes it from the GitHub secret of the same name.
* --unsigned writes the zip without a signature, for a look at the contents only: the launcher
* refuses an unsigned pack. In GitHub Actions --version defaults to the pushed tag
* (`patches-<version>`).
*
* Only committed files go into a pack: the script refuses when patches/ has uncommitted changes or
* untracked files, so a version record is released only after it was committed (it is committed
* once the build was played in the game, docs/architecture.md 5.2). --min-app-version defaults to
* the launcher version in apps/launcher/package.json, the oldest launcher whose patch engine is
* known to read this pack.
*
* Writes <out>/wtoffline-patches-<version>.zip, the .zip.sig and the .manifest.json next to it.
*/
import { execFileSync } from 'node:child_process';
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';
import {
checkPatchSet,
formatManifest,
isPackVersion,
packFileNames,
packGameVersions,
packSigningPublicKey,
packVersionOfTag,
PATCH_PACK_FORMAT,
signPatchPack,
verifyPatchPack,
writePatchPack,
type PatchPackFile,
type PatchPackManifest,
} from '@wto/patcher';
const REPOSITORY = fileURLToPath(new URL('..', import.meta.url));
const PATCHES = join(REPOSITORY, 'patches');
const PACKED_FOLDERS = ['patches/exe', 'patches/gui', 'patches/native', 'patches/versions'];
const USAGE = 'usage: build-patch-pack.ts --version <YYYY.MM.DD[.N]> [--out <folder>] [--min-app-version <x.y.z>] [--unsigned]';
function main(): number {
const { values } = parseArgs({
options: {
version: { type: 'string' },
out: { type: 'string', default: join(REPOSITORY, 'build', 'patch-packs') },
'min-app-version': { type: 'string' },
unsigned: { type: 'boolean', default: false },
},
});
const version = values.version ?? tagVersion();
if (!version || !isPackVersion(version)) {
console.error(version ? `not a pack version: ${version}` : USAGE);
return 2;
}
const privateKey = process.env['PATCH_SIGNING_KEY'];
if (!values.unsigned && !privateKey) {
console.error('PATCH_SIGNING_KEY is not set; pass --unsigned for a pack to look at only');
return 2;
}
const uncommitted = git(['status', '--porcelain', '--untracked-files=all', '--', ...PACKED_FOLDERS]).trim();
if (uncommitted) {
console.error(`patches/ has uncommitted changes; commit them first:\n${uncommitted}`);
return 1;
}
const summary = checkPatchSet(PATCHES);
const files = committedFiles();
const manifest: PatchPackManifest = {
format: PATCH_PACK_FORMAT,
version,
minAppVersion: values['min-app-version'] ?? launcherVersion(),
gameVersions: packGameVersions(files),
createdAt: new Date().toISOString(),
};
const zip = writePatchPack({ manifest, files });
const names = packFileNames(version);
mkdirSync(values.out, { recursive: true });
writeFileSync(join(values.out, names.zip), zip);
writeFileSync(join(values.out, names.manifest), formatManifest(manifest));
if (privateKey && !values.unsigned) {
const signature = signPatchPack(zip, privateKey);
const publicKey = packSigningPublicKey();
if (!publicKey) {
console.error('no committed public key (packages/patcher/keys/); run scripts/generate-patch-signing-key.ts first');
return 1;
}
if (!verifyPatchPack(zip, signature, publicKey)) {
console.error('the signature does not verify against the committed public key: PATCH_SIGNING_KEY is not its private key');
return 1;
}
writeFileSync(join(values.out, names.signature), signature);
}
console.log(`pack ${version} (format ${manifest.format}, launcher ${manifest.minAppVersion} or newer)`);
console.log(` ${summary.exePatches} exe patch(es), ${summary.guiPatches} GUI patch(es), ${summary.nativeModules} native module(s)`);
console.log(` game versions: ${manifest.gameVersions.join(', ') || 'none'}`);
console.log(` ${files.length} file(s), ${zip.length} bytes zipped${values.unsigned ? ', NOT signed' : ', signed'}`);
console.log(`wrote ${join(values.out, names.zip)}`);
return 0;
}
/** In GitHub Actions, the version of the pushed `patches-*` tag. */
function tagVersion(): string | undefined {
const ref = process.env['GITHUB_REF_NAME'];
return ref ? (packVersionOfTag(ref) ?? undefined) : undefined;
}
function committedFiles(): PatchPackFile[] {
return git(['ls-files', '-z', '--', ...PACKED_FOLDERS])
.split('\0')
.filter((path) => path.length > 0)
.map((path) => ({ path: path.slice('patches/'.length), content: readFileSync(join(REPOSITORY, path)) }));
}
function launcherVersion(): string {
const manifest = JSON.parse(readFileSync(join(REPOSITORY, 'apps', 'launcher', 'package.json'), 'utf8')) as { version?: unknown };
if (typeof manifest.version !== 'string') throw new Error('apps/launcher/package.json has no version');
return manifest.version;
}
function git(args: readonly string[]): string {
return execFileSync('git', args, { cwd: REPOSITORY, encoding: 'utf8' });
}
process.exitCode = main();
+63
View File
@@ -0,0 +1,63 @@
/**
* One-time developer script: create the Ed25519 key pair that signs adaptation packs
* (packages/patcher/src/patch-pack/signature.ts).
*
* node scripts/generate-patch-signing-key.ts --private-out <file outside the repository> [--replace]
*
* The public key is written to packages/patcher/keys/patch-pack-signing.pub.pem, to be committed;
* launchers built from then on accept only packs signed by the matching private key. The private
* key is written to --private-out, which must lie outside the repository. Put its contents into
* the GitHub secret PATCH_SIGNING_KEY, keep a backup (for example in a password manager), then
* delete the file. Replacing an existing key (--replace) makes every launcher released before
* the change refuse packs signed with the new key.
*/
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
import { dirname, isAbsolute, relative, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';
import { generateSigningKeys, SIGNING_PUBLIC_KEY_FILE } from '@wto/patcher';
const REPOSITORY = fileURLToPath(new URL('..', import.meta.url));
const USAGE = 'usage: generate-patch-signing-key.ts --private-out <file outside the repository> [--replace]';
function main(): number {
const { values } = parseArgs({ options: { 'private-out': { type: 'string' }, replace: { type: 'boolean', default: false } } });
const privateOut = values['private-out'];
if (!privateOut) {
console.error(USAGE);
return 2;
}
const privatePath = resolve(privateOut);
// On another drive `relative` gives an absolute path.
const fromRepository = relative(REPOSITORY, privatePath);
if (!fromRepository.startsWith('..') && !isAbsolute(fromRepository)) {
console.error(`refusing to write the private key inside the repository: ${privatePath}`);
return 2;
}
if (existsSync(privatePath)) {
console.error(`refusing to overwrite ${privatePath}`);
return 2;
}
if (existsSync(SIGNING_PUBLIC_KEY_FILE) && !values.replace) {
console.error(`a public key already exists at ${SIGNING_PUBLIC_KEY_FILE}; pass --replace to make a new pair`);
return 2;
}
const keys = generateSigningKeys();
mkdirSync(dirname(privatePath), { recursive: true });
writeFileSync(privatePath, keys.privateKeyPem, { mode: 0o600, flag: 'wx' });
mkdirSync(dirname(SIGNING_PUBLIC_KEY_FILE), { recursive: true });
writeFileSync(SIGNING_PUBLIC_KEY_FILE, keys.publicKeyPem);
console.log(`public key: ${SIGNING_PUBLIC_KEY_FILE} (commit this file)`);
console.log(`private key: ${privatePath}`);
console.log('');
console.log('Next:');
console.log(' 1. GitHub repository > Settings > Secrets and variables > Actions > New repository secret,');
console.log(' name PATCH_SIGNING_KEY, value: the whole contents of the private key file.');
console.log(' 2. Keep a backup of the private key outside this computer (for example a password manager).');
console.log(' 3. Delete the private key file.');
return 0;
}
process.exitCode = main();