"""DIMENSION 4 q2: locate the draft/tournament entry decision.

Hypothesis: entry is gated in the script layer / a manager singleton with no server
writer, NOT by any server-reachable field. Test by (a) enumerating draft/tournament
script-event + manager literals and their xrefs, (b) reading the CompetitionManager
setters FUN_180101680/FUN_1801016c0 (the Seasons lead) and looking for draft/tourney
analogues, (c) finding who READS the draft gate byte model+0x1fd3d and tournament
+0x1fd3b.

Control: 'IS_DRAFT_MODE_ENABLED' literal must resolve and xref into FUN_18006cc60
(the known publisher). If it does, the string/xref mechanics work.
"""
import traceback
try:
    def show_str_xrefs(lit, blocks=(".rdata",)):
        hits = find_all(lit.encode() + b"\x00", blocks)
        print("\n--- literal %r : %d hit(s) ---" % (lit, len(hits)))
        for h in hits:
            print("  @ %#x" % h)
            for frm, typ, fn, ent in xrefs_to(h):
                print("     xref from %#x  %s  in %s (%#x)" % (frm, typ, fn, ent))

    # control
    show_str_xrefs("IS_DRAFT_MODE_ENABLED")
    # draft / tournament script + manager literals
    for lit in ("NOSEASONS", "NODRAFT", "NOTOURNAMENT", "DRAFTSQUAD_ON",
                "SINGLE_PLAYER", "DRAFT_TOKEN", "DraftMode", "Draft",
                "CompetitionManager", "TournamentInfo", "TournamentManager",
                "DraftManager", "OnlineDraft", "OfflineDraft"):
        show_str_xrefs(lit)

    # substring scan for any *draft*/*tournament* ascii literal in .rdata
    print("\n=== .rdata literals containing 'raft' or 'ourna' ===")
    for needle in (b"raft", b"ourna"):
        seen = set()
        for h in find_all(needle, (".rdata",)):
            # back up to string start
            p = h
            while p > h - 64:
                b = read_bytes(p - 1, 1)
                if not b or b[0] == 0 or b[0] < 0x20 or b[0] > 0x7e:
                    break
                p -= 1
            s = rd_str(p, 96)
            if s and s not in seen and (b"raft" in s.encode() or b"ourna" in s.encode()):
                seen.add(s)
                print("  %#x  %r" % (p, s))
except Exception:
    traceback.print_exc()
print("QUERY_DONE")
