From c9fedebaf3231a2d5a00a95ff1ffd3ac5dac4ae2 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 28 Jun 2018 22:30:34 -0400 Subject: add support for unofficial version in update checks (#532) --- src/SMAPI.Web/Controllers/ModsApiController.cs | 47 ++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) (limited to 'src/SMAPI.Web/Controllers') diff --git a/src/SMAPI.Web/Controllers/ModsApiController.cs b/src/SMAPI.Web/Controllers/ModsApiController.cs index c4f1023b..b9af17dc 100644 --- a/src/SMAPI.Web/Controllers/ModsApiController.cs +++ b/src/SMAPI.Web/Controllers/ModsApiController.cs @@ -10,6 +10,7 @@ using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; using StardewModdingAPI.Toolkit; using StardewModdingAPI.Toolkit.Framework.Clients.WebApi; +using StardewModdingAPI.Toolkit.Framework.Clients.Wiki; using StardewModdingAPI.Toolkit.Framework.ModData; using StardewModdingAPI.Web.Framework.Clients.Chucklefish; using StardewModdingAPI.Web.Framework.Clients.GitHub; @@ -45,6 +46,9 @@ namespace StardewModdingAPI.Web.Controllers /// The internal mod metadata list. private readonly ModDatabase ModDatabase; + /// The web URL for the wiki compatibility list. + private readonly string WikiCompatibilityPageUrl; + /********* ** Public methods @@ -60,6 +64,7 @@ namespace StardewModdingAPI.Web.Controllers { this.ModDatabase = new ModToolkit().GetModDatabase(Path.Combine(environment.WebRootPath, "StardewModdingAPI.metadata.json")); ModUpdateCheckConfig config = configProvider.Value; + this.WikiCompatibilityPageUrl = config.WikiCompatibilityPageUrl; this.Cache = cache; this.SuccessCacheMinutes = config.SuccessCacheMinutes; @@ -84,6 +89,9 @@ namespace StardewModdingAPI.Web.Controllers ISemanticVersion apiVersion = this.GetApiVersion(); ModSearchEntryModel[] searchMods = this.GetSearchMods(model, apiVersion).ToArray(); + // fetch wiki data + WikiCompatibilityEntry[] wikiData = await this.GetWikiDataAsync(); + // perform checks IDictionary mods = new Dictionary(StringComparer.CurrentCultureIgnoreCase); foreach (ModSearchEntryModel mod in searchMods) @@ -123,7 +131,7 @@ namespace StardewModdingAPI.Web.Controllers continue; } - if (result.Main == null || result.Main.Version.IsOlderThan(version)) + if (this.IsNewer(version, result.Main?.Version)) { result.Name = data.Name; result.Main = new ModEntryVersionModel(version, data.Url); @@ -139,7 +147,7 @@ namespace StardewModdingAPI.Web.Controllers continue; } - if (result.Optional == null || result.Optional.Version.IsOlderThan(version)) + if (this.IsNewer(version, result.Optional?.Version)) { result.Name = result.Name ?? data.Name; result.Optional = new ModEntryVersionModel(version, data.Url); @@ -147,6 +155,13 @@ namespace StardewModdingAPI.Web.Controllers } } + // get unofficial version + { + WikiCompatibilityEntry wikiEntry = wikiData.FirstOrDefault(entry => entry.ID.Contains(result.ID.Trim(), StringComparer.InvariantCultureIgnoreCase)); + if (wikiEntry?.UnofficialVersion != null && this.IsNewer(wikiEntry.UnofficialVersion, result.Main?.Version) && this.IsNewer(wikiEntry.UnofficialVersion, result.Optional?.Version)) + result.Unofficial = new ModEntryVersionModel(wikiEntry.UnofficialVersion, this.WikiCompatibilityPageUrl); + } + // fallback to preview if latest is invalid if (result.Main == null && result.Optional != null) { @@ -199,6 +214,14 @@ namespace StardewModdingAPI.Web.Controllers return true; } + /// Get whether a version is newer than an version. + /// The current version. + /// The other version. + private bool IsNewer(ISemanticVersion current, ISemanticVersion other) + { + return current != null && (other == null || other.IsOlderThan(current)); + } + /// Get the mods for which the API should return data. /// The search model. /// The requested API version. @@ -222,6 +245,26 @@ namespace StardewModdingAPI.Web.Controllers } } + /// Get mod data from the wiki compatibility list. + private async Task GetWikiDataAsync() + { + ModToolkit toolkit = new ModToolkit(); + return await this.Cache.GetOrCreateAsync($"_wiki", async entry => + { + try + { + WikiCompatibilityEntry[] entries = await toolkit.GetWikiCompatibilityListAsync(); + entry.AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(this.SuccessCacheMinutes); + return entries; + } + catch + { + entry.AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(this.ErrorCacheMinutes); + return new WikiCompatibilityEntry[0]; + } + }); + } + /// Get the mod info for an update key. /// The namespaced update key. private async Task GetInfoForUpdateKeyAsync(string updateKey) -- cgit