From 2e4322b8a53385264a7f7017cf6d5b9d64ed22a3 Mon Sep 17 00:00:00 2001 From: shweet Date: Sun, 25 Oct 2026 16:36:51 -0400 Subject: [PATCH] db/globals.tsv + ApplyGlobals.java: label and type global data; apply_db.py 1000x faster (token match + dict lookup) Co-Authored-By: Claude Opus 5.5 --- README.md | 4 ++- build.sh | 1 + db/globals.tsv | 1 + scripts/ApplyGlobals.java | 51 +++++++++++++++++++++++++++++++++++++++ scripts/apply_db.py | 17 ++++++++++--- 5 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 db/globals.tsv create mode 100644 scripts/ApplyGlobals.java diff --git a/README.md b/README.md index 85f68a0..288124d 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ it, or paste large parts of it anywhere public. The game install is only read, n ``` db/ YOUR names and types: names.tsv (address -> name, beats every other source), fields.tsv (struct fields), - locals.tsv (local variables inside one function) + locals.tsv (local variables inside one function), globals.tsv (global data), protos.zhl (signatures) 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) @@ -240,6 +240,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 global** (a `DAT_
` you identified, e.g. the Game singleton): `addressg_nameC typeevidence` + in `db/globals.tsv`. Pointer types (`Game*`) type every access; `apply_db.py` renames it in the dump at once. **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 diff --git a/build.sh b/build.sh index b9de865..c23dfb6 100755 --- a/build.sh +++ b/build.sh @@ -21,6 +21,7 @@ apply_db() { # db names -> struct types (zhl + db fields) || echo "src/structs.hpp does not compile: a db/fields.tsv offset contradicts another" >&2 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 } diff --git a/db/globals.tsv b/db/globals.tsv new file mode 100644 index 0000000..896ddc0 --- /dev/null +++ b/db/globals.tsv @@ -0,0 +1 @@ +# address name C type evidence Global data, labelled and typed by scripts/ApplyGlobals.java; apply_db.py renames DAT_
in the dump. diff --git a/scripts/ApplyGlobals.java b/scripts/ApplyGlobals.java new file mode 100644 index 0000000..f8f4d50 --- /dev/null +++ b/scripts/ApplyGlobals.java @@ -0,0 +1,51 @@ +// Applies db/globals.tsv (address, name, C type, evidence): labels the global and lays down its type, so a Game* global +// makes every access read g_game->_field. Types: primitives, a struct name, or pointers to them ("Game*"). Arg: globals.tsv +import ghidra.app.script.GhidraScript; +import ghidra.program.model.address.Address; +import ghidra.program.model.data.*; +import ghidra.program.model.symbol.SourceType; +import java.nio.file.*; + +public class ApplyGlobals extends GhidraScript { + DataType type(DataTypeManager dtm, String t) { + t = t.replace("const ", "").trim(); + int ptr = 0; + while (t.endsWith("*")) { ptr++; t = t.substring(0, t.length() - 1).trim(); } + DataType b; + switch (t) { + case "void": b = VoidDataType.dataType; break; + case "bool": case "char": case "uint8_t": case "int8_t": b = ByteDataType.dataType; break; + case "short": case "uint16_t": case "int16_t": b = ShortDataType.dataType; break; + case "int": case "int32_t": b = IntegerDataType.dataType; break; + case "uint32_t": case "unsigned int": b = UnsignedIntegerDataType.dataType; break; + case "float": b = FloatDataType.dataType; break; + case "double": b = DoubleDataType.dataType; break; + case "uint64_t": case "int64_t": b = QWordDataType.dataType; break; + default: b = dtm.getDataType(new CategoryPath("/"), t); + } + if (b == null) b = ptr > 0 ? VoidDataType.dataType : null; + if (b == null) return null; + for (int i = 0; i < ptr; i++) b = new PointerDataType(b); + return b; + } + + public void run() throws Exception { + DataTypeManager dtm = currentProgram.getDataTypeManager(); + int n = 0, typed = 0; + for (String line : Files.readAllLines(Paths.get(getScriptArgs()[0]))) { + if (line.isBlank() || line.startsWith("#")) continue; + String[] c = line.split("\t"); + Address a = toAddr(c[0].replace("0x", "")); + createLabel(a, c[1], true, SourceType.USER_DEFINED); + n++; + DataType t = c.length > 2 ? type(dtm, c[2]) : null; + if (t == null || t instanceof VoidDataType) continue; + try { + clearListing(a, a.add(t.getLength() - 1)); + createData(a, t); + typed++; + } catch (Exception e) { println("ApplyGlobals: cannot type " + c[0] + " as " + c[2] + ": " + e.getMessage()); } + } + println("ApplyGlobals: " + n + " globals, " + typed + " typed"); + } +} diff --git a/scripts/apply_db.py b/scripts/apply_db.py index 73dece1..7c33d42 100644 --- a/scripts/apply_db.py +++ b/scripts/apply_db.py @@ -14,9 +14,12 @@ for l in open(db): # By address everywhere: "//@@ addr name" headers, functions.tsv, and addr:name user lists in strings.tsv/npc_ai.tsv. # Call sites only show the old name, so they are rewritten only where that old name belonged to one function. old2new = {fns[a]: n for a, n in ren.items() if fns[a] != n and count[fns[a]] == 1} -calls = re.compile(r'(? -> the global's name +gl = {} +for l in open(db.replace('names.tsv', 'globals.tsv')): + c = l.rstrip('\n').split('\t') + if l.strip() and not l.startswith('#'): gl['DAT_%08x' % int(c[0], 16)] = c[1] +dat = re.compile(r'\bDAT_[0-9a-f]{8}\b') +_fix = fix +def fix(s): return dat.sub(lambda m: gl.get(m.group(0), m.group(0)), _fix(s)) if gl else _fix(s) for f in ('all.c', 'strings.tsv', 'npc_ai.tsv'): p = f'{d}/{f}'; s = open(p).read(); open(p, 'w').write(fix_locals(fix(s)) if f == 'all.c' else fix(s)) rows = [l.split('\t', 2) for l in open(f'{d}/functions.tsv')]