Ghidra 12.2 project and full dump of isaac-ng.exe v1.9.7.17, query scripts, notes indexed by entity, anm2/rooms reimplementations, and db/ (names + fields) that build.sh re-applies. Fixes awk numeric address comparison in find.sh/decompile.sh. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
44 lines
2.2 KiB
Python
44 lines
2.2 KiB
Python
# Extracts the game's Lua API registrations from the decompiled corpus.
|
|
# Usage: lua_bindings.py exe all.c out_dir -> lua_bindings.tsv (class, member, target addr or field offset)
|
|
# lua_names.tsv (va, Class::Member, lua, decl) for ApplyNames
|
|
import re, sys, os, struct
|
|
exe, src, outdir = sys.argv[1:4]
|
|
data = open(exe, 'rb').read()
|
|
pe = struct.unpack_from('<I', data, 0x3c)[0]
|
|
optsz = struct.unpack_from('<H', data, pe + 20)[0]
|
|
base = struct.unpack_from('<I', data, pe + 52)[0]
|
|
secs = [struct.unpack_from('<III', data, pe + 24 + optsz + 40 * i + 12) for i in range(struct.unpack_from('<H', data, pe + 6)[0])]
|
|
def cstr(m): # short strings Ghidra left as &DAT_xxxxxxxx -> "literal"
|
|
va = int(m.group(1), 16) - base
|
|
for sva, rsz, raw in secs:
|
|
if sva <= va < sva + rsz:
|
|
off = raw + va - sva
|
|
s = data[off:data.index(b'\0', off)]
|
|
if 0 < len(s) < 40 and re.fullmatch(rb'\w+', s): return '"%s"' % s.decode()
|
|
return m.group(0)
|
|
classP = re.compile(r'\(\s*(?:local_\w+|&?[\w]+)\s*,\s*"([A-Z]\w+)"\s*\)|\(\s*"([A-Z]\w+)"\s*,\s*(?:[a-z]Var\d+|local_\w+)\s*\)')
|
|
funcP = re.compile(r'\(\s*"(\w+)"\s*,\s*&?(?:FUN_|LAB_)([0-9a-f]{8})')
|
|
namedP = re.compile(r'\(\s*"(\w+)"\s*,\s*([A-Za-z_][\w:]*::\w+)\s*[,)]')
|
|
offP = re.compile(r'\(\s*"(\w+)"\s*,\s*(0x[0-9a-f]+|\d+)\s*,\s*1\s*\)')
|
|
cls = None
|
|
rows, names = [], []
|
|
for line in open(src, errors='replace'):
|
|
line = re.sub(r'&DAT_([0-9a-f]{8})', cstr, line)
|
|
if line.startswith('//@@ '): cls = None; continue
|
|
m = classP.search(line)
|
|
if m: cls = m.group(1) or m.group(2); continue
|
|
if not cls: continue
|
|
m = funcP.search(line)
|
|
if m:
|
|
rows.append((cls, m.group(1), m.group(2)))
|
|
names.append('%s\t%s::%s\tlua\tLua binding %s:%s\n' % (m.group(2), cls, m.group(1), cls, m.group(1)))
|
|
continue
|
|
m = namedP.search(line)
|
|
if m: rows.append((cls, m.group(1), m.group(2))); continue
|
|
m = offP.search(line)
|
|
if m: rows.append((cls, m.group(1), 'field+0x%x' % int(m.group(2), 0)))
|
|
with open(os.path.join(outdir, 'lua_bindings.tsv'), 'w') as o:
|
|
for r in rows: o.write('\t'.join(r) + '\n')
|
|
with open(os.path.join(outdir, 'lua_names.tsv'), 'w') as o: o.writelines(names)
|
|
print(len(rows), 'bindings,', len(names), 'nameable')
|