-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
465 lines (401 loc) · 16 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env python
# coding: utf-8
__author__ = 'ievans3024'
__version__ = '0.0.2'
# TODO: REDO ALL OF THIS WITH PySide/Qt4/QML
from os import getcwd, getenv, walk, access, F_OK
from os.path import expanduser, join, split
from sys import platform
from tkinter import BOTH, filedialog, Menu, N, NW, PhotoImage, S, Tk, messagebox
from tkinter.ttk import Button, Frame, Separator, Style, Treeview
import nbt
import Pmw
# Tk instantiated outside of __main__ to allow use of Image classes for icon definitions
root = Tk()
# Icon definitions are necessary because widgets do not save data provided to the 'image' kwarg to the instance.
# To prevent image data from being garbage collected, they must exist in an external possible reference.
icons = {
'about': PhotoImage(file='icons/gnome-about-logo.png'),
'actions': {
'chunk_find': PhotoImage(file='icons/workspace-switcher.png'),
'copy': PhotoImage(file='icons/edit-copy.png'),
'cut': PhotoImage(file='icons/edit-cut.png'),
'delete': PhotoImage(file='icons/list-remove.png'),
'edit': PhotoImage(file='icons/gtk-edit.png'),
'exit': PhotoImage(file='icons/system-log-out.png'),
'move': {
'down': PhotoImage(file='icons/go-down.png'),
'right': PhotoImage(file='icons/go-next.png'),
'up': PhotoImage(file='icons/go-up.png')
},
'new': {
'generic': PhotoImage(file='icons/list-add.png'),
'tag': {
'byte': PhotoImage(file='icons/tag-byte-new.png'),
'byte_array': PhotoImage(file='icons/tag-byte-array-new.png'),
'compound': PhotoImage(file='icons/tag-compound-new.png'),
'double': PhotoImage(file='icons/tag-double-new.png'),
'float': PhotoImage(file='icons/tag-float-new.png'),
'int': PhotoImage(file='icons/tag-int-new.png'),
'int_array': PhotoImage(file='icons/tag-int-array-new.png'),
'list': PhotoImage(file='icons/tag-list-new.png'),
'long': PhotoImage(file='icons/tag-long-new.png'),
'short': PhotoImage(file='icons/tag-short-new.png'),
'string': PhotoImage(file='icons/tag-string-new.png'),
}
},
'open': {
'file': PhotoImage(file='icons/document-open.png'),
'folder': PhotoImage(file='icons/folder-move.png'),
'mc_folder': PhotoImage(file='icons/go-home.png')
},
'paste': PhotoImage(file='icons/edit-paste.png'),
'refresh': PhotoImage(file='icons/view-refresh.png'),
'rename': PhotoImage(file='icons/format-text-strikethrough.png'),
'replace': PhotoImage(file='icons/edit-find-replace.png'),
'save': PhotoImage(file='icons/media-floppy.png'),
'search': PhotoImage(file='icons/edit-find.png')
},
'tags': {
'byte': PhotoImage(file='icons/tag-byte.png'),
'byte_array': PhotoImage(file='icons/tag-byte-array.png'),
'compound': PhotoImage(file='icons/tag-compound.png'),
'double': PhotoImage(file='icons/tag-double.png'),
'float': PhotoImage(file='icons/tag-float.png'),
'int': PhotoImage(file='icons/tag-int.png'),
'int_array': PhotoImage(file='icons/tag-int-array.png'),
'list': PhotoImage(file='icons/tag-list.png'),
'long': PhotoImage(file='icons/tag-long.png'),
'short': PhotoImage(file='icons/tag-short.png'),
'string': PhotoImage(file='icons/text-plain.png'),
}
}
loaded_files = {}
loaded_dir = ''
class EventHandler(object):
def __init__(self):
super().__init__()
self.events = []
def fire(self, trigger):
for e in self.events:
if e[0] == trigger:
e[1]()
def bind(self, trigger, event):
self.events.append((trigger, event))
handler = EventHandler()
def new_file():
global loaded_files
newfile = filedialog.asksaveasfilename(defaultextension='.nbt',
filetypes=(('Generic NBT', '*.nbt'), ('All Files', '*')),
initialdir=getcwd())
nbtfile = nbt.nbt.NBTFile()
nbtfile.name = split(newfile)[1]
loaded_files[newfile] = nbtfile
handler.fire('open_files')
def open_files(get_files=()):
global loaded_files
loaded_files = {}
failures = []
if not get_files:
get_files = filedialog.askopenfilenames(initialdir=getcwd())
for f in get_files:
try:
loaded_files[f] = nbt.nbt.NBTFile(f)
except OSError:
failures.append(f)
if len(failures) > 1:
messagebox.showwarning('Open File', 'One or more files did not appear to be an NBT file and were not opened.')
elif len(failures) == 1:
messagebox.showwarning('Open File', '%s does not appear to be an NBT file.' % f)
handler.fire('open_files')
def open_folder():
global loaded_files, loaded_dir
get_dir = filedialog.askdirectory(initialdir=getcwd())
filetree = walk(get_dir)
files = []
for t in filetree:
for f in t[2]:
files.append(join(t[0], f))
open_files(get_files=files)
def open_mc_dir():
global loaded_files
if str(platform) in ('win32', 'win64'):
mc_dir = join(getenv('APPDATA'), '.minecraft', 'saves')
elif str(platform) in ('mac', 'darwin'):
mc_dir = join(expanduser('~'), 'Library', 'Application Support', 'minecraft', 'saves')
else:
mc_dir = join(expanduser('~'), '.minecraft', 'saves')
get_files = filedialog.askopenfilename(initialdir=mc_dir)
# TODO: decide if I want to open all savegame nbt files or just level.dat
def refresh_files():
global loaded_files
files = (f for f in loaded_files)
open_files(get_files=files)
def save_files():
global loaded_files
for f in loaded_files:
if isinstance(loaded_files[f], nbt.nbt.NBTFile):
loaded_files[f].write_file(f)
class MainMenu(Menu):
"""
Macro class for main window menus
"""
def __init__(self, parent):
Menu.__init__(self, parent)
self.parent = parent
menu_file = Menu(self, tearoff=0)
menu_file.add_command(compound='left',
image=icons['actions']['new']['generic'], label='New...', command=new_file)
menu_file.add_command(compound='left',
image=icons['actions']['open']['file'], label='Open...', command=open_files)
menu_file.add_command(compound='left',
image=icons['actions']['open']['folder'], label='Open Folder...', command=open_folder)
menu_file.add_command(compound='left',
image=icons['actions']['open']['mc_folder'],
label='Open Minecraft Save Folder...', command=open_mc_dir)
menu_file.add_separator()
menu_file.add_command(compound='left', image=icons['actions']['save'], label='Save...', command=save_files)
menu_file.add_command(compound='left', image=icons['actions']['refresh'],
label='Refresh', command=refresh_files)
menu_file.add_separator()
menu_file.add_command(compound='left', image=icons['actions']['exit'], label='Exit', command=root.quit)
menu_edit = Menu(self, tearoff=0)
menu_edit.add_command(compound='left', image=icons['actions']['copy'], label='Copy')
menu_edit.add_command(compound='left', image=icons['actions']['cut'], label='Cut')
menu_edit.add_command(compound='left', image=icons['actions']['paste'], label='Paste')
menu_edit.add_separator()
menu_edit.add_command(compound='left', image=icons['actions']['rename'], label='Rename')
menu_edit.add_command(compound='left', image=icons['actions']['edit'], label='Edit')
menu_edit.add_command(compound='left', image=icons['actions']['delete'], label='Delete')
menu_edit.add_separator()
menu_edit.add_command(compound='left', image=icons['actions']['move']['up'], label='Move Up')
menu_edit.add_command(compound='left', image=icons['actions']['move']['down'], label='Move Down')
menu_search = Menu(self, tearoff=0)
menu_search.add_command(compound='left', image=icons['actions']['search'], label='Find')
menu_search.add_command(compound='left', image=icons['actions']['move']['right'], label='Find Next')
menu_search.add_separator()
menu_search.add_command(compound='left', image=icons['actions']['replace'], label='Replace')
menu_search.add_separator()
menu_search.add_command(compound='left', image=icons['actions']['chunk_find'], label='Chunk Finder')
menu_help = Menu(self, tearoff=0)
menu_help.add_command(compound='left', image=icons['about'], label='About')
self.add_cascade(label='File', menu=menu_file)
self.add_cascade(label='Edit', menu=menu_edit)
self.add_cascade(label='Search', menu=menu_search)
self.add_cascade(label='Help', menu=menu_help)
class ToolBar(Frame):
"""
Macro class for main window toolbar
"""
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
toolbar_config = [
{
'type': 'button',
'title': 'Open Files',
'icon': icons['actions']['open']['file'],
'command': open_files
},
{
'type': 'button',
'title': 'Open Folder',
'icon': icons['actions']['open']['folder'],
'command': open_folder
},
{
'type': 'button',
'title': 'Save Files',
'icon': icons['actions']['save'],
'command': save_files
},
{
'type': 'button',
'title': 'Refresh Contents From Disk...',
'icon': icons['actions']['refresh'],
'command': refresh_files
},
{
'type': 'separator'
},
{
'type': 'button',
'title': 'Cut',
'icon': icons['actions']['cut'],
'command': None
},
{
'type': 'button',
'title': 'Copy',
'icon': icons['actions']['copy'],
'command': None
},
{
'type': 'button',
'title': 'Paste',
'icon': icons['actions']['paste'],
'command': None
},
{
'type': 'separator'
},
{
'type': 'button',
'title': 'Rename Tag',
'icon': icons['actions']['rename'],
'command': None
},
{
'type': 'button',
'title': 'Edit Tag',
'icon': icons['actions']['edit'],
'command': None
},
{
'type': 'button',
'title': 'Delete Tag',
'icon': icons['actions']['delete'],
'command': None
},
{
'type': 'separator'
},
{
'type': 'button',
'title': 'New Byte Tag',
'icon': icons['actions']['new']['tag']['byte'],
'command': None
},
{
'type': 'button',
'title': 'New Short Tag',
'icon': icons['actions']['new']['tag']['short'],
'command': None
},
{
'type': 'button',
'title': 'New Int Tag',
'icon': icons['actions']['new']['tag']['int'],
'command': None
},
{
'type': 'button',
'title': 'New Long Tag',
'icon': icons['actions']['new']['tag']['long'],
'command': None
},
{
'type': 'button',
'title': 'New Float Tag',
'icon': icons['actions']['new']['tag']['float'],
'command': None
},
{
'type': 'button',
'title': 'New Double Tag',
'icon': icons['actions']['new']['tag']['double'],
'command': None
},
{
'type': 'button',
'order': 19,
'title': 'New Byte Array Tag',
'icon': icons['actions']['new']['tag']['byte_array'],
'command': None
},
{
'type': 'button',
'title': 'New Int Array Tag',
'icon': icons['actions']['new']['tag']['int_array'],
'command': None
},
{
'type': 'button',
'title': 'New String Tag',
'icon': icons['actions']['new']['tag']['string'],
'command': None
},
{
'type': 'button',
'title': 'New List Tag',
'icon': icons['actions']['new']['tag']['list'],
'command': None
},
{
'type': 'button',
'title': 'New Compound Tag',
'icon': icons['actions']['new']['tag']['compound'],
'command': None
},
{
'type': 'separator'
},
{
'type': 'button',
'title': 'Find...',
'icon': icons['actions']['search'],
'command': None
}
]
elements = []
for element in toolbar_config:
if element['type'] == 'separator':
elements.append(
Separator(self, orient='vertical')
)
elif element['type'] == 'button':
button = Button(self, image=element['icon'], command=element['command'])
Pmw.Balloon(self).bind(button, element['title'])
elements.append(button)
for e in elements:
if isinstance(e, Separator):
e.grid(row=0, column=elements.index(e), sticky=N+S, padx=6)
else:
e.grid(row=0, column=elements.index(e), padx=1)
class TreeDisplay(object):
"""
Acts as an intermediary between a treeview object and one or more nbt objects
"""
def __init__(self, treeview):
self.treeview = treeview
self.file_tree_map = {}
def refresh(self):
global loaded_files
if loaded_files:
file_counter = 0
for f in loaded_files:
filename = '{file} ({save})'.format(
save=split(split(loaded_files[f].filename)[0])[-1],
file=split(loaded_files[f].filename)[-1]
)
self.treeview.insert('', '%i' % file_counter, filename,
text=filename, image=icons['tags']['compound'])
file_counter += 1
class MainWindow(Frame):
"""
Macro class for the main program window
"""
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.parent.title('PyNBTx %s' % __version__)
self.style = Style()
self.style.theme_use('default')
self.style.configure('TButton', padding=0)
self.pack(fill=BOTH, expand=1)
self.menu = MainMenu(root)
self.toolbar = ToolBar(self)
self.tree = Treeview(self, height=20)
self.tree_display = TreeDisplay(self.tree)
self.ui_init()
def ui_init(self):
root['menu'] = self.menu
self.toolbar.pack(anchor=NW, padx=4, pady=4)
self.tree.column('#0', width=300)
self.tree.pack(fill=BOTH, anchor=NW, padx=4, pady=4)
def main():
app = MainWindow(root)
handler.bind('open_files', app.tree_display.refresh)
root.mainloop()
if __name__ == '__main__':
main()