Files
WT-Offline/scripts/build-patch-pack.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

132 lines
5.5 KiB
TypeScript

/**
* 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();