import frida, time, subprocess, sys
sys.path.insert(0,"/root/ants"); import emu
JS=r"""
function ins(){var m=Process.findModuleByName("libil2cpp.so");if(!m)return false;var b=m.base;
 function H(a,nm){try{Interceptor.attach(b.add(a),{onEnter:function(){send({g:nm});}});}catch(e){}}
 H(0xd3df1a,"DoleCoro");H(0xe47cab,"GC.Awake");H(0xe484bd,"GC.Start");
 try{Interceptor.attach(b.add(0xe4e030),{onEnter:function(){var sp=this.context.esp;try{send({g:"GC.Update st="+sp.add(4).readPointer().add(0xe8).readS32()});}catch(e){}}});}catch(e){}
 try{Interceptor.attach(b.add(0xe4746f),{onEnter:function(){var sp=this.context.esp;send({g:"GC.set_state("+sp.add(8).readS32()+")"});}});}catch(e){}
 try{Interceptor.attach(b.add(0xd41677),{onEnter:function(){var sp=this.context.esp;send({g:"set_isSniper("+(sp.add(8).readU32()?1:0)+")"});}});}catch(e){}
 H(0x1003614,"LoadSingleLevel");H(0xe4bd6a,"SpawnPlayers");
 send({g:"__ready"});return true;}
if(!ins()){var iv=setInterval(function(){if(ins())clearInterval(iv);},100);}
"""
def run():
    t=emu.launch_to_menu(); print(f"menu {t:.0f}s",flush=True)
    emu.tap(700,730); time.sleep(5)
    dev=frida.get_usb_device(timeout=10)
    pid=int(subprocess.run([emu.ADB,"shell","pidof","se.foglo.svt"],capture_output=True,text=True).stdout.split()[0])
    s=dev.attach(pid); sc=s.create_script(JS)
    hits=set()
    def onmsg(m,d):
        p=m.get("payload",{})
        if isinstance(p,dict) and "g" in p:
            g=p["g"]
            if g.startswith("GC.Update"):
                if g in hits: return
            hits.add(g); print(g,flush=True)
    sc.on("message",onmsg); sc.load(); time.sleep(0.5)
    print("[GO]",flush=True); emu.tap(1355,960); time.sleep(30)
    s.detach()
    return any("DoleCoro" in h for h in hits)
for attempt in range(3):
    print(f"--- attempt {attempt+1} ---",flush=True)
    if run(): print("[match started]",flush=True); break
print("done",flush=True)
