2021-08-20 16:35:17 +02:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
from sys import argv
|
2021-09-01 08:09:28 +02:00
|
|
|
import xmltodict
|
2021-08-20 16:35:17 +02:00
|
|
|
|
|
|
|
|
2021-11-01 10:36:20 +01:00
|
|
|
def convert_to_string(filename, doors, misc):
|
2021-09-01 08:09:28 +02:00
|
|
|
output_filename, extension = filename.split('.')
|
2021-08-15 17:27:58 +02:00
|
|
|
char_list = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ "
|
|
|
|
|
2021-09-01 08:09:28 +02:00
|
|
|
with open(filename, "r") as file:
|
|
|
|
data = file.read()
|
2021-08-15 17:27:58 +02:00
|
|
|
|
2021-09-01 08:09:28 +02:00
|
|
|
if extension == "tmx":
|
|
|
|
data = xmltodict.parse(data)["map"]["layer"]["data"]["#text"].splitlines()
|
|
|
|
data = [[int(char_id) - 1 for char_id in line.split(",") if char_id] for line in data]
|
|
|
|
elif extension == "csv":
|
|
|
|
data = data.splitlines()
|
|
|
|
data = [[int(char_id) for char_id in line.split(",") if char_id] for line in data]
|
|
|
|
|
2021-10-03 15:22:16 +02:00
|
|
|
doors_coords = []
|
2021-11-01 10:36:20 +01:00
|
|
|
misc_coords = []
|
2021-08-15 17:27:58 +02:00
|
|
|
output = r""
|
2021-10-03 15:22:16 +02:00
|
|
|
for line_index, line in enumerate(data):
|
|
|
|
for char_index, char_id in enumerate(line):
|
2021-08-15 17:27:58 +02:00
|
|
|
output += char_list[char_id]
|
2021-11-01 10:36:20 +01:00
|
|
|
|
2021-10-03 15:22:16 +02:00
|
|
|
if char_list[char_id] in doors:
|
2021-11-02 08:51:14 +01:00
|
|
|
doors_coords.append(f"\t({char_index}, {line_index}, , 0, 0),")
|
2021-11-01 10:36:20 +01:00
|
|
|
|
|
|
|
if char_list[char_id] in misc:
|
2021-11-02 08:51:14 +01:00
|
|
|
misc_coords.append(f"# {char_list[char_id]} : ({char_index}, {line_index})")
|
2021-11-01 10:36:20 +01:00
|
|
|
|
2021-08-15 17:27:58 +02:00
|
|
|
output += "\n"
|
|
|
|
|
2021-10-03 15:22:16 +02:00
|
|
|
doors_coords = "\n".join(doors_coords)
|
2021-11-01 10:36:20 +01:00
|
|
|
misc_coords = "\n".join(misc_coords)
|
2021-10-03 15:22:16 +02:00
|
|
|
|
2021-08-22 13:57:49 +02:00
|
|
|
with open(f"{output_filename}.py", "w") as file:
|
2021-11-01 10:36:20 +01:00
|
|
|
file.write(f"{output_filename} = (r\"\"\"\n{output[:-1]}\"\"\",\n{doors_coords}\n)\n\n{misc_coords}")
|
|
|
|
|
|
|
|
|
|
|
|
filename, doors, misc = argv[1], "", ""
|
|
|
|
for arg in argv[2:]:
|
|
|
|
if arg.startswith("door"):
|
|
|
|
doors = arg.split("=", 1)[1]
|
|
|
|
elif arg.startswith("misc"):
|
|
|
|
misc = arg.split("=", 1)[1]
|
2021-08-20 16:35:17 +02:00
|
|
|
|
2021-11-01 10:36:20 +01:00
|
|
|
convert_to_string(filename, doors, misc)
|