# REPENTOGON's struct offsets target an older exe (1.9.7.12); fields drift as the game adds members, so they are only # kept where THIS exe confirms them. Evidence ("anchors"): a trivial getter in the corpus ("Class::GetFoo" returning # this+N anchors field "_foo" at N) or the game's Lua field table (lua_bindings.tsv: "EntityNPC State field+0xb64"). # A field is kept only when it is anchored; everything else is dropped (the decompiler then shows field_0xNNN) until # db/fields.tsv confirms it. Base-class rows are always kept. # Usage: zhl_fields.py all.c zhl_dir lua_bindings.tsv out.tsv [names.tsv: addr \t Class::Member, overrides corpus names] # Prints per class: kept/total and the shifts seen. import collections, glob, os, re, sys src, zdir, lua, fout = sys.argv[1:5] rename = dict(l.split('\t')[:2] for l in open(sys.argv[5])) if len(sys.argv) > 5 else {} norm = lambda s: re.sub(r'^(get|is|has)', '', s.lower().lstrip('_')) cls_of = lambda c: {'Item': 'ItemConfig_Item', 'Card': 'ItemConfig_Card', 'PillEffect': 'ItemConfig_Pill', 'Costume': 'ItemConfig_Costume', 'GridEntityDoor': 'GridEntity_Door'}.get(c, re.sub(r'^Entity(?=[A-Z])', 'Entity_', c)) # raw fields: "int _a : 0x0, _b : 0x4;" is two fields raw, bases = collections.defaultdict(list), [] for f in sorted(glob.glob(os.path.join(zdir, '*.zhl'))): struct_ = None for l in open(f, errors='replace'): m = re.match(r'^\s*struct\s+(\w+)', l) if m: struct_ = m.group(1) b = re.search(r':\s*public\s+(\w+)', l) if b: bases.append((struct_, b.group(1))) if not (struct_ and re.match(r'^\s*([\w:<>,\s\*&]+?)\s*:\s*(0x[0-9a-fA-F]+)\s*;', l)): continue parts = re.findall(r'([^,:]+?)\s*:\s*(0x[0-9a-fA-F]+)', l.split(';')[0]) ftype = parts[0][0].split()[:-1] for decl, off in parts: words = decl.split() raw[struct_].append((int(off, 16), ' '.join(words if len(words) > 1 else ftype + words))) anchor = {} # (class, normalised member) -> offset in this exe name, n = None, 0 for line in open(src, errors='replace'): if line.startswith('//@@ '): name, n = rename.get(line.split()[1], line.split()[2]), 0; continue n += 1 m = n < 14 and '::' in name and re.search(r'return (?:\*\([\w ]+\*\)\((?:in_ECX|this|param_1) \+ (0x[0-9a-f]+)\)|(?:in_ECX|this|param_1) \+ (0x[0-9a-f]+));', line) if m: cls, meth = name.rsplit('::', 1) anchor.setdefault((cls_of(cls), norm(meth)), int(m.group(1) or m.group(2), 16)) for l in open(lua): c = l.rstrip('\n').split('\t') m = len(c) > 2 and re.match(r'field\+(0x[0-9a-f]+)$', c[2]) if m: anchor[(cls_of(c[0]), norm(c[1]))] = int(m.group(1), 16) # the game's own table wins over a getter guess with open(fout, 'w') as o: for c, b in bases: o.write(f'{c}\tbase\t{b}\n') for c, fields in sorted(raw.items()): shifts = sorted((off, anchor[(c, norm(d.split()[-1]))] - off) for off, d in fields if (c, norm(d.split()[-1])) in anchor) kept = 0 for off, d in fields: if (c, norm(d.split()[-1])) in anchor: new = anchor[(c, norm(d.split()[-1]))] else: continue # not confirmed: being between two anchors with the same shift is not proof (Entity _mass, _depthOffset) o.write(f'{c}\t0x{new:x}\t{d}\n'); kept += 1 if shifts: print(f'{c}: kept {kept}/{len(fields)}, shifts ' + ' '.join(f'{a:x}{s:+#x}' for a, s in shifts if s))