From 5c0093e0d3e0eb6577665353f81151140ae9eaf3 Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 26 Jul 2023 13:52:51 -0400 Subject: [PATCH] add change tag_compound to use hashmap, allow vectors --- include/blt/nbt/nbt.h | 46 ++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/include/blt/nbt/nbt.h b/include/blt/nbt/nbt.h index 9cb9372..d104913 100755 --- a/include/blt/nbt/nbt.h +++ b/include/blt/nbt/nbt.h @@ -11,11 +11,17 @@ #include #include #include +#include #include "blt/std/format.h" #include "blt/std/filesystem.h" namespace blt::nbt { +#ifndef HASHMAP + template + using HASHMAP = std::unordered_map; +#endif + void writeUTF8String(blt::fs::block_writer& stream, const std::string& str); std::string readUTF8String(blt::fs::block_reader& stream); @@ -52,6 +58,7 @@ namespace blt::nbt { toBytes(d, data); out.write(data, sizeof(T)); } + template inline static void readData(blt::fs::block_reader& in, T& d) { char data[sizeof(T)]; @@ -59,7 +66,6 @@ namespace blt::nbt { fromBytes(data, &d); } - enum class nbt_tag : char { END = 0, BYTE = 1, @@ -268,6 +274,12 @@ namespace blt::nbt { return new blt::nbt::tag_long_array; } } + static HASHMAP toHashmap(const std::vector& v){ + HASHMAP tags; + for (const auto& t : v) + tags[t->getName()] = t; + return tags; + } } class tag_list : public tag> { @@ -303,10 +315,11 @@ namespace blt::nbt { } }; - class tag_compound : public tag> { + class tag_compound : public tag> { public: tag_compound(): tag(nbt_tag::COMPOUND) {} - tag_compound(const std::string& name, const std::vector& v): tag(nbt_tag::COMPOUND, name, v) {} + tag_compound(const std::string& name, const std::vector& v): tag(nbt_tag::COMPOUND, name, _internal_::toHashmap(v)) {} + tag_compound(const std::string& name, const HASHMAP& v): tag(nbt_tag::COMPOUND, name, v) {} void writePayload(blt::fs::block_writer& out) final { for (auto*& v : t){ out.put((char) v->getType()); @@ -334,22 +347,23 @@ namespace blt::nbt { return new blt::nbt::tag_list; } - class NBTDecoder { - private: - blt::fs::block_reader* m_reader; - public: - explicit NBTDecoder(blt::fs::block_reader* reader): m_reader(reader) {} - - }; - - /** - * Reads the entire NBT file when the read() function is called. - */ class NBTReader { private: - std::string m_file; + blt::fs::block_reader& reader; public: - explicit NBTReader(std::string file): m_file(std::move(file)) {} + explicit NBTReader(blt::fs::block_reader& reader): reader(reader) {} + + }; + + class NBTWriter { + private: + blt::fs::block_writer& writer; + tag_compound* root = nullptr; + public: + explicit NBTWriter(blt::fs::block_writer& writer): writer(writer) {} + void readFully(){ + + } }; }