From e285a400162988fa2b5739c5d19e55d3c1386510 Mon Sep 17 00:00:00 2001 From: shweet Date: Sat, 24 Oct 2026 22:50:46 -0400 Subject: [PATCH] db/protos.zhl: own function signatures, applied like REPENTOGON's only where RET n agrees Co-Authored-By: Claude Opus 5.5 --- README.md | 6 ++++-- build.sh | 2 +- db/protos.zhl | 2 ++ scripts/ApplyProtos.java | 10 ++++++---- 4 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 db/protos.zhl diff --git a/README.md b/README.md index 20a2cd3..9353554 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ read from the game's own trivial Lua getters, e.g. Entity 0x334 `_targetPosition 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` +600 functions, plus `db/protos.zhl`; the comment says `prototype: REPENTOGON, confirmed by RET n` or names the db file). 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 @@ -218,7 +218,7 @@ $ ./find.sh active 33 25 automatically and splices them in (`scripts/splice.py`). For anything else use `--asm`. - Item, trinket and player lookups are grep heuristics. Struct fields not confirmed in this exe are `field_0x...`. A function with no confirmed prototype can still show `in_ECX + 0x13c0` or odd parameter types guessed by the - decompiler; if you work out its signature, that is worth a db entry. + decompiler; if you work out its signature, add it to `db/protos.zhl`. ## Adding findings back @@ -237,6 +237,8 @@ finding only in your scratch files or your final message. **A local variable** in a function you have read closely: `functionlocal_2c0newName[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`. + **A function signature**: one REPENTOGON-style line in `db/protos.zhl`, e.g. + `__thiscall int Entity_NPC::FooBar(int count, Vector* pos);`. It is applied only if the callee's `RET n` agrees. 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//.md` diff --git a/build.sh b/build.sh index 6b31276..ed6f5e0 100755 --- a/build.sh +++ b/build.sh @@ -18,7 +18,7 @@ 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 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 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++ - \ diff --git a/db/protos.zhl b/db/protos.zhl new file mode 100644 index 0000000..f89389f --- /dev/null +++ b/db/protos.zhl @@ -0,0 +1,2 @@ +// Function signatures you have worked out, REPENTOGON syntax, one per line. Applied only where the callee's RET n agrees +// with the declared stack parameters (scripts/ApplyProtos.java). Tried before REPENTOGON's declaration of the same name. diff --git a/scripts/ApplyProtos.java b/scripts/ApplyProtos.java index 8b28211..5e8f722 100644 --- a/scripts/ApplyProtos.java +++ b/scripts/ApplyProtos.java @@ -1,6 +1,6 @@ // Applies REPENTOGON's function declarations (zhl dir) as prototypes, but only where this exe confirms them: for // __thiscall/__stdcall/__fastcall the callee pops its stack arguments (RET n), so the declared stack parameter sizes must -// add up to the purge Ghidra found; __cdecl must purge 0. Overloads pick the declaration that fits. Arg: zhl dir +// add up to the purge Ghidra found; __cdecl must purge 0. Overloads pick the declaration that fits. Args: dirs of *.zhl import ghidra.app.script.GhidraScript; import ghidra.program.model.data.*; import ghidra.program.model.listing.*; @@ -40,10 +40,12 @@ public class ApplyProtos extends GhidraScript { dtm = currentProgram.getDataTypeManager(); Pattern decl = Pattern.compile("^\\s*(?:static\\s+)?(__thiscall|__stdcall|__cdecl|__fastcall)\\s+(.+?)\\s*([\\w:]+::~?\\w+)\\s*\\((.*?)\\)\\s*;"); Map> byName = new HashMap<>(); - for (Path p : Files.newDirectoryStream(Paths.get(getScriptArgs()[0]), "*.zhl")) + Map from = new IdentityHashMap<>(); + for (String dir : getScriptArgs()) // db/ first: its declarations are tried before REPENTOGON's + for (Path p : Files.newDirectoryStream(Paths.get(dir), "*.zhl")) for (String l : Files.readAllLines(p, java.nio.charset.StandardCharsets.ISO_8859_1)) { Matcher m = decl.matcher(l); - if (m.find()) byName.computeIfAbsent(m.group(3).replace("_", ""), k -> new ArrayList<>()).add(m); + if (m.find()) { byName.computeIfAbsent(m.group(3).replace("_", ""), k -> new ArrayList<>()).add(m); from.put(m, dir.contains("repentogon") ? "REPENTOGON" : "db/" + p.getFileName()); } } int applied = 0, rejected = 0, unknown = 0; for (Function f : currentProgram.getListing().getFunctions(true)) { @@ -76,7 +78,7 @@ public class ApplyProtos extends GhidraScript { f.setReturnType(ret, SourceType.USER_DEFINED); f.replaceParameters(Function.FunctionUpdateType.DYNAMIC_STORAGE_FORMAL_PARAMS, true, SourceType.USER_DEFINED, ps.toArray(new ParameterImpl[0])); String cm = f.getComment() == null ? "" : f.getComment() + "\n"; - if (!cm.contains("prototype:")) f.setComment(cm + "prototype: REPENTOGON, confirmed by RET " + purge); + if (!cm.contains("prototype:")) f.setComment(cm + "prototype: " + from.get(m) + ", confirmed by RET " + purge); applied++; done = true; break; } if (!done) rejected++;