Files
isaac-port/scripts/zhl_match.py
T
shweetandClaude Opus 5.5 ad8f933086 Salvage Repentance+ 1.9.7.17 decompilation toolkit, notes and name database
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>
2026-10-24 13:08:19 -04:00

103 lines
4.6 KiB
Python

# Matches libzhl signatures against the exe. Usage: zhl_match.py exe zhl_dir out.tsv
# exact unique match -> "exact"; else longest unique prefix (>=20 bytes) -> "prefixN"
import re, sys, struct, glob, os
exe, zdir, out = sys.argv[1:4]
data = open(exe, 'rb').read()
pe = struct.unpack_from('<I', data, 0x3c)[0]
nsec = struct.unpack_from('<H', data, pe + 6)[0]
optsz = struct.unpack_from('<H', data, pe + 20)[0]
base = struct.unpack_from('<I', data, pe + 24 + 28)[0]
secs = []
for i in range(nsec):
o = pe + 24 + optsz + 40 * i
va, rsz, raw = struct.unpack_from('<III', data, o + 12)
secs.append((raw, raw + rsz, va))
def to_va(off):
for raw, end, va in secs:
if raw <= off < end: return base + va + off - raw
def rx(sig, n):
return re.compile(b''.join(b'.' if sig[i:i+2] == '??' else re.escape(bytes([int(sig[i:i+2], 16)])) for i in range(0, n, 2)), re.S)
def hits(sig, n, limit=2):
r = []
for m in rx(sig, n).finditer(data):
r.append(m.start())
if len(r) >= limit: break
return r
sigP = re.compile(r'^\s*"([0-9a-fA-F?]+)"\s*:\s*$')
structP = re.compile(r'^\s*struct\s+(\w+)')
nameP = re.compile(r'([A-Za-z_][\w:~]*)\s*\(')
stats = {}
rows = []
if True:
for f in sorted(glob.glob(os.path.join(zdir, '*.zhl'))):
lines = open(f, errors='replace').read().split('\n')
struct_ = None
for i, l in enumerate(lines):
m = structP.match(l)
if m: struct_ = m.group(1)
m = sigP.match(l)
if not m: continue
decl = ''
j = i + 1
while j < len(lines) and ';' not in decl: decl += lines[j].strip() + ' '; j += 1
nm = nameP.search(re.sub(r'<[^>]*>', '', decl))
if not nm: continue
name = nm.group(1)
if '::' not in name and struct_: name = struct_ + '::' + name
sig = m.group(1)
h = hits(sig, len(sig))
q = 'exact' if len(h) == 1 else ('multi' if h else None)
if not h:
lo, hi = 40, len(sig) # binary search longest prefix with >=1 hit
if len(sig) > lo and hits(sig, lo):
while hi - lo > 2:
mid = (lo + hi) // 4 * 2
if hits(sig, mid): lo = mid
else: hi = mid
h = hits(sig, lo)
q = 'prefix%d/%d' % (lo // 2, len(sig) // 2) if len(h) == 1 else 'multi'
stats[(q or 'miss').split('/')[0][:6]] = stats.get((q or 'miss').split('/')[0][:6], 0) + 1
if q and q != 'multi':
rows.append((to_va(h[0]), name, q, decl.strip()))
from collections import Counter
dup = Counter(r[0] for r in rows)
with open(out, 'w') as o:
for r in rows:
if dup[r[0]] > 1 and r[2] != 'exact': continue # prefix match collided with another name
o.write('%08x\t%s\t%s\t%s\n' % r)
print(stats)
# vtables: class \t slot \t name (slot names come from decls or "skip; // Name" comments)
with open(os.path.join(os.path.dirname(out), 'zhl_vtables.tsv'), 'w') as o:
for f in sorted(glob.glob(os.path.join(zdir, '*.zhl'))):
txt = open(f, errors='replace').read()
for m in re.finditer(r'struct\s+(\w+)[^{]*\{(.*?)__vtable\s*\{(.*?)\n\s*\};', txt, re.S):
cls, body = m.group(1), m.group(3)
body = re.sub(r'"[^"]*"\s*:', '', body)
entries = re.findall(r'\s*(skip\s*;[^\n]*|[^;]*?;)', body, re.S)
slot = 0
for e in entries:
e = e.strip()
if not e: continue
if e.startswith('skip'):
nm = re.search(r'//\s*(\w+)', e)
name = nm.group(1) if nm else None
else:
nm = nameP.search(re.sub(r'<[^>]*>', '', e))
name = nm.group(1) if nm else None
if name and name != 'skip': o.write('%s\t%d\t%s\n' % (cls, slot, name))
slot += 1
# struct fields from zhl: struct \t offset \t type name
with open(os.path.join(os.path.dirname(out), 'zhl_fields.tsv'), 'w') as o:
for f in sorted(glob.glob(os.path.join(zdir, '*.zhl'))):
struct_ = None
for l in open(f, errors='replace'):
m = structP.match(l)
if m:
struct_ = m.group(1)
b = re.search(r':\s*public\s+(\w+)', l)
if b: o.write('%s\tbase\t%s\n' % (struct_, b.group(1)))
m = re.match(r'^\s*([\w:<>,\s\*&]+?)\s*:\s*(0x[0-9a-fA-F]+)\s*;', l)
if m and struct_: o.write('%s\t%s\t%s\n' % (struct_, m.group(2).lower(), ' '.join(m.group(1).split())))