def enabled(self): """Should this package be loaded?""" if self.id == CLEAN_PACKAGE: # The clean style package is special! # It must be present. return True return PACK_CONFIG.get_bool(self.id, 'Enabled', default=True)
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