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) --- src/SMAPI.Internal.Patching/BasePatcher.cs | 6 ++---- src/SMAPI.Internal.Patching/HarmonyPatcher.cs | 2 -- src/SMAPI.Internal.Patching/IPatcher.cs | 2 -- src/SMAPI.Internal.Patching/PatchHelper.cs | 8 +++----- .../ConsoleWriting/ColorSchemeConfig.cs | 22 ++++++++++++++++++---- .../ConsoleWriting/ColorfulConsoleWriter.cs | 19 +++++++++---------- .../ConsoleWriting/IConsoleWriter.cs | 2 -- src/SMAPI.Internal/ExceptionHelper.cs | 6 ++---- 8 files changed, 34 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/SMAPI.Internal.Patching/BasePatcher.cs b/src/SMAPI.Internal.Patching/BasePatcher.cs index 6d019b52..c1936ccc 100644 --- a/src/SMAPI.Internal.Patching/BasePatcher.cs +++ b/src/SMAPI.Internal.Patching/BasePatcher.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Reflection; using HarmonyLib; @@ -32,7 +30,7 @@ namespace StardewModdingAPI.Internal.Patching /// The method name. /// The method parameter types, or null if it's not overloaded. /// The method generic types, or null if it's not generic. - protected MethodInfo RequireMethod(string name, Type[] parameters = null, Type[] generics = null) + protected MethodInfo RequireMethod(string name, Type[]? parameters = null, Type[]? generics = null) { return PatchHelper.RequireMethod(name, parameters, generics); } @@ -42,7 +40,7 @@ namespace StardewModdingAPI.Internal.Patching /// The patch priority to apply, usually specified using Harmony's enum, or null to keep the default value. protected HarmonyMethod GetHarmonyMethod(string name, int? priority = null) { - var method = new HarmonyMethod( + HarmonyMethod method = new( AccessTools.Method(this.GetType(), name) ?? throw new InvalidOperationException($"Can't find patcher method {PatchHelper.GetMethodString(this.GetType(), name)}.") ); diff --git a/src/SMAPI.Internal.Patching/HarmonyPatcher.cs b/src/SMAPI.Internal.Patching/HarmonyPatcher.cs index fc239fd2..6f30c241 100644 --- a/src/SMAPI.Internal.Patching/HarmonyPatcher.cs +++ b/src/SMAPI.Internal.Patching/HarmonyPatcher.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using HarmonyLib; diff --git a/src/SMAPI.Internal.Patching/IPatcher.cs b/src/SMAPI.Internal.Patching/IPatcher.cs index 5b373117..a732d64f 100644 --- a/src/SMAPI.Internal.Patching/IPatcher.cs +++ b/src/SMAPI.Internal.Patching/IPatcher.cs @@ -1,5 +1,3 @@ -#nullable disable - using HarmonyLib; namespace StardewModdingAPI.Internal.Patching diff --git a/src/SMAPI.Internal.Patching/PatchHelper.cs b/src/SMAPI.Internal.Patching/PatchHelper.cs index 52b15fd1..edd8ef57 100644 --- a/src/SMAPI.Internal.Patching/PatchHelper.cs +++ b/src/SMAPI.Internal.Patching/PatchHelper.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Linq; using System.Reflection; @@ -18,7 +16,7 @@ namespace StardewModdingAPI.Internal.Patching /// The type containing the method. /// The method parameter types, or null if it's not overloaded. /// The type has no matching constructor. - public static ConstructorInfo RequireConstructor(Type[] parameters = null) + public static ConstructorInfo RequireConstructor(Type[]? parameters = null) { return AccessTools.Constructor(typeof(TTarget), parameters) @@ -31,7 +29,7 @@ namespace StardewModdingAPI.Internal.Patching /// The method parameter types, or null if it's not overloaded. /// The method generic types, or null if it's not generic. /// The type has no matching method. - public static MethodInfo RequireMethod(string name, Type[] parameters = null, Type[] generics = null) + public static MethodInfo RequireMethod(string name, Type[]? parameters = null, Type[]? generics = null) { return AccessTools.Method(typeof(TTarget), name, parameters, generics) @@ -43,7 +41,7 @@ namespace StardewModdingAPI.Internal.Patching /// The method name, or null for a constructor. /// The method parameter types, or null if it's not overloaded. /// The method generic types, or null if it's not generic. - public static string GetMethodString(Type type, string name, Type[] parameters = null, Type[] generics = null) + public static string GetMethodString(Type type, string? name, Type[]? parameters = null, Type[]? generics = null) { StringBuilder str = new(); 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