From 1b5055dfaafc6dcf77b5262b8290e8ca2c8179ed Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 23 Sep 2019 17:09:35 -0400 Subject: [PATCH] make console colors configurable --- docs/release-notes.md | 1 + src/SMAPI.Installer/InteractiveInstaller.cs | 8 +- .../ConsoleWriting/ColorSchemeConfig.cs | 15 ++++ .../ConsoleWriting/ColorfulConsoleWriter.cs | 88 +++++++++++-------- .../{LogLevel.cs => ConsoleLogLevel.cs} | 0 src/SMAPI.Internal/SMAPI.Internal.projitems | 3 +- src/SMAPI/Framework/Models/SConfig.cs | 4 +- src/SMAPI/Framework/Monitor.cs | 6 +- src/SMAPI/Framework/SCore.cs | 4 +- src/SMAPI/SMAPI.config.json | 51 ++++++++--- 10 files changed, 121 insertions(+), 59 deletions(-) create mode 100644 src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs rename src/SMAPI.Internal/ConsoleWriting/{LogLevel.cs => ConsoleLogLevel.cs} (100%) diff --git a/docs/release-notes.md b/docs/release-notes.md index ba64db0d..285384b0 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -43,6 +43,7 @@ For modders: * Improved mod scanning: * Now ignores metadata files and folders (like `__MACOSX` and `__folder_managed_by_vortex`) and content files (like `.txt` or `.png`), which avoids missing-manifest errors in some common cases. * Now detects XNB mods more accurately, and consolidates multi-folder XNB mods in logged messages. + * Added support for configuring console colors via `smapi-internal/config.json` (intended for players with unusual consoles). * 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`. * Duplicate-mod errors now show the mod version in each folder. diff --git a/src/SMAPI.Installer/InteractiveInstaller.cs b/src/SMAPI.Installer/InteractiveInstaller.cs index 4d313a3b..964300ac 100644 --- a/src/SMAPI.Installer/InteractiveInstaller.cs +++ b/src/SMAPI.Installer/InteractiveInstaller.cs @@ -100,7 +100,7 @@ namespace StardewModdingApi.Installer public InteractiveInstaller(string bundlePath) { this.BundlePath = bundlePath; - this.ConsoleWriter = new ColorfulConsoleWriter(EnvironmentUtility.DetectPlatform(), MonitorColorScheme.AutoDetect); + this.ConsoleWriter = new ColorfulConsoleWriter(EnvironmentUtility.DetectPlatform()); } /// Run the install or uninstall script. @@ -217,8 +217,8 @@ namespace StardewModdingApi.Installer ** show theme selector ****/ // get theme writers - var lightBackgroundWriter = new ColorfulConsoleWriter(EnvironmentUtility.DetectPlatform(), MonitorColorScheme.LightBackground); - var darkBackgroundWriter = new ColorfulConsoleWriter(EnvironmentUtility.DetectPlatform(), MonitorColorScheme.DarkBackground); + var lightBackgroundWriter = new ColorfulConsoleWriter(platform, ColorfulConsoleWriter.GetDefaultColorSchemeConfig(MonitorColorScheme.LightBackground)); + var darkBackgroundWriter = new ColorfulConsoleWriter(platform, ColorfulConsoleWriter.GetDefaultColorSchemeConfig(MonitorColorScheme.DarkBackground)); // print question this.PrintPlain("Which text looks more readable?"); @@ -470,7 +470,7 @@ namespace StardewModdingApi.Installer { string text = File .ReadAllText(paths.ApiConfigPath) - .Replace(@"""ColorScheme"": ""AutoDetect""", $@"""ColorScheme"": ""{scheme}"""); + .Replace(@"""UseScheme"": ""AutoDetect""", $@"""UseScheme"": ""{scheme}"""); File.WriteAllText(paths.ApiConfigPath, text); } diff --git a/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs b/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs new file mode 100644 index 00000000..001840bf --- /dev/null +++ b/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; + +namespace StardewModdingAPI.Internal.ConsoleWriting +{ + /// The console color scheme options. + internal class ColorSchemeConfig + { + /// The default color scheme ID to use, or to select one automatically. + public MonitorColorScheme UseScheme { get; set; } + + /// The available console color schemes. + public IDictionary> Schemes { get; set; } + } +} diff --git a/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs b/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs index db016bae..aefda9b6 100644 --- a/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs +++ b/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs @@ -22,11 +22,16 @@ namespace StardewModdingAPI.Internal.ConsoleWriting *********/ /// Construct an instance. /// The target platform. - /// The console color scheme to use. - public ColorfulConsoleWriter(Platform platform, MonitorColorScheme colorScheme) + public ColorfulConsoleWriter(Platform platform) + : this(platform, ColorfulConsoleWriter.GetDefaultColorSchemeConfig(MonitorColorScheme.AutoDetect)) { } + + /// Construct an instance. + /// The target platform. + /// The colors to use for text written to the SMAPI console. + public ColorfulConsoleWriter(Platform platform, ColorSchemeConfig colorConfig) { this.SupportsColor = this.TestColorSupport(); - this.Colors = this.GetConsoleColorScheme(platform, colorScheme); + this.Colors = this.GetConsoleColorScheme(platform, colorConfig); } /// Write a message line to the log. @@ -54,6 +59,40 @@ namespace StardewModdingAPI.Internal.ConsoleWriting Console.WriteLine(message); } + /// Get the default color scheme config for cases where it's not configurable (e.g. the installer). + /// The default color scheme ID to use, or to select one automatically. + /// The colors here should be kept in sync with the SMAPI config file. + public static ColorSchemeConfig GetDefaultColorSchemeConfig(MonitorColorScheme useScheme) + { + return new ColorSchemeConfig + { + UseScheme = useScheme, + Schemes = new Dictionary> + { + [MonitorColorScheme.DarkBackground] = new Dictionary + { + [ConsoleLogLevel.Trace] = ConsoleColor.DarkGray, + [ConsoleLogLevel.Debug] = ConsoleColor.DarkGray, + [ConsoleLogLevel.Info] = ConsoleColor.White, + [ConsoleLogLevel.Warn] = ConsoleColor.Yellow, + [ConsoleLogLevel.Error] = ConsoleColor.Red, + [ConsoleLogLevel.Alert] = ConsoleColor.Magenta, + [ConsoleLogLevel.Success] = ConsoleColor.DarkGreen + }, + [MonitorColorScheme.LightBackground] = new Dictionary + { + [ConsoleLogLevel.Trace] = ConsoleColor.DarkGray, + [ConsoleLogLevel.Debug] = ConsoleColor.DarkGray, + [ConsoleLogLevel.Info] = ConsoleColor.Black, + [ConsoleLogLevel.Warn] = ConsoleColor.DarkYellow, + [ConsoleLogLevel.Error] = ConsoleColor.Red, + [ConsoleLogLevel.Alert] = ConsoleColor.DarkMagenta, + [ConsoleLogLevel.Success] = ConsoleColor.DarkGreen + } + } + }; + } + /********* ** Private methods @@ -74,47 +113,22 @@ namespace StardewModdingAPI.Internal.ConsoleWriting /// Get the color scheme to use for the current console. /// The target platform. - /// The console color scheme to use. - private IDictionary GetConsoleColorScheme(Platform platform, MonitorColorScheme colorScheme) + /// The colors to use for text written to the SMAPI console. + private IDictionary GetConsoleColorScheme(Platform platform, ColorSchemeConfig colorConfig) { - // auto detect color scheme - if (colorScheme == MonitorColorScheme.AutoDetect) + // get color scheme ID + MonitorColorScheme schemeID = colorConfig.UseScheme; + if (schemeID == MonitorColorScheme.AutoDetect) { - colorScheme = platform == Platform.Mac + schemeID = platform == Platform.Mac ? MonitorColorScheme.LightBackground // MacOS doesn't provide console background color info, but it's usually white. : ColorfulConsoleWriter.IsDark(Console.BackgroundColor) ? MonitorColorScheme.DarkBackground : MonitorColorScheme.LightBackground; } // get colors for scheme - switch (colorScheme) - { - case MonitorColorScheme.DarkBackground: - return new Dictionary - { - [ConsoleLogLevel.Trace] = ConsoleColor.DarkGray, - [ConsoleLogLevel.Debug] = ConsoleColor.DarkGray, - [ConsoleLogLevel.Info] = ConsoleColor.White, - [ConsoleLogLevel.Warn] = ConsoleColor.Yellow, - [ConsoleLogLevel.Error] = ConsoleColor.Red, - [ConsoleLogLevel.Alert] = ConsoleColor.Magenta, - [ConsoleLogLevel.Success] = ConsoleColor.DarkGreen - }; - - case MonitorColorScheme.LightBackground: - return new Dictionary - { - [ConsoleLogLevel.Trace] = ConsoleColor.DarkGray, - [ConsoleLogLevel.Debug] = ConsoleColor.DarkGray, - [ConsoleLogLevel.Info] = ConsoleColor.Black, - [ConsoleLogLevel.Warn] = ConsoleColor.DarkYellow, - [ConsoleLogLevel.Error] = ConsoleColor.Red, - [ConsoleLogLevel.Alert] = ConsoleColor.DarkMagenta, - [ConsoleLogLevel.Success] = ConsoleColor.DarkGreen - }; - - default: - throw new NotSupportedException($"Unknown color scheme '{colorScheme}'."); - } + return colorConfig.Schemes.TryGetValue(schemeID, out IDictionary scheme) + ? scheme + : throw new NotSupportedException($"Unknown color scheme '{schemeID}'."); } /// Get whether a console color should be considered dark, which is subjectively defined as 'white looks better than black on this text'. diff --git a/src/SMAPI.Internal/ConsoleWriting/LogLevel.cs b/src/SMAPI.Internal/ConsoleWriting/ConsoleLogLevel.cs similarity index 100% rename from src/SMAPI.Internal/ConsoleWriting/LogLevel.cs rename to src/SMAPI.Internal/ConsoleWriting/ConsoleLogLevel.cs diff --git a/src/SMAPI.Internal/SMAPI.Internal.projitems b/src/SMAPI.Internal/SMAPI.Internal.projitems index 1408cc46..7fcebc94 100644 --- a/src/SMAPI.Internal/SMAPI.Internal.projitems +++ b/src/SMAPI.Internal/SMAPI.Internal.projitems @@ -10,7 +10,8 @@ - + + \ No newline at end of file diff --git a/src/SMAPI/Framework/Models/SConfig.cs b/src/SMAPI/Framework/Models/SConfig.cs index 40ed9512..b778af5d 100644 --- a/src/SMAPI/Framework/Models/SConfig.cs +++ b/src/SMAPI/Framework/Models/SConfig.cs @@ -67,8 +67,8 @@ namespace StardewModdingAPI.Framework.Models /// Whether to generate a file in the mods folder with detailed metadata about the detected mods. public bool DumpMetadata { get; set; } - /// The console color scheme to use. - public MonitorColorScheme ColorScheme { get; set; } + /// The colors to use for text written to the SMAPI console. + public ColorSchemeConfig ConsoleColors { get; set; } /// The mod IDs SMAPI should ignore when performing update checks or validating update keys. public string[] SuppressUpdateChecks { get; set; } diff --git a/src/SMAPI/Framework/Monitor.cs b/src/SMAPI/Framework/Monitor.cs index 1fa55a9e..06cf1b46 100644 --- a/src/SMAPI/Framework/Monitor.cs +++ b/src/SMAPI/Framework/Monitor.cs @@ -50,9 +50,9 @@ namespace StardewModdingAPI.Framework /// The name of the module which logs messages using this instance. /// Intercepts access to the console output. /// The log file to which to write messages. - /// The console color scheme to use. + /// The colors to use for text written to the SMAPI console. /// Whether verbose logging is enabled. This enables more detailed diagnostic messages than are normally needed. - public Monitor(string source, ConsoleInterceptionManager consoleInterceptor, LogFileManager logFile, MonitorColorScheme colorScheme, bool isVerbose) + public Monitor(string source, ConsoleInterceptionManager consoleInterceptor, LogFileManager logFile, ColorSchemeConfig colorConfig, bool isVerbose) { // validate if (string.IsNullOrWhiteSpace(source)) @@ -61,7 +61,7 @@ namespace StardewModdingAPI.Framework // initialize this.Source = source; this.LogFile = logFile ?? throw new ArgumentNullException(nameof(logFile), "The log file manager cannot be null."); - this.ConsoleWriter = new ColorfulConsoleWriter(Constants.Platform, colorScheme); + this.ConsoleWriter = new ColorfulConsoleWriter(Constants.Platform, colorConfig); this.ConsoleInterceptor = consoleInterceptor; this.IsVerbose = isVerbose; } diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index 08d30a29..e293cefd 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -143,7 +143,7 @@ namespace StardewModdingAPI.Framework // init basics this.Settings = JsonConvert.DeserializeObject(File.ReadAllText(Constants.ApiConfigPath)); this.LogFile = new LogFileManager(logPath); - this.Monitor = new Monitor("SMAPI", this.ConsoleManager, this.LogFile, this.Settings.ColorScheme, this.Settings.VerboseLogging) + this.Monitor = new Monitor("SMAPI", this.ConsoleManager, this.LogFile, this.Settings.ConsoleColors, this.Settings.VerboseLogging) { WriteToConsole = writeToConsole, ShowTraceInConsole = this.Settings.DeveloperMode, @@ -1351,7 +1351,7 @@ namespace StardewModdingAPI.Framework /// The name of the module which will log messages with this instance. private Monitor GetSecondaryMonitor(string name) { - return new Monitor(name, this.ConsoleManager, this.LogFile, this.Settings.ColorScheme, this.Settings.VerboseLogging) + return new Monitor(name, this.ConsoleManager, this.LogFile, this.Settings.ConsoleColors, this.Settings.VerboseLogging) { WriteToConsole = this.Monitor.WriteToConsole, ShowTraceInConsole = this.Settings.DeveloperMode, diff --git a/src/SMAPI/SMAPI.config.json b/src/SMAPI/SMAPI.config.json index 225e4b3f..bccac678 100644 --- a/src/SMAPI/SMAPI.config.json +++ b/src/SMAPI/SMAPI.config.json @@ -10,12 +10,9 @@ The default values are mirrored in StardewModdingAPI.Framework.Models.SConfig to */ { /** - * The console color theme to use. The possible values are: - * - AutoDetect: SMAPI will assume a light background on Mac, and detect the background color automatically on Linux or Windows. - * - LightBackground: use darker text colors that look better on a white or light background. - * - DarkBackground: use lighter text colors that look better on a black or dark background. + * Whether SMAPI should log more information about the game context. */ - "ColorScheme": "AutoDetect", + "VerboseLogging": false, /** * Whether SMAPI should check for newer versions of SMAPI and mods when you load the game. If new @@ -57,11 +54,6 @@ The default values are mirrored in StardewModdingAPI.Framework.Models.SConfig to */ "WebApiBaseUrl": "https://api.smapi.io", - /** - * Whether SMAPI should log more information about the game context. - */ - "VerboseLogging": false, - /** * Whether SMAPI should log network traffic (may be very verbose). Best combined with VerboseLogging, which includes network metadata. */ @@ -73,6 +65,45 @@ The default values are mirrored in StardewModdingAPI.Framework.Models.SConfig to */ "DumpMetadata": false, + /** + * The colors to use for text written to the SMAPI console. + * + * The possible values for 'UseScheme' are: + * - AutoDetect: SMAPI will assume a light background on Mac, and detect the background color + * automatically on Linux or Windows. + * - LightBackground: use darker text colors that look better on a white or light background. + * - DarkBackground: use lighter text colors that look better on a black or dark background. + * + * For available color codes, see https://docs.microsoft.com/en-us/dotnet/api/system.consolecolor. + * + * (These values are synched with ColorfulConsoleWriter.GetDefaultColorSchemeConfig in the + * SMAPI code.) + */ + "ConsoleColors": { + "UseScheme": "AutoDetect", + + "Schemes": { + "DarkBackground": { + "Trace": "DarkGray", + "Debug": "DarkGray", + "Info": "White", + "Warn": "Yellow", + "Error": "Red", + "Alert": "Magenta", + "Success": "DarkGreen" + }, + "LightBackground": { + "Trace": "DarkGray", + "Debug": "DarkGray", + "Info": "Black", + "Warn": "DarkYellow", + "Error": "Red", + "Alert": "DarkMagenta", + "Success": "DarkGreen" + } + } + }, + /** * The mod IDs SMAPI should ignore when performing update checks or validating update keys. */