Skip to content

Commit 98a8e5a

Browse files
committed
Fixed regex by making it work across all lines instead of individual lines again. Added (hardcoded) version numbering to zips for mod.io. Folders and files that don't belong to mods in the input folder are now ignored.
1 parent d832e75 commit 98a8e5a

File tree

1 file changed

+67
-61
lines changed

1 file changed

+67
-61
lines changed

Python/convert.py

Lines changed: 67 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,13 @@ def convert():
7272

7373
for input_subfolder_path, input_subfolders, input_subfiles in os.walk(input_folder_path):
7474
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)
7675

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)
8082

8183
if cfg.sg.user_settings_get_entry("output_zips"):
8284
create_zips(input_folder_path, output_folder)
@@ -107,8 +109,10 @@ def get_total_progress(input_folder_path):
107109
if input_folder_path.endswith(".rte"):
108110
mod_count = 1
109111
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
112116
return mod_count * 2 if cfg.sg.user_settings_get_entry("output_zips") else mod_count
113117

114118

@@ -119,11 +123,9 @@ def get_mod_subfolder(input_folder_path, input_subfolder_path, true_input_folder
119123
return os.path.relpath(input_subfolder_path, input_folder_path)
120124

121125

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))
127129
update_progress()
128130

129131

@@ -140,14 +142,10 @@ def create_folder(input_subfolder_path, output_subfolder):
140142
pass
141143

142144

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):
144146
for full_filename in input_subfiles:
145147
filename, file_extension = os.path.splitext(full_filename)
146148

147-
# The ".empty" file exists so otherwise empty folders can be added to Git.
148-
if filename == ".empty":
149-
continue
150-
151149
input_file_path = os.path.join(input_subfolder_path, full_filename)
152150
output_file_path = os.path.join(output_subfolder, full_filename)
153151

@@ -167,55 +165,61 @@ def create_converted_file(input_file_path, output_file_path, input_folder_path):
167165
line_number = 0
168166
for line in file_in:
169167
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)
175168

176169
if warnings_available:
177170
for old_str, new_str in warning_rules.items():
178171
if old_str in line:
179172
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))
183183
except:
184184
shutil.copyfile(input_file_path, output_file_path)
185185

186186

187-
def regex_replace(line):
188-
line = simple_replace(line, "Framerate = (.*)", "SpriteAnimMode = 7")
189-
line = simple_replace(line, "\tPlayerCount = (.*)\n", "")
190-
line = simple_replace(line, "\tTeamCount = (.*)\n", "")
187+
def regex_replace(all_lines):
188+
all_lines = simple_replace(all_lines, "Framerate = (.*)", "SpriteAnimMode = 7")
189+
all_lines = simple_replace(all_lines, "\tPlayerCount = (.*)\n", "")
190+
all_lines = simple_replace(all_lines, "\tTeamCount = (.*)\n", "")
191191

192-
line = specific_replace(line, regex_replace_particle, False, "ParticleNumberToAdd = (.*)\n\tAddParticles = (.*)\n\t\tCopyOf = (.*)\n", "AddGib = Gib\n\t\tGibParticle = {}\n\t\t\tCopyOf = {}\n\t\tCount = {}\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\tAddParticles = (.*)\n\t\tCopyOf = (.*)\n", "AddGib = Gib\n\t\tGibParticle = {}\n\t\t\tCopyOf = {}\n\t\tCount = {}\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, "", "")
196200

197-
return line
201+
return all_lines
198202

199203

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)
202206
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
205209

206210

207-
def specific_replace(line, fn, dotall, pattern, replacement):
211+
def specific_replace(all_lines, fn, dotall, pattern, replacement):
208212
# TODO: Refactor so .findall takes re.DOTALL as an argument directly.
209213
if dotall:
210-
matches = re.findall(pattern, line, re.DOTALL)
214+
matches = re.findall(pattern, all_lines, re.DOTALL)
211215
else:
212-
matches = re.findall(pattern, line)
216+
matches = re.findall(pattern, all_lines)
213217
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
216220

217221

218-
def regex_replace_particle(line, pattern, replacement, matches):
222+
def regex_replace_particle(all_lines, pattern, replacement, matches):
219223
# matches == [(4, "foo", "bar"), (2, "baz", "bee")]
220224
new = [item for tup in matches for item in tup]
221225
# new == [4, "foo", "bar", 2, "baz", "bee"]
@@ -225,38 +229,39 @@ def regex_replace_particle(line, pattern, replacement, matches):
225229
new[1::3], new[2::3], new[0::3]
226230

227231
# 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)
229233

230234

231-
def regex_replace_sound_priority(line, pattern, replacement, matches):
235+
def regex_replace_sound_priority(all_lines, pattern, replacement, matches):
232236
# TODO: This pattern returns two items in each tuple, while we only need the first. Create a better pattern.
233237
# https://stackoverflow.com/a/406408/13279557
234238
# https://regex101.com/r/NdKaWs/2
235239

236240
# matches == [(4, "foo"), (2, "bar")]
237241
new = [item for tup in matches for item in tup][::2]
238242
# 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)
240244

241245

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)
244248

245249

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
248252
# # TODO:
249253
# # AudioMan:PlaySound("ModName.rte/Folder/SoundName.wav", SceneMan:TargetDistanceScalar(self.Pos), false, true, -1)
250254
# # to
251255
# # AudioMan:PlaySound("ModName.rte/Folder/SoundName.wav", self.Pos) -- Cut everything and leave the thing inside the brackets after SceneMan:TargetDistanceScalar
252256

253257

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
260265

261266

262267
def create_zips(input_folder_path, output_folder):
@@ -266,21 +271,22 @@ def create_zips(input_folder_path, output_folder):
266271
# TODO: Move check if it's a directory out of this loop.
267272
folder_names = [f for f in os.listdir(input_folder_path) if os.path.isdir(os.path.join(output_folder, f))]
268273
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)
270276

271277

272278
def create_single_zip(mod_name, output_folder):
273279
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)
277283
update_progress()
278284

279285

280286
def warnings_popup():
281287
if warnings_available:
282288
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.
284290
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())
285291

286292

0 commit comments

Comments
 (0)