-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAFModCompiler.py
260 lines (209 loc) · 9.31 KB
/
AFModCompiler.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from pathlib import Path
import json
import argparse
import sys
parser = argparse.ArgumentParser(description='Process a mod project')
parser.add_argument('--output, -o', type=str, nargs='?',
default='mod_output.json', dest='output_file',
help='Set the destination file that the mod will be compiled to')
parser.add_argument('project_directory', metavar='Directory', type=str, nargs='?',
default='.', help='Directory that will be processed')
parser.add_argument('--max-projectile-depth', type=int, nargs='?',
default=20, dest='max_projectile_depth',
help='Sets the maximum depth projectiles will generate ' +
'in the case of recursion or sufficiently long projectile chains')
parser.add_argument('--max-parent-depth', type=int, nargs='?',
default=50, dest='max_parent_depth',
help='Sets the maximum depth parents will be searched to ' +
'generate in the case of nested parent chains and infinite loops')
parser.add_argument('--random', action='store_true',
help='Randomizes projectiles used for projectile templates')
args = parser.parse_args()
if args.random:
from random import randint
def get_jsons(directory):
if not directory.is_dir():
return None
files = []
for file in directory.iterdir():
if file.is_dir():
files += get_jsons(file)
elif file.suffix == ".json":
files.append(file)
return files
def get_data(directory):
returned_data = {}
data_files = get_jsons(directory)
if len(data_files) == 0:
return None
for file in data_files:
file_json = parse_json_from_file(file)
data_name = file.stem
returned_data[data_name] = file_json.copy()
return returned_data
def get_constants(directory):
constant_info = {}
# AI Javascript
ai_file = directory.joinpath("ai.js")
if ai_file.exists() and not ai_file.is_dir():
constant_info["Code"] = ai_file.read_text()
# Audio constants
audio_file = directory.joinpath("audio.json")
audio_data = parse_json_from_file(audio_file)
if audio_data:
constant_info["Audio"] = audio_data
# Choices constants
choices_file = directory.joinpath("choices.json")
choices_data = parse_json_from_file(choices_file)
if choices_data:
constant_info["Choices"] = parse_json_from_file(choices_file)
# Hero constants
hero_file = directory.joinpath("hero.json")
hero_data = parse_json_from_file(hero_file)
if hero_data:
constant_info["Hero"] = parse_json_from_file(hero_file)
# Matchmaking constants
matchmaking_file = directory.joinpath("matchmaking.json")
matchmaking_data = parse_json_from_file(matchmaking_file)
if matchmaking_data:
constant_info["Matchmaking"] = parse_json_from_file(matchmaking_file)
# Obstacle constants
obstacle_file = directory.joinpath("obstacle.json")
obstacle_data = parse_json_from_file(obstacle_file)
if obstacle_data:
constant_info["Obstacle"] = parse_json_from_file(obstacle_file)
# Tips list
tips_file = directory.joinpath("tips.json")
tips_data = parse_json_from_file(tips_file)
if tips_data:
constant_info["Tips"] = parse_json_from_file(tips_file)
# Visuals constants
visuals_file = directory.joinpath("visuals.json")
visuals_data = parse_json_from_file(visuals_file)
if visuals_data:
constant_info["Visuals"] = parse_json_from_file(visuals_file)
# World constants
world_file = directory.joinpath("world.json")
world_data = parse_json_from_file(world_file)
if world_data:
constant_info["World"] = parse_json_from_file(world_file)
return constant_info
def parse_json_from_file(path):
returned_value = None
if path.exists() and not path.is_dir():
try:
returned_value = json.loads(path.read_text())
except json.decoder.JSONDecodeError as e:
print(f"JSON Error in {path.resolve()}\n{e}")
sys.exit(1)
return returned_value
def template_projectile(projectile, projectiles):
projectile_template = ""
if not args.random:
projectile_template = projectile.replace("ProjectileTemplate:", "")
else:
projectile_template = list(projectiles.keys())[randint(0, len(projectiles)-1)]
return projectiles[projectile_template].copy()
def template_projectile_spawners(projectile, projectiles, spawner_depth = 0):
returned_projectile = projectile.copy()
if not returned_projectile.get("behaviours"):
return returned_projectile
popped_items = []
for behaviour_index, behaviour_data in reversed(list(enumerate(returned_projectile["behaviours"].copy()))):
if behaviour_data["type"] == "spawn" and type(behaviour_data.get("projectile")) is str:
if spawner_depth < args.max_projectile_depth:
projectile_template = ""
if not args.random:
projectile_template = behaviour_data["projectile"].replace("ProjectileTemplate:", "")
else:
projectile_template = list(projectiles.keys())[randint(0, len(projectiles)-1)]
returned_projectile["behaviours"][behaviour_index]["projectile"] = template_projectile_spawners(projectiles[projectile_template], projectiles, spawner_depth + 1).copy()
else:
popped_items.append(behaviour_index)
continue
returned_projectile["behaviours"] = [behaviour.copy() for index, behaviour in enumerate(returned_projectile["behaviours"]) if index not in popped_items]
return returned_projectile
def process_projectiles(projectiles):
returned_projectiles = process_parents(projectiles)
for projectile_index, projectile_data in returned_projectiles.items():
returned_projectiles[projectile_index] = template_projectile_spawners(projectile_data, returned_projectiles)
return returned_projectiles
def process_spells(spells, projectiles):
returned_spells = process_parents(spells)
for spell_index, spell_data in returned_spells.items():
if spell_data.get("projectile") and type(spell_data.get("projectile")) is str:
spell_data["projectile"] = template_projectile(spell_data["projectile"], projectiles)
if spell_data.get("releaseBehaviours"):
for behaviour in spell_data["releaseBehaviours"]:
if behaviour["type"] == "spawn" and type(behaviour.get("projectile")) is str:
behaviour["projectile"] = template_projectile(behaviour["projectile"], projectiles)
if spell_data.get("behaviours"):
for behaviour in spell_data["behaviours"]:
if behaviour["type"] == "spawn" and type(behaviour.get("projectile")) is str:
behaviour["projectile"] = template_projectile(behaviour["projectile"], projectiles)
return returned_spells
def process_parents(data):
returned_data = data.copy()
# Start parenting projectiles
data_remains = True
nested_parent_attempts = 0
while nested_parent_attempts < args.max_parent_depth and data_remains:
data_remains = False
for data_index, data in returned_data.items():
parent_data = data.get("basedOn")
if parent_data:
if not returned_data.get(parent_data):
raise(f"{data['basedOn']} does not exist")
if returned_data[parent_data].get("basedOn"):
data_remains = True
nested_parent_attempts += 1
continue
else:
returned_data[data_index] = dict(returned_data[parent_data].copy(), **data)
returned_data[data_index].pop("basedOn")
else:
returned_data[data_index] = data.copy()
if nested_parent_attempts == args.max_parent_depth:
raise("Timed out on parenting data. Infinite loop?")
return returned_data
def main():
project_directory = Path(args.project_directory)
# Process all the mod data
mod_path = project_directory.joinpath("mod.json")
constant_path = project_directory.joinpath("constants")
projectile_path = project_directory.joinpath("projectiles")
spell_path = project_directory.joinpath("spells")
obstacle_path = project_directory.joinpath("obstacles")
map_path = project_directory.joinpath("maps")
sound_path = project_directory.joinpath("sounds")
icon_path = project_directory.joinpath("icons")
mod_info = parse_json_from_file(mod_path) if mod_path.exists() and not mod_path.is_dir() else None
constant_info = get_constants(constant_path) if constant_path.exists() and constant_path.is_dir() else None
processed_projectiles = process_projectiles(get_data(projectile_path)) if projectile_path.exists() and projectile_path.is_dir() else None
processed_spells = process_spells(get_data(spell_path), processed_projectiles) if spell_path.exists() and spell_path.is_dir() else None
processed_obstacles = process_parents(get_data(obstacle_path)) if obstacle_path.exists() and obstacle_path.is_dir() else None
processed_maps = process_parents(get_data(map_path)) if map_path.exists() and map_path.is_dir() else None
processed_sounds = process_parents(get_data(sound_path)) if sound_path.exists() and sound_path.is_dir() else None
processed_icons = process_parents(get_data(icon_path)) if icon_path.exists() and icon_path.is_dir() else None
# Format the mod data into a json
mod_data = {}
if mod_info:
mod_data["Mod"] = mod_info
for constant_index, constant_values in constant_info.items():
mod_data[constant_index] = constant_values
if processed_spells:
mod_data["Spells"] = processed_spells
if processed_maps:
mod_data["Layouts"] = processed_maps
if processed_obstacles:
mod_data["ObstacleTemplates"] = processed_obstacles
if processed_sounds:
mod_data["Sounds"] = processed_sounds
if processed_icons:
mod_data["Icons"] = processed_icons
# Export the mod data to the specified file
mod_file = Path(args.output_file)
mod_json = json.dumps(mod_data, indent=4)
mod_file.write_text(str(mod_json))
if __name__ == "__main__":
main()