#!/usr/bin/env python3
import frida, time, subprocess
ADB="/opt/android-sdk/platform-tools/adb"
JS=r"""
var GC=null,CN=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']);
 function rs(p){try{if(p.isNull())return "null";var l=p.add(8).readS32();if(l<0||l>300)return "<l"+l+">";return p.add(0xc).readUtf16String(l);}catch(e){return "<e>";}}
 var inDecode=false;
 T(function(){Interceptor.attach(b.add(0xd34fb9),{onEnter:function(){inDecode=true;send({g:"DecodeResponse ENTER"});},onLeave:function(){inDecode=false;send({g:"DecodeResponse LEAVE ok"});}});});
 // op_Equality(a,b) during decode -> log both strings (reveals field keys compared)
 T(function(){Interceptor.attach(b.add(0x6b319a),{onEnter:function(a){if(inDecode){send({g:"  strEq("+JSON.stringify(rs(a[0]))+","+JSON.stringify(rs(a[1]))+")"});}}});});
 // MatchPersona::Write -> log the persona string arg (a1 or a2)
 T(function(){Interceptor.attach(b.add(0xd3f6bb),{onEnter:function(a){send({g:"MatchPersona.Write a1="+JSON.stringify(rs(a[1]))+" a2="+JSON.stringify(rs(a[2]))});}});});
 [[0xe5a928,"AssemblePlayerList"],[0xd3c44a,"OnResponse"],[0xd34da2,"CreateBotMatch"],[0xd30e76,"OnInitiate(Photon)"],[0xe484bd,"GameController.Start"]].forEach(function(x){
   T(function(){Interceptor.attach(b.add(x[0]),{onEnter:function(){send({g:"CHAIN "+x[1]});}});});});
 // Photon dead-path markers
 T(function(){Interceptor.attach(b.add(0xd3c903),{onEnter:function(){send({g:"!!! NetworkController.Connect (PHOTON)"});}});});
 T(function(){Interceptor.attach(b.add(0xd3c818),{onEnter:function(){send({g:"!!! Photon.SetPlayerCustomProperties"});}});});
 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,10).map(function(a){var mm=Process.findModuleByAddress(a);return mm&&mm.name=="libil2cpp.so"?"il+0x"+a.sub(mm.base).toString(16):(mm?mm.name:""+a);}).join(" ");send({g:"*** ABORT "+bt});}});});
 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(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)
sc.on("message",lambda m,d: print(m.get("payload",m),flush=True)); sc.load(); time.sleep(1)
for c in [("690","730"),("690","730"),("1350","960"),("1350","960")]:
    subprocess.run([ADB,"shell","input","tap",c[0],c[1]]); time.sleep(4)
time.sleep(10); print("=== done ===",flush=True)
