Files
isaac-port/scripts/apply_db.py
T
shweetandClaude Opus 5.5 a928f6204f Full redump with 65 typed globals, 53 fields, 346 function names; apply_db.py writes atomically
g_game/g_manager typed: 1483 g_manager-> and 834 g_game-> member accesses now visible (field_0x 10636 -> 14385 counts them);
named members 81k -> 91k, raw in_ECX + 0x 18425 -> 16225. ApplyGlobals takes array types (char[5]).
apply_db.py used to truncate all.c before computing the new text, so a killed build left it empty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 17:25:09 -04:00

52 lines
2.9 KiB
Python

# Applies db/names.tsv to the dump in place (no Ghidra): headers, call sites, functions.tsv, strings.tsv, npc_ai.tsv.
# Only names change; types/fields need build.sh --db. Safe to re-run. Usage: apply_db.py names.tsv dumpdir
import os, re, sys
from collections import Counter
db, d = sys.argv[1:3]
fns = dict(l.rstrip('\n').split('\t')[:2] for l in open(f'{d}/functions.tsv'))
count = Counter(fns.values())
ren = {}
for l in open(db):
if l.strip() and not l.startswith('#'):
a, new = l.rstrip('\n').split('\t')[:2]
a = '%08x' % int(a, 16) # accept 0x59e730 / 0059e730 / 0x0059e730
if a in fns: ren[a] = new
# 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}
# (generic token patterns + dict lookups: an alternation of ~1,500 names is far too slow on a 35 MB dump)
calls = re.compile(r'(?<![\w:])([A-Za-z_~][\w~]*(?:::[A-Za-z_~][\w~]*)+|FUN_[0-9a-f]{8})(?![\w:])')
users = re.compile(r'(?:(?<=^//@@ )|\b)([0-9a-f]{8})([: ])([^\s]+)', re.M)
def fix(s):
s = calls.sub(lambda m: old2new.get(m.group(1), m.group(1)), s) if old2new else s
return users.sub(lambda m: m.group(1) + m.group(2) + ren[m.group(1)] if m.group(1) in ren else m.group(0), s)
# db/locals.tsv rows without a type: rename the variable inside its function's block (typed rows need build.sh --db)
locals = {}
for l in open(db.replace('names.tsv', 'locals.tsv')):
c = l.rstrip('\n').split('\t')
if l.strip() and not l.startswith('#') and (len(c) < 4 or not c[3].strip()):
locals.setdefault('%08x' % int(c[0], 16), []).append((re.compile(r'\b%s\b' % re.escape(c[1])), c[2]))
def fix_locals(s):
for a, subs in locals.items():
i = s.find(f'//@@ {a} ')
if i < 0: continue
j = s.find('\n//@@ ', i + 5) + 1 or len(s)
body = s[i:j]
for r, new in subs: body = r.sub(new, body)
s = s[:i] + body + s[j:]
return s
# db/globals.tsv: DAT_<address> -> 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(); s = fix_locals(fix(s)) if f == 'all.c' else fix(s)
open(p + '.tmp', 'w').write(s); os.replace(p + '.tmp', p) # a killed run never leaves a truncated dump
rows = [l.split('\t', 2) for l in open(f'{d}/functions.tsv')]
open(f'{d}/functions.tsv', 'w').writelines(f'{a}\t{ren.get(a, n)}\t{r}' for a, n, r in rows)
print(f'apply_db: {sum(fns[a] != n for a, n in ren.items())} renamed, {len(ren)} in db')