# 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. # Usage: headers.py out.hpp zhl_fields.tsv lua_fields.tsv db/fields.tsv import collections, re, sys out, *srcs = sys.argv[1:] 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 = 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 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 = {}, [] def size_of(t): if t.endswith('*'): return 4 return SIZE.get(t) or done.get(t) 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): 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: # unknown size: a byte span up to the next member n = (nxt - off) if nxt else 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 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 \n#include \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') print(f'headers: {len(done)} structs' + (f'; duplicate member names in {dupes}' if dupes else ''))