Tidy the repo root: build.sh's derived tables into analysis/, build.sh, decompile.sh and find.sh into scripts/, stale by-products removed, .gitignore regrouped, README describes the src/ layout and the test harness
This commit is contained in:
1 parent
aefd3e2098
commit
7d407d5c19
17 files changed
+140
-86
No files matched your search
Executable
+78
@@ -0,0 +1,78 @@
|
||||
#!/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/analysis/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/analysis/zhl_vtables.tsv" --classes "$D/dump/all.c" > "$D/analysis/classes.tsv" # RTTI: size, bases, slots
|
||||
P merge_fields.py "$D/analysis/build_fields.tsv" "$D/analysis/zhl_fields.tsv" "$D/analysis/lua_fields.tsv" "$D/db/fields.tsv" \
|
||||
<(awk -F'\t' '!/^#/ && $4 != "-" {print $1 "\tsize\t" $4}' "$D/analysis/classes.tsv") # later rows win, "-" deletes
|
||||
P this_types.py "$D/dump/all.c" "$D/analysis/build_fields.tsv" "$D/analysis/build_this.tsv"
|
||||
G -process isaac-ng.exe -noanalysis -postScript ApplyTypes.java "$D/analysis/build_fields.tsv" "$D/analysis/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/analysis/npc_ai.tsv" "$O/"; command mv -f "$D/analysis/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/scripts/decompile.sh" "$A" 1 > "$D/analysis/retry.c" # an old failure stays a failure, no 2 h retry
|
||||
else DECOMP_TIMEOUT=7200 "$D/scripts/decompile.sh" --live "$A" > "$D/analysis/retry.c"; fi
|
||||
P splice.py "$O/all.c" "$D/analysis/retry.c" || true
|
||||
done
|
||||
command rm -f "$D/analysis/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/analysis/classes.tsv" "$D/dump" "$D/analysis/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/scripts/decompile.sh" --live "$RX" 100000 | awk -v d="$D" '/^\/\/@@ /{f=d "/analysis/retry_" $2 ".c"} {print > f}'
|
||||
for F in "$D/analysis"/retry_*.c; do P splice.py "$D/dump/all.c" "$F" || true; done
|
||||
command rm -f "$D/analysis"/retry_*.c "$D/analysis/build_fields.tsv" "$D/analysis/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/analysis/zhl_matches.tsv"
|
||||
G -process isaac-ng.exe -noanalysis -postScript ApplyNames.java "$D/analysis/zhl_matches.tsv" "$D/analysis/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/analysis"
|
||||
cut -f1 "$D/analysis/lua_bindings.tsv" | sort -u > "$D/analysis/lua_classes.txt"
|
||||
G -process isaac-ng.exe -noanalysis -readOnly -postScript LuaAsm.java $LUA_REG "$D/analysis/lua_classes.txt" "$D/analysis/lua_asm.tsv"
|
||||
cat "$D/analysis/lua_names.tsv" "$D/analysis/lua_asm.tsv" | awk -F'\t' '!seen[$1]++' > "$D/analysis/lua_all.tsv"
|
||||
G -process isaac-ng.exe -noanalysis -postScript ApplyNames.java "$D/analysis/zhl_matches.tsv" "$D/analysis/zhl_vtables.tsv" "$D/analysis/lua_all.tsv"
|
||||
G -process isaac-ng.exe -noanalysis -postScript NpcTable.java $NPC_DISPATCH "$D/repentogon/libzhl/functions/Global.zhl" "$D/analysis/npc_ai.tsv"
|
||||
P zhl_fields.py "$D/dump/all.c" "$D/repentogon/libzhl/functions" "$D/analysis/lua_bindings.tsv" "$D/analysis/zhl_fields.tsv" "$D/analysis/lua_all.tsv"
|
||||
P lua_fields.py "$D/dump/all.c" "$D/analysis/lua_names.tsv" "$D/analysis/lua_asm.tsv" "$D/analysis/zhl_fields.tsv" "$D/analysis/lua_fields.tsv"
|
||||
apply_db
|
||||
types_dump
|
||||
@@ -45,13 +45,13 @@ How to take over a class:
|
||||
- Names: keep Rebirth's names for the class and for methods the game has (ANM2::Play, StringTable::GetString, ItemConfig::Load ...). Name fields in camelCase without a leading underscore (_spriteSheetPath becomes spriteSheetPath). Use REPENTOGON's names (repentogon/libzhl/functions/*.zhl) where they exist. Everything else follows CLAUDE.md: noun_verb snake_case free functions, camelCase locals, "is" booleans.
|
||||
- Method signatures may become idiomatic (std::string_view instead of std_string*, references instead of pointers, enums instead of ints) when that is clearer. Keep the meaning.
|
||||
- Each method the original has gets a comment directly above it that starts with its address (or addresses, if you merged several), then one short sentence: "// 0040bd50 Loads the .anm2 at path, sharing parsed files through the cache." The generator stops emitting a function once src/ carries its address.
|
||||
- Take over the WHOLE class you are assigned: every method the class has in the original that the game needs (see generated-src/classes.h for the list and generated-src/<class>.cpp, ./decompile.sh <address>, ./decompile.sh --asm <address> N for bodies).
|
||||
- Take over the WHOLE class you are assigned: every method the class has in the original that the game needs (see generated-src/classes.h for the list and generated-src/<class>.cpp, scripts/decompile.sh <address>, scripts/decompile.sh --asm <address> N for bodies).
|
||||
- Collapse compiler-generated ones (copy constructors, operator_assign, destructors that only free members) into the language defaults where std members make them unnecessary, and still list their addresses in a comment above the class so the generator drops them: "// Copies, assignment and destruction (0040b000, 0040b1d0, 0040ae70) are the defaults."
|
||||
- Behaviour must match the original exactly: same results, same order of effects, same edge cases. Where the original crashes or has undefined behaviour, pick a safe behaviour and say so in one comment line.
|
||||
- Data: XML is converted by the import tool into JSON under resources/ (lowercase paths, ".json" appended). Read it through src/engine/data.h: data_load, node_children, node_tag, node_attr, node_text. Element order is preserved and attribute values are the original strings. A file with several roots becomes {"tag":"#document","children":[...]}. Convert values exactly as the original did. resources/ holds the real converted game files: look at them.
|
||||
- Other classes: call whatever classes.h declares for classes nobody has taken over (they are stubs for now). Do NOT edit src/ files other than your own: other agents are rewriting other classes at the same time. If a round 1 file outside your assignment calls something you changed, list the needed call-site change in your report.
|
||||
- Every file you write must compile with no warnings: clang++ -std=c++20 -fsyntax-only -Wall -Wextra -I src -isystem ${R}/gen_\${i} -isystem external/simdjson/singleheader -isystem external/sdl/include -isystem external/lua -isystem external/stb src/<file>.cpp
|
||||
- First regenerate your private header with python3 -I scripts/source_tree.py ${R}/gen_\${i} classes.tsv dump zhl_vtables.tsv. Rerun it after creating your .h, because the generator drops the classes your header defines.
|
||||
- First regenerate your private header with python3 -I scripts/source_tree.py ${R}/gen_\${i} analysis/classes.tsv dump analysis/zhl_vtables.tsv. Rerun it after creating your .h, because the generator drops the classes your header defines.
|
||||
- Round 1 files that depend on your old API may stop compiling. That is expected, so only check your own files.
|
||||
- Style details from CLAUDE.md:
|
||||
- Allman braces; a one-line body stays on one line without braces.
|
||||
|
||||
Executable
+38
@@ -0,0 +1,38 @@
|
||||
#!/bin/bash
|
||||
# Usage: decompile.sh [--live|--asm] QUERY [MAX] | decompile.sh --case CASEADDR...
|
||||
# QUERY = entry/inner address (0x6c0f30 / 006c0f30) or a regex on the qualified name (grep -E).
|
||||
# Default reads the pre-dumped corpus dump/all.c (instant, concurrency-safe).
|
||||
# --asm ADDR [N] prints N (default 20) instructions from ADDR (for functions the decompiler times out on).
|
||||
# --case decompiles single switch cases of a function the decompiler times out on (Entity_Effect::Update): each
|
||||
# CASEADDR is a jump-table target; the switch is patched to jump straight there in a throwaway copy (DecompileCase.java).
|
||||
# --live runs the Ghidra decompiler on a copy of the saved project (~25s; env DECOMP_TIMEOUT=secs, default 180).
|
||||
D="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
LIVE=0; SCRIPT=Decompile.java
|
||||
[ "$1" = --live ] && { LIVE=1; shift; }
|
||||
[ "$1" = --asm ] && { LIVE=1; SCRIPT=Disasm.java; shift; }
|
||||
if [ "$1" = --case ]; then
|
||||
shift; TMP=$(mktemp -d); export JAVA_HOME="$D/jdk"
|
||||
cp -r "$D/project" "$TMP/project"; rm -f "$TMP"/project/*.lock*
|
||||
"$D/ghidra_12.2_PUBLIC/support/analyzeHeadless" "$TMP/project" isaac -process isaac-ng.exe -noanalysis -readOnly \
|
||||
-scriptPath "$D/scripts" -postScript DecompileCase.java "$TMP/out.c" "$@" >/dev/null 2>&1
|
||||
cat "$TMP/out.c"; rm -rf "$TMP"; exit
|
||||
fi
|
||||
Q="$1"; MAX="${2:-20}"
|
||||
[ -z "$Q" ] && { sed -n '2,7p' "$0"; exit 1; }
|
||||
if [[ "$Q" =~ ^(0x)?[0-9a-fA-F]{6,8}$ ]]; then
|
||||
A=$(printf '%08x' $((16#${Q#0x})))
|
||||
# resolve inner address to containing function (largest entry <= A)
|
||||
Q=$(awk -F'\t' -v a="$A" '($1 "") <= (a "") {best=$1} END{print best}' <(sort "$D/dump/functions.tsv"))
|
||||
MODE=addr; ASMADDR=$A
|
||||
fi
|
||||
if [ $LIVE = 1 ]; then
|
||||
# Ghidra locks a project even read-only, so each live run works on a private copy (~160MB, tmpfs-fast)
|
||||
TMP=$(mktemp -d); OUT="$TMP/out.c"; export JAVA_HOME="$D/jdk"
|
||||
cp -r "$D/project" "$TMP/project"; rm -f "$TMP"/project/*.lock*
|
||||
"$D/ghidra_12.2_PUBLIC/support/analyzeHeadless" "$TMP/project" isaac -process isaac-ng.exe -noanalysis -readOnly \
|
||||
-scriptPath "$D/scripts" -postScript $SCRIPT "${ASMADDR:-$Q}" "$MAX" "$OUT" >/dev/null 2>&1
|
||||
cat "$OUT"; rm -rf "$TMP"; exit
|
||||
fi
|
||||
awk -v q="$Q" -v mode="$MODE" -v max="$MAX" '
|
||||
/^\/\/@@ / { on = mode == "addr" ? ($2 == q "") : ($3 ~ q); if (on && ++n > max) { print "// ... more matches (raise MAX)"; exit } }
|
||||
on' "$D/dump/all.c"
|
||||
Executable
+54
@@ -0,0 +1,54 @@
|
||||
#!/bin/bash
|
||||
# Usage: find.sh MODE ARG
|
||||
# name REGEX functions whose qualified name matches (addr name size)
|
||||
# str REGEX strings matching, with the functions using them (addr "string" users)
|
||||
# strs X strings a function uses (anm2 animations/events, sounds, paths)
|
||||
# code REGEX functions whose decompiled C contains REGEX (addr name hits)
|
||||
# callers X who calls X (address or exact name)
|
||||
# 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 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)
|
||||
# val ADDR constant at DAT_ADDR as int/float/double/string (decompiler leaves floats as DAT_)
|
||||
# field REGEX known struct field offsets (REPENTOGON zhl struct defs)
|
||||
# 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,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
|
||||
}
|
||||
name_of() { awk -F'\t' 'NR==FNR {n[$1]=$2; next} {print $0 "\t" n[$1]}' "$T/functions.tsv" -; }
|
||||
case "$MODE" in
|
||||
name) grep -E -- "$ARG" <(cut -f1-3 "$T/functions.tsv") ;;
|
||||
str) awk -F'\t' -v r="$ARG" '$2 ~ r' "$T/strings.tsv" ;;
|
||||
code) awk -v r="$ARG" '/^\/\/@@ / {h=$2" "$3; next} $0 ~ r {c[h]++} END {for (k in c) print k, c[k]}' "$T/all.c" | sort ;;
|
||||
strs) for a in $(addr_of "$ARG"); do awk -F'\t' -v a="$a:" 'index($3, a) {print $2}' "$T/strings.tsv"; "$D/scripts/decompile.sh" "$a" | grep -o '"[^"]*"' | tr -d '"'; done | sort -u ;;
|
||||
callers) for a in $(addr_of "$ARG"); do awk -F'\t' -v a="$a" '$2 == a "" {print $1}' "$T/calls.tsv"; done | sort -u | name_of ;;
|
||||
callees) for a in $(addr_of "$ARG"); do awk -F'\t' -v a="$a" '$1 == a "" {print $2}' "$T/calls.tsv"; done | sort -u | name_of ;;
|
||||
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|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/analysis/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/scripts/decompile.sh" "^Entity_Player::$F\$" | awk -v r="$LABEL" -v a="$ANCHOR" -v n="${3:-60}" \
|
||||
-v t="LAB_$TARGET:" 'a == "-" || index($0, a) {on = 1} (on && !c && $0 ~ r && ++c) || index($0, t) == 1 {w = n; print "---- line " NR} w > 0 {print; w--}' ;; # also the case code under its LAB_ label (a case that is only a goto)
|
||||
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/^ *//' ;;
|
||||
lua) grep -P -- "$ARG" "$T/lua_bindings.tsv" ;;
|
||||
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/analysis/zhl_vtables.tsv" "$ARG" "${3:-60}" ;;
|
||||
*) sed -n '2,19p' "$0"; exit 1 ;;
|
||||
esac
|
||||
Reference in new issue
Block a user