"""DIMENSION 5 pass 4: who READS the unopened-pack count, and who consumes the
CreateUser response record.

Established so far (this run):
  userInfo 0x35e arm -> model+0x20950 = preOrderPacks + recoveredPacks, then raises
  event 0x273d.  Writers of +0x20950: 0x18010e06a and 0x18011e12f. Reader: 0x18011c200
  (= model vtable slot 0x4d8). 0x273d compared at 0x18007e85f and 0x1800b3944, raised
  again at 0x180199e07.

HYPOTHESIS: the 0x4d8 getter is called from UI/flow code that decides whether a
pending-packs tile exists, and one of the 0x273d handlers is the refresh path.

CONTROL: every call site is filtered by whether its containing function reaches the
model singleton FUN_18011a830 / DAT_1802e6398. Call sites on OTHER classes' vtables
that happen to use offset 0x4d8 must be rejected by that filter; if the filter rejects
nothing it is not filtering.
"""
import traceback, os

OUT = "/tmp/claude-1000/-home-alex-Documents-OpenFUT/8e521ca1-ca3e-4138-bb96-df1744dd1d30/scratchpad/store"

SITES_4D8 = [0x1800160c0, 0x180019816, 0x18007136b, 0x18007e498, 0x1800ad460,
             0x1800aeccf, 0x1800aece6, 0x1800af449, 0x1800b1d48]
SITES_4E0 = [0x1800173d1, 0x180019861, 0x18007137b, 0x1800ad498, 0x1800bcb71,
             0x18013f222]
OTHER = [0x18010e06a, 0x18007e85f, 0x1800b3944, 0x180199e07, 0x18011c200]

try:
    seen = {}
    for label, sites in (("get4d8", SITES_4D8), ("call4e0", SITES_4E0), ("other", OTHER)):
        print("\n===== %s =====" % label)
        for s in sites:
            f = func(s)
            if f is None:
                print("  %#x -> no function" % s)
                continue
            ent = int(f.getEntryPoint().getOffset())
            src = seen.get(ent)
            if src is None:
                src = dec(ent, 300)
                seen[ent] = src
            uses_model = ("FUN_18011a830" in src) or ("DAT_1802e6398" in src)
            print("  site %#x  fn %s @ %#x  len=%d  model=%s" % (
                s, f.getName(), ent, len(src), uses_model))
            open(os.path.join(OUT, "dec_fn_%x.c" % ent), "w").write(src)

    # The CreateUser response object: find its class literal, vtable and consumers.
    print("\n===== FutCreateUserServerResponse =====")
    for lit in find_all(b"RS4:FutCreateUserServerResponse\x00"):
        print("  literal @ %#x" % lit)
        for frm, typ, fn, ent in xrefs_to(lit):
            print("    xref from %#x (%s) in %s @ %#x" % (frm, typ, fn, ent))
            if ent:
                src = dec(ent, 300)
                open(os.path.join(OUT, "dec_createuser_factory_%x.c" % ent), "w").write(src)
                print("      -> dec_createuser_factory_%x.c len=%d" % (ent, len(src)))
                print(src[:1800])
except Exception:
    traceback.print_exc()
