From 17fa5aa8e266b4c6446c3b9b21842599c9e8cf5f Mon Sep 17 00:00:00 2001 From: shweet Date: Sat, 24 Oct 2026 21:14:38 -0400 Subject: [PATCH] use_dispatch: id -> case table for UseActiveItem/UseCard/UsePill from the jump tables; find.sh active/card/pill Co-Authored-By: Claude Opus 5.5 --- README.md | 14 +- find.sh | 18 +- notes/README.md | 1 + notes/systems/use_dispatch.md | 40 +++++ scripts/use_dispatch.py | 77 +++++++++ use_dispatch.tsv | 314 ++++++++++++++++++++++++++++++++++ 6 files changed, 452 insertions(+), 12 deletions(-) create mode 100644 notes/systems/use_dispatch.md create mode 100644 scripts/use_dispatch.py create mode 100644 use_dispatch.tsv diff --git a/README.md b/README.md index 61522ba..2376139 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ so parallel live calls work too. Set `DECOMP_TIMEOUT=secs` for big functions (de `find.sh` modes (run it without args to list them): `name REGEX`, `str REGEX` (strings and their users), `strs FUNC` (strings a function uses), `code REGEX` (functions whose C matches, with hit counts), -`callers X`, `callees X`, `npc TYPE`, `item ID`, `active ID [N]`, `player ID`, `enum REGEX`, `lua REGEX`, `field REGEX`, `val ADDR`, +`callers X`, `callees X`, `npc TYPE`, `item ID`, `active ID [N]`, `card ID [N]`, `pill ID [N]`, `player ID`, `enum REGEX`, `lua REGEX`, `field REGEX`, `val ADDR`, `vtable CLASS` (slots read from the exe's RTTI: the authoritative way to name Update/Render/Fire... of an Entity_* or Weapon_* class). ## Naming you will see @@ -118,10 +118,11 @@ at 0x6c41dc. Each case calls one AI routine with ECX = the NPC. `scripts/NpcTabl overlap, so read the callee name. The `this` (player) argument is implicit (ECX) and not printed. 3. Stat items live in `Entity_Player::EvaluateItems` (00763570, about 53 KB). Tear and fire behaviour is in `Entity_Player::GetMultiShotParams`, `Entity_Tear::*` and `Entity_Player::Update`. -4. **Actives:** `./find.sh active ` prints the `case :` blocks of `Entity_Player::UseActiveItem` - (005b39d0, about 10.7k lines; it took about 45 min to decompile and was spliced into the dump). Cards and - pills have their own functions, `Entity_Player::UseCard` and `UsePill`; use `decompile.sh` and grep for - `case 0x..`. +4. **Actives, cards, pills:** `./find.sh active ` (also `card `, `pill `) prints the case of + `Entity_Player::UseActiveItem` (005b39d0, about 10.7k lines; it took about 45 min to decompile and was spliced into + the dump), `UseCard` or `UsePill`. The id -> case table `use_dispatch.tsv` is read from the exe's jump tables + (`scripts/use_dispatch.py`); UseActiveItem's second table (ids 283+) is labelled by table slot in the C, not by id, + see `notes/systems/use_dispatch.md`. An active with no case only adds its TemporaryEffect: use `find.sh item`. **Character.** `./find.sh enum PLAYER_` gives the id. `./find.sh player ` lists compares against the player type that sit within 4 lines of a `GetPlayerType()` or `_playerType` (0x13c0) read. For gimmicks, also grep @@ -192,8 +193,7 @@ $ ./find.sh active 33 25 if ((Entity::Type() == 0x2d) || (Entity::Type() == 0x4e)) { /* Mom (45) / Mom's Heart (78) */ (*(code **)(*piVar21 + 0x24))(); /* vtable slot 9 = Kill */ ``` -`active` prints every `case 0x21:` in `UseActiveItem`. The function has more than one switch, so check which -switch a hit belongs to. Here the second hit, at line 5188, is a different switch. +`active` prints the one case the jump table sends id 33 to (`use_dispatch.tsv`), with the other ids sharing it. ## What works / what doesn't diff --git a/find.sh b/find.sh index 2d2045a..a6da8b8 100755 --- a/find.sh +++ b/find.sh @@ -8,7 +8,9 @@ # callees X what X calls # npc TYPE NPC AI function(s) for an entities2.xml type id (see README) # item ID code lines testing collectible/trinket ID (HasCollectible(0x34,..), HasTrinket, ...) -# active ID [N] the case(s) for active item ID in Entity_Player::UseActiveItem (N lines each, default 60) +# active ID [N] the case for active item ID in Entity_Player::UseActiveItem (N lines, default 60; table: use_dispatch.tsv) +# card ID [N] the case for card/rune/soul ID in Entity_Player::UseCard +# pill ID [N] the case for pill effect ID in Entity_Player::UsePill # player ID code comparing the player type to ID (heuristic: compare within 4 lines of a player-type read) # enum REGEX game enums (entity types, collectibles, trinkets, cards, players...) from REPENTOGON # lua REGEX Lua API bindings (class, member, target function or field+offset) @@ -17,7 +19,7 @@ # vtable CLASS vtable slots of an RTTI class, read from the exe (e.g. Weapon_Knife, Entity_Player) D="$(cd "$(dirname "$0")" && pwd)"; T="$D/dump" MODE="$1"; ARG="$2" -[ -z "$ARG" ] && { sed -n '2,17p' "$0"; exit 1; } +[ -z "$ARG" ] && { sed -n '2,19p' "$0"; exit 1; } addr_of() { # address or name -> 8-digit address if [[ "$1" =~ ^(0x)?[0-9a-fA-F]{6,8}$ ]]; then printf '%08x\n' $((16#${1#0x})) else awk -F'\t' -v n="$1" '$2 == n "" {print $1}' "$T/functions.tsv"; fi @@ -33,8 +35,14 @@ case "$MODE" in npc) grep -P "^$ARG\t" "$T/npc_ai.tsv" ;; item) H=$(printf '0x%x' "$ARG") awk -v r="(Collectible|Trinket)[A-Za-z]*[(]([^,()]*,)?($H|$ARG)[,)]" '/^\/\/@@ / {h=$2" "$3; next} $0 ~ r {gsub(/^ +/, ""); print h "\t" $0}' "$T/all.c" ;; - active) H=$(printf '%x' "$ARG") - "$D/decompile.sh" Entity_Player::UseActiveItem | awk -v r="case ([(][^)]*[)])?(0x$H|$ARG):|caseD_$H:" -v n="${3:-60}" '$0 ~ r {w=n; print "---- line " NR} w-- > 0' ;; + active|card|pill) # id -> case address and label from the exe's jump tables (scripts/use_dispatch.py) + F=UseActiveItem; [ "$MODE" = card ] && F=UseCard; [ "$MODE" = pill ] && F=UsePill + ROW=$(awk -F'\t' -v k="$MODE" -v i="$ARG" '$1 == k && $2 == i' "$D/use_dispatch.tsv") + [ -z "$ROW" ] && { echo "$MODE $ARG has no case in $F (an active then only adds its TemporaryEffect: try find.sh item $ARG)"; exit; } + IFS=$'\t' read -r _ _ ENUM TARGET LABEL ANCHOR SHARES <<< "$ROW" + echo "---- $ENUM: case at 0x$TARGET, shared with: $SHARES" + "$D/decompile.sh" "^Entity_Player::$F\$" | awk -v r="$LABEL" -v a="$ANCHOR" -v n="${3:-60}" \ + 'a == "-" || index($0, a) {on = 1} on && !w && $0 ~ r {w = n; print "---- line " NR} w > 0 {print; if (!--w) exit}' ;; player) H=$(printf '0x%x' "$ARG") # player-type read, then a compare/case with ID within 4 lines awk -v r="([!=]= |case )($H|$ARG)([^0-9a-fx]|$)" '/^\/\/@@ / {h=$2" "$3; next} /_playerType|0x13c0|GetPlayerType/ {w=5} w-- > 0 && $0 ~ r {gsub(/^ +/, ""); print h "\t" $0}' "$T/all.c" ;; enum) grep -P -- "$ARG" "$D/repentogon/libzhl/functions/Global.zhl" | sed 's/^ *//' ;; @@ -42,5 +50,5 @@ case "$MODE" in val) python3 -I "$D/scripts/read_value.py" "$D/bin/isaac-ng.exe" "${ARG#DAT_}" ;; field) grep -P -- "$ARG" "$T/zhl_fields.tsv" ;; vtable) python3 -I "$D/scripts/vtable.py" "$D/bin/isaac-ng.exe" "$T/functions.tsv" "$D/zhl_vtables.tsv" "$ARG" "${3:-60}" ;; - *) sed -n '2,17p' "$0"; exit 1 ;; + *) sed -n '2,19p' "$0"; exit 1 ;; esac diff --git a/notes/README.md b/notes/README.md index 3ba18d1..d83ecf0 100644 --- a/notes/README.md +++ b/notes/README.md @@ -8,6 +8,7 @@ stored here: `./decompile.sh 0xADDR` prints it from the dump, with the current n | `entities/` | per-entity findings from the monster-fidelity passes (state machines, timings, speeds, what the port changed or skipped) | | `systems/greed_mode.md` | Greed waves' coins and music, Ultra Greed/coins/doors states, donation machine spawn, Greed doors/button, Mega Satan door | | `systems/alt_path.md` | Repentance's path: secret exit kinds and doors, Level::SetNextStage's routing, the mirror world and mineshaft dimensions, quest doors, the white fire's Lost Curse, minecarts | +| `systems/use_dispatch.md` | id -> case of UseActiveItem / UseCard / UsePill from the exe's jump tables (`use_dispatch.tsv`, `find.sh active/card/pill`), the slot-labelled second active switch, the common tail (TemporaryEffect), actives without a case | | `systems/room_features.md` | Room spawns (grid index wrap, trigger outputs 969 / Room::TriggerOutput, held Spawners), pressure plates and the mineshaft rail plate, white and giga poops, Mom's bed, Fissure and its spawner, Mother's Shadow, secret Isaac; why Rotgut's maggot, the Beast, Ultra Horsemen and Dogma wait | | `items/items_sweep_10.md` | Smelter/Guppy's Tail room drop variant, Daemon's Tail pickup picker, I'm Drowsy slowdown, Guillotine orbit layer + orbit distance table, Converter | | `items/items_sweep_11.md` | Nancy Bombs flag roll (GetBombFlags) | diff --git a/notes/systems/use_dispatch.md b/notes/systems/use_dispatch.md new file mode 100644 index 0000000..1c48189 --- /dev/null +++ b/notes/systems/use_dispatch.md @@ -0,0 +1,40 @@ +# Active item / card / pill dispatch (rif/items-verify) + +`use_dispatch.tsv` (repo root, from `scripts/use_dispatch.py bin/isaac-ng.exe repentogon/libzhl/functions/Global.zhl`) +maps every id to its case: `kind id enum target label anchor shares`. `find.sh active|card|pill ID [N]` prints the case. +The table is read from the exe's jump tables, so it is exact; only the label (the regex finding the case in the C) +depends on how Ghidra printed the switch. + +## Entity_Player::UseActiveItem (0x5b39d0) dispatch at 0x5b3c77 + +- Prologue: id 0x3b (Book of Belial passive, Judas' birthright) is used as 0x22 (Book of Belial). Negative ids + (TMTRAINER glitched items) go to 0x9e01b0 instead of the switch. The use is passed through Lua first (0x863760: + a callback returning true skips everything). +- `id > 0x11a`: table B, `id - 0x11b` in 0..0x1be, byte index 0x5c7760, jump table 0x5c7598 (114 slots, slot 0x71 + = default). Ghidra could not normalise it ("Could not find normalized switch variable to match jumptable") and + labels its cases with the SLOT (`case 0:` .. `case 0x71:`, sometimes decimal: Abyss is `case 99:`, Suplex + `case 100:`). This is why the old `find.sh active` missed every id from 283 (D100) up. +- `id == 0x11a` (How to Jump): an `if (pEVar47 == 0x11a)` before switch A. +- `id - 0x21` in 0..0xe6: table A, byte index 0x5c746c, jump table 0x5c73a0 (51 slots); Ghidra labels these with the + real id (`case (Entity_Player *)0x21:`). +- Default 0x5c71f1: log if the id is out of range, then the common tail. +- Common tail LAB_005b54d1: Lua MC_USE_ITEM, AnimateCollectible when the case asked for it, RemoveCollectible for + single-use (flag 0x40), cache flags from the item config (+0x54) then EvaluateItems, **TemporaryEffects::AddEffect + of the item unless the case cleared the flag** (local_60a, default 1), AddCostume; 0x246 (Glowing Hourglass) and + 0x2c0 (R Key) extra calls. +- Shared cases: Pony 130 / White Pony 181, Candle 164 / Red Candle 289, D6 105 / Eternal D6 609, We Need To Go Deeper 84 / Mom's Shovel 552, Gamekid 93 / Esau Jr 703, Crack the Sky 160 / Book of Virtues + 584 (a shared case tests the id where they differ). +- Actives with no case at all (only the room TemporaryEffect, read through `HasCollectibleEffect(id)` elsewhere; + use `find.sh item ID`): 77 My Little Unicorn, 137 Remote Detonator, 298 Unicorn Stump, 474 Broken Glass Cannon, + 516 Sprinkler, 655 Spin to Win (and the unused 43). +- D Infinity (0x1e9) uses one of 10 dice from table 0xb1f450 through a recursive UseActiveItem, then a small second + switch at 0x5bfbd8 (id - 0x11b, byte index 0x5c79c4, table 0x5c79b0) for the dice's after-effects; that is not a + dispatcher. + +## Entity_Player::UseCard (0x7b2230) and UsePill (0x7c4180) + +- UseCard: `card - 1` in 0..0x60, jump table 0x7b8a00, default 0x7b8949; cases labelled with the card id. Ace of + Clubs/Diamonds/Spades/Hearts (27-30) share one case. A nested switch inside the Reverse Hermit (0x41) case has its + own `case 0x45:`, so the first match of `case 0x45` is not Reverse Death: find.sh matches only indent 2..4. +- UsePill: the pill EFFECT (already resolved from the colour by the caller), 0..0x31, jump table 0x7c77e4, default + 0x7c7528; horse pill = local_204 (bit 0x800 of the use flags), labelled with the effect id. diff --git a/scripts/use_dispatch.py b/scripts/use_dispatch.py new file mode 100644 index 0000000..0bc6315 --- /dev/null +++ b/scripts/use_dispatch.py @@ -0,0 +1,77 @@ +#!/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('