From a432477ea31c32e62fb347aef75861ab7ef62510 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 30 Dec 2016 12:04:27 -0500 Subject: fallback to launching SMAPI without a terminal on Linux if the terminal is unavailable (#198) --- src/StardewModdingAPI/Framework/Monitor.cs | 5 ++++- src/StardewModdingAPI/Program.cs | 14 +++++++++----- src/StardewModdingAPI/unix-launcher.sh | 8 ++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) (limited to 'src/StardewModdingAPI') diff --git a/src/StardewModdingAPI/Framework/Monitor.cs b/src/StardewModdingAPI/Framework/Monitor.cs index cf46a474..0989bb7e 100644 --- a/src/StardewModdingAPI/Framework/Monitor.cs +++ b/src/StardewModdingAPI/Framework/Monitor.cs @@ -37,6 +37,9 @@ namespace StardewModdingAPI.Framework /// Whether to show trace messages in the console. internal bool ShowTraceInConsole { get; set; } + /// Whether to write anything to the console. This should be disabled if no console is available. + internal bool WriteToConsole { get; set; } = true; + /********* ** Public methods @@ -108,7 +111,7 @@ namespace StardewModdingAPI.Framework message = $"[{DateTime.Now:HH:mm:ss} {levelStr} {source}] {message}"; // log - if (this.ShowTraceInConsole || level != LogLevel.Trace) + if (this.WriteToConsole && (this.ShowTraceInConsole || level != LogLevel.Trace)) { Console.ForegroundColor = color; Console.WriteLine(message); diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 9cb84cf5..58dbd87d 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -93,12 +93,14 @@ namespace StardewModdingAPI ** Public methods *********/ /// The main entry point which hooks into and launches the game. - private static void Main() + /// The command-line arguments. + private static void Main(string[] args) { - // set thread culture for consistent log formatting - Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB"); + // set log options + Program.Monitor.WriteToConsole = !args.Contains("--no-terminal"); + Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB"); // for consistent log formatting - // add info header + // add info headers Program.Monitor.Log($"SMAPI {Constants.ApiVersion} with Stardew Valley {Game1.version} on {Environment.OSVersion}", LogLevel.Info); // initialise user settings @@ -123,6 +125,8 @@ namespace StardewModdingAPI } if (!Program.Settings.CheckForUpdates) Program.Monitor.Log($"You configured SMAPI to not check for updates. Running an old version of SMAPI is not recommended. You can enable update checks by editing or deleting {Constants.ApiConfigPath}.", LogLevel.Warn); + if (!Program.Monitor.WriteToConsole) + Program.Monitor.Log($"Writing to the terminal is disabled because the --no-terminal argument was received. This usually means launching the terminal failed.", LogLevel.Warn); // initialise legacy log Log.Monitor = Program.GetSecondaryMonitor("legacy mod"); @@ -595,7 +599,7 @@ namespace StardewModdingAPI /// The name of the module which will log messages with this instance. private static Monitor GetSecondaryMonitor(string name) { - return new Monitor(name, Program.LogFile) { ShowTraceInConsole = Program.Settings.DeveloperMode }; + return new Monitor(name, Program.LogFile) { WriteToConsole = Program.Monitor.WriteToConsole, ShowTraceInConsole = Program.Settings.DeveloperMode }; } } } diff --git a/src/StardewModdingAPI/unix-launcher.sh b/src/StardewModdingAPI/unix-launcher.sh index 93e33c78..bf0e9d5e 100644 --- a/src/StardewModdingAPI/unix-launcher.sh +++ b/src/StardewModdingAPI/unix-launcher.sh @@ -64,4 +64,12 @@ else else $LAUNCHER fi + + # some Linux users get error 127 (command not found) from the above block, even though + # `command -v` indicates the command is valid. As a fallback, launch SMAPI without a terminal when + # that happens and pass in an argument indicating SMAPI shouldn't try writing to the terminal + # (which can be slow if there is none). + if [ $? -eq 127 ]; then + $LAUNCHER --no-terminal + fi fi -- cgit