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.
81 lines
4.1 KiB
TypeScript
81 lines
4.1 KiB
TypeScript
/**
|
|
* Adaptation pack releases on GitHub: the newest release tagged `patches-<version>` that carries
|
|
* all three files (zip, signature, manifest copy), and downloads of those files.
|
|
*
|
|
* Requests send no account, no cookie and nothing about the player; GitHub sees an anonymous
|
|
* request with a generic user agent.
|
|
*/
|
|
import { net } from 'electron';
|
|
import { comparePackVersions, packFileNames, packVersionOfTag, PatchPackError, patchPackTuning } from '@wto/patcher';
|
|
|
|
export interface PackReleaseFiles {
|
|
readonly version: string;
|
|
readonly publishedAt: string | null;
|
|
readonly zipUrl: string;
|
|
readonly signatureUrl: string;
|
|
readonly manifestUrl: string;
|
|
}
|
|
|
|
/** Thrown when GitHub cannot be reached or answers with an error. */
|
|
export class FeedUnreachableError extends Error {
|
|
override readonly name = 'FeedUnreachableError';
|
|
}
|
|
|
|
const HEADERS = { Accept: 'application/vnd.github+json', 'User-Agent': 'WTOffline-Launcher', 'X-GitHub-Api-Version': '2022-11-28' };
|
|
|
|
/** The newest complete pack release, or null when none is released (or the repository is not public yet). */
|
|
export async function newestPackRelease(): Promise<PackReleaseFiles | null> {
|
|
const tuning = patchPackTuning();
|
|
const url = `https://api.github.com/repos/${tuning.repository}/releases?per_page=${tuning.releasesPerCheck}`;
|
|
const response = await request(url, HEADERS, tuning.requestTimeoutMs);
|
|
if (response.status === 404) return null;
|
|
if (!response.ok) throw new FeedUnreachableError(`GitHub answered HTTP ${response.status} for the release list`);
|
|
const releases = (await response.json()) as unknown;
|
|
if (!Array.isArray(releases)) throw new PatchPackError('contents', 'the release list is not a list');
|
|
|
|
const packs = releases.map(packRelease).filter((release): release is PackReleaseFiles => release !== null);
|
|
packs.sort((a, b) => comparePackVersions(b.version, a.version));
|
|
return packs[0] ?? null;
|
|
}
|
|
|
|
function packRelease(value: unknown): PackReleaseFiles | null {
|
|
if (typeof value !== 'object' || value === null) return null;
|
|
const release = value as Record<string, unknown>;
|
|
if (release['draft'] === true || release['prerelease'] === true || typeof release['tag_name'] !== 'string') return null;
|
|
const version = packVersionOfTag(release['tag_name']);
|
|
if (!version || !Array.isArray(release['assets'])) return null;
|
|
const urls = new Map<string, string>();
|
|
for (const asset of release['assets'] as unknown[]) {
|
|
if (typeof asset !== 'object' || asset === null) continue;
|
|
const { name, browser_download_url: download } = asset as Record<string, unknown>;
|
|
if (typeof name === 'string' && typeof download === 'string' && download.startsWith('https://')) urls.set(name, download);
|
|
}
|
|
const names = packFileNames(version);
|
|
const zipUrl = urls.get(names.zip);
|
|
const signatureUrl = urls.get(names.signature);
|
|
const manifestUrl = urls.get(names.manifest);
|
|
if (!zipUrl || !signatureUrl || !manifestUrl) return null;
|
|
const publishedAt = typeof release['published_at'] === 'string' ? release['published_at'] : null;
|
|
return { version, publishedAt, zipUrl, signatureUrl, manifestUrl };
|
|
}
|
|
|
|
/** Download a release file, refusing anything larger than `maxBytes`. */
|
|
export async function downloadReleaseFile(url: string, maxBytes: number): Promise<Uint8Array> {
|
|
const tuning = patchPackTuning();
|
|
const response = await request(url, { 'User-Agent': HEADERS['User-Agent'] }, tuning.requestTimeoutMs);
|
|
if (!response.ok) throw new FeedUnreachableError(`HTTP ${response.status} for ${url}`);
|
|
const declared = Number(response.headers.get('content-length') ?? '0');
|
|
if (declared > maxBytes) throw new PatchPackError('size', `${url} is ${declared} bytes, more than ${maxBytes}`);
|
|
const content = new Uint8Array(await response.arrayBuffer());
|
|
if (content.length > maxBytes) throw new PatchPackError('size', `${url} is more than ${maxBytes} bytes`);
|
|
return content;
|
|
}
|
|
|
|
async function request(url: string, headers: Record<string, string>, timeoutMs: number): Promise<Response> {
|
|
try {
|
|
return await net.fetch(url, { credentials: 'omit', headers, signal: AbortSignal.timeout(timeoutMs) });
|
|
} catch (error) {
|
|
throw new FeedUnreachableError(`${url}: ${(error as Error).message}`);
|
|
}
|
|
}
|