Created
February 2, 2018 16:56
-
-
Save PJB3005/60ca8097e0cd4f986209a90a008fe156 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import re | |
# Maybe unhardcode this. | |
KEY_SIZE = 3 | |
MAP_LOC_HEADER = re.compile(r"\(1,1,[0-9]\) = {\"") | |
DEF_RE = re.compile(rf"\"([a-zA-Z]{{" + str(KEY_SIZE) + r"})\" = \((.*)\)") | |
print(DEF_RE) | |
def main(filename): | |
# Not using with to avoid indentation. | |
f = open(filename, "r") | |
# Get defs. | |
next_index = 0 | |
defs_indices = {} | |
defs = [] | |
while True: | |
line = f.readline() | |
match = DEF_RE.match(line) | |
if not match: | |
break | |
defs_indices[match.group(1)] = next_index | |
defs.append(match.group(2)) | |
next_index += 1 | |
# Next line read will be (1,1,1) = {" | |
maps = [] | |
while MAP_LOC_HEADER.match(f.readline()): | |
currentmap = [] | |
while True: | |
line = f.readline() | |
#print(repr(line)) | |
if line[:-1] == "\"}": | |
f.readline() | |
break | |
sections = [line[i:i+KEY_SIZE] for i in range(0, len(line)-1, KEY_SIZE)] # -1 for newline cut. | |
sections_indexed = [defs_indices[x] for x in sections] | |
currentmap.append(sections_indexed) | |
maps.append(currentmap) | |
f.close() | |
key_index = 0 | |
# Got everything now to manually re-create the file. | |
# Not using with to avoid indentation. | |
f = open(filename, "w") | |
def_keys = [] | |
for definition in defs: | |
key = get_key(key_index) | |
key_index += 1 | |
def_keys.append(key) | |
f.write(f"\"{key}\" = ({definition})\n") | |
for i, mapdata in enumerate(maps): | |
f.write(f"\n(1,1,{i+1}) = {{\"\n") | |
for row in mapdata: | |
for tile in row: | |
f.write(def_keys[tile]) | |
f.write("\n") | |
f.write("\"}\n") | |
f.close() | |
def get_key(key: int) -> str: | |
out = [] | |
for x in range(KEY_SIZE, 0, -1): | |
val = (key // (52 ** (x-1))) % 52 | |
if val <= 25: | |
val += 97 # ord("a") | |
else: | |
val += 39 # ord("A") - 25 | |
out.append(chr(val)) | |
return "".join(out) | |
if __name__ == '__main__': | |
for filename in sys.argv[1:]: | |
main(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment