From d8f1e27585ce7605d705c5b78ac0a0b81f9d989e Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Fri, 27 Sep 2024 23:48:59 +0100 Subject: [PATCH] Add exception handler for registry key writing --- Bloxstrap/Bootstrapper.cs | 4 +-- Bloxstrap/Extensions/RegistryKeyEx.cs | 21 ++++++++++++++ Bloxstrap/Installer.cs | 38 ++++++++++++------------- Bloxstrap/Resources/Strings.Designer.cs | 9 ++++++ Bloxstrap/Resources/Strings.resx | 3 ++ Bloxstrap/Utility/WindowsRegistry.cs | 18 ++++++------ 6 files changed, 63 insertions(+), 30 deletions(-) create mode 100644 Bloxstrap/Extensions/RegistryKeyEx.cs diff --git a/Bloxstrap/Bootstrapper.cs b/Bloxstrap/Bootstrapper.cs index 3a4a1817..a28ce228 100644 --- a/Bloxstrap/Bootstrapper.cs +++ b/Bloxstrap/Bootstrapper.cs @@ -252,7 +252,7 @@ and not HttpStatusCode.Forbidden clientVersion = await RobloxDeployment.GetInfo(channel, AppData.BinaryType); } - key.SetValue("www.roblox.com", channel); + key.SetValueSafe("www.roblox.com", channel); _latestVersionGuid = clientVersion.VersionGuid; @@ -724,7 +724,7 @@ private async Task UpgradeRoblox() using (var uninstallKey = Registry.CurrentUser.CreateSubKey(App.UninstallKey)) { - uninstallKey.SetValue("EstimatedSize", totalSize); + uninstallKey.SetValueSafe("EstimatedSize", totalSize); } App.Logger.WriteLine(LOG_IDENT, $"Registered as {totalSize} KB"); diff --git a/Bloxstrap/Extensions/RegistryKeyEx.cs b/Bloxstrap/Extensions/RegistryKeyEx.cs new file mode 100644 index 00000000..a5e9af4f --- /dev/null +++ b/Bloxstrap/Extensions/RegistryKeyEx.cs @@ -0,0 +1,21 @@ +using Microsoft.Win32; + +namespace Bloxstrap.Extensions +{ + public static class RegistryKeyEx + { + public static void SetValueSafe(this RegistryKey registryKey, string? name, object value) + { + try + { + App.Logger.WriteLine("RegistryKeyEx::SetValueSafe", $"Writing '{value}' to {registryKey}\\{name}"); + registryKey.SetValue(name, value); + } + catch (UnauthorizedAccessException) + { + Frontend.ShowMessageBox(Strings.Dialog_RegistryWriteError, System.Windows.MessageBoxImage.Error); + App.Terminate(); + } + } + } +} diff --git a/Bloxstrap/Installer.cs b/Bloxstrap/Installer.cs index 59fc133b..bda9f345 100644 --- a/Bloxstrap/Installer.cs +++ b/Bloxstrap/Installer.cs @@ -53,23 +53,23 @@ public void DoInstall() // TODO: registry access checks, i'll need to look back on issues to see what the error looks like using (var uninstallKey = Registry.CurrentUser.CreateSubKey(App.UninstallKey)) { - uninstallKey.SetValue("DisplayIcon", $"{Paths.Application},0"); - uninstallKey.SetValue("DisplayName", App.ProjectName); + uninstallKey.SetValueSafe("DisplayIcon", $"{Paths.Application},0"); + uninstallKey.SetValueSafe("DisplayName", App.ProjectName); - uninstallKey.SetValue("DisplayVersion", App.Version); + uninstallKey.SetValueSafe("DisplayVersion", App.Version); if (uninstallKey.GetValue("InstallDate") is null) - uninstallKey.SetValue("InstallDate", DateTime.Now.ToString("yyyyMMdd")); - - uninstallKey.SetValue("InstallLocation", Paths.Base); - uninstallKey.SetValue("NoRepair", 1); - uninstallKey.SetValue("Publisher", App.ProjectOwner); - uninstallKey.SetValue("ModifyPath", $"\"{Paths.Application}\" -settings"); - uninstallKey.SetValue("QuietUninstallString", $"\"{Paths.Application}\" -uninstall -quiet"); - uninstallKey.SetValue("UninstallString", $"\"{Paths.Application}\" -uninstall"); - uninstallKey.SetValue("HelpLink", App.ProjectHelpLink); - uninstallKey.SetValue("URLInfoAbout", App.ProjectSupportLink); - uninstallKey.SetValue("URLUpdateInfo", App.ProjectDownloadLink); + uninstallKey.SetValueSafe("InstallDate", DateTime.Now.ToString("yyyyMMdd")); + + uninstallKey.SetValueSafe("InstallLocation", Paths.Base); + uninstallKey.SetValueSafe("NoRepair", 1); + uninstallKey.SetValueSafe("Publisher", App.ProjectOwner); + uninstallKey.SetValueSafe("ModifyPath", $"\"{Paths.Application}\" -settings"); + uninstallKey.SetValueSafe("QuietUninstallString", $"\"{Paths.Application}\" -uninstall -quiet"); + uninstallKey.SetValueSafe("UninstallString", $"\"{Paths.Application}\" -uninstall"); + uninstallKey.SetValueSafe("HelpLink", App.ProjectHelpLink); + uninstallKey.SetValueSafe("URLInfoAbout", App.ProjectSupportLink); + uninstallKey.SetValueSafe("URLUpdateInfo", App.ProjectDownloadLink); } // only register player, for the scenario where the user installs bloxstrap, closes it, @@ -426,12 +426,12 @@ public static void HandleUpgrade() using (var uninstallKey = Registry.CurrentUser.CreateSubKey(App.UninstallKey)) { - uninstallKey.SetValue("DisplayVersion", App.Version); + uninstallKey.SetValueSafe("DisplayVersion", App.Version); - uninstallKey.SetValue("Publisher", App.ProjectOwner); - uninstallKey.SetValue("HelpLink", App.ProjectHelpLink); - uninstallKey.SetValue("URLInfoAbout", App.ProjectSupportLink); - uninstallKey.SetValue("URLUpdateInfo", App.ProjectDownloadLink); + uninstallKey.SetValueSafe("Publisher", App.ProjectOwner); + uninstallKey.SetValueSafe("HelpLink", App.ProjectHelpLink); + uninstallKey.SetValueSafe("URLInfoAbout", App.ProjectSupportLink); + uninstallKey.SetValueSafe("URLUpdateInfo", App.ProjectDownloadLink); } // update migrations diff --git a/Bloxstrap/Resources/Strings.Designer.cs b/Bloxstrap/Resources/Strings.Designer.cs index 8a3f4a54..82d3c72c 100644 --- a/Bloxstrap/Resources/Strings.Designer.cs +++ b/Bloxstrap/Resources/Strings.Designer.cs @@ -1021,6 +1021,15 @@ public static string Dialog_PlayerError_HelpInformation { } } + /// + /// Looks up a localized string similar to Bloxstrap is unable to write to the Windows Registry. An antivirus is likely interfering and causing issues. Please check to make sure there isn't anything that would restrict Bloxstrap's operation.. + /// + public static string Dialog_RegistryWriteError { + get { + return ResourceManager.GetString("Dialog.RegistryWriteError", resourceCulture); + } + } + /// /// Looks up a localized string similar to Early 2015. /// diff --git a/Bloxstrap/Resources/Strings.resx b/Bloxstrap/Resources/Strings.resx index 231b03f0..6ae94863 100644 --- a/Bloxstrap/Resources/Strings.resx +++ b/Bloxstrap/Resources/Strings.resx @@ -1204,4 +1204,7 @@ Please manually delete Bloxstrap.exe from the install location or try restarting To use for your shortcuts, right-click it, open properties, change icon, browse, and pick from the Icons folder. + + Bloxstrap is unable to write to the Windows Registry. An antivirus is likely interfering and causing issues. Please check to make sure there isn't anything that would restrict Bloxstrap's operation. + \ No newline at end of file diff --git a/Bloxstrap/Utility/WindowsRegistry.cs b/Bloxstrap/Utility/WindowsRegistry.cs index be2981c3..23ca7129 100644 --- a/Bloxstrap/Utility/WindowsRegistry.cs +++ b/Bloxstrap/Utility/WindowsRegistry.cs @@ -16,14 +16,14 @@ public static void RegisterProtocol(string key, string name, string handler, str if (uriKey.GetValue("") is null) { - uriKey.SetValue("", $"URL: {name} Protocol"); - uriKey.SetValue("URL Protocol", ""); + uriKey.SetValueSafe("", $"URL: {name} Protocol"); + uriKey.SetValueSafe("URL Protocol", ""); } if (uriCommandKey.GetValue("") as string != handlerArgs) { - uriIconKey.SetValue("", handler); - uriCommandKey.SetValue("", handlerArgs); + uriIconKey.SetValueSafe("", handler); + uriCommandKey.SetValueSafe("", handlerArgs); } } @@ -85,16 +85,16 @@ public static void RegisterStudioFileClass(string handler, string handlerParam) using RegistryKey uriCommandKey = uriOpenKey.CreateSubKey(@"command"); if (uriKey.GetValue("") as string != keyValue) - uriKey.SetValue("", keyValue); + uriKey.SetValueSafe("", keyValue); if (uriCommandKey.GetValue("") as string != handlerArgs) - uriCommandKey.SetValue("", handlerArgs); + uriCommandKey.SetValueSafe("", handlerArgs); if (uriOpenKey.GetValue("") as string != "Open") - uriOpenKey.SetValue("", "Open"); + uriOpenKey.SetValueSafe("", "Open"); if (uriIconKey.GetValue("") as string != iconValue) - uriIconKey.SetValue("", iconValue); + uriIconKey.SetValueSafe("", iconValue); } public static void RegisterStudioFileType(string key) @@ -103,7 +103,7 @@ public static void RegisterStudioFileType(string key) uriKey.CreateSubKey(RobloxPlaceKey + @"\ShellNew"); if (uriKey.GetValue("") as string != RobloxPlaceKey) - uriKey.SetValue("", RobloxPlaceKey); + uriKey.SetValueSafe("", RobloxPlaceKey); } public static void Unregister(string key)