"""D4 final: (1) who owns FUN_1800aa440 (the pack-open summary handler) and what the
0x33 / 0x34 state constants it pushes are; (2) the other seven indirect call sites
through vtable slot +0x2e0, to tell real packOpeningAnimationEnabled reads from
same-offset reads on unrelated objects; (3) FUN_1800d8330, the cardsubtypeid ->
cardtype map that decides item+0x4c (the ==1 player filter in the summary walk);
(4) the init constant at 0x1801f66a0 that seeds item+0x50/+0x54.

CONTROL for (2): FUN_1800aa440 is already PROVEN to call the FutDataManagerImpl
accessor at +0x2e0, because its object plVar6 comes from FUN_180009c80 (the same
getter FUN_18006cc60 uses for the IS_* publisher). Any classification rule I apply to
the other seven must classify FUN_1800aa440 as a true positive.
"""
import traceback

OUT = "/tmp/claude-1000/-home-alex-Documents-OpenFUT/8e521ca1-ca3e-4138-bb96-df1744dd1d30/scratchpad/packres/"
BUF = []


def p(*a):
    s = " ".join(str(x) for x in a)
    print(s)
    BUF.append(s)


try:
    p("=== xrefs to FUN_1800aa440 ===")
    for x in xrefs_to(0x1800AA440):
        p("   %s" % (x,))
    p("=== xrefs to FUN_18001eb90 (gmLoadFUTPackOpenSublevel owner) ===")
    for x in xrefs_to(0x18001EB90):
        p("   %s" % (x,))

    p("\n=== init constant at 0x1801f66a0 ===")
    p("   qword %#x  bytes %s" % (qword(0x1801F66A0), read_bytes(0x1801F66A0, 16).hex()))

    p("\n=== FUN_1800d8330 cardsubtypeid -> cardtype ===")
    s = dec(0x1800D8330, 200)
    p(s)
    with open(OUT + "d4_cardsubtype_to_cardtype.txt", "w") as f:
        f.write(s)

    for va, nm in ((0x180051CD0, "site_180051cd0"),
                   (0x18006AC20, "site_18006ac20"),
                   (0x180088CB0, "site_180088cb0"),
                   (0x18008B6E0, "site_18008b6e0"),
                   (0x1800D0600, "site_1800d0600"),
                   (0x18011A5C0, "site_18011a5c0"),
                   (0x18011CFA0, "site_18011cfa0")):
        src = dec(va, 300)
        with open(OUT + "d4_%s.txt" % nm, "w") as f:
            f.write(src)
        # only report the lines around a +0x2e0 dispatch and whether FUN_180009c80 appears
        p("\n--- %s len=%d  uses_FUN_180009c80=%s ---"
          % (nm, len(src), "FUN_180009c80" in src))
        lines = src.split("\n")
        for i, ln in enumerate(lines):
            if "0x2e0" in ln:
                p("\n".join(lines[max(0, i - 6):i + 4]))
                p("   ...")

except Exception:
    traceback.print_exc()
finally:
    with open(OUT + "d4_final.txt", "w") as f:
        f.write("\n".join(BUF))
    print("WROTE d4_final.txt")
