summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/StardewModdingAPI.Models/ModInfoModel.cs48
-rw-r--r--src/StardewModdingAPI.Models/StardewModdingAPI.Models.projitems14
-rw-r--r--src/StardewModdingAPI.Models/StardewModdingAPI.Models.shproj13
-rw-r--r--src/StardewModdingAPI.Tests/StardewModdingAPI.Tests.csproj4
-rw-r--r--src/StardewModdingAPI.Tests/packages.config4
-rw-r--r--src/StardewModdingAPI.Web/Controllers/ModsController.cs132
-rw-r--r--src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs54
-rw-r--r--src/StardewModdingAPI.Web/Framework/InternalControllerFeatureProvider.cs27
-rw-r--r--src/StardewModdingAPI.Web/Framework/ModRepositories/GitHubRepository.cs95
-rw-r--r--src/StardewModdingAPI.Web/Framework/ModRepositories/IModRepository.cs24
-rw-r--r--src/StardewModdingAPI.Web/Framework/ModRepositories/NexusRepository.cs91
-rw-r--r--src/StardewModdingAPI.Web/Framework/RewriteSubdomainRule.cs30
-rw-r--r--src/StardewModdingAPI.Web/Program.cs26
-rw-r--r--src/StardewModdingAPI.Web/Properties/launchSettings.json29
-rw-r--r--src/StardewModdingAPI.Web/StardewModdingAPI.Web.csproj20
-rw-r--r--src/StardewModdingAPI.Web/Startup.cs83
-rw-r--r--src/StardewModdingAPI.Web/appsettings.Development.json10
-rw-r--r--src/StardewModdingAPI.Web/appsettings.json24
-rw-r--r--src/StardewModdingAPI.sln31
-rw-r--r--src/StardewModdingAPI/Events/GameEvents.cs10
-rw-r--r--src/StardewModdingAPI/Framework/Models/GitRelease.cs19
-rw-r--r--src/StardewModdingAPI/Framework/Models/Manifest.cs10
-rw-r--r--src/StardewModdingAPI/Framework/Models/SConfig.cs14
-rw-r--r--src/StardewModdingAPI/Framework/SGame.cs2
-rw-r--r--src/StardewModdingAPI/Framework/UpdateHelper.cs37
-rw-r--r--src/StardewModdingAPI/Framework/WebApiClient.cs70
-rw-r--r--src/StardewModdingAPI/IManifest.cs10
-rw-r--r--src/StardewModdingAPI/Program.cs105
-rw-r--r--src/StardewModdingAPI/StardewModdingAPI.config.json19
-rw-r--r--src/StardewModdingAPI/StardewModdingAPI.csproj4
30 files changed, 968 insertions, 91 deletions
diff --git a/src/StardewModdingAPI.Models/ModInfoModel.cs b/src/StardewModdingAPI.Models/ModInfoModel.cs
new file mode 100644
index 00000000..44071230
--- /dev/null
+++ b/src/StardewModdingAPI.Models/ModInfoModel.cs
@@ -0,0 +1,48 @@
+using Newtonsoft.Json;
+
+namespace StardewModdingAPI.Models
+{
+ /// <summary>Generic metadata about a mod.</summary>
+ internal class ModInfoModel
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The mod name.</summary>
+ public string Name { get; }
+
+ /// <summary>The mod's semantic version number.</summary>
+ public string Version { get; }
+
+ /// <summary>The mod's web URL.</summary>
+ public string Url { get; }
+
+ /// <summary>The error message indicating why the mod is invalid (if applicable).</summary>
+ public string Error { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct a valid instance.</summary>
+ /// <param name="name">The mod name.</param>
+ /// <param name="version">The mod's semantic version number.</param>
+ /// <param name="url">The mod's web URL.</param>
+ /// <param name="error">The error message indicating why the mod is invalid (if applicable).</param>
+ [JsonConstructor]
+ public ModInfoModel(string name, string version, string url, string error = null)
+ {
+ this.Name = name;
+ this.Version = version;
+ this.Url = url;
+ this.Error = error; // mainly initialised here for the JSON deserialiser
+ }
+
+ /// <summary>Construct an valid instance.</summary>
+ /// <param name="error">The error message indicating why the mod is invalid.</param>
+ public ModInfoModel(string error)
+ {
+ this.Error = error;
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.Models/StardewModdingAPI.Models.projitems b/src/StardewModdingAPI.Models/StardewModdingAPI.Models.projitems
new file mode 100644
index 00000000..2465760e
--- /dev/null
+++ b/src/StardewModdingAPI.Models/StardewModdingAPI.Models.projitems
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
+ <HasSharedItems>true</HasSharedItems>
+ <SharedGUID>2aa02fb6-ff03-41cf-a215-2ee60ab4f5dc</SharedGUID>
+ </PropertyGroup>
+ <PropertyGroup Label="Configuration">
+ <Import_RootNamespace>StardewModdingAPI.Models</Import_RootNamespace>
+ </PropertyGroup>
+ <ItemGroup>
+ <Compile Include="$(MSBuildThisFileDirectory)ModInfoModel.cs" />
+ </ItemGroup>
+</Project> \ No newline at end of file
diff --git a/src/StardewModdingAPI.Models/StardewModdingAPI.Models.shproj b/src/StardewModdingAPI.Models/StardewModdingAPI.Models.shproj
new file mode 100644
index 00000000..c80517af
--- /dev/null
+++ b/src/StardewModdingAPI.Models/StardewModdingAPI.Models.shproj
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup Label="Globals">
+ <ProjectGuid>2aa02fb6-ff03-41cf-a215-2ee60ab4f5dc</ProjectGuid>
+ <MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
+ </PropertyGroup>
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
+ <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
+ <PropertyGroup />
+ <Import Project="StardewModdingAPI.Models.projitems" Label="Shared" />
+ <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
+</Project>
diff --git a/src/StardewModdingAPI.Tests/StardewModdingAPI.Tests.csproj b/src/StardewModdingAPI.Tests/StardewModdingAPI.Tests.csproj
index f3dbcdd4..41525bcb 100644
--- a/src/StardewModdingAPI.Tests/StardewModdingAPI.Tests.csproj
+++ b/src/StardewModdingAPI.Tests/StardewModdingAPI.Tests.csproj
@@ -39,8 +39,8 @@
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
- <Reference Include="nunit.framework, Version=3.7.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
- <HintPath>..\packages\NUnit.3.7.1\lib\net45\nunit.framework.dll</HintPath>
+ <Reference Include="nunit.framework, Version=3.8.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
+ <HintPath>..\packages\NUnit.3.8.1\lib\net45\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
</ItemGroup>
diff --git a/src/StardewModdingAPI.Tests/packages.config b/src/StardewModdingAPI.Tests/packages.config
index 6f04e625..5fdfebdb 100644
--- a/src/StardewModdingAPI.Tests/packages.config
+++ b/src/StardewModdingAPI.Tests/packages.config
@@ -3,5 +3,5 @@
<package id="Castle.Core" version="4.1.1" targetFramework="net45" />
<package id="Moq" version="4.7.99" targetFramework="net45" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net45" />
- <package id="NUnit" version="3.7.1" targetFramework="net45" />
-</packages> \ No newline at end of file
+ <package id="NUnit" version="3.8.1" targetFramework="net45" />
+</packages>
diff --git a/src/StardewModdingAPI.Web/Controllers/ModsController.cs b/src/StardewModdingAPI.Web/Controllers/ModsController.cs
new file mode 100644
index 00000000..8fc2cb51
--- /dev/null
+++ b/src/StardewModdingAPI.Web/Controllers/ModsController.cs
@@ -0,0 +1,132 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Caching.Memory;
+using Microsoft.Extensions.Options;
+using StardewModdingAPI.Web.Framework.ConfigModels;
+using StardewModdingAPI.Web.Framework.ModRepositories;
+using StardewModdingAPI.Models;
+
+namespace StardewModdingAPI.Web.Controllers
+{
+ /// <summary>Provides an API to perform mod update checks.</summary>
+ [Produces("application/json")]
+ internal class ModsController : Controller
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The mod repositories which provide mod metadata.</summary>
+ private readonly IDictionary<string, IModRepository> Repositories;
+
+ /// <summary>The cache in which to store mod metadata.</summary>
+ private readonly IMemoryCache Cache;
+
+ /// <summary>The number of minutes update checks should be cached before refetching them.</summary>
+ private readonly int CacheMinutes;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="cache">The cache in which to store mod metadata.</param>
+ /// <param name="configProvider">The config settings for mod update checks.</param>
+ public ModsController(IMemoryCache cache, IOptions<ModUpdateCheckConfig> configProvider)
+ {
+ ModUpdateCheckConfig config = configProvider.Value;
+
+ this.Cache = cache;
+ this.CacheMinutes = config.CacheMinutes;
+
+ this.Repositories =
+ new IModRepository[]
+ {
+ new GitHubRepository(
+ vendorKey: config.GitHubKey,
+ baseUrl: config.GitHubBaseUrl,
+ releaseUrlFormat: config.GitHubReleaseUrlFormat,
+ userAgent: config.GitHubUserAgent,
+ acceptHeader: config.GitHubAcceptHeader,
+ username: config.GitHubUsername,
+ password: config.GitHubPassword
+ ),
+ new NexusRepository(
+ vendorKey: config.NexusKey,
+ userAgent: config.NexusUserAgent,
+ baseUrl: config.NexusBaseUrl,
+ modUrlFormat: config.NexusModUrlFormat
+ )
+ }
+ .ToDictionary(p => p.VendorKey, StringComparer.CurrentCultureIgnoreCase);
+ }
+
+ /// <summary>Fetch version metadata for the given mods.</summary>
+ /// <param name="modKeys">The namespaced mod keys to search as a comma-delimited array.</param>
+ [HttpGet]
+ public async Task<IDictionary<string, ModInfoModel>> GetAsync(string modKeys)
+ {
+ // sort & filter keys
+ string[] modKeysArray = (modKeys?.Split(',').Select(p => p.Trim()).ToArray() ?? new string[0])
+ .Distinct(StringComparer.CurrentCultureIgnoreCase)
+ .OrderBy(p => p, StringComparer.CurrentCultureIgnoreCase)
+ .ToArray();
+
+ // fetch mod info
+ IDictionary<string, ModInfoModel> result = new Dictionary<string, ModInfoModel>(StringComparer.CurrentCultureIgnoreCase);
+ foreach (string modKey in modKeysArray)
+ {
+ // parse mod key
+ if (!this.TryParseModKey(modKey, out string vendorKey, out string modID))
+ {
+ result[modKey] = new ModInfoModel("The mod key isn't in a valid format. It should contain the mod repository key and mod ID like 'Nexus:541'.");
+ continue;
+ }
+
+ // get matching repository
+ if (!this.Repositories.TryGetValue(vendorKey, out IModRepository repository))
+ {
+ result[modKey] = new ModInfoModel("There's no mod repository matching this namespaced mod ID.");
+ continue;
+ }
+
+ // fetch mod info
+ result[modKey] = await this.Cache.GetOrCreateAsync($"{repository.VendorKey}:{modID}".ToLower(), async entry =>
+ {
+ entry.AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(this.CacheMinutes);
+ return await repository.GetModInfoAsync(modID);
+ });
+ }
+
+ return result;
+ }
+
+
+ /*********
+ ** Private methods
+ *********/
+ /// <summary>Parse a namespaced mod ID.</summary>
+ /// <param name="raw">The raw mod ID to parse.</param>
+ /// <param name="vendorKey">The parsed vendor key.</param>
+ /// <param name="modID">The parsed mod ID.</param>
+ /// <returns>Returns whether the value could be parsed.</returns>
+ 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];
+ modID = parts[1];
+ return true;
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs b/src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs
new file mode 100644
index 00000000..5d55ba18
--- /dev/null
+++ b/src/StardewModdingAPI.Web/Framework/ConfigModels/ModUpdateCheckConfig.cs
@@ -0,0 +1,54 @@
+namespace StardewModdingAPI.Web.Framework.ConfigModels
+{
+ /// <summary>The config settings for mod update checks.</summary>
+ public class ModUpdateCheckConfig
+ {
+ /*********
+ ** Accessors
+ *********/
+ /****
+ ** General
+ ****/
+ /// <summary>The number of minutes update checks should be cached before refetching them.</summary>
+ public int CacheMinutes { get; set; }
+
+ /****
+ ** GitHub
+ ****/
+ /// <summary>The repository key for Nexus Mods.</summary>
+ public string GitHubKey { get; set; }
+
+ /// <summary>The user agent for the GitHub API client.</summary>
+ public string GitHubUserAgent { get; set; }
+
+ /// <summary>The base URL for the GitHub API.</summary>
+ public string GitHubBaseUrl { get; set; }
+
+ /// <summary>The URL for a GitHub API latest-release query excluding the <see cref="GitHubBaseUrl"/>, where {0} is the organisation and project name.</summary>
+ public string GitHubReleaseUrlFormat { get; set; }
+
+ /// <summary>The Accept header value expected by the GitHub API.</summary>
+ public string GitHubAcceptHeader { get; set; }
+
+ /// <summary>The username with which to authenticate to the GitHub API (if any).</summary>
+ public string GitHubUsername { get; set; }
+
+ /// <summary>The password with which to authenticate to the GitHub API (if any).</summary>
+ public string GitHubPassword { get; set; }
+
+ /****
+ ** Nexus Mods
+ ****/
+ /// <summary>The repository key for Nexus Mods.</summary>
+ public string NexusKey { get; set; }
+
+ /// <summary>The user agent for the Nexus Mods API client.</summary>
+ public string NexusUserAgent { get; set; }
+
+ /// <summary>The base URL for the Nexus Mods API.</summary>
+ public string NexusBaseUrl { get; set; }
+
+ /// <summary>The URL for a Nexus Mods API query excluding the <see cref="NexusBaseUrl"/>, where {0} is the mod ID.</summary>
+ public string NexusModUrlFormat { get; set; }
+ }
+}
diff --git a/src/StardewModdingAPI.Web/Framework/InternalControllerFeatureProvider.cs b/src/StardewModdingAPI.Web/Framework/InternalControllerFeatureProvider.cs
new file mode 100644
index 00000000..2c24c610
--- /dev/null
+++ b/src/StardewModdingAPI.Web/Framework/InternalControllerFeatureProvider.cs
@@ -0,0 +1,27 @@
+using System;
+using System.Reflection;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Controllers;
+
+namespace StardewModdingAPI.Web.Framework
+{
+ /// <summary>Discovers controllers with support for non-public controllers.</summary>
+ internal class InternalControllerFeatureProvider : ControllerFeatureProvider
+ {
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Determines if a given type is a controller.</summary>
+ /// <param name="type">The <see cref="T:System.Reflection.TypeInfo" /> candidate.</param>
+ /// <returns><code>true</code> if the type is a controller; otherwise <code>false</code>.</returns>
+ protected override bool IsController(TypeInfo type)
+ {
+ return
+ type.IsClass
+ && !type.IsAbstract
+ && (/*type.IsPublic &&*/ !type.ContainsGenericParameters)
+ && (!type.IsDefined(typeof(NonControllerAttribute))
+ && (type.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) || type.IsDefined(typeof(ControllerAttribute))));
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.Web/Framework/ModRepositories/GitHubRepository.cs b/src/StardewModdingAPI.Web/Framework/ModRepositories/GitHubRepository.cs
new file mode 100644
index 00000000..421220de
--- /dev/null
+++ b/src/StardewModdingAPI.Web/Framework/ModRepositories/GitHubRepository.cs
@@ -0,0 +1,95 @@
+using System;
+using System.Threading.Tasks;
+using Newtonsoft.Json;
+using Pathoschild.Http.Client;
+using StardewModdingAPI.Models;
+
+namespace StardewModdingAPI.Web.Framework.ModRepositories
+{
+ /// <summary>An HTTP client for fetching mod metadata from GitHub project releases.</summary>
+ internal class GitHubRepository : IModRepository
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The underlying HTTP client.</summary>
+ private readonly IClient Client;
+
+
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The unique key for this vendor.</summary>
+ public string VendorKey { get; }
+
+ /// <summary>The URL for a Nexus Mods API query excluding the base URL, where {0} is the mod ID.</summary>
+ public string ReleaseUrlFormat { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="vendorKey">The unique key for this vendor.</param>
+ /// <param name="baseUrl">The base URL for the Nexus Mods API.</param>
+ /// <param name="releaseUrlFormat">The URL for a Nexus Mods API query excluding the <paramref name="baseUrl"/>, where {0} is the mod ID.</param>
+ /// <param name="userAgent">The user agent for the GitHub API client.</param>
+ /// <param name="acceptHeader">The Accept header value expected by the GitHub API.</param>
+ /// <param name="username">The username with which to authenticate to the GitHub API.</param>
+ /// <param name="password">The password with which to authenticate to the GitHub API.</param>
+ public GitHubRepository(string vendorKey, string baseUrl, string releaseUrlFormat, string userAgent, string acceptHeader, string username, string password)
+ {
+ this.VendorKey = vendorKey;
+ this.ReleaseUrlFormat = releaseUrlFormat;
+
+ this.Client = new FluentClient(baseUrl)
+ .SetUserAgent(string.Format(userAgent, this.GetType().Assembly.GetName().Version))
+ .AddDefault(req => req.WithHeader("Accept", acceptHeader));
+ if (!string.IsNullOrWhiteSpace(username))
+ this.Client = this.Client.SetBasicAuthentication(username, password);
+ }
+
+ /// <summary>Get metadata about a mod in the repository.</summary>
+ /// <param name="id">The mod ID in this repository.</param>
+ public async Task<ModInfoModel> GetModInfoAsync(string id)
+ {
+ try
+ {
+ GitRelease release = await this.Client
+ .GetAsync(string.Format(this.ReleaseUrlFormat, id))
+ .As<GitRelease>();
+
+ return new ModInfoModel(id, release.Tag, $"https://github.com/{id}/releases");
+ }
+ catch (Exception ex)
+ {
+ return new ModInfoModel(ex.ToString());
+ }
+ }
+
+ /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
+ public void Dispose()
+ {
+ this.Client.Dispose();
+ }
+
+
+ /*********
+ ** Private models
+ *********/
+ /// <summary>Metadata about a GitHub release tag.</summary>
+ private class GitRelease
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The display name.</summary>
+ [JsonProperty("name")]
+ public string Name { get; set; }
+
+ /// <summary>The semantic version string.</summary>
+ [JsonProperty("tag_name")]
+ public string Tag { get; set; }
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.Web/Framework/ModRepositories/IModRepository.cs b/src/StardewModdingAPI.Web/Framework/ModRepositories/IModRepository.cs
new file mode 100644
index 00000000..98e4c957
--- /dev/null
+++ b/src/StardewModdingAPI.Web/Framework/ModRepositories/IModRepository.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Threading.Tasks;
+using StardewModdingAPI.Models;
+
+namespace StardewModdingAPI.Web.Framework.ModRepositories
+{
+ /// <summary>A repository which provides mod metadata.</summary>
+ internal interface IModRepository : IDisposable
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The unique key for this vendor.</summary>
+ string VendorKey { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Get metadata about a mod in the repository.</summary>
+ /// <param name="id">The mod ID in this repository.</param>
+ Task<ModInfoModel> GetModInfoAsync(string id);
+ }
+}
diff --git a/src/StardewModdingAPI.Web/Framework/ModRepositories/NexusRepository.cs b/src/StardewModdingAPI.Web/Framework/ModRepositories/NexusRepository.cs
new file mode 100644
index 00000000..6cf5b04a
--- /dev/null
+++ b/src/StardewModdingAPI.Web/Framework/ModRepositories/NexusRepository.cs
@@ -0,0 +1,91 @@
+using System;
+using System.Threading.Tasks;
+using Newtonsoft.Json;
+using Pathoschild.Http.Client;
+using StardewModdingAPI.Models;
+
+namespace StardewModdingAPI.Web.Framework.ModRepositories
+{
+ /// <summary>An HTTP client for fetching mod metadata from Nexus Mods.</summary>
+ internal class NexusRepository : IModRepository
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The underlying HTTP client.</summary>
+ private readonly IClient Client;
+
+
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The unique key for this vendor.</summary>
+ public string VendorKey { get; }
+
+ /// <summary>The URL for a Nexus Mods API query excluding the base URL, where {0} is the mod ID.</summary>
+ public string ModUrlFormat { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="vendorKey">The unique key for this vendor.</param>
+ /// <param name="userAgent">The user agent for the Nexus Mods API client.</param>
+ /// <param name="baseUrl">The base URL for the Nexus Mods API.</param>
+ /// <param name="modUrlFormat">The URL for a Nexus Mods API query excluding the <paramref name="baseUrl"/>, where {0} is the mod ID.</param>
+ public NexusRepository(string vendorKey, string userAgent, string baseUrl, string modUrlFormat)
+ {
+ this.VendorKey = vendorKey;
+ this.ModUrlFormat = modUrlFormat;
+ this.Client = new FluentClient(baseUrl).SetUserAgent(userAgent);
+ }
+
+ /// <summary>Get metadata about a mod in the repository.</summary>
+ /// <param name="id">The mod ID in this repository.</param>
+ public async Task<ModInfoModel> GetModInfoAsync(string id)
+ {
+ try
+ {
+ NexusResponseModel response = await this.Client
+ .GetAsync(string.Format(this.ModUrlFormat, id))
+ .As<NexusResponseModel>();
+
+ return response != null
+ ? new ModInfoModel(response.Name, response.Version, response.Url)
+ : new ModInfoModel("Found no mod with this ID.");
+ }
+ catch (Exception ex)
+ {
+ return new ModInfoModel(ex.ToString());
+ }
+ }
+
+ /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
+ public void Dispose()
+ {
+ this.Client.Dispose();
+ }
+
+
+ /*********
+ ** Private models
+ *********/
+ /// <summary>A mod metadata response from Nexus Mods.</summary>
+ private class NexusResponseModel
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The mod name.</summary>
+ public string Name { get; set; }
+
+ /// <summary>The mod's semantic version number.</summary>
+ public string Version { get; set; }
+
+ /// <summary>The mod's web URL.</summary>
+ [JsonProperty("mod_page_uri")]
+ public string Url { get; set; }
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.Web/Framework/RewriteSubdomainRule.cs b/src/StardewModdingAPI.Web/Framework/RewriteSubdomainRule.cs
new file mode 100644
index 00000000..5a56844f
--- /dev/null
+++ b/src/StardewModdingAPI.Web/Framework/RewriteSubdomainRule.cs
@@ -0,0 +1,30 @@
+using System;
+using Microsoft.AspNetCore.Rewrite;
+
+namespace StardewModdingAPI.Web.Framework
+{
+ /// <summary>Rewrite requests to prepend the subdomain portion (if any) to the path.</summary>
+ /// <remarks>Derived from <a href="https://stackoverflow.com/a/44526747/262123" />.</remarks>
+ internal class RewriteSubdomainRule : IRule
+ {
+ /// <summary>Applies the rule. Implementations of ApplyRule should set the value for <see cref="RewriteContext.Result" /> (defaults to RuleResult.ContinueRules).</summary>
+ /// <param name="context">The rewrite context.</param>
+ public void ApplyRule(RewriteContext context)
+ {
+ context.Result = RuleResult.ContinueRules;
+
+ // get host parts
+ string host = context.HttpContext.Request.Host.Host;
+ string[] parts = host.Split('.');
+
+ // validate
+ if (parts.Length < 2)
+ return;
+ if (parts.Length < 3 && !"localhost".Equals(parts[1], StringComparison.InvariantCultureIgnoreCase))
+ return;
+
+ // prepend to path
+ context.HttpContext.Request.Path = $"/{parts[0]}{context.HttpContext.Request.Path}";
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.Web/Program.cs b/src/StardewModdingAPI.Web/Program.cs
new file mode 100644
index 00000000..eeecb791
--- /dev/null
+++ b/src/StardewModdingAPI.Web/Program.cs
@@ -0,0 +1,26 @@
+using System.IO;
+using Microsoft.AspNetCore.Hosting;
+
+namespace StardewModdingAPI.Web
+{
+ /// <summary>The main app entry point.</summary>
+ public class Program
+ {
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>The main app entry point.</summary>
+ /// <param name="args">The command-line arguments.</param>
+ public static void Main(string[] args)
+ {
+ // configure web server
+ new WebHostBuilder()
+ .UseKestrel()
+ .UseContentRoot(Directory.GetCurrentDirectory())
+ .UseIISIntegration()
+ .UseStartup<Startup>()
+ .Build()
+ .Run();
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.Web/Properties/launchSettings.json b/src/StardewModdingAPI.Web/Properties/launchSettings.json
new file mode 100644
index 00000000..3acee14d
--- /dev/null
+++ b/src/StardewModdingAPI.Web/Properties/launchSettings.json
@@ -0,0 +1,29 @@
+{
+ "iisSettings": {
+ "windowsAuthentication": false,
+ "anonymousAuthentication": true,
+ "iisExpress": {
+ "applicationUrl": "http://localhost:59482/",
+ "sslPort": 0
+ }
+ },
+ "profiles": {
+ "IIS Express": {
+ "commandName": "IISExpress",
+ "launchBrowser": true,
+ "launchUrl": "api/v1.0/mods",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ },
+ "Dewdrop": {
+ "commandName": "Project",
+ "launchBrowser": true,
+ "launchUrl": "api/v1.0/mods",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ },
+ "applicationUrl": "http://localhost:59483"
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.Web/StardewModdingAPI.Web.csproj b/src/StardewModdingAPI.Web/StardewModdingAPI.Web.csproj
new file mode 100644
index 00000000..c30abc55
--- /dev/null
+++ b/src/StardewModdingAPI.Web/StardewModdingAPI.Web.csproj
@@ -0,0 +1,20 @@
+<Project Sdk="Microsoft.NET.Sdk.Web">
+
+ <PropertyGroup>
+ <TargetFramework>netcoreapp2.0</TargetFramework>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
+ <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
+ <PackageReference Include="Microsoft.AspNetCore.Rewrite" Version="2.0.0" />
+ <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
+ <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />