forked from minetest/minetestmapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockDecoder.cpp
235 lines (202 loc) · 5.27 KB
/
BlockDecoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <stdint.h>
#include <string.h>
#include <string>
#include <iostream>
#include <sstream>
#include "BlockDecoder.h"
#include "ZlibDecompressor.h"
static inline uint16_t readU16(const unsigned char *data)
{
return data[0] << 8 | data[1];
}
static inline uint32_t readU32(const unsigned char *data)
{
return data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
}
static int readBlockContent(const unsigned char *mapData, u8 version, unsigned int datapos)
{
if (version >= 24) {
size_t index = datapos << 1;
return (mapData[index] << 8) | mapData[index + 1];
} else if (version >= 20) {
if (mapData[datapos] <= 0x80)
return mapData[datapos];
else
return (int(mapData[datapos]) << 4) | (int(mapData[datapos + 0x2000]) >> 4);
}
std::ostringstream oss;
oss << "Unsupported map version " << version;
throw std::runtime_error(oss.str());
}
BlockDecoder::BlockDecoder(bool withMetaData)
: m_withMetaData(withMetaData)
{
reset();
}
void BlockDecoder::reset()
{
m_blockAirId = -1;
m_blockIgnoreId = -1;
m_nameMap.clear();
m_metaData.clear();
m_version = 0;
m_mapData = ustring();
}
void BlockDecoder::decode(const ustring &datastr)
{
const unsigned char *data = datastr.c_str();
size_t length = datastr.length();
// TODO: bounds checks
uint8_t version = data[0];
//uint8_t flags = data[1];
m_version = version;
size_t dataOffset = 0;
if (version >= 27)
dataOffset = 6;
else if (version >= 22)
dataOffset = 4;
else
dataOffset = 2;
ZlibDecompressor decompressor(data, length);
decompressor.setSeekPos(dataOffset);
m_mapData = decompressor.decompress();
ustring metaData = decompressor.decompress(); // unused metadata
dataOffset = decompressor.seekPos();
if (m_withMetaData && version >= 27)
{
unsigned char const *md = metaData.c_str();
unsigned char const *metaDataEnd = md + metaData.size();
int metaDataVersion = md[0];
md++;
if (metaDataVersion)
{
int metaDataCount = readU16(md);
md+=2;
for (int i = 0; i < metaDataCount ; i++)
{
int position = readU16(md);
md+=2;
unsigned int numVars = readU32(md);
md+=4;
NodeMetaData nmd;
for (unsigned var = 0; var < numVars; var++)
{
size_t keyLen = readU16(md);
md+=2;
std::string key((char const *)md, keyLen);
md+= keyLen;
size_t valueLen = readU32(md);
md+=4;
std::string value((char const *)md, valueLen);
nmd.push_back(std::pair<std::string, std::string>(key, value));
md+= valueLen;
if (metaDataVersion > 1)
{
md++; // skip priv flag
}
}
m_metaData[position] = nmd;
if (md < metaDataEnd && !strncmp("List", reinterpret_cast<const char *>(md), 4)) // skip inventory for now
{
while (true)
{
unsigned char const *start = md;
while (*md != '\n')
{
md++;
if (md >= metaDataEnd)
{
std::cout << "Invalid metadata in Block!";
//! thrown runtime error
}
}
md++;
if (!strncmp("EndInventory", reinterpret_cast<char const *>(start), 12))
{
break;
}
}
}
}
}
}
// Skip unused data
if (version <= 21)
dataOffset += 2;
if (version == 23)
dataOffset += 1;
if (version == 24) {
uint8_t ver = data[dataOffset++];
if (ver == 1) {
uint16_t num = readU16(data + dataOffset);
dataOffset += 2;
dataOffset += 10 * num;
}
}
// Skip unused static objects
dataOffset++; // Skip static object version
int staticObjectCount = readU16(data + dataOffset);
dataOffset += 2;
for (int i = 0; i < staticObjectCount; ++i) {
dataOffset += 13;
uint16_t dataSize = readU16(data + dataOffset);
dataOffset += dataSize + 2;
}
dataOffset += 4; // Skip timestamp
// Read mapping
if (version >= 22) {
dataOffset++; // mapping version
uint16_t numMappings = readU16(data + dataOffset);
dataOffset += 2;
for (int i = 0; i < numMappings; ++i) {
uint16_t nodeId = readU16(data + dataOffset);
dataOffset += 2;
uint16_t nameLen = readU16(data + dataOffset);
dataOffset += 2;
std::string name(reinterpret_cast<const char *>(data) + dataOffset, nameLen);
if (name == "air")
m_blockAirId = nodeId;
else if (name == "ignore")
m_blockIgnoreId = nodeId;
else
m_nameMap[nodeId] = name;
dataOffset += nameLen;
}
}
// Node timers
if (version >= 25) {
dataOffset++;
uint16_t numTimers = readU16(data + dataOffset);
dataOffset += 2;
dataOffset += numTimers * 10;
}
}
bool BlockDecoder::isEmpty() const
{
// only contains ignore and air nodes?
return m_nameMap.empty();
}
std::string BlockDecoder::getNode(u8 x, u8 y, u8 z) const
{
unsigned int position = x + (y << 4) + (z << 8);
int content = readBlockContent(m_mapData.c_str(), m_version, position);
if (content == m_blockAirId || content == m_blockIgnoreId)
return "";
NameMap::const_iterator it = m_nameMap.find(content);
if (it == m_nameMap.end()) {
std::cerr << "Skipping node with invalid ID." << std::endl;
return "";
}
return it->second;
}
BlockDecoder::NodeMetaData const &BlockDecoder::getNodeMetaData(u8 x, u8 y, u8 z) const
{
unsigned int position = x + (y << 4) + (z << 8);
MetaData::const_iterator m = m_metaData.find(position);
if (m == m_metaData.end())
{
return s_emptyMetaData;
}
return m->second;
}
BlockDecoder::NodeMetaData BlockDecoder::s_emptyMetaData;