From 2bb13d950cf440c2fdbd7d820a50d29ce9fea5ff Mon Sep 17 00:00:00 2001 From: Bagus Nur Listiyono Date: Tue, 19 Nov 2024 08:06:34 +0700 Subject: [PATCH] Add missing DLL/Dependency error message for Database Handler This way, user will be asked to download and install the missing VCRedist #607 --- .../Classes/Helper/Database/DBHandler.cs | 6 +++ .../XAMLs/MainApp/Pages/SettingsPage.xaml.cs | 44 ++++++++++++++++--- Hi3Helper.Core/Lang/Locale/LangMisc.cs | 8 ++-- Hi3Helper.Core/Lang/en_US.json | 5 ++- 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/CollapseLauncher/Classes/Helper/Database/DBHandler.cs b/CollapseLauncher/Classes/Helper/Database/DBHandler.cs index 0ece4c24e..dbee3ad10 100644 --- a/CollapseLauncher/Classes/Helper/Database/DBHandler.cs +++ b/CollapseLauncher/Classes/Helper/Database/DBHandler.cs @@ -160,6 +160,12 @@ public static async Task Init(bool redirectThrow = false, bool bypassEnableFlag } else LogWriteLine("[DbHandler::Init] Reinitializing database system..."); } + catch (DllNotFoundException e) + { + LogWriteLine("[DbHandler::Init] Error when connecting to database system! Probably missing Visual C/C++ redist!\r\n" + e, + LogType.Error, true); + if (redirectThrow) throw; + } // No need to handle all these error catcher with sentry // The error should be handled in the method caller instead catch (LibsqlException e) when (e.Message.Contains("`api error: `{\"error\":\"Unauthorized: `The JWT is invalid`\"}``", diff --git a/CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml.cs b/CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml.cs index e20d1953b..8a13084fe 100644 --- a/CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml.cs +++ b/CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml.cs @@ -1,6 +1,7 @@ #if !DISABLEDISCORD using CollapseLauncher.DiscordPresence; #endif + using CollapseLauncher.CustomControls; using CollapseLauncher.Dialogs; using CollapseLauncher.Extension; using CollapseLauncher.Helper; @@ -1493,18 +1494,20 @@ private async void ValidateAndSaveDbButton_Click(object sender, RoutedEventArgs { // Show checking bar status ShowChecking(); - + // Set the value from prop DbHandler.Uri = _dbUrl; DbHandler.Token = _dbToken; DbHandler.UserId = _dbUserId; - + var r = Random.Shared.Next(100); // Generate random int for data verification await DbHandler.Init(true, true); // Initialize database await DbHandler.StoreKeyValue("TestKey", r.ToString(), true); // Store random number in TestKey - if (Convert.ToInt32(await DbHandler.QueryKey("TestKey", true)) != r) // Query key and check if value is correct - throw new InvalidDataException("Data validation failed!"); // Throw if value does not match (then catch), unlikely but maybe for really unstable db server + if (Convert.ToInt32(await DbHandler.QueryKey("TestKey", true)) != + r) // Query key and check if value is correct + throw + new InvalidDataException("Data validation failed!"); // Throw if value does not match (then catch), unlikely but maybe for really unstable db server // Show success bar status ShowSuccess(); @@ -1516,9 +1519,40 @@ await SpawnDialog( null, null, ContentDialogButton.Close, - CustomControls.ContentDialogTheme.Success + ContentDialogTheme.Success ); // Show success dialog } + catch (DllNotFoundException ex) + { + // No need to revert the value if fail, user is asked to restart the app + ShowFailed(ex); + var res = await SpawnDialog( + Lang._Misc.MissingVcRedist, + Lang._Misc.MissingVcRedistSubtitle, + sender as UIElement, + Lang._Misc.Close, + Lang._Misc.Yes, + null, + ContentDialogButton.Primary, + ContentDialogTheme.Error); + if (res == ContentDialogResult.Primary) + { + await Task.Run(() => + { + var uri = + "https://github.com/abbodi1406/vcredist/releases/latest/download/VisualCppRedist_AIO_x86_x64.exe"; + ProcessStartInfo psi = new ProcessStartInfo + { + FileName = "explorer.exe", + Arguments = uri, + UseShellExecute = true, + Verb = "runas" + }; + Process.Start(psi); + }); + } + else { await SentryHelper.ExceptionHandlerAsync(ex); } + } catch (Exception ex) { // Revert value if fail diff --git a/Hi3Helper.Core/Lang/Locale/LangMisc.cs b/Hi3Helper.Core/Lang/Locale/LangMisc.cs index 4b030fb5d..1f888b1f6 100644 --- a/Hi3Helper.Core/Lang/Locale/LangMisc.cs +++ b/Hi3Helper.Core/Lang/Locale/LangMisc.cs @@ -134,9 +134,11 @@ public sealed partial class LangMisc public string ImageCropperTitle { get; set; } = LangFallback?._Misc.ImageCropperTitle; - public string IsBytesMoreThanBytes { get; set; } = LangFallback?._Misc.IsBytesMoreThanBytes; - public string IsBytesUnlimited { get; set; } = LangFallback?._Misc.IsBytesUnlimited; - public string IsBytesNotANumber { get; set; } = LangFallback?._Misc.IsBytesNotANumber; + public string IsBytesMoreThanBytes { get; set; } = LangFallback?._Misc.IsBytesMoreThanBytes; + public string IsBytesUnlimited { get; set; } = LangFallback?._Misc.IsBytesUnlimited; + public string IsBytesNotANumber { get; set; } = LangFallback?._Misc.IsBytesNotANumber; + public string MissingVcRedist { get; set; } = LangFallback?._Misc.MissingVcRedist; + public string MissingVcRedistSubtitle { get; set; } = LangFallback?._Misc.MissingVcRedistSubtitle; } } #endregion diff --git a/Hi3Helper.Core/Lang/en_US.json b/Hi3Helper.Core/Lang/en_US.json index 06dccbcce..911737a4d 100644 --- a/Hi3Helper.Core/Lang/en_US.json +++ b/Hi3Helper.Core/Lang/en_US.json @@ -790,7 +790,10 @@ "IsBytesMoreThanBytes": "= {0} bytes (-/+ {1})", "IsBytesUnlimited": "= Unlimited", - "IsBytesNotANumber": "= NaN" + "IsBytesNotANumber": "= NaN", + + "MissingVcRedist": "Missing Visual C/C++ Redistributable", + "MissingVcRedistSubtitle": "You need to install the Visual C/C++ Redistributable to run this function. Do you want to download it now?\r\nNote: This will open a browser window and download a file from abbodi1406 GitHub repository. Please run the installer after downloading it then restart Collapse and try again.\r\nIf you have already installed it but the error remains, please send a issue ticket to us." }, "_BackgroundNotification": {