diff --git a/src/nbt/NBT.cpp b/src/nbt/NBT.cpp index 33f2de5..639b106 100644 --- a/src/nbt/NBT.cpp +++ b/src/nbt/NBT.cpp @@ -82,6 +82,10 @@ namespace NBT case TAG_LONG_ARRAY: return m_node->payload.tag_long_array.length; + case TAG_STRING: + // cNBT does not store string length so compute here + return strlen(m_node->payload.tag_string); + case TAG_LIST: return list_length(&(m_node->payload.tag_list->entry)); case TAG_COMPOUND: diff --git a/src/nbt/NBT.h b/src/nbt/NBT.h index a8b9352..66454e3 100644 --- a/src/nbt/NBT.h +++ b/src/nbt/NBT.h @@ -11,6 +11,7 @@ struct list_head; namespace NBT { + // Represents a generic NBT tag of any type class Tag { protected: @@ -21,10 +22,12 @@ namespace NBT static Tag CreateListTag(nbt_node* node); public: + // This will be "" if the tag is nameless const char* GetName() const; // Get the number of iterable immediate children size_t GetLength() const; + const char* ToString() const; }; @@ -42,6 +45,16 @@ namespace NBT inline operator T() const { return GetValue(); } }; + // Standard value types + using ByteTag = DataTag; + using ShortTag = DataTag; + using IntTag = DataTag; + using LongTag = DataTag; + using FloatTag = DataTag; + using DoubleTag = DataTag; + + // Represents a generic array, used for StringTag, + // ByteArrayTag, IntArrayTag, and LongArrayTag template class ArrayTag : public DataTag { @@ -50,12 +63,22 @@ namespace NBT const T* GetValue() const; + // Methods for range-based for loops + // TODO: check if this works for IntArrayTag and LongArrayTag const T* begin() { return GetValue(); } - const T* end() { return &(GetValue()[this->GetLength()]); } + const T* end() { return GetValue() + Tag::GetLength(); } }; + // A string is an array of char. Its length is + // not stored by cNBT, so Tag::GetLength uses strlen using StringTag = ArrayTag; + // Standard array types + using ByteArrayTag = ArrayTag; + using IntArrayTag = ArrayTag; + using LongArrayTag = ArrayTag; + + // Represents a list of nameless tags of the same type template class ListTag : public Tag {