Files
WT-Offline/apps/server/src/game-data/game-files.ts
T

62 lines
1.6 KiB
TypeScript

/**
* The game files the local server reads, opened lazily from the player's game folder.
*/
import { statSync } from 'node:fs';
import { join } from 'node:path';
import type { BlkBlock } from '@wto/blk';
import { GamePack } from './game-pack.ts';
/** The data packs the local server reads (file names in the game folder). */
export type GamePackName =
| 'char.vromfs.bin'
| 'mis.vromfs.bin'
| 'aces.vromfs.bin'
| 'gui.vromfs.bin'
| 'lang.vromfs.bin'
| 'ui/atlases.vromfs.bin';
/** Size and modification time provide a quick preliminary check of a pack file. */
export interface PackStamp {
readonly size: number;
readonly mtimeMs: number;
}
/** Gaijin's template for a new account's profile. */
const NEW_ACCOUNT_TEMPLATE = 'config/default_user.blk';
export class GameFiles {
readonly #gameFolder: string;
readonly #packs = new Map<GamePackName, GamePack>();
constructor(gameFolder: string) {
this.#gameFolder = gameFolder;
}
newAccountTemplate(): BlkBlock {
return this.pack('char.vromfs.bin').readBlk(NEW_ACCOUNT_TEMPLATE);
}
pack(name: GamePackName): GamePack {
let pack = this.#packs.get(name);
if (!pack) {
pack = GamePack.open(this.#packPath(name));
this.#packs.set(name, pack);
}
return pack;
}
/** Drop the opened packs (they are reopened on demand); called after start-up extraction. */
releasePacks(): void {
this.#packs.clear();
}
packStamp(name: GamePackName): PackStamp {
const { size, mtimeMs } = statSync(this.#packPath(name));
return { size, mtimeMs };
}
#packPath(name: GamePackName): string {
return join(this.#gameFolder, name);
}
}