-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
342 lines (279 loc) · 12.5 KB
/
main.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
import math
import os
import re
import zipfile
import inquirer
from rich import print as rprint
from rich.progress import Progress
from rich.prompt import Prompt, IntPrompt
from rich import traceback
version = "v1.0"
WORLD_FOLDER_TO_MCWORLD = "Convert world folder to .mcworld"
MCWORLD_TO_WORLD_FOLDER = "Convert .mcworld to world folder"
CURRENT_DIR = os.path.realpath(os.getcwd())
DEFAULT_PACKED_WORLD_OUTPUT_DIR = os.path.join(CURRENT_DIR, "Packed_Worlds")
DEFAULT_UNPACKED_WORLD_OUTPUT_DIR = os.path.join(CURRENT_DIR, "Unpacked_Worlds")
LOCAL_DIRECTORY_TYPE = f"Local directory (list all directories in {os.getcwd()})"
CUSTOM_PATH_DIRECTORY_TYPE = "Custom path to Directory"
LOCAL_MCWORLD_TYPE = "Local directory (list all .mcworld files)"
CUSTOM_PATH_MCWORLD_TYPE = "Custom path to .mcworld"
def create_folder_if_absent(directory: str):
if not os.path.exists(directory):
os.mkdir(directory)
def is_valid_path(path):
return len(path.strip()) > 0 and os.path.exists(path)
def get_folder_size(folder_path):
total_size = 0
for dirpath, dirnames, filenames in os.walk(folder_path):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
total_size += os.path.getsize(file_path)
return total_size
def convert_size(size_bytes):
if size_bytes == 0:
return "0 B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return f"{s} {size_name[i]}"
def init_operation_type():
operation_type_prompt = [
inquirer.List(
"selected_operation_type",
message="Choose an operation",
choices=[
WORLD_FOLDER_TO_MCWORLD,
MCWORLD_TO_WORLD_FOLDER
],
carousel=True
)
]
operation_type = inquirer.prompt(operation_type_prompt)
rprint(f"📃 You chose [bold green]{operation_type['selected_operation_type']}[/bold green]")
return operation_type["selected_operation_type"]
# Change file extension zip to mcworld
def change_ext_to_mcworld(zip_file):
base_name = os.path.splitext(zip_file)[0]
new_name = f"{base_name}.mcworld"
os.rename(zip_file, new_name)
class FolderToMcWorld():
def init_input_dir(self):
dir_type_prompt = [
inquirer.List(
"selected_dir_type",
message="📂 Choose the Minecraft Bedrock world folder location",
choices=[
LOCAL_DIRECTORY_TYPE,
CUSTOM_PATH_DIRECTORY_TYPE
],
carousel=True
)
]
dir_type = inquirer.prompt(dir_type_prompt)
rprint(f"📃 You chose [bold green]{dir_type['selected_dir_type']}[/bold green]")
return dir_type['selected_dir_type']
def select_local_world_folder(self):
folders = [
folder for folder in os.listdir(CURRENT_DIR) if os.path.isdir(os.path.join(CURRENT_DIR, folder)) and not folder.startswith(".")
]
folder_prompt = [
inquirer.List(
"selected_folder",
message="📂 Choose the world folder",
choices=folders,
carousel=True
)
]
selected_folder = inquirer.prompt(folder_prompt)
rprint(f"📁 You chose [bold green]{selected_folder['selected_folder']}[/bold green]")
return selected_folder['selected_folder']
def select_custom_world_folder_path(self):
selected_folder = ""
while not is_valid_path(selected_folder):
selected_folder = Prompt.ask(
"📁 Enter the path to the world folder",
default=DEFAULT_PACKED_WORLD_OUTPUT_DIR
)
if not is_valid_path(selected_folder):
rprint("[red]Invalid path. Please enter a valid path.[/red]")
rprint(f"📁 You chose [bold green]{selected_folder}[/bold green]")
return selected_folder
def select_output_folder(self):
output_folder_path = ""
while not is_valid_path(output_folder_path):
output_folder_path = Prompt.ask(
"📂 Enter the output folder path",
default=DEFAULT_PACKED_WORLD_OUTPUT_DIR
)
if not is_valid_path(output_folder_path):
rprint("[red]Invalid path. Please enter a valid path.[/red]")
rprint(f"📁 You chose [bold green]{output_folder_path}[/bold green]")
return output_folder_path
def select_output_filename(self):
default_filename = "output"
output_filename = ""
while not output_filename.strip():
output_filename = Prompt.ask(
"📂 Enter the output ZIP file name (without extension)",
default=default_filename
)
if not output_filename.strip():
rprint("[red]Filename cannot be empty. Please enter a valid filename.[/red]")
rprint(f"📁 You chose [bold green]{output_filename}[/bold green]")
return output_filename
def pack_mcworld(self, input_dir: str, output_path: str, compression_level: int):
with zipfile.ZipFile(output_path, 'w', compression=zipfile.ZIP_DEFLATED, compresslevel=compression_level) as zip_file:
file_count = sum(len(files) for _, _, files in os.walk(input_dir))
with Progress() as progress:
archived_file_count = 0
task = progress.add_task("🔧 [cyan]Archiving files...[/cyan]", total=file_count)
for root, dir_names, files in os.walk(input_dir):
for file in files:
file_path = os.path.join(root, file)
# Construct paths to files
arcname = os.path.relpath(file_path, input_dir)
# Write files to the zip archive
zip_file.write(file_path, arcname=arcname)
# Store file sizes to display in progress
zip_structured_arcname = os.path.relpath(file_path, input_dir).replace(os.path.sep, '/')
original_size = convert_size(os.path.getsize(file_path))
compressed_size = convert_size(zip_file.getinfo(zip_structured_arcname).compress_size)
# Print out the progress of file archiving
archived_file_count += 1
progress.console.print(f"Archiving file {archived_file_count} / {file_count} - [bold blue]{zip_structured_arcname}[/bold blue] [bold green]({original_size} -> {compressed_size})[/bold green]")
progress.advance(task)
class McWorldToFolder():
def init_input_mcworld(self):
dir_type_prompt = [
inquirer.List(
"selected_mcworld_type",
message="Choose the .mcworld file location type",
choices=[
LOCAL_MCWORLD_TYPE,
CUSTOM_PATH_MCWORLD_TYPE
],
carousel=True
)
]
mcworld_type = inquirer.prompt(dir_type_prompt)
rprint(f"📃 You chose [bold green]{mcworld_type['selected_mcworld_type']}[/bold green]")
return mcworld_type['selected_mcworld_type']
def select_local_mcworld_file(self):
mcworld_files = [
file for file in os.listdir(CURRENT_DIR) if file.endswith(".mcworld")
]
if not mcworld_files:
rprint(f"❌ [red]No .mcworld files found in local directory! Put your .mcworld file(s) in this directory ({CURRENT_DIR}) or choose a custom path.[/red]\n")
exit(0)
mcworld_prompt = [
inquirer.List(
"selected_mcworld",
message="📃 Choose a .mcworld file",
choices=mcworld_files,
carousel=True
)
]
selected_mcworld = inquirer.prompt(mcworld_prompt)
rprint(f"📃 You chose [bold green]{selected_mcworld['selected_mcworld']}[/bold green]")
return selected_mcworld['selected_mcworld']
def select_custom_mcworld_path(self):
selected_folder = ""
while not is_valid_path(selected_folder):
selected_folder = Prompt.ask(
"📂 Enter the path to the world directory",
default=DEFAULT_PACKED_WORLD_OUTPUT_DIR
)
if not is_valid_path(selected_folder):
rprint("[red]Invalid path. Please enter a valid path.[/red]")
rprint(f"📁 You chose [bold green]{selected_folder}[/bold green]")
return selected_folder
def select_output_folder(self):
output_folder_path = ""
while not is_valid_path(output_folder_path):
output_folder_path = Prompt.ask(
"📂 Enter the output folder path",
default=DEFAULT_UNPACKED_WORLD_OUTPUT_DIR
)
if not is_valid_path(output_folder_path):
rprint("[red]Invalid path. Please enter a valid path.[/red]")
rprint(f"📁 You chose [bold green]{output_folder_path}[/bold green]")
return output_folder_path
def unpack_mcworld(self, mcworld_file: str, output_folder: str):
with zipfile.ZipFile(mcworld_file, 'r') as zip_ref:
file_count = len(zip_ref.namelist())
mcworld_dir_name = re.sub(r'[\/:*?"<>|]', '', os.path.splitext(os.path.basename(mcworld_file))[0])
target_dir = os.path.join(output_folder, mcworld_dir_name)
with Progress() as progress:
extracted_file_count = 0
task = progress.add_task("🔓 [cyan]Extracting files...[/cyan]", total=file_count)
for member in zip_ref.infolist():
# Extract the files
zip_ref.extract(member, target_dir)
# Print out the progress of file extracting
extracted_file_count += 1
progress.console.print(f"Extracting file {extracted_file_count} / {file_count} - [bold blue]{member.filename}[/bold blue] [bold green]({convert_size(member.compress_size)} -> {convert_size(member.file_size)})[/bold green]")
progress.advance(task)
def main():
# Banner
print(
""" __ __ _____ __ _ _ _____ _
| \/ |/ __\ \ / /__ _ _| |__| | |_ _|__ ___| |___
| |\/| | (__ \ \/\/ / _ \ '_| / _` | | |/ _ \/ _ \ (_-<
|_| |_|\___| \_/\_/\___/_| |_\__,_| |_|\___/\___/_/__/
""")
rprint(f"Made by [blue]TrueMLGPro[/blue] | [bold green]{version}[/bold green]")
# Create folders required for the script to function
create_folder_if_absent(DEFAULT_PACKED_WORLD_OUTPUT_DIR)
create_folder_if_absent(DEFAULT_UNPACKED_WORLD_OUTPUT_DIR)
operation_type: str = init_operation_type()
# Minecraft Bedrock world folder to .mcworld conversion
if operation_type == WORLD_FOLDER_TO_MCWORLD:
mcworld_packer = FolderToMcWorld()
dir_type: str = mcworld_packer.init_input_dir()
if dir_type == LOCAL_DIRECTORY_TYPE:
input_world_directory: str = mcworld_packer.select_local_world_folder()
elif dir_type == CUSTOM_PATH_DIRECTORY_TYPE:
input_world_directory: str = mcworld_packer.select_custom_world_folder_path()
compression_level: int = IntPrompt.ask("🥽 Enter the compression level for the output archive (0-9)", default=0)
rprint(f"💾 You chose the compression level of [bold green]{compression_level}[/bold green]")
output_dir: str = mcworld_packer.select_output_folder()
output_zip_file_str: str = mcworld_packer.select_output_filename()
output_zip_file: str = output_zip_file_str if output_zip_file_str.endswith(".zip") else output_zip_file_str + ".zip"
output_mcworld_path: str = os.path.join(output_dir, output_zip_file)
rprint("🔒 Packing the [blue]world folder[/blue] to [blue].mcworld[/blue]...")
mcworld_packer.pack_mcworld(input_world_directory, output_mcworld_path, compression_level)
change_ext_to_mcworld(output_mcworld_path)
world_folder_name = os.path.basename(input_world_directory)
final_output_file_path = os.path.splitext(output_mcworld_path)[0] + ".mcworld"
world_folder_size = convert_size(get_folder_size(input_world_directory))
packed_mcworld_size = convert_size(os.path.getsize(final_output_file_path))
rprint(f"✅ [green]Successfully packed world [bold cyan]{world_folder_name}[/bold cyan] [bold blue]({world_folder_size})[/bold blue] to [bold cyan]{final_output_file_path}[/bold cyan] [bold blue]({packed_mcworld_size})[/bold blue]! :)[/green]")
# .mcworld to Minecraft Bedrock world folder conversion
elif operation_type == MCWORLD_TO_WORLD_FOLDER:
mcworld_unpacker = McWorldToFolder()
dir_type: str = mcworld_unpacker.init_input_mcworld()
if dir_type == LOCAL_MCWORLD_TYPE:
input_mcworld_path: str = mcworld_unpacker.select_local_mcworld_file()
elif dir_type == CUSTOM_PATH_MCWORLD_TYPE:
input_mcworld_path: str = mcworld_unpacker.select_custom_mcworld_path()
output_folder: str = mcworld_unpacker.select_output_folder()
rprint("🔓 Unpacking [blue].mcworld[/blue] to a [blue]world folder[/blue]...")
mcworld_unpacker.unpack_mcworld(input_mcworld_path, output_folder)
input_mcworld_name = os.path.basename(input_mcworld_path)
final_output_folder_path = os.path.join(output_folder, input_mcworld_name.split(".")[0])
mcworld_size = convert_size(os.path.getsize(input_mcworld_path))
unpacked_world_size = convert_size(get_folder_size(output_folder))
rprint(f"✅ [green]Successfully unpacked [bold cyan]{input_mcworld_name}[/bold cyan] [bold blue]({mcworld_size})[/bold blue] to [bold cyan]{final_output_folder_path}[/bold cyan] [bold blue]({unpacked_world_size})[/bold blue]! :)[/green]")
if __name__ == '__main__':
try:
# Initialize the rich traceback handler
_ = traceback.install()
main()
except Exception as e:
exception_type = type(e)
tb_obj = e.__traceback__
exc_tb = e.with_traceback(tb_obj)
rprint(traceback.Traceback.from_exception(exc_type=exception_type, exc_value=e, traceback=tb_obj))
except KeyboardInterrupt:
rprint("\n⏹ [bold blue]Exiting...[/bold blue]")