Files
WT-Offline/apps/server/src/modifications/profile-equipment-index.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

227 lines
9.0 KiB
TypeScript

/**
* Hash-indexed view over a profile's flat `hangar`, `modifications`, `weapons` and `modsResearch`
* blocks for the duration of a single refresh pass. Large profiles with 1,000+ spaded vehicles
* carry 30,000+ flat sub-blocks; indexing once drops refresh cost from O(N^2) to O(N).
*/
import { ensureBlock, findBlock, findParam, setParam, type BlkBlock, type BlkParam } from '@wto/blk';
import {
allModificationBlocks,
MAX_MODIFICATION_BLOCK_ENTRIES,
recordName,
type ModificationRecord,
} from '../profile/profile-modifications.ts';
import type { WeaponRecord } from '../profile/profile-weapons.ts';
const HANGAR = 'hangar';
const MODIFICATIONS = 'modifications';
const WEAPONS = 'weapons';
const MODS_RESEARCH = 'modsResearch';
const STATUS = 'modStatus';
const RESEARCH_POINTS = 'exp';
export class ProfileEquipmentIndex {
readonly #profile: BlkBlock;
readonly #hangar = new Map<string, BlkBlock>();
readonly #modifications = new Map<string, BlkBlock>();
readonly #weapons = new Map<string, BlkBlock>();
readonly #modsResearch = new Map<string, BlkBlock>();
readonly #removedResearch = new Set<string>();
readonly #modificationsBlocks: BlkBlock[];
#weaponsBlock: BlkBlock | undefined;
#modsResearchBlock: BlkBlock | undefined;
constructor(profile: BlkBlock) {
this.#profile = profile;
const hangar = findBlock(profile, HANGAR);
if (hangar) for (const entry of hangar.blocks) this.#hangar.set(entry.name, entry);
this.#modificationsBlocks = allModificationBlocks(profile);
for (const chunk of this.#modificationsBlocks) {
for (const entry of chunk.blocks) this.#modifications.set(entry.name, entry);
}
this.#weaponsBlock = findBlock(profile, WEAPONS);
if (this.#weaponsBlock) {
for (const entry of this.#weaponsBlock.blocks) this.#weapons.set(entry.name, entry);
}
this.#modsResearchBlock = findBlock(profile, MODS_RESEARCH);
if (this.#modsResearchBlock) {
for (const entry of this.#modsResearchBlock.blocks) this.#modsResearch.set(entry.name, entry);
}
}
hangarEntry(vehicle: string): BlkBlock | undefined {
return this.#hangar.get(vehicle);
}
setOpenTier(vehicle: string, tier: number): void {
const entry = this.#hangar.get(vehicle);
if (entry) setParam(entry, 'tierNumber', { type: 'int', value: tier });
}
moduleResearchTarget(vehicle: string): string {
const entry = this.#hangar.get(vehicle);
const value = entry && findParam(entry, 'investMode')?.value;
return value?.type === 'string' ? value.value : '';
}
setModuleResearchTarget(vehicle: string, modification: string): void {
const entry = this.#hangar.get(vehicle);
if (entry) setParam(entry, 'investMode', { type: 'string', value: modification });
}
finishedModuleResearch(vehicle: string): string {
const entry = this.#hangar.get(vehicle);
const value = entry && findParam(entry, 'investModePrev')?.value;
return value?.type === 'string' ? value.value : '';
}
vehicleExcessRp(vehicle: string): number {
const entry = this.#hangar.get(vehicle);
const value = entry && findParam(entry, 'excessExp')?.value;
if (value?.type === 'int64') return Math.max(0, Number(value.value));
return value?.type === 'int' ? Math.max(0, value.value) : 0;
}
setVehicleExcessRp(vehicle: string, amount: number): void {
const entry = this.#hangar.get(vehicle);
if (entry) setParam(entry, 'excessExp', { type: 'int64', value: BigInt(Math.max(0, Math.trunc(amount))) });
}
ownsModification(vehicle: string, modification: string): boolean {
return this.#modifications.has(recordName(vehicle, modification));
}
modificationRecord(vehicle: string, modification: string): ModificationRecord | undefined {
const block = this.#modifications.get(recordName(vehicle, modification));
if (!block) return undefined;
return {
silverLionsSpent: intOf(block, 'wpSpent'),
goldenEaglesSpent: intOf(block, 'goldSpent'),
enabled: boolOf(block, 'enabled') ?? true,
count: intOf(block, 'count'),
};
}
writeModificationRecord(vehicle: string, modification: string, record: ModificationRecord): void {
const key = recordName(vehicle, modification);
let block = this.#modifications.get(key);
if (!block) {
block = { name: key, params: [], blocks: [] };
this.#appendModificationBlock(block);
this.#modifications.set(key, block);
}
block.params = orderedModParams(record);
}
#appendModificationBlock(entry: BlkBlock): void {
for (let index = this.#modificationsBlocks.length - 1; index >= 0; index--) {
const chunk = this.#modificationsBlocks[index]!;
if (chunk.blocks.length < MAX_MODIFICATION_BLOCK_ENTRIES) {
chunk.blocks.push(entry);
return;
}
}
const nextChunk: BlkBlock = { name: MODIFICATIONS, params: [], blocks: [entry] };
const lastChunk = this.#modificationsBlocks[this.#modificationsBlocks.length - 1];
if (lastChunk) {
const insertAt = this.#profile.blocks.lastIndexOf(lastChunk) + 1;
this.#profile.blocks.splice(insertAt, 0, nextChunk);
} else {
this.#profile.blocks.push(nextChunk);
}
this.#modificationsBlocks.push(nextChunk);
}
weaponRecord(vehicle: string, weapon: string): WeaponRecord | undefined {
const block = this.#weapons.get(`${vehicle}_${weapon}`);
if (!block) return undefined;
return {
silverLionsSpent: intOf(block, 'wpSpent'),
goldenEaglesSpent: intOf(block, 'goldSpent'),
count: intOf(block, 'count'),
};
}
writeWeaponRecord(vehicle: string, weapon: string, record: WeaponRecord): void {
const key = `${vehicle}_${weapon}`;
let block = this.#weapons.get(key);
if (!block) {
this.#weaponsBlock ??= ensureBlock(this.#profile, WEAPONS);
block = { name: key, params: [], blocks: [] };
this.#weaponsBlock.blocks.push(block);
this.#weapons.set(key, block);
}
block.params = orderedWeaponParams(record);
}
moduleResearchPoints(vehicle: string, modification: string): number {
const key = recordName(vehicle, modification);
if (this.#removedResearch.has(key)) return 0;
const entry = this.#modsResearch.get(key);
const value = entry && findParam(entry, RESEARCH_POINTS)?.value;
if (value?.type === 'int64') return Number(value.value);
return value?.type === 'int' ? value.value : 0;
}
setModuleStatus(vehicle: string, modification: string, status: number): void {
const key = recordName(vehicle, modification);
const points = this.moduleResearchPoints(vehicle, modification);
this.#removedResearch.delete(key);
let entry = this.#modsResearch.get(key);
if (!entry) {
this.#modsResearchBlock ??= ensureBlock(this.#profile, MODS_RESEARCH);
entry = { name: key, params: [], blocks: [] };
this.#modsResearchBlock.blocks.push(entry);
this.#modsResearch.set(key, entry);
}
const params: BlkParam[] = [];
if (points) params.push({ name: RESEARCH_POINTS, value: { type: 'int64', value: BigInt(points) } });
params.push({ name: STATUS, value: { type: 'int', value: status } });
entry.params = params;
}
removeModuleResearch(vehicle: string, modification: string): void {
const key = recordName(vehicle, modification);
if (!this.#modsResearch.has(key)) return;
this.#removedResearch.add(key);
this.#modsResearch.delete(key);
}
flush(): void {
if (this.#removedResearch.size > 0 && this.#modsResearchBlock) {
this.#modsResearchBlock.blocks = this.#modsResearchBlock.blocks.filter((entry) => !this.#removedResearch.has(entry.name));
this.#removedResearch.clear();
}
}
}
function orderedModParams(record: ModificationRecord): BlkParam[] {
const params: BlkParam[] = [];
if (record.silverLionsSpent) params.push({ name: 'wpSpent', value: { type: 'int', value: record.silverLionsSpent } });
if (record.goldenEaglesSpent) params.push({ name: 'goldSpent', value: { type: 'int', value: record.goldenEaglesSpent } });
if (!record.enabled) params.push({ name: 'enabled', value: { type: 'bool', value: false } });
if (record.count) params.push({ name: 'count', value: { type: 'int64', value: BigInt(record.count) } });
return params;
}
function orderedWeaponParams(record: WeaponRecord): BlkParam[] {
const params: BlkParam[] = [];
if (record.silverLionsSpent) params.push({ name: 'wpSpent', value: { type: 'int', value: record.silverLionsSpent } });
if (record.goldenEaglesSpent) params.push({ name: 'goldSpent', value: { type: 'int', value: record.goldenEaglesSpent } });
if (record.count) params.push({ name: 'count', value: { type: 'int64', value: BigInt(record.count) } });
return params;
}
function intOf(block: BlkBlock, name: string): number {
const value = block.params.find((param) => param.name === name)?.value;
if (value?.type === 'int64') return Number(value.value);
return value?.type === 'int' ? value.value : 0;
}
function boolOf(block: BlkBlock, name: string): boolean | undefined {
const value = block.params.find((param) => param.name === name)?.value;
return value?.type === 'bool' ? value.value : undefined;
}