From 761f2d952b073b78a06453768992b7b0fd4c6463 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 10 Apr 2022 21:34:18 -0400 Subject: [PATCH] enable nullable annotations in mod data models (#837) --- src/SMAPI.Tests/Core/ModResolverTests.cs | 29 +++++++++++------ .../Framework/ModData/MetadataModel.cs | 4 +-- .../Framework/ModData/ModDataField.cs | 10 +++--- .../Framework/ModData/ModDataModel.cs | 32 +++++++++++-------- .../Framework/ModData/ModDataRecord.cs | 10 +++--- .../ModData/ModDataRecordVersionedFields.cs | 28 +++++++++------- .../Framework/ModData/ModDatabase.cs | 14 ++++---- src/SMAPI.Toolkit/Properties/AssemblyInfo.cs | 1 + .../Serialization/Models/Manifest.cs | 2 +- 9 files changed, 73 insertions(+), 57 deletions(-) diff --git a/src/SMAPI.Tests/Core/ModResolverTests.cs b/src/SMAPI.Tests/Core/ModResolverTests.cs index e1b56559..b8649a7d 100644 --- a/src/SMAPI.Tests/Core/ModResolverTests.cs +++ b/src/SMAPI.Tests/Core/ModResolverTests.cs @@ -145,7 +145,7 @@ namespace SMAPI.Tests.Core { // arrange Mock mock = this.GetMetadata("Mod A", Array.Empty(), allowStatusChange: true); - this.SetupMetadataForValidation(mock, new ModDataRecordVersionedFields + this.SetupMetadataForValidation(mock, new ModDataRecordVersionedFields(this.GetModDataRecord()) { Status = ModStatus.AssumeBroken }); @@ -216,9 +216,9 @@ namespace SMAPI.Tests.Core File.WriteAllText(Path.Combine(modFolder, manifest.EntryDll!), ""); // arrange - Mock mock = new Mock(MockBehavior.Strict); + Mock mock = new(MockBehavior.Strict); mock.Setup(p => p.Status).Returns(ModMetadataStatus.Found); - mock.Setup(p => p.DataRecord).Returns(() => null); + mock.Setup(p => p.DataRecord).Returns(this.GetModDataRecordVersionedFields()); mock.Setup(p => p.Manifest).Returns(manifest); mock.Setup(p => p.DirectoryPath).Returns(modFolder); @@ -265,7 +265,7 @@ namespace SMAPI.Tests.Core public void ProcessDependencies_Skips_Failed() { // arrange - Mock mock = new Mock(MockBehavior.Strict); + Mock mock = new(MockBehavior.Strict); mock.Setup(p => p.Status).Returns(ModMetadataStatus.Failed); // act @@ -380,7 +380,7 @@ namespace SMAPI.Tests.Core Mock modA = this.GetMetadata("Mod A"); Mock modB = this.GetMetadata("Mod B", dependencies: new[] { "Mod A" }); Mock modC = this.GetMetadata("Mod C", dependencies: new[] { "Mod B" }, allowStatusChange: true); - Mock modD = new Mock(MockBehavior.Strict); + Mock modD = new(MockBehavior.Strict); modD.Setup(p => p.Manifest).Returns(null); modD.Setup(p => p.Status).Returns(ModMetadataStatus.Failed); @@ -516,8 +516,8 @@ namespace SMAPI.Tests.Core /// Whether the code being tested is allowed to change the mod status. private Mock GetMetadata(IManifest manifest, bool allowStatusChange = false) { - Mock mod = new Mock(MockBehavior.Strict); - mod.Setup(p => p.DataRecord).Returns(() => null); + Mock mod = new(MockBehavior.Strict); + mod.Setup(p => p.DataRecord).Returns(this.GetModDataRecordVersionedFields()); mod.Setup(p => p.Status).Returns(ModMetadataStatus.Found); mod.Setup(p => p.DisplayName).Returns(manifest.UniqueID); mod.Setup(p => p.Manifest).Returns(manifest); @@ -538,11 +538,22 @@ namespace SMAPI.Tests.Core private void SetupMetadataForValidation(Mock mod, ModDataRecordVersionedFields? modRecord = null) { mod.Setup(p => p.Status).Returns(ModMetadataStatus.Found); - mod.Setup(p => p.DataRecord).Returns(() => null); mod.Setup(p => p.Manifest).Returns(this.GetManifest()); mod.Setup(p => p.DirectoryPath).Returns(Path.GetTempPath()); - mod.Setup(p => p.DataRecord).Returns(modRecord); + mod.Setup(p => p.DataRecord).Returns(modRecord ?? this.GetModDataRecordVersionedFields()); mod.Setup(p => p.GetUpdateKeys(It.IsAny())).Returns(Enumerable.Empty()); } + + /// Generate a default mod data record. + private ModDataRecord GetModDataRecord() + { + return new("Default Display Name", new ModDataModel("Sample ID", null, ModWarning.None)); + } + + /// Generate a default mod data versioned fields instance. + private ModDataRecordVersionedFields GetModDataRecordVersionedFields() + { + return new ModDataRecordVersionedFields(this.GetModDataRecord()); + } } } diff --git a/src/SMAPI.Toolkit/Framework/ModData/MetadataModel.cs b/src/SMAPI.Toolkit/Framework/ModData/MetadataModel.cs index 3fa70615..da678ac9 100644 --- a/src/SMAPI.Toolkit/Framework/ModData/MetadataModel.cs +++ b/src/SMAPI.Toolkit/Framework/ModData/MetadataModel.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Collections.Generic; namespace StardewModdingAPI.Toolkit.Framework.ModData @@ -11,6 +9,6 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData ** Accessors ********/ /// Extra metadata about mods. - public IDictionary ModData { get; set; } + public IDictionary ModData { get; } = new Dictionary(); } } diff --git a/src/SMAPI.Toolkit/Framework/ModData/ModDataField.cs b/src/SMAPI.Toolkit/Framework/ModData/ModDataField.cs index 46cb81e1..9674d283 100644 --- a/src/SMAPI.Toolkit/Framework/ModData/ModDataField.cs +++ b/src/SMAPI.Toolkit/Framework/ModData/ModDataField.cs @@ -1,5 +1,3 @@ -#nullable disable - using System.Linq; namespace StardewModdingAPI.Toolkit.Framework.ModData @@ -20,10 +18,10 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData public bool IsDefault { get; } /// The lowest version in the range, or null for all past versions. - public ISemanticVersion LowerVersion { get; } + public ISemanticVersion? LowerVersion { get; } /// The highest version in the range, or null for all future versions. - public ISemanticVersion UpperVersion { get; } + public ISemanticVersion? UpperVersion { get; } /********* @@ -35,7 +33,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /// Whether this field should only be applied if it's not already set. /// The lowest version in the range, or null for all past versions. /// The highest version in the range, or null for all future versions. - public ModDataField(ModDataFieldKey key, string value, bool isDefault, ISemanticVersion lowerVersion, ISemanticVersion upperVersion) + public ModDataField(ModDataFieldKey key, string value, bool isDefault, ISemanticVersion? lowerVersion, ISemanticVersion? upperVersion) { this.Key = key; this.Value = value; @@ -46,7 +44,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /// Get whether this data field applies for the given manifest. /// The mod manifest. - public bool IsMatch(IManifest manifest) + public bool IsMatch(IManifest? manifest) { return manifest?.Version != null // ignore invalid manifest diff --git a/src/SMAPI.Toolkit/Framework/ModData/ModDataModel.cs b/src/SMAPI.Toolkit/Framework/ModData/ModDataModel.cs index 4d96a555..5912fb87 100644 --- a/src/SMAPI.Toolkit/Framework/ModData/ModDataModel.cs +++ b/src/SMAPI.Toolkit/Framework/ModData/ModDataModel.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Linq; @@ -16,7 +14,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData ** Accessors *********/ /// The mod's current unique ID. - public string ID { get; set; } + public string ID { get; } /// The former mod IDs (if any). /// @@ -25,14 +23,14 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /// ID, if any. If the mod's ID changed over time, multiple variants can be separated by the /// | character. /// - public string FormerIDs { get; set; } + public string? FormerIDs { get; } /// The mod warnings to suppress, even if they'd normally be shown. - public ModWarning SuppressWarnings { get; set; } + public ModWarning SuppressWarnings { get; } /// This field stores properties that aren't mapped to another field before they're parsed into . [JsonExtensionData] - public IDictionary ExtensionData { get; set; } + public IDictionary ExtensionData { get; } = new Dictionary(); /// The versioned field data. /// @@ -52,6 +50,17 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /********* ** Public methods *********/ + /// Construct an instance. + /// The mod's current unique ID. + /// The former mod IDs (if any). + /// The mod warnings to suppress, even if they'd normally be shown. + public ModDataModel(string id, string? formerIds, ModWarning suppressWarnings) + { + this.ID = id; + this.FormerIDs = formerIds; + this.SuppressWarnings = suppressWarnings; + } + /// Get a parsed representation of the . public IEnumerable GetFields() { @@ -61,8 +70,8 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData string packedKey = pair.Key; string value = pair.Value; bool isDefault = false; - ISemanticVersion lowerVersion = null; - ISemanticVersion upperVersion = null; + ISemanticVersion? lowerVersion = null; + ISemanticVersion? upperVersion = null; // parse string[] parts = packedKey.Split('|').Select(p => p.Trim()).ToArray(); @@ -113,11 +122,8 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData [OnDeserialized] private void OnDeserialized(StreamingContext context) { - if (this.ExtensionData != null) - { - this.Fields = this.ExtensionData.ToDictionary(p => p.Key, p => p.Value.ToString()); - this.ExtensionData = null; - } + this.Fields = this.ExtensionData.ToDictionary(p => p.Key, p => p.Value.ToString()); + this.ExtensionData.Clear(); } } } diff --git a/src/SMAPI.Toolkit/Framework/ModData/ModDataRecord.cs b/src/SMAPI.Toolkit/Framework/ModData/ModDataRecord.cs index 4c09e1ba..e9ece387 100644 --- a/src/SMAPI.Toolkit/Framework/ModData/ModDataRecord.cs +++ b/src/SMAPI.Toolkit/Framework/ModData/ModDataRecord.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Linq; @@ -22,7 +20,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData public string[] FormerIDs { get; } /// The mod warnings to suppress, even if they'd normally be shown. - public ModWarning SuppressWarnings { get; set; } + public ModWarning SuppressWarnings { get; } /// The versioned field data. public ModDataField[] Fields { get; } @@ -72,9 +70,9 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData } /// Get the default update key for this mod, if any. - public string GetDefaultUpdateKey() + public string? GetDefaultUpdateKey() { - string updateKey = this.Fields.FirstOrDefault(p => p.Key == ModDataFieldKey.UpdateKey && p.IsDefault)?.Value; + string? updateKey = this.Fields.FirstOrDefault(p => p.Key == ModDataFieldKey.UpdateKey && p.IsDefault)?.Value; return !string.IsNullOrWhiteSpace(updateKey) ? updateKey : null; @@ -84,7 +82,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /// The manifest to match. public ModDataRecordVersionedFields GetVersionedFields(IManifest manifest) { - ModDataRecordVersionedFields parsed = new() { DisplayName = this.DisplayName, DataRecord = this }; + ModDataRecordVersionedFields parsed = new(this); foreach (ModDataField field in this.Fields.Where(field => field.IsMatch(manifest))) { switch (field.Key) diff --git a/src/SMAPI.Toolkit/Framework/ModData/ModDataRecordVersionedFields.cs b/src/SMAPI.Toolkit/Framework/ModData/ModDataRecordVersionedFields.cs index b599b343..65fa424e 100644 --- a/src/SMAPI.Toolkit/Framework/ModData/ModDataRecordVersionedFields.cs +++ b/src/SMAPI.Toolkit/Framework/ModData/ModDataRecordVersionedFields.cs @@ -1,5 +1,3 @@ -#nullable disable - namespace StardewModdingAPI.Toolkit.Framework.ModData { /// The versioned fields from a for a specific manifest. @@ -9,24 +7,32 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData ** Accessors *********/ /// The underlying data record. - public ModDataRecord DataRecord { get; set; } + public ModDataRecord DataRecord { get; } - /// The default mod name to display when the name isn't available (e.g. during dependency checks). - public string DisplayName { get; set; } - - /// The update key to apply. - public string UpdateKey { get; set; } + /// The update key to apply (if any). + public string? UpdateKey { get; set; } /// The predefined compatibility status. public ModStatus Status { get; set; } = ModStatus.None; /// A reason phrase for the , or null to use the default reason. - public string StatusReasonPhrase { get; set; } + public string? StatusReasonPhrase { get; set; } /// Technical details shown in TRACE logs for the , or null to omit it. - public string StatusReasonDetails { get; set; } + public string? StatusReasonDetails { get; set; } /// The upper version for which the applies (if any). - public ISemanticVersion StatusUpperVersion { get; set; } + public ISemanticVersion? StatusUpperVersion { get; set; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The underlying data record. + public ModDataRecordVersionedFields(ModDataRecord dataRecord) + { + this.DataRecord = dataRecord; + } } } diff --git a/src/SMAPI.Toolkit/Framework/ModData/ModDatabase.cs b/src/SMAPI.Toolkit/Framework/ModData/ModDatabase.cs index a5237334..168b8aac 100644 --- a/src/SMAPI.Toolkit/Framework/ModData/ModDatabase.cs +++ b/src/SMAPI.Toolkit/Framework/ModData/ModDatabase.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Linq; @@ -16,7 +14,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData private readonly ModDataRecord[] Records; /// Get an update URL for an update key (if valid). - private readonly Func GetUpdateUrl; + private readonly Func GetUpdateUrl; /********* @@ -29,7 +27,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /// Construct an instance. /// The underlying mod data records indexed by default display name. /// Get an update URL for an update key (if valid). - public ModDatabase(IEnumerable records, Func getUpdateUrl) + public ModDatabase(IEnumerable records, Func getUpdateUrl) { this.Records = records.ToArray(); this.GetUpdateUrl = getUpdateUrl; @@ -43,7 +41,7 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /// Get a mod data record. /// The unique mod ID. - public ModDataRecord Get(string modID) + public ModDataRecord? Get(string? modID) { return !string.IsNullOrWhiteSpace(modID) ? this.Records.FirstOrDefault(p => p.HasID(modID)) @@ -52,11 +50,11 @@ namespace StardewModdingAPI.Toolkit.Framework.ModData /// Get the mod page URL for a mod (if available). /// The unique mod ID. - public string GetModPageUrlFor(string id) + public string? GetModPageUrlFor(string? id) { // get update key - ModDataRecord record = this.Get(id); - ModDataField updateKeyField = record?.Fields.FirstOrDefault(p => p.Key == ModDataFieldKey.UpdateKey); + ModDataRecord? record = this.Get(id); + ModDataField? updateKeyField = record?.Fields.FirstOrDefault(p => p.Key == ModDataFieldKey.UpdateKey); if (updateKeyField == null) return null; diff --git a/src/SMAPI.Toolkit/Properties/AssemblyInfo.cs b/src/SMAPI.Toolkit/Properties/AssemblyInfo.cs index eede4562..6f5dffbe 100644 --- a/src/SMAPI.Toolkit/Properties/AssemblyInfo.cs +++ b/src/SMAPI.Toolkit/Properties/AssemblyInfo.cs @@ -2,4 +2,5 @@ using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("StardewModdingAPI")] [assembly: InternalsVisibleTo("SMAPI.Installer")] +[assembly: InternalsVisibleTo("SMAPI.Tests")] [assembly: InternalsVisibleTo("SMAPI.Web")] diff --git a/src/SMAPI.Toolkit/Serialization/Models/Manifest.cs b/src/SMAPI.Toolkit/Serialization/Models/Manifest.cs index 01010602..da3ad608 100644 --- a/src/SMAPI.Toolkit/Serialization/Models/Manifest.cs +++ b/src/SMAPI.Toolkit/Serialization/Models/Manifest.cs @@ -46,7 +46,7 @@ namespace StardewModdingAPI.Toolkit.Serialization.Models /// Any manifest fields which didn't match a valid field. [JsonExtensionData] - public IDictionary ExtraFields { get; set; } = new Dictionary(); + public IDictionary ExtraFields { get; } = new Dictionary(); /*********