From dc01eff8125c6fdfdc1967128bc863bd5504c540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Sun, 10 Dec 2023 11:57:21 -0600 Subject: [PATCH] feat: ka 0xff02 handling --- scripts/buildAssets.ts | 10 +--------- src/core/blitz/SCPGStream.ts | 38 +++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/scripts/buildAssets.ts b/scripts/buildAssets.ts index c405d9605..4551d3d72 100644 --- a/scripts/buildAssets.ts +++ b/scripts/buildAssets.ts @@ -315,7 +315,7 @@ if (allTargets || targets?.includes('tankModels')) { // const scgStream = await SCPGStream.fromDVPLFile( // `${DATA}/${DOI['3d']}/${scgPath}.dvpl`, // ); - // const sc2Data = sc2Stream.consumeSC2(); + const sc2Data = sc2Stream.consumeSC2(); // const scgData = scgStream.consumeSCG(); // writeFile( @@ -324,14 +324,6 @@ if (allTargets || targets?.includes('tankModels')) { // typeof value === 'bigint' ? value.toString() : value, // ), // ); - - try { - sc2Stream.consumeSC2(); - } catch (error) { - if (!(error as Error).message.startsWith('Unhandled KA version')) { - throw error; - } - } } }), ); diff --git a/src/core/blitz/SCPGStream.ts b/src/core/blitz/SCPGStream.ts index f1013c5bd..0b8f8cd41 100644 --- a/src/core/blitz/SCPGStream.ts +++ b/src/core/blitz/SCPGStream.ts @@ -309,20 +309,11 @@ export class SCPGStream { return this.consumeKA(); } - consumeKAHeader() { - return { - name: this.consumeAscii(2), - version: this.consumeUInt16(), - count: this.consumeUInt32(), - }; - } // TODO: change return type consumeKAValue( type: KAType = this.consumeUInt8(), stringTable?: Record, ): unknown { - console.log(KAType[type]); - switch (type) { case KAType.NONE: return undefined; @@ -439,30 +430,36 @@ export class SCPGStream { return { name, value }; } consumeKA(stringTable?: Record) { - const header = this.consumeKAHeader(); + this.consumeAscii(2); // "KA" + const version = this.consumeUInt16(); const pairs: Record = {}; - if (header.version === 1) { - for (let index = 0; index < header.count; index++) { + if (version === 0x0001) { + const count = this.consumeUInt32(); + + for (let index = 0; index < count; index++) { const { name, value } = this.consumeKAV1Pair(); pairs[name] = value; } - } else if (header.version === 2) { + } else if (version === 0x0002) { + const count = this.consumeUInt32(); const strings: string[] = []; const stringTable: Record = {}; - for (let index = 0; index < header.count; index++) { + + for (let index = 0; index < count; index++) { const length = this.consumeUInt16(); const string = this.consumeAscii(length); strings.push(string); } - for (let index = 0; index < header.count; index++) { + for (let index = 0; index < count; index++) { const id = this.consumeUInt32(); stringTable[id] = strings[index]; } const nodeCount = this.consumeUInt32(); + for (let index = 0; index < nodeCount; index++) { const keyId = this.consumeUInt32(); const value = this.consumeKAValue(undefined, stringTable); @@ -470,10 +467,12 @@ export class SCPGStream { pairs[key] = value; } - } else if (header.version === 258) { + } else if (version === 0x0102) { if (!stringTable) throw new Error('No string table provided'); - for (let index = 0; index < header.count; index++) { + const count = this.consumeUInt32(); + + for (let index = 0; index < count; index++) { const keyIndex = this.consumeUInt32(); const key = stringTable[keyIndex]; const valueType = this.consumeUInt8(); @@ -487,7 +486,10 @@ export class SCPGStream { pairs[key] = value; } } - } else throw new RangeError(`Unhandled KA version: ${header.version}`); + } else if (version === 0xff02) { + // same as 0x0102 but empty and with no count + return {}; + } else throw new RangeError(`Unhandled KA version: ${version}`); return pairs; }