Advertisement
Guest User

TUNG board tool

a guest
Jan 20th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.13 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter.filedialog import askopenfilename, asksaveasfilename
  3. import json
  4. import re
  5.  
  6. from os.path import basename
  7.  
  8. class MainFrame(Frame):
  9.     def __init__(self):
  10.         Frame.__init__(self)
  11.         self.master.title("Board Extractor")
  12.         self.master.rowconfigure(5, weight=1)
  13.         self.master.columnconfigure(5, weight=1)
  14.         self.grid(sticky=W+E+N+S, padx=5, pady=5)
  15.  
  16.         self.data = None
  17.         self.boards = []
  18.         self.current_board = None
  19.  
  20.         self.openbutton = Button(self, text="Open World", command=self.load_file, width=10)
  21.         self.openbutton.grid(row=0, column=0)
  22.         self.savebutton = Button(self, text="Save World", command=self.save_file, width=10)
  23.         self.savebutton.grid(row=0, column=1)
  24.  
  25.         self.label = Label(self, text="Boards (labels)")
  26.         self.label.grid(row=1, column=0, columnspan=2)
  27.  
  28.         listframe = Frame(self)
  29.         self.boardlist = Listbox(listframe, selectmode=SINGLE, width=50)
  30.         self.boardlist.pack(side="left", fill="y")
  31.         self.boardlist.bind("<<ListboxSelect>>", self.on_listbox_select)
  32.  
  33.         scrollbar = Scrollbar(listframe, orient="vertical")
  34.         scrollbar.config(command=self.boardlist.yview)
  35.         scrollbar.pack(side="right", fill="y")
  36.  
  37.         self.boardlist.config(yscrollcommand=scrollbar.set)
  38.         listframe.grid(row=2, column=0, columnspan=2)
  39.  
  40.         self.exportbutton = Button(self, text="Export Board", command=self.export_board, width=10, state=DISABLED)
  41.         self.exportbutton.grid(row=3, column=0)
  42.         self.importbutton = Button(self, text="Import Board", command=self.import_board, width=10, state=DISABLED)
  43.         self.importbutton.grid(row=3, column=1)
  44.  
  45.     def set_data(self, data):
  46.         self.data = data
  47.         self.boards = data["TopLevelObjects"]["value"]
  48.         self.boardlist.delete(0, END)
  49.         for board in self.boards:
  50.             labels = self.get_labels(board)
  51.             name = "Unlabeled"
  52.             if len(labels) > 0:
  53.                 name = ", ".join(labels)
  54.             self.boardlist.insert(END, name)
  55.         self.importbutton['state'] = NORMAL
  56.  
  57.     def add_board(self, board):
  58.         data = self.data
  59.         data["TopLevelObjects"]["value"].append(board)
  60.         self.set_data(data)
  61.  
  62.     def get_labels(self, board):
  63.         labels = []
  64.         for child in board["Children"]:
  65.             if child["ObjectType"] == "Panel Label":
  66.                 labels.append(child["CustomDataArray"][0]["value"])
  67.         return labels
  68.  
  69.     def on_listbox_select(self, event):
  70.         self.current_board = int(event.widget.curselection()[0])
  71.         self.exportbutton["state"] = NORMAL
  72.  
  73.     def load_file(self):
  74.         filename = askopenfilename(filetypes=[("TUNG saves", "*.tung")])
  75.         if filename:
  76.             print("Chosen file: %s" % filename)
  77.             self.master.title("%s - Board Extractor" % basename(filename))
  78.             with open(filename, "r") as infile:
  79.                 contents = infile.read()
  80.             # To anyone reading this part:
  81.             #  Yes, I am ashamed of what it does, but it works
  82.             index = contents.find("CustomDataArray")
  83.             custompart = re.sub("mscorlib\"(.*?)\\}", "mscorlib\", \"value\":\\1}", contents[index:])
  84.             contents = contents[:index] + custompart
  85.             #with open("jsonin.txt", "w") as logfile:
  86.             #    logfile.write(contents)
  87.             data = json.loads(contents)
  88.             #with open("jsonout.txt", "w") as logfile:
  89.             #    print(json.dumps(data, sort_keys=True, indent=4), file=logfile)
  90.             tlobjs = data["TopLevelObjects"]["value"]
  91.             if not isinstance(tlobjs, list):
  92.                 data["TopLevelObjects"]["value"] = [tlobjs]
  93.             self.set_data(data)
  94.         else:
  95.             print("No file chosen")
  96.  
  97.     def save_file(self):
  98.         filename = asksaveasfilename(filetypes=[("TUNG saves", "*.tung")])
  99.         if filename:
  100.             encoded = json.dumps(self.data)
  101.             # To anyone reading this part:
  102.             #  Yes, I am ashamed of what it does, but it works
  103.             index = encoded.find("CustomDataArray")
  104.             custompart = re.sub("__type\":(.*?),\\s\"value\":\\s*", "__type\":\\1", encoded[index:])
  105.             encoded = encoded[:index] + custompart
  106.             with open(filename, "w") as outfile:
  107.                 outfile.write(encoded)
  108.         else:
  109.             print("No file chosen")
  110.  
  111.     def export_board(self):
  112.         filename = asksaveasfilename(filetypes=[("TUNG Boad File", "*.btf")])
  113.         if filename:
  114.             board = self.boards[self.current_board]
  115.             contents = json.dumps(board)
  116.             with open(filename, "w") as boardfile:
  117.                 boardfile.write(contents)
  118.  
  119.     def import_board(self):
  120.         filename = askopenfilename(filetypes=[("TUNG Boad File", "*.btf")])
  121.         if filename:
  122.             with open(filename, "r") as boardfile:
  123.                 contents = boardfile.read()
  124.             board = json.loads(contents)
  125.             self.add_board(board)
  126.  
  127. def main():
  128.     MainFrame().mainloop()
  129.  
  130. if __name__ == "__main__":
  131.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement