import json,sys,time,frida
addrs=json.load(open('/root/ants/x86addrs.json'))
GI=addrs['PlayerProfile::get_instance/0']
JS=r"""
var gi=%d; var base=null;
function ins(){var m=Process.findModuleByName('libil2cpp.so'); if(m===null)return false; base=m.base;
 Interceptor.attach(base.add(gi),{onLeave:function(ret){send({t:'gi',ret:ret.toInt32()});}});
 send({t:'info',m:'hooked get_instance'}); return true;}
if(!ins()){var iv=setInterval(function(){if(ins())clearInterval(iv);},100);}
""" % GI
dev=frida.get_device_manager().add_remote_device('127.0.0.1:27042')
pid=dev.spawn(['se.foglo.svt']); ses=dev.attach(pid); sc=ses.create_script(JS)
seen={}
def om(m,d):
    if m['type']!='send': return
    p=m['payload']
    if p['t']=='gi':
        v=p['ret']; k='NULL' if v==0 else 'non-null'
        seen[k]=seen.get(k,0)+1
        if seen[k]<=3: print('%7.2fs  get_instance -> %s (0x%x)'%(time.time()-t0,k,v&0xffffffff))
    else: print('[info]',p['m'])
sc.on('message',om); sc.load(); t0=time.time(); dev.resume(pid)
time.sleep(int(sys.argv[1]) if len(sys.argv)>1 else 25)
print('\nsummary:',seen)
