/** * Bought modifications in the profile: * * modifications{ * us_m22_locust_tank_tool_kit{ count:i64=1 } * us_m22_locust_37mm_usa_m3_APC{ wpSpent:i=450 count:i64=150 } * us_m22_locust_new_tank_tracks{ enabled:b=no count:i64=1 } * } * * One block per bought modification, named _; having a block means the * modification is owned. The client reads a block's params strictly in this order and stops at * the first one that does not match (aces.exe 0x942740), so a record is always rewritten whole: * wpSpent:i goldSpent:i lastWpCost:i lastGoldCost:i persist:b rpEarned:i enabled:b count:i64 modLevel:i * Absent values are left out; enabled is only written when false (the default is enabled). * * Dagor's binary DataBlock stores child block counts in a uint16 (65,535 max). Because the full * vehicle catalogue exceeds 65,535 modifications, aces.exe splits modifications across up to 3 * sibling top-level `modifications` blocks of at most 65,535 entries each (2.61.0.37 0x938fae..0x9397e6, * 2.61.0.58 0x93ba70 / 0x93bb53). All readers and writers here span every sibling block. */ import type { BlkBlock, BlkParam } from '@wto/blk'; const MODIFICATIONS = 'modifications'; /** Maximum entries per `modifications` DataBlock (uint16 child count limit in aces.exe 0x939000 / 0x93ba70). */ export const MAX_MODIFICATION_BLOCK_ENTRIES = 0xffff; export interface ModificationRecord { readonly silverLionsSpent: number; readonly goldenEaglesSpent: number; readonly enabled: boolean; /** How many the player has: 1 for a plain modification, the number of rounds or uses for counted ones. */ readonly count: number; } export function recordName(vehicle: string, modification: string): string { return `${vehicle}_${modification}`; } /** Every top-level `modifications` chunk in the profile, in document order. */ export function allModificationBlocks(profile: BlkBlock): BlkBlock[] { return profile.blocks.filter((block) => block.name === MODIFICATIONS); } /** Append a new modification entry to a chunk with spare capacity, opening a sibling chunk when full. */ export function appendModificationEntry(profile: BlkBlock, entry: BlkBlock): void { const chunks = allModificationBlocks(profile); for (let index = chunks.length - 1; index >= 0; index--) { const chunk = chunks[index]!; if (chunk.blocks.length < MAX_MODIFICATION_BLOCK_ENTRIES) { chunk.blocks.push(entry); return; } } const nextChunk: BlkBlock = { name: MODIFICATIONS, params: [], blocks: [entry] }; const lastChunk = chunks[chunks.length - 1]; if (lastChunk) { const insertAt = profile.blocks.lastIndexOf(lastChunk) + 1; profile.blocks.splice(insertAt, 0, nextChunk); } else { profile.blocks.push(nextChunk); } } /** Ensure every top-level `modifications` chunk stays within the 65,535-child DataBlock limit. */ export function normalizeModificationBlocks(profile: BlkBlock): void { const chunks = allModificationBlocks(profile); if (chunks.length === 0) return; const needsRebalance = chunks.some((chunk) => chunk.blocks.length > MAX_MODIFICATION_BLOCK_ENTRIES) || (chunks.length > 1 && chunks.some((chunk) => chunk.blocks.length === 0)); if (!needsRebalance) return; const firstIndex = profile.blocks.findIndex((block) => block.name === MODIFICATIONS); const entries = chunks.flatMap((chunk) => chunk.blocks); profile.blocks = profile.blocks.filter((block) => block.name !== MODIFICATIONS); if (entries.length === 0) return; const rebuilt: BlkBlock[] = []; for (let offset = 0; offset < entries.length; offset += MAX_MODIFICATION_BLOCK_ENTRIES) { rebuilt.push({ name: MODIFICATIONS, params: [], blocks: entries.slice(offset, offset + MAX_MODIFICATION_BLOCK_ENTRIES), }); } profile.blocks.splice(Math.max(0, firstIndex), 0, ...rebuilt); } function findModificationEntry(profile: BlkBlock, key: string): BlkBlock | undefined { for (const chunk of profile.blocks) { if (chunk.name !== MODIFICATIONS) continue; const found = chunk.blocks.find((entry) => entry.name === key); if (found) return found; } return undefined; } export function modificationRecord(profile: BlkBlock, vehicle: string, modification: string): ModificationRecord | undefined { const record = findModificationEntry(profile, recordName(vehicle, modification)); if (!record) return undefined; return { silverLionsSpent: intOf(record, 'wpSpent'), goldenEaglesSpent: intOf(record, 'goldSpent'), enabled: boolOf(record, 'enabled') ?? true, count: intOf(record, 'count'), }; } export function ownsModification(profile: BlkBlock, vehicle: string, modification: string): boolean { return findModificationEntry(profile, recordName(vehicle, modification)) !== undefined; } export function writeModificationRecord(profile: BlkBlock, vehicle: string, modification: string, record: ModificationRecord): void { const key = recordName(vehicle, modification); const existing = findModificationEntry(profile, key); if (existing) { existing.params = orderedParams(record); return; } appendModificationEntry(profile, { name: key, params: orderedParams(record), blocks: [] }); } function orderedParams(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 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; }