7 using namespace Internal;
17 if constexpr (std::is_same_v<T, TagEnd>)
21 else if constexpr (std::is_same_v<T, TagByte>)
25 else if constexpr (std::is_same_v<T, TagShort>)
29 else if constexpr (std::is_same_v<T, TagInt>)
33 else if constexpr (std::is_same_v<T, TagLong>)
37 else if constexpr (std::is_same_v<T, TagFloat>)
41 else if constexpr (std::is_same_v<T, TagDouble>)
45 else if constexpr (std::is_same_v<T, TagByteArray>)
47 return "TagByteArray";
49 else if constexpr (std::is_same_v<T, TagString>)
53 else if constexpr (std::is_same_v<T, TagList>)
57 else if constexpr (std::is_same_v<T, TagCompound>)
61 else if constexpr (std::is_same_v<T, TagIntArray>)
65 else if constexpr (std::is_same_v<T, TagLongArray>)
67 return "TagLongArray";
78 throw std::runtime_error(
"NBT::Tag is not a TagCompound");
81 return get<TagCompound>().at(s);
91 return get<TagCompound>()[s];
98 std::visit([&](
auto&& arg) {
99 using T = std::decay_t<
decltype(arg)>;
101 if constexpr (std::is_same_v<T, RecursiveWrapper<TagList>> || std::is_same_v<T, RecursiveWrapper<TagCompound>>)
103 output = arg.get().size();
106 std::is_same_v<T, TagByteArray> ||
107 std::is_same_v<T, TagIntArray> ||
108 std::is_same_v<T, TagLongArray>
115 throw std::runtime_error(
"NBT::Tag is not a container, no size() method implemented");
124 return is<TagCompound>() && get<TagCompound>().count(s);
130 ReadPayload(ReadData<TagType, char>(iter, length), iter, length);
135 WriteData<char>(
static_cast<char>(
val.index()), container);
141 const TagType type = ReadData<TagType, char>(iter, length);
149 WriteData<char>(
static_cast<char>(
val.index()), container);
150 if (!std::holds_alternative<TagEnd>(
val))
161 std::visit([&](
auto&& arg) {
162 using T = std::decay_t<
decltype(arg)>;
164 if constexpr (std::is_same_v<T, TagEnd>)
166 output[
"type"] = GetTagName<T>();
168 else if constexpr (std::is_same_v<T, RecursiveWrapper<TagList>>)
170 output[
"type"] = GetTagName<TagList>();
171 output[
"content"] = arg.
get();
173 else if constexpr (std::is_same_v<T, RecursiveWrapper<TagCompound>>)
175 output[
"type"] = GetTagName<TagCompound>();
176 output[
"content"] = arg.
get();
180 output[
"type"] = GetTagName<T>();
181 output[
"content"] = arg;
196 val = ReadData<char>(iter, length);
199 val = ReadData<short>(iter, length);
202 val = ReadData<int>(iter, length);
205 val = ReadData<long long int>(iter, length);
208 val = ReadData<float>(iter, length);
211 val = ReadData<double>(iter, length);
215 val = ReadData<Vector<char, int>>(iter, length);
221 val = ReadData<TagList>(iter, length);
224 val = ReadData<TagCompound>(iter, length);
228 val = ReadData<Vector<int, int>>(iter, length);
232 val = ReadData<Vector<long long int, int>>(iter, length);
236 throw std::runtime_error(
"Unknown tag type " + std::to_string(
static_cast<int>(type)));
243 std::visit([&](
auto&& arg) {
244 using T = std::decay_t<
decltype(arg)>;
246 if constexpr (std::is_same_v<T, TagEnd>)
250 else if constexpr (std::is_same_v<T, TagByteArray>)
253 WriteData<Vector<char, int>>(arg, container);
255 else if constexpr (std::is_same_v<T, TagString>)
259 else if constexpr (std::is_same_v<T, RecursiveWrapper<TagList>>)
261 WriteData<TagList>(arg.get(), container);
263 else if constexpr (std::is_same_v<T, RecursiveWrapper<TagCompound>>)
265 WriteData<TagCompound>(arg.get(), container);
267 else if constexpr (std::is_same_v<T, TagIntArray>)
270 WriteData<Vector<int, int>>(arg, container);
272 else if constexpr (std::is_same_v<T, TagLongArray>)
275 WriteData<Vector<long long int, int>>(arg, container);
279 WriteData<T>(arg, container);
287 const TagType tags_type =
static_cast<TagType>(ReadData<char>(iter, length));
293 const int vector_size = ReadData<int>(iter, length);
294 vals = std::vector<TagEnd>(vector_size);
299 vals = ReadData<Vector<char, int>>(iter, length);
303 vals = ReadData<Vector<short, int>>(iter, length);
307 vals = ReadData<Vector<int, int>>(iter, length);
311 vals = ReadData<Vector<long long int, int>>(iter, length);
315 vals = ReadData<Vector<float, int>>(iter, length);
319 vals = ReadData<Vector<double, int>>(iter, length);
323 vals = ReadData<Vector<Vector<char, int>,
int>>(iter, length);
327 std::vector<std::string> values = std::vector<std::string>(ReadData<int>(iter, length));
328 for (
size_t i = 0; i < values.size(); ++i)
337 vals = ReadData<Vector<TagList, int>>(iter, length);
341 vals = ReadData<Vector<TagCompound, int>>(iter, length);
345 vals = ReadData<Vector<Vector<int, int>,
int>>(iter, length);
349 vals = ReadData<Vector<Vector<long long int, int>,
int>>(iter, length);
353 throw std::runtime_error(
"Unknown tag type " + std::to_string(
static_cast<int>(tags_type)));
360 WriteData<char>(
static_cast<char>(
vals.index()), container);
362 std::visit([&](
auto&& arg) {
363 using T = std::decay_t<
decltype(arg)>;
365 if constexpr (std::is_same_v<T, std::vector<TagEnd>>)
367 WriteData<int>(
static_cast<int>(arg.size()), container);
369 else if constexpr (std::is_same_v<T, std::vector<TagByteArray>>)
372 WriteData<Vector<Vector<char, int>,
int>>(arg, container);
374 else if constexpr (std::is_same_v<T, std::vector<TagString>>)
376 WriteData<int>(
static_cast<int>(arg.size()), container);
377 for (
size_t i = 0; i < arg.size(); ++i)
382 else if constexpr (std::is_same_v<T, std::vector<TagIntArray>>)
385 WriteData<Vector<Vector<int, int>,
int>>(arg, container);
387 else if constexpr (std::is_same_v<T, std::vector<TagLongArray>>)
390 WriteData<Vector<Vector<long long int, int>,
int>>(arg, container);
395 WriteData<Vector<typename T::value_type, int>>(arg, container);
404 std::visit([&](
auto&& arg) {
405 using T = std::decay_t<
decltype(arg)>;
407 if constexpr (std::is_same_v<T, std::vector<TagEnd>>)
410 for (
size_t i = 0; i < arg.size(); ++i)
428 std::visit([&](
auto&& arg)
452 std::string name = tag.
ReadNamed(iter, length);
461 emplace(std::move(name), std::move(tag));
468 for (
const auto& [name, tag] : *
this)
475 tag.WriteNamed(name, container);
485 return *
static_cast<const std::map<std::string, Tag>*
>(
this);
491 const unsigned short size = ReadData<unsigned short>(iter, length);
498 WriteData<unsigned short>(
static_cast<unsigned short>(s.size()), container);
Template magic to have a full type instead of an incomplete one as required for example by std::varia...
Real class declaration, just a derived class of std::vector<Value>
Main class, basically a JsonVariant with extra utility functions it doesn't inherit JsonVariant direc...
void push_back(const Value &value)
virtual Json::Value SerializeImpl() const override
virtual void WriteImpl(WriteContainer &container) const override
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
bool contains(const std::string &s) const
const Tag & operator[](const std::string &s) const
virtual Json::Value SerializeImpl() const override
virtual void WriteImpl(WriteContainer &container) const override
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
Internal::TagListVariant vals
bool contains(const std::string &s) const
void WritePayload(WriteContainer &container) const
virtual void WriteImpl(WriteContainer &container) const override
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
void WriteNamed(const std::string &tag_name, WriteContainer &container) const
virtual Json::Value SerializeImpl() const override
void ReadPayload(const TagType type, ReadIterator &iter, size_t &length)
const Tag & operator[](const std::string &s) const
std::string ReadNamed(ReadIterator &iter, size_t &length)
std::variant< TagEnd, TagByte, TagShort, TagInt, TagLong, TagFloat, TagDouble, TagByteArray, TagString, ProtocolCraft::Internal::RecursiveWrapper< TagList >, ProtocolCraft::Internal::RecursiveWrapper< TagCompound >, TagIntArray, TagLongArray > TagVariant
constexpr std::string_view GetTagName()
std::string ReadNBTString(ReadIterator &iter, size_t &length)
void WriteNBTString(const std::string &s, WriteContainer &container)
std::string ReadRawString(ReadIterator &iter, size_t &length, const size_t size)
void WriteRawString(const std::string &s, WriteContainer &container)
std::vector< unsigned char > WriteContainer
std::vector< unsigned char >::const_iterator ReadIterator