enable nullable annotations in shared projects (#837)

This commit is contained in:
Jesse Plamondon-Willard 2022-04-12 19:56:52 -04:00
parent 4e0e928c94
commit c3851ae2e6
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
8 changed files with 34 additions and 33 deletions

View File

@ -1,5 +1,3 @@
#nullable disable
using System; using System;
using System.Reflection; using System.Reflection;
using HarmonyLib; using HarmonyLib;
@ -32,7 +30,7 @@ namespace StardewModdingAPI.Internal.Patching
/// <param name="name">The method name.</param> /// <param name="name">The method name.</param>
/// <param name="parameters">The method parameter types, or <c>null</c> if it's not overloaded.</param> /// <param name="parameters">The method parameter types, or <c>null</c> if it's not overloaded.</param>
/// <param name="generics">The method generic types, or <c>null</c> if it's not generic.</param> /// <param name="generics">The method generic types, or <c>null</c> if it's not generic.</param>
protected MethodInfo RequireMethod<TTarget>(string name, Type[] parameters = null, Type[] generics = null) protected MethodInfo RequireMethod<TTarget>(string name, Type[]? parameters = null, Type[]? generics = null)
{ {
return PatchHelper.RequireMethod<TTarget>(name, parameters, generics); return PatchHelper.RequireMethod<TTarget>(name, parameters, generics);
} }
@ -42,7 +40,7 @@ namespace StardewModdingAPI.Internal.Patching
/// <param name="priority">The patch priority to apply, usually specified using Harmony's <see cref="Priority"/> enum, or <c>null</c> to keep the default value.</param> /// <param name="priority">The patch priority to apply, usually specified using Harmony's <see cref="Priority"/> enum, or <c>null</c> to keep the default value.</param>
protected HarmonyMethod GetHarmonyMethod(string name, int? priority = null) protected HarmonyMethod GetHarmonyMethod(string name, int? priority = null)
{ {
var method = new HarmonyMethod( HarmonyMethod method = new(
AccessTools.Method(this.GetType(), name) AccessTools.Method(this.GetType(), name)
?? throw new InvalidOperationException($"Can't find patcher method {PatchHelper.GetMethodString(this.GetType(), name)}.") ?? throw new InvalidOperationException($"Can't find patcher method {PatchHelper.GetMethodString(this.GetType(), name)}.")
); );

View File

@ -1,5 +1,3 @@
#nullable disable
using System; using System;
using HarmonyLib; using HarmonyLib;

View File

@ -1,5 +1,3 @@
#nullable disable
using HarmonyLib; using HarmonyLib;
namespace StardewModdingAPI.Internal.Patching namespace StardewModdingAPI.Internal.Patching

View File

@ -1,5 +1,3 @@
#nullable disable
using System; using System;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
@ -18,7 +16,7 @@ namespace StardewModdingAPI.Internal.Patching
/// <typeparam name="TTarget">The type containing the method.</typeparam> /// <typeparam name="TTarget">The type containing the method.</typeparam>
/// <param name="parameters">The method parameter types, or <c>null</c> if it's not overloaded.</param> /// <param name="parameters">The method parameter types, or <c>null</c> if it's not overloaded.</param>
/// <exception cref="InvalidOperationException">The type has no matching constructor.</exception> /// <exception cref="InvalidOperationException">The type has no matching constructor.</exception>
public static ConstructorInfo RequireConstructor<TTarget>(Type[] parameters = null) public static ConstructorInfo RequireConstructor<TTarget>(Type[]? parameters = null)
{ {
return return
AccessTools.Constructor(typeof(TTarget), parameters) AccessTools.Constructor(typeof(TTarget), parameters)
@ -31,7 +29,7 @@ namespace StardewModdingAPI.Internal.Patching
/// <param name="parameters">The method parameter types, or <c>null</c> if it's not overloaded.</param> /// <param name="parameters">The method parameter types, or <c>null</c> if it's not overloaded.</param>
/// <param name="generics">The method generic types, or <c>null</c> if it's not generic.</param> /// <param name="generics">The method generic types, or <c>null</c> if it's not generic.</param>
/// <exception cref="InvalidOperationException">The type has no matching method.</exception> /// <exception cref="InvalidOperationException">The type has no matching method.</exception>
public static MethodInfo RequireMethod<TTarget>(string name, Type[] parameters = null, Type[] generics = null) public static MethodInfo RequireMethod<TTarget>(string name, Type[]? parameters = null, Type[]? generics = null)
{ {
return return
AccessTools.Method(typeof(TTarget), name, parameters, generics) AccessTools.Method(typeof(TTarget), name, parameters, generics)
@ -43,7 +41,7 @@ namespace StardewModdingAPI.Internal.Patching
/// <param name="name">The method name, or <c>null</c> for a constructor.</param> /// <param name="name">The method name, or <c>null</c> for a constructor.</param>
/// <param name="parameters">The method parameter types, or <c>null</c> if it's not overloaded.</param> /// <param name="parameters">The method parameter types, or <c>null</c> if it's not overloaded.</param>
/// <param name="generics">The method generic types, or <c>null</c> if it's not generic.</param> /// <param name="generics">The method generic types, or <c>null</c> if it's not generic.</param>
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(); StringBuilder str = new();

View File

@ -1,5 +1,3 @@
#nullable disable
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -8,10 +6,26 @@ namespace StardewModdingAPI.Internal.ConsoleWriting
/// <summary>The console color scheme options.</summary> /// <summary>The console color scheme options.</summary>
internal class ColorSchemeConfig internal class ColorSchemeConfig
{ {
/*********
** Accessors
*********/
/// <summary>The default color scheme ID to use, or <see cref="MonitorColorScheme.AutoDetect"/> to select one automatically.</summary> /// <summary>The default color scheme ID to use, or <see cref="MonitorColorScheme.AutoDetect"/> to select one automatically.</summary>
public MonitorColorScheme UseScheme { get; set; } public MonitorColorScheme UseScheme { get; }
/// <summary>The available console color schemes.</summary> /// <summary>The available console color schemes.</summary>
public IDictionary<MonitorColorScheme, IDictionary<ConsoleLogLevel, ConsoleColor>> Schemes { get; set; } public IDictionary<MonitorColorScheme, IDictionary<ConsoleLogLevel, ConsoleColor>> Schemes { get; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="useScheme">The default color scheme ID to use, or <see cref="MonitorColorScheme.AutoDetect"/> to select one automatically.</param>
/// <param name="schemes">The available console color schemes.</param>
public ColorSchemeConfig(MonitorColorScheme useScheme, IDictionary<MonitorColorScheme, IDictionary<ConsoleLogLevel, ConsoleColor>> schemes)
{
this.UseScheme = useScheme;
this.Schemes = schemes;
}
} }
} }

View File

@ -1,7 +1,6 @@
#nullable disable
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using StardewModdingAPI.Toolkit.Utilities; using StardewModdingAPI.Toolkit.Utilities;
namespace StardewModdingAPI.Internal.ConsoleWriting namespace StardewModdingAPI.Internal.ConsoleWriting
@ -13,10 +12,11 @@ namespace StardewModdingAPI.Internal.ConsoleWriting
** Fields ** Fields
*********/ *********/
/// <summary>The console text color for each log level.</summary> /// <summary>The console text color for each log level.</summary>
private readonly IDictionary<ConsoleLogLevel, ConsoleColor> Colors; private readonly IDictionary<ConsoleLogLevel, ConsoleColor>? Colors;
/// <summary>Whether the current console supports color formatting.</summary> /// <summary>Whether the current console supports color formatting.</summary>
private readonly bool SupportsColor; [MemberNotNullWhen(true, nameof(ColorfulConsoleWriter.Colors))]
private bool SupportsColor { get; }
/********* /*********
@ -74,10 +74,9 @@ namespace StardewModdingAPI.Internal.ConsoleWriting
/// <remarks>The colors here should be kept in sync with the SMAPI config file.</remarks> /// <remarks>The colors here should be kept in sync with the SMAPI config file.</remarks>
public static ColorSchemeConfig GetDefaultColorSchemeConfig(MonitorColorScheme useScheme) public static ColorSchemeConfig GetDefaultColorSchemeConfig(MonitorColorScheme useScheme)
{ {
return new ColorSchemeConfig return new ColorSchemeConfig(
{ useScheme: useScheme,
UseScheme = useScheme, schemes: new Dictionary<MonitorColorScheme, IDictionary<ConsoleLogLevel, ConsoleColor>>
Schemes = new Dictionary<MonitorColorScheme, IDictionary<ConsoleLogLevel, ConsoleColor>>
{ {
[MonitorColorScheme.DarkBackground] = new Dictionary<ConsoleLogLevel, ConsoleColor> [MonitorColorScheme.DarkBackground] = new Dictionary<ConsoleLogLevel, ConsoleColor>
{ {
@ -100,7 +99,7 @@ namespace StardewModdingAPI.Internal.ConsoleWriting
[ConsoleLogLevel.Success] = ConsoleColor.DarkGreen [ConsoleLogLevel.Success] = ConsoleColor.DarkGreen
} }
} }
}; );
} }
@ -136,7 +135,7 @@ namespace StardewModdingAPI.Internal.ConsoleWriting
} }
// get colors for scheme // get colors for scheme
return colorConfig.Schemes.TryGetValue(schemeID, out IDictionary<ConsoleLogLevel, ConsoleColor> scheme) return colorConfig.Schemes.TryGetValue(schemeID, out IDictionary<ConsoleLogLevel, ConsoleColor>? scheme)
? scheme ? scheme
: throw new NotSupportedException($"Unknown color scheme '{schemeID}'."); : throw new NotSupportedException($"Unknown color scheme '{schemeID}'.");
} }

View File

@ -1,5 +1,3 @@
#nullable disable
namespace StardewModdingAPI.Internal.ConsoleWriting namespace StardewModdingAPI.Internal.ConsoleWriting
{ {
/// <summary>Writes text to the console.</summary> /// <summary>Writes text to the console.</summary>

View File

@ -1,5 +1,3 @@
#nullable disable
using System; using System;
using System.Reflection; using System.Reflection;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -14,7 +12,7 @@ namespace StardewModdingAPI.Internal
*********/ *********/
/// <summary>Get a string representation of an exception suitable for writing to the error log.</summary> /// <summary>Get a string representation of an exception suitable for writing to the error log.</summary>
/// <param name="exception">The error to summarize.</param> /// <param name="exception">The error to summarize.</param>
public static string GetLogSummary(this Exception exception) public static string GetLogSummary(this Exception? exception)
{ {
try try
{ {
@ -27,7 +25,7 @@ namespace StardewModdingAPI.Internal
case ReflectionTypeLoadException ex: case ReflectionTypeLoadException ex:
string summary = ex.ToString(); string summary = ex.ToString();
foreach (Exception childEx in ex.LoaderExceptions) foreach (Exception? childEx in ex.LoaderExceptions)
summary += $"\n\n{childEx?.GetLogSummary()}"; summary += $"\n\n{childEx?.GetLogSummary()}";
message = summary; message = summary;
break; break;