/**
* Generates a self-contained bilingual (zh-CN / EN) static download page (`index.html`)
* from the GitHub-compatible release list served by the R2 distribution mirror.
*/
import { compareAppVersions, comparePackVersions, packVersionOfTag } from '@wto/patcher';
export interface ReleaseAssetEntry {
readonly name: string;
readonly browser_download_url: string;
readonly size?: number;
readonly sha256?: string;
}
export interface ReleaseEntry {
readonly tag_name: string;
readonly name: string;
readonly draft: boolean;
readonly prerelease: boolean;
readonly published_at: string;
readonly body: string;
readonly game_versions?: readonly string[];
readonly min_app_version?: string;
readonly assets: readonly ReleaseAssetEntry[];
}
export interface DownloadPageOptions {
readonly releases: readonly ReleaseEntry[];
readonly publicUrl: string;
readonly repoUrl: string;
readonly generatedAt: string;
}
export function renderDownloadPage(options: DownloadPageOptions): string {
const launchers = options.releases
.filter((entry) => !entry.draft && !entry.prerelease && entry.tag_name.startsWith('v'))
.sort((a, b) => compareAppVersions(b.tag_name.slice(1), a.tag_name.slice(1)));
const packs = options.releases
.filter((entry) => !entry.draft && packVersionOfTag(entry.tag_name) !== null)
.sort((a, b) => comparePackVersions(packVersionOfTag(b.tag_name)!, packVersionOfTag(a.tag_name)!));
const latestLauncher = launchers[0] ?? null;
const latestPack = packs[0] ?? null;
const olderLaunchers = launchers.slice(1);
const olderPacks = packs.slice(1);
return `
WTOffline — Download / 下载站
${latestLauncher ? renderLatestLauncher(latestLauncher) : renderEmptyState('launcher')}
${latestPack ? renderLatestPack(latestPack) : renderEmptyState('pack')}
${olderLaunchers.length > 0 || olderPacks.length > 0 ? renderHistory(olderLaunchers, olderPacks) : ''}
`;
}
function renderLatestLauncher(release: ReleaseEntry): string {
const installer = release.assets.find((asset) => asset.name.endsWith('.exe'));
const portable = release.assets.find((asset) => asset.name.endsWith('-portable.zip'));
const date = release.published_at.slice(0, 10);
return `
${installer ? renderDownloadBox(installer, '安装版(推荐)', 'Windows Installer (Recommended)', '下载安装版', 'Download Installer', false) : ''}
${portable ? renderDownloadBox(portable, '便携压缩包(免安装)', 'Portable Archive (No Install)', '下载便携包', 'Download Portable', true) : ''}
已安装的启动器会在启动时自动从此镜像检查并验签更新;本程序不包含任何原版游戏文件,需配合你自己的客户端使用。
Installed launchers check this mirror automatically and verify Ed25519 signatures. No stock game files are distributed; your own client is required.
${release.body.trim() ? `
${renderSimpleMarkdown(release.body)}
` : ''}
`;
}
function renderDownloadBox(
asset: ReleaseAssetEntry,
titleZh: string,
titleEn: string,
btnZh: string,
btnEn: string,
secondary: boolean,
): string {
const sizeLabel = asset.size !== undefined ? ` (${formatBytes(asset.size)})` : '';
return ``;
}
function renderLatestPack(release: ReleaseEntry): string {
const zip = release.assets.find((asset) => asset.name.endsWith('.zip'));
const date = release.published_at.slice(0, 10);
const games = release.game_versions?.join(', ') ?? '';
const minApp = release.min_app_version ?? '';
return ``;
}
function renderEmptyState(kind: 'launcher' | 'pack'): string {
const zh = kind === 'launcher' ? '暂未发布启动器安装包。' : '暂未发布适配包。';
const en = kind === 'launcher' ? 'No launcher releases published yet.' : 'No adaptation packs published yet.';
return ``;
}
function renderHistory(launchers: readonly ReleaseEntry[], packs: readonly ReleaseEntry[]): string {
const rows = [...launchers, ...packs].map((release) => {
const links = release.assets
.filter((asset) => asset.name.endsWith('.exe') || asset.name.endsWith('.zip'))
.map((asset) => `${escapeHtml(asset.name)}`)
.join('');
return `
${escapeHtml(release.tag_name)} |
${escapeHtml(release.published_at.slice(0, 10))} |
${links} |
`;
}).join('');
return `
历史版本(${launchers.length + packs.length})
Previous Releases (${launchers.length + packs.length})
`;
}
function renderSimpleMarkdown(source: string): string {
const lines = source.replace(/\r\n/g, '\n').split('\n');
const html: string[] = [];
let inList = false;
const closeList = (): void => {
if (inList) {
html.push('');
inList = false;
}
};
for (const raw of lines) {
const line = raw.trim();
if (!line) {
closeList();
continue;
}
if (line.startsWith('### ')) {
closeList();
html.push(`${inlineMarkdown(line.slice(4))}
`);
continue;
}
if (line.startsWith('## ')) {
closeList();
html.push(`${inlineMarkdown(line.slice(3))}
`);
continue;
}
if (line.startsWith('- ') || line.startsWith('* ')) {
if (!inList) {
html.push('');
inList = true;
}
html.push(`- ${inlineMarkdown(line.slice(2))}
`);
continue;
}
closeList();
html.push(`${inlineMarkdown(line)}
`);
}
closeList();
return html.join('\n');
}
function inlineMarkdown(text: string): string {
return escapeHtml(text)
.replace(/`([^`]+)`/g, '$1')
.replace(/\*\*([^*]+)\*\*/g, '$1');
}
function formatBytes(bytes: number): string {
if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
if (bytes >= 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${bytes} B`;
}
function escapeHtml(value: string): string {
return value
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}