import frida, time, subprocess, sys
ADB="/opt/android-sdk/platform-tools/adb"
JS=r"""
var B=null, forceOff=false;
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,"GC.Awake");H(0xe4bd6a,"SpawnPlayers");H(0xe5a928,"AssemblePlayerList");
 try{Interceptor.attach(B.add(0xd41677),{onEnter:function(){var sp=this.context.esp;send({g:"set_isSniper("+(sp.add(8).readU32()?1:0)+")"});}});}catch(e){}
 send({g:"__ready"});
}
recv('off',function f(){forceOff=true; send({g:"offlineMode NOW 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); return len(d)

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("launched; polling for menu (past splash)",flush=True)
# poll until a large, STABLE screenshot (menu) appears
t0=time.time(); last=0; stable=0; menu=False
while time.time()-t0 < 120:
    time.sleep(4); n=len(cap())
    print("  poll size=%d"%n,flush=True)
    if n>450000:
        if abs(n-last)<3000: stable+=1
        else: stable=0
        if stable>=2: menu=True; break
    last=n
print("MENU" if menu else "MENU-TIMEOUT","at",int(time.time()-t0),"s",flush=True)
shot("c_00_menu.png")
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); shot("c_01_loadout.png")
print("[GO]",flush=True); tap(1355,960); time.sleep(9); shot("c_02_nest.png")
# gently progress intro tips (single taps) then observe
for i in range(4):
    tap(960,540); time.sleep(3); shot("c_03_tip%d.png"%i)
for k in range(6):
    time.sleep(5); shot("c_04_watch%d.png"%k)
print("done",flush=True)
