44import tkinter .filedialog
55import tkinter .messagebox
66import tkinter .ttk
7- from modules .pyBjson import convertBjsonToJson
8- import json
7+ from modules .pyBjson import BJSONFile
98import threading
109from functools import partial
1110import sys , os , argparse
1514
1615def getBjsonContent (fp : str | Path ):
1716 try :
18- data = convertBjsonToJson ( fp )
17+ data = BJSONFile (). open ( fp ). toPython ( )
1918 except Exception as e :
2019 tkinter .messagebox .showerror ("Unable to load file" , f"Could not open the specified file. Error: { e } " )
2120 return data
2221
23- def addDictToTree (tree : ttk .Treeview , root : str , key : str , data : dict , count : int ):
22+ def addDictToTree (tree : ttk .Treeview , root : str , key : str , data : dict ):
2423 if root == "" :
2524 opened = True
2625 else :
2726 opened = False
28- item_idx = count
29- sub_node = tree .insert (root , "end" , text = key , open = opened , values = ["Object" , item_idx ])
30- count += 1
27+ sub_node = tree .insert (root , "end" , text = key , open = opened , values = ["Object" ])
3128 for key in data :
3229 if type (data [key ]) == dict :
33- count = addDictToTree (tree , sub_node , key , data [key ], count )
30+ addDictToTree (tree , sub_node , key , data [key ])
3431 elif type (data [key ]) == list :
35- count = addListToTree (tree , sub_node , key , data [key ], count )
32+ addListToTree (tree , sub_node , key , data [key ])
3633 else :
37- addSingleElementToTree (tree , sub_node , key , data [key ], count )
38- count += 1
39-
40- return count
34+ addSingleElementToTree (tree , sub_node , key , data [key ])
4135
42- def addListToTree (tree : ttk .Treeview , root : str , key : str , data : list , count : int ):
36+ def addListToTree (tree : ttk .Treeview , root : str , key : str , data : list ):
4337 if root == "" :
4438 opened = True
4539 else :
4640 opened = False
47- item_idx = count
48- sub_node = tree .insert (root , "end" , text = key , open = opened , values = ["Array" , item_idx ])
49- count += 1
41+ sub_node = tree .insert (root , "end" , text = key , open = opened , values = ["Array" ])
5042 for element in data :
5143 if type (element ) == dict :
52- count = addDictToTree (tree , sub_node , "Object" , element , count )
44+ addDictToTree (tree , sub_node , "Object" , element )
5345 elif type (element ) == list :
54- count = addListToTree (tree , sub_node , "Array" , element , count )
46+ addListToTree (tree , sub_node , "Array" , element )
5547 elif type (element ) == int or type (element ) == float :
56- addSingleElementToTree (tree , sub_node , "Number" , element , count )
57- count += 1
48+ addSingleElementToTree (tree , sub_node , "Number" , element )
5849 elif type (element ) == str :
59- addSingleElementToTree (tree , sub_node , "String" , element , count )
60- count += 1
50+ addSingleElementToTree (tree , sub_node , "String" , element )
6151 elif type (element ) == bool :
62- addSingleElementToTree (tree , sub_node , "Boolean" , element , count )
63- count += 1
52+ addSingleElementToTree (tree , sub_node , "Boolean" , element )
6453 else :
65- addSingleElementToTree (tree , sub_node , "null" , None , count )
66- count += 1
67-
68- return count
54+ addSingleElementToTree (tree , sub_node , "null" , None )
6955
70- def addSingleElementToTree (tree : ttk .Treeview , root : str , key : str , data , count : int ):
71- item_idx = count
56+ def addSingleElementToTree (tree : ttk .Treeview , root : str , key : str , data ):
7257 if type (data ) == int or type (data ) == float :
73- tree .insert (root , "end" , text = key , values = ["Number" , item_idx , data ])
58+ tree .insert (root , "end" , text = key , values = ["Number" , data ])
7459 elif type (data ) == str :
75- tree .insert (root , "end" , text = key , values = ["String" , item_idx , data ])
60+ tree .insert (root , "end" , text = key , values = ["String" , data ])
7661 elif type (data ) == bool :
77- tree .insert (root , "end" , text = key , values = ["Boolean" , item_idx , data ])
62+ tree .insert (root , "end" , text = key , values = ["Boolean" , data ])
7863 else :
79- tree .insert (root , "end" , text = key , values = ["null" , item_idx , "null" ])
64+ tree .insert (root , "end" , text = key , values = ["null" , "null" ])
8065
8166def populate_tree (tree : ttk .Treeview , data : dict | list ):
82- count = 0
8367 if type (data ) == dict :
84- count = addDictToTree (tree , "" , "root" , data , count )
68+ addDictToTree (tree , "" , "root" , data )
8569 elif type (data ) == list :
86- count = addListToTree (tree , "" , "root" , data , count )
70+ addListToTree (tree , "" , "root" , data )
8771
8872def loadFileDataFromBjson (root , tree : ttk .Treeview , fp : str | Path ):
8973 loading_label = tkinter .Label (root , text = "Loading file..." )
9074 loading_label .grid (row = 0 , column = 0 )
9175 try :
92- json_str = getBjsonContent (fp )
93- if json_str == None :
94- tkinter .messagebox .showerror ("Unable to load file" , "The file could not be loaded. The format may be incorrect or unsupported." )
95- loading_label .grid_remove ()
96- return
97- bjson_dict = json .loads (json_str )
98-
99- populate_tree (tree , bjson_dict )
100- if not hasattr (tree , 'icons' ):
101- tree .icons = {}
102- tree .icons ["objectLogo" ] = tkinter .PhotoImage (file = os .path .join (root .app_path , "assets/object.png" ))
103- tree .icons ["arrayLogo" ] = tkinter .PhotoImage (file = os .path .join (root .app_path , "assets/array.png" ))
104- tree .icons ["textLogo" ] = tkinter .PhotoImage (file = os .path .join (root .app_path , "assets/text.png" ))
105- tree .icons ["numberLogo" ] = tkinter .PhotoImage (file = os .path .join (root .app_path , "assets/number.png" ))
106- tree .icons ["booleanLogo" ] = tkinter .PhotoImage (file = os .path .join (root .app_path , "assets/boolean.png" ))
107- tree .icons ["nullLogo" ] = tkinter .PhotoImage (file = os .path .join (root .app_path , "assets/null.png" ))
108- setIcons (tree , tree .icons )
109- tree .grid (row = 0 , column = 0 , sticky = "wesn" )
110- inputPath = Path (fp )
111- root .title (f"MC3DS BJSON Editor - { inputPath .name } " )
76+ bjson_dict = getBjsonContent (fp )
77+
78+ if bjson_dict :
79+ populate_tree (tree , bjson_dict )
80+ if not hasattr (tree , 'icons' ):
81+ tree .icons = {}
82+ tree .icons ["objectLogo" ] = tkinter .PhotoImage (file = os .path .join (root .app_path , "assets/object.png" ))
83+ tree .icons ["arrayLogo" ] = tkinter .PhotoImage (file = os .path .join (root .app_path , "assets/array.png" ))
84+ tree .icons ["textLogo" ] = tkinter .PhotoImage (file = os .path .join (root .app_path , "assets/text.png" ))
85+ tree .icons ["numberLogo" ] = tkinter .PhotoImage (file = os .path .join (root .app_path , "assets/number.png" ))
86+ tree .icons ["booleanLogo" ] = tkinter .PhotoImage (file = os .path .join (root .app_path , "assets/boolean.png" ))
87+ tree .icons ["nullLogo" ] = tkinter .PhotoImage (file = os .path .join (root .app_path , "assets/null.png" ))
88+ setIcons (tree , tree .icons )
89+ tree .grid (row = 0 , column = 0 , sticky = "wesn" )
90+ inputPath = Path (fp )
91+ root .title (f"MC3DS BJSON Editor - { inputPath .name } " )
11292 except :
11393 pass
11494
@@ -211,16 +191,14 @@ def __init__(self, fp = None):
211191
212192 self .lastValue = None
213193 self .filePath = fp
214- self .changes = []
215- self .actualIndex = None
194+ self .selectedItem = None
216195
217196 if fp != None :
218197 threading .Thread (target = partial (loadFileDataFromBjson , self , self .tree , fp )).start ()
219198
220199 self .saved = True
221200
222201 def registerChange (self ):
223- index = self .actualIndex
224202 dataType = self .dataTypeStringVar .get ()
225203 newValue = self .valueStringVar .get ()
226204 if dataType == "Number" :
@@ -239,39 +217,17 @@ def registerChange(self):
239217 tkinter .messagebox .showwarning (title = "Invalid value" , message = "Boolean values only accept 'true' or 'false'" )
240218 return
241219 if (type (newValue ) == type (self .lastValue )) or ((type (newValue ) == int or type (newValue ) == float ) and dataType == "Number" ):
242- for element in self .changes :
243- if element [0 ] == index :
244- self .changes .remove (element )
245- newItem = [index , dataType , newValue ]
246- self .changes .append (newItem )
247- print ("Change registered" )
248- self .saved = False
220+ if self .selectedItem :
221+ self .tree .item (self .selectedItem , values = [dataType , newValue ])
222+ print ("Change registered" )
223+ self .saved = False
249224 else :
250225 tkinter .messagebox .showwarning (title = "Invalid value" , message = "The value entered is not valid for this instance" )
251226
252227 def saveChanges (self ):
253228 if type (self .filePath ) == str :
254- with open (self .filePath , "rb" ) as f :
255- fileContent = list (f .read ())
256- for element in self .changes :
257- idx = element [0 ] * 3 + 1
258- if element [1 ] == "Number" :
259- if type (element [2 ]) == int :
260- data_id = list (uint_to_bytes (2 , "little" ))
261- value = list (int_to_bytes (element [2 ], "little" ))
262- else :
263- data_id = list (uint_to_bytes (3 , "little" ))
264- value = list (float_to_bytes (element [2 ], "little" ))
265- elif element [1 ] == "Boolean" :
266- if element [2 ] == "true" :
267- data_id = list (uint_to_bytes (1 , "little" ))
268- value = list (int_to_bytes (1 , "little" ))
269- else :
270- data_id = list (uint_to_bytes (1 , "little" ))
271- value = list (int_to_bytes (0 , "little" ))
272- if element [1 ] == "Number" or element [1 ] == "Boolean" :
273- fileContent [idx * 4 :idx * 4 + 4 ] = data_id
274- fileContent [idx * 4 + 4 :idx * 4 + 8 ] = value
229+ # TODO: Convert treeview to json
230+ fileContent = []
275231 outputPath = tkinter .filedialog .asksaveasfilename (defaultextension = ".bjson" , filetypes = [("BJSON Files" , ".bjson" )])
276232 if outputPath != "" :
277233 with open (outputPath , "wb" ) as f :
@@ -283,8 +239,6 @@ def saveChanges(self):
283239 self .valueStringVar .set ("" )
284240 self .dataTypeStringVar .set ("" )
285241 self .lastValue = None
286- self .changes = []
287- self .actualIndex = None
288242 self .filePath = outputPath
289243 self .saved = True
290244
@@ -300,8 +254,6 @@ def openFile(self):
300254 self .dataTypeStringVar .set ("" )
301255 self .saveButton .configure (state = "disabled" )
302256 self .lastValue = None
303- self .changes = []
304- self .actualIndex = None
305257 self .filePath = inputFp
306258 self .saved = True
307259
@@ -312,16 +264,13 @@ def clearTreeview(self):
312264 def itemSelected (self , event ):
313265 for selected_item in self .tree .selection ():
314266 item = self .tree .item (selected_item )
267+ self .selectedItem = selected_item
315268 record = item ['values' ]
316269 self .dataTypeStringVar .set (record [0 ])
317- self .actualIndex = record [1 ]
318- if len (record ) > 2 :
319- for element in self .changes :
320- if element [0 ] == record [1 ]:
321- record [2 ] = element [2 ]
270+ if len (record ) > 1 :
322271 if record [0 ] != "Boolean" :
323- self .valueStringVar .set (record [2 ])
324- self .lastValue = record [2 ]
272+ self .valueStringVar .set (record [1 ])
273+ self .lastValue = record [1 ]
325274 else :
326275 if record [2 ] == True :
327276 self .valueStringVar .set ("true" )
0 commit comments