From 422f428fce68327d9b2a70174d71a11aebe616b8 Mon Sep 17 00:00:00 2001 From: zontreck Date: Wed, 31 Jul 2024 00:22:12 -0700 Subject: [PATCH] Add short tag --- src/nbt/IntTag.cpp | 2 +- src/nbt/LongTag.cpp | 2 +- src/nbt/ShortTag.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++ src/nbt/ShortTag.h | 34 ++++++++++++++++++++++++ src/nbt/Tag.cpp | 3 +++ 5 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 src/nbt/ShortTag.cpp create mode 100644 src/nbt/ShortTag.h diff --git a/src/nbt/IntTag.cpp b/src/nbt/IntTag.cpp index b43e507..aaf6842 100644 --- a/src/nbt/IntTag.cpp +++ b/src/nbt/IntTag.cpp @@ -9,7 +9,7 @@ using namespace std; namespace nbt { - IntTag::IntTag() : Tag(), value(0.0) + IntTag::IntTag() : Tag(), value(0) { } diff --git a/src/nbt/LongTag.cpp b/src/nbt/LongTag.cpp index 2aebe54..39a4b5c 100644 --- a/src/nbt/LongTag.cpp +++ b/src/nbt/LongTag.cpp @@ -9,7 +9,7 @@ using namespace std; namespace nbt { - LongTag::LongTag() : Tag(), value(0.0) + LongTag::LongTag() : Tag(), value(0) { } diff --git a/src/nbt/ShortTag.cpp b/src/nbt/ShortTag.cpp new file mode 100644 index 0000000..0c8ca2f --- /dev/null +++ b/src/nbt/ShortTag.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include "ShortTag.h" + +using namespace libac; +using namespace std; + +namespace nbt +{ + + ShortTag::ShortTag() : Tag(), value(0) + { + } + + ShortTag::ShortTag(short val) : Tag(), value(val) + { + } + + void ShortTag::readValue(ByteLayer &data) + { + value = data.readShort(); + } + + void ShortTag::writeValue(ByteLayer &data) const + { + data.writeShort(value); + } + + TagType ShortTag::getTagType() const + { + return TagType::Short; + } + + dynamic ShortTag::getValue() const + { + return dynamic(value); + } + + void ShortTag::setValue(const dynamic &val) + { + value = val.get(); + } + + void ShortTag::prettyPrint(int indent, bool recurse) const + { + cout << setw(indent) << setfill('\t') << Tag::getCanonicalName(getTagType()) << ": " << value; + } + + void ShortTag::writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const + { + builder << (isList ? std::string(indent, '\t') : "") << value << "l"; + } + + void ShortTag::readStringifiedValue(StringReader &reader) + { + float val = stoi(reader.readNumber()); + value = val; + + reader.expect('s'); + } +} \ No newline at end of file diff --git a/src/nbt/ShortTag.h b/src/nbt/ShortTag.h new file mode 100644 index 0000000..267c057 --- /dev/null +++ b/src/nbt/ShortTag.h @@ -0,0 +1,34 @@ +#ifndef SHORT_TAG_H +#define SHORT_TAG_H + +#include "Tag.h" +#include "../utils/ByteLayer.h" +#include "../utils/StringBuilder.h" +#include "../utils/StringReader.h" +#include "../types/dynamic.h" + +namespace nbt +{ + class ShortTag : public Tag + { + public: + ShortTag(); + ShortTag(short val); + ~ShortTag(); + + // 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: + short value; + }; +} + +#endif \ No newline at end of file diff --git a/src/nbt/Tag.cpp b/src/nbt/Tag.cpp index ca578bd..b6a99a8 100644 --- a/src/nbt/Tag.cpp +++ b/src/nbt/Tag.cpp @@ -10,6 +10,7 @@ #include "ListTag.h" #include "LongArrayTag.h" #include "LongTag.h" +#include "ShortTag.h" #include #include @@ -49,6 +50,8 @@ namespace nbt return new LongArrayTag(); case TagType::Long: return new LongTag(); + case TagType::Short: + return new ShortTag(); default: // default handling return nullptr;