From 4adf8611131a5d86b15f017a42a0366837d14528 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 13 Apr 2022 21:07:43 -0400 Subject: enable nullable annotations in the rest of SMAPI core (#837) --- src/SMAPI/Metadata/CoreAssetPropagator.cs | 47 ++++++++++++++++--------------- 1 file changed, 25 insertions(+), 22 deletions(-) (limited to 'src/SMAPI/Metadata') diff --git a/src/SMAPI/Metadata/CoreAssetPropagator.cs b/src/SMAPI/Metadata/CoreAssetPropagator.cs index b7cec72c..f1b9af9a 100644 --- a/src/SMAPI/Metadata/CoreAssetPropagator.cs +++ b/src/SMAPI/Metadata/CoreAssetPropagator.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -712,7 +710,7 @@ namespace StardewModdingAPI.Metadata bool isPaintMask = assetName.BaseName.EndsWith(paintMaskSuffix, StringComparison.OrdinalIgnoreCase); // get building type - string type = Path.GetFileName(assetName.BaseName)!; + string type = Path.GetFileName(assetName.BaseName); if (isPaintMask) type = type.Substring(0, type.Length - paintMaskSuffix.Length); @@ -749,7 +747,7 @@ namespace StardewModdingAPI.Metadata if (!ignoreWorld) { - foreach (var location in this.GetLocations()) + foreach (GameLocation location in this.GetLocations()) { foreach (MapSeat seat in location.mapSeats.Where(p => p != null)) { @@ -783,7 +781,7 @@ namespace StardewModdingAPI.Metadata // update sprites Texture2D texture = content.Load(assetName.BaseName); - foreach (var entry in critters) + foreach (Critter entry in critters) entry.sprite.spriteTexture = texture; return critters.Length; @@ -799,16 +797,16 @@ namespace StardewModdingAPI.Metadata foreach (GameLocation location in this.GetLocations()) { - IEnumerable doors = location.interiorDoors?.Doors; + IEnumerable? doors = location.interiorDoors?.Doors; if (doors == null) continue; - foreach (InteriorDoor door in doors) + foreach (InteriorDoor? door in doors) { if (door?.Sprite == null) continue; - string curKey = this.Reflection.GetField(door.Sprite, "textureName").GetValue(); + string? curKey = this.Reflection.GetField(door.Sprite, "textureName").GetValue(); if (this.IsSameBaseName(assetName, curKey)) door.Sprite.texture = texture.Value; } @@ -933,7 +931,7 @@ namespace StardewModdingAPI.Metadata // warping onto the wrong tile (or even off-screen) if a patch changes the farmhouse // map on location change. if (playerPos.HasValue) - Game1.player.Position = playerPos.Value; + Game1.player!.Position = playerPos.Value; } /// Reload the disposition data for matching NPCs. @@ -1003,7 +1001,11 @@ namespace StardewModdingAPI.Metadata { GameLocation adventureGuild = Game1.getLocationFromName("AdventureGuild"); if (adventureGuild != null) - characters.Add(new { Npc = this.Reflection.GetField(adventureGuild, "Gil").GetValue(), AssetName = gilKey }); + { + NPC? gil = this.Reflection.GetField(adventureGuild, "Gil").GetValue(); + if (gil != null) + characters.Add(new { Npc = gil, AssetName = gilKey }); + } } } @@ -1029,7 +1031,7 @@ namespace StardewModdingAPI.Metadata foreach (Farmer player in players) { - this.Reflection.GetField>>>(typeof(FarmerRenderer), "_recolorOffsets").GetValue().Remove(player.getTexture()); + this.Reflection.GetField>>>(typeof(FarmerRenderer), "_recolorOffsets").GetValue()?.Remove(player.getTexture()); player.FarmerRenderer.MarkSpriteDirty(); } @@ -1048,11 +1050,12 @@ namespace StardewModdingAPI.Metadata { // get suspension bridges field var field = this.Reflection.GetField>(location, nameof(IslandNorth.suspensionBridges), required: false); + // ReSharper disable once ConditionIsAlwaysTrueOrFalse -- field is nullable when required: false if (field == null || !typeof(IEnumerable).IsAssignableFrom(field.FieldInfo.FieldType)) continue; // update textures - foreach (SuspensionBridge bridge in field.GetValue()) + foreach (SuspensionBridge bridge in field.GetValue()!) this.Reflection.GetField(bridge, "_texture").SetValue(texture.Value); } @@ -1158,7 +1161,7 @@ namespace StardewModdingAPI.Metadata Game1.samBandName = content.LoadString("Strings/StringsFromCSFiles:Game1.cs.2156"); Game1.elliottBookName = content.LoadString("Strings/StringsFromCSFiles:Game1.cs.2157"); - string[] dayNames = this.Reflection.GetField(typeof(Game1), "_shortDayDisplayName").GetValue(); + string[] dayNames = this.Reflection.GetField(typeof(Game1), "_shortDayDisplayName").GetValue()!; dayNames[0] = content.LoadString("Strings/StringsFromCSFiles:Game1.cs.3042"); dayNames[1] = content.LoadString("Strings/StringsFromCSFiles:Game1.cs.3043"); dayNames[2] = content.LoadString("Strings/StringsFromCSFiles:Game1.cs.3044"); @@ -1227,7 +1230,7 @@ namespace StardewModdingAPI.Metadata { foreach (Building building in buildableLocation.buildings) { - GameLocation indoors = building.indoors.Value; + GameLocation? indoors = building.indoors.Value; if (indoors != null) yield return new LocationInfo(indoors, building); } @@ -1238,19 +1241,19 @@ namespace StardewModdingAPI.Metadata /// Get whether two asset names are equivalent if you ignore the locale code. /// The first value to compare. /// The second value to compare. - private bool IsSameBaseName(IAssetName left, string right) + private bool IsSameBaseName(IAssetName? left, string? right) { if (left is null || right is null) return false; - IAssetName parsedB = this.ParseAssetNameOrNull(right); + IAssetName? parsedB = this.ParseAssetNameOrNull(right); return this.IsSameBaseName(left, parsedB); } /// Get whether two asset names are equivalent if you ignore the locale code. /// The first value to compare. /// The second value to compare. - private bool IsSameBaseName(IAssetName left, IAssetName right) + private bool IsSameBaseName(IAssetName? left, IAssetName? right) { if (left is null || right is null) return false; @@ -1260,7 +1263,7 @@ namespace StardewModdingAPI.Metadata /// Normalize an asset key to match the cache key and assert that it's valid, but don't raise an error for null or empty values. /// The asset key to normalize. - private IAssetName ParseAssetNameOrNull(string path) + private IAssetName? ParseAssetNameOrNull(string? path) { if (string.IsNullOrWhiteSpace(path)) return null; @@ -1270,7 +1273,7 @@ namespace StardewModdingAPI.Metadata /// Get the segments in a path (e.g. 'a/b' is 'a' and 'b'). /// The path to check. - private string[] GetSegments(string path) + private string[] GetSegments(string? path) { return path != null ? PathUtilities.GetSegments(path) @@ -1280,7 +1283,7 @@ namespace StardewModdingAPI.Metadata /// Load a texture, and dispose the old one if is enabled and it's different from the new instance. /// The previous texture to dispose. /// The asset key to load. - private Texture2D LoadAndDisposeIfNeeded(Texture2D oldTexture, string key) + private Texture2D LoadAndDisposeIfNeeded(Texture2D? oldTexture, string key) { // if aggressive memory optimizations are enabled, load the asset from the disposable // content manager and dispose the old instance if needed. @@ -1322,7 +1325,7 @@ namespace StardewModdingAPI.Metadata public GameLocation Location { get; } /// The building which contains the location, if any. - public Building ParentBuilding { get; } + public Building? ParentBuilding { get; } /********* @@ -1331,7 +1334,7 @@ namespace StardewModdingAPI.Metadata /// Construct an instance. /// The location instance. /// The building which contains the location, if any. - public LocationInfo(GameLocation location, Building parentBuilding) + public LocationInfo(GameLocation location, Building? parentBuilding) { this.Location = location; this.ParentBuilding = parentBuilding; -- cgit