Files
WT-Offline/packages/local-service/src/asset-pack-cli.ts
T
N0VAN0M4LY 64b11d56bf Serve stock artwork and link official store media
Show the installed game artwork in the local store and marketplace. Use the official cover and gallery for each mapped product, with on-demand video playback. Include store cover images and screenshots in optional offline asset packs.

Verified TypeScript, browser script syntax, local service startup, game artwork endpoints, a sample image pack, and BMD-4M WebM byte-range delivery. In-game verification is pending.
2026-11-26 17:18:32 +08:00

71 lines
3.3 KiB
TypeScript

/**
* Build an optional asset pack by downloading the public images that the local
* pages use into <data folder>/asset-pack/, so the pages show official pictures without a network
* connection. Run it on a machine that can reach the official sites; nothing is shipped with
* the project, every player makes their own copy.
*
* node packages/local-service/src/asset-pack-cli.ts --data <data folder> [--concurrency 6]
*
* Sources: official marketplace art and vehicle renders, plus store covers and screenshots.
* The storefront links videos directly.
* Files keep their host/path layout (static-ggc.gaijin.net/units/x.png); index.json lists them.
* The pages read the pack first (web-ui/web/common.js `sourcesFor`).
*/
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { parseArgs } from 'node:util';
import { writeFileAtomic } from './storage/atomic-file.ts';
const MARKET_IMAGES = new URL('./web-ui/web/official-images.json', import.meta.url);
const STORE_MEDIA = new URL('./web-ui/web/official-store-media.json', import.meta.url);
async function main(): Promise<number> {
const { values } = parseArgs({ options: { data: { type: 'string' }, concurrency: { type: 'string', default: '6' } } });
if (!values.data) {
console.error('usage: asset-pack-cli.ts --data <data folder> [--concurrency 6]');
return 2;
}
const packDir = join(values.data, 'asset-pack');
const table = JSON.parse(readFileSync(MARKET_IMAGES, 'utf8')) as { base: string; byKey: Record<string, string> };
const store = JSON.parse(readFileSync(STORE_MEDIA, 'utf8')) as {
base: string;
byProduct: Record<string, { cover: string; screenshots?: string[] }>;
};
const urls = new Set<string>();
for (const [key, path] of Object.entries(table.byKey)) {
urls.add(table.base + path);
if (path.startsWith('units/')) urls.add(`https://static.encyclopedia.warthunder.com/images/${key.toLowerCase()}.png`);
}
for (const { cover, screenshots = [] } of Object.values(store.byProduct)) {
urls.add(store.base + cover);
for (const screenshot of screenshots) urls.add(store.base + screenshot);
}
const indexFile = join(packDir, 'index.json');
const done = new Set<string>(existsSync(indexFile) ? JSON.parse(readFileSync(indexFile, 'utf8')) as string[] : []);
const queue = [...urls].filter((url) => !done.has(url.replace(/^https?:\/\//, '')));
let failed = 0;
const worker = async (): Promise<void> => {
for (let url = queue.shift(); url; url = queue.shift()) {
const relative = url.replace(/^https?:\/\//, '');
try {
const response = await fetch(url, { signal: AbortSignal.timeout(15000) });
if (!response.ok || !response.headers.get('content-type')?.startsWith('image/')) {
await response.body?.cancel();
failed += 1;
continue;
}
writeFileAtomic(join(packDir, relative), Buffer.from(await response.arrayBuffer()));
done.add(relative);
} catch {
failed += 1;
}
}
};
await Promise.all(Array.from({ length: Math.max(1, Number(values.concurrency)) }, worker));
writeFileAtomic(indexFile, JSON.stringify([...done].sort()));
console.log(`asset pack: ${done.size} file(s) in ${packDir}; ${failed} not available`);
return 0;
}
process.exitCode = await main();