From 56e57de574e559e429a57b0e38f3d0f330abebff Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Sun, 26 Apr 2020 15:14:30 +0200 Subject: [PATCH] store last used adjuster settings and use them for patching might use them as gui defaults later --- Adjuster.py | 11 +++++++---- AdjusterMain.py | 12 +++++++----- Gui.py | 6 ++++-- MultiClient.py | 20 ++++++++++++++++++++ Patch.py | 17 ++++++++++------- Utils.py | 2 +- 6 files changed, 49 insertions(+), 19 deletions(-) diff --git a/Adjuster.py b/Adjuster.py index 570bcef9..1c4c4e84 100755 --- a/Adjuster.py +++ b/Adjuster.py @@ -47,17 +47,20 @@ def main(): # ToDo: Validate files further than mere existance if not os.path.isfile(args.rom): - input('Could not find valid rom for patching at expected path %s. Please run with -h to see help for further information. \nPress Enter to exit.' % args.rom) + input( + 'Could not find valid rom for patching at expected path %s. Please run with -h to see help for further information. \nPress Enter to exit.' % args.rom) sys.exit(1) if args.sprite is not None and not os.path.isfile(args.sprite) and not get_sprite_from_name(args.sprite): input('Could not find link sprite sheet at given location. \nPress Enter to exit.') sys.exit(1) # set up logger - loglevel = {'error': logging.ERROR, 'info': logging.INFO, 'warning': logging.WARNING, 'debug': logging.DEBUG}[args.loglevel] + loglevel = {'error': logging.ERROR, 'info': logging.INFO, 'warning': logging.WARNING, 'debug': logging.DEBUG}[ + args.loglevel] logging.basicConfig(format='%(message)s', level=loglevel) - - adjust(args=args) + args, path = adjust(args=args) + from Utils import persistent_store + persistent_store("adjuster", "last_settings", args) if __name__ == '__main__': main() diff --git a/AdjusterMain.py b/AdjusterMain.py index 64b1042e..49d03c17 100644 --- a/AdjusterMain.py +++ b/AdjusterMain.py @@ -17,13 +17,15 @@ def adjust(args): baserom = LocalRom(args.baserom, patch=True) rom.orig_buffer = baserom.orig_buffer else: - raise RuntimeError('Provided Rom is not a valid Link to the Past Randomizer Rom. Please provide one for adjusting.') + raise RuntimeError( + 'Provided Rom is not a valid Link to the Past Randomizer Rom. Please provide one for adjusting.') - apply_rom_settings(rom, args.heartbeep, args.heartcolor, args.quickswap, args.fastmenu, args.disablemusic, args.sprite, args.ow_palettes, args.uw_palettes) - - rom.write_to_file(output_path(f'{os.path.basename(args.rom)[:-4]}_adjusted.sfc')) + apply_rom_settings(rom, args.heartbeep, args.heartcolor, args.quickswap, args.fastmenu, args.disablemusic, + args.sprite, args.ow_palettes, args.uw_palettes) + path = output_path(f'{os.path.basename(args.rom)[:-4]}_adjusted.sfc') + rom.write_to_file(path) logger.info('Done. Enjoy.') logger.debug('Total Time: %s', time.perf_counter() - start) - return args + return args, path diff --git a/Gui.py b/Gui.py index 67a5475b..75c969d1 100755 --- a/Gui.py +++ b/Gui.py @@ -623,12 +623,14 @@ def guiMain(args=None): guiargs.baserom = romVar.get() guiargs.sprite = sprite try: - adjust(args=guiargs) + guiargs, path = adjust(args=guiargs) except Exception as e: logging.exception(e) - messagebox.showerror(title="Error while creating seed", message=str(e)) + messagebox.showerror(title="Error while adjusting Rom", message=str(e)) else: messagebox.showinfo(title="Success", message="Rom patched successfully") + from Utils import persistent_store + persistent_store("adjuster", "last_settings", guiargs) adjustButton = Button(bottomFrame2, text='Adjust Rom', command=adjustRom) diff --git a/MultiClient.py b/MultiClient.py index 155aa882..35de6e82 100644 --- a/MultiClient.py +++ b/MultiClient.py @@ -1089,9 +1089,29 @@ async def main(): if args.diff_file: import Patch + logging.info("Patch file was supplied. Creating sfc rom..") meta, romfile = Patch.create_rom_file(args.diff_file) args.connect = meta["server"] logging.info(f"Wrote rom file to {romfile}") + adjuster_settings = Utils.persistent_load().get("adjuster", {}).get("last_settings", {}) + if adjuster_settings: + import pprint + adjuster_settings.rom = romfile + adjuster_settings.baserom = Patch.get_base_rom_path() + whitelist = {"disablemusic", "fastmenu", "heartbeep", "heartcolor", "ow_palettes", "quickswap", + "uw_palettes"} + printed_options = {name: value for name, value in vars(adjuster_settings).items() if name in whitelist} + sprite = getattr(adjuster_settings, "sprite", None) + if sprite: + printed_options["sprite"]: adjuster_settings.sprite.name + adjust_wanted = input(f"Last used adjuster settings were found. Would you like to apply these? \n" + f"{pprint.pformat(printed_options)}\n" + f"Enter yes or no: ") + if adjust_wanted and adjust_wanted.startswith("y"): + import AdjusterMain + _, romfile = AdjusterMain.adjust(adjuster_settings) + else: + logging.info("Skipping post-patch adjustment") asyncio.create_task(run_game(romfile)) ctx = Context(args.snes, args.connect, args.password, args.founditems) diff --git a/Patch.py b/Patch.py index afff7b0c..c6fbb34f 100644 --- a/Patch.py +++ b/Patch.py @@ -12,17 +12,19 @@ from typing import Tuple, Optional import Utils from Rom import JAP10HASH, read_rom -base_rom_bytes = None +def get_base_rom_path(file_name: str = "") -> str: + options = Utils.get_options() + if not file_name: + file_name = options["general_options"]["rom_file"] + if not os.path.exists(file_name): + file_name = Utils.local_path(file_name) + return file_name def get_base_rom_bytes(file_name: str = "") -> bytes: - global base_rom_bytes + base_rom_bytes = getattr(get_base_rom_bytes, "base_rom_bytes", None) if not base_rom_bytes: - options = Utils.get_options() - if not file_name: - file_name = options["general_options"]["rom_file"] - if not os.path.exists(file_name): - file_name = Utils.local_path(file_name) + file_name = get_base_rom_path(file_name) base_rom_bytes = bytes(read_rom(open(file_name, "rb"))) basemd5 = hashlib.md5() @@ -30,6 +32,7 @@ def get_base_rom_bytes(file_name: str = "") -> bytes: if JAP10HASH != basemd5.hexdigest(): raise Exception('Supplied Base Rom does not match known MD5 for JAP(1.0) release. ' 'Get the correct game and version, then dump it') + get_base_rom_bytes.base_rom_bytes = base_rom_bytes return base_rom_bytes diff --git a/Utils.py b/Utils.py index 96eeb585..7081cafd 100644 --- a/Utils.py +++ b/Utils.py @@ -221,7 +221,7 @@ def persistent_store(category, key, value): f.write(dump(storage)) -def persistent_load(): +def persistent_load() -> typing.Dict[dict]: path = local_path("_persistent_storage.yaml") storage: dict = {} if os.path.exists(path):