patcher.py (view raw)
1#!/usr/bin/python3
2import os, tkinter, Constants
3from tkinter.filedialog import askopenfilename
4from Functions import check_rom, show_warning, start_patching, apply_preset, set_progress
5from pathlib import Path
6from pygubu import Builder
7
8PROJECT_PATH = Path(__file__).parent
9PROJECT_UI = PROJECT_PATH / "patcher.ui"
10
11
12
13class PatcherApp:
14 def __init__(self, master=None):
15 self.builder = builder = Builder()
16 builder.add_resource_path(PROJECT_PATH)
17 builder.add_from_file(PROJECT_UI)
18 # Main widget
19 self.mainwindow = builder.get_object("main_frame", master)
20 self.apply_button = builder.get_object("apply_button")
21
22 self.browse_path = None
23 self.preset = None
24 self.progress = None
25 self.status_text = None
26 self.font = None
27 self.places = None
28 self.sprites = None
29 self.nes_palette = None
30 self.skip_m1 = None
31 builder.import_variables(
32 self,
33 [
34 "browse_path",
35 "preset",
36 "progress",
37 "status_text",
38 "font",
39 "places",
40 "sprites",
41 "nes_palette",
42 "skip_m1",
43 ],
44 )
45
46 self.preset.trace('w', self.on_change_preset)
47 self.preset.set(Constants.DEF_PRESET[0])
48 self.status_text.set(Constants.STATUS_START)
49
50 baserom = None
51 for file in os.listdir('.'):
52 if file.lower().endswith('.gba'):
53 filename = os.path.join('.', file)
54 baserom = filename if check_rom(filename) else baserom
55 if baserom is not None:
56 self.browse_path.set(baserom)
57
58 builder.connect_callbacks(self)
59
60 def run(self):
61 self.mainwindow.mainloop()
62
63 def on_browse_button(self):
64 fn = askopenfilename(filetypes=[(Constants.VAR_FILEPICKER, '*.gba')])
65 self.browse_path.set(fn)
66
67 def trace_f(self, var, index, mode):
68 preset = var.get()
69 if apply_preset(self, preset):
70 set_progress(self, 0, Constants.STATUS_PRESET)
71
72 def on_change_preset(self, *arg):
73 preset = self.preset.get()
74 if apply_preset(self, preset):
75 set_progress(self, 0, f'{Constants.STATUS_PRESET}: {preset}.')
76
77 def on_apply_button(self):
78 self.apply_button['state'] = 'disabled'
79
80 baserom_temp = self.browse_path.get()
81
82 if not check_rom(baserom_temp):
83 show_warning(Constants.WARNING_MD5_MISMATCH)
84 self.apply_button['state'] = 'normal'
85 return
86 self.baserom = baserom_temp
87 start_patching(self)
88 self.apply_button['state'] = 'enabled'
89 set_progress(self, 0, Constants.STATUS_START)
90
91def main():
92 if not os.path.isdir(Constants.PATH_TOOLS):
93 show_warning(Constants.WARNING_EXTRACT)
94 return
95
96 root = tkinter.Tk()
97 root.title(Constants.VAR_WINDOW_TITLE)
98 root.resizable(False, False)
99 app = PatcherApp(root)
100 app.run()
101
102if __name__ == "__main__":
103 main()