def test_packing(self):
        m1 = maps.Map().from_buffer(structure_tools.Buffer(TestEqual.raw1))
        m1.unpack()
        m2 = maps.Map().from_buffer(structure_tools.Buffer(TestEqual.raw1))
        m2.unpack()

        m2.pack()
        gen = m1.yield_inequalities(m2)

        for ineq in gen:
            self.fail("not equal: {}".format(ineq))
    def setUpClass(cls) -> None:
        map_file = str(pathlib.Path("resources/map/crusader/MxM_unseen_1.map"))
        with open(map_file, 'rb') as f:
            buf = structure_tools.Buffer(f.read())

        m = maps.Map().from_buffer(buf)
        m.directory.unpack(False)

        cls.map = m
    def setUpClass(cls) -> None:
        with open("resources/map/crusader/MxM_unseen_1.map", 'rb') as f:
            cls.raw1 = f.read()
            buf = structure_tools.Buffer(cls.raw1)

        m = maps.Map().from_buffer(buf)
        m.unpack()
        m.directory.unpack()

        buf2 = structure_tools.Buffer()
        m.serialize_to_buffer(buf2)
        cls.m = m
        cls.buf2 = buf2
def _test_parse_map(path):
    error = None
    ret = -1

    try:

        with open(path, 'rb') as f:
            data = f.read()

        buf = structure_tools.Buffer(data)
        m = maps.Map().from_buffer(buf)

        m.unpack()
        m.directory.unpack()

        ret = buf.remaining()

    except Exception as e:
        error = e
        ret = -1

    return ret, error
    dest = args.dest
    if not args.dest:
        dest = os.getcwd()

    if args.unpack:

        for file in args.files:
            with open(file, 'rb') as f:
                buf = structure_tools.Buffer(f.read())

            name = os.path.basename(file)

            while name.endswith(".map"):
                name = name[:-4]

            map = maps.Map().from_buffer(buf)

            map.dump_to_folder(os.path.join(dest, name))

    if args.pack:
        for file in args.files:
            while file.endswith('/') or file.endswith('\\'):
                file = file[:-1]

            map = maps.Map().load_from_folder(file)

            name = os.path.basename(file)

            buf = structure_tools.Buffer()

            map.serialize_to_buffer(buf)
import argparse
import PIL

parser = argparse.ArgumentParser(
    description="Extract, replace map preview image")
parser.add_argument('command', help='either extract, or replace')
parser.add_argument('input', help='input .map file')
parser.add_argument("--replacement", help="path to a replacement .png file")
parser.add_argument("output",
                    help="file to write the new map or extracted png image to")

if __name__ == "__main__":
    args = parser.parse_args()

    if args.command == 'extract':
        map_file = maps.Map().from_file(args.input)
        map_file.unpack()

        image_file = args.output

        map_file.preview.get_image().save(image_file, format='png')

    elif args.command == 'replace':
        map_file = maps.Map().from_file(args.input)
        map_file.unpack()

        image_file = args.input + ".png"
        if args.replacement:
            image_file = args.replacement

        map_file.preview.set_image(PIL.Image.open(image_file))