def load_objregister(ggpkfile):
    print("Loading object register")
    rnode = ggpkfile["Metadata/GameObjectRegister"]
    filebytes = rnode.record.extract()
    filebytes.seek(2)

    hashes = {}

    linelen = -1
    while linelen:
        ln = filebytes.readline()
        linelen = len(ln)
        
        if linelen < 1:
            break

        # handle issues from first and last lines
        if linelen%2 != 0:            
            if (ln[0] == 0):
                ln = ln[1:]
            else:
                ln = ln[:-1]
                
            objstring = ln.decode("utf-16-le").strip()
        else:
            objstring = ln.decode("utf-16-be").strip()

        ascbytes = objstring.encode('ascii')
        objhash = murmur2.murmur2_32(ascbytes, 0)
        hashes[objhash] = objstring
        
    return hashes
def load_characters(ggpkobj):
    print("Loading characters")
    resultDict = {}
    
    rit = getDatIterator(ggpkobj, "Characters.dat")

    for i in rit:
        objhash = murmur2.murmur2_32(i['Id'].encode('ascii'), 0)
        resultDict[objhash] = i['Id']

    return resultDict
def load_monster_varieties(ggpkobj):
    print("Loading monster varieties")

    resultDict = {}
    
    rIt = getDatIterator(ggpkobj, "MonsterVarieties.dat")
    for i in rIt:
        istring = i['Id']
        objhash = murmur2.murmur2_32(istring.encode('ascii'), 0)
        resultDict[objhash] = istring

    return resultDict
def load_items(ggpkobj):
    print("Loading items")
    resultDict = {}

    try:
        rit = getDatIterator(ggpkobj, "BaseItemTypes.dat")
    except:
        print("Failed to load baseItemTypes.dat")
        return resultDict

    for i in rit:
        objhash = murmur2.murmur2_32(i['Id'].encode('ascii'), 0)
        resultDict[objhash] = i['Id']

    return resultDict
    fd.close()
    return itemhashes
    

if __name__ == "__main__":

    st = "Metadata/Monsters/Spiders/SpiderExplodeFastWithExperience2"
    st = "Metadata/Monsters/Skeletons/SkeletonFireLightning"
    
    st = "Metadata/Monsters/Spiders/SpiderThornExplodeMinionsOnDeath2" #0x7fe07acf
    st = "OneHandMace8"
    #st = "Metadata/Monsters/LeagueBestiary/SpiderPlatedBestiarySpiritBoss" #0xc0a52243
    #st = "Metadata/Monsters/Scorpion/Scorpion"
    st = "Metadata/Pet/BabyElephant/BabyElephant"
    ascbytes = st.encode('ascii')
    print(hex(murmur2.murmur2_32(ascbytes, 0)))
    st = "HideoutBeach" 
    #exit()

    print("Loading GGPK")
    ggpkobj = createggpkfile(poedir)

    print("Gathering data...")
    resultdict = {}
    resultdict['MonsterVarietiesHashes'] = load_monster_varieties(ggpkobj)
    resultdict['MonsterVarietiesIndex'] = load_monster_varieties_index(ggpkobj)
    resultdict['AreaCodes'] = load_world_areacodes(ggpkobj)
    resultdict['ObjRegisterHashes'] = load_objregister(ggpkobj)
    resultdict['ChestHashes'] = load_chests(ggpkobj)
    resultdict['CharacterHashes'] = load_characters(ggpkobj)
    resultdict['NPCHashes'] = load_npcs(ggpkobj)