add change tag_compound to use hashmap, allow vectors

v1
Brett 2023-07-26 13:52:51 -04:00
parent c4c23e9cc6
commit 5c0093e0d3
1 changed files with 30 additions and 16 deletions

View File

@ -11,11 +11,17 @@
#include <bit> #include <bit>
#include <cstring> #include <cstring>
#include <type_traits> #include <type_traits>
#include <unordered_map>
#include "blt/std/format.h" #include "blt/std/format.h"
#include "blt/std/filesystem.h" #include "blt/std/filesystem.h"
namespace blt::nbt { namespace blt::nbt {
#ifndef HASHMAP
template<typename K, typename V>
using HASHMAP = std::unordered_map<K, V>;
#endif
void writeUTF8String(blt::fs::block_writer& stream, const std::string& str); void writeUTF8String(blt::fs::block_writer& stream, const std::string& str);
std::string readUTF8String(blt::fs::block_reader& stream); std::string readUTF8String(blt::fs::block_reader& stream);
@ -52,6 +58,7 @@ namespace blt::nbt {
toBytes(d, data); toBytes(d, data);
out.write(data, sizeof(T)); out.write(data, sizeof(T));
} }
template<typename T> template<typename T>
inline static void readData(blt::fs::block_reader& in, T& d) { inline static void readData(blt::fs::block_reader& in, T& d) {
char data[sizeof(T)]; char data[sizeof(T)];
@ -59,7 +66,6 @@ namespace blt::nbt {
fromBytes(data, &d); fromBytes(data, &d);
} }
enum class nbt_tag : char { enum class nbt_tag : char {
END = 0, END = 0,
BYTE = 1, BYTE = 1,
@ -268,6 +274,12 @@ namespace blt::nbt {
return new blt::nbt::tag_long_array; return new blt::nbt::tag_long_array;
} }
} }
static HASHMAP<std::string, tag_t*> toHashmap(const std::vector<tag_t*>& v){
HASHMAP<std::string, tag_t*> tags;
for (const auto& t : v)
tags[t->getName()] = t;
return tags;
}
} }
class tag_list : public tag<std::vector<tag_t*>> { class tag_list : public tag<std::vector<tag_t*>> {
@ -303,10 +315,11 @@ namespace blt::nbt {
} }
}; };
class tag_compound : public tag<std::vector<tag_t*>> { class tag_compound : public tag<HASHMAP<std::string, tag_t*>> {
public: public:
tag_compound(): tag(nbt_tag::COMPOUND) {} tag_compound(): tag(nbt_tag::COMPOUND) {}
tag_compound(const std::string& name, const std::vector<tag_t*>& v): tag(nbt_tag::COMPOUND, name, v) {} tag_compound(const std::string& name, const std::vector<tag_t*>& v): tag(nbt_tag::COMPOUND, name, _internal_::toHashmap(v)) {}
tag_compound(const std::string& name, const HASHMAP<std::string, tag_t*>& v): tag(nbt_tag::COMPOUND, name, v) {}
void writePayload(blt::fs::block_writer& out) final { void writePayload(blt::fs::block_writer& out) final {
for (auto*& v : t){ for (auto*& v : t){
out.put((char) v->getType()); out.put((char) v->getType());
@ -334,22 +347,23 @@ namespace blt::nbt {
return new blt::nbt::tag_list; return new blt::nbt::tag_list;
} }
class NBTDecoder { class NBTReader {
private: private:
blt::fs::block_reader* m_reader; blt::fs::block_reader& reader;
public: public:
explicit NBTDecoder(blt::fs::block_reader* reader): m_reader(reader) {} explicit NBTReader(blt::fs::block_reader& reader): reader(reader) {}
}; };
/** class NBTWriter {
* Reads the entire NBT file when the read() function is called.
*/
class NBTReader {
private: private:
std::string m_file; blt::fs::block_writer& writer;
tag_compound* root = nullptr;
public: public:
explicit NBTReader(std::string file): m_file(std::move(file)) {} explicit NBTWriter(blt::fs::block_writer& writer): writer(writer) {}
void readFully(){
}
}; };
} }