add installer command-line arguments for scripting (#337)

This commit is contained in:
Jesse Plamondon-Willard 2017-08-02 01:36:56 -04:00
parent 4d3292add2
commit 937750f100
3 changed files with 74 additions and 10 deletions

View File

@ -160,8 +160,16 @@ field | purpose
`ModCompatibility` | A list of mod versions SMAPI should consider compatible or broken regardless of whether it detects incompatible code. This can be used to force SMAPI to load an incompatible mod, though that isn't recommended.
### Command-line arguments
SMAPI recognises the following command-line arguments. These are intended for internal use or
testing and may change without warning.
The SMAPI installer recognises three command-line arguments:
argument | purpose
-------- | -------
`--install` | Preselects the install action, skipping the prompt asking what the user wants to do.
`--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.
argument | purpose
-------- | -------

View File

@ -26,6 +26,9 @@ For mod developers:
* Fixed `TimeEvents.AfterDayStarted` being raised during the new-game intro.
* Fixed `Context.IsPlayerFree` being incorrectly false in some cases (e.g. when using a tool).
For power users:
* Added command-line arguments to the SMAPI installer so it can be scripted.
## 1.15.2
For players:
* Improved error when using very old versions of Stardew Valley.

View File

@ -135,6 +135,27 @@ namespace StardewModdingApi.Installer
/// </remarks>
public void Run(string[] args)
{
/****
** read command-line arguments
****/
// get action from CLI
bool installArg = args.Contains("--install");
bool uninstallArg = args.Contains("--uninstall");
if (installArg && uninstallArg)
{
this.PrintError("You can't specify both --install and --uninstall command-line flags.");
Console.ReadLine();
return;
}
// get game path from CLI
string gamePathArg = null;
{
int pathIndex = Array.LastIndexOf(args, "--game-path") + 1;
if (pathIndex >= 1 && args.Length >= pathIndex)
gamePathArg = args[pathIndex];
}
/****
** collect details
****/
@ -142,9 +163,17 @@ namespace StardewModdingApi.Installer
Platform platform = this.DetectPlatform();
this.PrintDebug($"Platform: {(platform == Platform.Windows ? "Windows" : "Linux or Mac")}.");
// get game path
DirectoryInfo installDir = this.InteractivelyGetInstallPath(platform, gamePathArg);
if (installDir == null)
{
this.PrintError("Failed finding your game path.");
Console.ReadLine();
return;
}
// get folders
DirectoryInfo packageDir = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "internal", platform.ToString()));
DirectoryInfo installDir = this.InteractivelyGetInstallPath(platform);
DirectoryInfo modsDir = new DirectoryInfo(Path.Combine(installDir.FullName, "Mods"));
var paths = new
{
@ -203,13 +232,19 @@ namespace StardewModdingApi.Installer
/****
** ask user what to do
****/
Console.WriteLine("You can....");
Console.WriteLine("[1] Install SMAPI.");
Console.WriteLine("[2] Uninstall SMAPI.");
Console.WriteLine();
ScriptAction action;
if (installArg)
action = ScriptAction.Install;
else if (uninstallArg)
action = ScriptAction.Uninstall;
else
{
Console.WriteLine("You can....");
Console.WriteLine("[1] Install SMAPI.");
Console.WriteLine("[2] Uninstall SMAPI.");
Console.WriteLine();
string choice = this.InteractivelyChoose("What do you want to do? Type 1 or 2, then press enter.", "1", "2");
switch (choice)
{
@ -222,8 +257,8 @@ namespace StardewModdingApi.Installer
default:
throw new InvalidOperationException($"Unexpected action key '{choice}'.");
}
Console.WriteLine();
}
Console.WriteLine();
/****
** Always uninstall old files
@ -513,13 +548,31 @@ namespace StardewModdingApi.Installer
/// <summary>Interactively locate the game install path to update.</summary>
/// <param name="platform">The current platform.</param>
private DirectoryInfo InteractivelyGetInstallPath(Platform platform)
/// <param name="specifiedPath">The path specified as a command-line argument (if any), which should override automatic path detection.</param>
private DirectoryInfo InteractivelyGetInstallPath(Platform platform, string specifiedPath)
{
// get executable name
string executableFilename = platform == Platform.Windows
? "Stardew Valley.exe"
: "StardewValley.exe";
// validate specified path
if (specifiedPath != null)
{
var dir = new DirectoryInfo(specifiedPath);
if (!dir.Exists)
{
this.PrintError($"You specified --game-path \"{specifiedPath}\", but that folder doesn't exist.");
return null;
}
if (!dir.EnumerateFiles(executableFilename).Any())
{
this.PrintError($"You specified --game-path \"{specifiedPath}\", but that folder doesn't contain the Stardew Valley executable.");
return null;
}
return dir;
}
// get installed paths
DirectoryInfo[] defaultPaths =
(