Botcraft 26.3
Loading...
Searching...
No Matches
NBT.cpp
Go to the documentation of this file.
1#include <iterator>
2#include <fstream>
3#include <utility>
4
7
8namespace ProtocolCraft
9{
10 namespace NBT
11 {
16
17 UnnamedValue::UnnamedValue(const Value& named) : Tag(named)
18 {
19
20 }
21
22 UnnamedValue::UnnamedValue(Value&& named) : Tag(std::move(named))
23 {
24
25 }
26
31
32 void UnnamedValue::ReadImpl(ReadIterator& iter, size_t& length)
33 {
34#if PROTOCOL_VERSION < 764 /* < 1.20.2 */
35 // Name is read but ignored
36 Tag::ReadNamed(iter, length);
37#else
38 Tag::ReadImpl(iter, length);
39#endif
40 }
41
43 {
44#if PROTOCOL_VERSION < 764 /* < 1.20.2 */
45 // Always empty string is written as this is unnamed
46 Tag::WriteNamed("", container);
47#else
48 Tag::WriteImpl(container);
49#endif
50 }
51
52
53#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
58
60 {
61
62 }
63
65 {
66
67 }
68
73
74 void UntrustedValue::ReadImpl(ReadIterator& iter, size_t& length)
75 {
76 // Untrusted == size prefixed
77 const int size = ReadData<VarInt>(iter, length);
78 Tag::ReadImpl(iter, length);
79 }
80
82 {
83 // Untrusted == size prefixed
84 std::vector<unsigned char> temp_container;
85 Tag::WriteImpl(temp_container);
86 WriteData<VarInt>(static_cast<int>(temp_container.size()), container);
87 WriteByteArray(temp_container, container);
88 }
89#endif
90
91
93 {
94
95 }
96
97 Value::Value(TagCompound c, std::string root_name) : Tag(std::move(c)), name(std::move(root_name))
98 {
99
100 }
101
102 Value::Value(const UnnamedValue& unnamed) : Tag(unnamed)
103 {
104
105 }
106
107 Value::Value(UnnamedValue&& unnamed) : Tag(std::move(unnamed))
108 {
109
110 }
111
112#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
113 Value::Value(const UntrustedValue& untrusted) : Tag(untrusted)
114 {
115
116 }
117
118 Value::Value(UntrustedValue&& untrusted) : Tag(std::move(untrusted))
119 {
120
121 }
122#endif
123
125 {
126
127 }
128
129 bool Value::HasData() const
130 {
131 return !is<TagEnd>();
132 }
133
134 const std::string& Value::GetName() const
135 {
136 return name;
137 }
138
139 std::istream& operator>>(std::istream& is, Value& v)
140 {
141 std::vector<unsigned char> file_content = std::vector<unsigned char>(
142 std::istream_iterator<unsigned char>(is),
143 std::istream_iterator<unsigned char>()
144 );
145
146 ReadIterator iter = file_content.begin();
147 size_t length = file_content.size();
148
149 v = ReadData<Value>(iter, length);
150
151 return is;
152 }
153
154 void Value::ReadImpl(ReadIterator& iter, size_t& length)
155 {
156 // Check for GZIP minimal header size and magic number
157 if (length > 10 && *iter == 0x1F && *(iter + 1) == 0x8B)
158 {
159 const std::vector<unsigned char> decompressed = ExtractGZip(iter, length);
160 ReadIterator decomp_iter = decompressed.begin();
161 size_t decomp_length = decompressed.size();
162 name = Tag::ReadNamed(decomp_iter, decomp_length);
163 }
164 else
165 {
166 name = Tag::ReadNamed(iter, length);
167 }
168
169 if (is<TagEnd>())
170 {
171 return;
172 }
173
174 if (!is<TagCompound>())
175 {
176 throw std::runtime_error("Error reading NBT value, not starting with compound");
177 }
178 }
179
180 void Value::WriteImpl(WriteContainer& container) const
181 {
182 Tag::WriteNamed(name, container);
183 }
184 }
185}
size_t size() const
Definition Tag.cpp:94
virtual void WriteImpl(WriteContainer &container) const override
Definition Tag.cpp:133
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
Definition Tag.cpp:128
void WriteNamed(const std::string &tag_name, WriteContainer &container) const
Definition Tag.cpp:147
std::string ReadNamed(ReadIterator &iter, size_t &length)
Definition Tag.cpp:139
virtual ~UnnamedValue() override
Definition NBT.cpp:27
virtual void WriteImpl(WriteContainer &container) const override
Definition NBT.cpp:42
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
Definition NBT.cpp:32
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
Definition NBT.cpp:74
virtual ~UntrustedValue() override
Definition NBT.cpp:69
virtual void WriteImpl(WriteContainer &container) const override
Definition NBT.cpp:81
virtual ~Value() override
Definition NBT.cpp:124
virtual void WriteImpl(WriteContainer &container) const override
Definition NBT.cpp:180
const std::string & GetName() const
Definition NBT.cpp:134
bool HasData() const
Definition NBT.cpp:129
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
Definition NBT.cpp:154
std::istream & operator>>(std::istream &is, Value &v)
Definition NBT.cpp:139
std::vector< unsigned char > ExtractGZip(std::vector< unsigned char >::const_iterator &iter, std::size_t &length)
std::vector< unsigned char > WriteContainer
std::vector< unsigned char >::const_iterator ReadIterator
void WriteByteArray(const std::vector< unsigned char > &my_array, WriteContainer &container)
STL namespace.