From 2765e3f9b379f0dc2a5732c1ca9ba23dbe2a7f15 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 13 Apr 2022 22:06:07 -0400 Subject: enable nullable annotations in bundled mods (#837) --- .../Framework/Commands/ArgumentParser.cs | 7 ++-- .../Framework/Commands/ConsoleCommand.cs | 2 -- .../Framework/Commands/IConsoleCommand.cs | 2 -- .../Commands/Other/ApplySaveFixCommand.cs | 4 +-- .../Framework/Commands/Other/DebugCommand.cs | 2 -- .../Framework/Commands/Other/RegenerateBundles.cs | 2 -- .../Commands/Other/ShowDataFilesCommand.cs | 2 -- .../Commands/Other/ShowGameFilesCommand.cs | 2 -- .../Framework/Commands/Other/TestInputCommand.cs | 2 -- .../Framework/Commands/Player/AddCommand.cs | 14 ++++---- .../Commands/Player/ListItemTypesCommand.cs | 2 -- .../Framework/Commands/Player/ListItemsCommand.cs | 9 ++--- .../Framework/Commands/Player/SetColorCommand.cs | 6 ++-- .../Commands/Player/SetFarmTypeCommand.cs | 14 ++++---- .../Framework/Commands/Player/SetHealthCommand.cs | 2 -- .../Commands/Player/SetImmunityCommand.cs | 2 -- .../Commands/Player/SetMaxHealthCommand.cs | 2 -- .../Commands/Player/SetMaxStaminaCommand.cs | 2 -- .../Framework/Commands/Player/SetMoneyCommand.cs | 2 -- .../Framework/Commands/Player/SetNameCommand.cs | 6 ++-- .../Framework/Commands/Player/SetStaminaCommand.cs | 2 -- .../Framework/Commands/Player/SetStyleCommand.cs | 4 +-- .../Framework/Commands/World/ClearCommand.cs | 23 ++++++------ .../Commands/World/DownMineLevelCommand.cs | 2 -- .../Framework/Commands/World/FreezeTimeCommand.cs | 2 -- .../Framework/Commands/World/HurryAllCommand.cs | 2 -- .../Framework/Commands/World/SetDayCommand.cs | 2 -- .../Commands/World/SetMineLevelCommand.cs | 2 -- .../Framework/Commands/World/SetSeasonCommand.cs | 4 +-- .../Framework/Commands/World/SetTimeCommand.cs | 2 -- .../Framework/Commands/World/SetYearCommand.cs | 2 -- .../Framework/ItemData/SearchableItem.cs | 2 -- .../Framework/ItemRepository.cs | 42 ++++++++++++---------- src/SMAPI.Mods.ConsoleCommands/ModEntry.cs | 14 ++++---- src/SMAPI.Mods.ErrorHandler/ModEntry.cs | 6 ++-- .../Patches/DialoguePatcher.cs | 10 +++--- .../Patches/EventPatcher.cs | 4 +-- .../Patches/GameLocationPatcher.cs | 9 ++--- .../Patches/IClickableMenuPatcher.cs | 2 -- src/SMAPI.Mods.ErrorHandler/Patches/NpcPatcher.cs | 8 ++--- .../Patches/ObjectPatcher.cs | 4 +-- .../Patches/SaveGamePatcher.cs | 10 +++--- .../Patches/SpriteBatchPatcher.cs | 6 ++-- .../Patches/UtilityPatcher.cs | 4 +-- src/SMAPI.Mods.SaveBackup/ModEntry.cs | 11 +++--- 45 files changed, 92 insertions(+), 173 deletions(-) diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ArgumentParser.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ArgumentParser.cs index 8fcbf711..66f2f105 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ArgumentParser.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ArgumentParser.cs @@ -1,8 +1,7 @@ -#nullable disable - using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands @@ -54,7 +53,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands /// The parsed value. /// Whether to show an error if the argument is missing. /// Require that the argument match one of the given values (case-insensitive). - public bool TryGet(int index, string name, out string value, bool required = true, string[] oneOf = null) + public bool TryGet(int index, string name, [NotNullWhen(true)] out string? value, bool required = true, string[]? oneOf = null) { value = null; @@ -88,7 +87,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands value = 0; // get argument - if (!this.TryGet(index, name, out string raw, required)) + if (!this.TryGet(index, name, out string? raw, required)) return false; // parse diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ConsoleCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ConsoleCommand.cs index a8dd41f5..44b7824e 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ConsoleCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/ConsoleCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Linq; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/IConsoleCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/IConsoleCommand.cs index 4c6df538..9c82bbd3 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/IConsoleCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/IConsoleCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands { /// A console command to register. diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ApplySaveFixCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ApplySaveFixCommand.cs index f31457ed..f2194cff 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ApplySaveFixCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ApplySaveFixCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -25,7 +23,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other public override void Handle(IMonitor monitor, string command, ArgumentParser args) { // get fix ID - if (!args.TryGet(0, "fix_id", out string rawFixId, required: false)) + if (!args.TryGet(0, "fix_id", out string? rawFixId, required: false)) { monitor.Log("Invalid usage. Type 'help apply_save_fix' for details.", LogLevel.Error); return; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/DebugCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/DebugCommand.cs index f289c669..cf1dcbce 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/DebugCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/DebugCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using StardewValley; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs index 81a8c570..159d7c4a 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/RegenerateBundles.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowDataFilesCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowDataFilesCommand.cs index d762d8bf..a233d588 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowDataFilesCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowDataFilesCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics; using System.Diagnostics.CodeAnalysis; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowGameFilesCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowGameFilesCommand.cs index b5733eb9..745b821b 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowGameFilesCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ShowGameFilesCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics; using System.Diagnostics.CodeAnalysis; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/TestInputCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/TestInputCommand.cs index 5484fc7c..8bf9f5db 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/TestInputCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/TestInputCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Diagnostics.CodeAnalysis; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs index 0d8db870..74d3d9df 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/AddCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Linq; using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData; @@ -42,7 +40,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player } // read arguments - if (!args.TryGet(0, "item type", out string type, oneOf: this.ValidTypes)) + if (!args.TryGet(0, "item type", out string? type, oneOf: this.ValidTypes)) return; if (!args.TryGetInt(2, "count", out int count, min: 1, required: false)) count = 1; @@ -50,7 +48,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player quality = Object.lowQuality; // find matching item - SearchableItem match = Enum.TryParse(type, true, out ItemType itemType) + SearchableItem? match = Enum.TryParse(type, true, out ItemType itemType) ? this.FindItemByID(monitor, args, itemType) : this.FindItemByName(monitor, args); if (match == null) @@ -78,14 +76,14 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// Writes messages to the console and log file. /// The command arguments. /// The item type. - private SearchableItem FindItemByID(IMonitor monitor, ArgumentParser args, ItemType type) + private SearchableItem? FindItemByID(IMonitor monitor, ArgumentParser args, ItemType type) { // read arguments if (!args.TryGetInt(1, "item ID", out int id, min: 0)) return null; // find matching item - SearchableItem item = this.Items.GetAll().FirstOrDefault(p => p.Type == type && p.ID == id); + SearchableItem? item = this.Items.GetAll().FirstOrDefault(p => p.Type == type && p.ID == id); if (item == null) monitor.Log($"There's no {type} item with ID {id}.", LogLevel.Error); return item; @@ -94,10 +92,10 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// Get a matching item by its name. /// Writes messages to the console and log file. /// The command arguments. - private SearchableItem FindItemByName(IMonitor monitor, ArgumentParser args) + private SearchableItem? FindItemByName(IMonitor monitor, ArgumentParser args) { // read arguments - if (!args.TryGet(1, "item name", out string name)) + if (!args.TryGet(1, "item name", out string? name)) return null; // find matching items diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemTypesCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemTypesCommand.cs index e57d4065..ef35ad19 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemTypesCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemTypesCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewModdingAPI.Mods.ConsoleCommands.Framework.ItemData; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemsCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemsCommand.cs index 5a21b459..73d5b79d 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemsCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/ListItemsCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -63,15 +61,14 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player private IEnumerable GetItems(string[] searchWords) { // normalize search term - searchWords = searchWords?.Where(word => !string.IsNullOrWhiteSpace(word)).ToArray(); - if (searchWords?.Any() == false) - searchWords = null; + searchWords = searchWords.Where(word => !string.IsNullOrWhiteSpace(word)).ToArray(); + bool getAll = !searchWords.Any(); // find matches return ( from item in this.Items.GetAll() let term = $"{item.ID}|{item.Type}|{item.Name}|{item.DisplayName}" - where searchWords == null || searchWords.All(word => term.IndexOf(word, StringComparison.CurrentCultureIgnoreCase) != -1) + where getAll || searchWords.All(word => term.IndexOf(word, StringComparison.CurrentCultureIgnoreCase) != -1) select item ); } diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetColorCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetColorCommand.cs index e8605163..12a51bc9 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetColorCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetColorCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using Microsoft.Xna.Framework; using StardewValley; @@ -24,9 +22,9 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player public override void Handle(IMonitor monitor, string command, ArgumentParser args) { // parse arguments - if (!args.TryGet(0, "target", out string target, oneOf: new[] { "hair", "eyes", "pants" })) + if (!args.TryGet(0, "target", out string? target, oneOf: new[] { "hair", "eyes", "pants" })) return; - if (!args.TryGet(1, "color", out string rawColor)) + if (!args.TryGet(1, "color", out string? rawColor)) return; // parse color diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetFarmTypeCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetFarmTypeCommand.cs index 02670911..b2035d42 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetFarmTypeCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetFarmTypeCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections; using System.Collections.Generic; @@ -37,7 +35,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player } // parse arguments - if (!args.TryGet(0, "farm type", out string farmType)) + if (!args.TryGet(0, "farm type", out string? farmType)) return; bool isVanillaId = int.TryParse(farmType, out int vanillaId) && vanillaId is (>= 0 and < Farm.layout_max); @@ -112,7 +110,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player return; } - if (!this.GetCustomFarmTypes().TryGetValue(id, out ModFarmType customFarmType)) + if (!this.GetCustomFarmTypes().TryGetValue(id, out ModFarmType? customFarmType)) { monitor.Log($"Invalid farm type '{id}'. Enter `help set_farm_type` for more info.", LogLevel.Error); return; @@ -125,7 +123,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// Change the farm type. /// The farm type ID. /// The custom farm type data, if applicable. - private void SetFarmType(int type, ModFarmType customFarmData) + private void SetFarmType(int type, ModFarmType? customFarmData) { // set flags Game1.whichFarm = type; @@ -138,7 +136,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player farm.updateWarps(); // clear spouse area cache to avoid errors - FieldInfo cacheField = farm.GetType().GetField("_baseSpouseAreaTiles", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); + FieldInfo? cacheField = farm.GetType().GetField("_baseSpouseAreaTiles", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (cacheField == null) throw new InvalidOperationException("Failed to access '_baseSpouseAreaTiles' field to clear spouse area cache."); if (cacheField.GetValue(farm) is not IDictionary cache) @@ -166,7 +164,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player /// The farm type. private string GetVanillaName(int type) { - string translationKey = type switch + string? translationKey = type switch { Farm.default_layout => "Character_FarmStandard", Farm.riverlands_layout => "Character_FarmFishing", @@ -199,7 +197,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player ****/ /// Get the display name for a custom farm type. /// The custom farm type. - private string GetCustomName(ModFarmType farmType) + private string? GetCustomName(ModFarmType? farmType) { if (string.IsNullOrWhiteSpace(farmType?.TooltipStringPath)) return farmType?.ID; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs index 1a1a9eab..f169159f 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetHealthCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewValley; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetImmunityCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetImmunityCommand.cs index d1dede1f..1065bd21 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetImmunityCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetImmunityCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewValley; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxHealthCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxHealthCommand.cs index 2b3b140c..c2c4931d 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxHealthCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxHealthCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewValley; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxStaminaCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxStaminaCommand.cs index f9ed6c58..8c794e75 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxStaminaCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMaxStaminaCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewValley; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs index 56447a65..3afcc62b 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetMoneyCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewValley; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetNameCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetNameCommand.cs index 4ce7e1f8..37c02ed0 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetNameCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetNameCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using StardewValley; @@ -23,9 +21,9 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player public override void Handle(IMonitor monitor, string command, ArgumentParser args) { // parse arguments - if (!args.TryGet(0, "target", out string target, oneOf: new[] { "player", "farm" })) + if (!args.TryGet(0, "target", out string? target, oneOf: new[] { "player", "farm" })) return; - args.TryGet(1, "name", out string name, required: false); + args.TryGet(1, "name", out string? name, required: false); // handle switch (target) diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs index ea8d74c2..24718ace 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStaminaCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewValley; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStyleCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStyleCommand.cs index 84625a34..8193ff27 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStyleCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Player/SetStyleCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using StardewValley; @@ -23,7 +21,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Player public override void Handle(IMonitor monitor, string command, ArgumentParser args) { // parse arguments - if (!args.TryGet(0, "target", out string target, oneOf: new[] { "hair", "shirt", "acc", "skin", "shoe", "swim", "gender" })) + if (!args.TryGet(0, "target", out string? target, oneOf: new[] { "hair", "shirt", "acc", "skin", "shoe", "swim", "gender" })) return; if (!args.TryGetInt(1, "style ID", out int styleID)) return; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/ClearCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/ClearCommand.cs index 92c73e08..4905b89a 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/ClearCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/ClearCommand.cs @@ -1,8 +1,7 @@ -#nullable disable - using System; using System.Diagnostics.CodeAnalysis; using System.Linq; +using Microsoft.Xna.Framework; using StardewValley; using StardewValley.Locations; using StardewValley.Objects; @@ -53,13 +52,13 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World } // parse arguments - if (!args.TryGet(0, "location", out string locationName, required: true)) + if (!args.TryGet(0, "location", out string? locationName, required: true)) return; - if (!args.TryGet(1, "object type", out string type, required: true, oneOf: this.ValidTypes)) + if (!args.TryGet(1, "object type", out string? type, required: true, oneOf: this.ValidTypes)) return; // get target location - GameLocation location = Game1.locations.FirstOrDefault(p => p.Name != null && p.Name.Equals(locationName, StringComparison.OrdinalIgnoreCase)); + GameLocation? location = Game1.locations.FirstOrDefault(p => p.Name != null && p.Name.Equals(locationName, StringComparison.OrdinalIgnoreCase)); if (location == null && locationName == "current") location = Game1.currentLocation; if (location == null) @@ -168,11 +167,11 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { int removed = 0; - foreach (var pair in location.Objects.Pairs.ToArray()) + foreach ((Vector2 tile, SObject? obj) in location.Objects.Pairs.ToArray()) { - if (shouldRemove(pair.Value)) + if (shouldRemove(obj)) { - location.Objects.Remove(pair.Key); + location.Objects.Remove(tile); removed++; } } @@ -188,11 +187,11 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { int removed = 0; - foreach (var pair in location.terrainFeatures.Pairs.ToArray()) + foreach ((Vector2 tile, TerrainFeature? feature) in location.terrainFeatures.Pairs.ToArray()) { - if (shouldRemove(pair.Value)) + if (shouldRemove(feature)) { - location.terrainFeatures.Remove(pair.Key); + location.terrainFeatures.Remove(tile); removed++; } } @@ -228,7 +227,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World { int removed = 0; - foreach (var clump in location.resourceClumps.Where(shouldRemove).ToArray()) + foreach (ResourceClump clump in location.resourceClumps.Where(shouldRemove).ToArray()) { location.resourceClumps.Remove(clump); removed++; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/DownMineLevelCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/DownMineLevelCommand.cs index 0f18c760..5b1a4a13 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/DownMineLevelCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/DownMineLevelCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using StardewValley; using StardewValley.Locations; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/FreezeTimeCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/FreezeTimeCommand.cs index 8808fe35..16faa2fe 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/FreezeTimeCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/FreezeTimeCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Linq; using StardewValley; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/HurryAllCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/HurryAllCommand.cs index f9810dc3..09531720 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/HurryAllCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/HurryAllCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Diagnostics.CodeAnalysis; using StardewValley; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetDayCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetDayCommand.cs index 8aa27d93..399fd934 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetDayCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetDayCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewModdingAPI.Utilities; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetMineLevelCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetMineLevelCommand.cs index ad6ac777..f977fce3 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetMineLevelCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetMineLevelCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Diagnostics.CodeAnalysis; using StardewValley; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetSeasonCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetSeasonCommand.cs index ebe58913..505c0d1d 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetSeasonCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetSeasonCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewModdingAPI.Utilities; @@ -39,7 +37,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.World } // parse arguments - if (!args.TryGet(0, "season", out string season, oneOf: this.ValidSeasons)) + if (!args.TryGet(0, "season", out string? season, oneOf: this.ValidSeasons)) return; // handle diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetTimeCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetTimeCommand.cs index 1e6bab96..8c4458dd 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetTimeCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetTimeCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using System.Linq; using Microsoft.Xna.Framework; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetYearCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetYearCommand.cs index 995f222e..a666a634 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetYearCommand.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/World/SetYearCommand.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewModdingAPI.Utilities; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/ItemData/SearchableItem.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/ItemData/SearchableItem.cs index ab0b2e05..3675a963 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/ItemData/SearchableItem.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/ItemData/SearchableItem.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using StardewValley; diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs index 7d2a1662..3722e155 100644 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs +++ b/src/SMAPI.Mods.ConsoleCommands/Framework/ItemRepository.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -33,7 +31,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework /// The item types to fetch (or null for any type). /// Whether to include flavored variants like "Sunflower Honey". [SuppressMessage("ReSharper", "AccessToModifiedClosure", Justification = "TryCreate invokes the lambda immediately.")] - public IEnumerable GetAll(ItemType[] itemTypes = null, bool includeVariants = true) + public IEnumerable GetAll(ItemType[]? itemTypes = null, bool includeVariants = true) { // // @@ -45,9 +43,9 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework // // - IEnumerable GetAllRaw() + IEnumerable GetAllRaw() { - HashSet types = itemTypes?.Any() == true ? new HashSet(itemTypes) : null; + HashSet? types = itemTypes?.Any() == true ? new HashSet(itemTypes) : null; bool ShouldGet(ItemType type) => types == null || types.Contains(type); // get tools @@ -134,7 +132,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework { foreach (int id in Game1.objectInformation.Keys) { - string[] fields = Game1.objectInformation[id]?.Split('/'); + string[]? fields = Game1.objectInformation[id]?.Split('/'); // ring if (id != 801 && fields?.Length >= 4 && fields[3] == "Ring") // 801 = wedding ring, which isn't an equippable ring @@ -148,7 +146,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework { if (ShouldGet(ItemType.Object)) { - foreach (SearchableItem journalScrap in this.GetSecretNotes(isJournalScrap: true)) + foreach (SearchableItem? journalScrap in this.GetSecretNotes(isJournalScrap: true)) yield return journalScrap; } } @@ -158,7 +156,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework { if (ShouldGet(ItemType.Object)) { - foreach (SearchableItem secretNote in this.GetSecretNotes(isJournalScrap: false)) + foreach (SearchableItem? secretNote in this.GetSecretNotes(isJournalScrap: false)) yield return secretNote; } } @@ -167,7 +165,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework else if (ShouldGet(ItemType.Object)) { // spawn main item - SObject item = null; + SObject? item = null; yield return this.TryCreate(ItemType.Object, id, p => { return item = (p.ID == 812 // roe @@ -181,7 +179,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework // flavored items if (includeVariants) { - foreach (SearchableItem variant in this.GetFlavoredObjectVariants(item)) + foreach (SearchableItem? variant in this.GetFlavoredObjectVariants(item)) yield return variant; } } @@ -189,7 +187,11 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework } } - return GetAllRaw().Where(p => p != null); + return ( + from item in GetAllRaw() + where item != null + select item + ); } @@ -199,7 +201,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework /// Get the individual secret note or journal scrap items. /// Whether to get journal scraps. /// Derived from . - private IEnumerable GetSecretNotes(bool isJournalScrap) + private IEnumerable GetSecretNotes(bool isJournalScrap) { // get base item ID int baseId = isJournalScrap ? 842 : 79; @@ -235,7 +237,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework /// Get flavored variants of a base item (like Blueberry Wine for Blueberry), if any. /// A sample of the base item. - private IEnumerable GetFlavoredObjectVariants(SObject item) + private IEnumerable GetFlavoredObjectVariants(SObject item) { int id = item.ParentSheetIndex; @@ -305,9 +307,12 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework foreach (var pair in Game1.objectInformation) { // get input - SObject input = this.TryCreate(ItemType.Object, pair.Key, p => new SObject(p.ID, 1))?.Item as SObject; - var inputTags = input?.GetContextTags(); - if (inputTags?.Any() != true) + SObject? input = this.TryCreate(ItemType.Object, pair.Key, p => new SObject(p.ID, 1))?.Item as SObject; + if (input == null) + continue; + + HashSet inputTags = input.GetContextTags(); + if (!inputTags.Any()) continue; // check if roe-producing fish @@ -315,7 +320,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework continue; // yield roe - SObject roe = null; + SObject? roe = null; Color color = this.GetRoeColor(input); yield return this.TryCreate(ItemType.Object, this.CustomIDOffset * 7 + id, _ => { @@ -372,6 +377,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework /// The asset value type. /// The data asset name. private Dictionary TryLoad(string assetName) + where TKey : notnull { try { @@ -388,7 +394,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands.Framework /// The item type. /// The unique ID (if different from the item's parent sheet index). /// Create an item instance. - private SearchableItem TryCreate(ItemType type, int id, Func createItem) + private SearchableItem? TryCreate(ItemType type, int id, Func createItem) { try { diff --git a/src/SMAPI.Mods.ConsoleCommands/ModEntry.cs b/src/SMAPI.Mods.ConsoleCommands/ModEntry.cs index e3ca1a39..dbfca815 100644 --- a/src/SMAPI.Mods.ConsoleCommands/ModEntry.cs +++ b/src/SMAPI.Mods.ConsoleCommands/ModEntry.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Linq; @@ -15,13 +13,13 @@ namespace StardewModdingAPI.Mods.ConsoleCommands ** Fields *********/ /// The commands to handle. - private IConsoleCommand[] Commands; + private IConsoleCommand[] Commands = null!; /// The commands which may need to handle update ticks. - private IConsoleCommand[] UpdateHandlers; + private IConsoleCommand[] UpdateHandlers = null!; /// The commands which may need to handle input. - private IConsoleCommand[] InputHandlers; + private IConsoleCommand[] InputHandlers = null!; /********* @@ -52,7 +50,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands /// The method invoked when a button is pressed. /// The event sender. /// The event arguments. - private void OnButtonPressed(object sender, ButtonPressedEventArgs e) + private void OnButtonPressed(object? sender, ButtonPressedEventArgs e) { foreach (IConsoleCommand command in this.InputHandlers) command.OnButtonPressed(this.Monitor, e.Button); @@ -61,7 +59,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands /// The method invoked when the game updates its state. /// The event sender. /// The event arguments. - private void OnUpdateTicked(object sender, EventArgs e) + private void OnUpdateTicked(object? sender, EventArgs e) { foreach (IConsoleCommand command in this.UpdateHandlers) command.OnUpdated(this.Monitor); @@ -83,7 +81,7 @@ namespace StardewModdingAPI.Mods.ConsoleCommands return ( from type in this.GetType().Assembly.GetTypes() where !type.IsAbstract && typeof(IConsoleCommand).IsAssignableFrom(type) - select (IConsoleCommand)Activator.CreateInstance(type) + select (IConsoleCommand)Activator.CreateInstance(type)! ); } } diff --git a/src/SMAPI.Mods.ErrorHandler/ModEntry.cs b/src/SMAPI.Mods.ErrorHandler/ModEntry.cs index fa171012..bfbfd2dc 100644 --- a/src/SMAPI.Mods.ErrorHandler/ModEntry.cs +++ b/src/SMAPI.Mods.ErrorHandler/ModEntry.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Reflection; using StardewModdingAPI.Events; @@ -59,7 +57,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler /// The method invoked when a save is loaded. /// The event sender. /// The event arguments. - private void OnSaveLoaded(object sender, SaveLoadedEventArgs e) + private void OnSaveLoaded(object? sender, SaveLoadedEventArgs e) { // show in-game warning for removed save content if (this.IsSaveContentRemoved) @@ -82,7 +80,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler MethodInfo getMonitorForGame = coreType.GetMethod("GetMonitorForGame") ?? throw new InvalidOperationException("Can't access the SMAPI's 'GetMonitorForGame' method. This mod may not work correctly."); - return (IMonitor)getMonitorForGame.Invoke(core, Array.Empty()) ?? this.Monitor; + return (IMonitor?)getMonitorForGame.Invoke(core, Array.Empty()) ?? this.Monitor; } } } diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/DialoguePatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/DialoguePatcher.cs index b05c8cca..e98eec3c 100644 --- a/src/SMAPI.Mods.ErrorHandler/Patches/DialoguePatcher.cs +++ b/src/SMAPI.Mods.ErrorHandler/Patches/DialoguePatcher.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Diagnostics.CodeAnalysis; using HarmonyLib; @@ -19,10 +17,10 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches ** Fields *********/ /// Writes messages to the console and log file on behalf of the game. - private static IMonitor MonitorForGame; + private static IMonitor MonitorForGame = null!; /// Simplifies access to private code. - private static IReflectionHelper Reflection; + private static IReflectionHelper Reflection = null!; /********* @@ -56,12 +54,12 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches /// The NPC for which the dialogue is being parsed. /// The exception thrown by the wrapped method, if any. /// Returns the exception to throw, if any. - private static Exception Finalize_Constructor(Dialogue __instance, string masterDialogue, NPC speaker, Exception __exception) + private static Exception? Finalize_Constructor(Dialogue __instance, string masterDialogue, NPC? speaker, Exception? __exception) { if (__exception != null) { // log message - string name = !string.IsNullOrWhiteSpace(speaker?.Name) ? speaker.Name : null; + string? name = !string.IsNullOrWhiteSpace(speaker?.Name) ? speaker.Name : null; DialoguePatcher.MonitorForGame.Log($"Failed parsing dialogue string{(name != null ? $" for {name}" : "")}:\n{masterDialogue}\n{__exception.GetLogSummary()}", LogLevel.Error); // set default dialogue diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/EventPatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/EventPatcher.cs index 63674d09..073c62cc 100644 --- a/src/SMAPI.Mods.ErrorHandler/Patches/EventPatcher.cs +++ b/src/SMAPI.Mods.ErrorHandler/Patches/EventPatcher.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Diagnostics.CodeAnalysis; using HarmonyLib; @@ -18,7 +16,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches ** Fields *********/ /// Writes messages to the console and log file on behalf of the game. - private static IMonitor MonitorForGame; + private static IMonitor MonitorForGame = null!; /********* diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/GameLocationPatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/GameLocationPatcher.cs index 98aa4a38..9247fa48 100644 --- a/src/SMAPI.Mods.ErrorHandler/Patches/GameLocationPatcher.cs +++ b/src/SMAPI.Mods.ErrorHandler/Patches/GameLocationPatcher.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Diagnostics.CodeAnalysis; using HarmonyLib; @@ -19,8 +17,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches ** Fields *********/ /// Writes messages to the console and log file on behalf of the game. - private static IMonitor MonitorForGame; - + private static IMonitor MonitorForGame = null!; /********* ** Public methods @@ -54,7 +51,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches /// The precondition to be parsed. /// The exception thrown by the wrapped method, if any. /// Returns the exception to throw, if any. - private static Exception Finalize_CheckEventPrecondition(ref int __result, string precondition, Exception __exception) + private static Exception? Finalize_CheckEventPrecondition(ref int __result, string precondition, Exception? __exception) { if (__exception != null) { @@ -70,7 +67,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches /// The map whose tilesheets to update. /// The exception thrown by the wrapped method, if any. /// Returns the exception to throw, if any. - private static Exception Finalize_UpdateSeasonalTileSheets(GameLocation __instance, Map map, Exception __exception) + private static Exception? Finalize_UpdateSeasonalTileSheets(GameLocation __instance, Map map, Exception? __exception) { if (__exception != null) GameLocationPatcher.MonitorForGame.Log($"Failed updating seasonal tilesheets for location '{__instance.NameOrUniqueName}': \n{__exception}", LogLevel.Error); diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/IClickableMenuPatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/IClickableMenuPatcher.cs index 85ce8ac4..b65a695a 100644 --- a/src/SMAPI.Mods.ErrorHandler/Patches/IClickableMenuPatcher.cs +++ b/src/SMAPI.Mods.ErrorHandler/Patches/IClickableMenuPatcher.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Diagnostics.CodeAnalysis; using HarmonyLib; using StardewModdingAPI.Internal.Patching; diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/NpcPatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/NpcPatcher.cs index 5354f724..11f7ec69 100644 --- a/src/SMAPI.Mods.ErrorHandler/Patches/NpcPatcher.cs +++ b/src/SMAPI.Mods.ErrorHandler/Patches/NpcPatcher.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -20,7 +18,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches ** Fields *********/ /// Writes messages to the console and log file on behalf of the game. - private static IMonitor MonitorForGame; + private static IMonitor MonitorForGame = null!; /********* @@ -56,7 +54,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches /// The return value of the original method. /// The exception thrown by the wrapped method, if any. /// Returns the exception to throw, if any. - private static Exception Finalize_CurrentDialogue(NPC __instance, ref Stack __result, Exception __exception) + private static Exception? Finalize_CurrentDialogue(NPC __instance, ref Stack __result, Exception? __exception) { if (__exception == null) return null; @@ -73,7 +71,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches /// The patched method's return value. /// The exception thrown by the wrapped method, if any. /// Returns the exception to throw, if any. - private static Exception Finalize_ParseMasterSchedule(string rawData, NPC __instance, ref Dictionary __result, Exception __exception) + private static Exception? Finalize_ParseMasterSchedule(string rawData, NPC __instance, ref Dictionary __result, Exception? __exception) { if (__exception != null) { diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/ObjectPatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/ObjectPatcher.cs index 499718b0..09a6fbbd 100644 --- a/src/SMAPI.Mods.ErrorHandler/Patches/ObjectPatcher.cs +++ b/src/SMAPI.Mods.ErrorHandler/Patches/ObjectPatcher.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -59,7 +57,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches /// The patched method's return value. /// The exception thrown by the wrapped method, if any. /// Returns the exception to throw, if any. - private static Exception Finalize_Object_loadDisplayName(ref string __result, Exception __exception) + private static Exception? Finalize_Object_loadDisplayName(ref string __result, Exception? __exception) { if (__exception is KeyNotFoundException) { diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/SaveGamePatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/SaveGamePatcher.cs index 1941d2a8..490bbfb6 100644 --- a/src/SMAPI.Mods.ErrorHandler/Patches/SaveGamePatcher.cs +++ b/src/SMAPI.Mods.ErrorHandler/Patches/SaveGamePatcher.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -24,10 +22,10 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches ** Fields *********/ /// Writes messages to the console and log file. - private static IMonitor Monitor; + private static IMonitor Monitor = null!; /// A callback invoked when custom content is removed from the save data to avoid a crash. - private static Action OnContentRemoved; + private static Action OnContentRemoved = null!; /********* @@ -76,7 +74,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches /// The method to call after throws an exception. /// The exception thrown by the wrapped method, if any. /// Returns the exception to throw, if any. - private static Exception Finalize_LoadFarmType(Exception __exception) + private static Exception? Finalize_LoadFarmType(Exception? __exception) { // missing custom farm type if (__exception?.Message.Contains("not a valid farm type") == true && !int.TryParse(SaveGame.loaded.whichFarm, out _)) @@ -110,7 +108,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches /// Remove content which no longer exists in the game data. /// The current game location. /// The NPC data. - private static bool RemoveBrokenContent(GameLocation location, IDictionary npcs) + private static bool RemoveBrokenContent(GameLocation? location, IDictionary npcs) { bool removedAny = false; if (location == null) diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/SpriteBatchPatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/SpriteBatchPatcher.cs index b4c03bb9..d369e0ef 100644 --- a/src/SMAPI.Mods.ErrorHandler/Patches/SpriteBatchPatcher.cs +++ b/src/SMAPI.Mods.ErrorHandler/Patches/SpriteBatchPatcher.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Diagnostics.CodeAnalysis; using HarmonyLib; @@ -30,9 +28,9 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches /********* ** Private methods *********/ - /// The method to call after . + /// The method to call after . /// The texture to validate. - private static void After_CheckValid(Texture2D texture) + private static void After_CheckValid(Texture2D? texture) { if (texture?.IsDisposed == true) throw new ObjectDisposedException("Cannot draw this texture because it's disposed."); diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/UtilityPatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/UtilityPatcher.cs index 108ed585..6d75a581 100644 --- a/src/SMAPI.Mods.ErrorHandler/Patches/UtilityPatcher.cs +++ b/src/SMAPI.Mods.ErrorHandler/Patches/UtilityPatcher.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Diagnostics.CodeAnalysis; using HarmonyLib; @@ -35,7 +33,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches /// The delimiter by which to split the text description. /// The exception thrown by the wrapped method, if any. /// Returns the exception to throw, if any. - private static Exception Finalize_GetItemFromStandardTextDescription(string description, char delimiter, ref Exception __exception) + private static Exception? Finalize_GetItemFromStandardTextDescription(string description, char delimiter, ref Exception? __exception) { return __exception != null ? new FormatException($"Failed to parse item text description \"{description}\" with delimiter \"{delimiter}\".", __exception) diff --git a/src/SMAPI.Mods.SaveBackup/ModEntry.cs b/src/SMAPI.Mods.SaveBackup/ModEntry.cs index b2b41ca6..a79c092f 100644 --- a/src/SMAPI.Mods.SaveBackup/ModEntry.cs +++ b/src/SMAPI.Mods.SaveBackup/ModEntry.cs @@ -1,7 +1,6 @@ -#nullable disable - using System; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.IO.Compression; using System.Linq; @@ -82,7 +81,7 @@ namespace StardewModdingAPI.Mods.SaveBackup } // compress backup if possible - if (!this.TryCompress(fallbackDir.FullName, targetFile, out Exception compressError)) + if (!this.TryCompress(fallbackDir.FullName, targetFile, out Exception? compressError)) { this.Monitor.Log(Constants.TargetPlatform != GamePlatform.Android ? $"Backed up to {fallbackDir.FullName}." // expected to fail on Android @@ -142,7 +141,7 @@ namespace StardewModdingAPI.Mods.SaveBackup /// The destination file to create. /// The error which occurred trying to compress, if applicable. This is if compression isn't supported on this platform. /// Returns whether compression succeeded. - private bool TryCompress(string sourcePath, FileInfo destination, out Exception error) + private bool TryCompress(string sourcePath, FileInfo destination, [NotNullWhen(false)] out Exception? error) { try { @@ -210,7 +209,7 @@ namespace StardewModdingAPI.Mods.SaveBackup /// A filter which matches the files or directories to copy, or null to copy everything. /// Derived from the SMAPI installer code. /// Returns whether any files were copied. - private bool RecursiveCopy(FileSystemInfo source, DirectoryInfo targetFolder, Func filter, bool copyRoot = true) + private bool RecursiveCopy(FileSystemInfo source, DirectoryInfo targetFolder, Func? filter, bool copyRoot = true) { if (!source.Exists || filter?.Invoke(source) == false) return false; @@ -244,7 +243,7 @@ namespace StardewModdingAPI.Mods.SaveBackup private bool MatchSaveFolders(DirectoryInfo savesFolder, FileSystemInfo entry) { // only need to filter top-level entries - string parentPath = (entry as FileInfo)?.DirectoryName ?? (entry as DirectoryInfo)?.Parent?.FullName; + string? parentPath = (entry as FileInfo)?.DirectoryName ?? (entry as DirectoryInfo)?.Parent?.FullName; if (parentPath != savesFolder.FullName) return true; -- cgit