From 4da9e954df3846d01aa0536f4e8143466a1d62f3 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 11 Feb 2022 00:49:49 -0500 Subject: use Array.Empty to avoid unneeded array allocations --- src/SMAPI.Internal/ExceptionHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/SMAPI.Internal') diff --git a/src/SMAPI.Internal/ExceptionHelper.cs b/src/SMAPI.Internal/ExceptionHelper.cs index 05b96c2e..03d48911 100644 --- a/src/SMAPI.Internal/ExceptionHelper.cs +++ b/src/SMAPI.Internal/ExceptionHelper.cs @@ -25,7 +25,7 @@ namespace StardewModdingAPI.Internal case ReflectionTypeLoadException ex: string summary = ex.ToString(); - foreach (Exception childEx in ex.LoaderExceptions ?? new Exception[0]) + foreach (Exception childEx in ex.LoaderExceptions ?? Array.Empty()) summary += $"\n\n{childEx?.GetLogSummary()}"; message = summary; break; -- cgit From 077d8e4f401ad1806c6af0540f432366314a2300 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 6 Apr 2022 18:25:00 -0400 Subject: remove some unused/redundant code --- src/SMAPI.Internal/ExceptionHelper.cs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) (limited to 'src/SMAPI.Internal') diff --git a/src/SMAPI.Internal/ExceptionHelper.cs b/src/SMAPI.Internal/ExceptionHelper.cs index 03d48911..6bd1d579 100644 --- a/src/SMAPI.Internal/ExceptionHelper.cs +++ b/src/SMAPI.Internal/ExceptionHelper.cs @@ -25,7 +25,7 @@ namespace StardewModdingAPI.Internal case ReflectionTypeLoadException ex: string summary = ex.ToString(); - foreach (Exception childEx in ex.LoaderExceptions ?? Array.Empty()) + foreach (Exception childEx in ex.LoaderExceptions) summary += $"\n\n{childEx?.GetLogSummary()}"; message = summary; break; @@ -43,15 +43,6 @@ namespace StardewModdingAPI.Internal } } - /// Get the lowest exception in an exception stack. - /// The exception from which to search. - public static Exception GetInnermostException(this Exception exception) - { - while (exception.InnerException != null) - exception = exception.InnerException; - return exception; - } - /// Simplify common patterns in exception log messages that don't convey useful info. /// The log message to simplify. public static string SimplifyExtensionMessage(string message) -- cgit From 2e7c233f6c9bf6430672b39f970a3324deba79dd Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 6 Apr 2022 21:48:55 -0400 Subject: enable nullable annotations by default (#837) This adds `#nullable disable` to all existing code (except where null is impossible like enum files), so it can be migrated incrementally. --- src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs | 2 ++ src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs | 2 ++ src/SMAPI.Internal/ConsoleWriting/IConsoleWriter.cs | 2 ++ src/SMAPI.Internal/ExceptionHelper.cs | 2 ++ 4 files changed, 8 insertions(+) (limited to 'src/SMAPI.Internal') diff --git a/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs b/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs index 001840bf..b22aa231 100644 --- a/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs +++ b/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs @@ -1,3 +1,5 @@ +#nullable disable + using System; using System.Collections.Generic; diff --git a/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs b/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs index bfe155e0..19a31c7b 100644 --- a/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs +++ b/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs @@ -1,3 +1,5 @@ +#nullable disable + using System; using System.Collections.Generic; using StardewModdingAPI.Toolkit.Utilities; diff --git a/src/SMAPI.Internal/ConsoleWriting/IConsoleWriter.cs b/src/SMAPI.Internal/ConsoleWriting/IConsoleWriter.cs index fbcf161c..84e17207 100644 --- a/src/SMAPI.Internal/ConsoleWriting/IConsoleWriter.cs +++ b/src/SMAPI.Internal/ConsoleWriting/IConsoleWriter.cs @@ -1,3 +1,5 @@ +#nullable disable + namespace StardewModdingAPI.Internal.ConsoleWriting { /// Writes text to the console. diff --git a/src/SMAPI.Internal/ExceptionHelper.cs b/src/SMAPI.Internal/ExceptionHelper.cs index 6bd1d579..a856cf71 100644 --- a/src/SMAPI.Internal/ExceptionHelper.cs +++ b/src/SMAPI.Internal/ExceptionHelper.cs @@ -1,3 +1,5 @@ +#nullable disable + using System; using System.Reflection; using System.Text.RegularExpressions; -- cgit From c3851ae2e6c8fb286d4744612fbfea039d1baf7f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 12 Apr 2022 19:56:52 -0400 Subject: enable nullable annotations in shared projects (#837) --- .../ConsoleWriting/ColorSchemeConfig.cs | 22 ++++++++++++++++++---- .../ConsoleWriting/ColorfulConsoleWriter.cs | 19 +++++++++---------- .../ConsoleWriting/IConsoleWriter.cs | 2 -- src/SMAPI.Internal/ExceptionHelper.cs | 6 ++---- 4 files changed, 29 insertions(+), 20 deletions(-) (limited to 'src/SMAPI.Internal') diff --git a/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs b/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs index b22aa231..4e5850ea 100644 --- a/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs +++ b/src/SMAPI.Internal/ConsoleWriting/ColorSchemeConfig.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; @@ -8,10 +6,26 @@ namespace StardewModdingAPI.Internal.ConsoleWriting /// The console color scheme options. internal class ColorSchemeConfig { + /********* + ** Accessors + *********/ /// The default color scheme ID to use, or to select one automatically. - public MonitorColorScheme UseScheme { get; set; } + public MonitorColorScheme UseScheme { get; } /// The available console color schemes. - public IDictionary> Schemes { get; set; } + public IDictionary> Schemes { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The default color scheme ID to use, or to select one automatically. + /// The available console color schemes. + public ColorSchemeConfig(MonitorColorScheme useScheme, IDictionary> schemes) + { + this.UseScheme = useScheme; + this.Schemes = schemes; + } } } diff --git a/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs b/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs index 19a31c7b..78db0d65 100644 --- a/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs +++ b/src/SMAPI.Internal/ConsoleWriting/ColorfulConsoleWriter.cs @@ -1,7 +1,6 @@ -#nullable disable - using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using StardewModdingAPI.Toolkit.Utilities; namespace StardewModdingAPI.Internal.ConsoleWriting @@ -13,10 +12,11 @@ namespace StardewModdingAPI.Internal.ConsoleWriting ** Fields *********/ /// The console text color for each log level. - private readonly IDictionary Colors; + private readonly IDictionary? Colors; /// Whether the current console supports color formatting. - private readonly bool SupportsColor; + [MemberNotNullWhen(true, nameof(ColorfulConsoleWriter.Colors))] + private bool SupportsColor { get; } /********* @@ -74,10 +74,9 @@ namespace StardewModdingAPI.Internal.ConsoleWriting /// 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> + return new ColorSchemeConfig( + useScheme: useScheme, + schemes: new Dictionary> { [MonitorColorScheme.DarkBackground] = new Dictionary { @@ -100,7 +99,7 @@ namespace StardewModdingAPI.Internal.ConsoleWriting [ConsoleLogLevel.Success] = ConsoleColor.DarkGreen } } - }; + ); } @@ -136,7 +135,7 @@ namespace StardewModdingAPI.Internal.ConsoleWriting } // get colors for scheme - return colorConfig.Schemes.TryGetValue(schemeID, out IDictionary scheme) + return colorConfig.Schemes.TryGetValue(schemeID, out IDictionary? scheme) ? scheme : throw new NotSupportedException($"Unknown color scheme '{schemeID}'."); } diff --git a/src/SMAPI.Internal/ConsoleWriting/IConsoleWriter.cs b/src/SMAPI.Internal/ConsoleWriting/IConsoleWriter.cs index 84e17207..fbcf161c 100644 --- a/src/SMAPI.Internal/ConsoleWriting/IConsoleWriter.cs +++ b/src/SMAPI.Internal/ConsoleWriting/IConsoleWriter.cs @@ -1,5 +1,3 @@ -#nullable disable - namespace StardewModdingAPI.Internal.ConsoleWriting { /// Writes text to the console. diff --git a/src/SMAPI.Internal/ExceptionHelper.cs b/src/SMAPI.Internal/ExceptionHelper.cs index a856cf71..7edc0f62 100644 --- a/src/SMAPI.Internal/ExceptionHelper.cs +++ b/src/SMAPI.Internal/ExceptionHelper.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Reflection; using System.Text.RegularExpressions; @@ -14,7 +12,7 @@ namespace StardewModdingAPI.Internal *********/ /// Get a string representation of an exception suitable for writing to the error log. /// The error to summarize. - public static string GetLogSummary(this Exception exception) + public static string GetLogSummary(this Exception? exception) { try { @@ -27,7 +25,7 @@ namespace StardewModdingAPI.Internal case ReflectionTypeLoadException ex: string summary = ex.ToString(); - foreach (Exception childEx in ex.LoaderExceptions) + foreach (Exception? childEx in ex.LoaderExceptions) summary += $"\n\n{childEx?.GetLogSummary()}"; message = summary; break; -- cgit