Skip to content

Commit

Permalink
Add exception handler for registry key writing
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzaboxer committed Sep 27, 2024
1 parent fe04b46 commit d8f1e27
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Bloxstrap/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand Down
21 changes: 21 additions & 0 deletions Bloxstrap/Extensions/RegistryKeyEx.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
38 changes: 19 additions & 19 deletions Bloxstrap/Installer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions Bloxstrap/Resources/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Bloxstrap/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1204,4 +1204,7 @@ Please manually delete Bloxstrap.exe from the install location or try restarting
<data name="Menu.Shortcuts.ExtractIcons.Description" xml:space="preserve">
<value>To use for your shortcuts, right-click it, open properties, change icon, browse, and pick from the Icons folder.</value>
</data>
<data name="Dialog.RegistryWriteError" xml:space="preserve">
<value>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.</value>
</data>
</root>
18 changes: 9 additions & 9 deletions Bloxstrap/Utility/WindowsRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit d8f1e27

Please sign in to comment.