-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_savefile.py
executable file
·352 lines (297 loc) · 10.2 KB
/
parse_savefile.py
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
#!/usr/bin/env python3
import os
import sys
import zlib
import json
import struct
from PIL import Image
from concurrent.futures import ProcessPoolExecutor
from multiprocessing import cpu_count
from collections import namedtuple
# fmt: off
colors = {
1: (200, 200, 200, 255),
2: ( 80, 140, 50, 255),
3: (150, 100, 50, 255),
4: (175, 175, 175, 255),
5: (192, 150, 96, 255),
6: ( 70, 180, 40, 255),
8: ( 0, 0, 200, 255),
9: ( 0, 0, 200, 255),
10: (255, 150, 0, 255),
11: (255, 150, 0, 255),
12: (255, 255, 200, 255),
13: (170, 150, 170, 255),
14: (255, 255, 96, 255),
15: (200, 180, 150, 255),
16: ( 80, 80, 80, 255),
17: (192, 150, 96, 255), # copy of 5
18: ( 0, 75, 0, 255),
24: (255, 255, 200, 255), # copy of 12
26: (140, 25, 25, 255),
31: ( 60, 120, 60, 255), # grass? shrub?
32: ( 60, 120, 60, 255), # grass? shrub?
35: (255, 255, 255, 255),
37: (255, 255, 50, 255),
38: (255, 0, 0, 255),
39: (200, 150, 120, 255),
40: (180, 50, 50, 255),
44: (200, 200, 200, 255), # copy of 1
48: (115, 127, 115, 255),
49: ( 20, 20, 30, 255),
50: (255, 255, 50, 255),
51: (255, 150, 0, 255), # copy of 10
52: ( 60, 90, 180, 255),
53: (192, 150, 96, 255), # copy of 5
54: (192, 150, 96, 255), # copy of 5
58: (192, 150, 96, 255), # copy of 5
61: (200, 200, 200, 255), # copy of 1
63: (192, 150, 96, 255), # copy of 5
65: (192, 150, 96, 255), # copy of 5
67: (175, 175, 175, 255), # copy of 4
78: (255, 255, 255, 255),
79: (128, 192, 255, 255),
81: ( 10, 100, 25, 255),
82: (222, 222, 255, 255),
83: (150, 250, 150, 255),
85: (192, 150, 96, 255), # copy of 5
86: (255, 144, 0, 255),
91: (255, 144, 0, 255), # copy of 86
}
# fmt: on
TAG_End = 0
TAG_Byte = 1
TAG_Short = 2
TAG_Int = 3
TAG_Long = 4
TAG_Float = 5
TAG_Double = 6
TAG_Byte_Array = 7
TAG_String = 8
TAG_List = 9
TAG_Compound = 10
TAG_Int_Array = 11
TAG_Long_Array = 12
Job = namedtuple(
"Job",
[
"inFile", # input file
"outFile", # output file
"jobs_total", # total count
"job_id", # current item
"regionCoords", # region coordinates
],
)
def read_file(mcr_path):
# accepts path to file
# reads this file
# returns a list of chunks (nbt data as byte strings) from that file
with open(mcr_path, "rb") as f:
fraw = f.read()
chunks = list(
filter(
lambda c: c["ofs"],
[
{
# offset from start of file
"ofs": 4096 * ((fraw[i] << 16) + (fraw[i + 1] << 8) + fraw[i + 2]),
#
# how many sectors the chunk occupies in the file.
# Dont need this. Each chunk data is preceded by a length field
# 'sectors': fraw[i+3],
#
# chunk last modified time
"time": struct.unpack(">I", fraw[i + 4096 : i + 4100])[0],
}
for i in range(0, 4096, 4)
],
)
)
for c in chunks:
assert 2 == fraw[c["ofs"] + 4] # fail if compression is not Zlib
size = struct.unpack(">I", fraw[c["ofs"] : c["ofs"] + 4])[0]
c["raw"] = zlib.decompress(fraw[c["ofs"] + 5 : c["ofs"] + 5 + size])
del c["ofs"]
return chunks
def parse_nbt(raw, ofs, overrideMeta=None):
# accepts binary nbt data
# returns an object representing the nbt data
if overrideMeta == None:
typeID = raw[ofs]
ofs += 1
namelen = struct.unpack(">H", raw[ofs : ofs + 2])[0]
ofs += 2
name = raw[ofs : ofs + namelen].decode("utf8")
ofs += namelen
else:
typeID = overrideMeta
name = "UNNAMED"
if typeID == TAG_Byte:
ndata = struct.unpack(">b", raw[ofs : ofs + 1])[0]
ofs += 1
elif typeID == TAG_Short:
ndata = struct.unpack(">h", raw[ofs : ofs + 2])[0]
ofs += 2
elif typeID == TAG_Int:
ndata = struct.unpack(">i", raw[ofs : ofs + 4])[0]
ofs += 4
elif typeID == TAG_Long:
ndata = struct.unpack(">q", raw[ofs : ofs + 8])[0]
ofs += 8
elif typeID == TAG_Float:
ndata = struct.unpack(">f", raw[ofs : ofs + 4])[0]
ofs += 4
elif typeID == TAG_Double:
ndata = struct.unpack(">d", raw[ofs : ofs + 8])[0]
ofs += 8
elif typeID == TAG_Byte_Array:
arraysize = struct.unpack(">i", raw[ofs : ofs + 4])[0]
assert arraysize >= 0
ofs += 4
ndata = raw[ofs : ofs + arraysize]
ofs += arraysize
elif typeID == TAG_String:
stringsize = struct.unpack(">h", raw[ofs : ofs + 2])[0]
assert stringsize >= 0
ofs += 2
ndata = raw[ofs : ofs + stringsize].decode("utf8")
ofs += stringsize
elif typeID == TAG_List:
listContentsType = raw[ofs]
ofs += 1
count = struct.unpack(">i", raw[ofs : ofs + 4])[0]
assert count >= 0
ofs += 4
ndata = []
for _ in range(count):
_, elem, ofs = parse_nbt(raw, ofs, overrideMeta=listContentsType)
ndata.append(elem)
elif typeID == TAG_Compound:
ndata = dict()
while raw[ofs]:
elemName, elemData, ofs = parse_nbt(raw, ofs)
assert not elemName in ndata # I dont think duplicates happen
ndata[elemName] = elemData
ofs += 1
else:
raise Exception("Unknown NBT Tag (%i)!" % typeID)
return name, ndata, ofs
def fileWorker(j):
# accepts a "job"
# reads input file. writes output file
# returns ([bed1,bed2],[sign1,sign2])
bed_list = []
sign_list = []
print(f" progress: {j.job_id}/{j.jobs_total}", " " * 8, end="\r")
img = Image.new("RGBA", (512, 512), (0, 0, 0, 0))
pixels = img.load()
for chunk in read_file(j.inFile):
_, level, _ = parse_nbt(chunk["raw"], 0)
level = level["Level"]
# If this region file is storing chunks from
# outside of its region, we've got problems!
assert level["xPos"] // 32 == j.regionCoords[0]
assert level["zPos"] // 32 == j.regionCoords[1]
# Signs are stored in level['TileEntities'].
# Their rotation is stored somewhere else, but I don't need that.
for s in level["TileEntities"]:
if s["id"] == "Sign":
sign = {
"time": chunk["time"],
"x": s["x"],
"z": s["z"],
"text": [
s["Text1"],
s["Text2"],
s["Text3"],
s["Text4"],
],
}
if "".join(sign["text"]):
print("sign:", sign)
sign_list.append(sign)
# beds are regular blocks (ID 0x1A), and their
# rotation and head/foot info stored in level['Data'].
if b"\x1a" in level["Blocks"]:
chunk_beds = [
{
"x": level["xPos"] * 16 + i // 2048,
"z": level["zPos"] * 16 + i // 128 % 16,
"time": chunk["time"],
# head (8) or foot (0) of bed:
"end": (level["Data"][i >> 1] >> (i % 2 * 4)) & 8,
# Bed orientation:
# 'rotate':
# (level['Data'][i >> 1] >> (i % 2 * 4)) & 3,
}
for i, b in enumerate(level["Blocks"])
if b == 0x1A
]
for bed in chunk_beds:
if bed["end"]: # only count head of beds
del bed["end"]
print("bed:", bed)
bed_list.append(bed)
# for every z,x coordinate in this chunk, find the surface block and
# its color, and add the pixel to the PIL image.
for z in range(16):
for x in range(16):
# HeightMap saves the highest block the sun reaches
y = 128 * z + 2048 * x + level["HeightMap"][z * 16 + x] - 1
# snow does not block light, so HeightMap ignores it.
# check if block above is snow and use that instead.
while y % 128 < 127 and level["Blocks"][y + 1]:
y += 1
b = level["Blocks"][y]
if b:
if b in colors:
color = colors[b]
else:
print("no color for block:", b, " " * 8, end="\n" * 4)
color = (255, 0, 127, 255)
pixels[
level["xPos"] % 32 * 16 + x, level["zPos"] % 32 * 16 + z
] = color
img.save(j.outFile, "png")
return bed_list, sign_list
def tilesFromWorld(world_path):
# accepts path to world folder
# spawns file workers
# outputs ./static/data/tiles_0 and ./static/data/data.json
# returns nothing
for p in ["./static/data", "./static/data/tiles_0"]:
try:
os.mkdir(p)
except FileExistsError:
pass
regions = [
list(map(int, x.split(".")[1:3])) for x in os.listdir(world_path + "/region")
]
job_list = [
Job(*x)
for x in zip(
map(lambda x: f"{world_path}/region/r.{x[0]}.{x[1]}.mcr", regions),
map(lambda x: f"./static/data/tiles_0/r.{x[0]}.{x[1]}.png", regions),
(len(regions),) * len(regions),
range(len(regions)),
regions,
)
]
with ProcessPoolExecutor(max_workers=cpu_count()) as pool:
job_results = pool.map(fileWorker, job_list)
print("")
bed_list, sign_list = map(lambda x: sum(x, []), zip(*job_results))
with open("./static/data/data.json", "w") as f:
f.write(
json.dumps(
{
"beds": bed_list,
"signs": sign_list,
}
)
)
if __name__ == "__main__":
if len(sys.argv) == 2:
tilesFromWorld(sys.argv[1])
else:
print("Please specify a world directory.")