#!/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('