import frida, time, subprocess, sys
sys.path.insert(0,"/root/ants")
ADB="/opt/android-sdk/platform-tools/adb"
JS=r"""
var B=null;
function sstr(p){try{if(p.isNull()||p.compare(ptr(0x10000))<0)return null;var n=p.add(8).readS32();if(n<0||n>300)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){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 offline forced"});
}
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 shot(name):
    r=subprocess.run([ADB,"exec-out","screencap","-p"],capture_output=True)
    open("/root/ants/shots/"+name,"wb").write(r.stdout); print("  shot",name,len(r.stdout),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("launched, waiting 30s",flush=True); time.sleep(30)
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(3)
print("[SNIPER]",flush=True); tap(700,730); time.sleep(5)
print("[GO]",flush=True); tap(1355,960); time.sleep(8); shot("b_00_nest.png")
# progress intro tooltips by tapping the tooltip 'continue' spot, staying attached
for i in range(6):
    tap(960,540); time.sleep(3); shot("b_01_tip%d.png"%i)
# now just observe (no taps) to let thieves approach the window
for k in range(6):
    time.sleep(5); shot("b_02_watch%d.png"%k)
print("keeping attached 20s more",flush=True); time.sleep(20)
print("done",flush=True)
