forked from Fenixin/Minecraft-Region-Fixer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.py
190 lines (164 loc) · 6.16 KB
/
util.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
#!/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 platform
from os.path import join, split, exists, isfile
import world
# stolen from minecraft overviewer
# https://github.com/overviewer/Minecraft-Overviewer/
def is_bare_console():
"""Returns true if Overviewer is running in a bare console in
Windows, that is, if overviewer wasn't started in a cmd.exe
session.
"""
if platform.system() == 'Windows':
try:
import ctypes
GetConsoleProcessList = ctypes.windll.kernel32.GetConsoleProcessList
num = GetConsoleProcessList(ctypes.byref(ctypes.c_int(0)), ctypes.c_int(1))
if (num == 1):
return True
except Exception:
pass
return False
def entitle(text, level = 0):
""" Put the text in a title with lot's of hashes everywhere. """
t = ''
if level == 0:
t += "\n"
t += "{0:#^60}\n".format('')
t += "{0:#^60}\n".format(' ' + text + ' ')
t += "{0:#^60}\n".format('')
return t
def table(columns):
""" Gets a list with lists in which each list is a column,
returns a text string with a table. """
def get_max_len(l):
""" Takes a list and returns the length of the biggest
element """
m = 0
for e in l:
if len(str(e)) > m:
m = len(e)
return m
text = ""
# stores the size of the biggest element in that column
ml = []
# fill up ml
for c in columns:
m = 0
t = get_max_len(c)
if t > m:
m = t
ml.append(m)
# get the total width of the table:
ml_total = 0
for i in range(len(ml)):
ml_total += ml[i] + 2 # size of each word + 2 spaces
ml_total += 1 + 2# +1 for the separator | and +2 for the borders
text += "-"*ml_total + "\n"
# all the columns have the same number of rows
row = get_max_len(columns)
for r in range(row):
line = "|"
# put all the elements in this row together with spaces
for i in range(len(columns)):
line += "{0: ^{width}}".format(columns[i][r],width = ml[i] + 2)
# add a separator for the first column
if i == 0:
line += "|"
text += line + "|" + "\n"
if r == 0:
text += "-"*ml_total + "\n"
text += "-"*ml_total
return text
def parse_chunk_list(chunk_list, world_obj):
""" Generate a list of chunks to use with world.delete_chunk_list.
It takes a list of global chunk coordinates and generates a list of
tuples containing:
(region fullpath, chunk X, chunk Z)
"""
# this is not used right now
parsed_list = []
for line in chunk_list:
try:
chunk = eval(line)
except:
print "The chunk {0} is not valid.".format(line)
continue
region_name = world.get_chunk_region(chunk[0], chunk[1])
fullpath = join(world_obj.world_path, "region", region_name)
if fullpath in world_obj.all_mca_files:
parsed_list.append((fullpath, chunk[0], chunk[1]))
else:
print "The chunk {0} should be in the region file {1} and this region files doesn't extist!".format(chunk, fullpath)
return parsed_list
def parse_paths(args):
""" Parse the list of args passed to region-fixer.py and returns a
RegionSet object with the list of regions and a list of World
objects. """
# parese the list of region files and worlds paths
world_list = []
region_list = []
warning = False
for arg in args:
if arg[-4:] == ".mca":
region_list.append(arg)
elif arg[-4:] == ".mcr": # ignore pre-anvil region files
if not warning:
print "Warning: Region-Fixer only works with anvil format region files. Ignoring *.mcr files"
warning = True
else:
world_list.append(arg)
# check if they exist
region_list_tmp = []
for f in region_list:
if exists(f):
if isfile(f):
region_list_tmp.append(f)
else:
print "Warning: \"{0}\" is not a file. Skipping it and scanning the rest.".format(f)
else:
print "Warning: The region file {0} doesn't exists. Skipping it and scanning the rest.".format(f)
region_list = region_list_tmp
# init the world objects
world_list = parse_world_list(world_list)
return world_list, world.RegionSet(region_list = region_list)
def parse_world_list(world_path_list):
""" Parses a world list checking if they exists and are a minecraft
world folders. Returns a list of World objects. """
tmp = []
for d in world_path_list:
if exists(d):
w = world.World(d)
if w.isworld:
tmp.append(w)
else:
print "Warning: The folder {0} doesn't look like a minecraft world. I'll skip it.".format(d)
else:
print "Warning: The folder {0} doesn't exist. I'll skip it.".format(d)
return tmp
def parse_backup_list(world_backup_dirs):
""" Generates a list with the input of backup dirs containing the
world objects of valid world directories."""
directories = world_backup_dirs.split(',')
backup_worlds = parse_world_list(directories)
return backup_worlds