import frida, time, subprocess, sys
ADB="/opt/android-sdk/platform-tools/adb"
JS=r"""
var B=null, forceOff=false;
function rdstr(p){try{if(p.isNull()||p.compare(ptr(0x10000))<0)return null;var n=p.add(8).readS32();if(n<0||n>20000)return null;return p.add(0xc).readUtf16String(n);}catch(e){return null;}}
function main(){
 var m=Process.findModuleByName("libil2cpp.so");if(!m){setTimeout(main,150);return;}
 B=m.base;
 try{Interceptor.attach(B.add(0xd6bb99),{onLeave:function(r){ if(forceOff) r.replace(ptr(1)); }});}catch(e){}
 function H(a,nm){try{Interceptor.attach(B.add(a),{onEnter:function(){send({g:"HIT "+nm});}});}catch(e){}}
 H(0xe47cab,"GameController.Awake");H(0xe484bd,"GameController.Start");H(0xe4bd6a,"SpawnPlayers");
 H(0xdf9152,"Thief.Awake");H(0xdf9369,"Thief.Start");
 // DecodeResponse(this, string json) -> dump json (the bot-match data)
 try{Interceptor.attach(B.add(0xd34fb9),{onEnter:function(){var sp=this.context.esp;var j=rdstr(sp.add(8).readPointer());send({g:"DecodeResponse JSON: "+(j?j.substring(0,1500):"<null>")});}});}catch(e){}
 H(0xd34da2,"CreateBotMatch");
 send({g:"__ready"});
}
recv('off',function f(){forceOff=true; send({g:"offline forced"}); recv('off',f);});
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); n=len(cap())
    if n>600000: menu=True; break
print("MENU" if menu else "TIMEOUT","at",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("[force offline]",flush=True); sc.post({'type':'off'}); time.sleep(1)
print("[SNIPER]",flush=True); tap(700,730); time.sleep(6)
print("[GO]",flush=True); tap(1355,960); time.sleep(9)
print("[dismiss tips]",flush=True)
for i in range(3): tap(960,540); time.sleep(2)
shot("d_start.png")
print("[watch 90s for thief]",flush=True)
for k in range(15):
    time.sleep(6); shot("d_w%02d.png"%k)
print("done",flush=True)
