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,77 +0,0 @@
import sys
# .dmm format converter, by RemieRichards
# Version 2.0
# Converts the internal structure of a .dmm file to a syntax
# that git can better handle conflicts-wise, it's also fairly human readable!
# Processes Boxstation (tgstation.2.1.3) almost instantly
def convert_map(map_file):
#CHECK FOR PREVIOUS CONVERSION
with open(map_file, "r") as conversion_candidate:
header = conversion_candidate.readline()
if header.find("//MAP CONVERTED BY dmm2tgm.py THIS HEADER COMMENT PREVENTS RECONVERSION, DO NOT REMOVE") != -1:
sys.exit()
return
#ACTUAL CONVERSION
with open(map_file, "r+") as unconverted_map:
characters = unconverted_map.read()
converted_map = ""
in_object_block = False #()
in_variable_block = False #{}
in_quote_block = False #''
in_double_quote_block = False #""
for char in characters:
if not in_quote_block: #Checking for things like "Flashbangs (Warning!)" Because we only care about ({'";, that are used as byond syntax, not strings
if not in_double_quote_block:
if not in_variable_block:
if char == "(":
in_object_block = True
char = char + "\n"
if char == ")":
in_object_block = False
if char == ",":
char = char + "\n"
if char == "{":
in_variable_block = True
if in_object_block:
char = char + "\n\t"
if char == "}":
in_variable_block = False
if in_object_block:
char = "\n\t" + char
if char == ";":
char = char + "\n\t"
if char == "\"":
if in_double_quote_block:
in_double_quote_block = False
else:
in_double_quote_block = True
if char == "'":
if not in_double_quote_block:
if in_quote_block:
in_quote_block = False
else:
in_quote_block = True
converted_map = converted_map + char
#OVERWRITE MAP FILE WITH CONVERTED MAP STRING
with open(map_file, "r+") as final_converted_map:
final_converted_map.write("//MAP CONVERTED BY dmm2tgm.py THIS HEADER COMMENT PREVENTS RECONVERSION, DO NOT REMOVE \n")
final_converted_map.write(converted_map)
sys.exit()
if sys.argv[1]: #Run like dmm2tgm.py "folder/folder/a_map.dmm"
convert_map(sys.argv[1])

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,3 @@
@echo off
set MAPROOT="../../maps/"
python dmm2tgm.py %1 %MAPROOT%

39
tools/mapmerge/dmm2tgm.py Normal file
View File

@@ -0,0 +1,39 @@
import map_helpers
import sys
import shutil
#main("../../_maps/")
def main(map_folder):
tgm = "1"
maps = map_helpers.prompt_maps(map_folder, "convert", tgm)
print("\nConverting these maps:")
for i in maps.indices:
print(str(maps.files[i])[len(map_folder):])
convert = input("\nPress Enter to convert...\n")
if convert == "abort":
print("\nAborted map convert.")
sys.exit()
else:
for i in maps.indices:
path_str = str(maps.files[i])
path_str_pretty = path_str[len(map_folder):]
error = map_helpers.merge_map(path_str, path_str, tgm)
if error > 1:
print(map_helpers.error[error])
continue
if error == 1:
print(map_helpers.error[1])
print("CONVERTED: {}".format(path_str_pretty))
print(" - ")
print("\nFinished converting.")
def string_to_num(s):
try:
return int(s)
except ValueError:
return -1
main(sys.argv[1])

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)

View File

@@ -1,59 +1,22 @@
import map_helpers
import sys
import os
import pathlib
import shutil
#main("../../_maps/")
def main(map_folder, tgm=0):
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 merge (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 = map_helpers.prompt_maps(map_folder, "merge", tgm)
print("\nMerging these maps:")
for i in valid_indices:
print(str(list_of_files[i])[len(map_folder):])
for i in maps.indices:
print(str(maps.files[i])[len(map_folder):])
merge = input("\nPress Enter to merge...\n")
if merge == "abort":
print("\nAborted map merge.")
sys.exit()
else:
for i in valid_indices:
path_str = str(list_of_files[i])
for i in maps.indices:
path_str = str(maps.files[i])
shutil.copyfile(path_str, path_str + ".before")
path_str_pretty = path_str[len(map_folder):]
try: