all repos — m12-patcher @ 2aaac7b27ad8359b672aaf1a66f566952a2c118d

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_name):
 19    if preset_name in Constants.PRESETS:
 20        preset = Constants.PRESETS[preset_name]
 21        for key in preset.keys():
 22            getattr(app, key).set(preset[key])
 23        return True
 24    return False
 25
 26def apply_patch(base, ips):
 27    patch = Patch.load(ips)
 28    target = base + '_temp'
 29
 30    with open(base, 'rb') as f_in:
 31        with open(target, 'w+b') as f_out:
 32            f_out.write(patch.apply(f_in.read()))
 33
 34    os.replace(target, base)
 35
 36def show_warning(message):
 37    showwarning(title=Constants.WARNING_TITLE, message=message)
 38    
 39def show_success(message):
 40    showinfo(title=Constants.SUCCESS_TITLE, message=message)
 41
 42def set_progress(app, percent, message):
 43    app.progress.set(percent)
 44    app.status_text.set(message) 
 45
 46def start_patching(app):
 47    set_progress(app, 20, Constants.STATUS_MD5)
 48    delete_list = []
 49    
 50    sel_filenames = {
 51        'font': Constants.ALT_FILENAMES[app.font.get()],
 52        'sprites': Constants.ALT_FILENAMES[app.sprites.get()],
 53        'places': Constants.ALT_FILENAMES[app.places.get()]
 54    }
 55    
 56    sel_patches = {
 57        'palette': Constants.ALT_FILENAMES[app.palette.get()],
 58        'skip_m1': Constants.ALT_FILENAMES[app.skip_m1.get()]
 59    }
 60    
 61    for key in sel_filenames.keys():
 62        sel_list = sel_filenames[key]
 63        def_list = Constants.DEF_FILENAMES[key]
 64
 65        for idx, i in enumerate(sel_list):
 66            original = os.path.join(Constants.PATH_ALT, sel_list[idx])
 67            target = os.path.join(Constants.PATH_TOOLS, def_list[idx])
 68            
 69            copyfile(original, target)
 70            delete_list.append(target)
 71            
 72    target = os.path.join(Constants.PATH_TOOLS, 'test.gba')
 73    copyfile(app.baserom, target)
 74    
 75    set_progress(app, 40, Constants.STATUS_COPIED)
 76    p = subprocess.Popen([Constants.OS_FILENAMES['xkas'], '-o', 'test.gba', 'm12.asm'],
 77                         cwd=Constants.PATH_TOOLS, shell=Constants.OS_SHELL)
 78    p.wait()
 79
 80    set_progress(app, 50, Constants.STATUS_ASSEMBLY)
 81    p = subprocess.Popen([Constants.OS_FILENAMES['insert']],
 82                         cwd=Constants.PATH_TOOLS, shell=Constants.OS_SHELL)
 83    p.wait()
 84       
 85    set_progress(app, 70, Constants.STATUS_INJECTED)
 86    for key in sel_patches.keys():
 87        val = sel_patches[key]
 88        
 89        if val is not None:
 90            path = os.path.join(Constants.PATH_ALT, val)
 91            apply_patch(target, path)
 92        
 93    os.replace(target, Constants.FINAL_ROM_NAME)
 94       
 95    set_progress(app, 90, Constants.STATUS_PATCHED)
 96    for item in delete_list:
 97        os.remove(item)
 98
 99    set_progress(app, 100, Constants.STATUS_CLEANED)
100    show_success(Constants.SUCCESS_CONTENT)