From f11d663df73f68350820dfa65aa51a8a9b9ffd0f Mon Sep 17 00:00:00 2001 From: AxesP <24486155+AxesP@users.noreply.github.com> Date: Wed, 27 Dec 2023 22:43:17 -0300 Subject: [PATCH] Local Amiibo.json should be used if connection failed (#3681) * Local Amiibo.json should be used if connection failed Currently Ryujinx is not loading any Amiibo if connection fails, even if the Amiibo.json exists. This fix will use the local file and show a Dialog if connection fails. * using local Amiibo.json & fixed Amiibo.json date comparison Using local Amiibo.json when connection fails and comparison without milliseconds for LastModified that comes from https://amiibo.ryujinx.org/ and the local one (The JSON file has milliseconds on it, those will cause an error when comparing the date from the header because the header one doesn't has milliseconds on it). Both changes made for Avalonia UI. * Fixed date comparison Same date comparison fix, but made for normal UI (Not for AvaloniaUI). This error can be prevented if the file in https://amiibo.ryujinx.org/ did not have the date with milliseconds. * Securely trying to get a list of Amiibo (For normal UI) * Securely trying to get a list of Amiibo (Change for AvaloniaUI) * Date comparison reverted * Apply suggestions from code review * Use fallback amiibo.json if remote file is not valid (Normal UI) * Use fallback amiibo.json if remote file is not valid (Avalonia UI) * Code styles corrected. * Code styles corrected in AmiiboWindowViewModel. * Readded Ryujinx.Common.Logging using. * Fixed using order. --------- Co-authored-by: Ac_K --- .../UI/ViewModels/AmiiboWindowViewModel.cs | 78 ++++++++++-------- src/Ryujinx/Ui/Windows/AmiiboWindow.cs | 81 +++++++++++-------- 2 files changed, 93 insertions(+), 66 deletions(-) diff --git a/src/Ryujinx.Ava/UI/ViewModels/AmiiboWindowViewModel.cs b/src/Ryujinx.Ava/UI/ViewModels/AmiiboWindowViewModel.cs index 83624f5f3..c41e8bcc2 100644 --- a/src/Ryujinx.Ava/UI/ViewModels/AmiiboWindowViewModel.cs +++ b/src/Ryujinx.Ava/UI/ViewModels/AmiiboWindowViewModel.cs @@ -188,35 +188,61 @@ namespace Ryujinx.Ava.UI.ViewModels _httpClient.Dispose(); } - private async Task LoadContentAsync() + private bool TryGetAmiiboJson(string json, out AmiiboJson amiiboJson) { - string amiiboJsonString = DefaultJson; - - if (File.Exists(_amiiboJsonPath)) + try { - amiiboJsonString = await File.ReadAllTextAsync(_amiiboJsonPath); + amiiboJson = JsonHelper.Deserialize(json, _serializerContext.AmiiboJson); - if (await NeedsUpdate(JsonHelper.Deserialize(amiiboJsonString, _serializerContext.AmiiboJson).LastUpdated)) + return true; + } + catch + { + amiiboJson = JsonHelper.Deserialize(DefaultJson, _serializerContext.AmiiboJson); + + return false; + } + } + + private async Task GetMostRecentAmiiboListOrDefaultJson() + { + bool localIsValid = false; + bool remoteIsValid = false; + AmiiboJson amiiboJson = JsonHelper.Deserialize(DefaultJson, _serializerContext.AmiiboJson); + + try + { + localIsValid = TryGetAmiiboJson(File.ReadAllText(_amiiboJsonPath), out amiiboJson); + + if (!localIsValid || await NeedsUpdate(amiiboJson.LastUpdated)) { - amiiboJsonString = await DownloadAmiiboJson(); + remoteIsValid = TryGetAmiiboJson(await DownloadAmiiboJson(), out amiiboJson); } } - else + catch { - try + if (!(localIsValid || remoteIsValid)) { - amiiboJsonString = await DownloadAmiiboJson(); + // Neither local or remote files are valid JSON, close window. + ShowInfoDialog(); + Close(); } - catch (Exception ex) + else if (!remoteIsValid) { - Logger.Error?.Print(LogClass.Application, $"Failed to download amiibo data: {ex}"); - + // Only the local file is valid, the local one should be used + // but the user should be warned. ShowInfoDialog(); } } - _amiiboList = JsonHelper.Deserialize(amiiboJsonString, _serializerContext.AmiiboJson).Amiibo; - _amiiboList = _amiiboList.OrderBy(amiibo => amiibo.AmiiboSeries).ToList(); + return amiiboJson; + } + + private async Task LoadContentAsync() + { + AmiiboJson amiiboJson = await GetMostRecentAmiiboListOrDefaultJson(); + + _amiiboList = amiiboJson.Amiibo.OrderBy(amiibo => amiibo.AmiiboSeries).ToList(); ParseAmiiboData(); } @@ -362,26 +388,14 @@ namespace Ryujinx.Ava.UI.ViewModels private async Task NeedsUpdate(DateTime oldLastModified) { - try + HttpResponseMessage response = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, "https://amiibo.ryujinx.org/")); + + if (response.IsSuccessStatusCode) { - HttpResponseMessage response = - await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, "https://amiibo.ryujinx.org/")); - - if (response.IsSuccessStatusCode) - { - return response.Content.Headers.LastModified != new DateTimeOffset(oldLastModified.Ticks - (oldLastModified.Ticks % TimeSpan.TicksPerSecond), TimeSpan.Zero); - } - - return false; + return response.Content.Headers.LastModified != oldLastModified; } - catch (Exception ex) - { - Logger.Error?.Print(LogClass.Application, $"Failed to check for amiibo updates: {ex}"); - ShowInfoDialog(); - - return false; - } + return false; } private async Task DownloadAmiiboJson() diff --git a/src/Ryujinx/Ui/Windows/AmiiboWindow.cs b/src/Ryujinx/Ui/Windows/AmiiboWindow.cs index f9e4da59d..2673f9121 100644 --- a/src/Ryujinx/Ui/Windows/AmiiboWindow.cs +++ b/src/Ryujinx/Ui/Windows/AmiiboWindow.cs @@ -72,37 +72,61 @@ namespace Ryujinx.Ui.Windows _ = LoadContentAsync(); } - private async Task LoadContentAsync() + private bool TryGetAmiiboJson(string json, out AmiiboJson amiiboJson) { - string amiiboJsonString = DefaultJson; - - if (File.Exists(_amiiboJsonPath)) + try { - amiiboJsonString = await File.ReadAllTextAsync(_amiiboJsonPath); + amiiboJson = JsonHelper.Deserialize(json, _serializerContext.AmiiboJson); - if (await NeedsUpdate(JsonHelper.Deserialize(amiiboJsonString, _serializerContext.AmiiboJson).LastUpdated)) + return true; + } + catch + { + amiiboJson = JsonHelper.Deserialize(DefaultJson, _serializerContext.AmiiboJson); + + return false; + } + } + + private async Task GetMostRecentAmiiboListOrDefaultJson() + { + bool localIsValid = false; + bool remoteIsValid = false; + AmiiboJson amiiboJson = JsonHelper.Deserialize(DefaultJson, _serializerContext.AmiiboJson); + + try + { + localIsValid = TryGetAmiiboJson(File.ReadAllText(_amiiboJsonPath), out amiiboJson); + + if (!localIsValid || await NeedsUpdate(amiiboJson.LastUpdated)) { - amiiboJsonString = await DownloadAmiiboJson(); + remoteIsValid = TryGetAmiiboJson(await DownloadAmiiboJson(), out amiiboJson); } } - else + catch { - try + if (!(localIsValid || remoteIsValid)) { - amiiboJsonString = await DownloadAmiiboJson(); - } - catch (Exception ex) - { - Logger.Error?.Print(LogClass.Application, $"Failed to download amiibo data: {ex}"); - + // Neither local or remote files are valid JSON, close window. ShowInfoDialog(); - Close(); } + else if (!remoteIsValid) + { + // Only the local file is valid, the local one should be used + // but the user should be warned. + ShowInfoDialog(); + } } - _amiiboList = JsonHelper.Deserialize(amiiboJsonString, _serializerContext.AmiiboJson).Amiibo; - _amiiboList = _amiiboList.OrderBy(amiibo => amiibo.AmiiboSeries).ToList(); + return amiiboJson; + } + + private async Task LoadContentAsync() + { + AmiiboJson amiiboJson = await GetMostRecentAmiiboListOrDefaultJson(); + + _amiiboList = amiiboJson.Amiibo.OrderBy(amiibo => amiibo.AmiiboSeries).ToList(); if (LastScannedAmiiboShowAll) { @@ -172,25 +196,14 @@ namespace Ryujinx.Ui.Windows private async Task NeedsUpdate(DateTime oldLastModified) { - try + HttpResponseMessage response = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, "https://amiibo.ryujinx.org/")); + + if (response.IsSuccessStatusCode) { - HttpResponseMessage response = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, "https://amiibo.ryujinx.org/")); - - if (response.IsSuccessStatusCode) - { - return response.Content.Headers.LastModified != new DateTimeOffset(oldLastModified.Ticks - (oldLastModified.Ticks % TimeSpan.TicksPerSecond), TimeSpan.Zero); - } - - return false; + return response.Content.Headers.LastModified != oldLastModified; } - catch (Exception ex) - { - Logger.Error?.Print(LogClass.Application, $"Failed to check for amiibo updates: {ex}"); - ShowInfoDialog(); - - return false; - } + return false; } private async Task DownloadAmiiboJson()