using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using StardewModdingAPI.Toolkit; using StardewModdingAPI.Web.Framework.Caching.Wiki; using StardewModdingAPI.Web.Framework.ConfigModels; using StardewModdingAPI.Web.ViewModels; namespace StardewModdingAPI.Web.Controllers { /// Provides user-friendly info about SMAPI mods. internal class ModsController : Controller { /********* ** Fields *********/ /// The cache in which to store mod metadata. private readonly IWikiCacheRepository Cache; /// The number of minutes successful update checks should be cached before refetching them. private readonly int CacheMinutes; /********* ** Public methods *********/ /// Construct an instance. /// The cache in which to store mod metadata. /// The config settings for mod update checks. public ModsController(IWikiCacheRepository cache, IOptions configProvider) { ModCompatibilityListConfig config = configProvider.Value; this.Cache = cache; this.CacheMinutes = config.CacheMinutes; } /// Display information for all mods. [HttpGet] [Route("mods")] public async Task Index() { return this.View("Index", await this.FetchDataAsync()); } /********* ** Private methods *********/ /// Asynchronously fetch mod metadata from the wiki. public async Task FetchDataAsync() { // refresh cache CachedWikiMod[] mods; if (!this.Cache.TryGetWikiMetadata(out CachedWikiMetadata metadata) || this.Cache.IsStale(metadata.LastUpdated, this.CacheMinutes)) { var wikiCompatList = await new ModToolkit().GetWikiCompatibilityListAsync(); this.Cache.SaveWikiData(wikiCompatList.StableVersion, wikiCompatList.BetaVersion, wikiCompatList.Mods, out metadata, out mods); } else mods = this.Cache.GetWikiMods().ToArray(); // build model return new ModListModel( stableVersion: metadata.StableVersion, betaVersion: metadata.BetaVersion, mods: mods .Select(mod => new ModModel(mod.GetModel())) .OrderBy(p => Regex.Replace(p.Name.ToLower(), "[^a-z0-9]", "")) // ignore case, spaces, and special characters when sorting ); } } }