1
- import os , time , shutil , math , re , sys , shutil , zipfile , json , pathlib , webbrowser
1
+ import os , time , shutil , math , sys , zipfile , json , pathlib , webbrowser
2
2
from pathlib import Path
3
3
from jsoncomment import JsonComment
4
4
from playsound import playsound
5
5
6
6
from Python import shared_globals as cfg
7
+ from Python import regex_rules
8
+ from Python .zips import create_zips
9
+ from Python import update_progress
7
10
8
- progress = 0
9
- total_progress = 0
10
11
11
12
warnings_file_name = "Warnings.json"
12
13
warnings_path = os .path .join ("ConversionRules" , warnings_file_name )
@@ -59,7 +60,7 @@ def check_github_button_clicked_and_exit(clicked_github_button):
59
60
60
61
61
62
def convert ():
62
- global progress , total_progress , output_folder , warnings
63
+ global output_folder , warnings
63
64
64
65
time_start = time .time ()
65
66
@@ -68,7 +69,7 @@ def convert():
68
69
69
70
unzip (input_folder_path )
70
71
71
- total_progress = get_total_progress (input_folder_path )
72
+ update_progress . set_max_progress (input_folder_path )
72
73
73
74
for input_subfolder_path , input_subfolders , input_subfiles in os .walk (input_folder_path ):
74
75
mod_subfolder = get_mod_subfolder (input_folder_path , input_subfolder_path , true_input_folder_path )
@@ -86,8 +87,8 @@ def convert():
86
87
if len (warnings ) > 0 :
87
88
warnings_popup ()
88
89
89
- progress = 0
90
- total_progress = 0
90
+ update_progress . progress = 0
91
+ update_progress . total_progress = 0
91
92
warnings = []
92
93
93
94
elapsed = math .floor (time .time () - time_start )
@@ -105,17 +106,6 @@ def unzip(input_folder_path):
105
106
os .remove (zip_path )
106
107
107
108
108
- def get_total_progress (input_folder_path ):
109
- if input_folder_path .endswith (".rte" ):
110
- mod_count = 1
111
- else :
112
- mod_count = 0
113
- for mod_name in os .listdir (input_folder_path ):
114
- if os .path .isdir (os .path .join (input_folder_path , mod_name )) and mod_name .endswith (".rte" ):
115
- mod_count += 1
116
- return mod_count * 2 if cfg .sg .user_settings_get_entry ("output_zips" ) else mod_count
117
-
118
-
119
109
def get_mod_subfolder (input_folder_path , input_subfolder_path , true_input_folder_path ):
120
110
if input_folder_path .endswith (".rte" ):
121
111
return os .path .relpath (input_subfolder_path , true_input_folder_path )
@@ -126,13 +116,7 @@ def get_mod_subfolder(input_folder_path, input_subfolder_path, true_input_folder
126
116
def try_print_mod_name (mod_subfolder_parts , mod_subfolder ):
127
117
if len (mod_subfolder_parts ) == 1 :
128
118
print ("Converting '{}'" .format (mod_subfolder ))
129
- update_progress ()
130
-
131
-
132
- def update_progress ():
133
- global progress
134
- progress += 1
135
- cfg .progress_bar .UpdateBar (progress % total_progress , total_progress )
119
+ update_progress .increment_progress ()
136
120
137
121
138
122
def create_folder (input_subfolder_path , output_subfolder ):
@@ -149,14 +133,17 @@ def process_files(input_subfiles, input_subfolder_path, output_subfolder, input_
149
133
input_file_path = os .path .join (input_subfolder_path , full_filename )
150
134
output_file_path = os .path .join (output_subfolder , full_filename )
151
135
136
+ if full_filename == "desktop.ini" :
137
+ continue
138
+
152
139
if file_extension in (".ini" , ".lua" ):
153
140
create_converted_file (input_file_path , output_file_path , input_folder_path )
154
141
else :
155
142
shutil .copyfile (input_file_path , output_file_path )
156
143
157
144
158
145
def create_converted_file (input_file_path , output_file_path , input_folder_path ):
159
- try :
146
+ try : # TODO: Figure out why this try/except is necessary and why it doesn't check for an error type.
160
147
with open (input_file_path , "r" ) as file_in :
161
148
with open (output_file_path , "w" ) as file_out :
162
149
all_lines_list = []
@@ -178,111 +165,12 @@ def create_converted_file(input_file_path, output_file_path, input_folder_path):
178
165
for old_str , new_str in conversion_rules .items ():
179
166
all_lines = all_lines .replace (old_str , new_str )
180
167
181
- all_lines = regex_replace (all_lines )
182
- file_out .write (regex_replace_bmps_and_wavs (all_lines ))
168
+ all_lines = regex_rules . regex_replace (all_lines )
169
+ file_out .write (regex_rules . regex_replace_bmps_and_wavs (all_lines ))
183
170
except :
184
171
shutil .copyfile (input_file_path , output_file_path )
185
172
186
173
187
- def regex_replace (all_lines ):
188
- all_lines = simple_replace (all_lines , "Framerate = (.*)" , "SpriteAnimMode = 7" )
189
- all_lines = simple_replace (all_lines , "\t PlayerCount = (.*)\n " , "" )
190
- all_lines = simple_replace (all_lines , "\t TeamCount = (.*)\n " , "" )
191
-
192
- all_lines = specific_replace (all_lines , regex_replace_particle , False , "ParticleNumberToAdd = (.*)\n \t AddParticles = (.*)\n \t \t CopyOf = (.*)\n " , "AddGib = Gib\n \t \t GibParticle = {}\n \t \t \t CopyOf = {}\n \t \t Count = {}\n " )
193
-
194
- all_lines = specific_replace (all_lines , regex_replace_sound_priority , True , "SoundContainer(((?!SoundContainer).)*)Priority" , "SoundContainer{}// Priority" )
195
-
196
- # all_lines = specific_replace(all_lines, regex_replace_sound_priority, True, "AddSound(((?! AddSound).)*)Priority", "AddSound{}// Priority")
197
-
198
- all_lines = specific_replace (all_lines , regex_use_capture , False , "FundsOfTeam(.*) =" , "Team{}Funds =" )
199
- # all_lines = specific_replace(all_lines, regex_replace_playsound, False, "", "")
200
-
201
- return all_lines
202
-
203
-
204
- def simple_replace (all_lines , pattern , replacement ):
205
- matches = re .findall (pattern , all_lines )
206
- if len (matches ) > 0 :
207
- return re .sub (pattern , replacement , all_lines )
208
- return all_lines
209
-
210
-
211
- def specific_replace (all_lines , fn , dotall , pattern , replacement ):
212
- # TODO: Refactor so .findall takes re.DOTALL as an argument directly.
213
- if dotall :
214
- matches = re .findall (pattern , all_lines , re .DOTALL )
215
- else :
216
- matches = re .findall (pattern , all_lines )
217
- if len (matches ) > 0 :
218
- return fn (all_lines , pattern , replacement , matches )
219
- return all_lines
220
-
221
-
222
- def regex_replace_particle (all_lines , pattern , replacement , matches ):
223
- # matches == [(4, "foo", "bar"), (2, "baz", "bee")]
224
- new = [item for tup in matches for item in tup ]
225
- # new == [4, "foo", "bar", 2, "baz", "bee"]
226
-
227
- # 0, 1, 2 -> 1, 2, 0
228
- new [0 ::3 ], new [1 ::3 ], new [2 ::3 ] = \
229
- new [1 ::3 ], new [2 ::3 ], new [0 ::3 ]
230
-
231
- # new == ["foo", "bar", 4, "baz", "bee", 2]
232
- return re .sub (pattern , replacement , all_lines ).format (* new )
233
-
234
-
235
- def regex_replace_sound_priority (all_lines , pattern , replacement , matches ):
236
- # TODO: This pattern returns two items in each tuple, while we only need the first. Create a better pattern.
237
- # https://stackoverflow.com/a/406408/13279557
238
- # https://regex101.com/r/NdKaWs/2
239
-
240
- # matches == [(4, "foo"), (2, "bar")]
241
- new = [item for tup in matches for item in tup ][::2 ]
242
- # new == [4, 2]
243
- return re .sub (pattern , replacement , all_lines , flags = re .DOTALL ).format (* new )
244
-
245
-
246
- def regex_use_capture (all_lines , pattern , replacement , matches ):
247
- return re .sub (pattern , replacement , all_lines ).format (* matches )
248
-
249
-
250
- # def regex_replace_playsound(all_lines, pattern, replacement, matches):
251
- # return all_lines
252
- # # TODO:
253
- # # AudioMan:PlaySound("ModName.rte/Folder/SoundName.wav", SceneMan:TargetDistanceScalar(self.Pos), false, true, -1)
254
- # # to
255
- # # AudioMan:PlaySound("ModName.rte/Folder/SoundName.wav", self.Pos) -- Cut everything and leave the thing inside the brackets after SceneMan:TargetDistanceScalar
256
-
257
-
258
- def regex_replace_bmps_and_wavs (all_lines ):
259
- # TODO: Combine these four patterns into two.
260
- all_lines = specific_replace (all_lines , regex_use_capture , False , "Base\.rte(.*?)\.bmp" , "Base.rte{}.png" )
261
- all_lines = specific_replace (all_lines , regex_use_capture , False , "base\.rte(.*?)\.bmp" , "Base.rte{}.png" )
262
- all_lines = specific_replace (all_lines , regex_use_capture , False , "Base\.rte(.*?)\.wav" , "Base.rte{}.flac" )
263
- all_lines = specific_replace (all_lines , regex_use_capture , False , "base\.rte(.*?)\.wav" , "Base.rte{}.flac" )
264
- return all_lines
265
-
266
-
267
- def create_zips (input_folder_path , output_folder ):
268
- if input_folder_path .endswith (".rte" ):
269
- create_single_zip (Path (input_folder_path ).name , output_folder )
270
- else :
271
- # TODO: Move check if it's a directory out of this loop.
272
- folder_names = [f for f in os .listdir (input_folder_path ) if os .path .isdir (os .path .join (output_folder , f ))]
273
- for mod_name in folder_names :
274
- if mod_name .endswith (".rte" ):
275
- create_single_zip (mod_name , output_folder )
276
-
277
-
278
- def create_single_zip (mod_name , output_folder ):
279
- print ("Zipping '{}'" .format (mod_name ))
280
- mod_path = os .path .join (output_folder , mod_name )
281
- shutil .make_archive (mod_path .replace (".rte" , "" ) + "-v1.0" + ".rte" , "zip" , root_dir = output_folder , base_dir = mod_name )
282
- shutil .rmtree (mod_path )
283
- update_progress ()
284
-
285
-
286
174
def warnings_popup ():
287
175
if warnings_available :
288
176
w = max (30 , len (max (warnings , key = len )))
0 commit comments