Files
WT-Offline/apps/server/src/saves/save-store.ts
T
N0VAN0M4LY 4e72bf3746 fix(profile): split modifications across sibling blocks at 65,535 entries
Dagor's binary DataBlock stores child block counts in a uint16 (65,535 max),
and the full 2.61.0.58 vehicle catalogue contains 76,041 modifications. Writing
all owned modifications into a single `modifications` block caused encodeBlk to
throw BlkFormatError once a save crossed 65,535 entries, preventing saving and
login.

Match aces.exe's native multi-block layout (2.61.0.37 0x938fae..0x9397e6,
2.61.0.58 0x93ba70 / 0x93bb53), which splits modifications across up to 3
sibling top-level `modifications` blocks of at most 65,535 entries each:

- Add allModificationBlocks, appendModificationEntry, and
  normalizeModificationBlocks in profile-modifications.ts and span all chunks
  in modificationRecord, ownsModification, and writeModificationRecord.
- Index and append across sibling chunks in ProfileEquipmentIndex and
  grantSupportPlaneModifications.
- Filter all sibling chunks in prepareTransferredProfile and normalize before
  encoding in SaveStore.saveProfile.

Verified with root npm run typecheck and a 2.61.0.58 temporary save check
encoding, decoding, and filtering >65,535 modifications across sibling chunks.
In-game acceptance pending on an isolated 2.61.0.58 client copy.
2026-12-03 01:18:14 +08:00

67 lines
2.6 KiB
TypeScript

/**
* Save layer: one save per account, holding the profile in Gaijin's
* profile structure.
*
* <data>/saves/<userId>/save.json format version and bookkeeping
* <data>/saves/<userId>/profile.blk the profile, binary BLK with every value's type kept
*
* Every write reaches the disk before the caller replies to the game. A save written by an
* older format version is upgraded step by step when it is opened (save-upgrades.ts).
*/
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { decodeBlk, encodeBlk, type BlkBlock } from '@wto/blk';
import { normalizeModificationBlocks } from '../profile/profile-modifications.ts';
import { writeFileAtomic } from '../storage/atomic-file.ts';
import type { SaveUpgrades } from './save-upgrades.ts';
interface SaveInfo {
formatVersion: number;
created: string;
updated: string;
}
export class SaveStore {
readonly #folder: string;
readonly #upgrades: SaveUpgrades;
constructor(dataFolder: string, upgrades: SaveUpgrades) {
this.#folder = join(dataFolder, 'saves');
this.#upgrades = upgrades;
}
/** The account's profile, or undefined when the account has no save yet. */
loadProfile(userId: string): BlkBlock | undefined {
const infoPath = this.#infoPath(userId);
if (!existsSync(infoPath)) return undefined;
const info = JSON.parse(readFileSync(infoPath, 'utf8')) as SaveInfo;
const profile = decodeBlk(readFileSync(this.#profilePath(userId)));
if (info.formatVersion === this.#upgrades.currentFormat) return profile;
const upgraded = this.#upgrades.upgrade(profile, info.formatVersion);
this.saveProfile(userId, upgraded);
return upgraded;
}
saveProfile(userId: string, profile: BlkBlock): void {
const now = new Date().toISOString();
const previous = existsSync(this.#infoPath(userId)) ? (JSON.parse(readFileSync(this.#infoPath(userId), 'utf8')) as SaveInfo) : undefined;
normalizeModificationBlocks(profile);
writeFileAtomic(this.#profilePath(userId), encodeBlk(profile));
const info: SaveInfo = { formatVersion: this.#upgrades.currentFormat, created: previous?.created ?? now, updated: now };
writeFileAtomic(this.#infoPath(userId), JSON.stringify(info, null, 2));
}
#infoPath(userId: string): string {
return join(this.#folder, safeId(userId), 'save.json');
}
#profilePath(userId: string): string {
return join(this.#folder, safeId(userId), 'profile.blk');
}
}
function safeId(userId: string): string {
if (!/^\d+$/.test(userId)) throw new Error(`invalid user id: ${userId}`);
return userId;
}