From b96692b1fda635c485205dbd810e1a2da8373a7e Mon Sep 17 00:00:00 2001 From: Bradley Watton Date: Thu, 22 Oct 2020 16:59:30 +0100 Subject: [PATCH] Fix compound lists --- .gitignore | 2 ++ src/BinaryData.ts | 31 +++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 74d11d2..37f5955 100644 --- a/.gitignore +++ b/.gitignore @@ -105,3 +105,5 @@ dist # Compiled lib/ + +test/ diff --git a/src/BinaryData.ts b/src/BinaryData.ts index 26da07a..36d0191 100644 --- a/src/BinaryData.ts +++ b/src/BinaryData.ts @@ -1,5 +1,5 @@ import { BinaryData as BData, DataLengths } from '@strdst/utils.binary' -import { EndTag, ListTag, Tag, TagMapper, TagType } from '@strdst/utils.nbt' +import { CompoundTag, EndTag, ListTag, Tag, TagMapper, TagType } from '@strdst/utils.nbt' export class BinaryData extends BData { @@ -31,7 +31,7 @@ export class BinaryData extends BData { case TagType.Int: return this.readTagArray('readInt', 'readInt') case TagType.Compound: - return this.readTagArray('readTag', 'readInt') + return this.readCompoundList() default: throw new Error(`Don't know how to read ListTag.valueType of ${list.valueType}`) } @@ -46,6 +46,33 @@ export class BinaryData extends BData { return super.readTagValue(tag) } + public readCompoundList(): CompoundTag[] { + const count = this.readInt() + + let gotEnd = false + + const compounds: CompoundTag[] = [] + while(!gotEnd) { + const tag = new CompoundTag() + + const tags: Record = {} + for(let i = 0; i < count; i++) { + const tag = this.readTag() + + if(tag instanceof EndTag) { + gotEnd = true + continue + } + + tags[tag.name] = tag + } + + compounds.push(tag.assign('', tags)) + } + + return compounds + } + public readUnsignedInt(skip = true): number { const v = this.buf.readUInt32BE(this.pos)