using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; 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; using StardewModdingAPI.Web.Framework.Clients.Nexus; using StardewModdingAPI.Web.Framework.ConfigModels; using StardewModdingAPI.Web.Framework.ModRepositories; namespace StardewModdingAPI.Web.Controllers { /// Provides an API to perform mod update checks. [Produces("application/json")] [Route("api/v{version:semanticVersion}/mods")] internal class ModsApiController : Controller { /********* ** Properties *********/ /// The mod repositories which provide mod metadata. private readonly IDictionary Repositories; /// The cache in which to store mod metadata. private readonly IMemoryCache Cache; /// The number of minutes successful update checks should be cached before refetching them. private readonly int SuccessCacheMinutes; /// The number of minutes failed update checks should be cached before refetching them. private readonly int ErrorCacheMinutes; /// A regex which matches SMAPI-style semantic version. private readonly string VersionRegex; /// The internal mod metadata list. private readonly ModDatabase ModDatabase; /// The web URL for the wiki compatibility list. private readonly string WikiCompatibilityPageUrl; /********* ** Public methods *********/ /// Construct an instance. /// The web hosting environment. /// The cache in which to store mod metadata. /// The config settings for mod update checks. /// The Chucklefish API client. /// The GitHub API client. /// The Nexus API client. public ModsApiController(IHostingEnvironment environment, IMemoryCache cache, IOptions configProvider, IChucklefishClient chucklefish, IGitHubClient github, INexusClient nexus) { 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; this.ErrorCacheMinutes = config.ErrorCacheMinutes; this.VersionRegex = config.SemanticVersionRegex; this.Repositories = new IModRepository[] { new ChucklefishRepository(config.ChucklefishKey, chucklefish), new GitHubRepository(config.GitHubKey, github), new NexusRepository(config.NexusKey, nexus) } .ToDictionary(p => p.VendorKey, StringComparer.CurrentCultureIgnoreCase); } /// Fetch version metadata for the given mods. /// The mod search criteria. [HttpPost] public async Task PostAsync([FromBody] ModSearchModel model) { // parse request data ISemanticVersion apiVersion = this.GetApiVersion(); ModSearchEntryModel[] searchMods = this.GetSearchMods(model, apiVersion).ToArray(); // fetch wiki data WikiCompatibilityEntry[] wikiData = await this.GetWikiDataAsync(); // fetch data IDictionary mods = new Dictionary(StringComparer.CurrentCultureIgnoreCase); foreach (ModSearchEntryModel mod in searchMods) { if (string.IsNullOrWhiteSpace(mod.ID)) continue; ModEntryModel result = await this.GetModData(mod, wikiData, model.IncludeExtendedMetadata); result.SetBackwardsCompatibility(apiVersion); mods[mod.ID] = result; } // return in expected structure return apiVersion.IsNewerThan("2.6-beta.18") ? mods.Values : (object)mods; } /********* ** Private methods *********/ /// Get the metadata for a mod. /// The mod data to match. /// The wiki data. /// Whether to include extended metadata for each mod. /// Returns the mod data if found, else null. private async Task GetModData(ModSearchEntryModel search, WikiCompatibilityEntry[] wikiData, bool includeExtendedMetadata) { // resolve update keys var updateKeys = new HashSet(search.UpdateKeys ?? new string[0], StringComparer.InvariantCultureIgnoreCase); ModDataRecord record = this.ModDatabase.Get(search.ID); if (record?.Fields != null) { string defaultUpdateKey = record.Fields.FirstOrDefault(p => p.Key == ModDataFieldKey.UpdateKey && p.IsDefault)?.Value; if (!string.IsNullOrWhiteSpace(defaultUpdateKey)) updateKeys.Add(defaultUpdateKey); } // get latest versions ModEntryModel result = new ModEntryModel { ID = search.ID }; IList errors = new List(); foreach (string updateKey in updateKeys) { // fetch data ModInfoModel data = await this.GetInfoForUpdateKeyAsync(updateKey); if (data.Error != null) { errors.Add(data.Error); continue; } // handle main version if (data.Version != null) { if (!SemanticVersion.TryParse(data.Version, out ISemanticVersion version)) { errors.Add($"The update key '{updateKey}' matches a mod with invalid semantic version '{data.Version}'."); continue; } if (this.IsNewer(version, result.Main?.Version)) result.Main = new ModEntryVersionModel(version, data.Url); } // handle optional version if (data.PreviewVersion != null) { if (!SemanticVersion.TryParse(data.PreviewVersion, out ISemanticVersion version)) { errors.Add($"The update key '{updateKey}' matches a mod with invalid optional semantic version '{data.PreviewVersion}'."); continue; } if (this.IsNewer(version, result.Optional?.Version)) result.Optional = new ModEntryVersionModel(version, data.Url); } } // 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) { result.Main = result.Optional; result.Optional = null; } // special cases if (result.ID == "Pathoschild.SMAPI") { if (result.Main != null) result.Main.Url = "https://smapi.io/"; if (result.Optional != null) result.Optional.Url = "https://smapi.io/"; } // add extended metadata if (includeExtendedMetadata && (wikiEntry != null || record != null)) result.Metadata = new ModExtendedMetadataModel(wikiEntry, record); // add result result.Errors = errors.ToArray(); return result; } /// Parse a namespaced mod ID. /// The raw mod ID to parse. /// The parsed vendor key. /// The parsed mod ID. /// Returns whether the value could be parsed. private bool TryParseModKey(string raw, out string vendorKey, out string modID) { // split parts string[] parts = raw?.Split(':'); if (parts == null || parts.Length != 2) { vendorKey = null; modID = null; return false; } // parse vendorKey = parts[0].Trim(); modID = parts[1].Trim(); 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. private IEnumerable GetSearchMods(ModSearchModel model, ISemanticVersion apiVersion) { if (model == null) yield break; // yield standard entries if (model.Mods != null) { foreach (ModSearchEntryModel mod in model.Mods) yield return mod; } // yield mod update keys if backwards compatible if (model.ModKeys != null && model.ModKeys.Any() && !apiVersion.IsNewerThan("2.6-beta.17")) { foreach (string updateKey in model.ModKeys.Distinct()) yield return new ModSearchEntryModel(updateKey, new[] { updateKey }); } } /// 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) { // parse update key if (!this.TryParseModKey(updateKey, out string vendorKey, out string modID)) return new ModInfoModel($"The update key '{updateKey}' isn't in a valid format. It should contain the site key and mod ID like 'Nexus:541'."); // get matching repository if (!this.Repositories.TryGetValue(vendorKey, out IModRepository repository)) return new ModInfoModel($"There's no mod site with key '{vendorKey}'. Expected one of [{string.Join(", ", this.Repositories.Keys)}]."); // fetch mod info return await this.Cache.GetOrCreateAsync($"{repository.VendorKey}:{modID}".ToLower(), async entry => { ModInfoModel result = await repository.GetModInfoAsync(modID); if (result.Error != null) { if (result.Version == null) result.Error = $"The update key '{updateKey}' matches a mod with no version number."; else if (!Regex.IsMatch(result.Version, this.VersionRegex, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)) result.Error = $"The update key '{updateKey}' matches a mod with invalid semantic version '{result.Version}'."; } entry.AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(result.Error == null ? this.SuccessCacheMinutes : this.ErrorCacheMinutes); return result; }); } /// Get the requested API version. private ISemanticVersion GetApiVersion() { string actualVersion = (string)this.RouteData.Values["version"]; return new SemanticVersion(actualVersion); } } }