91 lines
5.0 KiB
TypeScript
91 lines
5.0 KiB
TypeScript
/**
|
|
* Files shared by the local web pages, under /_wto/ui/:
|
|
* common.css, common.js layout and helpers both pages load
|
|
* official-images.json official image paths (static-ggc.gaijin.net) per vehicle /
|
|
* decor / item key, from the official market snapshot
|
|
* labels.json the game's country and vehicle type names (ui-labels dataset)
|
|
* vehicle/<vehicle>.svg the game's silhouette icon of a vehicle (web-icons dataset)
|
|
* flag/<country>.svg the game's country flag (web-icons dataset)
|
|
* official-store-media.json public store cover, screenshot and video paths by product
|
|
* store-media/<path> cached public store media from the manifest
|
|
* art/<kind>/<key> game artwork for vehicles, products and coupons
|
|
* asset-pack/<path> the player's own optional copy of official images, made by
|
|
* asset-pack-cli.ts into <data folder>/asset-pack/
|
|
* Icons and names come from the player's game files at start-up; nothing of Gaijin's is shipped.
|
|
* Store media prefers the asset pack and the service cache before fetching the public file.
|
|
*/
|
|
import { existsSync, readFileSync } from 'node:fs';
|
|
import { resolve, sep } from 'node:path';
|
|
import type { WebIcons } from '../game-data/datasets/web-icons.ts';
|
|
import type { UiLabels } from '../game-data/datasets/ui-labels.ts';
|
|
import type { Service, ServiceRequest, ServiceResponse } from '../transport/exchange.ts';
|
|
import type { GameArtwork } from './game-artwork.ts';
|
|
import { StoreMediaCache, STORE_MEDIA_PREFIX } from './store-media-cache.ts';
|
|
import { contentResponse, createStaticWebSite, notFound } from './web-helpers.ts';
|
|
|
|
const PREFIX = '/_wto/ui/';
|
|
const SHARED_FILES = createStaticWebSite(new URL('./web/', import.meta.url), ['common.css', 'common.js', 'official-images.json', 'official-store-media.json']);
|
|
const ICON_PATH = /^(vehicle|flag)\/([^/]+)\.svg$/;
|
|
const ART_PATH = /^art\/(vehicle|group|product|skin|decal|attachable|unlock)\/([^/]+)$/;
|
|
const PACK_PREFIX = 'asset-pack/';
|
|
const PACK_TYPES: Readonly<Record<string, string>> = { '.png': 'image/png', '.jpg': 'image/jpeg', '.webp': 'image/webp', '.json': 'application/json; charset=utf-8' };
|
|
|
|
export class WebUiService implements Service {
|
|
readonly name = 'web-ui';
|
|
readonly #icons: WebIcons;
|
|
readonly #labels: string;
|
|
readonly #packDir: string;
|
|
readonly #artwork: GameArtwork;
|
|
readonly #storeMedia: StoreMediaCache;
|
|
|
|
constructor(icons: WebIcons, labels: UiLabels, dataFolder: string, artwork: GameArtwork) {
|
|
this.#icons = icons;
|
|
this.#labels = JSON.stringify(labels);
|
|
this.#packDir = resolve(dataFolder, 'asset-pack');
|
|
this.#artwork = artwork;
|
|
this.#storeMedia = new StoreMediaCache(dataFolder);
|
|
}
|
|
|
|
accepts(request: ServiceRequest): boolean {
|
|
return request.originalHost === undefined && request.method === 'GET' && request.path.startsWith(PREFIX);
|
|
}
|
|
|
|
handle(request: ServiceRequest): ServiceResponse | Promise<ServiceResponse> {
|
|
const subPath = request.path.slice(PREFIX.length);
|
|
if (subPath === 'labels.json') return contentResponse(subPath, this.#labels, 'web ui: labels');
|
|
if (subPath.startsWith(STORE_MEDIA_PREFIX)) return this.#storeMedia.serve(subPath.slice(STORE_MEDIA_PREFIX.length), request.headers.range);
|
|
if (subPath.startsWith(PACK_PREFIX)) return this.#packFile(subPath.slice(PACK_PREFIX.length));
|
|
const art = ART_PATH.exec(subPath);
|
|
if (art) return this.#gamePicture(art[1]!, art[2]!);
|
|
const icon = ICON_PATH.exec(subPath);
|
|
if (icon) {
|
|
const [, kind, name] = icon;
|
|
const table = kind === 'vehicle' ? this.#icons.vehicles : this.#icons.flags;
|
|
const svg = Object.hasOwn(table, name!) ? table[name!] : undefined;
|
|
return svg ? contentResponse(subPath, svg, `web ui: ${kind} icon`) : notFound(`web ui: no ${kind} icon ${name}`);
|
|
}
|
|
return SHARED_FILES.serve(subPath, 'web ui') ?? notFound(`web ui 404: ${subPath}`);
|
|
}
|
|
|
|
#gamePicture(kind: string, encodedKey: string): ServiceResponse {
|
|
let key: string;
|
|
try {
|
|
key = decodeURIComponent(encodedKey);
|
|
} catch {
|
|
return notFound('web ui: invalid artwork key');
|
|
}
|
|
const picture = this.#artwork.image(kind, key);
|
|
return picture ? contentResponse(picture.filename, picture.bytes, 'web ui: game ' + kind + ' artwork')
|
|
: notFound('web ui: no game artwork for ' + kind + '/' + key);
|
|
}
|
|
|
|
/** A file of the player's asset pack; only image/json files inside the pack folder. */
|
|
#packFile(relative: string): ServiceResponse {
|
|
const decoded = decodeURIComponent(relative);
|
|
const full = resolve(this.#packDir, decoded);
|
|
const type = PACK_TYPES[decoded.slice(decoded.lastIndexOf('.')).toLowerCase()];
|
|
if (!type || !full.startsWith(this.#packDir + sep) || !existsSync(full)) return notFound(`asset pack: no ${decoded}`);
|
|
return { status: 200, headers: { 'content-type': type, 'cache-control': 'max-age=86400' }, body: readFileSync(full), logNotes: { summary: `asset pack: ${decoded}` } };
|
|
}
|
|
}
|