Esempio n. 1
0
    def save_as_RDC(self, filename):
        raw_author_name = self.metadata["author.name-short"]
        author = raw_author_name.encode(
            'utf-8') if raw_author_name else bytes()
        HEADER_STRING = b"RETRODATACONTAINER"
        VERSION = 0x01

        blocks_with_type = self.get_rdc_meta_data_block(
        ) + self.get_rdc_export_blocks()
        number_of_blocks = len(blocks_with_type)

        preample_length = len(HEADER_STRING) + 1
        block_list_length = 4 + number_of_blocks * 8
        author_field_length = len(author) + 1
        block_offset = preample_length + block_list_length + author_field_length

        with open(filename, "wb") as rdc_file:
            rdc_file.write(HEADER_STRING)
            rdc_file.write(common.as_u8(VERSION))
            rdc_file.write(common.as_u32(number_of_blocks))
            for block_type, block in blocks_with_type:
                rdc_file.write(common.as_u32(block_type))
                rdc_file.write(common.as_u32(block_offset))
                block_offset += len(block)
            rdc_file.write(author)
            rdc_file.write(common.as_u8(0))

            for _, block in blocks_with_type:
                rdc_file.write(block)

        return True  # indicate success to caller
Esempio n. 2
0
    def save_as_ZSPR(self, filename):
        # check to see if the functions exist (e.g. crashes hard if used
        #  on Samus)
        if hasattr(self, "get_binary_sprite_sheet") and \
                hasattr(self, "get_binary_palettes"):
            sprite_sheet = self.get_binary_sprite_sheet()
            palettes = self.get_binary_palettes()
            HEADER_STRING = b"ZSPR"
            VERSION = 0x01
            SPRITE_TYPE = 0x01  # this format has "1" for the player sprite
            RESERVED_BYTES = b'\x00\x00\x00\x00\x00\x00'
            QUAD_BYTE_NULL_CHAR = b'\x00\x00\x00\x00'
            DOUBLE_BYTE_NULL_CHAR = b'\x00\x00'
            SINGLE_BYTE_NULL_CHAR = b'\x00'

            write_buffer = bytearray()

            write_buffer.extend(HEADER_STRING)
            write_buffer.extend(common.as_u8(VERSION))
            checksum_start = len(write_buffer)
            write_buffer.extend(QUAD_BYTE_NULL_CHAR)
            sprite_sheet_pointer = len(write_buffer)
            write_buffer.extend(QUAD_BYTE_NULL_CHAR)
            write_buffer.extend(common.as_u16(len(sprite_sheet)))
            palettes_pointer = len(write_buffer)
            write_buffer.extend(QUAD_BYTE_NULL_CHAR)
            write_buffer.extend(common.as_u16(len(palettes)))
            write_buffer.extend(common.as_u16(SPRITE_TYPE))
            write_buffer.extend(RESERVED_BYTES)
            write_buffer.extend(
                self.metadata["sprite.name"].encode('utf-16-le'))
            write_buffer.extend(DOUBLE_BYTE_NULL_CHAR)
            write_buffer.extend(
                self.metadata["author.name"].encode('utf-16-le'))
            write_buffer.extend(DOUBLE_BYTE_NULL_CHAR)
            write_buffer.extend(
                self.metadata["author.name-short"].encode('ascii'))
            write_buffer.extend(SINGLE_BYTE_NULL_CHAR)
            write_buffer[sprite_sheet_pointer:sprite_sheet_pointer +
                         4] = common.as_u32(len(write_buffer))
            write_buffer.extend(sprite_sheet)
            write_buffer[palettes_pointer:palettes_pointer +
                         4] = common.as_u32(len(write_buffer))
            write_buffer.extend(palettes)

            checksum = (sum(write_buffer) + 0xFF + 0xFF) % 0x10000
            checksum_complement = 0xFFFF - checksum

            write_buffer[checksum_start:checksum_start +
                         2] = common.as_u16(checksum)
            write_buffer[checksum_start + 2:checksum_start +
                         4] = common.as_u16(checksum_complement)

            with open(filename, "wb") as zspr_file:
                zspr_file.write(write_buffer)

            return True  # report success to caller
        else:
            return False  # report failure to caller