#!/usr/bin/env python3
import frida, time, subprocess
ADB="/opt/android-sdk/platform-tools/adb"
JS=r"""
var CN=null,GC=null;
function cn(p){try{if(p.isNull())return "NULL";return CN(GC(p)).readUtf8String();}catch(e){return "?";}}
function rs(p){try{if(p.isNull())return null;var l=p.add(8).readS32();if(l<0||l>400)return null;return p.add(0xc).readUtf16String(l);}catch(e){return null;}}
function ins(){var m=Process.findModuleByName("libil2cpp.so");if(!m)return false;var b=m.base;
 function T(f){try{f();}catch(e){send({w:""+e});}}
 GC=new NativeFunction(b.add(0xbc7128),'pointer',['pointer']);
 CN=new NativeFunction(b.add(0xbc61fc),'pointer',['pointer']);
 var dep=0;
 T(function(){Interceptor.attach(b.add(0xd34fb9),{onEnter:function(){dep++;send({g:"== DecodeResponse ENTER"});},onLeave:function(){dep--;}});});
 [[0xd35122,"personas-loop-start"],[0xd35738,"build-persona"],[0xd3552c,"ERROR-BRANCH(errCode<=0)"],[0xd357e1,"TAIL(build matchId+MatchResponse)"],[0xd35759,"MatchResponse-nullcheck"],[0xd358e6,"CATCH/finally"]].forEach(function(x){
   T(function(){Interceptor.attach(b.add(x[0]),{onEnter:function(){if(dep>0)send({g:"  ["+x[1]+"]"});}});});});
 T(function(){Interceptor.attach(b.add(0xbc68b8),{onEnter:function(a){if(dep<=0)return;var ex=a[0];send({g:"  *** EXC "+cn(ex)+" msg="+JSON.stringify(rs(ex.add(0x40).readPointer()))});}});});
 send({g:"__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(43)
subprocess.run([ADB,"shell","input","tap","700","730"]); time.sleep(3)
subprocess.run([ADB,"shell","input","tap","700","730"]); time.sleep(4)
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)
sc.on("message",lambda m,d: print(m.get("payload",m),flush=True)); sc.load(); time.sleep(1)
subprocess.run([ADB,"shell","input","tap","1355","970"]); time.sleep(4)
subprocess.run([ADB,"shell","input","tap","1355","970"]); time.sleep(16)
print("=== done ===",flush=True)
