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");
 [0x794653,0x79468d,0x794761,0x794795].forEach(function(a){H(a,"SceneManager.LoadScene");});
 H(0x7947d4,"SceneManager.LoadSceneAsync");
 H(0xd784e3,"PhotonNetwork.LoadLevel");H(0xd785c0,"PhotonNetwork.LoadLevel2");
 H(0x1003614,"NC.LoadSingleLevel");H(0x100356c,"NC.DoLoadAdditiveScenes");
 H(0xe47cab,"GC.Awake");
 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=[]
    sc.on("message",lambda m,d: (hits.append(m.get("payload",{}).get("g","")) if isinstance(m.get("payload"),dict) else None) or (print(m.get("payload",{}).get("g",m),flush=True) if isinstance(m.get("payload"),dict) else None))
    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)
