From 012ed052e77fd219af0223f5030e32963dfa44d5 Mon Sep 17 00:00:00 2001 From: Saphire Lattice Date: Sat, 16 Nov 2024 08:05:46 +0700 Subject: [PATCH] Clean up ParseReplay, remove warnings (#69) --- ReplayBrowser/Controllers/ReplayController.cs | 3 ++- ReplayBrowser/Pages/Shared/Layout/App.razor | 8 ++----- .../Pages/Shared/ReplayDisplay.razor | 4 +++- ReplayBrowser/Services/LeaderboardService.cs | 2 +- .../ReplayParser/ReplayParserService.cs | 24 +++++++++++++------ 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/ReplayBrowser/Controllers/ReplayController.cs b/ReplayBrowser/Controllers/ReplayController.cs index 03063c6..7248f50 100644 --- a/ReplayBrowser/Controllers/ReplayController.cs +++ b/ReplayBrowser/Controllers/ReplayController.cs @@ -247,7 +247,8 @@ IFormCollection form Replay? replay = null; try { - replay = _replayParserService.FinalizeReplayParse(reader, null); + var replayYaml = _replayParserService.ParseReplay(reader); + replay = _replayParserService.ParseReplayYaml(replayYaml, null); var replayFileName = Path.GetFileName(replay.Link); var storageUrl = _replayParserService.GetStorageUrlFromReplayLink(replay.Link); var match = storageUrl.ReplayRegexCompiled.Match(replayFileName); diff --git a/ReplayBrowser/Pages/Shared/Layout/App.razor b/ReplayBrowser/Pages/Shared/Layout/App.razor index fdbe22d..d67679d 100644 --- a/ReplayBrowser/Pages/Shared/Layout/App.razor +++ b/ReplayBrowser/Pages/Shared/Layout/App.razor @@ -1,9 +1,5 @@ @inject IConfiguration Configuration -@{ - var plausibleSnippet = new MarkupString(Configuration["Plausible:Snippet"]); -} - @@ -19,9 +15,9 @@ - @if(Configuration["Plausible:Enabled"] == "True") + @if(Configuration["Plausible:Enabled"] == "True" && Configuration["Plausible:Snippet"] is not null) { - @plausibleSnippet + @(new MarkupString(Configuration["Plausible:Snippet"]!)) } diff --git a/ReplayBrowser/Pages/Shared/ReplayDisplay.razor b/ReplayBrowser/Pages/Shared/ReplayDisplay.razor index 9872ae9..b0f3692 100644 --- a/ReplayBrowser/Pages/Shared/ReplayDisplay.razor +++ b/ReplayBrowser/Pages/Shared/ReplayDisplay.razor @@ -17,7 +17,9 @@
-
Date
@ReplayData.Date
+ @if (ReplayData.Date is not null) { +
Date
@(DateTime.SpecifyKind((DateTime) ReplayData.Date, DateTimeKind.Utc))
+ }
Gamemode
@ReplayData.Gamemode
@if (ReplayData.Map == null) { diff --git a/ReplayBrowser/Services/LeaderboardService.cs b/ReplayBrowser/Services/LeaderboardService.cs index c36a26f..dd635c9 100644 --- a/ReplayBrowser/Services/LeaderboardService.cs +++ b/ReplayBrowser/Services/LeaderboardService.cs @@ -28,7 +28,7 @@ public class LeaderboardService : IHostedService, IDisposable private readonly AccountService _accountService; private readonly IConfiguration _configuration; - private List RedactedAccounts; + private List RedactedAccounts = []; public LeaderboardService(IMemoryCache cache, Ss14ApiHelper apiHelper, IServiceScopeFactory factory, AccountService accountService, IConfiguration configuration) { diff --git a/ReplayBrowser/Services/ReplayParser/ReplayParserService.cs b/ReplayBrowser/Services/ReplayParser/ReplayParserService.cs index eb24b07..b620fcf 100644 --- a/ReplayBrowser/Services/ReplayParser/ReplayParserService.cs +++ b/ReplayBrowser/Services/ReplayParser/ReplayParserService.cs @@ -171,11 +171,12 @@ private async Task ConsumeQueue(CancellationToken token) Log.Information("Downloading " + replay); var fileSize = await client.GetFileSizeAsync(replay); // Check if the server supports range requests. - var supportsRange = (await client.SupportsRangeRequests(replay) && fileSize != -1); + var supportsRange = await client.SupportsRangeRequests(replay) && fileSize != -1; Replay? parsedReplay = null; try { + YamlReplay? yamlReplay = null; if (supportsRange) { try @@ -185,7 +186,7 @@ private async Task ConsumeQueue(CancellationToken token) var extractedFiles = await ZipDownloader.ExtractFilesFromZipAsync(replay, files); completed++; Details = $"{completed}/{total}"; - parsedReplay = FinalizeReplayParse(new StreamReader(extractedFiles["_replay/replay_final.yml"]), replay); + yamlReplay = ParseReplay(new StreamReader(extractedFiles["_replay/replay_final.yml"])); } catch (Exception e) { @@ -200,8 +201,11 @@ private async Task ConsumeQueue(CancellationToken token) var stream = await client.GetStreamAsync(replay, progress, token); completed++; Details = $"{completed}/{total}"; - parsedReplay = ParseReplay(stream, replay); + yamlReplay = ParseReplay(stream); } + + // This shouldn't ever be null in reality, as it will either be assigned in the try, or in the second if that runs on catch + parsedReplay = ParseReplayYaml(yamlReplay!, replay); } catch (Exception e) { @@ -277,10 +281,10 @@ private async Task ConsumeQueue(CancellationToken token) /// /// Parses a replay file and returns a Replay object. /// - private Replay ParseReplay(Stream stream, string replayLink) + private YamlReplay ParseReplay(Stream zipStream) { // Read the replay file and unzip it. - var zipArchive = new ZipArchive(stream, ZipArchiveMode.Read); + var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Read); var replayFile = zipArchive.GetEntry("_replay/replay_final.yml"); if (replayFile == null) @@ -291,10 +295,10 @@ private Replay ParseReplay(Stream stream, string replayLink) var replayStream = replayFile.Open(); var reader = new StreamReader(replayStream); - return FinalizeReplayParse(reader, replayLink); + return ParseReplay(reader); } - public Replay FinalizeReplayParse(TextReader stream, string? replayLink) + public YamlReplay ParseReplay(TextReader stream) { var deserializer = new DeserializerBuilder() .IgnoreUnmatchedProperties() @@ -307,6 +311,12 @@ public Replay FinalizeReplayParse(TextReader stream, string? replayLink) throw new Exception("Replay is not valid."); } + return yamlReplay; + } + + public Replay ParseReplayYaml(YamlReplay yamlReplay, string? replayLink) + { + var replay = Replay.FromYaml(yamlReplay, replayLink); var replayUrls = _configuration.GetSection("ReplayUrls").Get()!;