all repos — m12-patcher @ b84de75f35171c4c5072063225f2e8270f9dfa49

Functions.py (view raw)

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