all repos — m12-patcher @ 53d9b4aabf9ee4d4a4f7c1377e022831a19e4ac3

Functions.py (view raw)

 1#!/usr/bin/python3
 2from hashlib import md5
 3from tkinter.messagebox import showwarning, showinfo
 4from ips_util import Patch
 5from shutil import copyfile
 6import os, subprocess, Constants
 7
 8def check_rom(filename):
 9    if filename == '':
10        return False
11    with open(filename, 'rb') as f:
12        file_hash = md5()
13        while chunk := f.read(8192):
14            file_hash.update(chunk)
15
16    return file_hash.hexdigest() == 'f41e36204356974c94fabf7d144dd32a'
17
18def apply_preset(app, preset):
19    for key in preset.keys():
20            getattr(app, key).set(preset[key])
21
22def apply_patch(base, ips):
23    patch = Patch.load(ips)
24    target = base + '_temp'
25
26    with open(base, 'rb') as f_in:
27        with open(target, 'w+b') as f_out:
28            f_out.write(patch.apply(f_in.read()))
29
30    os.replace(target, base)
31
32def show_warning(message):
33    showwarning(title=Constants.WARNING_TITLE, message=message)
34    
35def show_success(message):
36    showinfo(title=Constants.SUCCESS_TITLE, message=message)
37
38def set_progress(app, percent, message):
39    app.progress.set(percent)
40    app.progress_text.set(message) 
41
42def start_patching(app):
43    set_progress(app, 20, Constants.STATUS_MD5)
44    delete_list = []
45    
46    sel_filenames = {
47        'font': Constants.ALT_FILENAMES[app.font.get()],
48        'sprites': Constants.ALT_FILENAMES[app.sprites.get()],
49        'places': Constants.ALT_FILENAMES[app.places.get()]
50    }
51    
52    sel_patches = {
53        'palette': Constants.ALT_FILENAMES[app.palette.get()],
54        'skip_m1': Constants.ALT_FILENAMES[app.skip_m1.get()]
55    }
56    
57    for key in sel_filenames.keys():
58        sel_list = sel_filenames[key]
59        def_list = Constants.DEF_FILENAMES[key]
60
61        for idx, i in enumerate(sel_list):
62            original = os.path.join(Constants.PATH_ALT, sel_list[idx])
63            target = os.path.join(Constants.PATH_TOOLS, def_list[idx])
64            
65            copyfile(original, target)
66            delete_list.append(target)
67            
68    target = os.path.join(Constants.PATH_TOOLS, 'test.gba')
69    copyfile(app.baserom, target)
70    
71    set_progress(app, 40, Constants.STATUS_COPIED)
72    p = subprocess.Popen([Constants.OS_FILENAMES['xkas'], '-o', 'test.gba', 'm12.asm'],
73                         cwd=Constants.PATH_TOOLS, shell=Constants.OS_SHELL)
74    p.wait()
75
76    set_progress(app, 50, Constants.STATUS_ASSEMBLY)
77    p = subprocess.Popen([Constants.OS_FILENAMES['insert']],
78                         cwd=Constants.PATH_TOOLS, shell=Constants.OS_SHELL)
79    p.wait()
80       
81    set_progress(app, 70, Constants.STATUS_INJECTED)
82    for key in sel_patches.keys():
83        val = sel_patches[key]
84        
85        if val is not None:
86            path = os.path.join(Constants.PATH_ALT, val)
87            apply_patch(target, path)
88        
89    os.replace(target, Constants.FINAL_ROM_NAME)
90       
91    set_progress(app, 90, Constants.STATUS_PATCHED)
92    for item in delete_list:
93        os.remove(item)
94
95    set_progress(app, 100, Constants.STATUS_CLEANED)
96    show_success(Constants.SUCCESS_CONTENT)
97