import json,sys,time,subprocess,frida
ADB="/opt/android-sdk/platform-tools/adb"
C=json.load(open("/root/ants/combo3.json")); A,TL=C["fixes"],C["trace"]
JS=r"""
var A=%s,TL=%s;
function ins(){var m=Process.findModuleByName("libil2cpp.so");if(!m)return false;var b=m.base;
var e=m.findExportByName?m.findExportByName("il2cpp_string_new"):Module.getGlobalExportByName("il2cpp_string_new");var sn=new NativeFunction(e,'pointer',['pointer']);var SID="rev-"+(Date.now()%%1000000);
Interceptor.attach(b.add(A["get_sessionId"]),{onLeave:function(rv){var em=true;try{if(!rv.isNull())em=(rv.add(8).readS32()<=0);}catch(e){}if(em)rv.replace(sn(Memory.allocUtf8String(SID)));}});
["get_TermsAccepted","get_FacebookTermsAccepted"].forEach(function(k){if(A[k])Interceptor.attach(b.add(A[k]),{onLeave:function(rv){rv.replace(ptr(1));}});});
if(A["Check25RoundsPlayed"])Interceptor.replace(b.add(A["Check25RoundsPlayed"]),new NativeCallback(function(s){},'void',['pointer']));
if(A["PP.get_initialized"]){var li=-1;Interceptor.attach(b.add(A["PP.get_initialized"]),{onLeave:function(rv){var v=rv.toInt32()&1;if(v!=li){li=v;send({e:"PP.initialized="+v});}}});}
["OnDeviceLimit","OnPlayingOnAnotherDevice","Core.OnPlayingOnAnotherDevice"].forEach(function(k){if(A[k])Interceptor.replace(b.add(A[k]),new NativeCallback(function(){},'void',['pointer','pointer']));});

for(var n in TL){(function(n,r){try{
  if(n=="BOOT"){var lp=-9;Interceptor.attach(b.add(r),{onEnter:function(a){this.s=a[0];},onLeave:function(rv){try{var pc=this.s.add(0x3c).readS32();if(pc!=lp){lp=pc;send({e:"BOOT pc="+pc});}
      if(pc==9){var core=this.s.add(0x1c).readPointer();var mes=this.s.add(0x20).readPointer();
        if(core.add(0xa0).readU8()==0)core.add(0xa0).writeU8(1);
        if(mes.add(0x20).readU8()!=0)mes.add(0x20).writeU8(0);
      }
    }catch(e){}}});}
  else if(n=="OnEvent"){Interceptor.attach(b.add(r),{onEnter:function(a){var v=a[1].toInt32();if(v==56||v==57||v==58)send({e:"OnEvent "+v});}});}
  else Interceptor.attach(b.add(r),{onEnter:function(){send({e:n});}});
}catch(e){}})(n,TL[n]);}
send({e:"ready"});return true;}
if(!ins()){var iv=setInterval(function(){if(ins())clearInterval(iv);},150);}
""" % (json.dumps(A),json.dumps(TL))
dev=frida.get_device_manager().add_remote_device("127.0.0.1:27042")
subprocess.run([ADB,"shell","am","force-stop","se.foglo.svt"])
subprocess.run([ADB,"shell","monkey","-p","se.foglo.svt","-c","android.intent.category.LAUNCHER","1"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
pid=None
for _ in range(80):
    o=subprocess.run([ADB,"shell","pidof","se.foglo.svt"],capture_output=True,text=True).stdout.strip()
    if o:pid=int(o.split()[0]);break
    time.sleep(0.25)
s=dev.attach(pid);sc=s.create_script(JS);t0=time.time()
sc.on("message",lambda m,d: print("[%6.2fs] %s"%(time.time()-t0,m["payload"]["e"]),flush=True) if m["type"]=="send" else None)
sc.load();time.sleep(int(sys.argv[1]) if len(sys.argv)>1 else 60)
