From d6cff5d9a6056a4cd727f3e9dd6301a78169d246 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 13 Jun 2023 23:29:09 +0100 Subject: Convert map into an .o file --- tools/map.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100755 tools/map.py (limited to 'tools') diff --git a/tools/map.py b/tools/map.py new file mode 100755 index 0000000..2f3479a --- /dev/null +++ b/tools/map.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 + +from argparse import ArgumentParser +import subprocess +import os +import json + +__version__ = "1.0" + +ld = os.environ.get("LD", "i586-pc-msdosdjgpp-ld") +strip = os.environ.get("STRIP", "i586-pc-msdosdjgpp-strip") + + +def get_layer(data, name): + for layer in data["layers"]: + if layer["name"] == name: + return layer + raise ValueError("Layer %s not found" % name) + + +def main(): + parser = ArgumentParser( + description="Tiled JSON Map to .o", + epilog="Copyright (C) 2023 Juan J Martinez ", + ) + + parser.add_argument( + "--version", action="version", version="%(prog)s " + __version__ + ) + parser.add_argument("file_json", help="JSON map to convert") + parser.add_argument("output", help="object name for the map data") + + args = parser.parse_args() + + with open(args.file_json, "rt") as fd: + data = json.load(fd) + + if len(data["tilesets"]) != 1: + parser.error("Unsupported number of tilesets %d" % len(data["tilesets"])) + + tileset = data["tilesets"][0] + + map_layer = get_layer(data, "Map") + + out = map(lambda x: x - tileset["firstgid"], map_layer["data"]) + + # TODO: process map entities + + tmp = args.output.rstrip(".o") + with open(tmp, "wb") as fd: + fd.write(bytearray(out)) + fd.flush() + + # create an object file from the binary + rc = subprocess.call( + [ + ld, + "-r", + "-b", + "binary", + "-o", + args.output, + tmp, + ] + ) + os.unlink(tmp) + if rc != 0: + parser.error("Failed to run %s" % ld) + + # strip unwanted symbols + rc = subprocess.call([strip, "-w", "-K", "*_%s_*" % tmp, args.output]) + if rc != 0: + parser.error("Failed to run %s" % ld) + + +if __name__ == "__main__": + main() -- cgit v1.2.3