Esempio n. 1
0
 def read_file_obj(file_obj):
     first_line = file_obj.readline()
     items = first_line.split()
     x, y = [int(a) for a in items[:2]]
     if len(items) == 3:
         color_type = items[2]
     else:
         color_type = RGB
     s = Sketch(Point(x, y), color_type)
     for line in file_obj:
         items = string_to_ints(line)
         if color_type == RGB:
             t = Triangle(items[:6], tuple(items[6:9]), items[9])
         else:
             t = Triangle(items[:6], items[6], items[7])
         s.triangles.append(t)
     return s
Esempio n. 2
0
    def __init__(self, teemap, map_path):
        path, filename = os.path.split(map_path)
        name, extension = os.path.splitext(filename)
        if extension == '':
            map_path = os.extsep.join([map_path, 'map'])
        elif extension != ''.join([os.extsep, 'map']):
            raise ValueError('Invalid fileextension')
        teemap.validate()
        items_ = []
        datas = []
        # add version item
        items_.append(DataFileWriter.DataFileItem(ITEM_VERSION, 0, pack('i', 1)))
        # save map info
        if teemap.info:
            num = 5*[-1]
            for i, type_ in enumerate(['author', 'map_version', 'credits', 'license']):
                item_data = getattr(teemap.info, type_)
                if item_data:
                    num[i] = len(datas)
                    item_data += '\x00' # 0 termination
                    datas.append(DataFileWriter.DataFileData(item_data))
            if teemap.info.settings:
                num[4] = len(datas)
                settings_str = ''
                for setting in teemap.info.settings:
                    settings_str += '{0}\x00'.format(setting)
                datas.append(DataFileWriter.DataFileData(settings_str))
            items_.append(DataFileWriter.DataFileItem(ITEM_INFO, 0,
                              pack('6i', 1, *num)))
        # save images
        for i, image in enumerate(teemap.images):
            image_name = len(datas)
            name_str = '{0}\x00'.format(image.name)
            datas.append(DataFileWriter.DataFileData(name_str))
            image_data = -1
            if image.external is False and image.data:
                image_data = len(datas)
                datas.append(DataFileWriter.DataFileData(image.data))
            items_.append(DataFileWriter.DataFileItem(ITEM_IMAGE, i,
                              pack('6i', 1, image.width, image.height,
                              image.external, image_name, image_data)))
        # save layers and groups
        layer_count = 0
        for i, group in enumerate(teemap.groups):
            start_layer = layer_count
            for layer in group.layers:
                if layer.type == 'tilelayer':
                    tile_data = -1
                    tele_tile_data = -1
                    speedup_tile_data = -1
                    tiles_str = ''
                    name = string_to_ints(layer.name or 'Tiles', 3)
                    if layer.is_telelayer:
                        tile_data = len(datas)
                        datas.append(DataFileWriter.DataFileData(len(layer.tele_tiles.tiles)*'\x00\x00\x00\x00'))
                        for tile in layer.tele_tiles.tiles:
                            tiles_str += tile
                        tele_tile_data = len(datas)
                        datas.append(DataFileWriter.DataFileData(tiles_str))
                        name = string_to_ints('Tele', 3)
                    elif layer.is_speeduplayer:
                        tile_data = len(datas)
                        datas.append(DataFileWriter.DataFileData(len(layer.speedup_tiles.tiles)*'\x00\x00\x00\x00'))
                        for tile in layer.speedup_tiles.tiles:
                            tiles_str += tile
                        speedup_tile_data = len(datas)
                        datas.append(DataFileWriter.DataFileData(tiles_str))
                        name = string_to_ints('Speedup', 3)
                    else:
                        for tile in layer.tiles.tiles:
                            tiles_str += tile
                        tile_data = len(datas)
                        datas.append(DataFileWriter.DataFileData(tiles_str))
                        if layer.is_gamelayer:
                            name = string_to_ints('Game', 3)
                    if teemap.telelayer or teemap.speeduplayer:
                        items_.append(DataFileWriter.DataFileItem(ITEM_LAYER, layer_count,
                               pack('20i', 0, LAYERTYPE_TILES, layer.detail, 3, layer.width,
                               layer.height, layer.game, layer.color[0], layer.color[1],
                               layer.color[2], layer.color[3], layer.color_env,
                               layer.color_env_offset, layer.image_id, tile_data, name[0],
                               name[1], name[2], tele_tile_data, speedup_tile_data)))
                    else:
                        items_.append(DataFileWriter.DataFileItem(ITEM_LAYER, layer_count,
                               pack('18i', 0, LAYERTYPE_TILES, layer.detail, 3, layer.width,
                               layer.height, layer.game, layer.color[0], layer.color[1],
                               layer.color[2], layer.color[3], layer.color_env,
                               layer.color_env_offset, layer.image_id, tile_data, *name)))
                    layer_count += 1
                elif layer.type == 'quadlayer':
                    if len(layer.quads.quads):
                        quads_str = ''
                        for quad in layer.quads.quads:
                            quads_str += quad
                        quad_data = len(datas)
                        datas.append(DataFileWriter.DataFileData(quads_str))
                        name = string_to_ints(layer.name, 3)
                        items_.append(DataFileWriter.DataFileItem(ITEM_LAYER, layer_count,
                               pack('10i', 7, LAYERTYPE_QUADS, layer.detail, 2,
                               len(layer.quads.quads), quad_data, layer.image_id, *name)))
                        layer_count += 1
            name = string_to_ints('Game' if group.is_gamegroup else group.name, 3)
            items_.append(DataFileWriter.DataFileItem(ITEM_GROUP, i,
                   pack('15i', 3, group.offset_x, group.offset_y, group.parallax_x,
                   group.parallax_y, start_layer, len(group.layers),
                   group.use_clipping, group.clip_x, group.clip_y, group.clip_w,
                   group.clip_h, *name)))

        # save envelopes
        start_point = 0
        for i, envelope in enumerate(teemap.envelopes):
            num_points = len(envelope.envpoints)
            name = string_to_ints(envelope.name)
            synced = 1 if envelope.synced else 0
            fmt = '{0}i'.format(items.Envelope.type_size)
            items_.append(DataFileWriter.DataFileItem(ITEM_ENVELOPE, i,
                   pack(fmt, 1, envelope.channels, start_point, num_points,
                   name[0], name[1], name[2], name[3], name[4], name[5], name[6], name[7],
                   synced)))
            start_point += num_points

        # save points
        envpoints = []
        for envpoint in teemap.envpoints:
            values = 4*[0]
            for i, value in enumerate(envpoint.values):
                values[i] = value
            envpoints.extend([envpoint.time, envpoint.curvetype, values[0],
                              values[1], values[2], values[3]])
        fmt = '{0}i'.format(len(envpoints))
        items_.append(DataFileWriter.DataFileItem(ITEM_ENVPOINT, 0,
               pack(fmt, *envpoints)))
        items_.sort()

        # calculate header
        item_size = 0
        num_item_types = 1
        item_types = []
        for i in range(len(items_)):
            item_size += items_[i].size
            if i > 0 and items_[i].type != items_[i-1].type:
                num_item_types += 1
        for i in range(7):
            num = 0
            start = -1
            for j, item in enumerate(items_):
                if item.type == i:
                    num += 1
                    if start < 0:
                        start = j
            if start > -1:
                item_types.extend([i, start, num])
        data_size = 0
        for data in datas:
            data_size += data.compressed_size
        item_types_size = num_item_types*12
        header_size = 36
        offset_size = (len(items_) + 2*len(datas)) * 4
        file_size = header_size + item_types_size + offset_size + item_size + data_size - 16
        swaplen = file_size - data_size

        # write file
        with open(map_path, 'wb') as f:
            f.write('DATA') # file signature
            header_str = pack('8i', 4, file_size, swaplen, num_item_types,
                          len(items_), len(datas), item_size, data_size)
            f.write(header_str)
            fmt = '{0}i'.format(len(item_types))
            item_types_str = pack(fmt, *item_types)
            f.write(item_types_str)
            item_offset_str = ''
            offset = 0
            for item in items_:
                item_offset_str += pack('i', offset)
                offset += item.size
            f.write(item_offset_str)
            data_offset_str = ''
            offset = 0
            for data in datas:
                data_offset_str += pack('i', offset)
                offset += data.compressed_size
            f.write(data_offset_str)
            for data in datas:
                f.write(pack('i', data.uncompressed_size))
            for item in items_:
                f.write(item.data)
            for data in datas:
                f.write(data.data)
Esempio n. 3
0
    def __init__(self, teemap, map_path):
        path, filename = os.path.split(map_path)
        name, extension = os.path.splitext(filename)
        if extension == '':
            map_path = os.extsep.join([map_path, 'map'])
        elif extension != ''.join([os.extsep, 'map']):
            raise ValueError('Invalid fileextension')
        teemap.validate()
        items_ = []
        datas = []
        # add version item
        items_.append(DataFileWriter.DataFileItem(ITEM_VERSION, 0, pack('i', 1)))
        # save map info
        if teemap.info:
            num = 5*[-1]
            for i, type_ in enumerate(['author', 'map_version', 'credits', 'license']):
                item_data = getattr(teemap.info, type_)
                if item_data:
                    num[i] = len(datas)
                    item_data += '\x00' # 0 termination
                    datas.append(DataFileWriter.DataFileData(item_data))
            items_.append(DataFileWriter.DataFileItem(ITEM_INFO, 0,
                              pack('6i', 1, *num)))
        # save images
        for i, image in enumerate(teemap.images):
            image_name = len(datas)
            name_str = '{0}\x00'.format(image.name)
            datas.append(DataFileWriter.DataFileData(name_str))
            image_data = -1
            if image.external is False and image.data:
                image_data = len(datas)
                datas.append(DataFileWriter.DataFileData(image.data))
            items_.append(DataFileWriter.DataFileItem(ITEM_IMAGE, i,
                              pack('6i', 1, image.width, image.height,
                              image.external, image_name, image_data)))
        # save layers and groups
        layer_count = 0
        for i, group in enumerate(teemap.groups):
            start_layer = layer_count
            for layer in group.layers:
                if layer.type == 'tilelayer':
                    tile_data = -1
                    tele_tile_data = -1
                    speedup_tile_data = -1
                    tiles_str = ''
                    name = string_to_ints(layer.name or 'Tiles', 3)
                    if layer.is_telelayer:
                        tile_data = len(datas)
                        datas.append(DataFileWriter.DataFileData(len(layer.tele_tiles.tiles)*'\x00\x00\x00\x00'))
                        for tile in layer.tele_tiles.tiles:
                            tiles_str += tile
                        tele_tile_data = len(datas)
                        datas.append(DataFileWriter.DataFileData(tiles_str))
                        name = string_to_ints('Tele', 3)
                    elif layer.is_speeduplayer:
                        tile_data = len(datas)
                        datas.append(DataFileWriter.DataFileData(len(layer.speedup_tiles.tiles)*'\x00\x00\x00\x00'))
                        for tile in layer.speedup_tiles.tiles:
                            tiles_str += tile
                        speedup_tile_data = len(datas)
                        datas.append(DataFileWriter.DataFileData(tiles_str))
                        name = string_to_ints('Speedup', 3)
                    else:
                        for tile in layer.tiles.tiles:
                            tiles_str += tile
                        tile_data = len(datas)
                        datas.append(DataFileWriter.DataFileData(tiles_str))
                        if layer.is_gamelayer:
                            name = string_to_ints('Game', 3)
                    if teemap.telelayer or teemap.speeduplayer:
                        items_.append(DataFileWriter.DataFileItem(ITEM_LAYER, layer_count,
                               pack('20i', 0, LAYERTYPE_TILES, layer.detail, 3, layer.width,
                               layer.height, layer.game, layer.color[0], layer.color[1],
                               layer.color[2], layer.color[3], layer.color_env,
                               layer.color_env_offset, layer.image_id, tile_data, name[0],
                               name[1], name[2], tele_tile_data, speedup_tile_data)))
                    else:
                        items_.append(DataFileWriter.DataFileItem(ITEM_LAYER, layer_count,
                               pack('18i', 0, LAYERTYPE_TILES, layer.detail, 3, layer.width,
                               layer.height, layer.game, layer.color[0], layer.color[1],
                               layer.color[2], layer.color[3], layer.color_env,
                               layer.color_env_offset, layer.image_id, tile_data, *name)))
                    layer_count += 1
                elif layer.type == 'quadlayer':
                    if len(layer.quads.quads):
                        quads_str = ''
                        for quad in layer.quads.quads:
                            quads_str += quad
                        quad_data = len(datas)
                        datas.append(DataFileWriter.DataFileData(quads_str))
                        name = string_to_ints(layer.name, 3)
                        items_.append(DataFileWriter.DataFileItem(ITEM_LAYER, layer_count,
                               pack('10i', 7, LAYERTYPE_QUADS, layer.detail, 2,
                               len(layer.quads.quads), quad_data, layer.image_id, *name)))
                        layer_count += 1
            name = string_to_ints('Game' if group.is_gamegroup else group.name, 3)
            items_.append(DataFileWriter.DataFileItem(ITEM_GROUP, i,
                   pack('15i', 3, group.offset_x, group.offset_y, group.parallax_x,
                   group.parallax_y, start_layer, len(group.layers),
                   group.use_clipping, group.clip_x, group.clip_y, group.clip_w,
                   group.clip_h, *name)))

        # save envelopes
        start_point = 0
        for i, envelope in enumerate(teemap.envelopes):
            num_points = len(envelope.envpoints)
            name = string_to_ints(envelope.name)
            synced = 1 if envelope.synced else 0
            fmt = '{0}i'.format(items.Envelope.type_size)
            items_.append(DataFileWriter.DataFileItem(ITEM_ENVELOPE, i,
                   pack(fmt, 1, envelope.channels, start_point, num_points,
                   name[0], name[1], name[2], name[3], name[4], name[5], name[6], name[7],
                   synced)))
            start_point += num_points

        # save points
        envpoints = []
        for envpoint in teemap.envpoints:
            values = 4*[0]
            for i, value in enumerate(envpoint.values):
                values[i] = value
            envpoints.extend([envpoint.time, envpoint.curvetype, values[0],
                              values[1], values[2], values[3]])
        fmt = '{0}i'.format(len(envpoints))
        items_.append(DataFileWriter.DataFileItem(ITEM_ENVPOINT, 0,
               pack(fmt, *envpoints)))
        items_.sort()

        # calculate header
        item_size = 0
        num_item_types = 1
        item_types = []
        for i in range(len(items_)):
            item_size += items_[i].size
            if i > 0 and items_[i].type != items_[i-1].type:
                num_item_types += 1
        for i in range(7):
            num = 0
            start = -1
            for j, item in enumerate(items_):
                if item.type == i:
                    num += 1
                    if start < 0:
                        start = j
            if start > -1:
                item_types.extend([i, start, num])
        data_size = 0
        for data in datas:
            data_size += data.compressed_size
        item_types_size = num_item_types*12
        header_size = 36
        offset_size = (len(items_) + 2*len(datas)) * 4
        file_size = header_size + item_types_size + offset_size + item_size + data_size - 16
        swaplen = file_size - data_size

        # write file
        with open(map_path, 'wb') as f:
            f.write('DATA') # file signature
            header_str = pack('8i', 4, file_size, swaplen, num_item_types,
                          len(items_), len(datas), item_size, data_size)
            f.write(header_str)
            fmt = '{0}i'.format(len(item_types))
            item_types_str = pack(fmt, *item_types)
            f.write(item_types_str)
            item_offset_str = ''
            offset = 0
            for item in items_:
                item_offset_str += pack('i', offset)
                offset += item.size
            f.write(item_offset_str)
            data_offset_str = ''
            offset = 0
            for data in datas:
                data_offset_str += pack('i', offset)
                offset += data.compressed_size
            f.write(data_offset_str)
            for data in datas:
                f.write(pack('i', data.uncompressed_size))
            for item in items_:
                f.write(item.data)
            for data in datas:
                f.write(data.data)
Esempio n. 4
0
 def test_string_to_ints(self):
     test = string_to_ints('test')
     self.assertEqual(test, TEST_INT)
Esempio n. 5
0
 def test_string_to_ints(self):
     test = string_to_ints('test')
     self.assertEqual(test, TEST_INT)