diff options
Diffstat (limited to 'src/SMAPI.Web')
16 files changed, 360 insertions, 23 deletions
diff --git a/src/SMAPI.Web/Controllers/ModsApiController.cs b/src/SMAPI.Web/Controllers/ModsApiController.cs index f0835592..12d349e0 100644 --- a/src/SMAPI.Web/Controllers/ModsApiController.cs +++ b/src/SMAPI.Web/Controllers/ModsApiController.cs @@ -15,6 +15,7 @@ using StardewModdingAPI.Toolkit.Framework.ModData; using StardewModdingAPI.Toolkit.Framework.UpdateData; using StardewModdingAPI.Web.Framework.Clients.Chucklefish; using StardewModdingAPI.Web.Framework.Clients.GitHub; +using StardewModdingAPI.Web.Framework.Clients.ModDrop; using StardewModdingAPI.Web.Framework.Clients.Nexus; using StardewModdingAPI.Web.Framework.ConfigModels; using StardewModdingAPI.Web.Framework.ModRepositories; @@ -60,8 +61,9 @@ namespace StardewModdingAPI.Web.Controllers /// <param name="configProvider">The config settings for mod update checks.</param> /// <param name="chucklefish">The Chucklefish API client.</param> /// <param name="github">The GitHub API client.</param> + /// <param name="modDrop">The ModDrop API client.</param> /// <param name="nexus">The Nexus API client.</param> - public ModsApiController(IHostingEnvironment environment, IMemoryCache cache, IOptions<ModUpdateCheckConfig> configProvider, IChucklefishClient chucklefish, IGitHubClient github, INexusClient nexus) + public ModsApiController(IHostingEnvironment environment, IMemoryCache cache, IOptions<ModUpdateCheckConfig> configProvider, IChucklefishClient chucklefish, IGitHubClient github, IModDropClient modDrop, INexusClient nexus) { this.ModDatabase = new ModToolkit().GetModDatabase(Path.Combine(environment.WebRootPath, "StardewModdingAPI.metadata.json")); ModUpdateCheckConfig config = configProvider.Value; @@ -76,6 +78,7 @@ namespace StardewModdingAPI.Web.Controllers { new ChucklefishRepository(chucklefish), new GitHubRepository(github), + new ModDropRepository(modDrop), new NexusRepository(nexus) } .ToDictionary(p => p.VendorKey); @@ -291,6 +294,8 @@ namespace StardewModdingAPI.Web.Controllers yield return $"Nexus:{entry.NexusID}"; if (entry.ChucklefishID.HasValue) yield return $"Chucklefish:{entry.ChucklefishID}"; + if (entry.ModDropID.HasValue) + yield return $"ModDrop:{entry.ModDropID}"; } } diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/IModDropClient.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/IModDropClient.cs new file mode 100644 index 00000000..3ede46e2 --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/IModDropClient.cs @@ -0,0 +1,17 @@ +using System; +using System.Threading.Tasks; + +namespace StardewModdingAPI.Web.Framework.Clients.ModDrop +{ + /// <summary>An HTTP client for fetching mod metadata from the ModDrop API.</summary> + internal interface IModDropClient : IDisposable + { + /********* + ** Methods + *********/ + /// <summary>Get metadata about a mod.</summary> + /// <param name="id">The ModDrop mod ID.</param> + /// <returns>Returns the mod info if found, else <c>null</c>.</returns> + Task<ModDropMod> GetModAsync(long id); + } +} diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs new file mode 100644 index 00000000..19b0b24d --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropClient.cs @@ -0,0 +1,96 @@ +using System.Threading.Tasks; +using Pathoschild.Http.Client; +using StardewModdingAPI.Toolkit; +using StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels; + +namespace StardewModdingAPI.Web.Framework.Clients.ModDrop +{ + /// <summary>An HTTP client for fetching mod metadata from the ModDrop API.</summary> + internal class ModDropClient : IModDropClient + { + /********* + ** Properties + *********/ + /// <summary>The underlying HTTP client.</summary> + private readonly IClient Client; + + /// <summary>The URL for a ModDrop mod page for the user, where {0} is the mod ID.</summary> + private readonly string ModUrlFormat; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="userAgent">The user agent for the API client.</param> + /// <param name="apiUrl">The base URL for the ModDrop API.</param> + /// <param name="modUrlFormat">The URL for a ModDrop mod page for the user, where {0} is the mod ID.</param> + public ModDropClient(string userAgent, string apiUrl, string modUrlFormat) + { + this.Client = new FluentClient(apiUrl).SetUserAgent(userAgent); + this.ModUrlFormat = modUrlFormat; + } + + /// <summary>Get metadata about a mod.</summary> + /// <param name="id">The ModDrop mod ID.</param> + /// <returns>Returns the mod info if found, else <c>null</c>.</returns> + public async Task<ModDropMod> GetModAsync(long id) + { + // get raw data + ModListModel response = await this.Client + .PostAsync("") + .WithBody(new + { + ModIDs = new[] { id }, + Files = true, + Mods = true + }) + .As<ModListModel>(); + ModModel mod = response.Mods[id]; + if (mod.Mod?.Title == null || mod.Mod.ErrorCode.HasValue) + return null; + + // get latest versions + ISemanticVersion latest = null; + ISemanticVersion optional = null; + foreach (FileDataModel file in mod.Files) + { + if (file.IsOld || file.IsDeleted || file.IsHidden) + continue; + + if (!SemanticVersion.TryParse(file.Version, out ISemanticVersion version)) + continue; + + if (file.IsDefault) + { + if (latest == null || version.IsNewerThan(latest)) + latest = version; + } + else if (optional == null || version.IsNewerThan(optional)) + optional = version; + } + if (latest == null) + { + latest = optional; + optional = null; + } + if (optional != null && latest.IsNewerThan(optional)) + optional = null; + + // generate result + return new ModDropMod + { + Name = mod.Mod?.Title, + LatestDefaultVersion = latest, + LatestOptionalVersion = optional, + Url = string.Format(this.ModUrlFormat, id) + }; + } + + /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary> + public void Dispose() + { + this.Client?.Dispose(); + } + } +} diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropMod.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropMod.cs new file mode 100644 index 00000000..291fb353 --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ModDropMod.cs @@ -0,0 +1,24 @@ +namespace StardewModdingAPI.Web.Framework.Clients.ModDrop +{ + /// <summary>Mod metadata from the ModDrop API.</summary> + internal class ModDropMod + { + /********* + ** Accessors + *********/ + /// <summary>The mod name.</summary> + public string Name { get; set; } + + /// <summary>The latest default file version.</summary> + public ISemanticVersion LatestDefaultVersion { get; set; } + + /// <summary>The latest optional file version.</summary> + public ISemanticVersion LatestOptionalVersion { get; set; } + + /// <summary>The mod's web URL.</summary> + public string Url { get; set; } + + /// <summary>A user-friendly error which indicates why fetching the mod info failed (if applicable).</summary> + public string Error { get; set; } + } +} diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs new file mode 100644 index 00000000..fa84b287 --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/FileDataModel.cs @@ -0,0 +1,21 @@ +namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels +{ + /// <summary>Metadata from the ModDrop API about a mod file.</summary> + public class FileDataModel + { + /// <summary>Whether the file is deleted.</summary> + public bool IsDeleted { get; set; } + + /// <summary>Whether the file is hidden from users.</summary> + public bool IsHidden { get; set; } + + /// <summary>Whether this is the default file for the mod.</summary> + public bool IsDefault { get; set; } + + /// <summary>Whether this is an archived file.</summary> + public bool IsOld { get; set; } + + /// <summary>The file version.</summary> + public string Version { get; set; } + } +} diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs new file mode 100644 index 00000000..cfdd6a4e --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModDataModel.cs @@ -0,0 +1,15 @@ +namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels +{ + /// <summary>Metadata about a mod from the ModDrop API.</summary> + public class ModDataModel + { + /// <summary>The mod's unique ID on ModDrop.</summary> + public int ID { get; set; } + + /// <summary>The error code, if any.</summary> + public int? ErrorCode { get; set; } + + /// <summary>The mod name.</summary> + public string Title { get; set; } + } +} diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs new file mode 100644 index 00000000..7f692ca1 --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModListModel.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels +{ + /// <summary>A list of mods from the ModDrop API.</summary> + public class ModListModel + { + /// <summary>The mod data.</summary> + public IDictionary<long, ModModel> Mods { get; set; } + } +} diff --git a/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs new file mode 100644 index 00000000..9f4b2c6f --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/ModDrop/ResponseModels/ModModel.cs @@ -0,0 +1,12 @@ +namespace StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels +{ + /// <summary>An entry in a mod list from the ModDrop API.</summary> + public class ModModel + { + /// <summary>The available file downloads.</summary> + public FileDataModel[] Files { get; set; } + + /// <summary>The mod metadata.</summary> + public ModDataModel Mod { get; set; } + } +} diff --git a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs index ae8f18d2..c27cadab 100644 --- a/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs +++ b/src/SMAPI.Web/Framework/ConfigModels/ApiClientsConfig.cs @@ -45,6 +45,15 @@ namespace StardewModdingAPI.Web.Framework.ConfigModels public string GitHubPassword { get; set; } /**** + ** ModDrop + ****/ + /// <summary>The base URL for the ModDrop API.</summary> + public string ModDropApiUrl { get; set; } + + /// <summary>The URL for a ModDrop mod page for the user, where {0} is the mod ID.</summary> + public string ModDropModPageUrl { get; set; } + + /**** ** Nexus Mods ****/ /// <summary>The base URL for the Nexus Mods API.</summary> diff --git a/src/SMAPI.Web/Framework/ModRepositories/ModDropRepository.cs b/src/SMAPI.Web/Framework/ModRepositories/ModDropRepository.cs new file mode 100644 index 00000000..09484aa8 --- /dev/null +++ b/src/SMAPI.Web/Framework/ModRepositories/ModDropRepository.cs @@ -0,0 +1,59 @@ +using System; +using System.Threading.Tasks; +using StardewModdingAPI.Toolkit.Framework.UpdateData; +using StardewModdingAPI.Web.Framework.Clients.ModDrop; + +namespace StardewModdingAPI.Web.Framework.ModRepositories +{ + /// <summary>An HTTP client for fetching mod metadata from the ModDrop API.</summary> + internal class ModDropRepository : RepositoryBase + { + /********* + ** Properties + *********/ + /// <summary>The underlying ModDrop API client.</summary> + private readonly IModDropClient Client; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="client">The underlying Nexus Mods API client.</param> + public ModDropRepository(IModDropClient client) + : base(ModRepositoryKey.ModDrop) + { + this.Client = client; + } + + /// <summary>Get metadata about a mod in the repository.</summary> + /// <param name="id">The mod ID in this repository.</param> + public override async Task<ModInfoModel> GetModInfoAsync(string id) + { + // validate ID format + if (!long.TryParse(id, out long modDropID)) + return new ModInfoModel($"The value '{id}' isn't a valid ModDrop mod ID, must be an integer ID."); + + // fetch info + try + { + ModDropMod mod = await this.Client.GetModAsync(modDropID); + if (mod == null) + return new ModInfoModel("Found no mod with this ID."); + if (mod.Error != null) + return new ModInfoModel(mod.Error); + return new ModInfoModel(name: mod.Name, version: mod.LatestDefaultVersion?.ToString(), previewVersion: mod.LatestOptionalVersion?.ToString(), url: mod.Url); + } + catch (Exception ex) + { + return new ModInfoModel(ex.ToString()); + } + } + + /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary> + public override void Dispose() + { + this.Client.Dispose(); + } + } +} diff --git a/src/SMAPI.Web/Startup.cs b/src/SMAPI.Web/Startup.cs index 60a16053..0bd71d26 100644 --- a/src/SMAPI.Web/Startup.cs +++ b/src/SMAPI.Web/Startup.cs @@ -11,6 +11,7 @@ using StardewModdingAPI.Toolkit.Serialisation; using StardewModdingAPI.Web.Framework; using StardewModdingAPI.Web.Framework.Clients.Chucklefish; using StardewModdingAPI.Web.Framework.Clients.GitHub; +using StardewModdingAPI.Web.Framework.Clients.ModDrop; using StardewModdingAPI.Web.Framework.Clients.Nexus; using StardewModdingAPI.Web.Framework.Clients.Pastebin; using StardewModdingAPI.Web.Framework.ConfigModels; @@ -86,6 +87,12 @@ namespace StardewModdingAPI.Web password: api.GitHubPassword )); + services.AddSingleton<IModDropClient>(new ModDropClient( + userAgent: userAgent, + apiUrl: api.ModDropApiUrl, + modUrlFormat: api.ModDropModPageUrl + )); + services.AddSingleton<INexusClient>(new NexusWebScrapeClient( userAgent: userAgent, baseUrl: api.NexusBaseUrl, @@ -155,6 +162,7 @@ namespace StardewModdingAPI.Web // shortcut redirects redirects.Add(new RedirectToUrlRule(@"^/buildmsg(?:/?(.*))$", "https://github.com/Pathoschild/SMAPI/blob/develop/docs/mod-build-config.md#$1")); redirects.Add(new RedirectToUrlRule(@"^/compat\.?$", "https://mods.smapi.io")); + redirects.Add(new RedirectToUrlRule(@"^/3\.0\.?$", "https://stardewvalleywiki.com/Modding:Migrate_to_SMAPI_3.0")); redirects.Add(new RedirectToUrlRule(@"^/docs\.?$", "https://stardewvalleywiki.com/Modding:Index")); redirects.Add(new RedirectToUrlRule(@"^/install\.?$", "https://stardewvalleywiki.com/Modding:Player_Guide/Getting_Started#Install_SMAPI")); diff --git a/src/SMAPI.Web/ViewModels/ModModel.cs b/src/SMAPI.Web/ViewModels/ModModel.cs index 0e7d2076..5c1840fc 100644 --- a/src/SMAPI.Web/ViewModels/ModModel.cs +++ b/src/SMAPI.Web/ViewModels/ModModel.cs @@ -96,6 +96,11 @@ namespace StardewModdingAPI.Web.ViewModels anyFound = true; yield return new ModLinkModel($"https://community.playstarbound.com/resources/{entry.ChucklefishID}", "Chucklefish"); } + if (entry.ModDropID.HasValue) + { + anyFound = true; + yield return new ModLinkModel($"https://www.moddrop.com/sdv/mod/467243/{entry.ModDropID}", "ModDrop"); + } // fallback if (!anyFound && !string.IsNullOrWhiteSpace(entry.CustomUrl)) diff --git a/src/SMAPI.Web/Views/Index/Index.cshtml b/src/SMAPI.Web/Views/Index/Index.cshtml index 01874f50..ef092cc8 100644 --- a/src/SMAPI.Web/Views/Index/Index.cshtml +++ b/src/SMAPI.Web/Views/Index/Index.cshtml @@ -108,6 +108,7 @@ else <a href="https://www.nexusmods.com/users/12252523">Karmylla</a>, Pucklynn, Robby LaFarge, + Tarryn K., and a few anonymous users for their ongoing support; you're awesome! 🏅 </p> diff --git a/src/SMAPI.Web/Views/Mods/Index.cshtml b/src/SMAPI.Web/Views/Mods/Index.cshtml index 372d6706..a49a24d9 100644 --- a/src/SMAPI.Web/Views/Mods/Index.cshtml +++ b/src/SMAPI.Web/Views/Mods/Index.cshtml @@ -4,11 +4,11 @@ ViewData["Title"] = "SMAPI mod compatibility"; } @section Head { - <link rel="stylesheet" href="~/Content/css/mods.css?r=20181109" /> + <link rel="stylesheet" href="~/Content/css/mods.css?r=20181122" /> <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.min.js" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/tablesorter@2.31.0/dist/js/jquery.tablesorter.combined.min.js" crossorigin="anonymous"></script> - <script src="~/Content/js/mods.js?r=20181109"></script> + <script src="~/Content/js/mods.js?r=20181122"></script> <script> $(function() { var data = @Json.Serialize(Model.Mods, new JsonSerializerSettings { Formatting = Formatting.None }); @@ -36,7 +36,7 @@ </div> <div id="filter-area"> <input type="checkbox" id="show-advanced" v-model="showAdvanced" /> - <label for="show-advanced">show detailed options</label> + <label for="show-advanced">show advanced info and options</label> <div id="filters" v-show="showAdvanced"> <div v-for="(filterGroup, key) in filters"> {{key}}: <span v-for="filter in filterGroup" v-bind:class="{ active: filter.value }"><input type="checkbox" v-bind:id="filter.id" v-model="filter.value" v-on:change="applyFilters" /> <label v-bind:for="filter.id">{{filter.label}}</label></span> @@ -44,7 +44,10 @@ </div> </div> </div> - <div id="mod-count" v-show="showAdvanced">{{visibleCount}} mods shown.</div> + <div id="mod-count" v-show="showAdvanced"> + <span v-if="visibleStats.total > 0">{{visibleStats.total}} mods shown ({{Math.round((visibleStats.compatible + visibleStats.workaround) / visibleStats.total * 100)}}% compatible or have a workaround, {{Math.round((visibleStats.soon + visibleStats.broken) / visibleStats.total * 100)}}% broken, {{Math.round(visibleStats.abandoned / visibleStats.total * 100)}}% obsolete).</span> + <span v-else>No matching mods found.</span> + </div> <table class="wikitable" id="mod-list"> <thead> <tr> diff --git a/src/SMAPI.Web/appsettings.json b/src/SMAPI.Web/appsettings.json index aba8c448..89505a45 100644 --- a/src/SMAPI.Web/appsettings.json +++ b/src/SMAPI.Web/appsettings.json @@ -35,6 +35,9 @@ "GitHubUsername": null, // see top note "GitHubPassword": null, // see top note + "ModDropApiUrl": "https://www.moddrop.com/api/mods/data", + "ModDropModPageUrl": "https://www.moddrop.com/sdv/mod/{0}", + "NexusBaseUrl": "https://www.nexusmods.com/stardewvalley/", "NexusModUrlFormat": "mods/{0}", "NexusModScrapeUrlFormat": "mods/{0}?tab=files", diff --git a/src/SMAPI.Web/wwwroot/Content/js/mods.js b/src/SMAPI.Web/wwwroot/Content/js/mods.js index 2cff551f..f7a8501e 100644 --- a/src/SMAPI.Web/wwwroot/Content/js/mods.js +++ b/src/SMAPI.Web/wwwroot/Content/js/mods.js @@ -4,10 +4,19 @@ var smapi = smapi || {}; var app; smapi.modList = function (mods) { // init data + var defaultStats = { + total: 0, + compatible: 0, + workaround: 0, + soon: 0, + broken: 0, + abandoned: 0, + invalid: 0 + }; var data = { mods: mods, - visibleCount: mods.length, showAdvanced: false, + visibleStats: $.extend({}, defaultStats), filters: { source: { open: { @@ -64,6 +73,11 @@ smapi.modList = function (mods) { id: "show-chucklefish", value: true }, + moddrop: { + label: "ModDrop", + id: "show-moddrop", + value: true + }, nexus: { label: "Nexus", id: "show-nexus", @@ -130,27 +144,16 @@ smapi.modList = function (mods) { var words = data.search.toLowerCase().split(" "); // apply criteria - data.visibleCount = data.mods.length; + var stats = data.visibleStats = $.extend({}, defaultStats); for (var i = 0; i < data.mods.length; i++) { var mod = data.mods[i]; mod.Visible = true; // check filters - if (!this.matchesFilters(mod)) { - mod.Visible = false; - data.visibleCount--; - continue; - } - - // check search terms (all search words should match) - if (words.length) { - for (var w = 0; w < words.length; w++) { - if (mod.SearchableText.indexOf(words[w]) === -1) { - mod.Visible = false; - data.visibleCount--; - break; - } - } + mod.Visible = this.matchesFilters(mod, words); + if (mod.Visible) { + stats.total++; + stats[this.getCompatibilityGroup(mod)]++; } } }, @@ -159,9 +162,10 @@ smapi.modList = function (mods) { /** * Get whether a mod matches the current filters. * @param {object} mod The mod to check. + * @param {string[]} searchWords The search words to match. * @returns {bool} Whether the mod matches the filters. */ - matchesFilters: function(mod) { + matchesFilters: function(mod, searchWords) { var filters = data.filters; // check source @@ -180,6 +184,8 @@ smapi.modList = function (mods) { if (!filters.download.chucklefish.value) ignoreSites.push("Chucklefish"); + if (!filters.download.moddrop.value) + ignoreSites.push("ModDrop"); if (!filters.download.nexus.value) ignoreSites.push("Nexus"); if (!filters.download.custom.value) @@ -198,8 +204,50 @@ smapi.modList = function (mods) { return false; } + // check search terms + for (var w = 0; w < searchWords.length; w++) { + if (mod.SearchableText.indexOf(searchWords[w]) === -1) + return false; + } + return true; + }, + + /** + * Get a mod's compatibility group for mod metrics. + * @param {object} mod The mod to check. + * @returns {string} The compatibility group (one of 'compatible', 'workaround', 'soon', 'broken', 'abandoned', or 'invalid'). + */ + getCompatibilityGroup: function (mod) { + var status = (mod.BetaCompatibility || mod.Compatibility).Status; + switch (status) { + // obsolete + case "abandoned": + case "obsolete": + return "abandoned"; + + // compatible + case "ok": + case "optional": + return "compatible"; + + // workaround + case "workaround": + case "unofficial": + return "workaround"; + + // soon/broken + case "broken": + if (mod.SourceUrl) + return "soon"; + else + return "broken"; + + default: + return "invalid"; + } } } }); + app.applyFilters(); }; |