Skip to content

Commit 41bd3bd

Browse files
committed
Grouped Python functions in new Python files.
Not copying desktop.ini file anymore as it causes the converter to crash. Added more conversion rules.
1 parent 2293f05 commit 41bd3bd

File tree

9 files changed

+168
-131
lines changed

9 files changed

+168
-131
lines changed

ConversionRules/Images.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,6 @@
171171
"Coalition.rte/Devices/Sprites/BulletGrenadeA.bmp": "Coalition.rte/Devices/Weapons/AutoCannon/AutoCannonShot.png",
172172
"Coalition.rte/Devices/Sprites/MagazineAutoCannonA.bmp": "Coalition.rte/Devices/Weapons/AutoCannon/AutoCannonMagazine.png",
173173
"Coalition.rte/Devices/Sprites/AutoCannonA.bmp": "Coalition.rte/Devices/Weapons/AutoCannon/AutoCannon.png",
174+
"Missions.rte/Scenes/Items/BombMaker.bmp": "Missions.rte/Objects/BombMaker/BombMaker.png",
175+
"Missions.rte/Scenes/Items/ZombieGenerator.bmp": "Missions.rte/Objects/ZombieGenerator/ZombieGenerator.png",
174176
}

ConversionRules/Misc.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
"10 oz Gold Brick": "10oz Gold Brick",
118118
"24 oz Gold Brick": "24oz Gold Brick",
119119
"Nailer Cannon": "Nailer Machinegun",
120-
"Silver Man": "Techion Silver Man",
120+
"\"Silver Man\"": "\"Techion Silver Man\"",
121121
"Nanorifle": "Nano Rifle",
122122
"Ronin Sniper": "Ronin Soldier",
123123
"Scouting Robot": "All Purpose Robot",
@@ -192,4 +192,10 @@
192192
"IsPlaying(": "IsBeingPlayed(",
193193
"Dummy.rte/Devices/Sprites/ShieldWall.bmp": "Base.rte/Null.png",
194194
"Dummy.rte/Devices/Sprites/ShieldWallDent.bmp": "Base.rte/Null.png",
195+
"DrawWhenOpen": "DrawMaterialLayerWhenOpen",
196+
"DrawWhenClosed": "DrawMaterialLayerWhenClosed",
197+
"Flame 1 Hurt Emitter": "Flame 1 Hurt",
198+
"Flame 2 Hurt Emitter": "Flame 2 Hurt",
199+
"Jetpack Silver Man": "Techion Jetpack",
200+
"math.mod(": "math.fmod(",
195201
}

ConversionRules/Sounds.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"Techion.rte/Effects/Sounds/Nanoswarm.wav": "Techion.rte/Devices/Explosives/NanoSwarmGrenade/Sounds/NanoSwarm.flac",
9898
"Base.rte/Actors/Foomph.wav": "Base.rte/Sounds/Physics/Foomph.flac",
9999
"Base.rte/Actors/Flumph.wav": "Base.rte/Sounds/Physics/Flumph.flac",
100+
"Base.rte/Actors/Chomp.wav": "Base.rte/Sounds/Physics/Chomp.flac",
100101
"Base.rte/Actors/BoneCrackA.wav": "Base.rte/Sounds/Physics/BoneCrack1.flac",
101102
"Base.rte/Actors/BoneCrackB.wav": "Base.rte/Sounds/Physics/BoneCrack2.flac",
102103
"Base.rte/Actors/BoneCrackC.wav": "Base.rte/Sounds/Physics/BoneCrack3.flac",
@@ -109,7 +110,17 @@
109110
"Base.rte/Actors/BoneCrackJ.wav": "Base.rte/Sounds/Physics/BoneCrack10.flac",
110111
"Base.rte/Actors/BoneCrackK.wav": "Base.rte/Sounds/Physics/BoneCrack11.flac",
111112
"Base.rte/Actors/BoneCrackL.wav": "Base.rte/Sounds/Physics/BoneCrack12.flac",
112-
"Base.rte/Actors/Chomp.wav": "Base.rte/Sounds/Physics/Chomp.flac",
113+
"Base.rte/Sounds/BoneSnapA.wav": "Base.rte/Sounds/Physics/BoneSnap1.flac",
114+
"Base.rte/Sounds/BoneSnapD.wav": "Base.rte/Sounds/Physics/BoneSnap1.flac",
115+
"Base.rte/Sounds/BoneSnapB.wav": "Base.rte/Sounds/Physics/BoneSnap2.flac",
116+
"Base.rte/Actors/BoneSnapB.wav": "Base.rte/Sounds/Physics/BoneSnap2.flac",
117+
"Base.rte/Sounds/Dank.wav": "Base.rte/Sounds/Physics/BoneSnap2.flac",
118+
"Base.rte/Sounds/BoneSnapC.wav": "Base.rte/Sounds/Physics/BoneSnap3.flac",
119+
"Base.rte/Sounds/BoneHitA.wav": "Base.rte/Sounds/Penetration/BoneHit1.flac",
120+
"Base.rte/Sounds/BoneHitB.wav": "Base.rte/Sounds/Penetration/BoneHit2.flac",
121+
"Base.rte/Sounds/BoneHitC.wav": "Base.rte/Sounds/Penetration/BoneHit3.flac",
122+
"Base.rte/Sounds/BoneHitD.wav": "Base.rte/Sounds/Penetration/BoneHit4.flac",
123+
"Base.rte/Actors/BoneHitD.wav": "Base.rte/Sounds/Penetration/BoneHit4.flac",
113124
"Techion.rte/Effects/Sounds/LaserFire.wav": "Techion.rte/Devices/Weapons/PulseRifle/Sounds/Fire1.flac",
114125
"Techion.rte/Effects/Sounds/LaserReloadStart.wav": "Techion.rte/Devices/Weapons/PulseRifle/Sounds/ReloadStart.flac",
115126
"Techion.rte/Effects/Sounds/LaserReloadEnd.wav": "Techion.rte/Devices/Weapons/PulseRifle/Sounds/ReloadEnd.flac",
@@ -129,4 +140,5 @@
129140
"Ronin.rte/Effects/Sounds/TommyFire.wav": "Ronin.rte/Devices/Weapons/AK47/Sounds/Fire1.flac",
130141
"Ronin.rte/Effects/Sounds/ThumperFire.wav": "Ronin.rte/Devices/Weapons/M79/Sounds/Fire1.flac",
131142
"Coalition.rte/Effects/Sounds/AutoCannonFire.wav": "Coalition.rte/Devices/Weapons/AutoCannon/Sounds/Fire1.flac",
143+
"Base.rte/Sounds/LightFlamerFire.wav": "Browncoats.rte/Devices/Weapons/Heatlance/Sounds/Loop.flac",
132144
}

Python/convert.py

Lines changed: 15 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import os, time, shutil, math, re, sys, shutil, zipfile, json, pathlib, webbrowser
1+
import os, time, shutil, math, sys, zipfile, json, pathlib, webbrowser
22
from pathlib import Path
33
from jsoncomment import JsonComment
44
from playsound import playsound
55

66
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
710

8-
progress = 0
9-
total_progress = 0
1011

1112
warnings_file_name = "Warnings.json"
1213
warnings_path = os.path.join("ConversionRules", warnings_file_name)
@@ -59,7 +60,7 @@ def check_github_button_clicked_and_exit(clicked_github_button):
5960

6061

6162
def convert():
62-
global progress, total_progress, output_folder, warnings
63+
global output_folder, warnings
6364

6465
time_start = time.time()
6566

@@ -68,7 +69,7 @@ def convert():
6869

6970
unzip(input_folder_path)
7071

71-
total_progress = get_total_progress(input_folder_path)
72+
update_progress.set_max_progress(input_folder_path)
7273

7374
for input_subfolder_path, input_subfolders, input_subfiles in os.walk(input_folder_path):
7475
mod_subfolder = get_mod_subfolder(input_folder_path, input_subfolder_path, true_input_folder_path)
@@ -86,8 +87,8 @@ def convert():
8687
if len(warnings) > 0:
8788
warnings_popup()
8889

89-
progress = 0
90-
total_progress = 0
90+
update_progress.progress = 0
91+
update_progress.total_progress = 0
9192
warnings = []
9293

9394
elapsed = math.floor(time.time() - time_start)
@@ -105,17 +106,6 @@ def unzip(input_folder_path):
105106
os.remove(zip_path)
106107

107108

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-
119109
def get_mod_subfolder(input_folder_path, input_subfolder_path, true_input_folder_path):
120110
if input_folder_path.endswith(".rte"):
121111
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
126116
def try_print_mod_name(mod_subfolder_parts, mod_subfolder):
127117
if len(mod_subfolder_parts) == 1:
128118
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()
136120

137121

138122
def create_folder(input_subfolder_path, output_subfolder):
@@ -149,14 +133,17 @@ def process_files(input_subfiles, input_subfolder_path, output_subfolder, input_
149133
input_file_path = os.path.join(input_subfolder_path, full_filename)
150134
output_file_path = os.path.join(output_subfolder, full_filename)
151135

136+
if full_filename == "desktop.ini":
137+
continue
138+
152139
if file_extension in (".ini", ".lua"):
153140
create_converted_file(input_file_path, output_file_path, input_folder_path)
154141
else:
155142
shutil.copyfile(input_file_path, output_file_path)
156143

157144

158145
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.
160147
with open(input_file_path, "r") as file_in:
161148
with open(output_file_path, "w") as file_out:
162149
all_lines_list = []
@@ -178,111 +165,12 @@ def create_converted_file(input_file_path, output_file_path, input_folder_path):
178165
for old_str, new_str in conversion_rules.items():
179166
all_lines = all_lines.replace(old_str, new_str)
180167

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))
183170
except:
184171
shutil.copyfile(input_file_path, output_file_path)
185172

186173

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", "")
191-
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, "", "")
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-
286174
def warnings_popup():
287175
if warnings_available:
288176
w = max(30, len(max(warnings, key=len)))

Python/gui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def init_window():
8686
cfg.sg = sg
8787
convert.load_conversion_and_warning_rules()
8888

89-
window = sg.Window("Legacy Mod Converter - v1.0", layout, icon=resource_path("Media/cclmc-icon.ico"))
89+
window = sg.Window("Legacy Mod Converter", layout, icon=resource_path("Media/cclmc-icon.ico"))
9090
cfg.progress_bar = window["-PROGRESS BAR-"]
9191

9292
return window

Python/regex_rules.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import re
2+
3+
4+
def regex_replace(all_lines):
5+
all_lines = simple_replace(all_lines, "Framerate = (.*)", "SpriteAnimMode = 7")
6+
all_lines = simple_replace(all_lines, "\tPlayerCount = (.*)\n", "")
7+
all_lines = simple_replace(all_lines, "\tTeamCount = (.*)\n", "")
8+
9+
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")
10+
11+
all_lines = specific_replace(all_lines, regex_replace_sound_priority, True, "SoundContainer(((?!SoundContainer).)*)Priority", "SoundContainer{}// Priority")
12+
13+
# all_lines = specific_replace(all_lines, regex_replace_sound_priority, True, "AddSound(((?! AddSound).)*)Priority", "AddSound{}// Priority")
14+
15+
all_lines = specific_replace(all_lines, regex_use_capture, False, "FundsOfTeam(.*) =", "Team{}Funds =")
16+
# all_lines = specific_replace(all_lines, regex_replace_playsound, False, "", "")
17+
18+
return all_lines
19+
20+
21+
def simple_replace(all_lines, pattern, replacement):
22+
matches = re.findall(pattern, all_lines)
23+
if len(matches) > 0:
24+
return re.sub(pattern, replacement, all_lines)
25+
return all_lines
26+
27+
28+
def specific_replace(all_lines, fn, dotall, pattern, replacement):
29+
# TODO: Refactor so .findall takes re.DOTALL as an argument directly.
30+
if dotall:
31+
matches = re.findall(pattern, all_lines, re.DOTALL)
32+
else:
33+
matches = re.findall(pattern, all_lines)
34+
if len(matches) > 0:
35+
return fn(all_lines, pattern, replacement, matches)
36+
return all_lines
37+
38+
39+
def regex_replace_particle(all_lines, pattern, replacement, matches):
40+
# matches == [(4, "foo", "bar"), (2, "baz", "bee")]
41+
new = [item for tup in matches for item in tup]
42+
# new == [4, "foo", "bar", 2, "baz", "bee"]
43+
44+
# 0, 1, 2 -> 1, 2, 0
45+
new[0::3], new[1::3], new[2::3] = \
46+
new[1::3], new[2::3], new[0::3]
47+
48+
# new == ["foo", "bar", 4, "baz", "bee", 2]
49+
return re.sub(pattern, replacement, all_lines).format(*new)
50+
51+
52+
def regex_replace_sound_priority(all_lines, pattern, replacement, matches):
53+
# TODO: This pattern returns two items in each tuple, while we only need the first. Create a better pattern.
54+
# https://stackoverflow.com/a/406408/13279557
55+
# https://regex101.com/r/NdKaWs/2
56+
57+
# matches == [(4, "foo"), (2, "bar")]
58+
new = [item for tup in matches for item in tup][::2]
59+
# new == [4, 2]
60+
return re.sub(pattern, replacement, all_lines, flags=re.DOTALL).format(*new)
61+
62+
63+
def regex_use_capture(all_lines, pattern, replacement, matches):
64+
return re.sub(pattern, replacement, all_lines).format(*matches)
65+
66+
67+
# def regex_replace_playsound(all_lines, pattern, replacement, matches):
68+
# return all_lines
69+
# # TODO:
70+
# # AudioMan:PlaySound("ModName.rte/Folder/SoundName.wav", SceneMan:TargetDistanceScalar(self.Pos), false, true, -1)
71+
# # to
72+
# # AudioMan:PlaySound("ModName.rte/Folder/SoundName.wav", self.Pos) -- Cut everything and leave the thing inside the brackets after SceneMan:TargetDistanceScalar
73+
74+
75+
def regex_replace_bmps_and_wavs(all_lines):
76+
# TODO: Combine these four patterns into two.
77+
all_lines = specific_replace(all_lines, regex_use_capture, False, "Base\.rte(.*?)\.bmp", "Base.rte{}.png")
78+
all_lines = specific_replace(all_lines, regex_use_capture, False, "base\.rte(.*?)\.bmp", "Base.rte{}.png")
79+
all_lines = specific_replace(all_lines, regex_use_capture, False, "Base\.rte(.*?)\.wav", "Base.rte{}.flac")
80+
all_lines = specific_replace(all_lines, regex_use_capture, False, "base\.rte(.*?)\.wav", "Base.rte{}.flac")
81+
return all_lines

Python/update_progress.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
3+
from Python import shared_globals as cfg
4+
5+
6+
progress = 0
7+
total_progress = 0
8+
9+
10+
def increment_progress():
11+
global progress # TODO: See what happens when this line is removed.
12+
progress += 1
13+
cfg.progress_bar.UpdateBar(progress % total_progress, total_progress)
14+
15+
16+
def set_max_progress(input_folder_path):
17+
global total_progress # TODO: See what happens when this line is removed.
18+
if input_folder_path.endswith(".rte"):
19+
mod_count = 1
20+
else:
21+
mod_count = 0
22+
for mod_name in os.listdir(input_folder_path):
23+
if os.path.isdir(os.path.join(input_folder_path, mod_name)) and mod_name.endswith(".rte"):
24+
mod_count += 1
25+
total_progress = mod_count * 2 if cfg.sg.user_settings_get_entry("output_zips") else mod_count

0 commit comments

Comments
 (0)