all repos — m12-patcher @ dd1011c2941c816701ba774ad5c727c6d7ff84b1

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