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
2
3
from jsoncomment import JsonComment
3
4
from playsound import playsound
4
5
from Python import shared_globals as cfg
@@ -31,20 +32,22 @@ def convert():
31
32
32
33
time_start = time .time ()
33
34
34
- unzip ()
35
+ input_folder = cfg .sg .user_settings_get_entry ("input_folder" )
36
+
37
+ unzip (input_folder )
35
38
36
- total_progress = get_total_progress ()
39
+ total_progress = get_total_progress (input_folder )
37
40
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 )
40
43
output_subfolder = os .path .join (output_folder , mod_subfolder )
41
44
42
45
try_print_mod_name (mod_subfolder )
43
46
create_folder (input_folder_path , output_subfolder )
44
47
process_file (full_filename_list , input_folder_path , output_subfolder )
45
48
46
49
if cfg .sg .user_settings_get_entry ("output_zips" ):
47
- create_zips (output_folder )
50
+ create_zips (input_folder , output_folder )
48
51
49
52
progress = 0
50
53
total_progress = 0
@@ -55,8 +58,7 @@ def convert():
55
58
print ("Finished in {} {}" .format (elapsed , pluralize ("second" , elapsed )))
56
59
57
60
58
- def unzip ():
59
- input_folder = cfg .sg .user_settings_get_entry ("input_folder" )
61
+ def unzip (input_folder ):
60
62
for f in os .listdir (input_folder ):
61
63
zip_path = os .path .join (input_folder , f )
62
64
if zipfile .is_zipfile (zip_path ):
@@ -65,14 +67,20 @@ def unzip():
65
67
os .remove (zip_path )
66
68
67
69
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
+
71
76
return mod_count * 2 if cfg .sg .user_settings_get_entry ("output_zips" ) else mod_count
72
77
73
78
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 )
76
84
77
85
78
86
def try_print_mod_name (mod_subfolder ):
@@ -89,16 +97,17 @@ def update_progress():
89
97
cfg .progress_bar .UpdateBar (progress % total_progress , total_progress )
90
98
91
99
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
99
108
100
109
101
- def process_file (full_filename_list , input_folder_path , output_folder ):
110
+ def process_file (full_filename_list , input_folder_path , output_subfolder ):
102
111
for full_filename in full_filename_list :
103
112
filename , file_extension = os .path .splitext (full_filename )
104
113
@@ -107,7 +116,7 @@ def process_file(full_filename_list, input_folder_path, output_folder):
107
116
continue
108
117
109
118
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 )
111
120
112
121
if file_extension in (".ini" , ".lua" ):
113
122
create_converted_file (input_file_path , output_file_path )
@@ -203,16 +212,23 @@ def regex_replace_bmps_and_wavs(all_lines):
203
212
return all_lines
204
213
205
214
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 ()
216
232
217
233
218
234
def pluralize (word , count ):
0 commit comments