Files
isaac-port/build.sh
T
shweetandClaude Opus 5.5 3ce74c6e2d Reconstruction layout: generated-src/ (generated) and src/ (curated), CMake, the first takeover: RNG
The decompiled C++ tree moves to generated-src/ (classes.h, C++20); src/ is for code written by hand. A curated function
defines the generated declaration and starts its comment with its address, and source_tree.py stops emitting that body.
src/rng.cpp takes over RNG (SetSeed, the constructor, Next, RandomInt, RandomFloat, Shuffle) with the shift table read
from the exe as a constexpr array; src/log.h names KAGE_LogMessage's levels (1 INFO, 2 NET, 4 WARN, 8 ERROR, 16 ASSERT,
from its prefix jump table). CMakeLists.txt builds src/*.cpp against generated-src/classes.h (clean with -Wall -Wextra).
structs.hpp and headers.py are retired: scripts/merge_fields.py writes the merged field list, classes.h checks the layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 21:10:23 -05:00

79 lines
6.1 KiB
Bash
Executable File

#!/bin/bash
# Rebuilds everything from the game exe (e.g. after a game update). ~25 min analysis + 2 dumps of ~25 min.
# build.sh [--keep-analysis] (skip the re-import/auto-analysis, just redo names/types/dump)
# build.sh --db (re-apply db/ to the project, re-decompile just the db functions into the dump, ~1 min)
# build.sh --db --full (same, then redump everything, ~50 min: needed after db/fields.tsv changes)
# Steps: import+auto-analysis -> ZHL sig/vtable names -> Lua-registration names -> NPC AI table
# -> REPENTOGON struct fields this exe confirms (getters, Lua field table) -> struct/this types -> final dump.
set -e
D="$(cd "$(dirname "$0")" && pwd)"
export JAVA_HOME="$D/jdk" GHIDRA_MAXMEM=24G JAVA_TOOL_OPTIONS="-Djava.io.tmpdir=$D/.tmp" # not /tmp: its user quota is shared
mkdir -p "$D/.tmp"
G() { "$D/ghidra_12.2_PUBLIC/support/analyzeHeadless" "$D/project" isaac "$@" -scriptPath "$D/scripts" >> "$D/build.log" 2>&1; }
P() { python3 -I "$D/scripts/$@"; }
LUA_REG=00866960 # function that registers the Lua API (references "UseActiveItem" etc.; find.sh str '^UseActiveItem$')
NPC_DISPATCH=006c2990 # Entity_NPC_UpdateAI_dispatch (callee of Entity_NPC::Update with a jump table on type-10)
GAME="${GAME:-$HOME/.steam/steam/steamapps/common/The Binding of Isaac Rebirth}"
apply_db() { # db names -> struct types (zhl + db fields)
G -process isaac-ng.exe -noanalysis -postScript ApplyDb.java "$D/db/names.tsv"
P vtable.py "$D/bin/isaac-ng.exe" "$D/dump/functions.tsv" "$D/zhl_vtables.tsv" --classes "$D/dump/all.c" > "$D/classes.tsv" # RTTI: size, bases, slots
P merge_fields.py "$D/build_fields.tsv" "$D/zhl_fields.tsv" "$D/lua_fields.tsv" "$D/db/fields.tsv" \
<(awk -F'\t' '!/^#/ && $4 != "-" {print $1 "\tsize\t" $4}' "$D/classes.tsv") # later rows win, "-" deletes
P this_types.py "$D/dump/all.c" "$D/build_fields.tsv" "$D/build_this.tsv"
G -process isaac-ng.exe -noanalysis -postScript ApplyTypes.java "$D/build_fields.tsv" "$D/build_this.tsv"
G -process isaac-ng.exe -noanalysis -postScript ApplyGlobals.java "$D/db/globals.tsv"
G -process isaac-ng.exe -noanalysis -postScript ApplyProtos.java "$D/db" "$D/repentogon/libzhl/functions" # only where RET n agrees
G -process isaac-ng.exe -noanalysis -postScript ApplyLocals.java "$D/db/locals.tsv" # after types: names refer to that decompile
}
types_dump() { # final dump, built in dump.new and swapped in at the end so readers never see a half-written dump
O="$D/dump.new"; command rm -rf "$O"
G -process isaac-ng.exe -noanalysis -readOnly -postScript DumpAll.java "$O"
P lua_bindings.py "$D/bin/isaac-ng.exe" "$O/all.c" "$O"
command cp -f "$D/npc_ai.tsv" "$O/"; command mv -f "$D/build_fields.tsv" "$O/zhl_fields.tsv"
command rm -f "$O/lua_names.tsv"
# the dump gives up after 900 s; retry the big named ones live (UseActiveItem takes ~30-60 min) and splice them in.
# With --db --full the exe is unchanged, so the previous dump's copy is reused instead (only newer types missing).
for A in $(grep -B1 'decompile failed' "$O/all.c" | awk '/^\/\/@@ / && $3 !~ /^FUN_/ {print $2}'); do
if [ -n "$REUSE" ]; then "$D/decompile.sh" "$A" 1 > "$D/retry.c" # an old failure stays a failure, no 2 h retry
else DECOMP_TIMEOUT=7200 "$D/decompile.sh" --live "$A" > "$D/retry.c"; fi
P splice.py "$O/all.c" "$D/retry.c" || true
done
command rm -f "$D/retry.c"
P apply_db.py "$D/db/names.tsv" "$O" # npc_ai.tsv was written before ApplyDb
n() { grep -c '^//@@ ' "$1/all.c" || true; } # a dump cut short (disk full, crash) must not replace a good one
[ ! -f "$D/dump/all.c" ] || [ "$(n "$O")" -ge "$(( $(n "$D/dump") * 99 / 100 ))" ] || { echo "dump.new has $(n "$O") functions, dump/ $(n "$D/dump"): kept dump/" >&2; exit 1; }
command mv "$D/dump" "$D/dump.old" && command mv "$O" "$D/dump" && command rm -rf "$D/dump.old"
source_tree
}
source_tree() { # generated-src/: the decompiled C++ tree (classes.h + one .cpp per class) for 64-bit Linux, minus what src/ took over
command rm -rf "$D/generated-src" && mkdir -p "$D/generated-src"
P source_tree.py "$D/generated-src" "$D/classes.tsv" "$D/dump" "$D/zhl_vtables.tsv" --check
}
if [ "$1" = --db ]; then
apply_db
[ "$2" = --full ] && { REUSE=1 types_dump; exit; }
RX="^($( (grep -v '^#' "$D/db/names.tsv" | cut -f2; grep -v '^#' "$D/db/locals.tsv" | grep . | cut -f1 | while read A; do
awk -F'\t' -v a="$(printf '%08x' $((16#${A#0x})))" '$1 == a "" {print $2}' "$D/dump/functions.tsv"; done) | sort -u | sed 's/[][().*+?^$|]/\\&/g' | paste -sd'|'))\$"
DECOMP_TIMEOUT=900 "$D/decompile.sh" --live "$RX" 100000 | awk -v d="$D" '/^\/\/@@ /{f=d "/retry_" $2 ".c"} {print > f}'
for F in "$D"/retry_*.c; do P splice.py "$D/dump/all.c" "$F" || true; done
command rm -f "$D"/retry_*.c "$D/build_fields.tsv" "$D/build_this.tsv"
P apply_db.py "$D/db/names.tsv" "$D/dump"; exit
fi
command cp -f "$GAME/isaac-ng.exe" "$D/bin/"
[ "$1" = --keep-analysis ] || G -import "$D/bin/isaac-ng.exe" -overwrite
P zhl_match.py "$D/bin/isaac-ng.exe" "$D/repentogon/libzhl/functions" "$D/zhl_matches.tsv"
G -process isaac-ng.exe -noanalysis -postScript ApplyNames.java "$D/zhl_matches.tsv" "$D/zhl_vtables.tsv"
G -process isaac-ng.exe -noanalysis -readOnly -postScript DumpAll.java "$D/dump"
P lua_bindings.py "$D/bin/isaac-ng.exe" "$D/dump/all.c" "$D"
cut -f1 "$D/lua_bindings.tsv" | sort -u > "$D/lua_classes.txt"
G -process isaac-ng.exe -noanalysis -readOnly -postScript LuaAsm.java $LUA_REG "$D/lua_classes.txt" "$D/lua_asm.tsv"
cat "$D/lua_names.tsv" "$D/lua_asm.tsv" | awk -F'\t' '!seen[$1]++' > "$D/lua_all.tsv"
G -process isaac-ng.exe -noanalysis -postScript ApplyNames.java "$D/zhl_matches.tsv" "$D/zhl_vtables.tsv" "$D/lua_all.tsv"
G -process isaac-ng.exe -noanalysis -postScript NpcTable.java $NPC_DISPATCH "$D/repentogon/libzhl/functions/Global.zhl" "$D/npc_ai.tsv"
P zhl_fields.py "$D/dump/all.c" "$D/repentogon/libzhl/functions" "$D/lua_bindings.tsv" "$D/zhl_fields.tsv" "$D/lua_all.tsv"
P lua_fields.py "$D/dump/all.c" "$D/lua_names.tsv" "$D/lua_asm.tsv" "$D/zhl_fields.tsv" "$D/lua_fields.tsv"
apply_db
types_dump