import frida, time, subprocess, sys
sys.path.insert(0,"/root/ants"); import emu
JS=r"""
function sstr(p){try{if(p.isNull())return "null";var n=p.add(8).readS32();if(n<0||n>200)return "?";return p.add(0xc).readUtf16String(n);}catch(e){return "ERR";}}
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){}}
 try{Interceptor.attach(b.add(0xd86224),{onLeave:function(r){send({g:"CreateMatch.MoveNext ret="+r.toInt32()});}});}catch(e){}
 try{Interceptor.attach(b.add(0x7947d4),{onEnter:function(){var sp=this.context.esp;send({g:"LoadSceneAsync '"+sstr(sp.add(4).readPointer())+"'"});}});}catch(e){}
 H(0xd85f5b,"PIERole.EnableControls");H(0xe47cab,"GC.Awake");H(0xe4bd6a,"SpawnPlayers");H(0xd3df1a,"DoleCoro");
 send({g:"__ready"});return true;}
if(!ins()){var iv=setInterval(function(){if(ins())clearInterval(iv);},100);}
"""
def run():
    t=emu.launch_to_menu(); print("menu",int(t) if t else "FAIL",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=[]
    sc.on("message",lambda m,d: (isinstance(m.get("payload"),dict) and (hits.append(m["payload"].get("g","")), print(m["payload"].get("g",""),flush=True))))
    sc.load(); time.sleep(0.5)
    print("[GO]",flush=True); emu.tap(1355,960); time.sleep(35)
    s.detach()
    return any("DoleCoro" in h or "CreateMatch" in h for h in hits)
for a in range(3):
    print("--- attempt",a+1,flush=True)
    if run(): break
print("done",flush=True)
