forked from Pizzaandy/EternalEncounterBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EBL_compiler.py
365 lines (317 loc) · 10.5 KB
/
EBL_compiler.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import eternalevents as ee
import EBL_grammar as EBL
from textwrap import indent
from dataclasses import dataclass
import re
import time
ebl = EBL.NodeVisitor()
ebl.grammar = EBL.grammar
variables = {}
debug_vars = False
@dataclass
class EBL_Assignment():
name: str
value: str
def debug_print(string):
if debug_vars:
print(string)
def add_variable(varname, value):
if varname in variables:
debug_print(f"Modified variable {varname} = {value}")
else:
debug_print(f"Added variable {varname} = {value}")
minimum_chars = 0
prefixed = False
if isinstance(value, str):
prefixed = "@" in value
minimum_chars = len(str(value)) if not prefixed else 0
items = variables.items()
sorted_variables = sorted(items, key=lambda x: len(x[0]), reverse=True)
for var, val in sorted_variables:
if len(var) < minimum_chars:
continue
if not isinstance(value, str):
continue
val = format_args([val], 1)[0]
old_value = value
value = value.replace(f'{var}', str(val))
if value != old_value:
debug_print(f"Substituted {var} = {value} into assignment {varname}")
if prefixed:
variables[varname] = value.replace("@", "")
debug_print(f'''Flattened nested prefix {varname} = {value.replace("@","")}''')
return
variables[varname] = format_args([value], 1)[0]
waitFor_keywords = {
"all": "ENCOUNTER_LOGICAL_OP_AND",
"any": "ENCOUNTER_LOGICAL_OP_OR"
}
# ENCOUNTER_SPAWN + name
encounter_spawn_names = [
"ANY",
"GENERIC",
"ARACHNOTRON",
"ARMORED_BARON",
"BARON",
"BLOOD_ANGEL",
"CACODEMON",
"CHAINGUN_SOLDIER",
"CUEBALL",
"CURSED_PROWLER",
"CYBER_MANCUBUS",
"DOOM_HUNTER",
"DREAD_KNIGHT",
"GARGOYLE",
"HELL_KNIGHT",
"HELL_SOLDIER",
"IMP",
"MANCUBUS",
"MARAUDER",
"PAIN_ELEMENTAL",
"PINKY",
"PROWLER",
"REVENANT",
"SHOTGUN_SOLDIER",
"STONE_IMP",
"TENTACLE",
"TYRANT",
"WHIPLASH",
"ZOMBIE_MAYKR",
"ZOMBIE_T1_SCREECHER",
"ZOMBIE_TIER_1",
"ZOMBIE_TIER_3",
"LOST_SOUL",
"SPECTRE",
"CARCASS",
"ARCHVILE",
"BUFF_POD",
"SPIRIT",
"TURRET",
"SUPER_TENTACLE",
]
# These are the REAL constants you should use
encounter_spawn_aliases = {
"Any": "ANY",
"Generic": "GENERIC",
"Arachnotron": "ARACHNOTRON",
"ArmoredBaron": "ARMORED_BARON",
"Spider": "ARACHNOTRON",
"Baron": "BARON",
"FireborneBaron": "BARON",
"BloodAngel": "BLOOD_ANGEL",
"BloodMaykr": "BLOOD_ANGEL",
"Cacodemon": "CACODEMON",
"ChaingunSoldier": "CHAINGUN_SOLDIER",
"Chaingunner": "CHAINGUN_SOLDIER",
"RiotSoldier": "CHAINGUN_SOLDIER",
"Cueball": "CUEBALL",
"CursedProwler": "CURSED_PROWLER",
"CyberMancubus": "CYBER_MANCUBUS",
"DoomHunter": "DOOM_HUNTER",
"DreadKnight": "DREAD_KNIGHT",
"Gargoyle": "GARGOYLE",
"HellKnight": "HELL_KNIGHT",
"HellSoldier": "HELL_SOLDIER",
"ZombieMan": "HELL_SOLDIER",
"Imp": "IMP",
"Mancubus": "MANCUBUS",
"Marauder": "MARAUDER",
"PainElemental": "PAIN_ELEMENTAL",
"Pinky": "PINKY",
"Prowler": "PROWLER",
"Revenant": "REVENANT",
"ShotgunSoldier": "SHOTGUN_SOLDIER",
"Shotgunner": "SHOTGUN_SOLDIER",
"ShotgunGuy": "SHOTGUN_SOLDIER",
"ShieldGuy": "SHOTGUN_SOLDIER",
"StoneImp": "STONE_IMP",
"SIMP": "STONE_IMP",
"Tentacle": "TENTACLE",
"Tyrant": "TYRANT",
"Cyberdemon": "TYRANT",
"Whiplash": "WHIPLASH",
"ZombieMaykr": "ZOMBIE_MAYKR",
"MaykrDrone": "ZOMBIE_MAYKR",
"ZombieT1Screecher": "ZOMBIE_T1_SCREECHER",
"Screecher": "ZOMBIE_T1_SCREECHER",
"ZombieTier1": "ZOMBIE_TIER_1",
"Zombie": "ZOMBIE_TIER_1",
"ZombieTier3": "ZOMBIE_TIER_3",
"MechaZombie": "ZOMBIE_TIER_3",
"LostSoul": "LOST_SOUL",
"Spectre": "SPECTRE",
"Carcass": "CARCASS",
"BigChungus": "CARCASS",
"Archvile": "ARCHVILE",
"BuffPod": "BUFF_POD",
"BuffTotem": "BUFF_POD",
"Spirit": "SPIRIT",
"Turret": "TURRET",
"SuperTentacle": "SUPER_TENTACLE",
}
def str_to_class(classname):
return getattr(ee, classname)
def get_event_args(classname):
return ([i for i in classname.__dict__.keys()
if not i.startswith('__')
and not i.startswith('args')])
def strip_comments(string):
pattern = r"//(.*)[\r\n]+"
return re.sub(pattern, "", string)
# Splits EBL file into segments at REPLACE ENCOUNTER headers
# and also handles SETTINGS flags
def generate_EBL_segments(filename, format_file=True):
with open(filename) as fp:
segments = re.split(r"^REPLACE ENCOUNTER", fp.read(), flags=re.MULTILINE)
start_index = 0
if segments[0].startswith("SETTINGS"):
print("SETTINGS header found!")
start_index = 1
if format_file:
format_entities_file(filename, segments[0])
else:
print("Settings Ignored lol")
else:
print("No SETTINGS found")
for segment in segments[start_index:]:
segment = "\n".join(segment.split("\n")[1:])
yield strip_comments(segment)
# Handles macros and fills in missing arguments
def format_args(args, arg_count):
for i, arg in enumerate(args):
if isinstance(arg, str):
args[i] = ""
for word in arg.split():
if word in encounter_spawn_names:
args[i] += "ENCOUNTER_SPAWN_" + word + " "
elif word in encounter_spawn_aliases:
args[i] += ("ENCOUNTER_SPAWN_" +
encounter_spawn_aliases[word] + " ")
else:
args[i] += word + " "
args[i] = args[i].strip()
if arg is None:
args[i] = ""
while len(args) < arg_count:
args += [""]
return args
# Consumes a parsed EBL file and generates a list of EternalEvents
def create_events(data):
if isinstance(data, list):
output = []
for item in data:
event = create_events(item)
output += event if event is not None else []
return output
if isinstance(data, dict):
if "variable" in data:
return [EBL_Assignment(data["variable"], data["value"])]
if data["event"] == "waitForBlock":
length = len([ev for ev in data["args"] if not "variable" in ev[0]])
waitevent = {
"event": "waitMulitpleConditions",
"args": [length, waitFor_keywords[data["keyword"]], "false"]
}
print(f'{data["keyword"]}: {waitFor_keywords[data["keyword"]]}')
return create_events([waitevent] + data["args"])
if data["event"] == "waitFor":
return create_events(data["args"])
if data["event"] in ee.ebl_to_event:
cls_name, arg_count = ee.ebl_to_event[data["event"]]
event_cls = str_to_class(cls_name)
else:
print(f'''ERROR: undefined event {data["event"]}!''')
args_list = data["args"]
# Assume nested argument list means a list of parameters
if any(isinstance(i, list) for i in args_list):
output = []
for args in args_list:
args = format_args(args, arg_count)
output += [event_cls(*args)]
return output
else:
args_list = format_args(args_list, arg_count)
return [event_cls(*args_list)]
return data
def format_targets(filename, do_all):
if do_all:
print("Formatting all spawn targets")
else:
print("Formatting modified spawn targets")
# SETTINGS flags: (function, parameters)
setting_to_func = {
"formatModifiedSpawnTargets": (format_targets, [False]),
"formatAllSpawnTargets": (format_targets, [True])
}
# Read SETTINGS flags and format entities file
# also make sure we don't miss any variables
def format_entities_file(filename, settings):
for line in settings.splitlines():
#print(line)
if line in setting_to_func:
func, args = setting_to_func[line]
func(filename, *args)
# Handle assignments written before REPLACE ENCOUNTERs
if line.count('=') == 1:
var = line.split("=")
add_variable(var[0].strip(), var[1].strip())
# cringe
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
# Apply @ prefixes and macro names (jank)
def apply_prefixes(event_string):
output_str = ""
items = variables.items()
sorted_variables = sorted(items, key=lambda x: len(x[0]), reverse=True)
#print(sorted_variables)
if "@" in event_string:
segments = event_string.split("@")
for i, seg in enumerate(segments):
for var, val in sorted_variables:
val = format_args([val], 1)[0]
if ((seg.endswith(f'{var}') and i != len(segments)-1)
or (seg.startswith(f'{var}') and i != 0)):
seg = seg.replace(f'{var}', str(val))
if not (is_number(str(val)) or val in ["true", "false"]):
debug_print(f'added "" for {var} = {val}')
val = f'"{val}"'
seg = seg.replace(f'"{var}"', str(val))
output_str += seg
else:
for var, val in sorted_variables:
if not (is_number(str(val)) or val in ["true", "false"]):
debug_print(f'added "" for {var} = {val}')
val = f'"{val}"'
event_string = event_string.replace(f'"{var}"', str(val))
output_str = event_string
#print(output_str)
return output_str
# The Big One:tm:
def compile_EBL(ebl_file):
tic = time.time()
segments = generate_EBL_segments(ebl_file, format_file=True)
encounters = map(ebl.parse, segments)
output_file = open("test_encounter.txt", "w")
for encounter in list(encounters):
output_str = ""
item_index = 0
events = create_events(encounter)
if events is None:
continue
for event in events:
# print(f"{vars(event)}")
if isinstance(event, EBL_Assignment):
add_variable(event.name, event.value)
continue
event_string = apply_prefixes(str(event))
output_str += (f"item[{item_index}]" + " = {\n" +
indent(event_string, "\t") + "}\n")
item_index += 1
output_file.write(output_str)
output_file.close()
print(f"Done compiling in {time.time()-tic:.1f} seconds")
compile_EBL("Test EBL Files/test_EBL.txt")