diff --git a/.gitignore b/.gitignore index a222580..cb7f10b 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ bld/ [Bb]in/ [Oo]bj/ +*.editorconfig # Visual Studio 2015 cache/options directory .vs/ diff --git a/DisplayMagician/GameLibraries/SteamLibrary.cs b/DisplayMagician/GameLibraries/SteamLibrary.cs index 42e5e76..30fb44f 100644 --- a/DisplayMagician/GameLibraries/SteamLibrary.cs +++ b/DisplayMagician/GameLibraries/SteamLibrary.cs @@ -24,6 +24,8 @@ namespace DisplayMagician.GameLibraries private static string _steamConfigVdfFile; private static string _registrySteamKey = @"SOFTWARE\\Valve\\Steam"; private static string _registryAppsKey = $@"{_registrySteamKey}\\Apps"; + private static bool _isSteamInstalled = false; + private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); // Other constants that are useful #endregion @@ -39,15 +41,34 @@ namespace DisplayMagician.GameLibraries #region Class Constructors static SteamLibrary() { - // Find the SteamExe location, and the SteamPath for later - using (var key = Registry.CurrentUser.OpenSubKey(SteamLibrary.SteamRegistryKey, RegistryKeyPermissionCheck.ReadSubTree)) + try { - _steamExe = (string)key?.GetValue(@"SteamExe", string.Empty) ?? string.Empty; - _steamExe = _steamExe.Replace('/', '\\'); - _steamPath = (string)key?.GetValue(@"SteamPath", string.Empty) ?? string.Empty; - _steamPath = _steamPath.Replace('/', '\\'); + // Find the SteamExe location, and the SteamPath for later + using (var key = Registry.CurrentUser.OpenSubKey(SteamLibrary.SteamRegistryKey, RegistryKeyPermissionCheck.ReadSubTree)) + { + _steamExe = (string)key?.GetValue(@"SteamExe", string.Empty) ?? string.Empty; + _steamExe = _steamExe.Replace('/', '\\'); + _steamPath = (string)key?.GetValue(@"SteamPath", string.Empty) ?? string.Empty; + _steamPath = _steamPath.Replace('/', '\\'); + } + _isSteamInstalled = true; + } + catch (SecurityException ex) + { + logger.Warn("The user does not have the permissions required to read the Steam registry key."); + } + catch (ObjectDisposedException ex) + { + logger.Warn("The Microsoft.Win32.RegistryKey is closed when trying to access theSteam registry key (closed keys cannot be accessed)."); + } + catch (IOException ex) + { + logger.Warn("The Steam registry key has been marked for deletion so we cannot access the value during the SteamLibrary check."); + } + catch (UnauthorizedAccessException ex) + { + logger.Warn("The user does not have the necessary registry rights to check whether Steam is installed."); } - } #endregion @@ -108,10 +129,7 @@ namespace DisplayMagician.GameLibraries { get { - if (!string.IsNullOrWhiteSpace(SteamExe) && File.Exists(SteamExe)) - return true; - - return false; + return _isSteamInstalled; } } diff --git a/DisplayMagician/GameLibraries/UplayLibrary.cs b/DisplayMagician/GameLibraries/UplayLibrary.cs index cffdbac..ff43265 100644 --- a/DisplayMagician/GameLibraries/UplayLibrary.cs +++ b/DisplayMagician/GameLibraries/UplayLibrary.cs @@ -21,12 +21,15 @@ namespace DisplayMagician.GameLibraries // Common items to the class private static List _allUplayGames = new List(); private static string uplayAppIdRegex = @"/^[0-9A-F]{1,10}$"; + private static bool _isUplayInstalled = false; private static string _uplayExe; private static string _uplayPath; private static string _uplayConfigVdfFile; internal static string registryUplayLauncherKey = @"SOFTWARE\WOW6432Node\Ubisoft\Launcher"; internal static string registryUplayInstallsKey = @"SOFTWARE\WOW6432Node\Ubisoft\Launcher\Installs"; internal static string registryUplayOpenCmdKey = @"SOFTWARE\Classes\uplay\Shell\Open\Command"; + private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); + // Other constants that are useful #endregion @@ -43,10 +46,30 @@ namespace DisplayMagician.GameLibraries #region Class Constructors static UplayLibrary() { - // Find the UplayExe location, and the UplayPath for later - RegistryKey uplayInstallKey = Registry.LocalMachine.OpenSubKey(registryUplayLauncherKey, RegistryKeyPermissionCheck.ReadSubTree); - _uplayPath = uplayInstallKey.GetValue("InstallDir", "C:\\Program Files (x86)\\Ubisoft\\Ubisoft Game Launcher\\").ToString(); - _uplayExe = $"{_uplayPath}upc.exe"; + try + { + // Find the UplayExe location, and the UplayPath for later + RegistryKey uplayInstallKey = Registry.LocalMachine.OpenSubKey(registryUplayLauncherKey, RegistryKeyPermissionCheck.ReadSubTree); + _uplayPath = uplayInstallKey.GetValue("InstallDir", "C:\\Program Files (x86)\\Ubisoft\\Ubisoft Game Launcher\\").ToString(); + _uplayExe = $"{_uplayPath}upc.exe"; + _isUplayInstalled = true; + } + catch (SecurityException ex) + { + logger.Warn("The user does not have the permissions required to read the Uplay InstallDir registry key."); + } + catch(ObjectDisposedException ex) + { + logger.Warn("The Microsoft.Win32.RegistryKey is closed when trying to access the Uplay InstallDir registry key (closed keys cannot be accessed)."); + } + catch (IOException ex) + { + logger.Warn("The Uplay InstallDir registry key has been marked for deletion so we cannot access the value dueing the UplayLibrary check."); + } + catch (UnauthorizedAccessException ex) + { + logger.Warn("The user does not have the necessary registry rights to check whether Uplay is installed."); + } } #endregion @@ -91,10 +114,7 @@ namespace DisplayMagician.GameLibraries { get { - if (!string.IsNullOrWhiteSpace(UplayExe) && File.Exists(UplayExe)) - return true; - - return false; + return _isUplayInstalled; } }