Beispiel #1
0
    def _open(self):
        io = self._io
        io.seek(0)

        magic = IOHelper.read_ascii_string(io, 16)
        if magic != FILE_MAGIC:
            raise Exception('Not NPK File.')

        [count] = IOHelper.read_struct(io, 'i')

        files = []
        for i in range(count):
            offset, size = IOHelper.read_struct(io, '<2i')
            name_data = NPK._decrypt_name(io.read(256))
            try:
                name = name_data.decode('euc_kr')
                name = name[:name.find('\x00')]
            except:
                name = name_data[:name_data.find(b'\x00')].decode(
                    'euc_kr', errors='ignore')
                print('Bad Filename: ', name_data)

            file = {
                'name': name,
                'offset': offset,
                'size': size,
                'data': None,
            }
            files.append(file)

        self._files = files
Beispiel #2
0
    def load(io: FileIO):
        instance = MPK(io)
        magic = IOHelper.read_ascii_string(io, 4)
        if magic == MPK_MAGIC:
            version, count = IOHelper.read_struct(io, '<2i')
            io.seek(52, SEEK_CUR)
            instance.set_version(version)
            for i in range(count):
                is_zip, index, offset, data_size, zip_size = IOHelper.read_struct(
                    io, '<2i3q')
                name_data = io.read(224)
                name = name_data[:name_data.find(b'\x00')].decode(
                    encoding='ascii')
                instance.insert_file({
                    'is_zip': is_zip != 0,
                    'index': index,
                    'offset': offset,
                    'data_size': data_size,
                    'zip_size': zip_size,
                    'name': name,
                    'data': None,
                })

        return instance
Beispiel #3
0
    def _open(self):
        io = self._io
        io.seek(0)

        magic = IOHelper.read_ascii_string(io, 18)
        if magic == FILE_MAGIC or magic == FILE_MAGIC_OLD:
            if magic == FILE_MAGIC:
                # images_size without version,count,extra(color_board,map_images)...
                [images_size] = IOHelper.read_struct(io, 'i')
            else:
                # unknown.
                [unknown] = IOHelper.read_struct(io, 'h')
                images_size = 0

            # keep: 0
            [keep, version, img_count] = IOHelper.read_struct(io, '<3i')
            self._version = version

            if version == FILE_VERSION_4:
                # single color board.
                self._color_board = self._open_color_board()
            elif version == FILE_VERSION_5:
                # map image.
                map_count, file_size = IOHelper.read_struct(io, '<2i')
                self._color_board = self._open_color_board()
                self._map_images = self._open_map_images(map_count)
            elif version == FILE_VERSION_6:
                # multiple color board.
                color_boards = []

                [color_board_count] = IOHelper.read_struct(io, 'i')
                for i in range(color_board_count):
                    color_board = self._open_color_board()
                    color_boards.append(color_board)

                self._color_boards = color_boards

            images = self._open_images(img_count)
            self._images = images

            # count image offset.
            if version != FILE_VERSION_1:
                # behind header.
                if images_size != 0:
                    offset = images_size + 32
                else:
                    offset = io.tell()

                if version == FILE_VERSION_5:
                    map_images = self._map_images
                    for i in range(len(map_images)):
                        map_image = map_images[i]
                        map_image['offset'] = offset
                        offset += map_image['data_size']
                for i in range(len(images)):
                    image = images[i]
                    if image['format'] != IMAGE_FORMAT_LINK and image[
                            'extra'] != IMAGE_EXTRA_MAP_ZLIB:
                        image['offset'] = offset
                        offset += image['size']
        else:
            raise Exception('Not IMG File.')