store last used adjuster settings and use them for patching

might use them as gui defaults later
This commit is contained in:
Fabian Dill 2020-04-26 15:14:30 +02:00
parent f380542bab
commit 56e57de574
6 changed files with 49 additions and 19 deletions

View File

@ -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()

View File

@ -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

6
Gui.py
View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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):