#by Durik256
from inc_noesis import *

def registerNoesisTypes():
    handle = noesis.register("Football Superstars", ".keg")
    noesis.setHandlerExtractArc(handle, ExtractARC)
    return 1
    
def ExtractARC(fileName, fileLen, justChecking):
    if fileLen < 16:
        return 0
        
    if justChecking: #it's valid
        return 1
    
    with open(fileName ,'rb') as f:
        fsize = os.path.getsize(fileName)
        magic, ver = f.read(4), noeUnpack('I', f.read(4))[0]
        
        while f.tell() < fsize:
            inf = noeUnpack('2I', f.read(8))
            
            if inf[0] == 3585640067:
                f.seek(inf[1] + 8,1)
            
            elif inf[0] == 3514407732:
                f.seek(-4,1)
            
            elif inf[0] == 3694837229:
                lenPath = inf[1] + 1#noeUnpack('I', f.read(4))[0] + 1
                _path = noeStrFromBytes(f.read(lenPath))
                lenName = noeUnpack('I', f.read(4))[0] + 1
                _name = noeStrFromBytes(f.read(lenName))
                
                V0 = noeUnpack('I', f.read(4))[0]
                f.seek(19,1)
                V1 = noeUnpack('I', f.read(4))[0]
                
                curPos = f.tell()
                
                f.seek(V1+16)
                data = f.read(V0)
                rapi.exportArchiveFile(_path, data)
                print(_path)
                f.seek(curPos)
            
            else:
                break
    
    print("Extracting all files.")
    return 1
    
   
   