diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-06-13 23:29:09 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-06-13 23:29:09 +0100 |
commit | d6cff5d9a6056a4cd727f3e9dd6301a78169d246 (patch) | |
tree | d2d7f5d9718c16401fb88bfd6835b82f03315e6e | |
parent | 01161f861d9a40bfb25d9715f79e991eff33bcc8 (diff) | |
download | gold-mine-run-d6cff5d9a6056a4cd727f3e9dd6301a78169d246.tar.gz gold-mine-run-d6cff5d9a6056a4cd727f3e9dd6301a78169d246.zip |
Convert map into an .o file
-rwxr-xr-x | tools/map.py | 77 |
1 files changed, 77 insertions, 0 deletions
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 <jjm@usebox.net>", + ) + + 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() |