forked from icebound777/PMR-SeedGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.py
388 lines (360 loc) · 15.3 KB
/
parse.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import os
import re
import json
from enums import Enums, enum_int, create_enums
from utility import get_files
def gather_keys():
"""
Gather a list of ALL keys under the ../globals/patch/ directory
AF = Options
A1 = Items
A2 = Actors
A3 = Entrances
A4 = Palettes
A5 =
A6 = Moves (cost FP/BP)
AA = RESERVED (see table.py unique itemID)
AF = Quizzes
"""
files = get_files("../../globals/patch")
keys = {
"items": {},
"item_prices": {},
"blocks": {},
"move_costs": {},
"actors": {},
"entrances": {},
"palettes": {},
"options": {},
"quizzes": {},
}
sprite_palette_counts = {}
for filepath in files:
with open(filepath, "r", encoding="utf-8") as file:
for line in file:
if match := re.match(r"#export\s*.DBKey:", line):
data = line[match.end():]
match = re.match(r"(\S*)\s*(\S*)", data)
key_info,number = match.group(1), match.group(2)
byte_id = int(number[0:2], 16)
area_id = int(number[2:4], 16)
map_id = int(number[4:6], 16)
value_id = int(number[6:8], 16)
key = (byte_id << 24) | (area_id << 16) | (map_id << 8) | value_id
if byte_id == 0xA1 and 0x40 <= value_id <= 0x4F:
name = key_info.split(":")[-1]
keys["blocks"][key] = {
"name": name,
"map_name": key_info.split(":")[0],
"byte_id": byte_id,
"area_id": area_id,
"map_id": map_id,
"value_id": value_id,
}
elif byte_id == 0xA1:
name = key_info.split(":")[-1]
if "ShopPrice" in name or "RewardAmount" in name:
keys["item_prices"][key] = {
"name": name,
"map_name": key_info.split(":")[0],
"byte_id": byte_id,
"area_id": area_id,
"map_id": map_id,
"value_id": value_id,
}
else:
keys["items"][key] = {
"name": name,
"map_name": key_info.split(":")[0],
"byte_id": byte_id,
"area_id": area_id,
"map_id": map_id,
"value_id": value_id,
}
elif byte_id == 0xA2:
name,attribute = key_info.split(":")
if key not in keys["actors"]:
keys["actors"][key] = {}
keys["actors"][key][attribute] = {
"name": name,
"byte_id": byte_id,
"area_id": area_id,
"actor_id": map_id,
"value_id": value_id,
}
elif byte_id == 0xA3:
_,map_name,entrance = key_info.split(":")
entrance = int(entrance, 16)
keys["entrances"][key] = {
"name": map_name,
"entrance": entrance,
"byte_id": byte_id,
"area_id": area_id,
"map_id": map_id,
"value_id": value_id,
}
elif byte_id == 0xA4:
sprite = key_info.split(":")[-1]
keys["palettes"][key] = {
"sprite": sprite
}
elif byte_id == 0xA6:
cost_type,move = key_info.split(":")
keys["move_costs"][key] = {
"name": move,
"cost_type": cost_type,
"byte_id": byte_id,
"area_id": area_id,
"map_id": map_id,
"value_id": value_id,
}
elif byte_id == 0xAF:
name,attribute = key_info.split(":")
if name in ("Options", "Cosmetic", "Mystery"):
keys["options"][key] = {
"name": attribute,
"byte_id": byte_id,
"area_id": area_id,
"map_id": map_id,
"value_id": value_id,
}
elif name == "Quiz":
keys["quizzes"][key] = {
"name": attribute,
"byte_id": byte_id,
"area_id": area_id,
"map_id": map_id,
"value_id": value_id,
}
elif match := re.match(r"#export\s*.PalCount:", line):
# Palette count per Sprite
data = line[match.end():]
match = re.match(r"(\S*)\s*(\S*)", data)
sprite, palette_count = match.group(1), match.group(2)
sprite_palette_counts[sprite] = palette_count
for sprite, palette_count in sprite_palette_counts.items():
for dbkey, sprite_dict in keys["palettes"].items():
if sprite_dict["sprite"] == sprite:
keys["palettes"][dbkey]["palette_count"] = int(palette_count.replace("`", ""))
with open("./debug/keys.json", "w", encoding="utf-8") as file:
json.dump(keys, file, indent=4)
def gather_values():
def get_value(value):
# Booleans
if value == ".True":
return True
if value == ".False":
return False
# Numbers
if value.endswith("`"):
return int(value[:-1])
try:
value = int(value, 16)
except:
pass
else:
return value
# Items
if value.startswith(".Item"):
item = value.split(":")[-1]
value = Enums.get("Item")[item]
return value
# Blocks
if value.startswith(".BlockType"):
# As per definition in RandomSuperBlocks.patch in base mod
if "Multi" in value:
value = 0
elif "Super" in value:
value = 1
else:
raise ValueError
return value
return None
create_enums()
values = {
"items": {},
"item_prices": {},
"blocks": {},
"move_costs": {},
"actors": {},
"entrances": {},
"palettes": {},
"options": {},
"quizzes": {},
}
file_path = "/../../../globals/patch/DatabaseDefaults.patch"
with open(os.path.abspath(__file__ + file_path), "r", encoding="utf-8") as file:
for line in file:
if match := re.match(r"\s*.DBKey:(\S*)\s*(\S*)", line):
key_info = match.group(1)
value = match.group(2)
if "Options" in key_info or "Cosmetic" in key_info or "Mystery" in key_info:
name = key_info.split(":")[-1]
values["options"][name] = get_value(value)
elif "Quiz" in key_info:
name = key_info.split(":")[-1]
values["quizzes"][name] = get_value(value)
elif "Move" in key_info: # MoveBP:SpinSmash 1`
name = key_info.split(":")[-1]
cost_type = key_info.split(":")[0][-2:]
if name not in values["move_costs"]:
values["move_costs"][name] = {}
values["move_costs"][name][cost_type] = get_value(value)
# Check for map name (which means it's an item, item price or block)
elif match := re.match(r"([A-Z]{2,5}_\d+):(\S*)", key_info):
map_name = match.group(1)
key_name = match.group(2)
if "RandomBlock" in key_info:
if map_name not in values["blocks"]:
values["blocks"][map_name] = {}
values["blocks"][map_name][key_name] = get_value(value)
elif "ShopPrice" in key_name or "RewardAmount" in key_name:
if map_name not in values["item_prices"]:
values["item_prices"][map_name] = {}
values["item_prices"][map_name][key_name] = get_value(value)
else:
if map_name not in values["items"]:
values["items"][map_name] = {}
values["items"][map_name][key_name] = get_value(value)
# Check for actor data
elif any(["HP" in key_info,
"Damage" in key_info,
"Level" in key_info,
"Increment" in key_info]):
actor,attribute = key_info.split(":")
value = get_value(value)
if actor not in values["actors"]:
values["actors"][actor] = {}
values["actors"][actor][attribute] = value
file_path = "../../globals/patch/Actors.patch"
with open(file_path, "r", encoding="utf-8") as file:
for line in file:
if match := re.match(r"#export\s*.ActorPtr:", line):
data = line[match.end():]
match = re.match(r"(\S*)\s*(\S*)", data)
actor, pointer = match.group(1), match.group(2)
if actor not in values["actors"]:
values["actors"][actor] = {}
values["actors"][actor]["Pointer"] = int(pointer, base=16)
with open("./debug/values.json", "w", encoding="utf-8") as file:
json.dump(values, file, indent=4)
def get_default_table():
# Get general data
db = {}
with open(os.path.abspath(__file__ + "/../../globals/patch/DatabaseDefaults.patch"), "r", encoding="utf-8") as file:
db_found = False
while not db_found:
if match := re.match(r"#export:Data\s*\$DefaultDatabase", next(file)):
db_found = True
for line in file:
if line.startswith("}"):
break
if match := re.match(r"\s*.DBKey:(\S*):(\S*)\s*(\S*)", line):
table = match.group(1)
attribute = match.group(2)
value = match.group(3)
enum_type = None
if value == ".True":
value = True
elif value == ".False":
value = False
elif value.endswith("`"): # Decimal
value = int(value[:-1])
else:
try:
value = int(value, 16) # Hexadecimal
except:
value,enum_type = enum_int(value) # Convert enum to number
if table not in db:
db[table] = {}
db[table][attribute] = {
"value": value,
"enum_type": enum_type,
"attribute": attribute,
"table": table,
}
# Get entrance data
db["Entrance"] = {}
with open(os.path.abspath(__file__ + "/../../globals/patch/RandomEntrances.patch"), "r", encoding="utf-8") as file:
for line in file:
if match := re.match(r"#export\s*.DBKey:Entrance:(\S*):(\S*)\s*(\S*)", line):
map_name = match.group(1)
map_exit = int(match.group(2), 16)
key = match.group(3)
byte_id = int(key[0:2], 16)
area_id = int(key[2:4], 16)
map_id = int(key[4:6], 16)
entry_id = int(key[6:8], 16)
if map_name not in db["Entrance"]:
db["Entrance"][map_name] = {}
db["Entrance"][map_name][map_exit] = {
"byte_id": byte_id,
"area": area_id,
"map": map_id,
"entry": entry_id,
}
return db
def get_table_info():
# Defaults
table_info = {
"magic_value": 0x504D4442,
"header_size": 0x20,
"db_size": 0,
"seed": 0xDEADBEEF,
"address": 0x1D00000,
"formations_offset": 0,
"itemhints_offset": 0,
}
return table_info
def create_table(default_table):
def get_keys(db, filepath):
with open(filepath, "r", encoding="utf-8") as file:
for line in file:
if match := re.match(r"#export\s*.DBKey:(\S*):(\S*)\s*(\S*)", line):
table = match.group(1)
attribute = match.group(2)
key = int(match.group(3), 16)
if table not in db:
db[table] = {}
if data := default_table.get(table, {}).get(attribute):
default_value = data["value"]
db[table][attribute] = {
"key": key,
"value": default_value,
"attribute": attribute,
"table": table,
"enum_type": {
0xAF: "Option",
0xA1: "Item" if "ShopPrice" not in attribute else "ItemPrice",
0xA2: "Actor",
0xA3: "Entrance",
0xA4: "Palette",
0xA6: "Move"
}.get((key & 0xFF000000) >> 24)
}
db = {}
get_keys(db, os.path.abspath(__file__ + "/../../globals/patch/DatabaseKeys.patch"))
get_keys(db, os.path.abspath(__file__ + "/../../globals/patch/generated/keys.patch"))
db["Entrance"] = {}
for map_name,entrance_data in default_table["Entrance"].items():
db["Entrance"][map_name] = {}
for entrance,data in entrance_data.items():
db_key = (data["byte_id"] << 24) \
| (data["area"] << 16) \
| (data["map"] << 8) \
| data["entry"]
db["Entrance"][map_name][entrance] = {
"key": db_key,
"value": db_key & 0x00FFFFFF,
"map_name": map_name,
"entrance": entrance,
"enum_type": {
0xAF: "Option",
0xA1: "Item",
0xA2: "Actor",
0xA3: "Entrance",
0xA4: "Palette",
0xA6: "Move"
}.get((db_key & 0xFF000000) >> 24)
}
return db