"""Q13: every FUT_MYCLUB_ loc key, and the table row that carries it.

Q12 reproduced the consumables table (control passed) and showed the staff table's
middle column IS the cardtype (manager 2, headcoach 3, fitnesscoach 4, gkcoach 0xa,
physio 5 -- exactly the merge's switch arms), and a trophies pair:
    0x180203380  {0x05, 0, FUT_MYCLUB_OFFLINE_TROPHIES_EARNED}
    0x180203398  {0x15, 1, FUT_MYCLUB_ONLINE_TROPHIES_EARNED}

If a badges/kits/stadia/balls row exists in the same shape, its middle column is the
answer. Enumerate EVERY FUT_MYCLUB_ literal, find the pointer to it in .rdata/.data,
and print the 0x18-byte row it sits in for all three possible cell positions, plus
the rows either side.

CONTROL: FUT_MYCLUB_CONSUMABLES_TRAINING_EARNED must resolve to the row
{0, ptr, 'training'} at 0x180203260. Any layout guess that cannot reproduce that row
is wrong.
"""
import traceback

try:
    keys = []
    for h in find_all(b"FUT_MYCLUB_"):
        s = rd_str(h, 120)
        keys.append((h, s))
    keys.sort()
    print("=== %d FUT_MYCLUB_ literals ===" % len(keys))
    for h, s in keys:
        print("  %#x %r" % (h, s))
    print()
    print("=== pointer rows ===")
    for h, s in keys:
        ptrs = find_all(h.to_bytes(8, "little"), blocks=(".rdata", ".data"))
        if not ptrs:
            print("  %-46r no pointer" % s)
            continue
        for pa in ptrs:
            ctx = []
            for off in (-0x18, -0x10, -8, 0, 8, 0x10, 0x18):
                try:
                    q = qword(pa + off)
                except Exception:
                    continue
                t = ""
                if 0x180000000 <= q < 0x181000000:
                    try:
                        u = rd_str(q, 80)
                        if u and all(0x20 <= ord(c) < 0x7F for c in u):
                            t = u
                    except Exception:
                        pass
                ctx.append("%+#5x:%#x%s" % (off, q, (" %r" % t) if t else ""))
            print("  %-46r @%#x" % (s, pa))
            print("        " + "  ".join(ctx))
except Exception:
    traceback.print_exc()
