diff options
Diffstat (limited to 'src')
5 files changed, 96 insertions, 4 deletions
diff --git a/src/SMAPI.Web/Framework/UserAgentParsing/ClientPlatformMiddleware.cs b/src/SMAPI.Web/Framework/UserAgentParsing/ClientPlatformMiddleware.cs new file mode 100644 index 00000000..d056d85c --- /dev/null +++ b/src/SMAPI.Web/Framework/UserAgentParsing/ClientPlatformMiddleware.cs @@ -0,0 +1,52 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; + +namespace StardewModdingAPI.Web.Framework.UserAgentParsing +{ + /// <summary>Middleware that detects the client's platform.</summary> + public class ClientPlatformMiddleware + { + /// <summary>The key used to retrieve the client's platform from <see cref="HttpContext.Items"/>.</summary> + public const string ClientPlatformKey = "ClientPlatformKey"; + + /// <summary>The next delegate in the middleware pipeline.</summary> + private readonly RequestDelegate Next; + + /// <summary>Construct an instance.</summary> + /// <param name="next">The next delegate in the middleware pipeline.</param> + public ClientPlatformMiddleware(RequestDelegate next) + { + this.Next = next; + } + + /// <summary>Invoke the middleware.</summary> + /// <param name="context">The HTTP request context.</param> + public async Task InvokeAsync(HttpContext context) + { + context.Items[ClientPlatformMiddleware.ClientPlatformKey] = this.DetectClientPlatform(context.Request.Headers["User-Agent"]); + + await this.Next(context); + } + + /// <summary>Detect the platform that the client is on.</summary> + /// <param name="userAgent">The client's user agent.</param> + /// <returns>The client's platform, or null if no platforms could be detected.</returns> + private Platform? DetectClientPlatform(string userAgent) + { + switch (userAgent) + { + case string ua when ua.Contains("Windows"): + return Platform.Windows; + // check for Android before Linux because Android user agents also contain Linux + case string ua when ua.Contains("Android"): + return Platform.Android; + case string ua when ua.Contains("Linux"): + return Platform.Linux; + case string ua when ua.Contains("Mac"): + return Platform.Mac; + default: + return null; + } + } + } +} diff --git a/src/SMAPI.Web/Framework/UserAgentParsing/ClientPlatformMiddlewareExtensions.cs b/src/SMAPI.Web/Framework/UserAgentParsing/ClientPlatformMiddlewareExtensions.cs new file mode 100644 index 00000000..4ac2ebfa --- /dev/null +++ b/src/SMAPI.Web/Framework/UserAgentParsing/ClientPlatformMiddlewareExtensions.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Builder; + +namespace StardewModdingAPI.Web.Framework.UserAgentParsing +{ + /// <summary>Extension methods for the client platform middleware.</summary> + internal static class ClientPlatformMiddlewareExtensions + { + /// <summary>Adds client platform detection to the request pipeline.</summary> + /// <param name="builder">The application builder.</param> + /// <returns>The application builder with the client platform middleware enabled.</returns> + public static IApplicationBuilder UseClientPlatform(this IApplicationBuilder builder) + { + return builder.UseMiddleware<ClientPlatformMiddleware>(); + } + } +} diff --git a/src/SMAPI.Web/Framework/UserAgentParsing/Platform.cs b/src/SMAPI.Web/Framework/UserAgentParsing/Platform.cs new file mode 100644 index 00000000..07a247cb --- /dev/null +++ b/src/SMAPI.Web/Framework/UserAgentParsing/Platform.cs @@ -0,0 +1,18 @@ +namespace StardewModdingAPI.Web.Framework.UserAgentParsing +{ + /// <summary>A software platform.</summary> + public enum Platform + { + /// <summary>The Android platform.</summary> + Android, + + /// <summary>The Linux platform.</summary> + Linux, + + /// <summary>The Mac platform.</summary> + Mac, + + /// <summary>The Windows platform.</summary> + Windows + } +} diff --git a/src/SMAPI.Web/Startup.cs b/src/SMAPI.Web/Startup.cs index da5c1f1b..04ec47a6 100644 --- a/src/SMAPI.Web/Startup.cs +++ b/src/SMAPI.Web/Startup.cs @@ -23,6 +23,7 @@ using StardewModdingAPI.Web.Framework.Clients.Pastebin; using StardewModdingAPI.Web.Framework.Compression; using StardewModdingAPI.Web.Framework.ConfigModels; using StardewModdingAPI.Web.Framework.RewriteRules; +using StardewModdingAPI.Web.Framework.UserAgentParsing; namespace StardewModdingAPI.Web { @@ -172,6 +173,7 @@ namespace StardewModdingAPI.Web ) .UseRewriter(this.GetRedirectRules()) .UseStaticFiles() // wwwroot folder + .UseClientPlatform() .UseMvc(); // enable Hangfire dashboard diff --git a/src/SMAPI.Web/Views/LogParser/Index.cshtml b/src/SMAPI.Web/Views/LogParser/Index.cshtml index e974c308..0dca003b 100644 --- a/src/SMAPI.Web/Views/LogParser/Index.cshtml +++ b/src/SMAPI.Web/Views/LogParser/Index.cshtml @@ -1,5 +1,6 @@ @using Newtonsoft.Json @using StardewModdingAPI.Web.Framework.LogParsing.Models +@using StardewModdingAPI.Web.Framework.UserAgentParsing @model StardewModdingAPI.Web.ViewModels.LogParserModel @{ @@ -67,10 +68,13 @@ else if (Model.ParsedLog?.IsValid == true) <h2>Where do I find my SMAPI log?</h2> <div>What system do you use?</div> <ul id="os-list"> - <li><input type="radio" name="os" value="android" id="os-android" /> <label for="os-android">Android</label></li> - <li><input type="radio" name="os" value="linux" id="os-linux" /> <label for="os-linux">Linux</label></li> - <li><input type="radio" name="os" value="mac" id="os-mac" /> <label for="os-mac">Mac</label></li> - <li><input type="radio" name="os" value="windows" id="os-windows" /> <label for="os-windows">Windows</label></li> + @{ + Platform? clientPlatform = Context.Items[ClientPlatformMiddleware.ClientPlatformKey] as Platform?; + } + <li><input type="radio" name="os" value="android" id="os-android" @(clientPlatform == Platform.Android ? "checked" : "")/> <label for="os-android">Android</label></li> + <li><input type="radio" name="os" value="linux" id="os-linux" @(clientPlatform == Platform.Linux ? "checked" : "")/> <label for="os-linux">Linux</label></li> + <li><input type="radio" name="os" value="mac" id="os-mac" @(clientPlatform == Platform.Mac ? "checked" : "")/> <label for="os-mac">Mac</label></li> + <li><input type="radio" name="os" value="windows" id="os-windows" @(clientPlatform == Platform.Windows ? "checked" : "")/> <label for="os-windows">Windows</label></li> </ul> <div data-os="android"> On Android: |