def _find(self): """Load all objects from the object directory""" directories = glob.iglob(os.path.join(glob.escape(self._directory), "**", ""), recursive=True) for d in directories: meta_path = os.path.join(d, f"{self._object_type}.meta") if not os.path.exists(meta_path): continue with open(meta_path) as fp: meta = json.load(fp) if meta["meta_version"] != self._supported_meta_version: log.error( f'Could not enable {self._object_type} located in "{d}" due to unsupported meta version' ) continue if (meta[self._object_type][f"{self._object_type}_version"] != self._supported_version): log.error( f'Could not enable {self._object_type} "{meta[self._object_type]["id"]}" due to unsupported {self._object_type} version' ) continue spec = importlib.util.spec_from_file_location( meta[self._object_type]["entry_point"], os.path.join(d, meta[self._object_type]["entry_point"] + ".py"), ) modu = importlib.util.module_from_spec(spec) spec.loader.exec_module(modu) if not hasattr(modu, f"{self._object_type.upper()}_CLASS"): log.error( f'{self._object_type} "{meta[self._object_type]["id"]}" is missing the {self._object_type.upper()}_CLASS attribute' ) continue c = getattr(modu, f"{self._object_type.upper()}_CLASS") if self._create_instance: self._loaded[meta[f"{self._object_type}"]["id"]] = c() else: self._loaded[meta[f"{self._object_type}"]["id"]] = c log.debug( f'Enabled {self._object_type} "{meta[self._object_type]["id"]}", version {meta[self._object_type]["wrapper_version"]}' ) self._is_loaded = True
def translate_block( input_object: Block, get_block_callback: Optional[GetBlockCallback], block_location: BlockCoordinates, ) -> TranslateBlockCallbackReturn: final_block = None final_block_entity = None final_entities = [] final_extra = False for depth, block in enumerate(input_object.block_tuple): ( output_object, output_block_entity, extra, ) = version.block.from_universal( block, get_block_callback=get_block_callback, block_location=block_location, ) if isinstance(output_object, Block): if __debug__ and output_object.namespace.startswith( "universal"): log.debug( f"Error translating {input_object.full_blockstate} from universal. Got {output_object.full_blockstate}" ) if version.data_version > 0: properties = output_object.properties properties["__version__"] = amulet_nbt.TAG_Int( version.data_version) output_object = Block( output_object.namespace, output_object.base_name, properties, output_object.extra_blocks, ) if final_block is None: final_block = output_object else: final_block += output_object if depth == 0: final_block_entity = output_block_entity elif isinstance(output_object, Entity): final_entities.append(output_object) # TODO: offset entity coords final_extra |= extra return final_block, final_block_entity, final_entities, final_extra
def _load_obj(self, path: str) -> bool: if os.path.isdir(path): py_path = os.path.join(path, "__init__.py") if not os.path.isfile(py_path): return False import_path = ".".join( os.path.normpath( os.path.relpath( path, os.path.dirname(os.path.dirname( amulet.__file__)))).split(os.sep)) obj_name = os.path.basename(path) elif os.path.isfile(path): if not path.endswith(".py"): return False py_path = path import_path = ".".join( os.path.normpath( os.path.relpath( path[:-3], os.path.dirname(os.path.dirname( amulet.__file__)))).split(os.sep)) obj_name = os.path.basename(path[:-3]) else: return False with open(py_path) as f: first_line = f.readline() if first_line.strip() == f"# meta {self._object_type}": if obj_name in self._objects: log.error( f"Multiple {self._object_type} classes with the name {obj_name}" ) return False modu = importlib.import_module(import_path) if not hasattr(modu, "export"): log.error( f'{self._object_type} "{obj_name}" is missing the export attribute' ) return False c = getattr(modu, "export") if self._create_instance: self._objects[obj_name] = c() else: self._objects[obj_name] = c log.debug(f'Enabled {self._object_type} "{obj_name}"') return True return False
def translate_block( input_object: Block, get_block_callback: Optional[GetBlockCallback], block_location: BlockCoordinates, ) -> TranslateBlockCallbackReturn: final_block = None final_block_entity = None final_entities = [] final_extra = False for depth, block in enumerate(input_object.block_tuple): if "__version__" in block.properties: game_version_: int = block.properties["__version__"].value else: if "block_data" in block.properties: # if block_data is in properties cap out at 1.12.x game_version_: VersionNumberTuple = min( game_version, (1, 12, 999)) else: game_version_: VersionNumberTuple = game_version version_key = self._translator_key(game_version_) if version_key not in versions: versions[version_key] = translation_manager.get_version( *version_key).block.to_universal output_object, output_block_entity, extra = versions[ version_key]( block, get_block_callback=get_block_callback, block_location=block_location, ) if isinstance(output_object, Block): if not output_object.namespace.startswith("universal"): log.debug( f"Error translating {block.full_blockstate} to universal. Got {output_object.full_blockstate}" ) if final_block is None: final_block = output_object else: final_block += output_object if depth == 0: final_block_entity = output_block_entity elif isinstance(output_object, Entity): final_entities.append(output_object) # TODO: offset entity coords final_extra |= extra return final_block, final_block_entity, final_entities, final_extra
def _load_obj(self, module_name: str): modu = importlib.import_module(module_name) if hasattr(modu, "export"): c = getattr(modu, "export") if issubclass(c, self._base_class): if self._create_instance: self._objects[module_name] = c() else: self._objects[module_name] = c else: log.error( f"export for {module_name} must be a subclass of {self._base_class}" ) log.debug(f'Enabled {self._object_type} "{module_name}"')
def translate_block( input_object: Block, get_block_callback: Optional[GetBlockCallback], block_location: BlockCoordinates, ) -> TranslateBlockCallbackReturn: final_block = None final_block_entity = None final_entities = [] final_extra = False for depth, block in enumerate(input_object.block_tuple): ( output_object, output_block_entity, extra, ) = version.block.to_universal( block, get_block_callback=get_block_callback, block_location=block_location, ) if isinstance(output_object, Block): if not output_object.namespace.startswith("universal"): log.debug( f"Error translating {input_object.blockstate} to universal. Got {output_object.blockstate}" ) if final_block is None: final_block = output_object else: final_block += output_object if depth == 0: final_block_entity = output_block_entity elif isinstance(output_object, Entity): final_entities.append(output_object) # TODO: offset entity coords final_extra |= extra return final_block, final_block_entity, final_entities, final_extra