import frida, time, subprocess, sys
ADB="/opt/android-sdk/platform-tools/adb"
JS=r"""
var B=null, seq=0, aplCount=0;
function main(){
 var m=Process.findModuleByName("libil2cpp.so");if(!m){setTimeout(main,150);return;}
 B=m.base;
 function H(a,nm){try{Interceptor.attach(B.add(a),{onEnter:function(){seq++;send({g:"["+seq+"] "+nm});}});}catch(e){}}
 // THE FIX: force AssemblePlayerList to return true
 try{Interceptor.attach(B.add(0xe5a928),{
   onEnter:function(){aplCount++; if(aplCount<=3||aplCount%30==0)send({g:"AssemblePlayerList ENTER #"+aplCount});},
   onLeave:function(r){ if(r.toInt32()==0){ r.replace(ptr(1)); if(aplCount<=3||aplCount%30==0)send({g:"  -> FORCED true (was 0)"});} }
 });}catch(e){send({g:"apl hook err "+e});}
 H(0xd315ce,"Matchmaking.Finish");
 H(0xe47cab,"GameController.Awake");H(0xe484bd,"GameController.Start");H(0xe4bd6a,"SpawnPlayers");
 H(0xdf9152,"Thief.Awake");H(0xdf9369,"Thief.Start");
 try{Interceptor.attach(B.add(0x7947d4),{onEnter:function(){var sp=this.context.esp;seq++;var idx=sp.add(4).readS32();send({g:"["+seq+"] LoadSceneAsync idx="+idx});}});}catch(e){}
 send({g:"__ready (AssemblePlayerList forced-true)"});
}
main();
"""
def sh(*a): return subprocess.run([ADB]+list(a),capture_output=True,text=True)
def tap(x,y): subprocess.run([ADB,"shell","input","tap",str(x),str(y)],capture_output=True)
def cap(): return subprocess.run([ADB,"exec-out","screencap","-p"],capture_output=True).stdout
def shot(name):
    d=cap(); open("/root/ants/shots/"+name,"wb").write(d); print("  shot",name,len(d),flush=True)

sh("shell","am","force-stop","se.foglo.svt"); time.sleep(1)
sh("shell","input","keyevent","3"); time.sleep(1)
sh("shell","monkey","-p","se.foglo.svt","-c","android.intent.category.LAUNCHER","1")
print("polling for menu",flush=True)
t0=time.time(); menu=False
while time.time()-t0<120:
    time.sleep(4)
    if len(cap())>600000: menu=True; break
print("MENU" if menu else "TIMEOUT",int(time.time()-t0),flush=True); time.sleep(3)
dev=frida.get_usb_device(timeout=10)
pid=int(sh("shell","pidof","se.foglo.svt").stdout.split()[0]); print("pid",pid,flush=True)
s=dev.attach(pid); sc=s.create_script(JS)
sc.on("message",lambda m,d: print(m.get("payload",{}).get("g",""),flush=True) if isinstance(m.get("payload"),dict) else None)
sc.load(); time.sleep(2)
print("[SNIPER] (ONLINE path, no offline force)",flush=True); tap(700,730); time.sleep(6)
print("[GO]",flush=True); tap(1355,960)
# wait through waitPlayersTimeout -> forcing start -> AssemblePlayerList forced true -> Finish -> gameplay
for k in range(12):
    time.sleep(8); shot("fix_%02d.png"%k)
print("done",flush=True)
