Let a saved HTTPS repository serve as the fallback for launcher and adaptation pack releases. Retry downloads for the selected version and validate against the project signing key. Verified type checks, the launcher build, and a simulated primary outage.
123 lines
5.8 KiB
TypeScript
123 lines
5.8 KiB
TypeScript
/**
|
|
* Adaptation pack updates: which patch set the builds use, whether a source 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 { app } from 'electron';
|
|
import {
|
|
comparePackVersions,
|
|
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, bundledPatchVersion, installPack, useBundledPatchSet } from './pack-store.ts';
|
|
import {
|
|
newestProjectRelease, projectReleaseVersion, ReleasePageLimitError, withReleaseSources, type PackReleaseFiles, type ReleaseSource,
|
|
} from './release-feed.ts';
|
|
import { FeedUnreachableError, readReleaseAsset, RELEASE_MANIFEST_MAX_BYTES, RELEASE_SIGNATURE_MAX_BYTES } from './release-assets.ts';
|
|
|
|
export function activePatchSet(): ActivePatchSet {
|
|
const root = bundledResourcePaths().patches;
|
|
const pack = usesDownloadedPacks() ? activeDownloadedPack(root) : null;
|
|
if (pack) return { source: 'downloaded', version: pack.manifest.version, gameVersions: [...pack.manifest.gameVersions] };
|
|
return { source: 'bundled', version: bundledPatchVersion(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 withReleaseSources((source) => newestRelease(active.version, source));
|
|
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 selected pack when it is newer than the active set and usable. */
|
|
export async function applyPackUpdate(version: string): Promise<PackApplyResult> {
|
|
if (!packSigningPublicKey()) return { ok: false, problem: 'no_signing_key' };
|
|
if (!isNewer(version, activePatchSet().version)) return { ok: false, problem: 'up_to_date' };
|
|
try {
|
|
const downloaded = await withReleaseSources(async (source) => {
|
|
const files = await projectReleaseVersion('pack', version, source);
|
|
const manifest = await readPackManifest(files);
|
|
if (manifestIncompatibility(manifest, app.getVersion())) return null;
|
|
const tuning = patchPackTuning();
|
|
const zip = await readReleaseAsset(files.zipUrl, tuning.maxPackBytes, AbortSignal.timeout(tuning.requestTimeoutMs), source);
|
|
const signature = new TextDecoder().decode(await readReleaseAsset(
|
|
files.signatureUrl, RELEASE_SIGNATURE_MAX_BYTES, AbortSignal.timeout(tuning.requestTimeoutMs), source,
|
|
));
|
|
return { zip, signature };
|
|
});
|
|
if (!downloaded) return { ok: false, problem: 'needs_app_update' };
|
|
if (!isNewer(version, activePatchSet().version)) return { ok: false, problem: 'up_to_date' };
|
|
await installPack(version, downloaded.zip, downloaded.signature);
|
|
console.info(`[patch-pack] pack ${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(installedVersion: string | null, source: ReleaseSource): Promise<FoundRelease | null> {
|
|
const files = await newestProjectRelease('pack', installedVersion, source);
|
|
return files ? { files, manifest: await readPackManifest(files) } : null;
|
|
}
|
|
|
|
async function readPackManifest(files: PackReleaseFiles): Promise<PatchPackManifest> {
|
|
const deadline = AbortSignal.timeout(patchPackTuning().requestTimeoutMs);
|
|
const text = new TextDecoder().decode(await readReleaseAsset(files.manifestUrl, RELEASE_MANIFEST_MAX_BYTES, deadline, files.source));
|
|
const manifest = parseManifest(text);
|
|
if (manifest.version !== files.version) throw new PatchPackError('contents', `release ${files.version} carries the manifest of ${manifest.version}`);
|
|
return 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;
|
|
}
|
|
|
|
function problemOf(error: unknown): PackProblem {
|
|
if (error instanceof ReleasePageLimitError) return 'page_limit';
|
|
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';
|
|
}
|