avoid duplicate update key parsing logic, fix unit tests

This commit is contained in:
Jesse Plamondon-Willard 2021-09-02 19:27:49 -04:00
parent 8789b7efa8
commit 0ce8939988
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
3 changed files with 16 additions and 15 deletions

View File

@ -10,6 +10,7 @@ using StardewModdingAPI.Framework;
using StardewModdingAPI.Framework.ModLoading; using StardewModdingAPI.Framework.ModLoading;
using StardewModdingAPI.Toolkit; using StardewModdingAPI.Toolkit;
using StardewModdingAPI.Toolkit.Framework.ModData; using StardewModdingAPI.Toolkit.Framework.ModData;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
using StardewModdingAPI.Toolkit.Serialization.Models; using StardewModdingAPI.Toolkit.Serialization.Models;
using SemanticVersion = StardewModdingAPI.SemanticVersion; using SemanticVersion = StardewModdingAPI.SemanticVersion;
@ -489,7 +490,8 @@ namespace SMAPI.Tests.Core
EntryDll = entryDll ?? $"{Sample.String()}.dll", EntryDll = entryDll ?? $"{Sample.String()}.dll",
ContentPackFor = contentPackForID != null ? new ManifestContentPackFor { UniqueID = contentPackForID } : null, ContentPackFor = contentPackForID != null ? new ManifestContentPackFor { UniqueID = contentPackForID } : null,
MinimumApiVersion = minimumApiVersion != null ? new SemanticVersion(minimumApiVersion) : 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.Manifest).Returns(this.GetManifest());
mod.Setup(p => p.DirectoryPath).Returns(Path.GetTempPath()); mod.Setup(p => p.DirectoryPath).Returns(Path.GetTempPath());
mod.Setup(p => p.DataRecord).Returns(modRecord); mod.Setup(p => p.DataRecord).Returns(modRecord);
mod.Setup(p => p.GetUpdateKeys(It.IsAny<bool>())).Returns(Enumerable.Empty<UpdateKey>());
} }
} }
} }

View File

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -8,6 +7,7 @@ using StardewModdingAPI.Toolkit.Framework.Clients.Wiki;
using StardewModdingAPI.Toolkit.Framework.GameScanning; using StardewModdingAPI.Toolkit.Framework.GameScanning;
using StardewModdingAPI.Toolkit.Framework.ModData; using StardewModdingAPI.Toolkit.Framework.ModData;
using StardewModdingAPI.Toolkit.Framework.ModScanning; using StardewModdingAPI.Toolkit.Framework.ModScanning;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
using StardewModdingAPI.Toolkit.Serialization; using StardewModdingAPI.Toolkit.Serialization;
namespace StardewModdingAPI.Toolkit namespace StardewModdingAPI.Toolkit
@ -22,11 +22,11 @@ namespace StardewModdingAPI.Toolkit
private readonly string UserAgent; private readonly string UserAgent;
/// <summary>Maps vendor keys (like <c>Nexus</c>) to their mod URL template (where <c>{0}</c> is the mod ID). This doesn't affect update checks, which defer to the remote web API.</summary> /// <summary>Maps vendor keys (like <c>Nexus</c>) to their mod URL template (where <c>{0}</c> is the mod ID). This doesn't affect update checks, which defer to the remote web API.</summary>
private readonly IDictionary<string, string> VendorModUrls = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) private readonly IDictionary<ModSiteKey, string> VendorModUrls = new Dictionary<ModSiteKey, string>()
{ {
["Chucklefish"] = "https://community.playstarbound.com/resources/{0}", [ModSiteKey.Chucklefish] = "https://community.playstarbound.com/resources/{0}",
["GitHub"] = "https://github.com/{0}/releases", [ModSiteKey.GitHub] = "https://github.com/{0}/releases",
["Nexus"] = "https://www.nexusmods.com/stardewvalley/mods/{0}" [ModSiteKey.Nexus] = "https://www.nexusmods.com/stardewvalley/mods/{0}"
}; };
@ -89,15 +89,12 @@ namespace StardewModdingAPI.Toolkit
/// <param name="updateKey">The update key.</param> /// <param name="updateKey">The update key.</param>
public string GetUpdateUrl(string updateKey) public string GetUpdateUrl(string updateKey)
{ {
string[] parts = updateKey.Split(new[] { ':' }, 2); UpdateKey parsed = UpdateKey.Parse(updateKey);
if (parts.Length != 2) if (!parsed.LooksValid)
return null; return null;
string vendorKey = parts[0].Trim(); if (this.VendorModUrls.TryGetValue(parsed.Site, out string urlTemplate))
string modID = parts[1].Trim(); return string.Format(urlTemplate, parsed.ID);
if (this.VendorModUrls.TryGetValue(vendorKey, out string urlTemplate))
return string.Format(urlTemplate, modID);
return null; return null;
} }

View File

@ -5,6 +5,7 @@ using System.Linq;
using StardewModdingAPI.Toolkit; using StardewModdingAPI.Toolkit;
using StardewModdingAPI.Toolkit.Framework.ModData; using StardewModdingAPI.Toolkit.Framework.ModData;
using StardewModdingAPI.Toolkit.Framework.ModScanning; using StardewModdingAPI.Toolkit.Framework.ModScanning;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
using StardewModdingAPI.Toolkit.Serialization.Models; using StardewModdingAPI.Toolkit.Serialization.Models;
using StardewModdingAPI.Toolkit.Utilities; using StardewModdingAPI.Toolkit.Utilities;
@ -82,9 +83,9 @@ namespace StardewModdingAPI.Framework.ModLoading
// get update URLs // get update URLs
List<string> updateUrls = new List<string>(); List<string> updateUrls = new List<string>();
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) if (url != null)
updateUrls.Add(url); updateUrls.Add(url);
} }