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
19 self.browse_path = None
20 self.preset = None
21 self.font = None
22 self.places = None
23 self.sprites = None
24 self.palette = None
25 self.skip_m1 = None
26 self.progress = None
27 self.status_text = None
28 builder.import_variables(
29 self,
30 [
31 "browse_path",
32 "preset",
33 "font",
34 "places",
35 "sprites",
36 "palette",
37 "skip_m1",
38 "progress",
39 "status_text",
40 ],
41 )
42 apply_preset(self, next(iter(Constants.PRESETS.items()))[1])
43 self.status_text.set(Constants.STATUS_START)
44
45 self.apply_button = builder.get_object("apply_button")
46
47 baserom = None
48 for file in os.listdir('.'):
49 if file.lower().endswith('.gba'):
50 filename = os.path.join('.', file)
51 baserom = filename if check_rom(filename) else baserom
52 if baserom is not None:
53 self.browse_path.set(baserom)
54
55 builder.connect_callbacks(self)
56
57 def run(self):
58 self.mainwindow.mainloop()
59
60 def on_browse_button(self):
61 fn = askopenfilename(filetypes=[(Constants.VAR_FILEPICKER, '*.gba')])
62 self.browse_path.set(fn)
63
64 def on_change_preset(self, option):
65 preset = Constants.PRESETS[option]
66 apply_preset(self, preset)
67
68 set_progress(self, 0, Constants.STATUS_PRESET)
69
70 def on_apply_button(self):
71 self.apply_button['state'] = 'disabled'
72
73 baserom_temp = self.browse_path.get()
74
75 if not check_rom(baserom_temp):
76 show_warning(Constants.WARNING_MD5_MISMATCH)
77 self.apply_button['state'] = 'normal'
78 return
79 self.baserom = baserom_temp
80 start_patching(self)
81
82def main():
83 if not os.path.isdir(Constants.PATH_TOOLS):
84 show_warning(Constants.WARNING_EXTRACT)
85 return
86
87 root = tkinter.Tk()
88 root.title(Constants.VAR_WINDOW_TITLE)
89 app = PatcherApp(root)
90 app.run()
91
92if __name__ == "__main__":
93 main()