From d329d4f8bb6b31095e6423b563c7e4738dcceaa7 Mon Sep 17 00:00:00 2001 From: Kemal Setya Adhi Date: Fri, 24 May 2024 16:02:01 +0700 Subject: [PATCH] Fix retry mechanism on API loading not working --- .../Classes/Extension/TaskExtensions.cs | 5 +++-- .../Helper/LauncherApiLoader/ILauncherApi.cs | 2 +- .../Helper/LauncherApiLoader/LauncherApiBase.cs | 12 +++++++----- .../Classes/RegionManagement/RegionManagement.cs | 16 +++++++++------- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/CollapseLauncher/Classes/Extension/TaskExtensions.cs b/CollapseLauncher/Classes/Extension/TaskExtensions.cs index 66a01937f..83cee18d0 100644 --- a/CollapseLauncher/Classes/Extension/TaskExtensions.cs +++ b/CollapseLauncher/Classes/Extension/TaskExtensions.cs @@ -70,9 +70,10 @@ internal static class TaskExtensions } if (lastException is not null - && lastException is TaskCanceledException && !fromToken.IsCancellationRequested) - throw new TimeoutException($"The operation has timed out with inner exception!", lastException); + throw lastException is TaskCanceledException ? + new TimeoutException($"The operation has timed out with inner exception!", lastException) : + lastException; throw new TimeoutException($"The operation has timed out!"); } diff --git a/CollapseLauncher/Classes/Helper/LauncherApiLoader/ILauncherApi.cs b/CollapseLauncher/Classes/Helper/LauncherApiLoader/ILauncherApi.cs index 1372cc1bd..1ce803797 100644 --- a/CollapseLauncher/Classes/Helper/LauncherApiLoader/ILauncherApi.cs +++ b/CollapseLauncher/Classes/Helper/LauncherApiLoader/ILauncherApi.cs @@ -17,7 +17,7 @@ internal interface ILauncherApi string? GameRegionTranslation { get; } RegionResourceProp? LauncherGameResource { get; } LauncherGameNews? LauncherGameNews { get; } - Task LoadAsync(OnLoadAction? beforeLoadRoutine = null, OnLoadAction? afterLoadRoutine = null, + Task LoadAsync(OnLoadAction? beforeLoadRoutine = null, OnLoadAction? afterLoadRoutine = null, ActionOnTimeOutRetry? onTimeoutRoutine = null, ErrorLoadRoutineDelegate? errorLoadRoutine = null, CancellationToken token = default); } diff --git a/CollapseLauncher/Classes/Helper/LauncherApiLoader/LauncherApiBase.cs b/CollapseLauncher/Classes/Helper/LauncherApiLoader/LauncherApiBase.cs index 01a22ba96..9f4f2bd19 100644 --- a/CollapseLauncher/Classes/Helper/LauncherApiLoader/LauncherApiBase.cs +++ b/CollapseLauncher/Classes/Helper/LauncherApiLoader/LauncherApiBase.cs @@ -46,9 +46,9 @@ protected LauncherApiBase(PresetConfig presetConfig, string gameName, string gam GameRegion = gameRegion; } - public async Task LoadAsync(OnLoadAction? beforeLoadRoutine, OnLoadAction? afterLoadRoutine, - ActionOnTimeOutRetry? onTimeoutRoutine, - ErrorLoadRoutineDelegate? errorLoadRoutine, CancellationToken token) + public async Task LoadAsync(OnLoadAction? beforeLoadRoutine, OnLoadAction? afterLoadRoutine, + ActionOnTimeOutRetry? onTimeoutRoutine, ErrorLoadRoutineDelegate? errorLoadRoutine, + CancellationToken token) { beforeLoadRoutine?.Invoke(token); @@ -56,15 +56,17 @@ public async Task LoadAsync(OnLoadAction? beforeLoadRoutine, OnLoadA { IsLoadingCompleted = false; await LoadAsyncInner(onTimeoutRoutine, token); + afterLoadRoutine?.Invoke(token); + + return true; } catch (Exception ex) { errorLoadRoutine?.Invoke(ex); + return false; } finally { - afterLoadRoutine?.Invoke(token); - IsLoadingCompleted = true; } } diff --git a/CollapseLauncher/Classes/RegionManagement/RegionManagement.cs b/CollapseLauncher/Classes/RegionManagement/RegionManagement.cs index f10a862b7..c08a4bbc3 100644 --- a/CollapseLauncher/Classes/RegionManagement/RegionManagement.cs +++ b/CollapseLauncher/Classes/RegionManagement/RegionManagement.cs @@ -75,12 +75,20 @@ void AfterLoadRoutine(CancellationToken token) LoadingMessageHelper.HideLoadingFrame(); IsLoadRegionComplete = true; + + LogWriteLine($"Game: {regionToChangeName} has been completely initialized!", LogType.Scheme, true); + FinalizeLoadRegion(gameName, gameRegion); + ChangeBackgroundImageAsRegionAsync(); } void OnErrorRoutine(Exception ex) { + LoadingMessageHelper.HideActionButton(); + LoadingMessageHelper.HideLoadingFrame(); + LogWriteLine($"Error has occurred while loading: {regionToChangeName}!\r\n{ex}", LogType.Scheme, true); ErrorSender.SendExceptionWithoutPage(ex, ErrorType.Connection); + MainFrameChanger.ChangeWindowFrame(typeof(DisconnectedPage)); } async void CancelLoadEvent(object sender, RoutedEventArgs args) @@ -114,13 +122,7 @@ void ActionOnTimeOutRetry(int retryAttemptCount, int retryAttemptTotal, int time LoadingMessageHelper.ShowActionButton(Lang._Misc.Cancel, "", CancelLoadEvent); } - await preset.GameLauncherApi.LoadAsync(BeforeLoadRoutine, AfterLoadRoutine, ActionOnTimeOutRetry, OnErrorRoutine, tokenSource.Token); - - LogWriteLine($"Game: {regionToChangeName} has been completely initialized!", LogType.Scheme, true); - FinalizeLoadRegion(gameName, gameRegion); - ChangeBackgroundImageAsRegionAsync(); - - return true; + return await preset.GameLauncherApi.LoadAsync(BeforeLoadRoutine, AfterLoadRoutine, ActionOnTimeOutRetry, OnErrorRoutine, tokenSource.Token); } public void ClearMainPageState()