From b707619aad6bf557559ad0a619cf716c51676747 Mon Sep 17 00:00:00 2001 From: Bryce Wilson Date: Wed, 18 Oct 2023 22:07:15 -0700 Subject: [PATCH] BizHawkClient: Add autostart setting (#2322) --- settings.py | 20 ++++++++++++++++++++ worlds/_bizhawk/context.py | 21 +++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/settings.py b/settings.py index a7dcbbf8..acae8609 100644 --- a/settings.py +++ b/settings.py @@ -694,6 +694,25 @@ does nothing if not found snes_rom_start: Union[SnesRomStart, bool] = True +class BizHawkClientOptions(Group): + class EmuHawkPath(UserFilePath): + """ + The location of the EmuHawk you want to auto launch patched ROMs with + """ + is_exe = True + description = "EmuHawk Executable" + + class RomStart(str): + """ + Set this to true to autostart a patched ROM in BizHawk with the connector script, + to false to never open the patched rom automatically, + or to a path to an external program to open the ROM file with that instead. + """ + + emuhawk_path: EmuHawkPath = EmuHawkPath(None) + rom_start: Union[RomStart, bool] = True + + # Top-level group with lazy loading of worlds class Settings(Group): @@ -701,6 +720,7 @@ class Settings(Group): server_options: ServerOptions = ServerOptions() generator: GeneratorOptions = GeneratorOptions() sni_options: SNIOptions = SNIOptions() + bizhawkclient_options: BizHawkClientOptions = BizHawkClientOptions() _filename: Optional[str] = None diff --git a/worlds/_bizhawk/context.py b/worlds/_bizhawk/context.py index 6e53b370..46533427 100644 --- a/worlds/_bizhawk/context.py +++ b/worlds/_bizhawk/context.py @@ -5,6 +5,7 @@ checking or launching the client, otherwise it will probably cause circular impo import asyncio +import subprocess import traceback from typing import Any, Dict, Optional @@ -146,8 +147,24 @@ async def _game_watcher(ctx: BizHawkClientContext): async def _run_game(rom: str): - import webbrowser - webbrowser.open(rom) + import os + auto_start = Utils.get_settings().bizhawkclient_options.rom_start + + if auto_start is True: + emuhawk_path = Utils.get_settings().bizhawkclient_options.emuhawk_path + subprocess.Popen([emuhawk_path, "--lua=data/lua/connector_bizhawk_generic.lua", os.path.realpath(rom)], + cwd=Utils.local_path("."), + stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) + elif isinstance(auto_start, str): + import shlex + + subprocess.Popen([*shlex.split(auto_start), os.path.realpath(rom)], + cwd=Utils.local_path("."), + stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) async def _patch_and_run_game(patch_file: str):