From 126faa0b032a3b36564ce5a88d2ecc5c15eba7f0 Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 27 Jul 2023 01:47:11 -0400 Subject: [PATCH] NBT working!! --- include/blt/nbt/nbt.h | 3 +++ include/blt/std/filesystem.h | 10 +++++++--- include/blt/std/format.h | 2 +- src/blt/nbt/nbt.cpp | 4 ++-- src/blt/std/filesystem.cpp | 2 +- src/tests/main.cpp | 10 +++++++++- 6 files changed, 23 insertions(+), 8 deletions(-) diff --git a/include/blt/nbt/nbt.h b/include/blt/nbt/nbt.h index 605a9a0..273c7ad 100755 --- a/include/blt/nbt/nbt.h +++ b/include/blt/nbt/nbt.h @@ -15,6 +15,7 @@ #include "blt/std/format.h" #include "blt/std/filesystem.h" +#include "blt/std/logging.h" namespace blt::nbt { #ifndef HASHMAP @@ -273,6 +274,8 @@ namespace blt::nbt { case nbt_tag::LONG_ARRAY: return new blt::nbt::tag_long_array; } + BLT_WARN("Tag Type not found!"); + return nullptr; } static HASHMAP toHashmap(const std::vector& v){ HASHMAP tags; diff --git a/include/blt/std/filesystem.h b/include/blt/std/filesystem.h index 9e6527b..f5e4a9a 100755 --- a/include/blt/std/filesystem.h +++ b/include/blt/std/filesystem.h @@ -78,7 +78,7 @@ namespace blt::fs { char* m_buffer; size_t readIndex = 0; public: - explicit fstream_block_reader(std::fstream& stream, size_t bufferSize): + explicit fstream_block_reader(std::fstream& stream, size_t bufferSize = 131072): block_reader(bufferSize), m_stream(stream), m_buffer(new char[bufferSize]) {} explicit fstream_block_reader(fstream_block_reader& copy) = delete; @@ -101,8 +101,9 @@ namespace blt::fs { std::fstream& m_stream; char* m_buffer; size_t writeIndex = 0; + void flush_internal(); public: - explicit fstream_block_writer(std::fstream& stream, size_t bufferSize): + explicit fstream_block_writer(std::fstream& stream, size_t bufferSize = 131072): block_writer(bufferSize), m_stream(stream), m_buffer(new char[bufferSize]) {} explicit fstream_block_writer(fstream_block_writer& copy) = delete; @@ -114,9 +115,12 @@ namespace blt::fs { fstream_block_writer& operator=(const fstream_block_writer&& move) = delete; int write(char* buffer, size_t bytes) override; - void flush() override; + inline void flush() override { + flush_internal(); + } ~fstream_block_writer() { + flush_internal(); delete[] m_buffer; } }; diff --git a/include/blt/std/format.h b/include/blt/std/format.h index 5f0a18a..370c91a 100755 --- a/include/blt/std/format.h +++ b/include/blt/std/format.h @@ -86,8 +86,8 @@ namespace blt::string { chars.characters = new char[chars.size]; int count = 0; - chars.characters[count++] = (char) ((utflen >> 0) & 0xFF); chars.characters[count++] = (char) ((utflen >> 8) & 0xFF); + chars.characters[count++] = (char) ((utflen >> 0) & 0xFF); unsigned int i = 0; for (i = 0; i < strlen; i++) { // optimized for initial run of ASCII diff --git a/src/blt/nbt/nbt.cpp b/src/blt/nbt/nbt.cpp index 3787b39..da3b561 100755 --- a/src/blt/nbt/nbt.cpp +++ b/src/blt/nbt/nbt.cpp @@ -16,9 +16,9 @@ namespace blt::nbt { } std::string readUTF8String(blt::fs::block_reader& stream) { - unsigned short utflen; + int16_t utflen; - stream.read(reinterpret_cast(&utflen), sizeof(utflen)); + readData(stream, utflen); blt::string::utf8_string str{}; str.size = utflen; diff --git a/src/blt/std/filesystem.cpp b/src/blt/std/filesystem.cpp index 4384457..f854ca3 100755 --- a/src/blt/std/filesystem.cpp +++ b/src/blt/std/filesystem.cpp @@ -40,7 +40,7 @@ int blt::fs::fstream_block_writer::write(char* buffer, size_t bytes) { return 0; } -void blt::fs::fstream_block_writer::flush() { +void blt::fs::fstream_block_writer::flush_internal() { m_stream.write(m_buffer, (long) writeIndex); writeIndex = 0; } diff --git a/src/tests/main.cpp b/src/tests/main.cpp index 93bffb6..7694441 100755 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -78,7 +78,7 @@ int main(int argc, char** argv) { auto* funy = new class_func; super_func* virtual_funy = new class_func; - for (int _ = 0; _ < 100; _++ ) { + for (int _ = 0; _ < 10; _++ ) { int num_of_tests = 10000; int acc = 1; BLT_START_INTERVAL("Functions Test", "std::function (lambda)"); @@ -222,5 +222,13 @@ int main(int argc, char** argv) { BLT_INFO("STDDEV of # random values: %f", stdev); + std::fstream nbtFile("super_file.nbt", std::ios::out | std::ios::binary); + blt::fs::fstream_block_writer blockWriter(nbtFile); + blt::nbt::NBTWriter nbtWriter(blockWriter); + nbtWriter.write(new blt::nbt::tag_compound("root", { + new blt::nbt::tag_byte("super_byte", 8), + new blt::nbt::tag_short("shortTest", 32767), + })); + return 0; } \ No newline at end of file