From d706a25053cdc5d9f1ccc2c09dc3913f835c3f78 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 7 Apr 2022 02:33:23 -0400 Subject: enable nullable annotations for most of the SMAPI toolkit (#837) --- .../Framework/UpdateData/UpdateKey.cs | 32 +++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/SMAPI.Toolkit/Framework/UpdateData') diff --git a/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs b/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs index ec94ed51..d40d8f2b 100644 --- a/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs +++ b/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs @@ -1,6 +1,5 @@ -#nullable disable - using System; +using System.Diagnostics.CodeAnalysis; namespace StardewModdingAPI.Toolkit.Framework.UpdateData { @@ -17,10 +16,11 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData public ModSiteKey Site { get; } /// The mod ID within the repository. - public string ID { get; } + [MemberNotNullWhen(true, nameof(LooksValid))] + public string? ID { get; } /// If specified, a substring in download names/descriptions to match. - public string Subkey { get; } + public string? Subkey { get; } /// Whether the update key seems to be valid. public bool LooksValid { get; } @@ -34,9 +34,9 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData /// The mod site containing the mod. /// The mod ID within the site. /// If specified, a substring in download names/descriptions to match. - public UpdateKey(string rawText, ModSiteKey site, string id, string subkey) + public UpdateKey(string? rawText, ModSiteKey site, string? id, string? subkey) { - this.RawText = rawText?.Trim(); + this.RawText = rawText?.Trim() ?? string.Empty; this.Site = site; this.ID = id?.Trim(); this.Subkey = subkey?.Trim(); @@ -49,19 +49,19 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData /// The mod site containing the mod. /// The mod ID within the site. /// If specified, a substring in download names/descriptions to match. - public UpdateKey(ModSiteKey site, string id, string subkey) + public UpdateKey(ModSiteKey site, string? id, string? subkey) : this(UpdateKey.GetString(site, id, subkey), site, id, subkey) { } /// Parse a raw update key. /// The raw update key to parse. - public static UpdateKey Parse(string raw) + public static UpdateKey Parse(string? raw) { // extract site + ID - string rawSite; - string id; + string? rawSite; + string? id; { - string[] parts = raw?.Trim().Split(':'); - if (parts == null || parts.Length != 2) + string[]? parts = raw?.Trim().Split(':'); + if (parts?.Length != 2) return new UpdateKey(raw, ModSiteKey.Unknown, null, null); rawSite = parts[0].Trim(); @@ -71,7 +71,7 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData id = null; // extract subkey - string subkey = null; + string? subkey = null; if (id != null) { string[] parts = id.Split('@'); @@ -111,7 +111,7 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData /// Indicates whether the current object is equal to another object of the same type. /// An object to compare with this object. - public bool Equals(UpdateKey other) + public bool Equals(UpdateKey? other) { if (!this.LooksValid) { @@ -129,7 +129,7 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData /// Determines whether the specified object is equal to the current object. /// The object to compare with the current object. - public override bool Equals(object obj) + public override bool Equals(object? obj) { return obj is UpdateKey other && this.Equals(other); } @@ -145,7 +145,7 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData /// The mod site containing the mod. /// The mod ID within the repository. /// If specified, a substring in download names/descriptions to match. - public static string GetString(ModSiteKey site, string id, string subkey = null) + public static string GetString(ModSiteKey site, string? id, string? subkey = null) { return $"{site}:{id}{subkey}".Trim(); } -- cgit