import json,sys,time,subprocess,frida
ADB="/opt/android-sdk/platform-tools/adb"
A=json.load(open("/root/ants/sid_addr.json"))
JS=r"""
var A=%s;
function cname(p){try{var k=p.readPointer();var n=k.add(0x10).readPointer();var s=n.readUtf8String();if(s&&/^[\x20-\x7e]+$/.test(s))return s;}catch(e){} 
  try{var k=p.readPointer();for(var o=8;o<=0x2c;o+=4){var np=k.add(o).readPointer();var s=np.readUtf8String();if(s&&s.length<40&&/^[A-Za-z]/.test(s))return s;}}catch(e){}
  return "?";}
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']);
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("revived-session-0001")));}});
["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']));
var forced=0;
Interceptor.attach(b.add(0xfd1c2a),{onEnter:function(a){this.s=a[0];},onLeave:function(rv){
  try{var pc=this.s.add(0x3c).readS32();
    if(pc==9){
      var core=this.s.add(0x1c).readPointer();   // FE.Core
      if(core.add(0xa0).readU8()==0){ core.add(0xa0).writeU8(1); forced++; if(forced<=2)send({info:"forced Core.ready(+0xa0)=1"}); }
    }
  }catch(e){send({err:""+e});}
}});
send({info:"ready"});return true;}
if(!ins()){var iv=setInterval(function(){if(ins())clearInterval(iv);},150);}
""" % json.dumps(A)
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)
sc.on("message",lambda m,d: print(json.dumps(m.get("payload",m)),flush=True))
sc.load();time.sleep(40)
