forked from Fenixin/Minecraft-Region-Fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.py
356 lines (308 loc) · 13.8 KB
/
scan.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Region Fixer.
# Fix your region files with a backup copy of your Minecraft world.
# Copyright (C) 2011 Alejandro Aguilera (Fenixin)
# https://github.com/Fenixin/Minecraft-Region-Fixer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import nbt.region as region
import nbt.nbt as nbt
from os.path import split
import progressbar
import multiprocessing
from multiprocessing import queues
import world
import time
import sys
import traceback
class ChildProcessException(Exception):
"""Takes the child process traceback text and prints it as a
real traceback with asterisks everywhere."""
def __init__(self, r):
# Helps to see wich one is the child process traceback
print "*"*10
print "*** Printint the child's Traceback:"
print "*** Exception:", r[0], r[1]
for tb in r[2]:
print "*"*10
print "*** File {0}, line {1}, in {2} \n*** {3}".format(*tb)
print "*"*10
class FractionWidget(progressbar.ProgressBarWidget):
""" Convenience class to use the progressbar.py """
def __init__(self, sep=' / '):
self.sep = sep
def update(self, pbar):
return '%2d%s%2d' % (pbar.currval, self.sep, pbar.maxval)
def scan_world(world_obj, options):
w = world_obj
# scan the world dir
print "Scanning directory..."
if not w.scanned_level.path:
print "Warning: No \'level.dat\' file found!"
if not w.normal_region_files:
print "Warning: No region files found in the \"region\" directory!"
if not w.nether_region_files:
print "Info: No nether dimension in the world directory."
if not w.aether_region_files:
print "Info: No end dimension in the world directory."
if w.players:
print "There are {0} region files and {1} player files in the world directory.".format(\
len(w.normal_region_files) + len(w.nether_region_files) + len(w.aether_region_files), len(w.players))
else:
print "There are {0} region files in the world directory.".format(\
len(w.normal_region_files) + len(w.nether_region_files) + len(w.aether_region_files))
# check the level.dat file and the *.dat files in players directory
print "\n{0:-^60}".format(' Checking level.dat ')
if not w.scanned_level.path:
print "[WARNING!] \'level.dat\' doesn't exist!"
else:
if w.scanned_level.readable == True:
print "\'level.dat\' is readable"
else:
print "[WARNING!]: \'level.dat\' is corrupted with the following error/s:"
print "\t {0}".format(w.scanned_level.status_text)
print "\n{0:-^60}".format(' Checking player files ')
if not w.players:
print "Info: No player files to scan."
else:
scan_all_players(w)
all_ok = True
for name in w.players:
if w.players[name].readable == False:
print "[WARNING]: Player file {0} has problems.\n\tError: {1}".format(w.players[name].filename, w.players[name].status_text)
all_ok = False
if all_ok:
print "All player files are readable."
# SCAN ALL THE CHUNKS!
if len(w.normal_region_files) + len(w.nether_region_files) + len(w.aether_region_files) == 0:
print "No region files to scan!"
else:
if w.normal_region_files.regions:
print "\n{0:-^60}".format(' Scanning the overworld ')
scan_regionset(w.normal_region_files, options)
if w.nether_region_files.regions:
print "\n{0:-^60}".format(' Scanning the nether ')
scan_regionset(w.nether_region_files, options)
if w.aether_region_files.regions:
print "\n{0:-^60}".format(' Scanning the end ')
scan_regionset(w.aether_region_files, options)
w.scanned = True
def scan_player(scanned_dat_file):
""" At the moment only tries to read a .dat player file. It returns
0 if it's ok and 1 if has some problem """
s = scanned_dat_file
try:
player_dat = nbt.NBTFile(filename = s.path)
s.readable = True
except Exception, e:
s.readable = False
s.status_text = e
def scan_all_players(world_obj):
""" Scans all the players using the scan_player function. """
for name in world_obj.players:
scan_player(world_obj.players[name])
def scan_region_file(to_scan_region_file):
""" Scans a region file and fills a ScannedRegionFile obj.
"""
try:
r = to_scan_region_file
o = scan_region_file.options
delete_entities = o.delete_entities
entity_limit = o.entity_limit
regionset = scan_region_file.regionset
region_file = region.RegionFile(r.path)
chunk_count = 0
corrupted = 0
wrong = 0
entities_prob = 0
filename = r.filename
try:
for x in range(32):
for z in range(32):
chunk, c = scan_chunk(region_file, (x,z), o)
if c != None: # chunk not created
r.chunks[(x,z)] = c
chunk_count += 1
else: continue
if c[TUPLE_STATUS] == world.CHUNK_OK:
continue
elif c[TUPLE_STATUS] == world.CHUNK_TOO_MANY_ENTITIES:
# deleting entities is in here because parsing a chunk with thousands of wrong entities
# takes a long time, and once detected is better to fix it at once.
if delete_entities:
world.delete_entities(region_file, x, z)
print "Deleted {0} entities in chunk ({1},{2}) of the region file: {3}".format(c[TUPLE_NUM_ENTITIES], x, z, r.filename)
# entities removed, change chunk status to OK
r.chunks[(x,z)] = (0, world.CHUNK_OK)
else:
entities_prob += 1
# This stores all the entities in a file,
# comes handy sometimes.
#~ pretty_tree = chunk['Level']['Entities'].pretty_tree()
#~ name = "{2}.chunk.{0}.{1}.txt".format(x,z,split(region_file.filename)[1])
#~ archivo = open(name,'w')
#~ archivo.write(pretty_tree)
elif c[TUPLE_STATUS] == world.CHUNK_CORRUPTED:
corrupted += 1
elif c[TUPLE_STATUS] == world.CHUNK_WRONG_LOCATED:
wrong += 1
except KeyboardInterrupt:
print "\nInterrupted by user\n"
# TODO this should't exit directly in the next verion...
sys.exit(1)
r.chunk_count = chunk_count
r.corrupted_chunks = corrupted
r.wrong_located_chunks = wrong
r.entities_prob = entities_prob
r.scan_time = time.time()
scan_region_file.q.put((r, filename, corrupted, wrong, entities_prob, chunk_count))
return
# Fatal exceptions:
except IOError, e:
print "\nWARNING: I can't open the file {0} !\nThe error is \"{1}\".\nTypical causes are file blocked or problems in the file system.\n".format(filename,e)
# TODO: This doesn't need to be fatal.
scan_region_file.q.put((r, filename, None))
return
except:
# anything else is a ChildProcessException
except_type, except_class, tb = sys.exc_info()
scan_region_file.q.put((except_type, except_class, traceback.extract_tb(tb)))
return
def scan_chunk(region_file, coords, options):
""" Takes a RegionFile obj and the local coordinatesof the chunk as
inputs, then scans the chunk and returns all the data."""
try:
chunk = region_file.get_chunk(*coords)
if chunk:
data_coords = world.get_chunk_data_coords(chunk)
global_coords = world.get_global_chunk_coords(region_file.filename, coords[0], coords[1])
num_entities = len(chunk["Level"]["Entities"])
if data_coords != global_coords:
status = world.CHUNK_WRONG_LOCATED
status_text = "Mismatched coordinates (wrong located chunk)."
scan_time = time.time()
elif num_entities > options.entity_limit:
status = world.CHUNK_TOO_MANY_ENTITIES
status_text = "The chunks has too many entities (it has {0}, and it's more than the limit {1})".format(num_entities, options.entity_limit)
scan_time = time.time()
else:
status = world.CHUNK_OK
status_text = "OK"
scan_time = time.time()
else:
data_coords = None
global_coords = world.get_global_chunk_coords(region_file.filename, coords[0], coords[1])
num_entities = None
status = world.CHUNK_NOT_CREATED
status_text = "The chunk doesn't exist"
scan_time = time.time()
except region.RegionHeaderError as e:
error = "Region header error: " + e.msg
status = world.CHUNK_CORRUPTED
status_text = error
scan_time = time.time()
chunk = None
data_coords = None
global_coords = world.get_global_chunk_coords(region_file.filename, coords[0], coords[1])
num_entities = None
except region.ChunkDataError as e:
error = "Chunk data error: " + e.msg
status = world.CHUNK_CORRUPTED
status_text = error
scan_time = time.time()
chunk = None
data_coords = None
global_coords = world.get_global_chunk_coords(region_file.filename, coords[0], coords[1])
num_entities = None
except region.ChunkHeaderError as e:
error = "Chunk herader error: " + e.msg
status = world.CHUNK_CORRUPTED
status_text = error
scan_time = time.time()
chunk = None
data_coords = None
global_coords = world.get_global_chunk_coords(region_file.filename, coords[0], coords[1])
num_entities = None
return chunk, (num_entities, status) if status != world.CHUNK_NOT_CREATED else None
#~ TUPLE_COORDS = 0
#~ TUPLE_DATA_COORDS = 0
#~ TUPLE_GLOBAL_COORDS = 2
TUPLE_NUM_ENTITIES = 0
TUPLE_STATUS = 1
#~ def scan_and_fill_chunk(region_file, scanned_chunk_obj, options):
#~ """ Takes a RegionFile obj and a ScannedChunk obj as inputs,
#~ scans the chunk, fills the ScannedChunk obj and returns the chunk
#~ as a NBT object."""
#~
#~ c = scanned_chunk_obj
#~ chunk, region_file, c.h_coords, c.d_coords, c.g_coords, c.num_entities, c.status, c.status_text, c.scan_time, c.region_path = scan_chunk(region_file, c.h_coords, options)
#~ return chunk
def _mp_pool_init(regionset,options,q):
""" Function to initialize the multiprocessing in scan_all_mca_files.
Is used to pass values to the child process. """
scan_region_file.regionset = regionset
scan_region_file.q = q
scan_region_file.options = options
def scan_regionset(regionset, options):
""" This function scans all te region files in a regionset object
and fills the ScannedRegionFile obj with the results
"""
total_regions = len(regionset.regions)
total_chunks = 0
corrupted_total = 0
wrong_total = 0
entities_total = 0
# init progress bar
if not options.verbose:
pbar = progressbar.ProgressBar(
widgets=['Scanning: ', FractionWidget(), ' ', progressbar.Percentage(), ' ', progressbar.Bar(left='[',right=']'), ' ', progressbar.ETA()],
maxval=total_regions)
# queue used by processes to pass finished stuff
q = queues.SimpleQueue()
pool = multiprocessing.Pool(processes=options.processes,
initializer=_mp_pool_init,initargs=(regionset,options,q))
if not options.verbose:
pbar.start()
# start the pool
# Note to self: every child process has his own memory space,
# that means every obj recived by them will be a copy of the
# main obj
result = pool.map_async(scan_region_file, regionset.get_region_list(), max(1,total_regions//options.processes))
# printing status
counter = 0
while not result.ready() or not q.empty():
time.sleep(0.01)
if not q.empty():
r = q.get()
if len(r) == 3:
raise ChildProcessException(r)
else:
scanned_regionfile, filename, corrupted, wrong, entities_prob, num_chunks = r
# the obj returned is a copy, overwrite it in regionset
regionset[world.get_region_coords(filename)] = scanned_regionfile
corrupted_total += corrupted
wrong_total += wrong
total_chunks += num_chunks
entities_total += entities_prob
counter += 1
if options.verbose:
stats = "(c: {0}, w: {1}, tme: {2}, t: {3})".format( corrupted, wrong, entities_prob, num_chunks)
print "Scanned {0: <15} {1:.<40} {2}/{3}".format(filename, stats, counter, total_regions)
else:
pbar.update(counter)
if not options.verbose: pbar.finish()
regionset.scanned = True