From 0ad9fbddddbf9edfd847c507d70e10d2f8ce559b Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 12 Jan 2018 01:24:49 -0500 Subject: fix semantic versions always ignoring `-0` tag (#421) --- docs/release-notes.md | 1 + src/SMAPI.Common/SemanticVersionImpl.cs | 4 +--- src/SMAPI.Tests/Utilities/SemanticVersionTests.cs | 17 ++++++++++++++ src/SMAPI/Framework/LegacyManifestVersion.cs | 26 ++++++++++++++++++++++ .../Framework/Serialisation/SFieldConverter.cs | 2 +- src/SMAPI/StardewModdingAPI.csproj | 1 + 6 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 src/SMAPI/Framework/LegacyManifestVersion.cs diff --git a/docs/release-notes.md b/docs/release-notes.md index 87ef671e..4b6a7ba5 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -2,6 +2,7 @@ ## 2.4 * For players: * SMAPI now fixes curly quotes in `config.json` if possible. + * Fixed semantic versions always ignoring `-0` tag. * Fixed rare issues caused by assembly references being incorrectly loaded twice. * For the [log parser][]: diff --git a/src/SMAPI.Common/SemanticVersionImpl.cs b/src/SMAPI.Common/SemanticVersionImpl.cs index 53cf5a21..1c713b47 100644 --- a/src/SMAPI.Common/SemanticVersionImpl.cs +++ b/src/SMAPI.Common/SemanticVersionImpl.cs @@ -190,9 +190,7 @@ namespace StardewModdingAPI.Common private string GetNormalisedTag(string tag) { tag = tag?.Trim(); - if (string.IsNullOrWhiteSpace(tag) || tag == "0") // '0' from incorrect examples in old SMAPI documentation - return null; - return tag; + return !string.IsNullOrWhiteSpace(tag) ? tag : null; } } } diff --git a/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs b/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs index d3e0988e..f1a72012 100644 --- a/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs +++ b/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs @@ -33,6 +33,7 @@ namespace StardewModdingAPI.Tests.Utilities [TestCase(3000, 4000, 5000, null, ExpectedResult = "3000.4000.5000")] [TestCase(1, 2, 3, "", ExpectedResult = "1.2.3")] [TestCase(1, 2, 3, " ", ExpectedResult = "1.2.3")] + [TestCase(1, 2, 3, "0", ExpectedResult = "1.2.3-0")] [TestCase(1, 2, 3, "some-tag.4", ExpectedResult = "1.2.3-some-tag.4")] [TestCase(1, 2, 3, "some-tag.4 ", ExpectedResult = "1.2.3-some-tag.4")] public string Constructor_FromParts(int major, int minor, int patch, string tag) @@ -270,6 +271,22 @@ namespace StardewModdingAPI.Tests.Utilities Assert.IsTrue(version.IsOlderThan(new SemanticVersion("1.2.30")), "The game version should be considered older than the later semantic versions."); } + /**** + ** LegacyManifestVersion + ****/ + [Test(Description = "Assert that the LegacyManifestVersion subclass correctly parses legacy manifest versions.")] + [TestCase(1, 0, 0, null, ExpectedResult = "1.0")] + [TestCase(3000, 4000, 5000, null, ExpectedResult = "3000.4000.5000")] + [TestCase(1, 2, 3, "", ExpectedResult = "1.2.3")] + [TestCase(1, 2, 3, " ", ExpectedResult = "1.2.3")] + [TestCase(1, 2, 3, "0", ExpectedResult = "1.2.3")] // special case: drop '0' tag for legacy manifest versions + [TestCase(1, 2, 3, "some-tag.4", ExpectedResult = "1.2.3-some-tag.4")] + [TestCase(1, 2, 3, "some-tag.4 ", ExpectedResult = "1.2.3-some-tag.4")] + public string LegacyManifestVersion(int major, int minor, int patch, string tag) + { + return new LegacyManifestVersion(major, minor, patch, tag).ToString(); + } + /********* ** Private methods diff --git a/src/SMAPI/Framework/LegacyManifestVersion.cs b/src/SMAPI/Framework/LegacyManifestVersion.cs new file mode 100644 index 00000000..454b9137 --- /dev/null +++ b/src/SMAPI/Framework/LegacyManifestVersion.cs @@ -0,0 +1,26 @@ +using Newtonsoft.Json; + +namespace StardewModdingAPI.Framework +{ + /// An implementation of that hamdles the legacy version format. + internal class LegacyManifestVersion : SemanticVersion + { + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The major version incremented for major API changes. + /// The minor version incremented for backwards-compatible changes. + /// The patch version for backwards-compatible bug fixes. + /// An optional build tag. + [JsonConstructor] + public LegacyManifestVersion(int majorVersion, int minorVersion, int patchVersion, string build = null) + : base( + majorVersion, + minorVersion, + patchVersion, + build != "0" ? build : null // '0' from incorrect examples in old SMAPI documentation + ) + { } + } +} diff --git a/src/SMAPI/Framework/Serialisation/SFieldConverter.cs b/src/SMAPI/Framework/Serialisation/SFieldConverter.cs index 917c950d..6e068599 100644 --- a/src/SMAPI/Framework/Serialisation/SFieldConverter.cs +++ b/src/SMAPI/Framework/Serialisation/SFieldConverter.cs @@ -51,7 +51,7 @@ namespace StardewModdingAPI.Framework.Serialisation int minor = obj.Value(nameof(ISemanticVersion.MinorVersion)); int patch = obj.Value(nameof(ISemanticVersion.PatchVersion)); string build = obj.Value(nameof(ISemanticVersion.Build)); - return new SemanticVersion(major, minor, patch, build); + return new LegacyManifestVersion(major, minor, patch, build); } case JTokenType.String: diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj index f76ac439..c9c302f5 100644 --- a/src/SMAPI/StardewModdingAPI.csproj +++ b/src/SMAPI/StardewModdingAPI.csproj @@ -86,6 +86,7 @@ Properties\GlobalAssemblyInfo.cs + -- cgit