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;
 // hook the coroutine's per-player ready check at d3e014 ('test [eax+0xb2],cl'); eax=player
 try{Interceptor.attach(b.add(0xd3e014),{onEnter:function(){var p=this.context.eax;try{
   var b2=p.add(0xb2).readU8(); var f60=p.add(0x60).readS32(); var d0=p.add(0xd0).readU8(); var e8=p.add(0xe8).readS32(); var e4=p.add(0xe4).readS32();
   send({g:"player b2="+b2+" f60="+f60+" role(d0)="+d0+" isAI(e8)="+e8+" pawn(e4)="+f60});
 }catch(e){send({g:"rd err "+e});}}});}catch(e){send({g:"h err "+e});}
 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: (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(28)
    s.detach()
    return any("player b2" in h for h in hits)
for attempt in range(3):
    print(f"--- attempt {attempt+1} ---",flush=True)
    if run(): break
print("done",flush=True)
