diff --git a/src/SMAPI/Program.cs b/src/SMAPI/Program.cs index e55a96b2..05a3134e 100644 --- a/src/SMAPI/Program.cs +++ b/src/SMAPI/Program.cs @@ -577,7 +577,11 @@ namespace StardewModdingAPI new Thread(() => { // create client - WebApiClient client = new WebApiClient(this.Settings.WebApiBaseUrl, Constants.ApiVersionForToolkit); + string url = this.Settings.WebApiBaseUrl; +#if !SMAPI_FOR_WINDOWS + url = url.Replace("https://", "http://"); // workaround for OpenSSL issues with the game's bundled Mono on Linux/Mac +#endif + WebApiClient client = new WebApiClient(url, Constants.ApiVersionForToolkit); this.Monitor.Log("Checking for updates...", LogLevel.Trace); // check SMAPI version diff --git a/src/StardewModdingAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs b/src/StardewModdingAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs index 277fbeeb..d94b0259 100644 --- a/src/StardewModdingAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs +++ b/src/StardewModdingAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Net; using Newtonsoft.Json; namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi { /// Provides methods for interacting with the SMAPI web API. - internal class WebApiClient + public class WebApiClient { /********* ** Properties @@ -26,9 +27,6 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi /// The web API version. public WebApiClient(string baseUrl, ISemanticVersion version) { -#if !SMAPI_FOR_WINDOWS - baseUrl = baseUrl.Replace("https://", "http://"); // workaround for OpenSSL issues with the game's bundled Mono on Linux/Mac -#endif this.BaseUrl = new Uri(baseUrl); this.Version = version; } @@ -43,6 +41,34 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi ); } + /// Get the latest version for a mod. + /// The update keys to search. + public ISemanticVersion GetLatestVersion(string[] updateKeys) + { + if (!updateKeys.Any()) + return null; + + // fetch update results + ModInfoModel[] results = this + .GetModInfo(updateKeys) + .Values + .Where(p => p.Error == null) + .ToArray(); + if (!results.Any()) + return null; + + ISemanticVersion latest = null; + foreach (ModInfoModel result in results) + { + if (!SemanticVersion.TryParse(result.PreviewVersion ?? result.Version, out ISemanticVersion cur)) + continue; + + if (latest == null || cur.IsNewerThan(latest)) + latest = cur; + } + return latest; + } + /********* ** Private methods