@@ -72,11 +72,13 @@ def convert():
72
72
73
73
for input_subfolder_path , input_subfolders , input_subfiles in os .walk (input_folder_path ):
74
74
mod_subfolder = get_mod_subfolder (input_folder_path , input_subfolder_path , true_input_folder_path )
75
- output_subfolder = os .path .join (output_folder , mod_subfolder )
76
75
77
- try_print_mod_name (mod_subfolder )
78
- create_folder (input_subfolder_path , output_subfolder )
79
- process_file (input_subfiles , input_subfolder_path , output_subfolder , input_folder_path )
76
+ mod_subfolder_parts = pathlib .Path (mod_subfolder ).parts
77
+ if len (mod_subfolder_parts ) > 0 and mod_subfolder_parts [0 ].endswith (".rte" ):
78
+ output_subfolder = os .path .join (output_folder , mod_subfolder )
79
+ try_print_mod_name (mod_subfolder_parts , mod_subfolder )
80
+ create_folder (input_subfolder_path , output_subfolder )
81
+ process_files (input_subfiles , input_subfolder_path , output_subfolder , input_folder_path )
80
82
81
83
if cfg .sg .user_settings_get_entry ("output_zips" ):
82
84
create_zips (input_folder_path , output_folder )
@@ -107,8 +109,10 @@ def get_total_progress(input_folder_path):
107
109
if input_folder_path .endswith (".rte" ):
108
110
mod_count = 1
109
111
else :
110
- mod_count = len ([name for name in os .listdir (input_folder_path ) if os .path .isdir (os .path .join (input_folder_path , name ))])
111
-
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
112
116
return mod_count * 2 if cfg .sg .user_settings_get_entry ("output_zips" ) else mod_count
113
117
114
118
@@ -119,11 +123,9 @@ def get_mod_subfolder(input_folder_path, input_subfolder_path, true_input_folder
119
123
return os .path .relpath (input_subfolder_path , input_folder_path )
120
124
121
125
122
- def try_print_mod_name (mod_subfolder ):
123
- input_subfolder_path_tuple = pathlib .Path (mod_subfolder ).parts
124
-
125
- if len (input_subfolder_path_tuple ) == 1 :
126
- print ("Converting '{}'" .format (input_subfolder_path_tuple [0 ]))
126
+ def try_print_mod_name (mod_subfolder_parts , mod_subfolder ):
127
+ if len (mod_subfolder_parts ) == 1 :
128
+ print ("Converting '{}'" .format (mod_subfolder ))
127
129
update_progress ()
128
130
129
131
@@ -140,14 +142,10 @@ def create_folder(input_subfolder_path, output_subfolder):
140
142
pass
141
143
142
144
143
- def process_file (input_subfiles , input_subfolder_path , output_subfolder , input_folder_path ):
145
+ def process_files (input_subfiles , input_subfolder_path , output_subfolder , input_folder_path ):
144
146
for full_filename in input_subfiles :
145
147
filename , file_extension = os .path .splitext (full_filename )
146
148
147
- # The ".empty" file exists so otherwise empty folders can be added to Git.
148
- if filename == ".empty" :
149
- continue
150
-
151
149
input_file_path = os .path .join (input_subfolder_path , full_filename )
152
150
output_file_path = os .path .join (output_subfolder , full_filename )
153
151
@@ -167,55 +165,61 @@ def create_converted_file(input_file_path, output_file_path, input_folder_path):
167
165
line_number = 0
168
166
for line in file_in :
169
167
line_number += 1
170
-
171
- line = regex_replace (line )
172
- for old_str , new_str in conversion_rules .items ():
173
- line = line .replace (old_str , new_str )
174
- all_lines_list .append (line )
175
168
176
169
if warnings_available :
177
170
for old_str , new_str in warning_rules .items ():
178
171
if old_str in line :
179
172
warnings .append ("'{}' line {}: {} -> {}" .format (file_path , line_number , old_str , new_str ))
180
-
181
- all_lines_str = "" .join (all_lines_list )
182
- file_out .write (regex_replace_bmps_and_wavs (all_lines_str ))
173
+
174
+ all_lines_list .append (line )
175
+
176
+ all_lines = "" .join (all_lines_list )
177
+
178
+ for old_str , new_str in conversion_rules .items ():
179
+ all_lines = all_lines .replace (old_str , new_str )
180
+
181
+ all_lines = regex_replace (all_lines )
182
+ file_out .write (regex_replace_bmps_and_wavs (all_lines ))
183
183
except :
184
184
shutil .copyfile (input_file_path , output_file_path )
185
185
186
186
187
- def regex_replace (line ):
188
- line = simple_replace (line , "Framerate = (.*)" , "SpriteAnimMode = 7" )
189
- line = simple_replace (line , "\t PlayerCount = (.*)\n " , "" )
190
- line = simple_replace (line , "\t TeamCount = (.*)\n " , "" )
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
191
192
- line = specific_replace (line , 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
- line = specific_replace (line , regex_replace_sound_priority , True , " Sound(((?! Sound).)*)Priority" , " Sound{}// Priority" )
194
- line = specific_replace (line , regex_use_capture , False , "FundsOfTeam(.*) =" , "Team{}Funds =" )
195
- # line = specific_replace(line, regex_replace_playsound, False, "", "")
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, "", "")
196
200
197
- return line
201
+ return all_lines
198
202
199
203
200
- def simple_replace (line , pattern , replacement ):
201
- matches = re .findall (pattern , line )
204
+ def simple_replace (all_lines , pattern , replacement ):
205
+ matches = re .findall (pattern , all_lines )
202
206
if len (matches ) > 0 :
203
- return re .sub (pattern , replacement , line )
204
- return line
207
+ return re .sub (pattern , replacement , all_lines )
208
+ return all_lines
205
209
206
210
207
- def specific_replace (line , fn , dotall , pattern , replacement ):
211
+ def specific_replace (all_lines , fn , dotall , pattern , replacement ):
208
212
# TODO: Refactor so .findall takes re.DOTALL as an argument directly.
209
213
if dotall :
210
- matches = re .findall (pattern , line , re .DOTALL )
214
+ matches = re .findall (pattern , all_lines , re .DOTALL )
211
215
else :
212
- matches = re .findall (pattern , line )
216
+ matches = re .findall (pattern , all_lines )
213
217
if len (matches ) > 0 :
214
- return fn (line , pattern , replacement , matches )
215
- return line
218
+ return fn (all_lines , pattern , replacement , matches )
219
+ return all_lines
216
220
217
221
218
- def regex_replace_particle (line , pattern , replacement , matches ):
222
+ def regex_replace_particle (all_lines , pattern , replacement , matches ):
219
223
# matches == [(4, "foo", "bar"), (2, "baz", "bee")]
220
224
new = [item for tup in matches for item in tup ]
221
225
# new == [4, "foo", "bar", 2, "baz", "bee"]
@@ -225,38 +229,39 @@ def regex_replace_particle(line, pattern, replacement, matches):
225
229
new [1 ::3 ], new [2 ::3 ], new [0 ::3 ]
226
230
227
231
# new == ["foo", "bar", 4, "baz", "bee", 2]
228
- return re .sub (pattern , replacement , line ).format (* new )
232
+ return re .sub (pattern , replacement , all_lines ).format (* new )
229
233
230
234
231
- def regex_replace_sound_priority (line , pattern , replacement , matches ):
235
+ def regex_replace_sound_priority (all_lines , pattern , replacement , matches ):
232
236
# TODO: This pattern returns two items in each tuple, while we only need the first. Create a better pattern.
233
237
# https://stackoverflow.com/a/406408/13279557
234
238
# https://regex101.com/r/NdKaWs/2
235
239
236
240
# matches == [(4, "foo"), (2, "bar")]
237
241
new = [item for tup in matches for item in tup ][::2 ]
238
242
# new == [4, 2]
239
- return re .sub (pattern , replacement , line , flags = re .DOTALL ).format (* new )
243
+ return re .sub (pattern , replacement , all_lines , flags = re .DOTALL ).format (* new )
240
244
241
245
242
- def regex_use_capture (line , pattern , replacement , matches ):
243
- return re .sub (pattern , replacement , line ).format (* matches )
246
+ def regex_use_capture (all_lines , pattern , replacement , matches ):
247
+ return re .sub (pattern , replacement , all_lines ).format (* matches )
244
248
245
249
246
- # def regex_replace_playsound(line , pattern, replacement, matches):
247
- # return line
250
+ # def regex_replace_playsound(all_lines , pattern, replacement, matches):
251
+ # return all_lines
248
252
# # TODO:
249
253
# # AudioMan:PlaySound("ModName.rte/Folder/SoundName.wav", SceneMan:TargetDistanceScalar(self.Pos), false, true, -1)
250
254
# # to
251
255
# # AudioMan:PlaySound("ModName.rte/Folder/SoundName.wav", self.Pos) -- Cut everything and leave the thing inside the brackets after SceneMan:TargetDistanceScalar
252
256
253
257
254
- def regex_replace_bmps_and_wavs (line ):
255
- line = specific_replace (line , regex_use_capture , False , "Base\.rte(.*?)\.bmp" , "Base.rte{}.png" )
256
- line = specific_replace (line , regex_use_capture , False , "base\.rte(.*?)\.bmp" , "Base.rte{}.png" )
257
- line = specific_replace (line , regex_use_capture , False , "Base\.rte(.*?)\.wav" , "Base.rte{}.flac" )
258
- line = specific_replace (line , regex_use_capture , False , "base\.rte(.*?)\.wav" , "Base.rte{}.flac" )
259
- return line
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
260
265
261
266
262
267
def create_zips (input_folder_path , output_folder ):
@@ -266,21 +271,22 @@ def create_zips(input_folder_path, output_folder):
266
271
# TODO: Move check if it's a directory out of this loop.
267
272
folder_names = [f for f in os .listdir (input_folder_path ) if os .path .isdir (os .path .join (output_folder , f ))]
268
273
for mod_name in folder_names :
269
- create_single_zip (mod_name , output_folder )
274
+ if mod_name .endswith (".rte" ):
275
+ create_single_zip (mod_name , output_folder )
270
276
271
277
272
278
def create_single_zip (mod_name , output_folder ):
273
279
print ("Zipping '{}'" .format (mod_name ))
274
- folder_path = os .path .join (output_folder , mod_name )
275
- shutil .make_archive (folder_path , "zip" , root_dir = output_folder , base_dir = mod_name )
276
- shutil .rmtree (folder_path )
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 )
277
283
update_progress ()
278
284
279
285
280
286
def warnings_popup ():
281
287
if warnings_available :
282
288
w = max (30 , len (max (warnings , key = len )))
283
- h = min (50 , len (warnings )) + 1 # + 1 necessary because popup_scrolled adds an extra line
289
+ h = min (50 , len (warnings )) + 1 # + 1 necessary because popup_scrolled adds an extra line.
284
290
cfg .sg .popup_scrolled ("\n " .join (warnings ), title = "Lines needing manual replacing" , size = (w , h ), button_color = cfg .sg .theme_button_color (), background_color = cfg .sg .theme_background_color ())
285
291
286
292
0 commit comments