forked from Fenixin/Minecraft-Region-Fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.py
306 lines (237 loc) · 10.7 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
#!/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, getsize
import progressbar
import multiprocessing
import world
import time
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_level(world_obj):
""" At the moment only tries to read a level.dat file and print a
warning if there are problems. """
w = world_obj
try:
level_dat = nbt.NBTFile(filename = w.level_file)
del level_dat
except Exception, e:
w.level_problems.append(e)
def scan_player(world_obj, player_file_path):
""" At the moment only tries to read a .dat player file. It returns
0 if it's ok and 1 if has some problem """
w = world_obj
nick = split(player_file_path)[1].split(".")[0]
try:
player_dat = nbt.NBTFile(filename = player_file_path)
world_obj.player_status[nick] = w.OK
del player_dat
except Exception, e:
w.player_status[nick] = e
w.player_with_problems.append(nick)
def scan_all_players(world_obj):
""" Scans all the players using the scan_player function. """
for player in world_obj.player_files:
scan_player(world_obj, player)
def scan_mcr_file(region_file_path):
""" Scans a region file reporting problems.
Takes a RegionFile obj and returns a list of corrupted
chunks where each element represents a corrupted chunk and
is a tuple containing:
(region file, (coord x, coord y), problem)
This function is used from scan_all_mcr_files and uses a
multiprocessing queue to return in real time info about the process.
"""
delete_entities = scan_mcr_file.options.delete_entities
entity_limit = scan_mcr_file.options.entity_limit
region_file = region.RegionFile(region_file_path)
w = scan_mcr_file.w
chunks = 0
problems = []
corrupted = 0
wrong = 0
entities_prob = 0
filename = split(region_file.filename)[1]
try:
for x in range(32):
for z in range(32):
chunk, status, error_msg = scan_chunk(region_file, x, z)
if status == 0:
chunks += 1
total_entities = len(chunk['Level']['Entities'].tags)
# deleting entities is in here because to parse a chunk with thousands of wrong entities
# takes a long time, and once detected is better to fix it at once.
if total_entities >= entity_limit:
if delete_entities == True:
empty_tag_list = nbt.TAG_List(nbt.TAG_Byte,'','Entities')
chunk['Level']['Entities'] = empty_tag_list
print "Deleted {0} entities in chunk ({1},{2}).".format(total_entities, x, z)
region_file.write_chunk(x, z, chunk)
else:
problems.append((region_file.filename,(x,z),w.TOO_MUCH_ENTITIES))
entities_prob += 1
print "[WARNING!]: The chunk ({0},{1}) in region file {2} has {3} entities, and this may be too much. This may be a problem!".format(x,z,split(region_file.filename)[1],total_entities)
# 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 status == -1:
chunks += 1
problems.append((region_file.filename,(x,z),w.CORRUPTED))
corrupted += 1
elif status == -2:
chunks += 1
problems.append((region_file.filename,(x,z),w.WRONG_LOCATED))
wrong += 1
# if None do nothing
del chunk # unload chunk from memory
del region_file
except KeyboardInterrupt:
print "\nInterrupted by user\n"
sys.exit(1)
scan_mcr_file.q.put((filename, corrupted, wrong, entities_prob, chunks))
return problems
def add_problem(world_obj, region_file, chunk, problem):
""" This function adds a problem to the mcr_problems dict. """
w = world_obj
if region_file in w.mcr_problems:
if chunk in w.mcr_problems[region_file]:
w.mcr_problems[region_file][chunk].append(problem)
else:
w.mcr_problems[region_file][chunk] = []
w.mcr_problems[region_file][chunk].append(problem)
else:
w.mcr_problems[region_file] = {}
w.mcr_problems[region_file][chunk] = []
w.mcr_problems[region_file][chunk].append(problem)
def scan_chunk(region_file, x, z):
""" Returns a tuple with (chunk, status_integer, error_text).
Status integers are: 0 if exists and it's OK, -1 if it's corrupted,
-2 if it the header coords doesn't match the coords stored in the
chunk data (wrong located chunk) and 1 if it doesn't exist.
The variable chunk can be None if there's no chunk to return."""
try:
chunk = region_file.get_chunk(x,z)
if chunk:
data_coords = world.get_chunk_data_coords(chunk)
header_coords = world.get_global_chunk_coords(region_file.filename, x, z)
if data_coords != header_coords:
return (chunk, -2, "Mismatched coordinates.")
except region.RegionHeaderError as e:
error = "Region header error: " + e.msg
return (None, -1, error)
except region.ChunkDataError as e:
error = "Chunk data error: " + e.msg
return (None, -1, error)
except region.ChunkHeaderError as e:
error = "Chunk herader error: " + e.msg
return (None, -1, error)
if chunk != None:
return (chunk, 0, "OK")
return (None, 1, "The chunk doesn't exist")
def _mp_pool_init(world_obj,options,q):
""" Function to initialize the multiprocessing in scan_all_mcr_files.
Is used to pass values to the child process. """
scan_mcr_file.q = q
scan_mcr_file.options = options
scan_mcr_file.w = world_obj
def scan_all_mcr_files(world_obj, options):
""" This function scans all te region files from a world_object
printing status info in the process.
Takes a world object and the options object from region-fixer.py and
fills up the mcr_problems dict.
The process always uses a multiprocessing pool but the single thread
code is still stored just in case is needed. """
w = world_obj
total_regions = len(w.all_mcr_files)
total_chunks = 0
corrupted_total = 0
wrong_total = 0
entities_total = 0
region_counter = 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)
if True:
#~ if abs(options.processes) >= 1:
# queue used by processes to pass finished stuff
q = multiprocessing.Queue()
pool = multiprocessing.Pool(processes=options.processes,
initializer=_mp_pool_init,initargs=(w,options,q))
if not options.verbose:
pbar.start()
# start the pool
result = pool.map_async(scan_mcr_file, w.all_mcr_files, max(1,total_regions//options.processes))
# printing status
counter = 0
while not result.ready():
time.sleep(0.01)
# if q.qsize() > 0: # important, it hangs waiting for results
# if size = 0
filename, corrupted, wrong, entities_prob, num_chunks = q.get()
corrupted_total += corrupted
wrong_total += wrong
total_chunks += num_chunks
entities_total += entities_prob
counter += 1
if options.verbose:
stats = "(c: {0}, w: {1}, t: {2})".format( corrupted, wrong, num_chunks)
print "Scanned {0: <15} {1:.<60} {2}/{3}".format(filename, stats, counter, total_regions)
else:
pbar.update(counter)
if not options.verbose: pbar.finish()
# extract results and fill in the world class
w.num_chunks = total_chunks
for region_problems in result.get():
for prob in region_problems:
filename, chunk, problem = prob
add_problem(w, filename, chunk, problem)
else: # single thread version, non used anymore, left here because
# just-in-case
################## not used >>>>>>>>>>>>>>>>>>>
counter = 0
# init the progress bar
if not options.verbose:
pbar.start()
for region_path in w.all_mcr_files:
# scan for errors
filename, corrupted, wrong, total = scan_mcr_file(region_path, options.delete_entities, options.entity_limit)
counter += 1
# print status
if options.verbose:
stats = "(corrupted: {0}, wrong located: {1}, chunks: {2})".format( len(corrupted), len(wrong), total)
print "Scanned {0: <15} {1:.<60} {2}/{3}".format(filename, stats, counter, total_regions)
else:
pbar.update(counter)
total_chunks += total
if not options.verbose:
pbar.finish()
#<<<<<<<<<<<<<<<<< not used ###################