classes.tsv (vtable.py --classes): 194 RTTI classes with vftable, direct bases (offsets from the PMD), vtable slots and, for 176, the exact size from the deleting destructor's sized delete. Sizes flow into structs.hpp (static_assert sizeof) and, next build, into Ghidra's structs (ApplyTypes 'size' rows) so this[1]._x stops standing in for the tail. scripts/source_tree.py generates src/game/classes.hpp: 293 classes with real bases, vtables in slot order (overrides, REPENTOGON slot names, vfunc_N placeholders), fields at their offsets, 2400 method declarations; it compiles with clang --target=i686-pc-windows-msvc and all 174 sizeof / 1899 offsetof asserts hold. Round 26's prototypes are held. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
6.0 KiB
Python
83 lines
6.0 KiB
Python
# Generates src/structs.hpp: one C++ struct per class from the confirmed fields (zhl_fields.tsv, lua_fields.tsv,
|
|
# db/fields.tsv; later files win at the same offset), base-class fields flattened in, gaps as explicit padding, and a
|
|
# static_assert on every member offset. Only offsets this exe confirms are in these files, so the header is as trustworthy
|
|
# as the evidence behind them. Pointers are uint32_t (the game is 32-bit), so it compiles on any host.
|
|
# A "Class<TAB>size<TAB>0x2f00" row (classes.tsv, from the RTTI deleting destructor) pads the struct to its real size.
|
|
# A db row with type "-" ("Entity<TAB>0x2d4<TAB>- _mass<TAB>why") deletes a wrong field from the earlier files.
|
|
# Usage: headers.py [--merged fields_out.tsv] out.hpp zhl_fields.tsv lua_fields.tsv db/fields.tsv
|
|
# --merged also writes the merged field list (what ApplyTypes.java gets)
|
|
import collections, re, sys
|
|
args = sys.argv[1:]
|
|
merged = args.pop(args.index('--merged') + 1) if '--merged' in args else None
|
|
if merged: args.remove('--merged')
|
|
out, *srcs = args
|
|
SIZE = {'bool': 1, 'char': 1, 'int8_t': 1, 'uint8_t': 1, 'unsigned char': 1, 'short': 2, 'int16_t': 2, 'uint16_t': 2,
|
|
'unsigned short': 2, 'int': 4, 'int32_t': 4, 'uint32_t': 4, 'unsigned int': 4, 'unsigned long': 4, 'long': 4,
|
|
'float': 4, 'uint64_t': 8, 'int64_t': 8, 'unsigned long long': 8, 'long long': 8, 'double': 8, 'Vector': 8}
|
|
CTYPE = {'unsigned int': 'uint32_t', 'unsigned long': 'uint32_t', 'long': 'int32_t', 'unsigned short': 'uint16_t',
|
|
'unsigned char': 'uint8_t', 'unsigned long long': 'uint64_t', 'long long': 'int64_t', 'char': 'int8_t'}
|
|
fields, bases, sizes, rows = collections.defaultdict(dict), {}, {}, {}
|
|
for p in srcs:
|
|
for l in open(p):
|
|
if l.startswith('#') or not l.strip(): continue
|
|
c = l.rstrip('\n').split('\t')
|
|
if c[1] == 'base': bases[c[0]] = c[2]; continue
|
|
if c[1] == 'size': sizes[c[0]] = int(c[2], 0); continue
|
|
if c[2].startswith('- '):
|
|
fields[c[0]].pop(int(c[1], 16), None); rows.pop((c[0], int(c[1], 16)), None); continue
|
|
rows[(c[0], int(c[1], 16))] = l if l.endswith('\n') else l + '\n'
|
|
decl = c[2].replace('const ', '').strip()
|
|
count = re.search(r'\[(\w+)\]\s*$', decl) # "int _trinketsID[2]"
|
|
if count: decl = decl[:count.start()]
|
|
typ, name = decl.rsplit(None, 1) if ' ' in decl else (decl, '_unnamed')
|
|
fields[c[0]][int(c[1], 16)] = (typ.strip(), re.sub(r'\W', '', name), c[3] if len(c) > 3 else '', int(count.group(1), 0) if count else 1)
|
|
def flat(cls, depth=0):
|
|
f = dict(flat(bases[cls], depth + 1)) if cls in bases and depth < 8 else {}
|
|
f.update(fields.get(cls, {}))
|
|
return f
|
|
done, order = {}, []
|
|
STL = [(r'std_string', 0x18), (r'(std_)?vector_bool', 0x10), (r'(std_)?vector_', 0xc), (r'(std_)?deque_', 0x14), (r'std_(map|set|multiset|multimap|list)_', 8)]
|
|
def size_of(t):
|
|
if t.endswith('*'): return 4
|
|
return SIZE.get(t) or done.get(t) or next((n for p, n in STL if re.match(p, t)), None)
|
|
def emit(cls, stack=()):
|
|
if cls in done or cls in stack: return
|
|
f = flat(cls)
|
|
for t, *_ in f.values():
|
|
if t in fields and not t.endswith('*'): emit(t, stack + (cls,))
|
|
lines, asserts, pos = [], [], 0
|
|
for off in sorted(f):
|
|
t, name, note, count = f[off]
|
|
if off < pos: continue # overlaps the previous member (e.g. a union view): keep the first
|
|
if off > pos: lines.append(f' uint8_t _pad_0x{pos:x}[0x{off - pos:x}];')
|
|
sz = size_of(t) and size_of(t) * count
|
|
dim = f'[{count}]' if count > 1 else ''
|
|
nxt = min([o for o in f if o > off], default=None)
|
|
if sz and (nxt is None or off + sz <= nxt) and (t in done or t in SIZE or t.endswith('*')): # STL types stay bytes
|
|
ct = 'uint32_t' if t.endswith('*') else CTYPE.get(t, t)
|
|
cmt = f' // {t}' if t.endswith('*') else ''
|
|
lines.append(f' {ct} {name}{dim};{cmt}' + (f' // {note}' if note else ''))
|
|
asserts.append(f'static_assert(offsetof({cls}, {name}) == 0x{off:x});')
|
|
pos = off + sz
|
|
else: # an STL container (its MSVC size) or unknown size: a byte span up to the next member
|
|
n = min(sz or 1 << 30, nxt - off if nxt else sz or 4)
|
|
lines.append(f' uint8_t {name}[0x{n:x}]; // {t}' + (f'; {note}' if note else ''))
|
|
asserts.append(f'static_assert(offsetof({cls}, {name}) == 0x{off:x});')
|
|
pos = off + n
|
|
if not lines: return
|
|
if pos < sizes.get(cls, 0): lines.append(f' uint8_t _pad_0x{pos:x}[0x{sizes[cls] - pos:x}];'); pos = sizes[cls]
|
|
if cls in sizes: asserts.append(f'static_assert(sizeof({cls}) == 0x{sizes[cls]:x});' if pos == sizes[cls] else f'// {cls}: fields end at 0x{pos:x}, past its real size 0x{sizes[cls]:x}')
|
|
if pos % 4: lines.append(f' uint8_t _pad_0x{pos:x}[0x{4 - pos % 4:x}];'); pos += 4 - pos % 4 # MSVC alignment padding: arrays get the real stride
|
|
done[cls] = pos
|
|
order.append(f'struct {cls} {{' + (f' // : {bases[cls]} (flattened)' if cls in bases else '') + '\n' + '\n'.join(lines) + '\n};\n' + '\n'.join(asserts) + '\n')
|
|
for c in sorted(set(fields) | set(bases)): emit(c)
|
|
dupes = [c for c in done if len({v[1] for v in flat(c).values()}) != len(flat(c))] # same name at two offsets
|
|
with open(out, 'w') as o:
|
|
o.write('// Generated by scripts/headers.py from the confirmed struct fields of isaac-ng.exe v1.9.7.17. Do not edit:\n'
|
|
'// add or fix fields in db/fields.tsv and regenerate. Offsets not confirmed in this exe are padding.\n'
|
|
'#pragma once\n#include <cstddef>\n#include <cstdint>\n\n#pragma pack(push, 1)\n'
|
|
'struct Vector { float x, y; };\n\n' + '\n'.join(o_ for o_ in order if not o_.startswith('struct Vector ')) + '#pragma pack(pop)\n')
|
|
if merged:
|
|
with open(merged, 'w') as o: o.writelines([f'{c}\tbase\t{b}\n' for c, b in bases.items()] + [f'{c}\tsize\t{n:#x}\n' for c, n in sizes.items()] + list(rows.values()))
|
|
print(f'headers: {len(done)} structs' + (f'; duplicate member names in {dupes}' if dupes else ''))
|