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.
This commit is contained in:
N0VAN0M4LY committed 2026-12-03 01:18:14 +08:00
1 parent 5c09c4e47a
commit 4e72bf3746
5 files changed
+121 -21

No files matched your search

@@ -4,7 +4,12 @@
* 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 { recordName, type ModificationRecord } from '../profile/profile-modifications.ts';
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';
@@ -21,7 +26,7 @@ export class ProfileEquipmentIndex {
readonly #weapons = new Map<string, BlkBlock>();
readonly #modsResearch = new Map<string, BlkBlock>();
readonly #removedResearch = new Set<string>();
#modificationsBlock: BlkBlock | undefined;
readonly #modificationsBlocks: BlkBlock[];
#weaponsBlock: BlkBlock | undefined;
#modsResearchBlock: BlkBlock | undefined;
@@ -30,9 +35,9 @@ export class ProfileEquipmentIndex {
const hangar = findBlock(profile, HANGAR);
if (hangar) for (const entry of hangar.blocks) this.#hangar.set(entry.name, entry);
this.#modificationsBlock = findBlock(profile, MODIFICATIONS);
if (this.#modificationsBlock) {
for (const entry of this.#modificationsBlock.blocks) this.#modifications.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);
@@ -103,14 +108,32 @@ export class ProfileEquipmentIndex {
const key = recordName(vehicle, modification);
let block = this.#modifications.get(key);
if (!block) {
this.#modificationsBlock ??= ensureBlock(this.#profile, MODIFICATIONS);
block = { name: key, params: [], blocks: [] };
this.#modificationsBlock.blocks.push(block);
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;
@@ -11,7 +11,7 @@
*/
import { ensureBlock, findBlock, type BlkBlock } from '@wto/blk';
import type { BattleSpawnRulesData } from '../game-data/datasets/battle-spawn-rules.ts';
import { recordName } from '../profile/profile-modifications.ts';
import { allModificationBlocks, appendModificationEntry, recordName } from '../profile/profile-modifications.ts';
import type { EquipmentCatalogue } from './equipment-catalogue.ts';
export interface SupportPlaneModification {
@@ -98,13 +98,14 @@ export function supportPlaneModifications(
export function grantSupportPlaneModifications(profile: BlkBlock, grants: SupportUnitEquipmentGrants): void {
if (grants.modifications.length > 0) {
let modsBlock = findBlock(profile, 'modifications');
const existing = new Set((modsBlock?.blocks ?? []).map((entry) => entry.name));
const existing = new Set<string>();
for (const chunk of allModificationBlocks(profile)) {
for (const entry of chunk.blocks) existing.add(entry.name);
}
for (const { vehicle, modification } of grants.modifications) {
const key = recordName(vehicle, modification);
if (existing.has(key)) continue;
modsBlock ??= ensureBlock(profile, 'modifications');
modsBlock.blocks.push({ name: key, params: [{ name: 'count', value: { type: 'int64', value: 1n } }], blocks: [] });
appendModificationEntry(profile, { name: key, params: [{ name: 'count', value: { type: 'int64', value: 1n } }], blocks: [] });
existing.add(key);
}
}
@@ -12,11 +12,19 @@
* 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 { ensureBlock, findBlock, type BlkBlock, type BlkParam } from '@wto/blk';
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;
@@ -29,9 +37,66 @@ 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 block = findBlock(profile, MODIFICATIONS);
const record = block && findBlock(block, recordName(vehicle, modification));
const record = findModificationEntry(profile, recordName(vehicle, modification));
if (!record) return undefined;
return {
silverLionsSpent: intOf(record, 'wpSpent'),
@@ -42,12 +107,17 @@ export function modificationRecord(profile: BlkBlock, vehicle: string, modificat
}
export function ownsModification(profile: BlkBlock, vehicle: string, modification: string): boolean {
return modificationRecord(profile, vehicle, modification) !== undefined;
return findModificationEntry(profile, recordName(vehicle, modification)) !== undefined;
}
export function writeModificationRecord(profile: BlkBlock, vehicle: string, modification: string, record: ModificationRecord): void {
const block = ensureBlock(ensureBlock(profile, MODIFICATIONS), recordName(vehicle, modification));
block.params = orderedParams(record);
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[] {
+2
View File
@@ -11,6 +11,7 @@
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';
@@ -44,6 +45,7 @@ export class SaveStore {
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));
+7 -3
View File
@@ -10,7 +10,7 @@ import { InventoryCatalogue } from '../inventory/itemdef-catalogue.ts';
import { intField, stringField } from '../profile/blk-fields.ts';
import { allCrews } from '../profile/profile-crews.ts';
import { ACTIVE_LISTS } from '../profile/profile-items.ts';
import { recordName } from '../profile/profile-modifications.ts';
import { normalizeModificationBlocks, recordName } from '../profile/profile-modifications.ts';
import { loadTuning } from '../tuning/tuning.ts';
import type { SaveLocation } from './save-editor.ts';
@@ -38,9 +38,13 @@ export function prepareTransferredProfile(location: SaveLocation, bytes: Uint8Ar
['modifications', modifications], ['modsResearch', modifications], ['weapons', weapons],
] as const;
for (const [key, allowed] of blockKeys) {
const collection = findBlock(profile, key);
if (collection) collection.blocks = collection.blocks.filter((entry) => allowed.has(entry.name));
for (const collection of profile.blocks) {
if (collection.name === key) {
collection.blocks = collection.blocks.filter((entry) => allowed.has(entry.name));
}
}
}
normalizeModificationBlocks(profile);
for (const crew of allCrews(profile)) {
const aircraft = findBlock(crew, 'aircrafts');
if (aircraft) {