Heroic-Games-Launcher / HeroicGamesLauncher

A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.
https://heroicgameslauncher.com
GNU General Public License v3.0
8.04k stars 422 forks source link

Allow to pass arguments for scripts to run before and after a game is launched #3691

Closed casasfernando closed 2 months ago

casasfernando commented 6 months ago

Problem description

I need to use a pre-launch/post-exit scripts (https://github.com/Heroic-Games-Launcher/HeroicGamesLauncher/pull/3565 - https://github.com/Heroic-Games-Launcher/HeroicGamesLauncher/issues/2507) with my games to be able to do some work for the games to run correctly in my environment and then clean up after the game exits. At the moment is only possible to select the pre-launch/post-exit script but I couldn't find a way to also pass some arguments to the scripts, like the game name or game prefix path, or any other arbitrary one. Without this I will have to keep separate scripts for each game since all would require a little twist specific to the game.

Feature description

I would like to be able to pass arguments to the scripts so I can only have on pre-launch and one post-launch or even just one script that can run differently accordingly to the arguments passed that would be different if the script is executed before launching the game or after it exits. In terms of implementation it can be something similar to what Windows does with shortcuts, that means full script path in double quotes, followed by arguments. E.g.: "/path/to/pre-launch/script.sh" arg0 arg1 argX Another, I think better, option would be to have something similar to the wrapper command in the same settings page, where there are two fields: wrapper and arguments. In this case it would be script and arguments.

Alternatives

I tried to check if the application is passing any game related information in the environment variables or default arguments when running the scripts but it doesn't. Having some information through environment variables or even default arguments, containing the game name and/or the game prefix path would be a nice to have.

Additional information

No response

casasfernando commented 5 months ago

I've looked into the code and came up with the following patch to pass some additional useful information (game title and executable path) to the pre/post launch script using environment variables. It gets the job done and it is a very small code change that will add more possibilities and convenience for the scripts (e.g: using only one script for several different games and execute different actions depending on the game title or executable file):

diff --git a/src/backend/launcher.ts b/src/backend/launcher.ts
index 2835cfff..5f7f8ad1 100644
--- a/src/backend/launcher.ts
+++ b/src/backend/launcher.ts
@@ -1368,7 +1368,9 @@ async function runScriptForGame(
   scriptPath: string
 ): Promise<boolean | string> {
   return new Promise((resolve, reject) => {
-    const child = spawn(scriptPath, { cwd: gameInfo.install.install_path })
+    var scriptEnv = { GAMEINFO_TITLE: gameInfo.title , GAMEINFO_EXEC: gameInfo.install.executable };
+    Object.assign(scriptEnv, process.env);
+    const child = spawn(scriptPath, { cwd: gameInfo.install.install_path , env: scriptEnv })

     child.stdout.on('data', (data) => {
       appendGamePlayLog(gameInfo, data.toString())

This wouldn't fulfill the feature request completely but it's a good step forward. Would you be interested in a PR?

Thanks

OlegAckbar commented 5 months ago

@casasfernando I have the same issue and I would be interested in such PR.

casasfernando commented 5 months ago

I will submit this and hopefully it will get approved and merged. Fingers crossed.

Morbiuzx commented 2 months ago

I would like to be able to pass arguments to the scripts

I would like that too, in my case I made a script to record the screen or activate an instant replay feature depending on the argument I pass to it, this works nice in lutris or steam, but whenever I try to pass an argument to a script in heroic it stays in "launching" forever.

arielj commented 2 months ago

this was included in the 2.15.0 release