#!/usr/bin/env python3
"""Robust: fresh-launch to menu, tap SNIPER (retry) + GO, trace the full match
chain, screenshot the result. Use to validate a bmm reply format."""
import frida, time, subprocess, sys
ADB="/opt/android-sdk/platform-tools/adb"
JS=r"""
function rd(p){try{if(p.isNull())return null;var l=p.add(8).readS32();if(l<0||l>300)return null;return p.add(0xc).readUtf16String(l);}catch(e){return "<o>";}}
function ins(){var m=Process.findModuleByName("libil2cpp.so");if(!m)return false;var b=m.base;
 function T(f){try{f();}catch(e){send({warn:""+e});}}
 var seen={};
 [[0xea27d3,"OnSelectSniperMode"],[0xd31a4f,"Matchmaking.Initiate"],[0xd34fb9,"DecodeResponse"],[0xd3f6bb,"MatchPersona.parse"],[0xd415f7,"MatchPersona.Read"],[0xd4164b,"SetBot"],[0xd410d6,"Thief.GetBotDef"],[0xd4110b,"Sniper.GetBotDef"],[0xe5a928,"AssemblePlayerList"],[0xd73f05,"Photon.SetPlayerProps"],[0x1003caf,"Photon.Connect"],[0xd3cb37,"LoadLevel"],[0xe484bd,"GameController.Start"],[0xe4bd6a,"SpawnPlayers"]].forEach(function(x){
   T(function(){Interceptor.attach(b.add(x[0]),{onEnter:function(){ if(!seen[x[1]]){seen[x[1]]=1; send({info:"CHAIN "+x[1]});} }});});
 });
 // MatchPersona parse: dump the persona name being read (if arg is a string)
 T(function(){Interceptor.attach(b.add(0xd3f6bb),{onEnter:function(a){ try{var s=rd(a[1]); if(s)send({info:"  persona-in: "+s.substring(0,60)});}catch(e){} }});});
 T(function(){var ab=Process.findModuleByName("libc.so").findExportByName("abort");Interceptor.attach(ab,{onEnter:function(){var bt=Thread.backtrace(this.context,Backtracer.ACCURATE).slice(0,8).map(function(a){var mm=Process.findModuleByAddress(a);return mm?mm.name+"+0x"+a.sub(mm.base).toString(16):""+a;}).join(",");send({info:"*** ABORT "+bt});}});});
 send({info:"__ready"});return true;}
if(!ins()){var iv=setInterval(function(){if(ins())clearInterval(iv);},120);}
"""
subprocess.run([ADB,"shell","am","force-stop","se.foglo.svt"]); time.sleep(3)
subprocess.run([ADB,"shell","monkey","-p","se.foglo.svt","-c","android.intent.category.LAUNCHER","1"],stdout=-3,stderr=-3)
time.sleep(44)
dev=frida.get_usb_device(timeout=10)
pid=int(subprocess.run([ADB,"shell","pidof","se.foglo.svt"],capture_output=True,text=True).stdout.split()[0])
s=dev.attach(pid); sc=s.create_script(JS); state={"sel":False,"init":False}
def om(mm,d):
    p=mm.get("payload",mm); info=str(p.get("info","")) if isinstance(p,dict) else ""
    if "OnSelectSniperMode" in info: state["sel"]=True
    if "Matchmaking.Initiate" in info: state["init"]=True
    print(p,flush=True)
sc.on("message",om); sc.load(); time.sleep(1)
for _ in range(5):
    if state["sel"]: break
    subprocess.run([ADB,"shell","input","tap","690","730"]); time.sleep(3)
time.sleep(2)
for _ in range(4):
    if state["init"]: break
    subprocess.run([ADB,"shell","input","tap","1350","960"]); time.sleep(3)
time.sleep(12)
subprocess.run([ADB,"shell","screencap","-p","/sdcard/mr.png"])
subprocess.run([ADB,"pull","/sdcard/mr.png","/root/ants/matchrun.png"],stdout=-3,stderr=-3)
print("=== screenshot -> matchrun.png ===",flush=True)
