add support for using environment variables instead of command-line arguments (#665)

This commit is contained in:
Jesse Plamondon-Willard 2019-11-03 18:24:34 -05:00
parent f386df7e4d
commit df7e814286
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
3 changed files with 31 additions and 13 deletions

View File

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

View File

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

View File

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