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
11class PatcherApp:
12 def __init__(self, master=None):
13 self.builder = builder = Builder()
14 builder.add_resource_path(PROJECT_PATH)
15 builder.add_from_file(PROJECT_UI)
16 # Main widget
17 self.mainwindow = builder.get_object("main_frame", master)
18 self.apply_button = builder.get_object("apply_button")
19 self.preset_combo = builder.get_object("preset_combo")
20
21 self.browse_path = None
22 self.preset = None
23 self.progress = None
24 self.status_text = None
25 self.font = None
26 self.places = None
27 self.sprites = None
28 self.nes_palette = None
29 self.skip_m1 = None
30 builder.import_variables(
31 self,
32 [
33 "browse_path",
34 "preset",
35 "progress",
36 "status_text",
37 "font",
38 "places",
39 "sprites",
40 "nes_palette",
41 "skip_m1",
42 ],
43 )
44
45 for preset in Constants.PRESETS.keys():
46 self.preset_combo['values'] = (*self.preset_combo['values'], preset)
47 self.preset.trace('w', self.on_change_preset)
48 self.preset.set(Constants.DEF_PRESET[0])
49 self.status_text.set(Constants.STATUS_START)
50
51 baserom = None
52 for file in os.listdir('.'):
53 if file.lower().endswith('.gba'):
54 filename = os.path.join('.', file)
55 baserom = filename if check_rom(filename) else baserom
56 if baserom is not None:
57 self.browse_path.set(baserom)
58
59 builder.connect_callbacks(self)
60
61 def run(self):
62 self.mainwindow.mainloop()
63
64 def on_browse_button(self):
65 fn = askopenfilename(filetypes=[(Constants.VAR_FILEPICKER, '*.gba')], initialdir='.')
66 self.browse_path.set(fn)
67
68 def on_change_preset(self, *arg):
69 preset = self.preset.get()
70 if apply_preset(self, preset):
71 set_progress(self, 0, f'{Constants.STATUS_PRESET}: {preset}.')
72
73 def on_apply_button(self):
74 self.apply_button['state'] = 'disabled'
75
76 baserom_temp = self.browse_path.get()
77
78 if not check_rom(baserom_temp):
79 show_warning(Constants.WARNING_MD5_MISMATCH)
80 self.apply_button['state'] = 'normal'
81 return
82 self.baserom = baserom_temp
83 start_patching(self)
84 self.apply_button['state'] = 'enabled'
85 set_progress(self, 0, Constants.STATUS_START)
86
87def main():
88 if not os.path.isdir(Constants.PATH_TOOLS):
89 show_warning(Constants.WARNING_EXTRACT)
90 return
91
92 root = tkinter.Tk()
93 root.title(Constants.VAR_WINDOW_TITLE)
94 root.iconphoto(False, tkinter.PhotoImage(file=Constants.PATH_ICON))
95 root.resizable(False, False)
96 app = PatcherApp(root)
97 app.run()
98
99if __name__ == "__main__":
100 main()