Skip to content

Commit 0d2c198

Browse files
committed
Converting a single mod and multiple mods at once is now supported.
1 parent ab874fb commit 0d2c198

File tree

2 files changed

+50
-41
lines changed

2 files changed

+50
-41
lines changed

python/convert.py

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import os, time, pathlib, shutil, math, re, sys, shutil, zipfile, json
1+
import os, time, shutil, math, re, sys, shutil, zipfile, json, pathlib
2+
from pathlib import Path
23
from jsoncomment import JsonComment
34
from playsound import playsound
45
from Python import shared_globals as cfg
@@ -31,20 +32,22 @@ def convert():
3132

3233
time_start = time.time()
3334

34-
unzip()
35+
input_folder = cfg.sg.user_settings_get_entry("input_folder")
36+
37+
unzip(input_folder)
3538

36-
total_progress = get_total_progress()
39+
total_progress = get_total_progress(input_folder)
3740

38-
for input_folder_path, input_subfolders, full_filename_list in os.walk(cfg.sg.user_settings_get_entry("input_folder")):
39-
mod_subfolder = get_mod_subfolder(input_folder_path)
41+
for input_folder_path, input_subfolders, full_filename_list in os.walk(input_folder):
42+
mod_subfolder = get_mod_subfolder(input_folder, input_folder_path)
4043
output_subfolder = os.path.join(output_folder, mod_subfolder)
4144

4245
try_print_mod_name(mod_subfolder)
4346
create_folder(input_folder_path, output_subfolder)
4447
process_file(full_filename_list, input_folder_path, output_subfolder)
4548

4649
if cfg.sg.user_settings_get_entry("output_zips"):
47-
create_zips(output_folder)
50+
create_zips(input_folder, output_folder)
4851

4952
progress = 0
5053
total_progress = 0
@@ -55,8 +58,7 @@ def convert():
5558
print("Finished in {} {}".format(elapsed, pluralize("second", elapsed)))
5659

5760

58-
def unzip():
59-
input_folder = cfg.sg.user_settings_get_entry("input_folder")
61+
def unzip(input_folder):
6062
for f in os.listdir(input_folder):
6163
zip_path = os.path.join(input_folder, f)
6264
if zipfile.is_zipfile(zip_path):
@@ -65,14 +67,20 @@ def unzip():
6567
os.remove(zip_path)
6668

6769

68-
def get_total_progress():
69-
input_folder = cfg.sg.user_settings_get_entry("input_folder")
70-
mod_count = len([name for name in os.listdir(input_folder) if os.path.isdir(os.path.join(input_folder, name))])
70+
def get_total_progress(input_folder):
71+
if input_folder.endswith(".rte"):
72+
mod_count = 1
73+
else:
74+
mod_count = len([name for name in os.listdir(input_folder) if os.path.isdir(os.path.join(input_folder, name))])
75+
7176
return mod_count * 2 if cfg.sg.user_settings_get_entry("output_zips") else mod_count
7277

7378

74-
def get_mod_subfolder(input_folder_path):
75-
return input_folder_path.replace(cfg.sg.user_settings_get_entry("input_folder") + "\\", "") # TODO: Find proper replacement for removing the \\ part that will also work for Unix.
79+
def get_mod_subfolder(input_folder, input_folder_path):
80+
if input_folder.endswith(".rte"):
81+
return os.path.relpath(input_folder_path, os.path.join(input_folder, os.pardir))
82+
else:
83+
return os.path.relpath(input_folder_path, input_folder)
7684

7785

7886
def try_print_mod_name(mod_subfolder):
@@ -89,16 +97,17 @@ def update_progress():
8997
cfg.progress_bar.UpdateBar(progress % total_progress, total_progress)
9098

9199

92-
def create_folder(input_folder_path, output_folder):
93-
# Prevents putting the input_folder itself into the output_folder.
94-
if input_folder_path != cfg.sg.user_settings_get_entry("input_folder"):
95-
try:
96-
os.makedirs(output_folder)
97-
except FileExistsError:
98-
pass
100+
def create_folder(input_folder_path, output_subfolder):
101+
# Prevents putting the input_folder itself into the output_subfolder.
102+
# if input_folder_path != cfg.sg.user_settings_get_entry("input_folder"):
103+
104+
try:
105+
os.makedirs(output_subfolder)
106+
except FileExistsError:
107+
pass
99108

100109

101-
def process_file(full_filename_list, input_folder_path, output_folder):
110+
def process_file(full_filename_list, input_folder_path, output_subfolder):
102111
for full_filename in full_filename_list:
103112
filename, file_extension = os.path.splitext(full_filename)
104113

@@ -107,7 +116,7 @@ def process_file(full_filename_list, input_folder_path, output_folder):
107116
continue
108117

109118
input_file_path = os.path.join(input_folder_path, full_filename)
110-
output_file_path = os.path.join(output_folder, full_filename)
119+
output_file_path = os.path.join(output_subfolder, full_filename)
111120

112121
if file_extension in (".ini", ".lua"):
113122
create_converted_file(input_file_path, output_file_path)
@@ -203,16 +212,23 @@ def regex_replace_bmps_and_wavs(all_lines):
203212
return all_lines
204213

205214

206-
def create_zips(output_folder):
207-
# Get mod folder names from the input_folder.
208-
folder_names = [f for f in os.listdir(cfg.sg.user_settings_get_entry("input_folder")) if os.path.isdir(os.path.join(output_folder, f))]
209-
210-
for f in folder_names:
211-
print("Zipping '{}'".format(f))
212-
folder_path = os.path.join(output_folder, f)
213-
shutil.make_archive(folder_path, "zip", root_dir=output_folder, base_dir=f)
214-
shutil.rmtree(folder_path)
215-
update_progress()
215+
def create_zips(input_folder, output_folder):
216+
print(input_folder)
217+
if input_folder.endswith(".rte"):
218+
create_single_zip(Path(input_folder).name, output_folder)
219+
else:
220+
# TODO: Move check if it's a directory out of this loop.
221+
folder_names = [f for f in os.listdir(cfg.sg.user_settings_get_entry("input_folder")) if os.path.isdir(os.path.join(output_folder, f))]
222+
for mod_name in folder_names:
223+
create_single_zip(mod_name, output_folder)
224+
225+
226+
def create_single_zip(mod_name, output_folder):
227+
print("Zipping '{}'".format(mod_name))
228+
folder_path = os.path.join(output_folder, mod_name)
229+
shutil.make_archive(folder_path, "zip", root_dir=output_folder, base_dir=mod_name)
230+
shutil.rmtree(folder_path)
231+
update_progress()
216232

217233

218234
def pluralize(word, count):

python/gui.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,13 @@ def init_window_theme():
1717
sg.theme_progress_bar_color((progress_bar_color, sg.theme_progress_bar_color()[1]))
1818

1919

20-
def get_input_folder(input_folder):
21-
parts = pathlib.Path(input_folder).parts
22-
if parts[-1].endswith(".rte"):
23-
return pathlib.Path(*parts[:-1]).as_posix() # .as_posix() prevents .replace() issues.
24-
return input_folder
25-
26-
2720
def init_window():
2821
no_path_set_color = "#b35858"
2922

3023
paths_column = [
3124
[sg.Frame(layout=[
3225
[
33-
sg.Text("Mod to convert"),
26+
sg.Text("Mod(s) to convert", tooltip=" You can convert multiple legacy mods at once by selecting their parent directory "),
3427
sg.In(
3528
sg.user_settings_get_entry("input_folder"),
3629
size=(34, 1),
@@ -95,7 +88,7 @@ def run_window(window):
9588
if event == "-INPUT FOLDER-":
9689
input_folder_or_file = values[event]
9790
if input_folder_or_file != "":
98-
sg.user_settings_set_entry("input_folder", get_input_folder(input_folder_or_file))
91+
sg.user_settings_set_entry("input_folder", input_folder_or_file)
9992
window[event](background_color = sg.theme_input_background_color())
10093

10194
elif event == "-OUTPUT ZIPS-":

0 commit comments

Comments
 (0)