Exemplo n.º 1
0
    def get_resource(self, res_ref: str, res_type: Union[ResourceType, str, int],
                     check_folders: List[str] = None,
                     check_modules: List[str] = None,
                     skip_override: bool = False) -> Optional[bytes]:
        res_type = ResourceType.get(res_type)

        data = None

        if check_folders is not None:
            for folder in check_folders:
                try:
                    with open(folder + "\\" + res_ref + "." + res_type.extension, 'rb') as file:
                        data = file.read()
                except:
                    pass

        if os.path.exists(self.override_directory() + res_ref + "." + res_type.extension) and not skip_override:
            with open(self.override_directory() + res_ref + "." + res_type.extension, 'rb') as file:
                data = file.read()

        if check_modules is not None:
            for module_path in check_modules:
                cap = Encapsulator.new(module_path)
                if cap.has_resource(res_ref, res_type):
                    data = cap.load(res_ref, res_type)

        if self.chitin.has_resource(res_ref, res_type):
            data = self.chitin.load(res_ref, res_type)

        return data
Exemplo n.º 2
0
    def load_resources(path: str) -> List[Resource]:
        reader = BinaryReader.from_path(path)
        resources = []

        file_type = reader.read_string(4)
        file_version = reader.read_string(4)
        reader.seek(16)
        resource_count = reader.read_uint32()
        reader.seek(24)
        key_list_offset = reader.read_uint32()
        resource_list_offset = reader.read_uint32()

        for i in range(resource_count):
            reader.seek(key_list_offset + 24 * i)
            res_ref = reader.read_string(16)
            reader.skip(4)
            res_type = ResourceType.get(reader.read_uint16())
            reader.skip(2)

            reader.seek(resource_list_offset + 8 * i)
            res_offset = reader.read_uint32()
            res_size = reader.read_uint32()

            resources.append(Resource(res_ref, res_type, res_size, res_offset))

        return resources
Exemplo n.º 3
0
    def _handle_search(self, request: ResourceRequest) -> Optional[str]:
        path = None
        file_name = request.res_ref + "." + ResourceType.get(request.res_type).extension

        for folder_path in self.check_folders:
            if path is None and os.path.exists(folder_path + file_name):
                path = folder_path + file_name

        if path is None and self._resource_manager is not None:
            if os.path.exists(self._resource_manager.override_directory() + file_name):
                path = self._resource_manager.override_directory() + file_name

        for module_path in self.check_modules:
            if path is None and os.path.exists(module_path):
                try:
                    self._open_handle(module_path)
                    if self._encapsulators[module_path].has_resource(request.res_ref, request.res_type):
                        path = module_path
                except:
                    pass

        if path is None and self._chitin is not None:
            path = self._chitin.resource_path(request.res_ref, request.res_type)

        return path
Exemplo n.º 4
0
    def load(data: bytes) -> ERF:
        reader = BinaryReader.from_data(data)
        erf = ERF()

        file_type = reader.read_string(4)
        file_version = reader.read_string(4)
        reader.seek(16)
        resource_count = reader.read_uint32()
        reader.seek(24)
        key_list_offset = reader.read_uint32()
        resource_list_offset = reader.read_uint32()

        for i in range(resource_count):
            reader.seek(key_list_offset + 24 * i)
            res_ref = reader.read_string(16)
            reader.skip(4)
            res_type = ResourceType.get(reader.read_uint16())
            reader.skip(2)

            reader.seek(resource_list_offset + 8 * i)
            res_offset = reader.read_uint32()
            res_size = reader.read_uint32()

            reader.seek(res_offset)
            res_data = reader.read_bytes(res_size)
            erf.add(Resource.new(res_ref, res_type, res_data))

        return erf
Exemplo n.º 5
0
 def new(res_ref: str, res_type: Union[str, int, ResourceType],
         res_data: bytearray) -> Resource:
     resource = Resource()
     resource.res_ref = res_ref
     resource.res_type = ResourceType.get(res_type)
     resource.res_data = res_data
     return resource
Exemplo n.º 6
0
    def load_resources(directory: str) -> Dict[int, Resource]:
        reader = BinaryReader.from_path(directory + "/chitin.key")
        resources: Dict[int, Resource] = dict()

        file_type = reader.read_string(4)
        file_version = reader.read_string(4)

        bif_count = reader.read_uint32()
        key_count = reader.read_uint32()

        bif_names_offset = reader.read_uint32()
        keys_offset = reader.read_uint32()

        bif_paths = []
        for i in range(bif_count):
            reader.seek(bif_names_offset + i * 12)
            bif_size = reader.read_uint32()
            filename_offset = reader.read_uint32()
            filename_size = reader.read_uint16()
            drive = reader.read_uint16()

            reader.seek(filename_offset)
            bif_path = (directory + '/' +
                        reader.read_string(filename_size)).replace('\\', '/')
            bif_paths.append(bif_path)

        reader.seek(keys_offset)
        for i in range(key_count):
            res_ref = reader.read_string(16)
            res_type = ResourceType.get(reader.read_uint16())
            res_id = reader.read_uint32()
            resources[res_id] = Resource(res_ref, res_type)

        for bif in bif_paths:
            try:
                _BIFReader.load_resources(bif, resources)
            finally:
                pass

        return resources
Exemplo n.º 7
0
    def load(data: bytes) -> RIM:
        reader = BinaryReader.from_data(data)
        rim = RIM()

        file_type = reader.read_string(4)
        file_version = reader.read_string(4)
        reader.skip(4)
        resource_count = reader.read_uint32()
        table_offset = reader.read_uint32()
        reader.skip(100)

        for i in range(resource_count):
            reader.seek(table_offset + 32 * i)
            res_ref = reader.read_string(16)
            res_type = ResourceType.get(reader.read_uint32())
            reader.skip(4)
            res_offset = reader.read_uint32()
            res_size = reader.read_uint32()
            reader.seek(res_offset)
            res_data = reader.read_bytes(res_size)
            rim.add(Resource.new(res_ref, res_type, res_data))

        return rim