70 lines
5.2 KiB
Bash
Executable File
70 lines
5.2 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
|
|
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"
|
|
grep -hv '^#' "$D/zhl_fields.tsv" "$D/lua_fields.tsv" "$D/db/fields.tsv" > "$D/build_fields.tsv" # later lines win
|
|
G -process isaac-ng.exe -noanalysis -postScript ApplyTypes.java "$D/build_fields.tsv"
|
|
G -process isaac-ng.exe -noanalysis -postScript ApplyProtos.java "$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
|
|
P headers.py "$D/src/structs.hpp" "$D/zhl_fields.tsv" "$D/lua_fields.tsv" "$D/db/fields.tsv"
|
|
echo '#include "structs.hpp"' | g++ -std=c++17 -fsyntax-only -I"$D/src" -x c++ - \
|
|
|| echo "src/structs.hpp does not compile: a db/fields.tsv offset contradicts another" >&2
|
|
}
|
|
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
|
|
command mv "$D/dump" "$D/dump.old" && command mv "$O" "$D/dump" && command rm -rf "$D/dump.old"
|
|
}
|
|
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"
|
|
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
|