-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
143 lines (98 loc) · 4.97 KB
/
convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import os, time, pathlib, shutil, math, re, sys, shutil
from conversion_rules import conversion_rules
def main():
time_start = time.time()
for input_folder_path, input_subfolders, full_filename_list in os.walk("input"):
output_folder = get_output_folder_path(input_folder_path)
try_print_mod_name(input_folder_path)
create_folder(input_folder_path, output_folder)
process_file(full_filename_list, input_folder_path, output_folder)
elapsed = math.floor(time.time() - time_start)
print("Finished in {} {}".format(elapsed, pluralize("second", elapsed)))
def get_output_folder_path(input_folder_path):
return os.path.join("output", pathlib.Path(*pathlib.Path(input_folder_path).parts[1:]))
def try_print_mod_name(input_folder_path):
input_folder_path_tuple = pathlib.Path(input_folder_path).parts
if len(input_folder_path_tuple) == 2:
print("Converting '{}'".format(input_folder_path_tuple[1]))
def create_folder(input_folder_path, output_folder):
# Prevents putting the "input" folder itself into the "output" folder.
if input_folder_path != "input":
try:
os.makedirs(output_folder)
except:
raise SystemExit("^ Stopped, because the above mod was already present in the 'output' folder;\
\n remove it from the 'output' folder before running convert.py.")
def process_file(full_filename_list, input_folder_path, output_folder):
for full_filename in full_filename_list:
filename, file_extension = os.path.splitext(full_filename)
# The ".empty" file exists so otherwise empty folders can be added to Git.
if filename == ".empty":
continue
input_file_path = os.path.join(input_folder_path, full_filename)
output_file_path = os.path.join(output_folder, full_filename)
if file_extension in (".ini", ".lua"):
create_converted_file(input_file_path, output_file_path)
else:
shutil.copyfile(input_file_path, output_file_path)
def create_converted_file(input_file_path, output_file_path):
try:
with open(input_file_path, "r") as file_in:
with open(output_file_path, "w") as file_out:
all_lines = regex_replace(file_in.read())
for old_str, new_str in conversion_rules.items():
all_lines = all_lines.replace(old_str, new_str)
file_out.write(all_lines)
except:
shutil.copyfile(input_file_path, output_file_path)
def regex_replace(all_lines):
all_lines = simple_replace(all_lines, "Framerate = (.*)", "SpriteAnimMode = 7")
all_lines = simple_replace(all_lines, "\tPlayerCount = (.*)\n", "")
all_lines = simple_replace(all_lines, "\tTeamCount = (.*)\n", "")
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")
all_lines = specific_replace(all_lines, regex_replace_sound_priority, True, " Sound(((?! Sound).)*)Priority", " Sound{}// Priority")
all_lines = specific_replace(all_lines, regex_replace_fundsofteam, False, "FundsOfTeam(.*) =", "Team{}Funds =")
# all_lines = specific_replace(all_lines, regex_replace_playsound, False, "", "")
return all_lines
def specific_replace(all_lines, fn, dotall, pattern, replacement):
if dotall:
matches = re.findall(pattern, all_lines, re.DOTALL)
else:
matches = re.findall(pattern, all_lines)
if len(matches) > 0:
return fn(all_lines, pattern, replacement, matches)
return all_lines
def regex_replace_particle(all_lines, pattern, replacement, matches):
# matches == [(4, "foo", "bar"), (2, "baz", "bee")]
new = [item for tup in matches for item in tup]
# new == [4, "foo", "bar", 2, "baz", "bee"]
# 0, 1, 2 -> 1, 2, 0
new[0::3], new[1::3], new[2::3] = \
new[1::3], new[2::3], new[0::3]
# new == ["foo", "bar", 4, "baz", "bee", 2]
return re.sub(pattern, replacement, all_lines).format(*new)
def regex_replace_sound_priority(all_lines, pattern, replacement, matches):
# TODO: This pattern returns two items in each tuple, while we only need the first. Create a better pattern.
# https://stackoverflow.com/a/406408/13279557
# https://regex101.com/r/NdKaWs/2
# matches == [(4, "foo"), (2, "bar")]
new = [item for tup in matches for item in tup][::2]
# new == [4, 2]
return re.sub(pattern, replacement, all_lines, flags=re.DOTALL).format(*new)
def regex_replace_fundsofteam(all_lines, pattern, replacement, matches):
return re.sub(pattern, replacement, all_lines).format(*matches)
# def regex_replace_playsound(all_lines):
# return all_lines
# # TODO:
# # AudioMan:PlaySound("ModName.rte/Folder/SoundName.wav", SceneMan:TargetDistanceScalar(self.Pos), false, true, -1)
# # to
# # AudioMan:PlaySound("ModName.rte/Folder/SoundName.wav", self.Pos) -- Cut everything and leave the thing inside the brackets after SceneMan:TargetDistanceScalar
def simple_replace(all_lines, pattern, replacement):
matches = re.findall(pattern, all_lines)
if len(matches) > 0:
return re.sub(pattern, replacement, all_lines)
return all_lines
def pluralize(word, count):
return word + "s" if count != 1 else word
if __name__ == "__main__":
main()