Update mapmerge (#2918)

Updates mapmerge from /tg/ upstream & deletes the obsolete dmm2tgm tool, replaced with additional functionality in mapmerge.
This commit is contained in:
Lohikar
2017-07-02 12:30:50 -05:00
committed by skull132
parent 9f45ea79fc
commit 64263b5ff9
9 changed files with 97 additions and 124 deletions

View File

@@ -1,5 +1,8 @@
import sys
import subprocess
import os
import pathlib
import collections
from datetime import datetime
tgm_header = "//MAP CONVERTED BY dmm2tgm.py THIS HEADER COMMENT PREVENTS RECONVERSION, DO NOT REMOVE"
@@ -125,7 +128,7 @@ def merge_map(newfile, backupfile, tgm):
#######################
#write to file helpers#
def write_dictionary_tgm(filename, dictionary, header = None): #write dictionary in tgm format
with open(filename, "w") as output:
with open(filename, "w", newline='\n') as output:
output.write("{}\n".format(tgm_header))
if header:
output.write("{}\n".format(header))
@@ -172,7 +175,7 @@ def write_dictionary_tgm(filename, dictionary, header = None): #write dictionary
def write_grid_coord_small(filename, grid, maxx, maxy): #thanks to YotaXP for finding out about this one
with open(filename, "a") as output:
with open(filename, "a", newline='\n') as output:
output.write("\n")
for x in range(1, maxx+1):
@@ -183,7 +186,7 @@ def write_grid_coord_small(filename, grid, maxx, maxy): #thanks to YotaXP for fi
def write_dictionary(filename, dictionary, header = None): #writes a tile dictionary the same way Dreammaker does
with open(filename, "w") as output:
with open(filename, "w", newline='\n') as output:
for key, value in dictionary.items():
if header:
output.write("{}\n".format(header))
@@ -191,7 +194,7 @@ def write_dictionary(filename, dictionary, header = None): #writes a tile dictio
def write_grid(filename, grid, maxx, maxy): #writes a map grid the same way Dreammaker does
with open(filename, "a") as output:
with open(filename, "a", newline='\n') as output:
output.write("\n")
output.write("(1,1,1) = {\"\n")
@@ -727,3 +730,45 @@ def combine_tiles(tile_A, tile_B, priority, marker):
def run_shell_command(command):
return subprocess.run(command, shell=True, stdout=subprocess.PIPE, universal_newlines=True).stdout
def prompt_maps(map_folder, verb, tgm):
list_of_files = list()
for root, directories, filenames in os.walk(map_folder):
for filename in [f for f in filenames if f.endswith(".dmm")]:
list_of_files.append(pathlib.Path(root, filename))
last_dir = ""
for i in range(0, len(list_of_files)):
this_dir = list_of_files[i].parent
if last_dir != this_dir:
print("--------------------------------")
last_dir = this_dir
print("[{}]: {}".format(i, str(list_of_files[i])[len(map_folder):]))
print("--------------------------------")
in_list = input("List the maps you want to " + verb + " (example: 1,3-5,12):\n")
in_list = in_list.replace(" ", "")
in_list = in_list.split(",")
valid_indices = list()
for m in in_list:
index_range = m.split("-")
if len(index_range) == 1:
index = string_to_num(index_range[0])
if index >= 0 and index < len(list_of_files):
valid_indices.append(index)
elif len(index_range) == 2:
index0 = string_to_num(index_range[0])
index1 = string_to_num(index_range[1])
if index0 >= 0 and index0 <= index1 and index1 < len(list_of_files):
valid_indices.extend(range(index0, index1 + 1))
if tgm == "1":
print("\nMaps will be converted to tgm.")
tgm = True
else:
print("\nMaps will not be converted to tgm.")
tgm = False
maps_to_run = collections.namedtuple('maps_to_run', ['files', 'indices'])
return maps_to_run(list_of_files, valid_indices)