# Path: omnitool/database.py
# def get_custom_db(txt):
# def parse():


from struct import unpack, pack, Struct
from .database import items, rev_items, multitiles
import sys

def get_header(f):
    """ returns a dict of header data"""
    sectiondata = None
    version = get_gint(f)
    if version >= 87:
        if version > 140:
            if get_long(f) != 172097103742133618:
                raise Exception("Invalid world header. (Typecheck number mismatch)")
            rev = get_uint(f)
            get_long(f)


        #get sections
        sectiondata = {"sections" : get_uint(f, get_gshort(f)),
                       "tiletypes" : get_gshort(f)}
        multitiles_ = set()
        mask = 0x80
        for x in range(sectiondata["tiletypes"]):
            if mask == 0x80:
                flags = get_gbyte(f)
                mask = 0x01
            else: mask <<=1
            if flags & mask == mask:
                multitiles_.add(x)
        if f.tell() != sectiondata["sections"][0]:
            print("Warning: SectionHeader for world", self.header["name"].decode(),
                  "of different size than expected, errors may occur.")

    else:

# Path: omnitool/database.py
# def get_custom_db(txt):
# def parse():


# Path: omnitool/tlib.py
from struct import unpack, pack, Struct
from .database import items, rev_items, multitiles
import sys


def get_header(f):
    """ returns a dict of header data"""
    sectiondata = None
    version = get_gint(f)
    if version >= 87:
        if version > 140:
            if get_long(f) != 172097103742133618:
                raise Exception("Invalid world header. (Typecheck number mismatch)")
            rev = get_uint(f)
            get_long(f)


        #get sections
        sectiondata = {"sections" : get_uint(f, get_gshort(f)),
                       "tiletypes" : get_gshort(f)}
        multitiles_ = set()
        mask = 0x80
        for x in range(sectiondata["tiletypes"]):
            if mask == 0x80:
                flags = get_gbyte(f)
                mask = 0x01
            else: mask <<=1
            if flags & mask == mask:
                multitiles_.add(x)
        if f.tell() != sectiondata["sections"][0]:
            print("Warning: SectionHeader for world", self.header["name"].decode(),
                  "of different size than expected, errors may occur.")

    else:

        multitiles_ = multitiles