#!/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});}}
 // Guard Enumerable.Count(source,pred,mi): return 0 if source null
 T(function(){
   var addr=b.add(0x11f1032);
   var orig=new NativeFunction(addr,'int',['pointer','pointer','pointer']);
   Interceptor.replace(addr,new NativeCallback(function(src,pred,mi){
     if(src.isNull()){send({g:"Count(null)->0 guarded"});return 0;}
     try{return orig(src,pred,mi);}catch(e){return 0;}
   },'int',['pointer','pointer','pointer']));
 });
 // Passthrough Photon DH crypto so ops are plaintext on the wire.
 // DiffieHellmanCryptoProvider::Encrypt/Decrypt(this,byte[] data,int off,int cnt)->byte[]
 // passthrough: set array length to cnt and return the buffer (plaintext at 0x10)
 // Encrypt (client->server): shift +2 so client's msg-header write lands in a reserved gap
 function passthru_enc(thiz,data,off,cnt,mi){
   try{
     var tmp=Memory.alloc(cnt); Memory.copy(tmp,data.add(0x10).add(off),cnt);
     Memory.copy(data.add(0x12),tmp,cnt);
     data.add(0x10).writeU8(0); data.add(0x11).writeU8(0);
     data.add(0xc).writeS32(cnt+2);
   }catch(e){}
   return data;
 }
 // Decrypt (server->client): return content as-is
 function passthru_dec(thiz,data,off,cnt,mi){
   try{ if(off!=0){ Memory.copy(data.add(0x10),data.add(0x10).add(off),cnt); } data.add(0xc).writeS32(cnt); }catch(e){}
   return data;
 }
//  T(function(){Interceptor.replace(b.add(0xb57948),new NativeCallback(passthru_enc,'pointer',['pointer','pointer','int','int','pointer']));});
//  T(function(){Interceptor.replace(b.add(0xb57ae8),new NativeCallback(passthru_dec,'pointer',['pointer','pointer','int','int','pointer']));});
 // log NetworkingPeer.OnOperationResponse (auth flow) + OnStatusChanged
 T(function(){Interceptor.attach(b.add(0x100ee8a),{onEnter:function(){send({g:">>> OnOperationResponse"});}});});
 T(function(){Interceptor.attach(b.add(0x1012c1a),{onEnter:function(a){try{send({g:">>> OnStatusChanged "+a[1].toInt32()});}catch(e){}}});});
 [[0xd3c44a,"OnResponse"],[0xe5a928,"AssemblePlayerList"],[0xd3c818,"SetPlayerCustomProperties"],[0xd3c903,"NetworkController.Connect"],[0xd6c1e0,"PingRegionsConnectBest"],[0x1009bc0,"ConnectToNameServer"],[0xd8685b,"CreateRoom"],[0xd34da2,"CreateBotMatch"],[0xe484bd,"GameController.Start"],[0xe4bd6a,"SpawnPlayers"]].forEach(function(x){
   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,9).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(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)
