-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminecraftmap.cpp
384 lines (343 loc) · 8.95 KB
/
minecraftmap.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include "zlib.h"
#include <new>
#define REGION_SIDE 32
#define REGION_HEADER_SIZE (REGION_SIDE * REGION_SIDE * 4 * 2)
void
dumphex (const unsigned char *data, int num)
{
while (num--)
{
fprintf (stderr, "%.2X", *data);
data++;
}
fprintf (stderr, "\n");
}
unsigned int
reverse_byte_order(unsigned int v) {
return v >> 24 | v << 24 | v >> 8 & 0xFF00 | v << 8 & 0xFF0000;
}
unsigned char *
zlib_decompress (const unsigned char *compressed_data, unsigned int compressed_length,
unsigned int *decompressed_length)
{
int ret;
// zlib init
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = compressed_length;
strm.next_in = (unsigned char *)compressed_data;
// Guess a first output size
unsigned int output_size = compressed_length;
Bytef *output = (unsigned char *) malloc (output_size);
assert (output);
unsigned int have = 0;
ret = inflateInit (&strm);
if (ret != Z_OK) {
fprintf(stderr, "ERROR: inflateInit() returned %d\n", ret);
return 0;
}
do
{
if (have == compressed_length && have != output_size) {
return 0;
}
// Simple growing buffer
if (output_size >= have / 2)
{
output_size *= 2;
output = (unsigned char *) realloc (output, output_size);
assert (output);
}
strm.avail_out = output_size - have;
strm.next_out = output + have;
ret = inflate (&strm, Z_NO_FLUSH);
if (ret == Z_STREAM_ERROR) {
fprintf(stderr, "ERROR: inflate() returned %d\n", ret);
return 0;
}
switch (ret)
{
case Z_NEED_DICT:
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void) inflateEnd (&strm);
fprintf(stderr, "ERROR: inflate() returned %d\n", ret);
return 0;
}
have = output_size - strm.avail_out;
}
while (ret != Z_STREAM_END);
(void) inflateEnd (&strm);
*decompressed_length = have;
return output;
}
class MCRegion {
char * region_file_name;
FILE * region_file;
unsigned char * region_header;
unsigned char * chunks[REGION_SIDE][REGION_SIDE];
public:
MCRegion(char *w, int x, int z) {
// Find region file
{
assert( asprintf (®ion_file_name, "%s/region/r.%d.%d.mcr", w, x, z) != -1);
assert(region_file_name);
region_file = fopen (region_file_name, "r");
if (!region_file)
{
fprintf (stderr, "ERROR: Could not open region file %s.\n", region_file_name);
}
}
if (region_file) {
// Load header
unsigned int region_header_size = 4 * 2 * REGION_SIDE * REGION_SIDE;
region_header = (unsigned char *) malloc (region_header_size);
assert (region_header);
if(fread (region_header, 1, region_header_size, region_file) != region_header_size) {
fprintf(stderr, "WARNING: Could not load region header completely.\n");
}
fprintf(stderr, "a: %d\n", sizeof(chunks));
memset(chunks, 0, sizeof(chunks));
dumphex((unsigned char *)chunks, 4000);
}
}
unsigned char * getChunk(int x, int z) {
if (region_file) {
// Find chunk
unsigned int chunk_number = 4 * (x + z * REGION_SIDE);
unsigned int chunk_offset =
(unsigned int) region_header[chunk_number] << 16 | (unsigned int) region_header[chunk_number + 1] << 8 | region_header[chunk_number + 2];
unsigned int sectors = region_header[chunk_number + 3];
if (chunk_offset == 0) {
return 0;
}
fprintf (stderr, "INFO: Loading chunk #%d (%d, %d) from offset %d with %d sectors.\n", chunk_number, x, z, chunk_offset, sectors);
if (fseek (region_file, chunk_offset << 12, SEEK_SET) != 0) {
fprintf(stderr, "ERROR: Could not seek to %d in region file (perhaps incomplete).\n", chunk_offset << 12);
return 0;
}
// Read chunk header
unsigned int chunk_length;
if (fread (&chunk_length, 1, 4, region_file) != 4) {
fprintf(stderr, "ERROR: Could not read chunk length from region file (perhaps incomplete).\n");
return 0;
}
chunk_length = reverse_byte_order(chunk_length);
char chunk_compression_type;
if (fread (&chunk_compression_type, 1, 1, region_file) != 1) {
fprintf(stderr, "ERROR: Could not read chunk compression type from region file (perhaps incomplete).\n");
return 0;
}
if (chunk_compression_type != 2) {
fprintf(stderr, "WARNING: Chunk compression type is not 2 (zlib) but %d.\n", chunk_compression_type);
}
// Read chunk header
unsigned char *chunk = (unsigned char *) malloc (chunk_length);
assert (chunk);
unsigned int a;
int chunk_length_read = fread (chunk, 1, chunk_length, region_file);
if (chunk_length_read != chunk_length) {
fprintf(stderr, "ERROR: Could not read all chunk data from region file (%d/%d).\n", chunk_length_read, chunk_length);
}
unsigned int uchunk_length;
unsigned char *uchunk = zlib_decompress (chunk, chunk_length,
&uchunk_length);
if (!uchunk) {
fprintf(stderr, "ERROR: zlib_decompress() returned 0");
return 0;
}
free(chunk);
return uchunk;
} else {
return 0;
}
}
unsigned char * chunk(int x, int z) {
if (chunks[x][z]) {
return chunks[x][z];
} else {
return chunks[x][z] = getChunk(x, z);
}
}
};
class World {
MCRegion *regions[256][256];
public:
char * world_path;
World(char * world_path) {
this->world_path = world_path;
memset(regions, 0, 256 * 256);
}
MCRegion *region(int x, int z) {
if (regions[x][z]) {
return regions[x][z];
} else {
fprintf(stderr, "Creating new region\n");
return (regions[x][z] = new MCRegion(world_path, x, z));
}
}
unsigned char * chunk(int x, int z) {
region(x / REGION_SIDE, z / REGION_SIDE)->chunk(x % REGION_SIDE, z % REGION_SIDE);
}
};
int
toShort (const unsigned char *data)
{
return data[0] << 8 | data[1];
}
int
toInt (const unsigned char *data)
{
return data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
}
int
sizeNBT (int type, const unsigned char *data)
{
switch (type)
{
case 1:
// bool
return 1;
case 2:
// short
return 2;
case 3:
// int
return 4;
case 4:
// long
return 8;
case 5:
// float
return 4;
case 6:
// double
return 8;
case 7:
// array[int]
return toInt (data) + 4;
case 8:
// array[short]
return toShort (data) + 2;
case 9:
// list
{
int size = 5;
int type = data[0];
int num = toInt (data + 1);
for (int i = 0; i < num; i++)
{
int tmpsize = sizeNBT (type, data + size);
if (!tmpsize) {
return 0;
}
size += tmpsize;
}
return size;
}
case 10:
// compound
{
int size = 0;
while (1)
{
int type = data[size];
size++;
if (type == 0)
return size;
int stringlength = toShort (data + size);
size += stringlength + 2;
int tmpsize = sizeNBT (type, data + size);
if (!tmpsize) {
return 0;
}
size += tmpsize;
}
}
}
return 0;
}
const unsigned char *
getNBT (const char *search, const unsigned char *nbt)
{
// Separate first part (search.split('.').first in ruby :-( )
char *search_first;
int search_first_length;
{
const char *tmp = strchr (search, '.');
if (tmp == 0)
{
search_first_length = strlen (search);
}
else
{
search_first_length = tmp - search;
}
search_first = (char *) malloc (search_first_length + 1);
assert (search_first);
memcpy (search_first, search, search_first_length);
search_first[search_first_length] = '\0';
}
// Find sub item
int size = 0;
while (nbt[size] != 0)
{
int type = nbt[size];
size++;
int name_length = toShort (nbt + size);
size += 2;
int chk = (name_length == search_first_length
&& memcmp (search_first, nbt + size, search_first_length) == 0);
size += name_length;
if (chk)
{
// MATCH !!!
if (type == 10)
{
return getNBT (search + name_length + 1, nbt + size);
}
else
{
return nbt + size;
}
}
int tmpsize = sizeNBT (type, nbt + size);
if (!tmpsize) {
fprintf(stderr, "ERROR: Malformed NBT (sizeNBT() returned 0).\n");
return 0;
}
size += tmpsize;
}
return 0;
}
/*
void zerr(int ret)
{
fputs("zpipe: ", stderr);
switch (ret) {
case Z_ERRNO:
if (ferror(stdin))
fputs("error reading stdin\n", stderr);
if (ferror(stdout))
fputs("error writing stdout\n", stderr);
break;
case Z_STREAM_ERROR:
fputs("invalid compression level\n", stderr);
break;
case Z_DATA_ERROR:
fputs("invalid or incomplete deflate data\n", stderr);
break;
case Z_MEM_ERROR:
fputs("out of memory\n", stderr);
break;
case Z_VERSION_ERROR:
fputs("zlib version mismatch!\n", stderr);
}
}*/