From 8a2282b2c27fadb87f0c86b52aca459d9af253bc Mon Sep 17 00:00:00 2001 From: zontreck Date: Tue, 30 Jul 2024 23:37:27 -0700 Subject: [PATCH] Fix compile errors in libnbt --- src/nbt/Accountant.cpp | 1 + src/nbt/Accountant.h | 4 ++ src/nbt/ByteArrayTag.h | 5 +- src/nbt/ByteTag.h | 1 + src/nbt/CompoundTag.cpp | 16 ++--- src/nbt/CompoundTag.h | 7 +- src/nbt/DoubleTag.h | 4 ++ src/nbt/EndTag.h | 3 + src/nbt/FloatTag.h | 4 ++ src/nbt/IntArrayTag.cpp | 104 ++++++++++++++++++++++++++++ src/nbt/IntArrayTag.h | 40 +++++++++++ src/nbt/IntTag.cpp | 63 +++++++++++++++++ src/nbt/IntTag.h | 34 +++++++++ src/nbt/ListTag.cpp | 149 ++++++++++++++++++++++++++++++++++++++++ src/nbt/ListTag.h | 43 ++++++++++++ src/nbt/Tag.cpp | 15 ++++ 16 files changed, 479 insertions(+), 14 deletions(-) create mode 100644 src/nbt/IntArrayTag.cpp create mode 100644 src/nbt/IntArrayTag.h create mode 100644 src/nbt/IntTag.cpp create mode 100644 src/nbt/IntTag.h create mode 100644 src/nbt/ListTag.cpp create mode 100644 src/nbt/ListTag.h diff --git a/src/nbt/Accountant.cpp b/src/nbt/Accountant.cpp index e0790dd..f22b0b6 100644 --- a/src/nbt/Accountant.cpp +++ b/src/nbt/Accountant.cpp @@ -1,6 +1,7 @@ #include "Accountant.h" #include "Tag.h" #include "CompoundTag.h" +#include "ListTag.h" #include using namespace nbt; diff --git a/src/nbt/Accountant.h b/src/nbt/Accountant.h index d93bcab..0091515 100644 --- a/src/nbt/Accountant.h +++ b/src/nbt/Accountant.h @@ -2,6 +2,10 @@ #define NBTACCOUNTANT_H #include "Tag.h" // Assuming this includes definitions for Tag, CompoundTag, and ListTag +#include "../utils/ByteLayer.h" +#include "../utils/StringBuilder.h" +#include "../utils/StringReader.h" +#include "../types/dynamic.h" namespace nbt { diff --git a/src/nbt/ByteArrayTag.h b/src/nbt/ByteArrayTag.h index 2b68b25..9bfae70 100644 --- a/src/nbt/ByteArrayTag.h +++ b/src/nbt/ByteArrayTag.h @@ -2,7 +2,10 @@ #define BYTEARRAYTAG_H #include "Tag.h" -#include "../utils/ByteLayer.h" // Assuming ByteLayer is a class you have +#include "../utils/ByteLayer.h" +#include "../utils/StringBuilder.h" +#include "../utils/StringReader.h" +#include "../types/dynamic.h" #include #include #include diff --git a/src/nbt/ByteTag.h b/src/nbt/ByteTag.h index 1e2153f..df18b27 100644 --- a/src/nbt/ByteTag.h +++ b/src/nbt/ByteTag.h @@ -6,6 +6,7 @@ #include "../utils/ByteLayer.h" #include "../utils/StringBuilder.h" #include "../utils/StringReader.h" +#include "../types/dynamic.h" using namespace libac; diff --git a/src/nbt/CompoundTag.cpp b/src/nbt/CompoundTag.cpp index d8fc1c8..fbce858 100644 --- a/src/nbt/CompoundTag.cpp +++ b/src/nbt/CompoundTag.cpp @@ -62,7 +62,7 @@ namespace nbt { } dynamic CompoundTag::getValue() const { - return dynamic(); // Returning a default dynamic instance + return new dynamic(0); // Returning a default dynamic instance } void CompoundTag::prettyPrint(int indent, bool recurse) const { @@ -88,23 +88,23 @@ namespace nbt { if (!firstEntry) { builder.append(",\n"); } - Tag::writeStringifiedNamedTag(tag, builder, indent); + Tag::writeStringifiedNamedTag(*tag, builder, indent); firstEntry = false; } builder.append("\n" + std::string(indent - 1, '\t') + "}"); } void CompoundTag::readStringifiedValue(StringReader& reader) { - reader.expect("{"); + reader.expect('{'); - while (reader.peek() != "}") { + while (reader.peek() != '}') { Tag* tag = Tag::readStringifiedNamedTag(reader); put(tag->getKey(), tag); - if (reader.peek() == ",") reader.next(); + if (reader.peek() == ',') reader.next(); } - reader.expect("}"); + reader.expect('}'); } Tag* CompoundTag::operator[](const std::string& key) const { @@ -112,10 +112,6 @@ namespace nbt { return (it != value.end()) ? it->second : nullptr; } - void CompoundTag::operator[]=(const std::string& key, Tag* tag) { - put(key, tag); - } - void CompoundTag::addAll(const std::map& other) { value.insert(other.begin(), other.end()); for (auto& [key, tag] : other) { diff --git a/src/nbt/CompoundTag.h b/src/nbt/CompoundTag.h index 5fcce5c..4dcf211 100644 --- a/src/nbt/CompoundTag.h +++ b/src/nbt/CompoundTag.h @@ -5,9 +5,10 @@ #include #include #include "Tag.h" -#include "ByteLayer.h" -#include "StringBuilder.h" -#include "StringReader.h" +#include "../utils/ByteLayer.h" +#include "../utils/StringBuilder.h" +#include "../utils/StringReader.h" +#include "../types/dynamic.h" namespace nbt { diff --git a/src/nbt/DoubleTag.h b/src/nbt/DoubleTag.h index 530285b..a3473be 100644 --- a/src/nbt/DoubleTag.h +++ b/src/nbt/DoubleTag.h @@ -2,6 +2,10 @@ #define DOUBLE_TAG_H #include "Tag.h" +#include "../utils/ByteLayer.h" +#include "../utils/StringBuilder.h" +#include "../utils/StringReader.h" +#include "../types/dynamic.h" namespace nbt { diff --git a/src/nbt/EndTag.h b/src/nbt/EndTag.h index ce595b8..c7e97ea 100644 --- a/src/nbt/EndTag.h +++ b/src/nbt/EndTag.h @@ -3,6 +3,9 @@ #include "Tag.h" #include "../utils/ByteLayer.h" +#include "../utils/StringBuilder.h" +#include "../utils/StringReader.h" +#include "../types/dynamic.h" using namespace std; diff --git a/src/nbt/FloatTag.h b/src/nbt/FloatTag.h index e15004e..aee3494 100644 --- a/src/nbt/FloatTag.h +++ b/src/nbt/FloatTag.h @@ -2,6 +2,10 @@ #define FLOAT_TAG_H #include "Tag.h" +#include "../utils/ByteLayer.h" +#include "../utils/StringBuilder.h" +#include "../utils/StringReader.h" +#include "../types/dynamic.h" namespace nbt { diff --git a/src/nbt/IntArrayTag.cpp b/src/nbt/IntArrayTag.cpp new file mode 100644 index 0000000..33919d5 --- /dev/null +++ b/src/nbt/IntArrayTag.cpp @@ -0,0 +1,104 @@ +#include "IntArrayTag.h" +#include +#include +#include +#include + +namespace nbt +{ + + IntArrayTag::IntArrayTag() : Tag() + { + // Default constructor + } + + IntArrayTag::IntArrayTag(const std::vector &value) : Tag(), value(value) + { + // Constructor with initial value + } + + void IntArrayTag::readValue(ByteLayer &data) + { + int len = data.readInt(); + value.clear(); + for (int i = 0; i < len; i++) + { + value.push_back(data.readInt()); + } + } + + void IntArrayTag::writeValue(ByteLayer &data) const + { + data.writeInt(static_cast(value.size())); + for (int i : value) + { + data.writeInt(i); + } + } + + TagType IntArrayTag::getTagType() const + { + return TagType::IntArray; + } + + dynamic IntArrayTag::getValue() const + { + return dynamic(value); + } + + void IntArrayTag::setValue(const dynamic &val) + { + value = val.get>(); + } + + void IntArrayTag::prettyPrint(int indent, bool recurse) const + { + std::cout << std::setw(indent) << std::setfill('\t') << "" + << Tag::getCanonicalName(getTagType()) << ": ["; + for (size_t i = 0; i < value.size(); i++) + { + std::cout << value[i]; + if (i < value.size() - 1) + { + std::cout << ", "; + } + } + std::cout << "]"; + } + + void IntArrayTag::writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const + { + builder << (isList ? std::string(indent, '\t') : "") << "[I; "; + for (size_t i = 0; i < value.size(); i++) + { + builder << value[i] << "I"; + if (i < value.size() - 1) + { + builder << ", "; + } + } + builder << "]"; + } + + void IntArrayTag::readStringifiedValue(StringReader &reader) + { + std::string temp; + reader.expect('['); + reader.expect('I'); + reader.expect(';'); + value.clear(); + + while (reader.peek() != ']') + { + uint8_t num = stoi(reader.readNumber()); + value.push_back(num); + + reader.expect('I'); + + if (reader.peek() == ',') + reader.next(); + } + reader.expect(']'); + } + +} \ No newline at end of file diff --git a/src/nbt/IntArrayTag.h b/src/nbt/IntArrayTag.h new file mode 100644 index 0000000..327cd91 --- /dev/null +++ b/src/nbt/IntArrayTag.h @@ -0,0 +1,40 @@ +#ifndef INTARRAYTAG_H +#define INTARRAYTAG_H + +#include "Tag.h" +#include "../utils/ByteLayer.h" +#include "../utils/StringBuilder.h" +#include "../utils/StringReader.h" +#include "../types/dynamic.h" +#include +#include +#include + +using namespace libac; +using namespace std; + +namespace nbt +{ + + class IntArrayTag : public Tag + { + public: + IntArrayTag(); + IntArrayTag(const std::vector &value); + + // Override functions from the Tag class + void readValue(ByteLayer &data) override; + void writeValue(ByteLayer &data) const override; + TagType getTagType() const override; + dynamic getValue() const; + void setValue(const dynamic &val) override; + void prettyPrint(int indent, bool recurse) const override; + void writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const override; + void readStringifiedValue(StringReader &reader) override; + + private: + vector value; + }; +} + +#endif // INTARRAYTAG_H diff --git a/src/nbt/IntTag.cpp b/src/nbt/IntTag.cpp new file mode 100644 index 0000000..6d45611 --- /dev/null +++ b/src/nbt/IntTag.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include "IntTag.h" + +using namespace libac; +using namespace std; + +namespace nbt +{ + + IntTag::IntTag() : Tag(), value(0.0) + { + } + + IntTag::IntTag(int val) : Tag(), value(val) + { + } + + void IntTag::readValue(ByteLayer &data) + { + value = data.readInt(); + } + + void IntTag::writeValue(ByteLayer &data) const + { + data.writeInt(value); + } + + TagType IntTag::getTagType() const + { + return TagType::Int; + } + + dynamic IntTag::getValue() const + { + return dynamic(value); + } + + void IntTag::setValue(const dynamic &val) + { + value = val.get(); + } + + void IntTag::prettyPrint(int indent, bool recurse) const + { + cout << setw(indent) << setfill('\t') << Tag::getCanonicalName(getTagType()) << ": " << value; + } + + void IntTag::writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const + { + builder << (isList ? std::string(indent, '\t') : "") << value << "i"; + } + + void IntTag::readStringifiedValue(StringReader &reader) + { + float val = stof(reader.readNumber()); + value = val; + + if(reader.peek() == 'i') + reader.expect('i'); + } +} \ No newline at end of file diff --git a/src/nbt/IntTag.h b/src/nbt/IntTag.h new file mode 100644 index 0000000..3197a38 --- /dev/null +++ b/src/nbt/IntTag.h @@ -0,0 +1,34 @@ +#ifndef INT_TAG_H +#define INT_TAG_H + +#include "Tag.h" +#include "../utils/ByteLayer.h" +#include "../utils/StringBuilder.h" +#include "../utils/StringReader.h" +#include "../types/dynamic.h" + +namespace nbt +{ + class IntTag : public Tag + { + public: + IntTag(); + IntTag(int val); + ~IntTag(); + + // Function overrides + void readValue(ByteLayer &data) override; + void writeValue(ByteLayer &data) const override; + TagType getTagType() const override; + dynamic getValue() const override; + void setValue(const dynamic &val) override; + void prettyPrint(int indent, bool recurse) const override; + void writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const override; + void readStringifiedValue(StringReader &reader) override; + + private: + int value; + }; +} + +#endif \ No newline at end of file diff --git a/src/nbt/ListTag.cpp b/src/nbt/ListTag.cpp new file mode 100644 index 0000000..e22259b --- /dev/null +++ b/src/nbt/ListTag.cpp @@ -0,0 +1,149 @@ +#include "ListTag.h" +#include "Tag.h" +#include +#include +#include + +using namespace std; + +namespace nbt { + + ListTag::ListTag() : Tag() { + // Constructor implementation if needed + } + + TagType ListTag::getListTagType() const { + if (value.empty()) { + return TagType::End; + } + return value[0]->getTagType(); + } + + void ListTag::readValue(ByteLayer &data) { + TagType type = (TagType) data.readByte(); + + int size = data.readInt(); + + for (int i = 0; i < size; ++i) { + Tag* tag = Tag::makeTagOfType(type); + tag->readValue(data); + add(tag); + } + } + + void ListTag::writeValue(ByteLayer &data) const { + TagType type = TagType::End; + if (!value.empty()) { + type = value[0]->getTagType(); + } + + data.writeByte(static_cast(type)); + data.writeInt(static_cast(value.size())); + for (const auto& tag : value) { + tag->writeValue(data); + } + } + + void ListTag::add(Tag* tag) { + TagType type = TagType::End; + if (!value.empty()) { + type = value[0]->getTagType(); + } + if (type == TagType::End || type == tag->getTagType()) { + value.push_back(tag); + tag->updateParent(this); + } + } + + Tag* ListTag::get(int index) const { + if (index >= 0 && index < static_cast(value.size())) { + return value[index]; + } else { + return new EndTag(); // Return a new EndTag instance if out of range + } + } + + void ListTag::remove(Tag* tag) { + auto it = std::find(value.begin(), value.end(), tag); + if (it != value.end()) { + (*it)->updateParent(nullptr); + value.erase(it); + } + } + + void ListTag::removeAt(int index) { + if (index >= 0 && index < static_cast(value.size())) { + value[index]->updateParent(nullptr); + value.erase(value.begin() + index); + } + } + + int ListTag::indexOf(Tag* tag) const { + auto it = find(value.begin(), value.end(), tag); + return it != value.end() ? std::distance(value.begin(), it) : -1; + } + + TagType ListTag::getTagType() const { + return TagType::List; + } + + void ListTag::setValue(const dynamic &val) { + // Implementation if needed + } + + dynamic ListTag::getValue() const { + return new dynamic(0); // Return a default dynamic instance if needed + } + + int ListTag::size() const { + return static_cast(value.size()); + } + + void ListTag::clear() { + for (auto& tag : value) { + tag->updateParent(nullptr); + tag->setParentTagType(TagType::End); + } + value.clear(); + } + + void ListTag::prettyPrint(int indent, bool recurse) const { + std::string indentStr(indent, '\t'); + std::cout << indentStr << Tag::getCanonicalName(getTagType()) << ": [" << value.size() << " entries]" << std::endl; + std::cout << indentStr << "[" << std::endl; + for (const auto& tag : value) { + tag->prettyPrint(indent + 1, true); + } + } + + void ListTag::endPrettyPrint(int indent) const { + std::cout << std::string(indent, '\t') << "]" << std::endl; + } + + void ListTag::writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const { + builder.append(std::string(isList ? indent - 1 : 0, '\t') + "[\n"); + bool firstTag = true; + for (const auto& tag : value) { + if (!firstTag) { + builder.append(",\n"); + } + firstTag = false; + tag->writeStringifiedValue(builder, indent + 1, true); + } + builder.append("\n" + std::string(indent - 1, '\t') + "]"); + } + + void ListTag::readStringifiedValue(StringReader &reader) { + reader.expect('['); + while (reader.peek() != ']') { + TagType type = Tag::getStringifiedTagType(reader); + Tag* newTag = Tag::makeTagOfType(type); + newTag->readStringifiedValue(reader); + add(newTag); + + if (reader.peek() == ',') reader.next(); + } + reader.expect(']'); + } + +} diff --git a/src/nbt/ListTag.h b/src/nbt/ListTag.h new file mode 100644 index 0000000..a27f359 --- /dev/null +++ b/src/nbt/ListTag.h @@ -0,0 +1,43 @@ +#ifndef LISTTAG_H +#define LISTTAG_H + +#include +#include +#include "Tag.h" +#include "../utils/ByteLayer.h" +#include "../utils/StringBuilder.h" +#include "../utils/StringReader.h" +#include "../types/dynamic.h" +#include "EndTag.h" + +namespace nbt { + + class ListTag : public Tag { + public: + ListTag(); + + TagType getListTagType() const; + void readValue(ByteLayer &data) override; + void writeValue(ByteLayer &data) const override; + void add(Tag* tag); + Tag* get(int index) const; + void remove(Tag* tag); + void removeAt(int index); + int indexOf(Tag* tag) const; + TagType getTagType() const override; + void setValue(const dynamic &val) override; + dynamic getValue() const override; + int size() const; + void clear(); + void prettyPrint(int indent, bool recurse) const override; + void endPrettyPrint(int indent) const; + void writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const override; + void readStringifiedValue(StringReader &reader) override; + + private: + std::vector value; + }; + +} + +#endif // LISTTAG_H diff --git a/src/nbt/Tag.cpp b/src/nbt/Tag.cpp index 13b0efa..7724236 100644 --- a/src/nbt/Tag.cpp +++ b/src/nbt/Tag.cpp @@ -2,6 +2,11 @@ #include "EndTag.h" #include "ByteTag.h" #include "ByteArrayTag.h" +#include "DoubleTag.h" +#include "FloatTag.h" +#include "CompoundTag.h" +#include "IntArrayTag.h" +#include "IntTag.h" #include #include @@ -25,6 +30,16 @@ namespace nbt return new ByteTag(); case TagType::ByteArray: return new ByteArrayTag(); + case TagType::Double: + return new DoubleTag(); + case TagType::Float: + return new FloatTag(); + case TagType::Compound: + return new CompoundTag(); + case TagType::IntArray: + return new IntArrayTag(); + case TagType::Int: + return new IntTag(); default: // default handling return nullptr;