From a25de242d964342cbd510c53c0cfbcaba7c1654b Mon Sep 17 00:00:00 2001 From: derrod Date: Sun, 9 Jan 2022 14:21:11 +0100 Subject: [PATCH] [cli/core/models] Add config option for pre-launch command --- README.md | 4 ++++ legendary/cli.py | 40 ++++++++++++++++++++++++++++++++++++++++ legendary/core.py | 18 ++++++++++++++++++ legendary/models/game.py | 2 ++ 4 files changed, 64 insertions(+) diff --git a/README.md b/README.md index f128987..f4801ec 100644 --- a/README.md +++ b/README.md @@ -740,6 +740,10 @@ override_exe = relative/path/to/file.exe disable_sdl = true [AppName3] +; Command to run before launching the gmae +pre_launch_command = /path/to/script.sh +; Whether or not to wait for command to finish running +pre_launch_wait = false ; (macOS) override crossover settings crossover_app = /Applications/CrossOver Nightly.app crossover_bottle = SomethingElse diff --git a/legendary/cli.py b/legendary/cli.py index 36f38d6..d78f862 100644 --- a/legendary/cli.py +++ b/legendary/cli.py @@ -658,6 +658,8 @@ class LegendaryCLI: if args.dry_run: logger.info(f'Not Launching {app_name} (dry run)') + if params.pre_launch_command: + logger.info(f'Pre-Launch Command: {params.pre_launch_command}') logger.info(f'Launch parameters: {shlex.join(full_params)}') logger.info(f'Working directory: {params.working_directory}') if params.environment: @@ -665,6 +667,16 @@ class LegendaryCLI: f'{k}={v}' for k, v in params.environment.items()))) else: logger.info(f'Launching {app_name}...') + if params.pre_launch_command: + try: + logger.debug(f'Running pre-launch command: {params.pre_launch_command}') + p = subprocess.Popen(shlex.split(params.pre_launch_command), env=full_env) + if params.pre_launch_wait: + logger.debug('Waiting for pre-launch command to finish...') + p.wait() + except Exception as e: + logger.warning(f'Pre-launch command failed: {e!r}') + logger.debug(f'Launch parameters: {shlex.join(full_params)}') logger.debug(f'Working directory: {params.working_directory}') if params.environment: @@ -695,9 +707,23 @@ class LegendaryCLI: return self._print_json(dict(uri=origin_uri), args.pretty_json) if os.name == 'nt': + cmd, wait_for_exit = self.core.get_pre_launch_command(args.app_name) + if args.dry_run: + if cmd: + logger.info(f'Pre-launch command: {cmd}') logger.info(f'Origin URI: {origin_uri}') else: + if cmd: + try: + logger.debug(f'Running pre-launch command: {cmd}') + p = subprocess.Popen(shlex.split(cmd)) + if wait_for_exit: + logger.debug('Waiting for pre-launch command to finish...') + p.wait() + except Exception as e: + logger.warning(f'Pre-launch command failed: {e!r}') + logger.debug(f'Opening Origin URI: {origin_uri}') webbrowser.open(origin_uri) return @@ -709,6 +735,8 @@ class LegendaryCLI: crossover_app=args.crossover_app) env = self.core.get_app_environment(args.app_name, wine_pfx=args.wine_pfx, cx_bottle=args.crossover_bottle) + cmd, wait_for_exit = self.core.get_pre_launch_command(args.app_name) + full_env = os.environ.copy() full_env.update(env) @@ -732,8 +760,20 @@ class LegendaryCLI: command.append(origin_uri) if args.dry_run: + if cmd: + logger.info(f'Pre-launch command: {cmd}') logger.info(f'Origin launch command: {shlex.join(command)}') else: + if cmd: + try: + logger.debug(f'Running pre-launch command: {cmd}') + p = subprocess.Popen(shlex.split(cmd), env=full_env) + if wait_for_exit: + logger.debug('Waiting for pre-launch command to finish...') + p.wait() + except Exception as e: + logger.warning(f'Pre-launch command failed: {e!r}') + logger.debug(f'Opening Origin URI with command: {shlex.join(command)}') subprocess.Popen(command, env=full_env) diff --git a/legendary/core.py b/legendary/core.py index 1ab3979..89cd1d5 100644 --- a/legendary/core.py +++ b/legendary/core.py @@ -642,6 +642,19 @@ class LegendaryCore: return _cmd + def get_pre_launch_command(self, app_name=''): + if app_name: + pre_launch_command = self.lgd.config.get(app_name, 'pre_launch_command', fallback=None) + pre_launch_wait = self.lgd.config.getboolean(app_name, 'pre_launch_wait', fallback=False) + + if pre_launch_command: + return pre_launch_command, pre_launch_wait + + # try default if no per-game override exists + pre_launch_command = self.lgd.config.get('default', 'pre_launch_command', fallback=None) + pre_launch_wait = self.lgd.config.getboolean('default', 'pre_launch_wait', fallback=False) + return pre_launch_command, pre_launch_wait + def get_launch_parameters(self, app_name: str, offline: bool = False, user: str = None, extra_args: list = None, wine_bin: str = None, wine_pfx: str = None, @@ -681,6 +694,11 @@ class LegendaryCore: cx_bottle=crossover_bottle, disable_wine=disable_wine) ) + cmd, wait = self.get_pre_launch_command(app_name) + if cmd: + params.pre_launch_command = cmd + params.pre_launch_wait = wait + if install.launch_parameters: try: params.game_parameters.extend(shlex.split(install.launch_parameters, posix=False)) diff --git a/legendary/models/game.py b/legendary/models/game.py index bf58536..4c527d5 100644 --- a/legendary/models/game.py +++ b/legendary/models/game.py @@ -222,3 +222,5 @@ class LaunchParameters: # user and environment supplied options user_parameters: list = field(default_factory=list) environment: dict = field(default_factory=dict) + pre_launch_command: str = '' + pre_launch_wait: bool = False