From 00b067fead282bb30eb8ca67572f16bd6e1920b7 Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Sun, 25 Aug 2019 17:14:55 -0700 Subject: [PATCH] detect the client's platform and check the appropriate input in the log parser --- .../ClientPlatformMiddleware.cs | 52 +++++++++++++++++++ .../ClientPlatformMiddlewareExtensions.cs | 16 ++++++ .../Framework/UserAgentParsing/Platform.cs | 18 +++++++ src/SMAPI.Web/Startup.cs | 2 + src/SMAPI.Web/Views/LogParser/Index.cshtml | 12 +++-- 5 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 src/SMAPI.Web/Framework/UserAgentParsing/ClientPlatformMiddleware.cs create mode 100644 src/SMAPI.Web/Framework/UserAgentParsing/ClientPlatformMiddlewareExtensions.cs create mode 100644 src/SMAPI.Web/Framework/UserAgentParsing/Platform.cs 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 +{ + /// Middleware that detects the client's platform. + public class ClientPlatformMiddleware + { + /// The key used to retrieve the client's platform from . + public const string ClientPlatformKey = "ClientPlatformKey"; + + /// The next delegate in the middleware pipeline. + private readonly RequestDelegate Next; + + /// Construct an instance. + /// The next delegate in the middleware pipeline. + public ClientPlatformMiddleware(RequestDelegate next) + { + this.Next = next; + } + + /// Invoke the middleware. + /// The HTTP request context. + public async Task InvokeAsync(HttpContext context) + { + context.Items[ClientPlatformMiddleware.ClientPlatformKey] = this.DetectClientPlatform(context.Request.Headers["User-Agent"]); + + await this.Next(context); + } + + /// Detect the platform that the client is on. + /// The client's user agent. + /// The client's platform, or null if no platforms could be detected. + 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 +{ + /// Extension methods for the client platform middleware. + internal static class ClientPlatformMiddlewareExtensions + { + /// Adds client platform detection to the request pipeline. + /// The application builder. + /// The application builder with the client platform middleware enabled. + public static IApplicationBuilder UseClientPlatform(this IApplicationBuilder builder) + { + return builder.UseMiddleware(); + } + } +} 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 +{ + /// A software platform. + public enum Platform + { + /// The Android platform. + Android, + + /// The Linux platform. + Linux, + + /// The Mac platform. + Mac, + + /// The Windows platform. + 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)

Where do I find my SMAPI log?

What system do you use?
On Android: