-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanvilord.py
357 lines (281 loc) · 12.2 KB
/
anvilord.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
353
354
355
356
357
"""
Anvilord is a tool for lossless Minecraft world compression.
"""
import os
import argparse
import time
import datetime
import gzip
import json
import zipfile
import zopfli
import region
__author__ = "Raccffy"
__version__ = "1.2.0"
def search_for_region_folders(path):
contents = os.listdir(path)
regions = []
for i, j in enumerate(contents):
temp_path = path + "/" + j
if os.path.isdir(temp_path):
if (temp_path.endswith("region")
or temp_path.endswith("entities")
or temp_path.endswith("poi")):
regions.append(temp_path)
else:
regions += search_for_region_folders(temp_path)
return regions
def get_all_files_exclude_region(path):
contents = os.listdir(path)
files = []
for i, j in enumerate(contents):
temp_path = path + "/" + j
if os.path.isdir(temp_path):
if not (temp_path.endswith("region")
or temp_path.endswith("entities")
or temp_path.endswith("poi")):
files += get_all_files_exclude_region(temp_path)
else:
files.append(temp_path)
return files
def recompress_chunk(x):
x_loc = x % 32
z_loc = x // 32
if current_region.chunks[z_loc][x_loc] is None:
return
stats.total_chunks += 1
if (not args.disable_quick_compression
and current_region.chunks[z_loc][x_loc].calculate_sections() == 1):
stats.skipped_chunks += 1
return
if args.verbose:
print(f"\rRecompressing chunk {z_loc:>2}; {x_loc:>2}...", end=" ")
if args.zopfli_chunk:
current_region.chunks[z_loc][x_loc].recompress_chunk_zopfli(compression_scheme_mappings[args.compression_scheme],
args.zopfli_iterations,
args.zopfli_disable_block_splitting,
args.zopfli_block_splitting_max)
else:
current_region.chunks[z_loc][x_loc].recompress_chunk(compression_scheme_mappings[args.compression_scheme],
args.compression_level)
stats.recompressed_chunks += 1
def region_files_integrity():
for i, j in enumerate(region_folders):
files = os.listdir(j)
for i2, j2 in enumerate(files):
if j2.endswith(".mca") or j2.endswith(".mcr"):
path = f"{j}/{j2}"
try:
current_region = region.Region()
current_region.read_from_file(path)
except region.HeadersErrorException:
print(f'"{path}" has empty headers.')
def squash_region_file():
if args.verbose:
time_rs = time.monotonic()
source_region_sections = 0
compressed_region_sections = 0
for x in range(1024):
source_region_sections += calculate_chunk_sections(x)
recompress_chunk(x)
compressed_region_sections += calculate_chunk_sections(x)
if compressed_region_sections < source_region_sections:
diff = source_region_sections - compressed_region_sections
print(f"Saved {diff * 4_096} bytes.")
stats.sections_saved += diff
elif args.verbose:
print()
if args.verbose:
print("Archiving region file.")
if not args.disable_datetime_preservation:
date_time = os.path.getmtime(path)
date_time = datetime.datetime.fromtimestamp(date_time).timetuple()
infoobj = zopfli.ZipInfo(path, date_time=date_time)
else:
infoobj = zopfli.ZipInfo(path)
# This forces Zopfli to compress files instead of storing them.
infoobj.compress_type = 8
arc.writestr(infoobj,
current_region.compile_region_file(),
compress_type=zipfile.ZIP_DEFLATED,
compresslevel=args.compression_level)
if args.verbose:
time_re = time.monotonic()
print(f"Time elapsed: {display_time(time_re - time_rs)}")
def write_everything_but_region():
for i, path in enumerate(files):
gzipped = False
if args.verbose:
print(f'Packing "{path}"...')
with open(path, "rb") as f:
if (not args.disable_gzip_data_recompression
and not path.endswith(".json")):
magic = f.read(2)
f.seek(0)
d = f.read()
# Check if current file was gzipped.
if magic == b"\x1f\x8b":
if args.verbose:
print("File is gzipped. Recompressing.")
gzipped = True
elif (not args.disable_json_data_minification
and path.endswith(".json")):
d = f.read()
try:
d_jsoned = json.loads(d)
except json.JSONDecodeError as e:
if args.verbose:
print(f'Cannot read JSON file: "{e}"')
d = json.dumps(d_jsoned, separators=(",", ":"))
else:
d = f.read()
if not args.disable_datetime_preservation:
date_time = os.path.getmtime(path)
date_time = datetime.datetime.fromtimestamp(date_time).timetuple()
infoobj = zipfile.ZipInfo(path, date_time=date_time)
else:
infoobj = zipfile.ZipInfo(path)
if gzipped:
try:
d_decompressed = gzip.decompress(d)
except gzip.BadGzipFile as e:
if args.verbose:
print(f'Failed to decompress: "{e}"')
gzipped = False
if gzipped and args.zopfli_chunk:
cobj = zopfli.ZopfliCompressor(zopfli.ZOPFLI_FORMAT_GZIP,
iterations=args.zopfli_iterations,
block_splitting=not args.zopfli_disable_block_splitting,
block_splitting_max=args.zopfli_block_splitting_max)
d = cobj.compress(d_decompressed) + cobj.flush()
elif gzipped and not args.zopfli_chunk:
d = gzip.compress(d_decompressed, compresslevel=args.compression_level)
# This forces Zopfli to compress files instead of storing them.
infoobj.compress_type = 8
arc.writestr(infoobj,
d,
compress_type=zipfile.ZIP_DEFLATED,
compresslevel=args.compression_level)
def calculate_chunk_sections(x):
x_loc = x % 32
z_loc = x // 32
if current_region.chunks[z_loc][x_loc] is None:
return 0
return current_region.chunks[z_loc][x_loc].calculate_sections()
def display_time(time):
hours = int(time // 3600)
minutes = int(time % 3600 // 60)
seconds = int(time % 60)
return f"{hours:0>2}:{minutes:0>2}:{seconds:0>2}"
class Stats:
def __init__(self):
self.total_chunks = 0
self.skipped_chunks = 0
self.recompressed_chunks = 0
self.sections_saved = 0
if __name__ == "__main__":
compression_scheme_mappings = {"gzip": 1, "zlib": 2, "uncompressed": 3}
parser = argparse.ArgumentParser(prog="Anvilord",
description="Lossless Minecraft world compression.")
parser.add_argument("-w", "--world",
help="Minecraft world.",
required=True)
parser.add_argument("-o", "--output",
help="Minecraft world ZIP file output.",
required=True)
parser.add_argument("-v", "--verbose",
help="Enable debug messages.",
action="store_true")
parser.add_argument("--version",
action="version",
help="Show program's version and exit.",
version=f"%(prog)s {__version__}")
# Groups.
general = parser.add_argument_group("General")
compression = parser.add_argument_group("Compression")
# General.
general.add_argument("--disable-quick-compression",
help="Disable skipping of one-sectioned chunks.",
action="store_true")
general.add_argument("--disable-region-integrity",
help="Disable quick region file integrity check.",
action="store_true")
general.add_argument("--disable-datetime-preservation",
help="Disable modified datetime preservation.",
action="store_true")
general.add_argument("--disable-gzip-data-recompression",
help="Disable recompression of GZip data. (e.g. level data, maps, scoreboards)",
action="store_true")
general.add_argument("--disable-json-data-minification",
help="Disable JSON data minification.",
action="store_true")
# Compression.
compression.add_argument("-s", "--compression-scheme",
default="zlib",
choices=tuple(compression_scheme_mappings.keys()),
help='Override chunk compression scheme. Default: "zlib". Uncompressed is supported for '
'Minecraft 1.15.1+ only.')
compression.add_argument("-c", "--compression-level",
type=int,
default=9,
choices=range(1, 10),
help="Set compression level for reference tools. Default: 9.")
compression.add_argument("--zopfli-chunk",
help="Use Zopfli to compress Minecraft chunks. Brutally slower, but more effective.",
action="store_true")
compression.add_argument("--zopfli-gzip",
help="Use Zopfli to compress GZip data.",
action="store_true")
compression.add_argument("--zopfli-output",
help="Use Zopfli to compress ZIP output.",
action="store_true")
compression.add_argument("--zopfli-iterations",
type=int,
default=15,
help="Set Zopfli iteration count. Default: 15.")
compression.add_argument("--zopfli-disable-block-splitting",
help="Disable Zopfli block splitting.",
action="store_false")
compression.add_argument("--zopfli-block-splitting-max",
type=int,
default=15,
help="Set maximum block splitting. Default: 15.")
args = parser.parse_args()
stats = Stats()
region_folders = search_for_region_folders(args.world)
if args.disable_region_integrity is False:
print("Checking region files integrity. This can take some time.")
region_files_integrity()
time_s = time.monotonic()
files = get_all_files_exclude_region(args.world)
if args.zopfli_output:
arc = zopfli.ZipFile(args.output, "w")
else:
arc = zipfile.ZipFile(args.output, "w")
print("Packaging non-region files.")
write_everything_but_region()
print("Region files squashing started.")
for i, j in enumerate(region_folders):
files = os.listdir(j)
for i2, j2 in enumerate(files):
valid_region = False
path = j + "/" + j2
print(f'Squashing "{path}"...')
try:
current_region = region.Region()
current_region.read_from_file(f"{path}")
valid_region = True
except region.HeadersErrorException:
print(f'"{path}" has empty headers. Skipped.')
if valid_region:
squash_region_file()
time_e = time.monotonic()
print("Complete! Anvilord carefully recompressed your region files.")
print()
print(f"Total chunks: {stats.total_chunks}")
print(f"Skipped chunks: {stats.skipped_chunks}")
print(f"Compressed chunks: {stats.recompressed_chunks}")
print()
print(f"Bytes saved: {stats.sections_saved * 4_096}")
print()
print(f"Total time elapsed: {display_time(time_e - time_s)}")