From 8789b7efa816aab0f4ce9d3149c26b8033e0b0a5 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 2 Sep 2021 18:54:56 -0400 Subject: prepare path utilities for the upcoming Stardew Valley 1.5.5 The game will use Linux-style paths for assets on all platforms, which will break the current equivalence between path and asset name formats. --- src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs | 26 +++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'src/SMAPI.Tests') diff --git a/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs b/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs index 5a342974..c18f47a5 100644 --- a/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs +++ b/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs @@ -1,3 +1,4 @@ +using System.IO; using NUnit.Framework; using StardewModdingAPI.Toolkit.Utilities; @@ -175,9 +176,30 @@ namespace SMAPI.Tests.Utilities } /**** - ** NormalizePathSeparators + ** NormalizeAssetName ****/ - [Test(Description = "Assert that PathUtilities.NormalizePathSeparators normalizes paths correctly.")] + [Test(Description = "Assert that PathUtilities.NormalizeAssetName normalizes paths correctly.")] + [TestCaseSource(nameof(PathUtilitiesTests.SamplePaths))] + public void NormalizeAssetName(SamplePath path) + { + if (Path.IsPathRooted(path.OriginalPath) || path.OriginalPath.StartsWith("/") || path.OriginalPath.StartsWith("\\")) + Assert.Ignore("Absolute paths can't be used as asset names."); + + // act + string normalized = PathUtilities.NormalizeAssetName(path.OriginalPath); + + // assert +#if SMAPI_FOR_WINDOWS + Assert.AreEqual(path.NormalizedOnWindows, normalized); +#else + Assert.AreEqual(path.NormalizedOnUnix, normalized); +#endif + } + + /**** + ** NormalizePath + ****/ + [Test(Description = "Assert that PathUtilities.NormalizePath normalizes paths correctly.")] [TestCaseSource(nameof(PathUtilitiesTests.SamplePaths))] public void NormalizePath(SamplePath path) { -- cgit From 0ce8939988159723851e4e64eeb82b026f982ef5 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 2 Sep 2021 19:27:49 -0400 Subject: avoid duplicate update key parsing logic, fix unit tests --- src/SMAPI.Tests/Core/ModResolverTests.cs | 5 ++++- src/SMAPI.Toolkit/ModToolkit.cs | 21 +++++++++------------ src/SMAPI/Framework/ModLoading/ModResolver.cs | 5 +++-- 3 files changed, 16 insertions(+), 15 deletions(-) (limited to 'src/SMAPI.Tests') diff --git a/src/SMAPI.Tests/Core/ModResolverTests.cs b/src/SMAPI.Tests/Core/ModResolverTests.cs index 28262111..da3446bb 100644 --- a/src/SMAPI.Tests/Core/ModResolverTests.cs +++ b/src/SMAPI.Tests/Core/ModResolverTests.cs @@ -10,6 +10,7 @@ using StardewModdingAPI.Framework; using StardewModdingAPI.Framework.ModLoading; using StardewModdingAPI.Toolkit; using StardewModdingAPI.Toolkit.Framework.ModData; +using StardewModdingAPI.Toolkit.Framework.UpdateData; using StardewModdingAPI.Toolkit.Serialization.Models; using SemanticVersion = StardewModdingAPI.SemanticVersion; @@ -489,7 +490,8 @@ namespace SMAPI.Tests.Core EntryDll = entryDll ?? $"{Sample.String()}.dll", ContentPackFor = contentPackForID != null ? new ManifestContentPackFor { UniqueID = contentPackForID } : null, MinimumApiVersion = minimumApiVersion != null ? new SemanticVersion(minimumApiVersion) : null, - Dependencies = dependencies + Dependencies = dependencies ?? new IManifestDependency[0], + UpdateKeys = new string[0] }; } @@ -541,6 +543,7 @@ namespace SMAPI.Tests.Core 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.GetUpdateKeys(It.IsAny())).Returns(Enumerable.Empty()); } } } diff --git a/src/SMAPI.Toolkit/ModToolkit.cs b/src/SMAPI.Toolkit/ModToolkit.cs index 695a2c52..38a67ae5 100644 --- a/src/SMAPI.Toolkit/ModToolkit.cs +++ b/src/SMAPI.Toolkit/ModToolkit.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -8,6 +7,7 @@ using StardewModdingAPI.Toolkit.Framework.Clients.Wiki; using StardewModdingAPI.Toolkit.Framework.GameScanning; using StardewModdingAPI.Toolkit.Framework.ModData; using StardewModdingAPI.Toolkit.Framework.ModScanning; +using StardewModdingAPI.Toolkit.Framework.UpdateData; using StardewModdingAPI.Toolkit.Serialization; namespace StardewModdingAPI.Toolkit @@ -22,11 +22,11 @@ namespace StardewModdingAPI.Toolkit private readonly string UserAgent; /// Maps vendor keys (like Nexus) to their mod URL template (where {0} is the mod ID). This doesn't affect update checks, which defer to the remote web API. - private readonly IDictionary VendorModUrls = new Dictionary(StringComparer.OrdinalIgnoreCase) + private readonly IDictionary VendorModUrls = new Dictionary() { - ["Chucklefish"] = "https://community.playstarbound.com/resources/{0}", - ["GitHub"] = "https://github.com/{0}/releases", - ["Nexus"] = "https://www.nexusmods.com/stardewvalley/mods/{0}" + [ModSiteKey.Chucklefish] = "https://community.playstarbound.com/resources/{0}", + [ModSiteKey.GitHub] = "https://github.com/{0}/releases", + [ModSiteKey.Nexus] = "https://www.nexusmods.com/stardewvalley/mods/{0}" }; @@ -89,15 +89,12 @@ namespace StardewModdingAPI.Toolkit /// The update key. public string GetUpdateUrl(string updateKey) { - string[] parts = updateKey.Split(new[] { ':' }, 2); - if (parts.Length != 2) + UpdateKey parsed = UpdateKey.Parse(updateKey); + if (!parsed.LooksValid) return null; - string vendorKey = parts[0].Trim(); - string modID = parts[1].Trim(); - - if (this.VendorModUrls.TryGetValue(vendorKey, out string urlTemplate)) - return string.Format(urlTemplate, modID); + if (this.VendorModUrls.TryGetValue(parsed.Site, out string urlTemplate)) + return string.Format(urlTemplate, parsed.ID); return null; } diff --git a/src/SMAPI/Framework/ModLoading/ModResolver.cs b/src/SMAPI/Framework/ModLoading/ModResolver.cs index 2f506571..4b05d1e5 100644 --- a/src/SMAPI/Framework/ModLoading/ModResolver.cs +++ b/src/SMAPI/Framework/ModLoading/ModResolver.cs @@ -5,6 +5,7 @@ using System.Linq; using StardewModdingAPI.Toolkit; using StardewModdingAPI.Toolkit.Framework.ModData; using StardewModdingAPI.Toolkit.Framework.ModScanning; +using StardewModdingAPI.Toolkit.Framework.UpdateData; using StardewModdingAPI.Toolkit.Serialization.Models; using StardewModdingAPI.Toolkit.Utilities; @@ -82,9 +83,9 @@ namespace StardewModdingAPI.Framework.ModLoading // get update URLs List updateUrls = new List(); - foreach (string key in mod.Manifest.UpdateKeys) + foreach (UpdateKey key in mod.GetUpdateKeys(validOnly: true)) { - string url = getUpdateUrl(key); + string url = getUpdateUrl(key.ToString()); if (url != null) updateUrls.Add(url); } -- cgit