Skip to content

Commit

Permalink
Clean up ParseReplay, remove warnings (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
SaphireLattice authored Nov 16, 2024
1 parent bce888c commit 012ed05
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
3 changes: 2 additions & 1 deletion ReplayBrowser/Controllers/ReplayController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions ReplayBrowser/Pages/Shared/Layout/App.razor
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
@inject IConfiguration Configuration

@{
var plausibleSnippet = new MarkupString(Configuration["Plausible:Snippet"]);
}

<!DOCTYPE html>
<html lang="en" data-bs-theme="dark" data-enhance-nav="false">

Expand All @@ -19,9 +15,9 @@
<link rel="stylesheet" href="app.css"/>
<link rel="icon" type="image/png" href="favicon.png"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
@if(Configuration["Plausible:Enabled"] == "True")
@if(Configuration["Plausible:Enabled"] == "True" && Configuration["Plausible:Snippet"] is not null)
{
@plausibleSnippet
@(new MarkupString(Configuration["Plausible:Snippet"]!))
}

<script src="_framework/blazor.web.js" autostart="false"></script>
Expand Down
4 changes: 3 additions & 1 deletion ReplayBrowser/Pages/Shared/ReplayDisplay.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
</div>
<div class="card-body">
<dl class="replay-description">
<dt>Date</dt><dd>@ReplayData.Date</dd>
@if (ReplayData.Date is not null) {
<dt>Date</dt><dd>@(DateTime.SpecifyKind((DateTime) ReplayData.Date, DateTimeKind.Utc))</dd>
}
<dt>Gamemode</dt><dd>@ReplayData.Gamemode</dd>
@if (ReplayData.Map == null)
{
Expand Down
2 changes: 1 addition & 1 deletion ReplayBrowser/Services/LeaderboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class LeaderboardService : IHostedService, IDisposable
private readonly AccountService _accountService;
private readonly IConfiguration _configuration;

private List<Guid> RedactedAccounts;
private List<Guid> RedactedAccounts = [];

public LeaderboardService(IMemoryCache cache, Ss14ApiHelper apiHelper, IServiceScopeFactory factory, AccountService accountService, IConfiguration configuration)
{
Expand Down
24 changes: 17 additions & 7 deletions ReplayBrowser/Services/ReplayParser/ReplayParserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -277,10 +281,10 @@ private async Task ConsumeQueue(CancellationToken token)
/// <summary>
/// Parses a replay file and returns a Replay object.
/// </summary>
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)
Expand All @@ -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()
Expand All @@ -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<StorageUrl[]>()!;
Expand Down

0 comments on commit 012ed05

Please sign in to comment.