#!/usr/bin/env python3
import frida, time, subprocess
ADB="/opt/android-sdk/platform-tools/adb"
JS=r"""
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});}}
 // force PhotonNetwork.get_offlineMode -> true
 T(function(){Interceptor.attach(b.add(0xd6bb98),{onLeave:function(r){r.replace(ptr(1));}});});
 // observe flow
 [[0xd34fb9,"DecodeResponse"],[0xd3c44a,"OnResponse"],[0xe5a928,"AssemblePlayerList"],[0xd34da2,"CreateBotMatch"],[0xe484bd,"GameController.Start"],[0xe4bd6a,"SpawnPlayers"],[0xd6c1e0,"PingRegionsConnectBest"],[0x1009bc0,"ConnectToNameServer"]].forEach(function(x){
   if(typeof x[0]!=="number")return;
   T(function(){Interceptor.attach(b.add(x[0]),{onEnter:function(){send({g:"CHAIN "+x[1]});}});});});
 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=="libil2cpp.so"?"il+0x"+a.sub(mm.base).toString(16):(mm?mm.name:""+a);}).join(" ");send({g:"*** ABORT "+bt});}});});
 send({g:"__ready offlineMode=1"});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(8)
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(34)
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(12); print("=== done ===",flush=True)
