示例#1
0
def parse_package(zip_file, info, pak_id, disp_name):
    """Parse through the given package to find all the components."""
    global res_count
    for pre in Property.find_key(info, 'Prerequisites', []).value:
        if pre.value not in packages:
            utils.con_log(
                'Package "' +
                pre.value +
                '" required for "' +
                pak_id +
                '" - ignoring package!'
            )
            return False
    objects = 0
    # First read through all the components we have, so we can match
    # overrides to the originals
    for comp_type in obj_types:
        # Look for overrides
        for obj in info.find_all("Overrides", comp_type):
            obj_id = obj['id']
            obj_override[comp_type][obj_id].append(
                (zip_file, obj)
            )

        for obj in info.find_all(comp_type):
            obj_id = obj['id']
            if obj_id in all_obj[comp_type]:
                raise Exception('ERROR! "' + obj_id + '" defined twice!')
            objects += 1
            all_obj[comp_type][obj_id] = ObjData(
                zip_file,
                obj,
                pak_id,
                disp_name,
            )

    if res_count != -1:
        for item in zip_names(zip_file):
            if item.startswith("resources"):
                res_count += 1
        loader.set_length("RES", res_count)
    return objects
示例#2
0
def load_packages(
        pak_dir,
        load_res,
        log_item_fallbacks=False,
        log_missing_styles=False,
        log_missing_ent_count=False,
        ):
    """Scan and read in all packages in the specified directory."""
    global res_count, LOG_ENT_COUNT
    pak_dir = os.path.abspath(os.path.join(os.getcwd(), '..', pak_dir))
    if load_res:
        res_count = 0
    else:
        loader.skip_stage("RES")

    LOG_ENT_COUNT = log_missing_ent_count
    print('ENT_COUNT:', LOG_ENT_COUNT)
    zips = []
    data['zips'] = []
    try:
        find_packages(pak_dir, zips, data['zips'])

        loader.set_length("PAK", len(packages))

        for obj_type in obj_types:
            all_obj[obj_type] = {}
            obj_override[obj_type] = defaultdict(list)
            data[obj_type] = []

        objects = 0
        for pak_id, (zip_file, info, name, dispName) in packages.items():
            print(("Reading objects from '" + pak_id + "'...").ljust(50), end='')
            obj_count = parse_package(zip_file, info, pak_id, dispName)
            objects += obj_count
            loader.step("PAK")
            print("Done!")

        loader.set_length("OBJ", objects)

        # Except for StyleVars, each object will have at least 1 image -
        # in UI.py we step the progress once per object.
        loader.set_length("IMG", objects - len(all_obj['StyleVar']))

        for obj_type, objs in all_obj.items():
            for obj_id, obj_data in objs.items():
                print("Loading " + obj_type + ' "' + obj_id + '"!')
                # parse through the object and return the resultant class
                try:
                    object_ = obj_types[obj_type].parse(
                        ParseData(
                            obj_data.zip_file,
                            obj_id,
                            obj_data.info_block,
                        )
                    )
                except (NoKeyError, IndexError) as e:
                    reraise_keyerror(e, obj_id)

                object_.pak_id = obj_data.pak_id
                object_.pak_name = obj_data.disp_name
                for zip_file, info_block in \
                        obj_override[obj_type].get(obj_id, []):
                    override = obj_types[obj_type].parse(
                        ParseData(
                            zip_file,
                            obj_id,
                            info_block,
                        )
                    )
                    object_.add_over(override)
                data[obj_type].append(object_)
                loader.step("OBJ")
        if load_res:
            print('Extracting Resources...')
            for zip_file in zips:
                for path in zip_names(zip_file):
                    loc = os.path.normcase(path)
                    if loc.startswith("resources"):
                        loader.step("RES")
                        zip_file.extract(path, path="../cache/")

            shutil.rmtree('../images/cache', ignore_errors=True)
            shutil.rmtree('../inst_cache/', ignore_errors=True)
            shutil.rmtree('../source_cache/', ignore_errors=True)

            if os.path.isdir("../cache/resources/bee2"):
                shutil.move("../cache/resources/bee2", "../images/cache")
            if os.path.isdir("../cache/resources/instances"):
                shutil.move("../cache/resources/instances", "../inst_cache/")
            for file_type in ("materials", "models", "sound", "scripts"):
                if os.path.isdir("../cache/resources/" + file_type):
                    shutil.move(
                        "../cache/resources/" + file_type,
                        "../source_cache/" + file_type,
                    )

            shutil.rmtree('../cache/', ignore_errors=True)
            print('Done!')

    finally:
        # close them all, we've already read the contents.
        for z in zips:
            z.close()

    print('Allocating styled items...')
    setup_style_tree(
        data['Item'],
        data['Style'],
        log_item_fallbacks,
        log_missing_styles,
    )
    print(data['zips'])
    print('Done!')
    return data
示例#3
0
def load_packages(
        pak_dir,
        log_item_fallbacks=False,
        log_missing_styles=False,
        log_missing_ent_count=False,
        ):
    """Scan and read in all packages in the specified directory."""
    global LOG_ENT_COUNT
    pak_dir = os.path.abspath(os.path.join(os.getcwd(), '..', pak_dir))

    if not os.path.isdir(pak_dir):
        from tkinter import messagebox
        import sys
        # We don't have a packages directory!
        messagebox.showerror(
            master=loader,
            title='BEE2 - Invalid Packages Directory!',
            message='The given packages directory is not present!\n'
                    'Get the packages from '
                    '"http://github.com/TeamSpen210/BEE2-items" '
                    'and place them in "' + pak_dir +
                    os.path.sep + '".',
                    # Add slash to the end to indicate it's a folder.
        )
        sys.exit('No Packages Directory!')

    LOG_ENT_COUNT = log_missing_ent_count
    print('ENT_COUNT:', LOG_ENT_COUNT)
    zips = []
    data['zips'] = []
    try:
        find_packages(pak_dir, zips, data['zips'])

        loader.set_length("PAK", len(packages))

        for obj_type in OBJ_TYPES:
            all_obj[obj_type] = {}
            obj_override[obj_type] = defaultdict(list)
            data[obj_type] = []

        objects = 0
        images = 0
        for pak_id, (zip_file, info, name, dispName) in packages.items():
            print(
                ("Reading objects from '" + pak_id + "'...").ljust(50),
                end=''
            )
            obj_count, img_count = parse_package(
                zip_file,
                info,
                pak_id,
                dispName,
            )
            objects += obj_count
            images += img_count
            loader.step("PAK")
            print("Done!")

        loader.set_length("OBJ", objects)
        loader.set_length("IMG_EX", images)

        # The number of images we need to load is the number of objects,
        # excluding some types like Stylevars or PackLists.
        loader.set_length(
            "IMG",
            sum(
                len(all_obj[key])
                for key, opts in
                OBJ_TYPES.items()
                if opts.has_img
            )
        )

        for obj_type, objs in all_obj.items():
            for obj_id, obj_data in objs.items():
                print("Loading " + obj_type + ' "' + obj_id + '"!')
                # parse through the object and return the resultant class
                try:
                    object_ = OBJ_TYPES[obj_type].cls.parse(
                        ParseData(
                            obj_data.zip_file,
                            obj_id,
                            obj_data.info_block,
                            obj_data.pak_id,
                        )
                    )
                except (NoKeyError, IndexError) as e:
                    reraise_keyerror(e, obj_id)

                object_.pak_id = obj_data.pak_id
                object_.pak_name = obj_data.disp_name
                for override_data in obj_override[obj_type].get(obj_id, []):
                    override = OBJ_TYPES[obj_type].cls.parse(
                        override_data
                    )
                    object_.add_over(override)
                data[obj_type].append(object_)
                loader.step("OBJ")

        cache_folder = os.path.abspath('../cache/')

        shutil.rmtree(cache_folder, ignore_errors=True)
        img_loc = os.path.join('resources', 'bee2')
        for zip_file in zips:
            for path in zip_names(zip_file):
                loc = os.path.normcase(path).casefold()
                if loc.startswith(img_loc):
                    loader.step("IMG_EX")
                    zip_file.extract(path, path=cache_folder)

        shutil.rmtree('../images/cache', ignore_errors=True)
        if os.path.isdir("../cache/resources/bee2"):
            shutil.move("../cache/resources/bee2", "../images/cache")
        shutil.rmtree('../cache/', ignore_errors=True)

    finally:
        # close them all, we've already read the contents.
        for z in zips:
            z.close()

    print('Allocating styled items...')
    setup_style_tree(
        data['Item'],
        data['Style'],
        log_item_fallbacks,
        log_missing_styles,
    )
    print(data['zips'])
    print('Done!')
    return data
示例#4
0
def load_packages(
    pak_dir,
    log_item_fallbacks=False,
    log_missing_styles=False,
    log_missing_ent_count=False,
):
    """Scan and read in all packages in the specified directory."""
    global LOG_ENT_COUNT
    pak_dir = os.path.abspath(os.path.join(os.getcwd(), '..', pak_dir))

    if not os.path.isdir(pak_dir):
        from tkinter import messagebox
        import sys
        # We don't have a packages directory!
        messagebox.showerror(
            master=loader,
            title='BEE2 - Invalid Packages Directory!',
            message='The given packages directory is not present!\n'
            'Get the packages from '
            '"http://github.com/TeamSpen210/BEE2-items" '
            'and place them in "' + pak_dir + os.path.sep + '".',
            # Add slash to the end to indicate it's a folder.
        )
        sys.exit('No Packages Directory!')

    LOG_ENT_COUNT = log_missing_ent_count
    print('ENT_COUNT:', LOG_ENT_COUNT)
    zips = []
    data['zips'] = []
    try:
        find_packages(pak_dir, zips, data['zips'])

        loader.set_length("PAK", len(packages))

        for obj_type in OBJ_TYPES:
            all_obj[obj_type] = {}
            obj_override[obj_type] = defaultdict(list)
            data[obj_type] = []

        objects = 0
        images = 0
        for pak_id, (zip_file, info, name, dispName) in packages.items():
            print(("Reading objects from '" + pak_id + "'...").ljust(50),
                  end='')
            obj_count, img_count = parse_package(
                zip_file,
                info,
                pak_id,
                dispName,
            )
            objects += obj_count
            images += img_count
            loader.step("PAK")
            print("Done!")

        loader.set_length("OBJ", objects)
        loader.set_length("IMG_EX", images)

        # The number of images we need to load is the number of objects,
        # excluding some types like Stylevars or PackLists.
        loader.set_length(
            "IMG",
            sum(
                len(all_obj[key]) for key, opts in OBJ_TYPES.items()
                if opts.has_img))

        for obj_type, objs in all_obj.items():
            for obj_id, obj_data in objs.items():
                print("Loading " + obj_type + ' "' + obj_id + '"!')
                # parse through the object and return the resultant class
                try:
                    object_ = OBJ_TYPES[obj_type].cls.parse(
                        ParseData(
                            obj_data.zip_file,
                            obj_id,
                            obj_data.info_block,
                            obj_data.pak_id,
                        ))
                except (NoKeyError, IndexError) as e:
                    reraise_keyerror(e, obj_id)

                object_.pak_id = obj_data.pak_id
                object_.pak_name = obj_data.disp_name
                for override_data in obj_override[obj_type].get(obj_id, []):
                    override = OBJ_TYPES[obj_type].cls.parse(override_data)
                    object_.add_over(override)
                data[obj_type].append(object_)
                loader.step("OBJ")

        cache_folder = os.path.abspath('../cache/')

        shutil.rmtree(cache_folder, ignore_errors=True)
        img_loc = os.path.join('resources', 'bee2')
        for zip_file in zips:
            for path in zip_names(zip_file):
                loc = os.path.normcase(path).casefold()
                if loc.startswith(img_loc):
                    loader.step("IMG_EX")
                    zip_file.extract(path, path=cache_folder)

        shutil.rmtree('../images/cache', ignore_errors=True)
        if os.path.isdir("../cache/resources/bee2"):
            shutil.move("../cache/resources/bee2", "../images/cache")
        shutil.rmtree('../cache/', ignore_errors=True)

    finally:
        # close them all, we've already read the contents.
        for z in zips:
            z.close()

    print('Allocating styled items...')
    setup_style_tree(
        data['Item'],
        data['Style'],
        log_item_fallbacks,
        log_missing_styles,
    )
    print(data['zips'])
    print('Done!')
    return data
示例#5
0
def load_packages(
        pak_dir,
        log_item_fallbacks=False,
        log_missing_styles=False,
        log_missing_ent_count=False,
        log_incorrect_packfile=False,
        ):
    """Scan and read in all packages in the specified directory."""
    global LOG_ENT_COUNT, CHECK_PACKFILE_CORRECTNESS
    pak_dir = os.path.abspath(os.path.join(os.getcwd(), '..', pak_dir))

    if not os.path.isdir(pak_dir):
        from tkinter import messagebox
        import sys
        # We don't have a packages directory!
        messagebox.showerror(
            master=loader,
            title='BEE2 - Invalid Packages Directory!',
            message='The given packages directory is not present!\n'
                    'Get the packages from '
                    '"http://github.com/TeamSpen210/BEE2-items" '
                    'and place them in "' + pak_dir +
                    os.path.sep + '".',
                    # Add slash to the end to indicate it's a folder.
        )
        sys.exit('No Packages Directory!')

    LOG_ENT_COUNT = log_missing_ent_count
    CHECK_PACKFILE_CORRECTNESS = log_incorrect_packfile
    zips = []
    data['zips'] = []
    try:
        find_packages(pak_dir, zips, data['zips'])

        pack_count = len(packages)
        loader.set_length("PAK", pack_count)

        for obj_type in OBJ_TYPES:
            all_obj[obj_type] = {}
            obj_override[obj_type] = defaultdict(list)
            data[obj_type] = []

        images = 0
        for pak_id, pack in packages.items():
            if not pack.enabled:
                LOGGER.info('Package {id} disabled!', id=pak_id)
                pack_count -= 1
                loader.set_length("PAK", pack_count)
                continue

            LOGGER.info('Reading objects from "{id}"...', id=pak_id)
            img_count = parse_package(pack)
            images += img_count
            loader.step("PAK")

        # If new packages were added, update the config!
        PACK_CONFIG.save_check()

        loader.set_length("OBJ", sum(
            len(obj_type)
            for obj_type in
            all_obj.values()
        ))
        loader.set_length("IMG_EX", images)

        # The number of images we need to load is the number of objects,
        # excluding some types like Stylevars or PackLists.
        loader.set_length(
            "IMG",
            sum(
                len(all_obj[key])
                for key, opts in
                OBJ_TYPES.items()
                if opts.has_img
            )
        )

        for obj_type, objs in all_obj.items():
            for obj_id, obj_data in objs.items():
                LOGGER.debug('Loading {type} "{id}"!', type=obj_type, id=obj_id)
                # parse through the object and return the resultant class
                try:
                    object_ = OBJ_TYPES[obj_type].cls.parse(
                        ParseData(
                            obj_data.zip_file,
                            obj_id,
                            obj_data.info_block,
                            obj_data.pak_id,
                        )
                    )
                except (NoKeyError, IndexError) as e:
                    reraise_keyerror(e, obj_id)

                object_.pak_id = obj_data.pak_id
                object_.pak_name = obj_data.disp_name
                for override_data in obj_override[obj_type].get(obj_id, []):
                    override = OBJ_TYPES[obj_type].cls.parse(
                        override_data
                    )
                    object_.add_over(override)
                data[obj_type].append(object_)
                loader.step("OBJ")

        cache_folder = os.path.abspath('../cache/')

        shutil.rmtree(cache_folder, ignore_errors=True)
        img_loc = os.path.join('resources', 'bee2')
        for zip_file in zips:
            for path in zip_names(zip_file):
                loc = os.path.normcase(path).casefold()
                if loc.startswith(img_loc):
                    loader.step("IMG_EX")
                    zip_file.extract(path, path=cache_folder)

        shutil.rmtree('../images/cache', ignore_errors=True)
        if os.path.isdir("../cache/resources/bee2"):
            shutil.move("../cache/resources/bee2", "../images/cache")
        shutil.rmtree('../cache/', ignore_errors=True)

    finally:
        # close them all, we've already read the contents.
        for z in zips:
            z.close()

    LOGGER.info('Allocating styled items...')
    setup_style_tree(
        data['Item'],
        data['Style'],
        log_item_fallbacks,
        log_missing_styles,
    )
    return data