From 61f027b57e20ca6f040e6992c0adf210c620c1c4 Mon Sep 17 00:00:00 2001 From: jvyden Date: Tue, 2 Nov 2021 20:35:21 -0400 Subject: [PATCH] Cleanup code and formatting --- ProjectLighthouse.Tests/LighthouseTest.cs | 6 +++--- .../Controllers/MessageController.cs | 9 +-------- .../Controllers/ScoreController.cs | 2 +- .../Controllers/SlotsController.cs | 8 ++++---- ProjectLighthouse/Controllers/UserController.cs | 17 +++++++++++------ ProjectLighthouse/Helpers/FileHelper.cs | 5 ++--- ProjectLighthouse/Helpers/GameVersionHelper.cs | 6 ++---- ProjectLighthouse/ProjectLighthouse.csproj | 14 +++++++------- .../Serialization/LbpSerializer.cs | 2 +- ProjectLighthouse/Startup.cs | 6 ++---- .../Types/Match/UpdatePlayersInRoom.cs | 2 ++ ProjectLighthouse/Types/Profiles/Pins.cs | 6 ++++-- ProjectLighthouse/Types/Score.cs | 5 ++--- 13 files changed, 42 insertions(+), 46 deletions(-) diff --git a/ProjectLighthouse.Tests/LighthouseTest.cs b/ProjectLighthouse.Tests/LighthouseTest.cs index e3966fe1..5f88d8c4 100644 --- a/ProjectLighthouse.Tests/LighthouseTest.cs +++ b/ProjectLighthouse.Tests/LighthouseTest.cs @@ -47,7 +47,7 @@ namespace LBPUnion.ProjectLighthouse.Tests public Task AuthenticatedRequest(string endpoint, string mmAuth, HttpMethod method) { - using HttpRequestMessage? requestMessage = new(method, endpoint); + using HttpRequestMessage requestMessage = new(method, endpoint); requestMessage.Headers.Add("Cookie", mmAuth); return this.Client.SendAsync(requestMessage); @@ -61,7 +61,7 @@ namespace LBPUnion.ProjectLighthouse.Tests public async Task AuthenticatedUploadFileRequest(string endpoint, string filePath, string mmAuth) { - using HttpRequestMessage? requestMessage = new(HttpMethod.Post, endpoint); + using HttpRequestMessage requestMessage = new(HttpMethod.Post, endpoint); requestMessage.Headers.Add("Cookie", mmAuth); requestMessage.Content = new StringContent(await File.ReadAllTextAsync(filePath)); return await this.Client.SendAsync(requestMessage); @@ -69,7 +69,7 @@ namespace LBPUnion.ProjectLighthouse.Tests public async Task AuthenticatedUploadDataRequest(string endpoint, byte[] data, string mmAuth) { - using HttpRequestMessage? requestMessage = new(HttpMethod.Post, endpoint); + using HttpRequestMessage requestMessage = new(HttpMethod.Post, endpoint); requestMessage.Headers.Add("Cookie", mmAuth); requestMessage.Content = new ByteArrayContent(data); return await this.Client.SendAsync(requestMessage); diff --git a/ProjectLighthouse/Controllers/MessageController.cs b/ProjectLighthouse/Controllers/MessageController.cs index de94e0a8..26ec9d3e 100644 --- a/ProjectLighthouse/Controllers/MessageController.cs +++ b/ProjectLighthouse/Controllers/MessageController.cs @@ -24,14 +24,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers public async Task Eula() { User user = await this.database.UserFromRequest(this.Request); - return user == null - ? this.StatusCode(403, "") - : this.Ok - ( - EulaHelper.PrivateInstanceNoticeOrBlank + - "\n" + - $"{EulaHelper.License}\n" - ); + return user == null ? this.StatusCode(403, "") : this.Ok(EulaHelper.PrivateInstanceNoticeOrBlank + "\n" + $"{EulaHelper.License}\n"); } [HttpGet("announce")] diff --git a/ProjectLighthouse/Controllers/ScoreController.cs b/ProjectLighthouse/Controllers/ScoreController.cs index 21baa5d4..287be62c 100644 --- a/ProjectLighthouse/Controllers/ScoreController.cs +++ b/ProjectLighthouse/Controllers/ScoreController.cs @@ -12,7 +12,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers public class ScoreController : ControllerBase { private readonly Database database; - + public ScoreController(Database database) { this.database = database; diff --git a/ProjectLighthouse/Controllers/SlotsController.cs b/ProjectLighthouse/Controllers/SlotsController.cs index ac319fef..fd203a65 100644 --- a/ProjectLighthouse/Controllers/SlotsController.cs +++ b/ProjectLighthouse/Controllers/SlotsController.cs @@ -55,19 +55,19 @@ namespace LBPUnion.ProjectLighthouse.Controllers return this.Ok(LbpSerializer.TaggedStringElement("slots", response, "hint_start", pageStart + Math.Min(pageSize, 30))); } - + [HttpGet("slots/mmpicks")] public IActionResult TeamPickedSlots([FromQuery] int pageStart, [FromQuery] int pageSize) { - IQueryable slots = this.database.Slots - .Where(s => s.TeamPick) + IQueryable slots = this.database.Slots.Where + (s => s.TeamPick) .Include(s => s.Creator) .Include(s => s.Location) .OrderByDescending(s => s.LastUpdated) .Skip(pageStart - 1) .Take(Math.Min(pageSize, 30)); string response = Enumerable.Aggregate(slots, string.Empty, (current, slot) => current + slot.Serialize()); - + return this.Ok(LbpSerializer.TaggedStringElement("slots", response, "hint_start", pageStart + Math.Min(pageSize, 30))); } } diff --git a/ProjectLighthouse/Controllers/UserController.cs b/ProjectLighthouse/Controllers/UserController.cs index 1dc7ecb0..3f3a1e4b 100644 --- a/ProjectLighthouse/Controllers/UserController.cs +++ b/ProjectLighthouse/Controllers/UserController.cs @@ -1,8 +1,9 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; -using System.Threading.Tasks; using System.Text.Json; +using System.Threading.Tasks; using System.Xml; using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types.Profiles; @@ -137,17 +138,21 @@ namespace LBPUnion.ProjectLighthouse.Controllers User user = await this.database.UserFromRequest(this.Request); if (user == null) return this.StatusCode(403, ""); - string pinsString = await new System.IO.StreamReader(this.Request.Body).ReadToEndAsync(); + string pinsString = await new StreamReader(this.Request.Body).ReadToEndAsync(); Pins pinJson = JsonSerializer.Deserialize(pinsString); + if (pinJson == null) return this.BadRequest(); + // Sometimes the update gets called periodically as pin progress updates via playing, // may not affect equipped profile pins however, so check before setting it. string currentPins = user.Pins; string newPins = string.Join(",", pinJson.ProfilePins); - if (!String.Equals(currentPins,newPins)) { - user.Pins = newPins; - await this.database.SaveChangesAsync(); - } + + if (string.Equals(currentPins, newPins)) return this.Ok("[{\"StatusCode\":200}]"); + + user.Pins = newPins; + await this.database.SaveChangesAsync(); + return this.Ok("[{\"StatusCode\":200}]"); } } diff --git a/ProjectLighthouse/Helpers/FileHelper.cs b/ProjectLighthouse/Helpers/FileHelper.cs index e81fe118..62d9f06b 100644 --- a/ProjectLighthouse/Helpers/FileHelper.cs +++ b/ProjectLighthouse/Helpers/FileHelper.cs @@ -38,7 +38,7 @@ namespace LBPUnion.ProjectLighthouse.Helpers public static LbpFileType DetermineFileType(byte[] data) { if (data.Length == 0) return LbpFileType.Unknown; // Can't be anything if theres no data. - + using MemoryStream ms = new(data); using BinaryReader reader = new(ms); @@ -69,8 +69,7 @@ namespace LBPUnion.ProjectLighthouse.Helpers // Determine if file is JPEG byte[] header = reader.ReadBytes(9); - if (header[0] == 0xFF && header[1] == 0xD8 && header[2] == 0xFF && header[3] == 0xE0) - return LbpFileType.Jpeg; + if (header[0] == 0xFF && header[1] == 0xD8 && header[2] == 0xFF && header[3] == 0xE0) return LbpFileType.Jpeg; return LbpFileType.Unknown; // Still unknown. } diff --git a/ProjectLighthouse/Helpers/GameVersionHelper.cs b/ProjectLighthouse/Helpers/GameVersionHelper.cs index 161a3831..023e4f59 100644 --- a/ProjectLighthouse/Helpers/GameVersionHelper.cs +++ b/ProjectLighthouse/Helpers/GameVersionHelper.cs @@ -1,5 +1,3 @@ -using System; -using System.Collections.Generic; using System.Linq; using LBPUnion.ProjectLighthouse.Types; @@ -77,13 +75,13 @@ namespace LBPUnion.ProjectLighthouse.Helpers "CUSA01077", "CUSA01304", }; - + public GameVersion FromTitleId(string titleId) { if (this.LittleBigPlanet1TitleIds.Contains(titleId)) return GameVersion.LittleBigPlanet1; if (this.LittleBigPlanet2TitleIds.Contains(titleId)) return GameVersion.LittleBigPlanet2; if (this.LittleBigPlanet3TitleIds.Contains(titleId)) return GameVersion.LittleBigPlanet3; - + return GameVersion.Unknown; } } diff --git a/ProjectLighthouse/ProjectLighthouse.csproj b/ProjectLighthouse/ProjectLighthouse.csproj index a18c281d..3a7ade23 100644 --- a/ProjectLighthouse/ProjectLighthouse.csproj +++ b/ProjectLighthouse/ProjectLighthouse.csproj @@ -8,23 +8,23 @@ - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + - + - + diff --git a/ProjectLighthouse/Serialization/LbpSerializer.cs b/ProjectLighthouse/Serialization/LbpSerializer.cs index d1dcfdd9..9bec4cb2 100644 --- a/ProjectLighthouse/Serialization/LbpSerializer.cs +++ b/ProjectLighthouse/Serialization/LbpSerializer.cs @@ -17,7 +17,7 @@ namespace LBPUnion.ProjectLighthouse.Serialization public static string StringElement(KeyValuePair pair) => $"<{pair.Key}>{pair.Value}"; public static string StringElement(string key, bool value) => $"<{key}>{value.ToString().ToLower()}"; - + public static string StringElement(string key, object value) => $"<{key}>{value}"; public static string TaggedStringElement diff --git a/ProjectLighthouse/Startup.cs b/ProjectLighthouse/Startup.cs index 34a9b9fc..63befbb2 100644 --- a/ProjectLighthouse/Startup.cs +++ b/ProjectLighthouse/Startup.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Diagnostics; using System.IO; using Kettu; @@ -68,8 +67,7 @@ namespace LBPUnion.ProjectLighthouse context.Request.EnableBuffering(); // Allows us to reset the position of Request.Body for later logging // Client digest check. - string authCookie; - if (!context.Request.Cookies.TryGetValue("MM_AUTH", out authCookie)) authCookie = string.Empty; + if (!context.Request.Cookies.TryGetValue("MM_AUTH", out string authCookie)) authCookie = string.Empty; string digestPath = context.Request.Path; Stream body = context.Request.Body; @@ -91,7 +89,7 @@ namespace LBPUnion.ProjectLighthouse } // This does the same as above, but for the response stream. - using MemoryStream responseBuffer = new(); + await using MemoryStream responseBuffer = new(); Stream oldResponseStream = context.Response.Body; context.Response.Body = responseBuffer; diff --git a/ProjectLighthouse/Types/Match/UpdatePlayersInRoom.cs b/ProjectLighthouse/Types/Match/UpdatePlayersInRoom.cs index a7752ec2..a9f6e1f3 100644 --- a/ProjectLighthouse/Types/Match/UpdatePlayersInRoom.cs +++ b/ProjectLighthouse/Types/Match/UpdatePlayersInRoom.cs @@ -1,7 +1,9 @@ using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace LBPUnion.ProjectLighthouse.Types.Match { + [SuppressMessage("ReSharper", "CollectionNeverUpdated.Global")] public class UpdatePlayersInRoom : IMatchData { public List Players; diff --git a/ProjectLighthouse/Types/Profiles/Pins.cs b/ProjectLighthouse/Types/Profiles/Pins.cs index 101ebf94..567e739e 100644 --- a/ProjectLighthouse/Types/Profiles/Pins.cs +++ b/ProjectLighthouse/Types/Profiles/Pins.cs @@ -1,14 +1,16 @@ using System.Text.Json.Serialization; + namespace LBPUnion.ProjectLighthouse.Types.Profiles { public class Pins { [JsonPropertyName("progress")] public long[] Progress { get; set; } + [JsonPropertyName("awards")] public long[] Awards { get; set; } + [JsonPropertyName("profile_pins")] public long[] ProfilePins { get; set; } } -} - +} \ No newline at end of file diff --git a/ProjectLighthouse/Types/Score.cs b/ProjectLighthouse/Types/Score.cs index 6ee0df73..14668872 100644 --- a/ProjectLighthouse/Types/Score.cs +++ b/ProjectLighthouse/Types/Score.cs @@ -12,14 +12,14 @@ namespace LBPUnion.ProjectLighthouse.Types [XmlIgnore] [Key] public int ScoreId { get; set; } - + [XmlIgnore] public int SlotId { get; set; } [XmlIgnore] [ForeignKey(nameof(SlotId))] public Slot Slot { get; set; } - + [XmlElement("type")] public int Type { get; set; } @@ -33,7 +33,6 @@ namespace LBPUnion.ProjectLighthouse.Types set => this.PlayerIdCollection = string.Join(',', value); } - [XmlElement("score")] public int Points { get; set; } }