diff --git a/src/nbt/IntTag.cpp b/src/nbt/IntTag.cpp index 6d45611..b43e507 100644 --- a/src/nbt/IntTag.cpp +++ b/src/nbt/IntTag.cpp @@ -54,7 +54,7 @@ namespace nbt void IntTag::readStringifiedValue(StringReader &reader) { - float val = stof(reader.readNumber()); + float val = stoi(reader.readNumber()); value = val; if(reader.peek() == 'i') diff --git a/src/nbt/LongArrayTag.cpp b/src/nbt/LongArrayTag.cpp new file mode 100644 index 0000000..aea5b2b --- /dev/null +++ b/src/nbt/LongArrayTag.cpp @@ -0,0 +1,104 @@ +#include "LongArrayTag.h" +#include +#include +#include +#include + +namespace nbt +{ + + LongArrayTag::LongArrayTag() : Tag() + { + // Default constructor + } + + LongArrayTag::LongArrayTag(const std::vector &value) : Tag(), value(value) + { + // Constructor with initial value + } + + void LongArrayTag::readValue(ByteLayer &data) + { + int len = data.readInt(); + value.clear(); + for (int i = 0; i < len; i++) + { + value.push_back(data.readLong()); + } + } + + void LongArrayTag::writeValue(ByteLayer &data) const + { + data.writeInt(static_cast(value.size())); + for (int i : value) + { + data.writeLong(i); + } + } + + TagType LongArrayTag::getTagType() const + { + return TagType::LongArray; + } + + dynamic LongArrayTag::getValue() const + { + return dynamic(value); + } + + void LongArrayTag::setValue(const dynamic &val) + { + value = val.get>(); + } + + void LongArrayTag::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 LongArrayTag::writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const + { + builder << (isList ? std::string(indent, '\t') : "") << "[L; "; + for (size_t i = 0; i < value.size(); i++) + { + builder << value[i] << "L"; + if (i < value.size() - 1) + { + builder << ", "; + } + } + builder << "]"; + } + + void LongArrayTag::readStringifiedValue(StringReader &reader) + { + std::string temp; + reader.expect('['); + reader.expect('L'); + reader.expect(';'); + value.clear(); + + while (reader.peek() != ']') + { + uint8_t num = stol(reader.readNumber()); + value.push_back(num); + + reader.expect('l'); + + if (reader.peek() == ',') + reader.next(); + } + reader.expect(']'); + } + +} \ No newline at end of file diff --git a/src/nbt/LongArrayTag.h b/src/nbt/LongArrayTag.h new file mode 100644 index 0000000..5e5c67d --- /dev/null +++ b/src/nbt/LongArrayTag.h @@ -0,0 +1,40 @@ +#ifndef LONGARRAYTAG_H +#define LONGARRAYTAG_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 LongArrayTag : public Tag + { + public: + LongArrayTag(); + LongArrayTag(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 // LONGARRAYTAG_H diff --git a/src/nbt/LongTag.cpp b/src/nbt/LongTag.cpp new file mode 100644 index 0000000..2aebe54 --- /dev/null +++ b/src/nbt/LongTag.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include "LongTag.h" + +using namespace libac; +using namespace std; + +namespace nbt +{ + + LongTag::LongTag() : Tag(), value(0.0) + { + } + + LongTag::LongTag(long val) : Tag(), value(val) + { + } + + void LongTag::readValue(ByteLayer &data) + { + value = data.readLong(); + } + + void LongTag::writeValue(ByteLayer &data) const + { + data.writeLong(value); + } + + TagType LongTag::getTagType() const + { + return TagType::Long; + } + + dynamic LongTag::getValue() const + { + return dynamic(value); + } + + void LongTag::setValue(const dynamic &val) + { + value = val.get(); + } + + void LongTag::prettyPrint(int indent, bool recurse) const + { + cout << setw(indent) << setfill('\t') << Tag::getCanonicalName(getTagType()) << ": " << value; + } + + void LongTag::writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const + { + builder << (isList ? std::string(indent, '\t') : "") << value << "l"; + } + + void LongTag::readStringifiedValue(StringReader &reader) + { + float val = stol(reader.readNumber()); + value = val; + + reader.expect('l'); + } +} \ No newline at end of file diff --git a/src/nbt/LongTag.h b/src/nbt/LongTag.h new file mode 100644 index 0000000..4a3d4fe --- /dev/null +++ b/src/nbt/LongTag.h @@ -0,0 +1,34 @@ +#ifndef LONG_TAG_H +#define LONG_TAG_H + +#include "Tag.h" +#include "../utils/ByteLayer.h" +#include "../utils/StringBuilder.h" +#include "../utils/StringReader.h" +#include "../types/dynamic.h" + +namespace nbt +{ + class LongTag : public Tag + { + public: + LongTag(); + LongTag(long val); + ~LongTag(); + + // 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: + long value; + }; +} + +#endif \ No newline at end of file diff --git a/src/nbt/Tag.cpp b/src/nbt/Tag.cpp index 7724236..ca578bd 100644 --- a/src/nbt/Tag.cpp +++ b/src/nbt/Tag.cpp @@ -7,6 +7,9 @@ #include "CompoundTag.h" #include "IntArrayTag.h" #include "IntTag.h" +#include "ListTag.h" +#include "LongArrayTag.h" +#include "LongTag.h" #include #include @@ -40,6 +43,12 @@ namespace nbt return new IntArrayTag(); case TagType::Int: return new IntTag(); + case TagType::List: + return new ListTag(); + case TagType::LongArray: + return new LongArrayTag(); + case TagType::Long: + return new LongTag(); default: // default handling return nullptr; diff --git a/src/utils/StringBuilder.cpp b/src/utils/StringBuilder.cpp index c9c76d0..658d068 100644 --- a/src/utils/StringBuilder.cpp +++ b/src/utils/StringBuilder.cpp @@ -62,4 +62,12 @@ namespace libac append(oss.str()); return *this; } + + // Overload operator<< for longs + StringBuilder& StringBuilder::operator<<(long value) { + std::stringstream oss; + oss << value; + append(oss.str()); + return *this; + } } diff --git a/src/utils/StringBuilder.h b/src/utils/StringBuilder.h index e362828..7b823d8 100644 --- a/src/utils/StringBuilder.h +++ b/src/utils/StringBuilder.h @@ -23,6 +23,7 @@ namespace libac StringBuilder& operator<<(char value); StringBuilder& operator<<(int value); StringBuilder& operator<<(double value); + StringBuilder& operator<<(long value); private: std::string buffer; };