[cli/core/models] Add config option for pre-launch command

This commit is contained in:
derrod 2022-01-09 14:21:11 +01:00
parent 4ab0c99a0f
commit a25de242d9
4 changed files with 64 additions and 0 deletions

View file

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

View file

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

View file

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

View file

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