import struct
D=open('extracted/assets/bin/Data/Managed/Metadata/global-metadata.dat','rb').read()
H=struct.unpack('<68i',D[:272])
STR_OFF,STR_SZ=H[6],H[7]
def s(i):
    if i<0 or i>=STR_SZ: return None
    e=D.index(b'\0',STR_OFF+i)
    return D[STR_OFF+i:e].decode('utf-8','replace')
def ok(x):
    return x is not None and 0<len(x)<200 and all(32<=ord(c)<127 for c in x)

# probe a region: try strides, score how often field[k] is a valid string index
def probe(off,size,name,fieldslots=(0,1)):
    best=[]
    for stride in range(8,132,4):
        if size%stride: continue
        n=size//stride
        if n<2: continue
        sc=0; tot=0; samples=[]
        for i in range(0,min(n,400)):
            base=off+i*stride
            vals=struct.unpack_from('<%di'%(stride//4),D,base)
            for k in fieldslots:
                if k<len(vals):
                    tot+=1
                    t=s(vals[k])
                    if ok(t):
                        sc+=1
                        if len(samples)<4 and k==0: samples.append(t)
        best.append((sc/max(tot,1),stride,n,samples))
    best.sort(reverse=True)
    print('==',name,'off=%d size=%d'%(off,size))
    for r in best[:3]:
        print('   score=%.3f stride=%-4d count=%-7d %s'%r)

probe(H[8],H[9],'events(idx8)')
probe(H[10],H[11],'properties(idx10)')
probe(H[12],H[13],'methods(idx12)')
probe(H[22],H[23],'parameters(idx22)')
probe(H[24],H[25],'fields(idx24)')
probe(H[40],H[41],'typeDefs(idx40)')
probe(H[44],H[45],'images(idx44)')
