272 lines
20 KiB
Markdown
272 lines
20 KiB
Markdown
# Repentance+ decompilation toolkit
|
||
|
||
Ghidra 12.2 headless setup for `isaac-ng.exe` (**v1.9.7.17**, PE32 i386, MSVC, RTTI present), with names from
|
||
REPENTOGON's signatures, the game's own Lua-API registration table and an NPC AI dispatch table. Use it to read
|
||
real game behaviour instead of wiki text, and to grow it into readable source: every name, type and note you
|
||
add is kept and re-applied (see **Adding findings back**). Everything is in this folder. Nothing outside it is touched
|
||
(Ghidra writes its user settings to `~/.config/ghidra` and `~/.cache/ghidra`), and the game install is only read.
|
||
|
||
**Private.** This repo holds decompiled game code. It is a local git repo with no remote: never push it, publish
|
||
it, or paste large parts of it anywhere public. The game install is only read, never modified.
|
||
|
||
```
|
||
db/ YOUR names and types: names.tsv (address -> name, beats every other source), fields.tsv (struct fields),
|
||
locals.tsv (local variables inside one function)
|
||
notes/ what is known per entity/item/character/system, with addresses; notes/README.md is the index
|
||
src/ C++ checked against the decompile: structs.hpp (generated: every confirmed struct, offsets static_asserted),
|
||
anm2/ (sprite player), rooms/ (.stb + RoomConfig)
|
||
decompile.sh print decompiled C for a function (name regex or address), from the dump or live
|
||
find.sh search: names, strings, string users, code, callers/callees, NPC type -> AI, item id, enums, fields
|
||
build.sh rebuild everything from the exe (after a game update)
|
||
dump/ the corpus the scripts read: all.c (every function, ~21k), functions.tsv, calls.tsv,
|
||
strings.tsv (string -> functions using it), npc_ai.tsv, lua_bindings.tsv, zhl_fields.tsv
|
||
project/ saved Ghidra project (auto-analysed + names + types)
|
||
scripts/ Ghidra Java scripts and Python helpers used by the above
|
||
repentogon/ shallow clone of TeamREPENTOGON/REPENTOGON (libzhl/functions/*.zhl = sigs, structs, enums)
|
||
bin/ private copy of the exe (analysis input; also used to read constants)
|
||
jdk/ ghidra_12.2_PUBLIC/ Temurin 21 (system java is 8, too old) and Ghidra
|
||
```
|
||
|
||
## Quick use
|
||
|
||
```sh
|
||
cd <this folder>
|
||
./find.sh npc 29 # entity type 29 (Hopper) -> its AI routine
|
||
./decompile.sh '^NpcAI::ENTITY_HOPPER$' # decompiled C (instant, from dump/all.c)
|
||
./find.sh strs NpcAI::ENTITY_HOPPER # anm2 animation/event names, sounds, paths it uses
|
||
./find.sh item 4 # every line testing collectible/trinket 4
|
||
./find.sh active 33 # The Bible's case in Entity_Player::UseActiveItem
|
||
./find.sh enum 'COLLECTIBLE_.*HEAD' # id lookup (entity types, items, trinkets, cards, pills, players...)
|
||
./find.sh val DAT_00baa2d0 # float/int constant the decompiler left as DAT_
|
||
./decompile.sh 0x526470 # by address (any address inside the function works)
|
||
./decompile.sh --live 'Entity_NPC::Morph$' # live Ghidra decompile (~30s), for anything not in the dump
|
||
./decompile.sh --asm 0x6c29f0 30 # disassembly, for the few functions that will not decompile
|
||
```
|
||
|
||
`decompile.sh QUERY [MAX]`: QUERY is a hex address or an awk/ERE regex matched against the full name
|
||
(`Class::Method`). It prints up to MAX (default 20) functions. Reading the dump is instant and safe for many
|
||
agents at once. `--live` copies the project to a temp dir first, because Ghidra locks a project even read-only,
|
||
so parallel live calls work too. Set `DECOMP_TIMEOUT=secs` for big functions (default 180).
|
||
|
||
`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]`, `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
|
||
|
||
| prefix | source | trust |
|
||
|---|---|---|
|
||
| `NpcAI::ENTITY_<NAME>` | NPC AI dispatch table, named by the first entity type that uses it | high, but it is the first call in the case block: if one has many callers it is a shared helper (that is how ProjectileParams::ProjectileParams got called ENTITY_GEMINI). Types 79 Gemini and 93 Mask have no AI function at all: their case is inline in the dispatcher |
|
||
| `Entity_Player::UseActiveItem`, `Sprite::Play`, `Game::GetRoom` ... with comment `ZHL [lua]` | the game's own Lua API registration (method name -> C++ function) | high, but see the ICF note |
|
||
| comment `ZHL [exact]` | REPENTOGON byte signature, exact unique match | high for long sigs, but not proof (0099b520 matched Entity_Player::InitPreLevelInitStats exactly and is GridEntity_Gravity::PostInit); short sigs can hit the wrong function; where the Lua table disagrees, db/names.tsv records the Lua name (e.g. 007ab380 is `Entity_Player::AnimateCollectible`, not HasCollectible) |
|
||
| comment `ZHL [prefixN/M]` | the sig failed (version drift); the longest unique prefix of N of M bytes matched | medium |
|
||
| comment `ZHL [vtable slot N]` | REPENTOGON vtable layout applied to the RTTI `Class::vftable` | high |
|
||
| comment `DB: ...` | `db/names.tsv`, a person or agent checked it (the note says how) | highest; overrides all of the above |
|
||
| `NpcHelper::...` | `db/names.tsv`: a helper of one NPC AI that takes more than the npc (so not typed `npc`) | as its note says |
|
||
| `FUN_xxxxxxxx` | unnamed | |
|
||
|
||
About 730 functions are named from signatures (679 exact plus 75 prefix; 224 sigs missed and 196 matched in more
|
||
than one place, because REPENTOGON targets 1.9.7.12), about 160 from vtables, about 1100 from Lua registrations,
|
||
and 230 `NpcAI::` routines.
|
||
**ICF caveat:** MSVC folds identical tiny functions, so a 1–3 instruction getter can carry an unrelated name
|
||
(`EntityPtr::Ref` is also `RNG::GetSeed`, `Level::GetCurrentRoom` is `Game::GetRoom`, and so on). Judge tiny
|
||
functions by their body, not their name.
|
||
|
||
**Struct fields are only shown where this exe confirms the offset.** REPENTOGON's definitions target 1.9.7.12 and members
|
||
drift (Game +0x58 .. +0xd98 in steps, Entity_Player +4 .. +0x2c, Entity_Familiar +0x14, GridEntity_Door +0x114), so a
|
||
REPENTOGON offset is never evidence by itself. `scripts/zhl_fields.py` keeps a REPENTOGON field only when a trivial getter
|
||
or the game's Lua field table confirms it, or when it sits between two confirmed fields with the same shift;
|
||
`db/fields.tsv` holds the rest, each with the function that proves it (about 600 were verified that way). Anything else is
|
||
`field_0xNNN`: unknown, not guessed. `src/structs.hpp` is generated from the same data and only compiles if every offset
|
||
is consistent.
|
||
|
||
Types: structs built from the confirmed REPENTOGON fields, plus `lua_fields.tsv` (about 100 fields REPENTOGON lacks,
|
||
read from the game's own trivial Lua getters, e.g. Entity 0x334 `_targetPosition`) and `db/fields.tsv`. Embedded structs
|
||
are typed too (`npc->_sprite._animState._isPlaying`).
|
||
**Prototypes** come from REPENTOGON's declarations only where this exe confirms them: a `__thiscall`/`__stdcall` callee
|
||
pops its own stack arguments (`RET n`), so the declared parameters must add up to `n` (`scripts/ApplyProtos.java`, about
|
||
600 functions; the comment says `prototype: REPENTOGON, confirmed by RET n`). Methods of a class get a typed `this`
|
||
even without a prototype, and every `NpcAI::` routine is
|
||
`__fastcall (Entity_NPC *npc)`, so you read `npc->_state`, `npc->_variant`, `npc->_i1` rather than
|
||
`param_1[0x2d9]`. Unknown offsets still show as `field_0xNNN` or `*(int *)(p + 0xNNN)`. Look them up with
|
||
`find.sh field` or `find.sh lua` (the Lua table has authoritative offsets such as `EntityNPC State field+0xb64`).
|
||
|
||
Useful offsets (Entity / Entity_NPC; check with `find.sh field`): Type 0x28, Variant 0x2c, SubType 0x30,
|
||
Position 0x33c... (`_pos`), NPC StateFrame 0x410, Pathfinder 0x414, State 0xb64, ProjectileCooldown 0xba0,
|
||
V1 0xbb0, V2 0xbb8, I1 0xbc0, I2 0xbc4. Entity_Player PlayerType 0x13c0.
|
||
|
||
## From an id to its code
|
||
|
||
**Entity (entities2.xml `id` = type).** `Entity_NPC::Update` (006c4b30) calls `Entity_NPC_UpdateAI_dispatch`
|
||
(006c2990). That function checks `type - 10 < 0x3be`, then reads a byte index table at 0x6c45a4 and a jump table
|
||
at 0x6c41dc. Each case calls one AI routine with ECX = the NPC. `scripts/NpcTable.java` resolved all of these into
|
||
`dump/npc_ai.tsv` (`type, ENTITY_ENUM, case block, calls in the block`). So:
|
||
1. `./find.sh npc <type>` gives the routine, for example `29 ENTITY_HOPPER ... 00526470:NpcAI::ENTITY_HOPPER`.
|
||
2. A routine is often shared, and **variants are handled inside it** (`if (npc->_type == 0x56)`,
|
||
`npc->_variant == 2`, `switch(npc->_variant)`). Search the body for the variant or subtype you care about.
|
||
Several types share a routine. Fly 13 and Pooter 14 both run `NpcAI::ENTITY_FLY`, and Hopper's routine serves
|
||
7 types. The routine is named after the lowest type.
|
||
3. Types whose case is the default block (`006c41af`, about 630 ids) have no AI in this dispatcher. They are
|
||
effects, pickups and so on, or are handled elsewhere: in `Entity_NPC::Update` itself (look for
|
||
`this->_type == N` there), in Entity_Effect/Entity_Familiar `Update` (big switches on variant), or in Lua.
|
||
Two types (98 Heart of Infamy, 922 Cadavra) run inline code with no call. Read that with
|
||
`decompile.sh --asm <case block>`.
|
||
4. Entity states are `npc->_state` (Lua `npc.State`). REPENTOGON has no NpcState enum, so use the modding docs' NpcState
|
||
values: 0 init, 3 idle, 4 move, 8 attack, ... Animations come from the string args:
|
||
`Sprite::Play` / `Sprite::SetAnimation` / `Sprite::IsEventTriggered("Shoot")` / `Sprite::IsFinished("Attack")`.
|
||
The decompiler often prints only the string (`FUN_0040a5d0("WalkVert",1)`), because the sprite is passed
|
||
in ECX.
|
||
5. Bosses work the same way (`find.sh npc 20` -> `NpcAI::ENTITY_MONSTRO`). Projectile patterns go through
|
||
`Entity_NPC::FireProjectiles(pos, vel, mode, params)` and `FireBossProjectiles`. `ProjectileParams` field
|
||
offsets are in `find.sh lua ProjectileParams`.
|
||
|
||
**Item / trinket id.** There is no table, because effects are scattered `HasCollectible(id)` checks:
|
||
1. `./find.sh enum 'COLLECTIBLE_SAD_ONION'` gives the id.
|
||
2. `./find.sh item <id>` lists every call like `HasCollectible(0x34,..)`, `GetCollectibleNum(4,..)`,
|
||
`HasTrinket(..)`, `GetTrinketMultiplier(..)` with that first argument, along with its function. This is a
|
||
heuristic: it also shows `ItemPool::GetCollectible(4,..)`, where 4 is a pool, and trinket/collectible ids
|
||
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, cards, pills:** `./find.sh active <id>` (also `card <id>`, `pill <effect>`) 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 <id>` lists compares against the player
|
||
type that sit within 4 lines of a `GetPlayerType()` or `_playerType` (0x13c0) read. For gimmicks, also grep
|
||
code: `./find.sh code 'GetPlayerType'`, and look in `Entity_Player::Init`, `Update`, `EvaluateItems`,
|
||
`TakeDamage`.
|
||
|
||
**Animation, event or sound name to code.** `./find.sh str '^Hop$'` lists the functions using that string, and
|
||
`./find.sh strs <func>` goes the other way. Ghidra leaves strings of 4 characters or fewer as `&DAT_...`. The
|
||
dump turns most into literals, and `find.sh val DAT_x` reads any of them.
|
||
|
||
## Worked example 1: Hopper (type 29)
|
||
|
||
```
|
||
$ ./find.sh npc 29
|
||
29 ENTITY_HOPPER 006c3b4d 00526470:NpcAI::ENTITY_HOPPER
|
||
$ ./find.sh strs NpcAI::ENTITY_HOPPER
|
||
Appear Attack BigJumpDown BigJumpUp Hop Hop2 HopLong HopShort JumpDown Regen Shoot ShootDown
|
||
$ ./decompile.sh '^NpcAI::ENTITY_HOPPER$' | less
|
||
if (npc->_state == 0) { npc->_state = 3; ... /* init -> idle */
|
||
local_278 = "Hop";
|
||
if (iVar13 == 0x56) { /* 86 = ENTITY_KEEPER (find.sh enum 'ENTITY_\w+ = 86,') */
|
||
local_278 = "JumpDown"; pcVar18 = "ShootDown"; }
|
||
else if (iVar13 == 0x131) { ... /* 305 = ENTITY_MINISTRO */
|
||
uVar15 = npc->_i1 & 1; ... "Hop2" : "Hop" /* alternates hop anim on I1 */
|
||
... npc->_state = 8 / 7 / 4 ... switch(npc->_i1) { ... }
|
||
```
|
||
One routine serves 7 types (Hopper, Keeper, Ministro, ...), and they branch on `npc->_type`. From here, follow
|
||
the `npc->_state` transitions, the `"Hop"` animation/event checks and the velocity math. Constants the
|
||
decompiler did not inline show as `DAT_00xxxxxx`. Read those with `find.sh val`.
|
||
|
||
## Worked example 2: Gaper (type 10)
|
||
|
||
`./find.sh npc 10` gives `NpcAI::ENTITY_GAPER` (00508f40, about 790 lines). It handles every type-10 variant
|
||
(`npc->_variant == 1/2/3/10`) plus a few other types (`_type == 0xd0`, `0x101`, `0x11c`, `0x12e`). State 0
|
||
is init and sets `_state = 4`. Movement chases `Entity_NPC::GetPlayerTarget(npc)`, and the anims are
|
||
`WalkVert`/`WalkHori` with the head layer on `HeadIdle`.
|
||
|
||
## Worked example 3: item effect, Cricket's Head (collectible 4)
|
||
|
||
```
|
||
$ ./find.sh enum CRICKETS_HEAD -> COLLECTIBLE_CRICKETS_HEAD = 4,
|
||
$ ./find.sh item 4
|
||
00763570 Entity_Player::EvaluateItems iVar12 = GetCollectibleNum(local_45c,4,0);
|
||
00763570 Entity_Player::EvaluateItems cVar2 = HasCollectible(4,0);
|
||
00782af0 FUN_00782af0 iVar11 = Entity_Player::GetTrinketMultiplier(4); <- trinket 4, unrelated
|
||
$ ./decompile.sh Entity_Player::EvaluateItems | grep -n -A4 'GetCollectibleNum(local_45c,4,0)'
|
||
iVar12 = GetCollectibleNum(local_45c,4,0);
|
||
iVar17 = TemporaryEffects::GetCollectibleEffectNum(0x2b4);
|
||
local_460 = (float)(iVar17 + ... + iVar11 + iVar12) * 0.5 + local_460;
|
||
$ ./decompile.sh Entity_Player::EvaluateItems | grep -n -A8 'HasCollectible(4,0)'
|
||
cVar2 = HasCollectible(4,0);
|
||
if (cVar2 || (HasCollectibleEffect(0x22) && HasCollectible(7,0)) || HasCollectible(0xc,0)
|
||
|| TemporaryEffects::HasCollectibleEffect(0xc)) {
|
||
pEVar51->_damage = pEVar51->_damage * 1.5;
|
||
```
|
||
So each Cricket's Head copy adds +0.5 to the flat damage-up sum it shares with several other items. It also
|
||
grants the ×1.5 damage multiplier, the same one given by Magic Mushroom (12) and by Blood of the Martyr (7)
|
||
together with the Book of Belial (34) effect. The multiplier applies once, not per copy.
|
||
|
||
## Worked example 4: active item, The Bible (33)
|
||
|
||
```
|
||
$ ./find.sh active 33 25
|
||
---- line 628
|
||
case (Entity_Player *)0x21:
|
||
FUN_009568e0(); ... (sound/effect calls)
|
||
Room::GetEntities(); ... loop:
|
||
if ((Entity::Type() == 0x2d) || (Entity::Type() == 0x4e)) { /* Mom (45) / Mom's Heart (78) */
|
||
(*(code **)(*piVar21 + 0x24))(); /* vtable slot 9 = Kill */
|
||
```
|
||
`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
|
||
|
||
- Works: the full dump of 21k functions (all but about 20 decompile), name and address lookup, strings with
|
||
users, call graph, NPC type to AI for 324 types, Lua API names (Sprite, Entity, Player, Room, Game ...),
|
||
typed NPC AI, constants, live decompile and disassembly, and running agents in parallel.
|
||
- Doesn't: 22 functions are not in the dump because they timed out at 900 s (`grep -B1 'decompile failed'
|
||
dump/all.c`). Most are unnamed CRT/GL code; `Entity_Effect::Update` (times out even at 7200 s) and
|
||
`Entity_NPC::HandleCollision` (decompiler error "PTRSUB off of non structured pointer type") do not
|
||
decompile at all; read them with `--asm`. `build.sh` retries named failures
|
||
automatically and splices them in (`scripts/splice.py`). For anything else use `--asm`.
|
||
- Typed `__thiscall` code prints casts such as `case (Entity_Player *)0x21:`, because the switch variable got
|
||
the `this` type. It is harmless. Item, trinket and player lookups are grep
|
||
heuristics. Struct fields REPENTOGON doesn't know are `field_0x...`. Lua-named methods outside the zhl set
|
||
have no typed `this`, so you see `in_ECX + 0x13c0`.
|
||
|
||
## Adding findings back
|
||
|
||
Whatever you learn reading the code goes back into the repo, so the next reader gets it for free. Never leave a
|
||
finding only in your scratch files or your final message.
|
||
|
||
1. **A function's real name** (a `FUN_`, or a wrong ZHL/ICF name such as `MinimapConfig::Render` being the Gate AI):
|
||
add a line to `db/names.tsv` (address in any hex form): `address<TAB>Qualified::Name<TAB>evidence`. Use `NpcAI::ENTITY_<ENUM>[_part]` for
|
||
routines that take only the npc (they get typed `Entity_NPC *npc`), `NpcHelper::snake_name` for NPC helpers with
|
||
other arguments, `Class::snake_name` otherwise. Then run `python3 -I scripts/apply_db.py db/names.tsv dump`
|
||
(instant: renames it in the dump everywhere, including call sites and `npc_ai.tsv`).
|
||
For a virtual method, `./find.sh vtable CLASS` shows which slot it is; slot names are the most reliable evidence
|
||
there is (a short ZHL sig or ICF can put a name on the wrong function, a vtable slot cannot).
|
||
2. **A struct field** you have confirmed in this exe: add `Class<TAB>0xOFF<TAB>type _name<TAB>evidence` to `db/fields.tsv`
|
||
(arrays as `int _slots[4]`). Evidence is a function that reads/writes it with a known meaning, never REPENTOGON's offset.
|
||
**A local variable** in a function you have read closely: `function<TAB>local_2c0<TAB>newName[<TAB>C type]` in
|
||
`db/locals.tsv`. Only when every use has the same role: MSVC reuses registers and stack slots (see the file header).
|
||
Name-only rows reach the dump with `apply_db.py`; rows with a type (e.g. `Vector` for an x/y pair) need `--db`.
|
||
3. **Re-apply to Ghidra** after any of them: `./build.sh --db` (about 10 min: updates the project and re-decompiles every
|
||
db function into the dump, typed). Field changes reach every function only with `./build.sh --db --full` (about 50 min).
|
||
4. **Behaviour** (states, timings, speeds, odds, bugs): write it in `notes/<entities|items|characters|systems>/<name>.md`
|
||
with a `## name (NpcAI::ENTITY_X 0xADDR, type.variant)` heading, so `python3 -I scripts/notes_index.py notes`
|
||
puts it in the index. Quote addresses, not copies of decompiled C: `./decompile.sh 0xADDR` reprints it with the
|
||
newest names.
|
||
5. Commit: `git add -A && git commit -m "names/notes: <what>"`. Several agents may work at once; commit only your own
|
||
files, and if two of you touch `db/names.tsv`, keep both lines.
|
||
|
||
## After a game update
|
||
|
||
1. `./build.sh` (about 75 min) copies the new `isaac-ng.exe` into `bin/`, re-imports, re-applies every name source
|
||
and `db/`, and redumps. Set `GAME=/path/to/game` if the install is elsewhere.
|
||
2. If the NPC dispatcher or Lua-registration address moved, update `NPC_DISPATCH` and `LUA_REG` at the top of
|
||
`build.sh` first. Find them with `find.sh callees Entity_NPC::Update` (the callee with a 264-case switch) and
|
||
`find.sh str '^UseActiveItem$'`.
|
||
3. **`db/names.tsv` and `notes/` are keyed by address, and addresses move.** Before rebuilding, record each db
|
||
function's strings (`./find.sh strs ADDR`) so you can find it again in the new build (`./find.sh str`, then
|
||
`find.sh callers`). Rewrite the addresses, then `./build.sh --db`. Pull a newer REPENTOGON first
|
||
(`git -C repentogon pull`) if it supports the new version.
|
||
4. Commit the result as one commit naming the game version.
|
||
|
||
## Setting up from scratch
|
||
|
||
Git keeps everything except the large tools and inputs (see `.gitignore`). On a fresh checkout:
|
||
|
||
- `jdk/`: a Temurin 21 JDK (portable tarball, unpacked as `jdk/`). System Java 8 is too old.
|
||
- `ghidra_12.2_PUBLIC/`: Ghidra 12.2 release zip, unpacked here.
|
||
- `repentogon/`: `git clone --depth 1 https://github.com/TeamREPENTOGON/REPENTOGON.git repentogon` (the dump was
|
||
built from commit e2c5aecacbbb83bb5938cfa89c0a58b78e616a36). `find.sh enum` and `build.sh` read it.
|
||
- `bin/isaac-ng.exe`: `build.sh` copies it from the game install.
|