import frida, time, subprocess, sys
ADB="/opt/android-sdk/platform-tools/adb"
JS=r"""
var B=null, inAPL=false, ctor=0, setbot=0, call=0;
function main(){
 var m=Process.findModuleByName("libil2cpp.so");if(!m){setTimeout(main,150);return;}
 B=m.base;
 try{Interceptor.attach(B.add(0xe5a928),{
   onEnter:function(){inAPL=true; ctor=0; setbot=0; call++;},
   onLeave:function(r){inAPL=false; if(call<=4||call%40==0) send({g:"APL call#"+call+": GamePlayer.ctor="+ctor+" SetBot="+setbot+" RET="+r});}
 });}catch(e){send({g:"e1 "+e});}
 try{Interceptor.attach(B.add(0xe5a77e),{onEnter:function(){ if(inAPL) ctor++; }});}catch(e){send({g:"e2 "+e});}
 try{Interceptor.attach(B.add(0xd4164b),{onEnter:function(){ if(inAPL) setbot++; }});}catch(e){send({g:"e3 "+e});}
 send({g:"__ready counting"});
}
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
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 menu",flush=True); t0=time.time()
while time.time()-t0<120:
    time.sleep(4)
    if len(cap())>600000: break
print("menu",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]",flush=True); tap(700,730); time.sleep(6)
print("[GO]",flush=True); tap(1355,960)
time.sleep(55)  # let state-8 timeout + AssemblePlayerList run repeatedly
print("done",flush=True)
