From 00b067fead282bb30eb8ca67572f16bd6e1920b7 Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Sun, 25 Aug 2019 17:14:55 -0700 Subject: detect the client's platform and check the appropriate input in the log parser --- .../UserAgentParsing/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 (limited to 'src') 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: -- cgit From f04f7811536ba6e35c4ad6357fb6904826eda9dc Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Sun, 25 Aug 2019 17:21:03 -0700 Subject: rename namespace to be more accurate --- .../ClientPlatformMiddleware.cs | 52 ++++++++++++++++++++++ .../ClientPlatformMiddlewareExtensions.cs | 16 +++++++ .../Framework/ClientPlatformDetection/Platform.cs | 18 ++++++++ .../UserAgentParsing/ClientPlatformMiddleware.cs | 52 ---------------------- .../ClientPlatformMiddlewareExtensions.cs | 16 ------- .../Framework/UserAgentParsing/Platform.cs | 18 -------- src/SMAPI.Web/Startup.cs | 2 +- src/SMAPI.Web/Views/LogParser/Index.cshtml | 2 +- 8 files changed, 88 insertions(+), 88 deletions(-) create mode 100644 src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs create mode 100644 src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs create mode 100644 src/SMAPI.Web/Framework/ClientPlatformDetection/Platform.cs delete mode 100644 src/SMAPI.Web/Framework/UserAgentParsing/ClientPlatformMiddleware.cs delete mode 100644 src/SMAPI.Web/Framework/UserAgentParsing/ClientPlatformMiddlewareExtensions.cs delete mode 100644 src/SMAPI.Web/Framework/UserAgentParsing/Platform.cs (limited to 'src') diff --git a/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs b/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs new file mode 100644 index 00000000..2430d239 --- /dev/null +++ b/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs @@ -0,0 +1,52 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; + +namespace StardewModdingAPI.Web.Framework.ClientPlatformDetection +{ + /// 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/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs b/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs new file mode 100644 index 00000000..274e5486 --- /dev/null +++ b/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Builder; + +namespace StardewModdingAPI.Web.Framework.ClientPlatformDetection +{ + /// 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/ClientPlatformDetection/Platform.cs b/src/SMAPI.Web/Framework/ClientPlatformDetection/Platform.cs new file mode 100644 index 00000000..2fe1d324 --- /dev/null +++ b/src/SMAPI.Web/Framework/ClientPlatformDetection/Platform.cs @@ -0,0 +1,18 @@ +namespace StardewModdingAPI.Web.Framework.ClientPlatformDetection +{ + /// 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/Framework/UserAgentParsing/ClientPlatformMiddleware.cs b/src/SMAPI.Web/Framework/UserAgentParsing/ClientPlatformMiddleware.cs deleted file mode 100644 index d056d85c..00000000 --- a/src/SMAPI.Web/Framework/UserAgentParsing/ClientPlatformMiddleware.cs +++ /dev/null @@ -1,52 +0,0 @@ -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 deleted file mode 100644 index 4ac2ebfa..00000000 --- a/src/SMAPI.Web/Framework/UserAgentParsing/ClientPlatformMiddlewareExtensions.cs +++ /dev/null @@ -1,16 +0,0 @@ -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 deleted file mode 100644 index 07a247cb..00000000 --- a/src/SMAPI.Web/Framework/UserAgentParsing/Platform.cs +++ /dev/null @@ -1,18 +0,0 @@ -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 04ec47a6..e12fa63b 100644 --- a/src/SMAPI.Web/Startup.cs +++ b/src/SMAPI.Web/Startup.cs @@ -15,6 +15,7 @@ using StardewModdingAPI.Web.Framework; using StardewModdingAPI.Web.Framework.Caching; using StardewModdingAPI.Web.Framework.Caching.Mods; using StardewModdingAPI.Web.Framework.Caching.Wiki; +using StardewModdingAPI.Web.Framework.ClientPlatformDetection; using StardewModdingAPI.Web.Framework.Clients.Chucklefish; using StardewModdingAPI.Web.Framework.Clients.GitHub; using StardewModdingAPI.Web.Framework.Clients.ModDrop; @@ -23,7 +24,6 @@ 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 { diff --git a/src/SMAPI.Web/Views/LogParser/Index.cshtml b/src/SMAPI.Web/Views/LogParser/Index.cshtml index 0dca003b..7f8036db 100644 --- a/src/SMAPI.Web/Views/LogParser/Index.cshtml +++ b/src/SMAPI.Web/Views/LogParser/Index.cshtml @@ -1,6 +1,6 @@ @using Newtonsoft.Json +@using StardewModdingAPI.Web.Framework.ClientPlatformDetection @using StardewModdingAPI.Web.Framework.LogParsing.Models -@using StardewModdingAPI.Web.Framework.UserAgentParsing @model StardewModdingAPI.Web.ViewModels.LogParserModel @{ -- cgit From d49ead6113c85d3b30db664aad1a965ffdef6bbb Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 31 Aug 2019 16:26:42 -0400 Subject: remove custom enum, add Android to SMAPI's platform enum This will also be used in an upcoming commit for SMAPI's target platform constant. --- src/SMAPI.Toolkit/Utilities/Platform.cs | 3 +++ .../ClientPlatformMiddleware.cs | 1 + .../Framework/ClientPlatformDetection/Platform.cs | 18 ------------------ src/SMAPI.Web/Views/LogParser/Index.cshtml | 1 + 4 files changed, 5 insertions(+), 18 deletions(-) delete mode 100644 src/SMAPI.Web/Framework/ClientPlatformDetection/Platform.cs (limited to 'src') diff --git a/src/SMAPI.Toolkit/Utilities/Platform.cs b/src/SMAPI.Toolkit/Utilities/Platform.cs index d64cbeb9..f780e812 100644 --- a/src/SMAPI.Toolkit/Utilities/Platform.cs +++ b/src/SMAPI.Toolkit/Utilities/Platform.cs @@ -3,6 +3,9 @@ namespace StardewModdingAPI.Toolkit.Utilities /// The game's platform version. public enum Platform { + /// The Android version of the game. + Android, + /// The Linux version of the game. Linux, diff --git a/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs b/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs index 2430d239..2bf23ff9 100644 --- a/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs +++ b/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using StardewModdingAPI.Toolkit.Utilities; namespace StardewModdingAPI.Web.Framework.ClientPlatformDetection { diff --git a/src/SMAPI.Web/Framework/ClientPlatformDetection/Platform.cs b/src/SMAPI.Web/Framework/ClientPlatformDetection/Platform.cs deleted file mode 100644 index 2fe1d324..00000000 --- a/src/SMAPI.Web/Framework/ClientPlatformDetection/Platform.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace StardewModdingAPI.Web.Framework.ClientPlatformDetection -{ - /// 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/Views/LogParser/Index.cshtml b/src/SMAPI.Web/Views/LogParser/Index.cshtml index 7f8036db..42887019 100644 --- a/src/SMAPI.Web/Views/LogParser/Index.cshtml +++ b/src/SMAPI.Web/Views/LogParser/Index.cshtml @@ -1,4 +1,5 @@ @using Newtonsoft.Json +@using StardewModdingAPI.Toolkit.Utilities @using StardewModdingAPI.Web.Framework.ClientPlatformDetection @using StardewModdingAPI.Web.Framework.LogParsing.Models @model StardewModdingAPI.Web.ViewModels.LogParserModel -- cgit From e7b214390a09fc6edc7664bce0b48cecf1d986a8 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 31 Aug 2019 16:27:36 -0400 Subject: move platform to log parser model instead of middleware --- src/SMAPI.Web/Controllers/LogParserController.cs | 43 ++++++++++++++++-- .../ClientPlatformMiddleware.cs | 53 ---------------------- .../ClientPlatformMiddlewareExtensions.cs | 16 ------- src/SMAPI.Web/Startup.cs | 2 - src/SMAPI.Web/ViewModels/LogParserModel.cs | 17 ++++--- src/SMAPI.Web/Views/LogParser/Index.cshtml | 13 +++--- 6 files changed, 56 insertions(+), 88 deletions(-) delete mode 100644 src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs delete mode 100644 src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs (limited to 'src') diff --git a/src/SMAPI.Web/Controllers/LogParserController.cs b/src/SMAPI.Web/Controllers/LogParserController.cs index 0556a81e..2a071b6c 100644 --- a/src/SMAPI.Web/Controllers/LogParserController.cs +++ b/src/SMAPI.Web/Controllers/LogParserController.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; +using StardewModdingAPI.Toolkit.Utilities; using StardewModdingAPI.Web.Framework; using StardewModdingAPI.Web.Framework.Clients.Pastebin; using StardewModdingAPI.Web.Framework.Compression; @@ -59,14 +60,14 @@ namespace StardewModdingAPI.Web.Controllers { // fresh page if (string.IsNullOrWhiteSpace(id)) - return this.View("Index", new LogParserModel(this.Config.LogParserUrl, id)); + return this.View("Index", this.GetModel(id)); // log page PasteInfo paste = await this.GetAsync(id); ParsedLog log = paste.Success ? new LogParser().Parse(paste.Content) : new ParsedLog { IsValid = false, Error = "Pastebin error: " + paste.Error }; - return this.View("Index", new LogParserModel(this.Config.LogParserUrl, id, log, raw)); + return this.View("Index", this.GetModel(id).SetResult(log, raw)); } /*** @@ -80,7 +81,7 @@ namespace StardewModdingAPI.Web.Controllers // get raw log text string input = this.Request.Form["input"].FirstOrDefault(); if (string.IsNullOrWhiteSpace(input)) - return this.View("Index", new LogParserModel(this.Config.LogParserUrl, null) { UploadError = "The log file seems to be empty." }); + return this.View("Index", this.GetModel(null, uploadError: "The log file seems to be empty.")); // upload log input = this.GzipHelper.CompressString(input); @@ -88,7 +89,7 @@ namespace StardewModdingAPI.Web.Controllers // handle errors if (!result.Success) - return this.View("Index", new LogParserModel(this.Config.LogParserUrl, result.ID) { UploadError = $"Pastebin error: {result.Error ?? "unknown error"}" }); + return this.View("Index", this.GetModel(result.ID, uploadError: $"Pastebin error: {result.Error ?? "unknown error"}")); // redirect to view UriBuilder uri = new UriBuilder(new Uri(this.Config.LogParserUrl)); @@ -108,5 +109,39 @@ namespace StardewModdingAPI.Web.Controllers response.Content = this.GzipHelper.DecompressString(response.Content); return response; } + + /// Construct an instance. + /// The paste ID. + /// An error which occurred while uploading the log to Pastebin. + private LogParserModel GetModel(string pasteID, string uploadError = null) + { + string sectionUrl = this.Config.LogParserUrl; + Platform? platform = this.DetectClientPlatform(); + return new LogParserModel(sectionUrl, pasteID, platform) { UploadError = uploadError }; + } + + /// Detect the viewer's OS. + /// Returns the viewer OS if known, else null. + private Platform? DetectClientPlatform() + { + string userAgent = this.Request.Headers["User-Agent"]; + switch (userAgent) + { + case string ua when ua.Contains("Windows"): + return Platform.Windows; + + case string ua when ua.Contains("Android"): // check for Android before Linux because Android user agents also contain Linux + 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/ClientPlatformDetection/ClientPlatformMiddleware.cs b/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs deleted file mode 100644 index 2bf23ff9..00000000 --- a/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using StardewModdingAPI.Toolkit.Utilities; - -namespace StardewModdingAPI.Web.Framework.ClientPlatformDetection -{ - /// 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/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs b/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs deleted file mode 100644 index 274e5486..00000000 --- a/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Microsoft.AspNetCore.Builder; - -namespace StardewModdingAPI.Web.Framework.ClientPlatformDetection -{ - /// 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/Startup.cs b/src/SMAPI.Web/Startup.cs index e12fa63b..da5c1f1b 100644 --- a/src/SMAPI.Web/Startup.cs +++ b/src/SMAPI.Web/Startup.cs @@ -15,7 +15,6 @@ using StardewModdingAPI.Web.Framework; using StardewModdingAPI.Web.Framework.Caching; using StardewModdingAPI.Web.Framework.Caching.Mods; using StardewModdingAPI.Web.Framework.Caching.Wiki; -using StardewModdingAPI.Web.Framework.ClientPlatformDetection; using StardewModdingAPI.Web.Framework.Clients.Chucklefish; using StardewModdingAPI.Web.Framework.Clients.GitHub; using StardewModdingAPI.Web.Framework.Clients.ModDrop; @@ -173,7 +172,6 @@ namespace StardewModdingAPI.Web ) .UseRewriter(this.GetRedirectRules()) .UseStaticFiles() // wwwroot folder - .UseClientPlatform() .UseMvc(); // enable Hangfire dashboard diff --git a/src/SMAPI.Web/ViewModels/LogParserModel.cs b/src/SMAPI.Web/ViewModels/LogParserModel.cs index 25493a29..f4c5214b 100644 --- a/src/SMAPI.Web/ViewModels/LogParserModel.cs +++ b/src/SMAPI.Web/ViewModels/LogParserModel.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; +using StardewModdingAPI.Toolkit.Utilities; using StardewModdingAPI.Web.Framework.LogParsing.Models; namespace StardewModdingAPI.Web.ViewModels @@ -24,6 +25,9 @@ namespace StardewModdingAPI.Web.ViewModels /// The paste ID. public string PasteID { get; set; } + /// The viewer's detected OS, if known. + public Platform? DetectedPlatform { get; set; } + /// The parsed log info. public ParsedLog ParsedLog { get; set; } @@ -46,24 +50,25 @@ namespace StardewModdingAPI.Web.ViewModels /// Construct an instance. /// The root URL for the log parser controller. /// The paste ID. - public LogParserModel(string sectionUrl, string pasteID) + /// The viewer's detected OS, if known. + public LogParserModel(string sectionUrl, string pasteID, Platform? platform) { this.SectionUrl = sectionUrl; this.PasteID = pasteID; + this.DetectedPlatform = platform; this.ParsedLog = null; this.ShowRaw = false; } - /// Construct an instance. - /// The root URL for the log parser controller. - /// The paste ID. + /// Set the log parser result. /// The parsed log info. /// Whether to show the raw unparsed log. - public LogParserModel(string sectionUrl, string pasteID, ParsedLog parsedLog, bool showRaw) - : this(sectionUrl, pasteID) + public LogParserModel SetResult(ParsedLog parsedLog, bool showRaw) { this.ParsedLog = parsedLog; this.ShowRaw = showRaw; + + return this; } /// Get all content packs in the log grouped by the mod they're for. diff --git a/src/SMAPI.Web/Views/LogParser/Index.cshtml b/src/SMAPI.Web/Views/LogParser/Index.cshtml index 42887019..9a06d85a 100644 --- a/src/SMAPI.Web/Views/LogParser/Index.cshtml +++ b/src/SMAPI.Web/Views/LogParser/Index.cshtml @@ -1,6 +1,5 @@ @using Newtonsoft.Json @using StardewModdingAPI.Toolkit.Utilities -@using StardewModdingAPI.Web.Framework.ClientPlatformDetection @using StardewModdingAPI.Web.Framework.LogParsing.Models @model StardewModdingAPI.Web.ViewModels.LogParserModel @@ -69,13 +68,13 @@ else if (Model.ParsedLog?.IsValid == true)

Where do I find my SMAPI log?

What system do you use?
    - @{ - Platform? clientPlatform = Context.Items[ClientPlatformMiddleware.ClientPlatformKey] as Platform?; + @foreach (Platform platform in new[] { Platform.Android, Platform.Linux, Platform.Mac, Platform.Windows }) + { +
  • + + +
  • } -
  • -
  • -
  • -
On Android: -- cgit