From ecf8a1dd0b958cae726062ab04c3e495efd259ba Mon Sep 17 00:00:00 2001 From: DankParrot Date: Mon, 3 Aug 2020 16:08:00 -0700 Subject: [PATCH] Add NBT::Tag::GetData() and NBT::DataBuffer --- src/nbt/NBT.cpp | 14 ++++++++++++++ src/nbt/NBT.h | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/nbt/NBT.cpp b/src/nbt/NBT.cpp index 5a10d5e..c10d727 100644 --- a/src/nbt/NBT.cpp +++ b/src/nbt/NBT.cpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace NBT { @@ -38,6 +39,12 @@ namespace NBT return list_entry(item, const decltype(nbt_node::payload)::nbt_list, entry)->data; } + DataBuffer::~DataBuffer() + { + // equivalent to buffer_free in cNBT buffer.h + free(data); + } + Tag Tag::CreateTag(nbt_node *node) { switch (node->type) @@ -119,6 +126,13 @@ namespace NBT } } + const DataBuffer Tag::GetData() const + { + buffer buf = nbt_dump_binary(m_node); + DataBuffer result { buf.data, buf.len }; + return result; + } + const char *Tag::ToString() const { return nbt_dump_ascii(m_node); diff --git a/src/nbt/NBT.h b/src/nbt/NBT.h index 932c0ae..d49bff7 100644 --- a/src/nbt/NBT.h +++ b/src/nbt/NBT.h @@ -13,7 +13,17 @@ struct nbt_node; struct list_head; namespace NBT -{ +{ + // NBT-specific data buffer. + // TODO: could replace with some project-wide utility class + struct DataBuffer + { + uint8_t* data; + size_t length; + + ~DataBuffer(); + }; + // Represents a generic NBT tag of any type class Tag { @@ -31,6 +41,9 @@ namespace NBT // Get the number of iterable immediate children size_t GetLength() const; + // Get uncompressed NBT binary data + const DataBuffer GetData() const; + const char* ToString() const; };