From 910964dccbdcf2191801a50de207bafca55e3ca6 Mon Sep 17 00:00:00 2001 From: alwaysintreble Date: Mon, 29 Jul 2024 02:54:51 -0500 Subject: [PATCH] use "proper" args argparsing and clean up uri handling --- CommonClient.py | 4 ++-- Launcher.py | 32 +++++++++++++------------------- worlds/LauncherComponents.py | 8 ++++---- worlds/messenger/client_setup.py | 4 ++-- 4 files changed, 21 insertions(+), 27 deletions(-) diff --git a/CommonClient.py b/CommonClient.py index 09937e4b9ab8..396fd16a8d14 100644 --- a/CommonClient.py +++ b/CommonClient.py @@ -994,7 +994,7 @@ def get_base_parser(description: typing.Optional[str] = None): return parser -def run_as_textclient(): +def run_as_textclient(*args): class TextContext(CommonContext): # Text Mode to use !hint and such with games that have no text entry tags = CommonContext.tags | {"TextOnly"} @@ -1033,7 +1033,7 @@ async def main(args): parser = get_base_parser(description="Gameless Archipelago Client, for text interfacing.") parser.add_argument('--name', default=None, help="Slot Name to connect as.") parser.add_argument("url", nargs="?", help="Archipelago connection url") - args = parser.parse_args() + args = parser.parse_args(args) if args.url: url = urllib.parse.urlparse(args.url) diff --git a/Launcher.py b/Launcher.py index bae682ccbfe2..2dfdd42f9bd0 100644 --- a/Launcher.py +++ b/Launcher.py @@ -108,7 +108,7 @@ def update_settings(): ]) -def handle_uri(path: str) -> Tuple[Union[None, str], Union[None, Component]]: +def handle_uri(path: str, launch_args: Tuple[str, ...]) -> None: url = urllib.parse.urlparse(path) queries = urllib.parse.parse_qs(url.query) client_component = None @@ -138,20 +138,14 @@ def build(self): text_client_button = Button( text=text_client_component.display_name, - on_release=lambda *args: launch(get_exe(text_client_component), True) + on_release=lambda *args: run_component(text_client_component, *launch_args) ) button_row.add_widget(text_client_button) if client_component is not None: - def launch_component(*args) -> None: - if client_component.func: - client_component.func() - else: - launch(get_exe(client_component), True) - game_client_button = Button( text=client_component.display_name, - on_release=launch_component + on_release=lambda *args: run_component(client_component, *launch_args) ) button_row.add_widget(game_client_button) @@ -161,15 +155,10 @@ def launch_component(*args) -> None: Popup().run() - # prevents launcher from trying to start up its gui - return path, None - def identify(path: Union[None, str]) -> Tuple[Union[None, str], Union[None, Component]]: if path is None: return None, None - if path.startswith("archipelago://"): - return handle_uri(path) for component in components: if component.handles_file(path): return path, component @@ -359,14 +348,18 @@ def main(args: Optional[Union[argparse.Namespace, dict]] = None): elif not args: args = {} - if args.get("Patch|Game|Component", None) is not None: - file, component = identify(args["Patch|Game|Component"]) + path = args.get("Patch|Game|Component|url", None) + if path is not None: + if path.startswith("archipelago://"): + handle_uri(path, args.get("args", ())) + return + file, component = identify(path) if file: args['file'] = file if component: args['component'] = component if not component: - logging.warning(f"Could not identify Component responsible for {args['Patch|Game|Component']}") + logging.warning(f"Could not identify Component responsible for {path}") if args["update_settings"]: update_settings() @@ -386,8 +379,9 @@ def main(args: Optional[Union[argparse.Namespace, dict]] = None): run_group = parser.add_argument_group("Run") run_group.add_argument("--update_settings", action="store_true", help="Update host.yaml and exit.") - run_group.add_argument("Patch|Game|Component", type=str, nargs="?", - help="Pass either a patch file, a generated game or the name of a component to run.") + run_group.add_argument("Patch|Game|Component|url", type=str, nargs="?", + help="Pass either a patch file, a generated game, the component name to run, or a url to " + "connect with.") run_group.add_argument("args", nargs="*", help="Arguments to pass to component.") main(parser.parse_args()) diff --git a/worlds/LauncherComponents.py b/worlds/LauncherComponents.py index fa3993226854..38a9f14bd0b9 100644 --- a/worlds/LauncherComponents.py +++ b/worlds/LauncherComponents.py @@ -61,10 +61,10 @@ def __repr__(self): processes = weakref.WeakSet() -def launch_subprocess(func: Callable, name: str = None): +def launch_subprocess(func: Callable, name: str = None, args: Tuple[str, ...] = ()): global processes import multiprocessing - process = multiprocessing.Process(target=func, name=name) + process = multiprocessing.Process(target=func, name=name, args=args) process.start() processes.add(process) @@ -83,9 +83,9 @@ def __call__(self, path: str) -> bool: return False -def launch_textclient(): +def launch_textclient(*args): import CommonClient - launch_subprocess(CommonClient.run_as_textclient, "TextClient") + launch_subprocess(CommonClient.run_as_textclient, "TextClient", args) def _install_apworld(apworld_src: str = "") -> Optional[Tuple[pathlib.Path, pathlib.Path]]: diff --git a/worlds/messenger/client_setup.py b/worlds/messenger/client_setup.py index b86ff4c7f4a6..6bff78df364d 100644 --- a/worlds/messenger/client_setup.py +++ b/worlds/messenger/client_setup.py @@ -18,7 +18,7 @@ MOD_URL = "https://api.github.com/repos/alwaysintreble/TheMessengerRandomizerModAP/releases/latest" -def launch_game() -> None: +def launch_game(*args) -> None: """Check the game installation, then launch it""" def courier_installed() -> bool: """Check if Courier is installed""" @@ -154,7 +154,7 @@ def available_mod_update(latest_version: str) -> bool: parser = argparse.ArgumentParser(description="Messenger Client Launcher") parser.add_argument("url", type=str, nargs="?", help="Archipelago Webhost uri to auto connect to.") - args = parser.parse_args() + args = parser.parse_args(args) if not is_windows: if args.url: open_file(f"steam://rungameid/764790//{args.url}/")