diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-11-03 18:24:34 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-11-03 18:24:34 -0500 |
commit | df7e814286641c27a64cf354c15d69ddcfae062d (patch) | |
tree | c872ee47e71f2efd7b90d45c4020872e10de71d0 | |
parent | f386df7e4d245254ca46afb3d152c600fd6035f9 (diff) | |
download | SMAPI-df7e814286641c27a64cf354c15d69ddcfae062d.tar.gz SMAPI-df7e814286641c27a64cf354c15d69ddcfae062d.tar.bz2 SMAPI-df7e814286641c27a64cf354c15d69ddcfae062d.zip |
add support for using environment variables instead of command-line arguments (#665)
-rw-r--r-- | docs/release-notes.md | 1 | ||||
-rw-r--r-- | docs/technical/smapi.md | 15 | ||||
-rw-r--r-- | src/SMAPI/Program.cs | 28 |
3 files changed, 31 insertions, 13 deletions
diff --git a/docs/release-notes.md b/docs/release-notes.md index b31950ed..47e12f83 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -48,6 +48,7 @@ For modders: * Now detects XNB mods more accurately, and consolidates multi-folder XNB mods in logged messages. * SMAPI now automatically removes invalid content when loading a save to prevent crashes. A warning is shown in-game when this happens. This applies for locations and NPCs. * Added support for configuring console colors via `smapi-internal/config.json` (intended for players with unusual consoles). + * Added support for specifying SMAPI command-line arguments as environment variables for Linux/Mac compatibility. * Improved launch script compatibility on Linux (thanks to kurumushi and toastal!). * Save Backup now works in the background, to avoid affecting startup time for players with a large number of saves. * The installer now recognises custom game paths stored in `stardewvalley.targets`. diff --git a/docs/technical/smapi.md b/docs/technical/smapi.md index f6994ba3..96f7dff5 100644 --- a/docs/technical/smapi.md +++ b/docs/technical/smapi.md @@ -40,14 +40,25 @@ argument | purpose `--uninstall` | Preselects the uninstall action, skipping the prompt asking what the user wants to do. `--game-path "path"` | Specifies the full path to the folder containing the Stardew Valley executable, skipping automatic detection and any prompt to choose a path. If the path is not valid, the installer displays an error. -SMAPI itself recognises two arguments, but these are intended for internal use or testing and may -change without warning. +SMAPI itself recognises two arguments **on Windows only**, but these are intended for internal use +or testing and may change without warning. On Linux/Mac, see _environment variables_ below. argument | purpose -------- | ------- `--no-terminal` | SMAPI won't write anything to the console window. (Messages will still be written to the log file.) `--mods-path` | The path to search for mods, if not the standard `Mods` folder. This can be a path relative to the game folder (like `--mods-path "Mods (test)"`) or an absolute path. +### Environment variables +The above SMAPI arguments don't work on Linux/Mac due to the way the game launcher works. You can +set temporary environment variables instead. For example: +> SMAPI_MODS_PATH="Mods (multiplayer)" /path/to/StardewValley + +environment variable | purpose +-------------------- | ------- +`SMAPI_NO_TERMINAL` | Equivalent to `--no-terminal` above. +`SMAPI_MODS_PATH` | Equivalent to `--mods-path` above. + + ### Compile flags SMAPI uses a small number of conditional compilation constants, which you can set by editing the `<DefineConstants>` element in `SMAPI.csproj`. Supported constants: diff --git a/src/SMAPI/Program.cs b/src/SMAPI/Program.cs index 2d143439..05d7ff66 100644 --- a/src/SMAPI/Program.cs +++ b/src/SMAPI/Program.cs @@ -116,21 +116,27 @@ namespace StardewModdingAPI /// <remarks>This method is separate from <see cref="Main"/> because that can't contain any references to assemblies loaded by <see cref="CurrentDomain_AssemblyResolve"/> (e.g. via <see cref="Constants"/>), or Mono will incorrectly show an assembly resolution error before assembly resolution is set up.</remarks> private static void Start(string[] args) { - // get flags from arguments - bool writeToConsole = !args.Contains("--no-terminal"); + // get flags + bool writeToConsole = !args.Contains("--no-terminal") && Environment.GetEnvironmentVariable("SMAPI_NO_TERMINAL") == null; - // get mods path from arguments - string modsPath = null; + // get mods path + string modsPath; { + string rawModsPath = null; + + // get from command line args int pathIndex = Array.LastIndexOf(args, "--mods-path") + 1; if (pathIndex >= 1 && args.Length >= pathIndex) - { - modsPath = args[pathIndex]; - if (!string.IsNullOrWhiteSpace(modsPath) && !Path.IsPathRooted(modsPath)) - modsPath = Path.Combine(Constants.ExecutionPath, modsPath); - } - if (string.IsNullOrWhiteSpace(modsPath)) - modsPath = Constants.DefaultModsPath; + rawModsPath = args[pathIndex]; + + // get from environment variables + if (string.IsNullOrWhiteSpace(rawModsPath)) + rawModsPath = Environment.GetEnvironmentVariable("SMAPI_MODS_PATH"); + + // normalise + modsPath = !string.IsNullOrWhiteSpace(rawModsPath) + ? Path.Combine(Constants.ExecutionPath, rawModsPath) + : Constants.DefaultModsPath; } // load SMAPI |