Files
isaac-port/scripts/use_dispatch.py
T

78 lines
4.0 KiB
Python

#!/usr/bin/env python3
"""Map every active item / card / pill id to its case in Entity_Player::UseActiveItem / UseCard / UsePill.
Usage: use_dispatch.py bin/isaac-ng.exe repentogon/libzhl/functions/Global.zhl > use_dispatch.tsv
Columns: kind, id, enum, target (case address), label (regex for the case in dump/all.c), anchor (regex of the line
the label is searched after, '-' = from the function start), shares (other ids of this kind with the same target).
The switch tables are read from the exe. UseActiveItem dispatches in two jump tables plus one compare
(0x5b3c77): id 0x11a is an `if`, ids 0x21..0x107 go through table A (Ghidra labels its cases with the real id), ids
0x11b..0x2d9 through table B (Ghidra could not normalise it: its cases are labelled with the table SLOT, 0..0x71,
in the switch right after the "Could not find normalized switch variable" warning). Id 0x3b is first remapped to
0x22 and negative (TMTRAINER) ids go to 0x9e01b0. UseCard (card - 1) and UsePill (effect) have one plain table
each, labelled with the real id. After a game update, re-read the addresses below from `decompile.sh --asm`.
"""
import re, struct, sys
ACTIVE_DEFAULT, CARD_DEFAULT, PILL_DEFAULT = 0x5c71f1, 0x7b8949, 0x7c7528
ACTIVE_IF = {0x11a: 0x5b78d9}
ACTIVE_REMAP = {0x3b: 0x22}
B_ANCHOR = 'Could not find normalized switch variable'
# kind, first id, last id, byte index table (0 = none), jump table, labelled by slot
TABLES = [('active', 0x21, 0x107, 0x5c746c, 0x5c73a0, False), ('active', 0x11b, 0x2d9, 0x5c7760, 0x5c7598, True),
('card', 1, 0x61, 0, 0x7b8a00, False), ('pill', 0, 0x31, 0, 0x7c77e4, False)]
ENUMS = {'active': ('CollectibleType', 'COLLECTIBLE_', ACTIVE_DEFAULT), 'card': ('eCard', '(?:CARD|RUNE)_', CARD_DEFAULT),
'pill': ('ePillEffect', 'PILLEFFECT_', PILL_DEFAULT)}
def pe_load(path):
b = open(path, 'rb').read()
e = struct.unpack_from('<I', b, 0x3c)[0]
count, opt = struct.unpack_from('<H', b, e + 6)[0], struct.unpack_from('<H', b, e + 20)[0]
base = struct.unpack_from('<I', b, e + 52)[0]
secs = [struct.unpack_from('<IIII', b, e + 24 + opt + 40 * i + 8) for i in range(count)]
def read(va, size):
for vsize, rva, rsize, raw in secs:
if base + rva <= va < base + rva + max(vsize, rsize):
return int.from_bytes(b[raw + va - base - rva:][:size], 'little')
raise ValueError(hex(va))
return read
def enums_load(path):
text, out = open(path).read(), {}
for kind, (name, prefix, _) in ENUMS.items():
body = re.search(r'enum %s\b[^{]*\{(.*?)\}' % name, text, re.S).group(1)
out[kind] = {int(v, 0): n for n, v in re.findall(r'(%s\w+)\s*=\s*(-?\w+)' % prefix, body)}
return out
def case_label(value): # top-level cases sit at indent 2 or 4; Ghidra prints a value in decimal or hex
return r'^ {2,4}case ([(][^)]*[)])?(0x%x|%d):' % (value, value)
def main():
read, enums = pe_load(sys.argv[1]), enums_load(sys.argv[2])
rows = []
for kind, first, last, indexTable, jumpTable, isSlot in TABLES:
for i in range(last - first + 1):
slot = read(indexTable + i, 1) if indexTable else i
target = read(jumpTable + 4 * slot, 4)
if target != ENUMS[kind][2]:
rows.append([kind, first + i, target, case_label(slot if isSlot else first + i), B_ANCHOR if isSlot else '-'])
for itemId, target in ACTIVE_IF.items():
rows.append(['active', itemId, target, r'== ([(][^)]*[)])?0x%x[)]' % itemId, '-'])
targets = {(r[0], r[2]): [] for r in rows}
for r in rows:
targets[(r[0], r[2])].append(r[1])
for itemId, toId in ACTIVE_REMAP.items():
rows += [[r[0], itemId] + r[2:] for r in rows if r[0] == 'active' and r[1] == toId]
print('kind\tid\tenum\ttarget\tlabel\tanchor\tshares')
for kind, itemId, target, label, anchor in sorted(rows, key=lambda r: (r[0], r[1])):
shares = ','.join(str(o) for o in targets[(kind, target)] if o != itemId) or '-'
print('%s\t%d\t%s\t%08x\t%s\t%s\t%s' % (kind, itemId, enums[kind].get(itemId, '?'), target, label, anchor, shares))
main()