all repos — m12-patcher @ 2aaac7b27ad8359b672aaf1a66f566952a2c118d

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.font = None
 25        self.places = None
 26        self.sprites = None
 27        self.palette = None
 28        self.skip_m1 = None
 29        self.progress = None
 30        self.status_text = None
 31        builder.import_variables(
 32            self,
 33            [
 34                "browse_path",
 35                "preset",
 36                "font",
 37                "places",
 38                "sprites",
 39                "palette",
 40                "skip_m1",
 41                "progress",
 42                "status_text",
 43            ],
 44        )
 45
 46        self.preset.trace('w', self.on_change_preset)
 47        self.preset.set(next(iter(Constants.PRESETS.keys())))
 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        if apply_preset(self, self.preset.get()):
 74            set_progress(self, 0, Constants.STATUS_PRESET)
 75
 76    def on_apply_button(self):
 77        self.apply_button['state'] = 'disabled'
 78        
 79        baserom_temp = self.browse_path.get()
 80        
 81        if not check_rom(baserom_temp):
 82            show_warning(Constants.WARNING_MD5_MISMATCH)
 83            self.apply_button['state'] = 'normal'
 84            return
 85        self.baserom = baserom_temp
 86        start_patching(self)
 87        self.apply_button['state'] = 'enabled'
 88        set_progress(self, 0, Constants.STATUS_START)
 89
 90def main():
 91    if not os.path.isdir(Constants.PATH_TOOLS):
 92        show_warning(Constants.WARNING_EXTRACT)
 93        return
 94    
 95    root = tkinter.Tk()
 96    root.title(Constants.VAR_WINDOW_TITLE)
 97    root.resizable(False, False)
 98    app = PatcherApp(root)
 99    app.run()
100
101if __name__ == "__main__":
102    main()