-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
1051 lines (856 loc) · 33.9 KB
/
gui.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
if __name__ == '__main__':
print('Please run main.py to start the program!')
sys.exit()
import os
import platform
import traceback
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.font
import tkinter.messagebox
import tkinter.filedialog
# for PyInstaller binaries
try:
temp_path = sys._MEIPASS
except AttributeError:
temp_path = os.getcwd()
import re
import json
import urllib.request
import threading
import webbrowser
import configparser
import pkg_resources
pg_name = 'PyU8ROP' # program name here
username = 'fxesdev' # GitHub username here
repo_name = 'pyu8rop' # GitHub repository name here
version = '0.2.0' # displayed version (e.g. 1.0.0 Prerelease - must match GH release title)
internal_version = 'v0.2.0' # internal version (must match GitHub release tag)
prerelease = False # prerelease flag (must match GitHub release's prerelease flag)
def report_error(self=None, exc=None, val=None, tb=None, term=True):
"""
Logs in the console and displays a dialog box showing the error.
Replaces the report_callback_exception() function in
the tkinter.Tk class.
NOTE: DO NOT REMOVE THE UNUSED ARGUMENTS! Due to the function replacement
these arguments must be added.
"""
e = traceback.format_exc()
err_text = f'''\
Whoops! An error has occurred.
{e}
If this error persists, please report it here:
https://github.com/{username}/{repo_name}/issues\
'''
print(err_text)
tk.messagebox.showerror('Whoops!', err_text)
if term:
sys.exit()
tk.Tk.report_callback_exception = report_error
class GUI:
def __init__(self, window):
self.version = version
self.window = window
self.temp_path = temp_path
# change width and height of window here
self.display_w = 800
self.display_h = 600
# TODO: add more "open" bools for other Toplevel classes
self.updater_win_open = False
tk_font = tk.font.nametofont('TkDefaultFont').actual()
self.font_name = tk_font['family']
self.font_size = tk_font['size']
# TODO: add more font styles (italic, condensed, etc.)
self.bold_font = (self.font_name, self.font_size, 'bold')
self.init_window()
self.init_protocols()
self.gadgets = {}
self.romgadgets = {}
self.gadgetdropdown_w = None
# updater settings
self.auto_check_updates = tk.BooleanVar()
self.auto_check_updates.set(True)
self.check_prerelease_version = tk.BooleanVar()
self.check_prerelease_version.set(False)
self.debug = False
# gets appdata folder
if os.name == 'nt':
self.appdata_folder = f'{os.getenv("LOCALAPPDATA")}\\{pg_name}'
else:
if platform.system() == 'Darwin':
self.appdata_folder = os.path.expanduser(f'~/Library/Application Support/{pg_name}')
else:
self.appdata_folder = os.path.expanduser(f'~/.config/{pg_name}')
self.save_to_cwd = False
self.ini = configparser.ConfigParser()
self.parse_settings()
self.refreshing = True
# TODO: add more toplevel classes
self.UpdaterGUI = UpdaterGUI(self)
self.unsupported_tcl = False
if sys.version_info < (3, 7, 6):
if tk.messagebox.askyesno('Warning', f'''
It looks like you are running Python {platform.python_version()}, which has a version of Tcl/Tk that doesn\'t support \
some Unicode characters.
Do you want to continue?\
''', icon='warning'):
self.unsupported_tcl = True
else:
self.quit()
self.menubar()
def start_main(self):
"""
Runs necessary commands before calling the main function.
"""
self.updates_checked = False
if self.auto_check_updates.get():
threading.Thread(target=self.auto_update).start()
else:
self.updates_checked = True
ttk.Label(text = 'PyU8ROP\n', font = self.bold_font).pack()
addmenu = tk.Menu()
addmenu.add_command(label = 'Address', command = self.add_gadget)
addmenu.add_command(label = 'Popped WORD', command = lambda: self.add_gadget('pop', [None]*2))
addmenu.add_command(label = 'Popped DWORD', command = lambda: self.add_gadget('pop', [None]*4))
addmenu.add_command(label = 'Popped QWORD', command = lambda: self.add_gadget('pop', [None]*8))
buttonframe = FocusFrame()
addbtn = ttk.Button(buttonframe, text = 'Add gadget')
addbtn['command'] = lambda: addmenu.post(addbtn.winfo_rootx(), addbtn.winfo_rooty() + addbtn.winfo_height())
addbtn.pack(side = 'left')
self.clearbutton = ttk.Button(buttonframe, text = 'Delete all gadgets', command = self.clear_all, state = 'disabled'); self.clearbutton.pack(side = 'left')
buttonframe.pack()
self.gadgetframe = VerticalScrolledFrame(self.window)
self.gadgetframe.pack(fill = 'both', expand = True)
self.main()
def add_gadget(self, type_ = 'address', data = None):
if type_ == 'address': wclass = Address
elif type_ == 'pop': wclass = Pop
else:
self.n_a()
return
idx = max(self.gadgets) + 1 if len(self.gadgets) > 0 else 0
if len(self.gadgets) == 0: self.clearbutton['state'] = 'normal'
widget = wclass(self.gadgetframe.interior, self, idx, data)
self.gadgets[idx] = {'type': type_, 'data': data, 'widget': widget}
widget.pack(fill = 'x')
def clear_all(self, confirm = True):
if confirm and not tk.messagebox.askyesno('Warning', 'Are you sure you want to delete all gadgets?', icon = 'warning'): return
for j in [i['widget'] for i in self.gadgets.values()]: j.destroy()
@staticmethod
def validate_hex(new_char, new_str, act_code, rang = None, spaces = False):
act_code = int(act_code)
if rang: rang = eval(rang)
if act_code == 1:
try: new_value_int = int(new_char, 16)
except ValueError:
if len(new_char) == 1:
if new_char != ' ': return False
elif not spaces: return False
else:
try: new_value_int = int(new_char.replace(' ', ''), 16)
except ValueError: return False
if rang:
if len(new_str) > len(hex(rang[-1])[2:]): return False
elif len(new_str) == len(hex(rang[-1])[2:]) and int(new_str, 16) not in rang: return False
return True
def open(self):
f = tk.filedialog.askopenfile(mode = 'rb', filetypes = [('All Files', '*.*'), ('Binary Files', '*.bin')], defaultextension = '.bin')
if f is None: return
self.clear_all(False)
bytecode = f.read()
for i in range(0, len(bytecode), 4):
data = int.from_bytes(bytecode[i:i+4], 'little')
self.add_gadget(data = data & 0xffffe)
f.close()
@staticmethod
def process_name(name):
tags = re.findall(r'{([^}]+)}', name)
result = {'name': re.sub(r'{.*?}', '', name), 'pop': [], 'tags': [], 'warning': ''}
name_index = name.index(result['name'])
if tags and all([name.find(f'{{{tag}}}') < name_index for tag in tags]):
for tag in tags:
if tag.startswith('warning'):
warning_content = tag.split(' ', 1)[1]
result['warning'] = warning_content
else: result['tags'].append(tag)
n = name.lower().split(',')
for m in n:
if m == 'rt': result['tags'].append('rt')
if m.startswith('pop'):
o = m.split()
for p in o:
if p == 'ea': result['pop'].append(2)
elif p == 'lr': result['pop'].append(1)
elif p == 'psw' or p.startswith('r') or p.startswith('er'): result['pop'].append(2)
elif p.startswith('xr'): result['pop'].append(4)
elif p.startswith('qr'): result['pop'].append(8)
return result
def load_gadgets(self):
fname = tk.filedialog.askopenfilename(filetypes = [('All Files', '*.*')])
if not fname: return
f = open(fname, encoding = 'utf-8')
gadget_data = []
gs = f.readlines()
f.close()
si = [i for i, x in enumerate(gs) if x == "/*\n"]
ei = [i for i, x in enumerate(gs) if x == "*/\n"]
for sidx, eidx in zip(si[::-1], ei[::-1]):
del gs[sidx:eidx + 1]
gs.remove("/*\n")
gs.remove("*/\n")
gdata = [re.split(r'\t', g.strip().split('#', 1)[0].strip()) for g in gs]
gadget_data.extend(list(filter((['']).__ne__, gdata)))
for data in gadget_data:
if len(data) < 2: continue
try:
addr = int(data[0], 16)
if addr in self.romgadgets: logging.warning(f'Duplicate gadget definition {addr:05X}, skipping')
else: self.romgadgets[addr] = self.process_name(data[1])
except ValueError: logging.warning(f'Invalid gadget definition {addr:05X}, skipping')
self.gadgetdropdown_w = max([10+len(v["name"]) for v in self.romgadgets.values()])
tk.messagebox.showinfo('Load ROM gadget list', 'Done! You can now use the dropdowns in the address gadgets.')
def auto_update(self):
self.update_thread = ThreadWithResult(target=self.UpdaterGUI.updater.check_updates, args=(True,))
self.update_thread.start()
i = 0
j = 0
mult = 5000
while self.update_thread.is_alive():
if i == mult * 4:
i += 1
j = 1
else:
i = i - 1 if j else i + 1
if i == 0:
j = 0
print(' ' * (int(i / mult)) + '.' + ' ' * (5 - int(i / mult)), end='\r')
print('\r ', end='\r')
update_info = self.update_thread.result
if update_info['newupdate']:
self.UpdaterGUI.init_window(True, (update_info['title'], update_info['tag'], update_info['prerelease'],
update_info['body']))
self.updates_checked = True
def parse_settings(self):
"""
Loads the program settings.
"""
# load override settings
if os.path.exists(os.path.join(os.getcwd(), 'settings.ini')):
self.ini.read('settings.ini')
self.save_to_cwd = True
else:
# load normal settings
self.ini.read(f'{self.appdata_folder}\\settings.ini')
sects = self.ini.sections()
if sects:
if 'settings' in sects:
# TODO: add commands for loading settings
pass
if 'updater' in sects:
try:
self.auto_check_updates.set(self.ini.getboolean('updater', 'auto_check_updates'))
except (configparser.NoSectionError, configparser.NoOptionError):
pass
try:
self.check_prerelease_version.set(self.ini.getboolean('updater', 'check_prerelease_version'))
except (configparser.NoSectionError, configparser.NoOptionError):
pass
if 'dont_touch_this_area_unless_you_know_what_youre_doing' in sects:
try:
self.debug = self.ini.getboolean('dont_touch_this_area_unless_you_know_what_youre_doing', 'debug')
except (configparser.NoSectionError, configparser.NoOptionError):
pass
self.save_settings()
def save_settings(self):
"""
Saves the program settings.
"""
# settings are set individually and initialized when needed to retain compatibility between versions
if 'settings' not in self.ini.keys():
self.ini['settings'] = {}
# TODO: add commands for saving settings
if 'updater' not in self.ini.keys():
self.ini['updater'] = {}
self.ini['updater']['auto_check_updates'] = str(self.auto_check_updates.get())
self.ini['updater']['check_prerelease_version'] = str(self.check_prerelease_version.get())
if 'dont_touch_this_area_unless_you_know_what_youre_doing' not in self.ini.keys():
self.ini['dont_touch_this_area_unless_you_know_what_youre_doing'] = {}
self.ini['dont_touch_this_area_unless_you_know_what_youre_doing']['debug'] = str(self.debug)
if self.save_to_cwd:
with open(os.path.join(os.getcwd(), 'settings.ini'), 'w') as f:
self.ini.write(f)
if not os.path.exists(self.appdata_folder):
os.makedirs(self.appdata_folder)
with open(f'{self.appdata_folder}\\settings.ini', 'w') as f:
self.ini.write(f)
@staticmethod
def n_a():
"""
Used to prevent access to unimplemented or unfinished features.
"""
tk.messagebox.showinfo('Not implemented',
f'This feature is not implemented into this version of {pg_name}. Sorry!')
def set_title(self, custom_str=None):
"""
Sets the Tkinter window title.
"""
self.window.title(f'{pg_name} {version}{" - " + custom_str if custom_str is not None else ""}')
def init_window(self):
"""
Initializes the Tkinter window.
"""
self.window.geometry(f'{self.display_w}x{self.display_h}')
self.window.resizable(False, False)
self.window.bind('<F12>', self.version_details)
self.window.bind('<Control-O>', lambda x: self.open())
self.window.bind('<Control-o>', lambda x: self.open())
self.window.bind('<Control-G>', lambda x: self.load_gadgets())
self.window.bind('<Control-g>', lambda x: self.load_gadgets())
self.window.option_add('*tearOff', False)
self.set_title()
# TODO: uncomment this when you actually have an icon.ico/xbm file
# try:
# self.window.iconbitmap(f'{self.temp_path}\\icon.{"ico" if os.name == "nt" else "xbm"}')
# except tk.TclError:
# err_text = f'''\
# Whoops! The icon file "icon.ico" is required.
# Can you make sure the file is in "{self.temp_path}"?
# {traceback.format_exc()}
# If this problem persists, please report it here:
# https://github.com/{username}/{repo_name}/issues\
# '''
# print(err_text)
# tk.messagebox.showerror('Hmmm?', err_text)
# sys.exit()
def init_protocols(self):
"""
Initializes protocols.
"""
self.window.protocol('WM_DELETE_WINDOW', self.quit)
def quit(self):
"""
Quits the program.
"""
if not any([
self.updater_win_open,
# TODO: add other "open" bools here
]):
sys.exit()
@staticmethod
def about_menu():
"""
Shows basic information about the version, system and architecture, as well as the license of the project.
"""
nl = '\n'
syst = platform.system()
syst += ' x64' if platform.machine().endswith('64') else ' x86'
tk.messagebox.showinfo(f'About {pg_name}', f'''\
{pg_name} - {version} ({'64' if sys.maxsize > 2 ** 31 - 1 else '32'}-bit) - Running on {syst}
Project page: https://github.com/{username}/{repo_name}
{nl + 'WARNING: This is a pre-release version, therefore it may have bugs and/or glitches.' + nl if prerelease else ''}
Licensed under the GNU GPL-v3 license
(LICENSE file available on the GitHub repository or included with source code)\
''')
def version_details(self, event=None):
"""
Shows technical information about the Python installation and operating system.
By default, it can be triggered via the F12 key.
"""
if self.debug:
dnl = '\n\n'
tk.messagebox.showinfo(f'{pg_name} version details', f'''\
{pg_name} {version}{" (prerelease)" if prerelease else ""}
Internal version: {internal_version}
Python version information:
Python {platform.python_version()} ({'64' if sys.maxsize > 2 ** 31 - 1 else '32'}-bit)
Tkinter (Tcl/Tk) version {self.window.tk.call('info', 'patchlevel')}\
{" (most Unicode chars not supported)" if self.unsupported_tcl else ""}
Operating system information:
{platform.system()} {platform.release()}
{'NT version: ' if os.name == 'nt' else ''}{platform.version()}
Architecture: {platform.machine()}{dnl + "Settings file is saved to working directory" if self.save_to_cwd else ""}\
''')
def disable_debug(self):
if tk.messagebox.askyesno('Warning',
'To re-enable debug mode you must set the debug flag to True in settings.ini.\nContinue?',
icon='warning'):
self.debug = False
self.save_settings()
self.menubar() # update the menubar
def menubar(self):
"""
Sets up the menubar.
"""
menubar = tk.Menu()
file_menu = tk.Menu(menubar)
file_menu.add_command(label = 'New', accelerator = 'Ctrl+N', state = 'disabled')
file_menu.add_command(label = 'Open...', accelerator = 'Ctrl+O', command = self.open)
file_menu.add_command(label = 'Save', accelerator = 'Ctrl+S', state = 'disabled')
file_menu.add_command(label = 'Save as...', accelerator = 'Ctrl+Shift+S', state = 'disabled')
file_menu.add_separator()
file_menu.add_command(label = 'Load ROM', accelerator = 'Ctrl+L', state = 'disabled')
file_menu.add_command(label = 'Load ROM gadget list', accelerator = 'Ctrl+G', command = self.load_gadgets)
file_menu.add_separator()
file_menu.add_command(label = 'Exit', command = self.quit)
menubar.add_cascade(label='File', menu=file_menu)
settings_menu = tk.Menu(menubar)
# TODO: add more setting options
updater_settings_menu = tk.Menu(settings_menu)
updater_settings_menu.add_checkbutton(label='Check for updates on startup', variable=self.auto_check_updates,
command=self.save_settings)
updater_settings_menu.add_checkbutton(label='Check for pre-release versions',
variable=self.check_prerelease_version,
command=self.save_settings)
settings_menu.add_cascade(label='Updates', menu=updater_settings_menu)
if self.debug:
settings_menu.add_separator()
debug_menu = tk.Menu(settings_menu)
debug_menu.add_command(label='Version details', command=self.version_details, accelerator='F12')
debug_menu.add_separator()
debug_menu.add_command(label='Updater test', command=lambda: self.UpdaterGUI.init_window(debug=True))
debug_menu.add_separator()
debug_menu.add_command(label='Disable debug mode', command=self.disable_debug)
settings_menu.add_cascade(label='Debug', menu=debug_menu)
menubar.add_cascade(label='Settings', menu=settings_menu)
help_menu = tk.Menu(menubar)
help_menu.add_command(label='Check for updates', command=self.UpdaterGUI.init_window)
help_menu.add_command(label=f'About {pg_name}', command=self.about_menu)
menubar.add_cascade(label='Help', menu=help_menu)
self.window.config(menu=menubar)
def main(self):
"""
Where the mainloop is called.
"""
self.set_title('Experimental version')
self.window.mainloop()
class UpdaterGUI:
def __init__(self, gui):
self.gui = gui
self.after_ms = 100
self.updater = Updater()
def init_window(self, auto=False, auto_download_options=None, debug=False):
if not self.gui.updater_win_open:
self.gui.updater_win_open = True
self.auto = auto
self.debug = debug
self.win = tk.Toplevel(self.gui.window)
self.win.geometry('400x400')
self.win.resizable(False, False)
self.win.protocol('WM_DELETE_WINDOW', self.quit)
self.win.title('Updater')
# TODO: uncomment this when you actually have an icon.ico/xbm file
# try:
# self.updater_win.iconbitmap(f'{self.gui.temp_path}\\icon.{"ico" if os.name == "nt" else "xbm"}')
# except tk.TclError:
# err_text = f'''\
# Whoops! The icon file "icon.ico" is required.
# Can you make sure the file is in "{self.gui.temp_path}"?
# {traceback.format_exc()}
# If this problem persists, please report it here:
# https://github.com/{username}/{repo_name}/issues\
# '''
# print(err_text)
# tk.messagebox.showerror('Hmmm?', err_text)
# sys.exit()
self.win.focus()
self.win.grab_set()
if self.debug:
self.debug_menu()
elif self.auto:
self.win.after(0, lambda: self.draw_download_msg(*auto_download_options))
else: self.main()
def quit(self):
self.win.grab_release()
self.win.destroy()
self.gui.updater_win_open = False
if self.auto:
self.auto = False
self.gui.main()
def main(self):
self.update_thread = ThreadWithResult(target=self.updater.check_updates,
args=(self.gui.check_prerelease_version.get(),))
self.draw_check()
self.win.after(1, self.start_thread)
self.win.mainloop()
def debug_menu(self):
ttk.Button(self.win, text='Check updates', command=self.main).pack()
ttk.Button(self.win, text='Message test',
command=lambda: self.draw_msg('Updater message test.\nLine 2\nLine 3\nLine 4')).pack()
ttk.Button(self.win, text='New update screen test',
command=lambda: self.draw_download_msg(version, internal_version, False, '''\
Hello! **This is a *test* of the updater\'s Markdown viewer**, made possible with the [Markdown](https://pypi.org/project/Markdown/), [`mdformat`](https://pypi.org/project/mdformat/), and [TkinterWeb](https://pypi.org/project/tkinterweb/) modules.
By the way, here\'s [TkTemplate](https://github.com/gamingwithevets/tktemplate), which is what this program was based on.
Also, you should check out the [Steveyboi/GWE Discord server](https://gamingwithevets.github.io/redirector/discord).\
''')).pack()
ttk.Button(self.win, text='Quit', command=self.quit).pack(side='bottom')
def start_thread(self):
self.update_thread.start()
while self.update_thread.is_alive():
self.win.update_idletasks()
self.progressbar['value'] = self.updater.progress
self.progressbar['value'] = 100
self.update_thread.join()
update_info = self.update_thread.result
if update_info['error']:
if 'exceeded' in update_info and update_info['exceeded']:
self.draw_msg('GitHub API rate limit exceeded! Please try again later.')
elif 'nowifi' in update_info and update_info['nowifi']:
self.draw_msg(
'Unable to connect to the internet. Please try again\nwhen you have a stable internet connection.')
elif 'prerelease' in update_info and update_info['prerelease']:
self.draw_msg('Cannot get the latest release. Try enabling "Check for\npre-release versions" in Settings.')
else:
self.draw_msg('Unable to check for updates! Please try again later.')
elif update_info['newupdate']:
self.draw_download_msg(update_info['title'], update_info['tag'], update_info['prerelease'])
else:
self.draw_msg('You are already using the latest version.')
def draw_check(self):
for w in self.win.winfo_children():
w.destroy()
ttk.Label(self.win, text='Checking for updates...').pack()
self.progressbar = ttk.Progressbar(self.win, orient='horizontal', length=100, mode='determinate')
self.progressbar.pack()
ttk.Label(self.win, text='DO NOT close the program\nwhile checking for updates',
justify='center', font=self.gui.bold_font).pack(side='bottom')
def draw_msg(self, msg):
if self.auto:
self.gui.set_title()
self.quit()
else:
for w in self.win.winfo_children():
w.destroy()
ttk.Label(self.win, text=msg, justify='center').pack()
ttk.Button(self.win, text='Back', command=self.quit).pack(side='bottom')
@staticmethod
def package_installed(package):
try:
pkg_resources.get_distribution(package)
except pkg_resources.DistributionNotFound:
return False
return True
def draw_download_msg(self, title, tag, prever, body):
if self.auto:
self.win.deiconify()
self.gui.set_title()
for w in self.win.winfo_children():
w.destroy()
ttk.Label(self.win, justify='center', text=f'''\
An update is available!
Current version: {self.gui.version}{" (pre-release)" if prerelease else ""}
New version: {title}{" (pre-release)" if prever else ""}\
''').pack()
ttk.Button(self.win, text='Cancel', command=self.quit).pack(side='bottom')
ttk.Button(self.win, text='Visit download page',
command=lambda: self.open_download(tag)).pack(side='bottom')
ttk.Label(self.win).pack()
packages_missing = []
for package in ('markdown', 'mdformat-gfm', 'tkinterweb'):
if not self.package_installed(package):
packages_missing.append(package)
if packages_missing:
ttk.Label(self.win,
text=f'Missing package(s): {", ".join(packages_missing[:2])}{" and " + str(len(packages_missing) - 2) + " others" if len(packages_missing) > 2 else ""}',
font=self.gui.bold_font).pack()
else:
import markdown
import mdformat
import tkinterweb
html = tkinterweb.HtmlFrame(self.win, messages_enabled=False)
html.load_html(
markdown.markdown(mdformat.text(body)).replace('../..', f'https://github.com/{username}/{repo_name}'))
html.on_link_click(webbrowser.open_new_tab)
html.pack()
if self.auto:
self.win.deiconify()
def open_download(self, tag):
webbrowser.open_new_tab(f'https://github.com/{username}/{repo_name}/releases/tag/{tag}')
self.quit()
class Updater:
def __init__(self):
self.username, self.reponame = username, repo_name
self.request_limit = 5
self.progress = 0
self.progress_inc = 25
def check_internet(self):
try:
self.request('https://google.com', True)
return True
except Exception:
return False
def request(self, url, testing=False):
success = False
for i in range(self.request_limit):
try:
r = urllib.request.urlopen(url)
success = True
break
except urllib.error.HTTPError as e:
r = e.fp
success = True
except urllib.error.URLError as e:
if not testing:
if not self.check_internet():
return
if success:
if not testing:
d = r.read().decode()
return json.loads(d)
def check_updates(self, pr):
self.progress = 0
if not self.check_internet():
return {
'newupdate': False,
'error': True,
'exceeded': False,
'nowifi': True
}
try:
versions = []
if not self.check_internet():
return {'newupdate': False, 'error': True, 'exceeded': False, 'nowifi': True}
response = self.request(f'https://api.github.com/repos/{self.username}/{self.reponame}/releases')
if response is None:
return {'newupdate': False, 'error': True, 'exceeded': False, 'nowifi': True}
for info in response:
versions.append(info['tag_name'])
# UPDATE POINT 1
self.progress += self.progress_inc
if internal_version not in versions:
try:
testvar = response['message']
if 'API rate limit exceeded for' in testvar:
return {
'newupdate': False,
'error': True,
'exceeded': True
}
else:
return {'newupdate': False, 'error': False}
except Exception:
return {'newupdate': False, 'error': False}
if not self.check_internet():
return {'newupdate': False, 'error': True, 'exceeded': False, 'nowifi': True}
# UPDATE POINT 2
self.progress += self.progress_inc
response = self.request(
f'https://api.github.com/repos/{self.username}/{self.reponame}/releases/tags/{internal_version}')
if response is None:
return {'newupdate': False, 'error': True, 'exceeded': False, 'nowifi': True}
try:
testvar = response['message']
if 'API rate limit exceeded for' in testvar:
return {
'newupdate': False,
'error': True,
'exceeded': True
}
else:
return {'newupdate': False, 'error': False}
except Exception:
pass
currvertime = response['published_at']
# UPDATE POINT 3
self.progress += self.progress_inc
if not pr:
if not self.check_internet():
return {'newupdate': False, 'error': True, 'exceeded': False, 'nowifi': True}
response = self.request(f'https://api.github.com/repos/{self.username}/{self.reponame}/releases/latest')
if response is None:
return {'newupdate': False, 'error': True, 'exceeded': False, 'nowifi': True}
try:
testvar = response['message']
if 'API rate limit exceeded for' in testvar:
return {
'newupdate': False,
'error': True,
'exceeded': True
}
else:
return {'newupdate': False, 'error': True, 'prerelease': True}
except Exception:
pass
if response['tag_name'] != internal_version and response['published_at'] > currvertime:
return {
'newupdate': True,
'prerelease': False,
'error': False,
'title': response['name'],
'tag': response['tag_name'],
'body': response['body']
}
else:
return {
'newupdate': False,
'unofficial': False,
'error': False
}
else:
for ver in versions:
if not self.check_internet():
return {'newupdate': False, 'error': True, 'exceeded': False, 'nowifi': True}
response = self.request(
f'https://api.github.com/repos/{self.username}/{self.reponame}/releases/tags/{ver}')
if response is None:
return {'newupdate': False, 'error': True, 'exceeded': False, 'nowifi': True}
try:
testvar = response['message']
if 'API rate limit exceeded for' in testvar:
return {
'newupdate': False,
'error': True,
'exceeded': True
}
else:
return {'newupdate': False, 'error': True, 'exceeded': False, 'nowifi': False}
except Exception:
pass
if currvertime < response['published_at']:
return {
'newupdate': True,
'prerelease': response['prerelease'],
'error': False,
'title': response['name'],
'tag': response['tag_name'],
'body': response['body']
}
else:
return {
'newupdate': False,
'unofficial': False,
'error': False
}
except Exception:
return {
'newupdate': False,
'error': True,
'exceeded': False,
'nowifi': False
}
# https://stackoverflow.com/a/24072653
class FocusFrame(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.bind('<1>', lambda event: self.focus_set())
class Address(FocusFrame):
def __init__(self, master, gui, index, data, **kw):
tk.Frame.__init__(self, master, **kw)
self.gui = gui
self.index = index
ttk.Label(self, text = 'Address').pack(side = 'left')
ttk.Button(self, text = 'X', width = 2, command = self.destroy_confirm).pack(side = 'right')
ttk.Label(self, text = 'H ').pack(side = 'right')
vcmd = self.register(self.gui.validate_hex)
self.pc = ttk.Entry(self, width = 6, justify = 'right', validate = 'key', validatecommand = (vcmd, '%S', '%P', '%d', range(0x10000)))
if data is not None: self.pc.insert(0, f'{data & 0xffff:04X}')
self.pc.bind('<KeyPress>', self.cap_input)
self.pc.pack(side = 'right')
ttk.Label(self, text = ':').pack(side = 'right')
self.csr = ttk.Entry(self, width = 2, justify = 'right', validate = 'key', validatecommand = (vcmd, '%S', '%P', '%d', range(0x10)))
if data is not None: self.csr.insert(0, f'{(data >> 16) & 0xf:X}')
self.csr.bind('<KeyPress>', self.cap_input)
self.csr.pack(side = 'right')
ttk.Label(self, text = ' ').pack(side = 'right')
self.dropdown_var = tk.StringVar(value = 'Undefined')
self.dropdown = ttk.Combobox(self, values = ['Undefined'], state = 'readonly', width = self.gui.gadgetdropdown_w, textvariable = self.dropdown_var, postcommand = self.add_values)
self.dropdown.bind('<<ComboboxSelected>>', self.update_csrpc)
self.dropdown.pack(side = 'right')
if data: self.focusout()
self.bind('<FocusOut>', self.focusout)
def cap_input(self, event):
if event.char.lower() in '0123456789abcdef':
event.widget.insert('end', event.char.upper())
return 'break'
def focusout(self, event = None):
self.pc.insert(0, '0'*(4-len(self.pc.get())))
if len(self.csr.get()) == 0: self.csr.insert(0, '0')
self.add_values()
l = [i for i in self.dropdown['values'] if i.startswith(f'{self.csr.get()}:{self.pc.get()}H - ')]
self.dropdown_var.set(l[0] if len(l) == 1 else 'Undefined')
def add_values(self):
if len(self.dropdown['values']) == 1 and len(self.gui.romgadgets) > 0:
self.dropdown['values'] = ['Undefined'] + [f'{k >> 16:X}:{k & 0xffff:04X}H - {v["name"]}' for k, v in self.gui.romgadgets.items()]
if self.gui.gadgetdropdown_w is None: self.dropdown['width'] = max([10+len(v["name"]) for v in self.gui.romgadgets.values()])
def update_csrpc(self, event):
gadget = self.dropdown_var.get()
if gadget == 'Undefined': return
self.csr.delete(0, 'end')
self.pc.delete(0, 'end')
self.csr.insert('end', gadget[0])
self.pc.insert('end', gadget[2:6])
def destroy_confirm(self):
if tk.messagebox.askyesno('Warning', 'Are you sure you want to delete this gadget?', icon = 'warning'): self.destroy()
def destroy(self):
del self.gui.gadgets[self.index]
if len(self.gui.gadgets) == 0: self.gui.clearbutton['state'] = 'disabled'
super().destroy()
class Pop(FocusFrame):
sizenames = {2: "", 4: "D", 8: "Q"}
def __init__(self, master, gui, index, data, **kw):
tk.Frame.__init__(self, master, **kw)
self.gui = gui
self.index = index
self.size = len(data)
ttk.Label(self, text = f'Popped {self.sizenames[self.size]}WORD').pack(side = 'left')
ttk.Button(self, text = 'X', width = 2, command = self.destroy_confirm).pack(side = 'right')
ttk.Label(self, text = ' ').pack(side = 'right')
vcmd = self.register(self.gui.validate_hex)
if self.size == 2:
ttk.Label(self, text = 'H').pack(side = 'right')
self.word = ttk.Entry(self, width = 6, justify = 'right', validate = 'key', validatecommand = (vcmd, '%S', '%P', '%d', range(0x10000)))
if data[0] != None and data[1] != None: self.word.insert(0, f'{((data[1] << 8) & 0xff) + (data[0] & 0xff):04X}')
self.word.bind('<KeyPress>', self.cap_input)
self.word.pack(side = 'right')
else:
self.bytes = [ttk.Entry(self, width = 2, justify = 'right', validate = 'key', validatecommand = (vcmd, '%S', '%P', '%d', range(0x100))) for i in range(self.size)]
for i in range(self.size - 1, -1, -1):
if data[i] is not None: self.bytes[i].insert(0, f'{data[i]:02X}')
self.bytes[i].bind('<KeyPress>', self.cap_input)
self.bytes[i].pack(side = 'right')
if data != [None]*self.size: self.focusout()
self.bind('<FocusOut>', self.focusout)
def cap_input(self, event):
if event.char.lower() in '0123456789abcdef':
event.widget.insert('end', event.char.upper())
return 'break'
def focusout(self, event = None):
if self.size == 2: self.word.insert(0, '0'*(2-len(self.word.get())))