Skip to content

Commit

Permalink
ConfigApp/Workshop: Query submissions file hash and compare to local …
Browse files Browse the repository at this point in the history
…hash before fetching submissions
  • Loading branch information
pongo1231 committed Jan 30, 2024
1 parent ff54810 commit 164c263
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions ConfigApp/Tabs/WorkshopTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.ObjectModel;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Windows;
using System.Windows.Controls;
Expand All @@ -19,6 +20,8 @@ namespace ConfigApp.Tabs
{
public class WorkshopTab : Tab
{
public const string SUBMISSIONS_CACHED_FILENAME = "workshop/submissions_cached.json.zst";

enum SortingMode
{
Name,
Expand Down Expand Up @@ -229,19 +232,36 @@ private async Task ForceRefreshWorkshopContentFromRemote()
HttpClient httpClient = new HttpClient();
try
{
var result = await httpClient.GetAsync($"{domain}/workshop/fetch_submissions");
if (!result.IsSuccessStatusCode)
if (File.Exists(SUBMISSIONS_CACHED_FILENAME))
{
var hashResult = await httpClient.GetAsync($"{domain}/workshop/fetch_submissionshash");
if (hashResult.IsSuccessStatusCode)
{
var remoteHash = await hashResult.Content.ReadAsStringAsync();
var localContent = File.ReadAllBytes(SUBMISSIONS_CACHED_FILENAME);
using var sha256 = SHA256.Create();
if (remoteHash.ToLower() == Convert.ToHexString(sha256.ComputeHash(localContent)).ToLower())
{
ParseWorkshopSubmissionsFile(localContent);

return;
}
}
}

var submissionsResult = await httpClient.GetAsync($"{domain}/workshop/fetch_submissions");
if (!submissionsResult.IsSuccessStatusCode)
{
MessageBox.Show("Remote server provided no master submissions file! Can not fetch available submissions.", "ChaosModV", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
var compressedResult = await result.Content.ReadAsByteArrayAsync();
var submissionsCompressedResult = await submissionsResult.Content.ReadAsByteArrayAsync();

ParseWorkshopSubmissionsFile(compressedResult);
ParseWorkshopSubmissionsFile(submissionsCompressedResult);

// Cache submissions
File.WriteAllBytes("workshop/submissions_cached.json.zst", compressedResult);
File.WriteAllBytes(SUBMISSIONS_CACHED_FILENAME, submissionsCompressedResult);
}
}
catch (HttpRequestException)
Expand Down Expand Up @@ -441,11 +461,11 @@ public async override void OnTabSelected()

byte[] fileContent = null;
// Use cached content if existing (and accessible), otherwise fall back to server request
if (File.Exists("workshop/submissions_cached.json.zst"))
if (File.Exists(SUBMISSIONS_CACHED_FILENAME))
{
try
{
fileContent = File.ReadAllBytes("workshop/submissions_cached.json.zst");
fileContent = File.ReadAllBytes(SUBMISSIONS_CACHED_FILENAME);
}
catch (IOException)
{
Expand Down

0 comments on commit 164c263

Please sign in to comment.