From b300ac8451d725e403e029d3c1a13e6c8a6c7014 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Mon, 26 Apr 2021 22:09:54 +0200 Subject: [PATCH] Improved Exception-handling in device providers --- .../Devices/AbstractRGBDeviceProvider.cs | 18 ++++++++++++++---- .../CoolerMasterDeviceProvider.cs | 2 +- .../Native/_CoolerMasterSDK.cs | 3 ++- .../CorsairDeviceProvider.cs | 6 +++--- RGB.NET.Devices.Corsair/Native/_CUESDK.cs | 1 + .../LogitechDeviceProvider.cs | 2 +- .../Native/_LogitechGSDK.cs | 1 + RGB.NET.Devices.Msi/MsiDeviceProvider.cs | 6 +++--- RGB.NET.Devices.Msi/Native/_MsiSDK.cs | 1 + RGB.NET.Devices.Razer/Native/_RazerSDK.cs | 1 + RGB.NET.Devices.Razer/RazerDeviceProvider.cs | 4 ++-- RGB.NET.Devices.Wooting/Native/_WootingSDK.cs | 1 + 12 files changed, 31 insertions(+), 15 deletions(-) diff --git a/RGB.NET.Core/Devices/AbstractRGBDeviceProvider.cs b/RGB.NET.Core/Devices/AbstractRGBDeviceProvider.cs index 5ff52a6..f8d4cc7 100644 --- a/RGB.NET.Core/Devices/AbstractRGBDeviceProvider.cs +++ b/RGB.NET.Core/Devices/AbstractRGBDeviceProvider.cs @@ -71,13 +71,23 @@ namespace RGB.NET.Core protected virtual IEnumerable GetLoadedDevices(RGBDeviceType loadFilter) { + List devices = new(); foreach (IRGBDevice device in LoadDevices()) { - if (loadFilter.HasFlag(device.DeviceInfo.DeviceType)) - yield return device; - else - device.Dispose(); + try + { + if (loadFilter.HasFlag(device.DeviceInfo.DeviceType)) + devices.Add(device); + else + device.Dispose(); + } + catch (Exception ex) + { + Throw(ex); + } } + + return devices; } protected abstract void InitializeSDK(); diff --git a/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs b/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs index b833aae..f0fdcbf 100644 --- a/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs +++ b/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs @@ -56,7 +56,7 @@ namespace RGB.NET.Devices.CoolerMaster protected override void InitializeSDK() { _CoolerMasterSDK.Reload(); - if (_CoolerMasterSDK.GetSDKVersion() <= 0) Throw(new RGBDeviceException("Failed to initialize CoolerMaster-SDK")); + if (_CoolerMasterSDK.GetSDKVersion() <= 0) Throw(new RGBDeviceException("Failed to initialize CoolerMaster-SDK"), true); } protected override IEnumerable LoadDevices() diff --git a/RGB.NET.Devices.CoolerMaster/Native/_CoolerMasterSDK.cs b/RGB.NET.Devices.CoolerMaster/Native/_CoolerMasterSDK.cs index 4dfde8f..28f5776 100644 --- a/RGB.NET.Devices.CoolerMaster/Native/_CoolerMasterSDK.cs +++ b/RGB.NET.Devices.CoolerMaster/Native/_CoolerMasterSDK.cs @@ -16,7 +16,7 @@ namespace RGB.NET.Devices.CoolerMaster.Native #region Libary Management private static IntPtr _dllHandle = IntPtr.Zero; - + /// /// Reloads the SDK. /// @@ -41,6 +41,7 @@ namespace RGB.NET.Devices.CoolerMaster.Native if (dllPath == null) throw new RGBDeviceException($"Can't find the CoolerMaster-SDK at one of the expected locations:\r\n '{string.Join("\r\n", possiblePathList.Select(Path.GetFullPath))}'"); _dllHandle = LoadLibrary(dllPath); + if (_dllHandle == IntPtr.Zero) throw new RGBDeviceException($"CoolerMaster LoadLibrary failed with error code {Marshal.GetLastWin32Error()}"); _getSDKVersionPointer = (GetSDKVersionPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "GetCM_SDK_DllVer"), typeof(GetSDKVersionPointer)); _setControlDevicenPointer = (SetControlDevicePointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "SetControlDevice"), typeof(SetControlDevicePointer)); diff --git a/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs b/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs index 0389501..6456fe4 100644 --- a/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs +++ b/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs @@ -71,16 +71,16 @@ namespace RGB.NET.Devices.Corsair CorsairError error = LastError; if (error != CorsairError.Success) - Throw(new CUEException(error)); + Throw(new CUEException(error), true); if (ProtocolDetails.BreakingChanges) Throw(new RGBDeviceException("The SDK currently used isn't compatible with the installed version of CUE.\r\n" + $"CUE-Version: {ProtocolDetails.ServerVersion} (Protocol {ProtocolDetails.ServerProtocolVersion})\r\n" - + $"SDK-Version: {ProtocolDetails.SdkVersion} (Protocol {ProtocolDetails.SdkProtocolVersion})")); + + $"SDK-Version: {ProtocolDetails.SdkVersion} (Protocol {ProtocolDetails.SdkProtocolVersion})"), true); // DarthAffe 02.02.2021: 127 is iCUE if (!_CUESDK.CorsairSetLayerPriority(128)) - Throw(new CUEException(LastError)); + Throw(new CUEException(LastError), false); } /// diff --git a/RGB.NET.Devices.Corsair/Native/_CUESDK.cs b/RGB.NET.Devices.Corsair/Native/_CUESDK.cs index e8a0656..b10feb0 100644 --- a/RGB.NET.Devices.Corsair/Native/_CUESDK.cs +++ b/RGB.NET.Devices.Corsair/Native/_CUESDK.cs @@ -36,6 +36,7 @@ namespace RGB.NET.Devices.Corsair.Native if (dllPath == null) throw new RGBDeviceException($"Can't find the CUE-SDK at one of the expected locations:\r\n '{string.Join("\r\n", possiblePathList.Select(Path.GetFullPath))}'"); _dllHandle = LoadLibrary(dllPath); + if (_dllHandle == IntPtr.Zero) throw new RGBDeviceException($"Corsair LoadLibrary failed with error code {Marshal.GetLastWin32Error()}"); _corsairSetLedsColorsBufferByDeviceIndexPointer = (CorsairSetLedsColorsBufferByDeviceIndexPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairSetLedsColorsBufferByDeviceIndex"), typeof(CorsairSetLedsColorsBufferByDeviceIndexPointer)); _corsairSetLedsColorsFlushBufferPointer = (CorsairSetLedsColorsFlushBufferPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairSetLedsColorsFlushBuffer"), typeof(CorsairSetLedsColorsFlushBufferPointer)); diff --git a/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs b/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs index f24ecad..01bd373 100644 --- a/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs +++ b/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs @@ -124,7 +124,7 @@ namespace RGB.NET.Devices.Logitech _perKeyUpdateQueue = new LogitechPerKeyUpdateQueue(GetUpdateTrigger()); _LogitechGSDK.Reload(); - if (!_LogitechGSDK.LogiLedInit()) Throw(new RGBDeviceException("Failed to initialize Logitech-SDK.")); + if (!_LogitechGSDK.LogiLedInit()) Throw(new RGBDeviceException("Failed to initialize Logitech-SDK."), true); _LogitechGSDK.LogiLedSaveCurrentLighting(); } diff --git a/RGB.NET.Devices.Logitech/Native/_LogitechGSDK.cs b/RGB.NET.Devices.Logitech/Native/_LogitechGSDK.cs index 0fd1c05..ebec0c7 100644 --- a/RGB.NET.Devices.Logitech/Native/_LogitechGSDK.cs +++ b/RGB.NET.Devices.Logitech/Native/_LogitechGSDK.cs @@ -37,6 +37,7 @@ namespace RGB.NET.Devices.Logitech.Native if (dllPath == null) throw new RGBDeviceException($"Can't find the Logitech-SDK at one of the expected locations:\r\n '{string.Join("\r\n", possiblePathList.Select(Path.GetFullPath))}'"); _dllHandle = LoadLibrary(dllPath); + if (_dllHandle == IntPtr.Zero) throw new RGBDeviceException($"Logitech LoadLibrary failed with error code {Marshal.GetLastWin32Error()}"); _logiLedInitPointer = (LogiLedInitPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "LogiLedInit"), typeof(LogiLedInitPointer)); _logiLedShutdownPointer = (LogiLedShutdownPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "LogiLedShutdown"), typeof(LogiLedShutdownPointer)); diff --git a/RGB.NET.Devices.Msi/MsiDeviceProvider.cs b/RGB.NET.Devices.Msi/MsiDeviceProvider.cs index 00abd7b..1be843d 100644 --- a/RGB.NET.Devices.Msi/MsiDeviceProvider.cs +++ b/RGB.NET.Devices.Msi/MsiDeviceProvider.cs @@ -59,14 +59,14 @@ namespace RGB.NET.Devices.Msi int errorCode; if ((errorCode = _MsiSDK.Initialize()) != 0) - ThrowMsiError(errorCode); + ThrowMsiError(errorCode, true); } protected override IEnumerable LoadDevices() { int errorCode; if ((errorCode = _MsiSDK.GetDeviceInfo(out string[] deviceTypes, out int[] ledCounts)) != 0) - ThrowMsiError(errorCode); + ThrowMsiError(errorCode, true); for (int i = 0; i < deviceTypes.Length; i++) { @@ -93,7 +93,7 @@ namespace RGB.NET.Devices.Msi } } - private void ThrowMsiError(int errorCode) => Throw(new MysticLightException(errorCode, _MsiSDK.GetErrorMessage(errorCode))); + private void ThrowMsiError(int errorCode, bool isCritical = false) => Throw(new MysticLightException(errorCode, _MsiSDK.GetErrorMessage(errorCode)), isCritical); /// public override void Dispose() diff --git a/RGB.NET.Devices.Msi/Native/_MsiSDK.cs b/RGB.NET.Devices.Msi/Native/_MsiSDK.cs index 3bede30..e5c0700 100644 --- a/RGB.NET.Devices.Msi/Native/_MsiSDK.cs +++ b/RGB.NET.Devices.Msi/Native/_MsiSDK.cs @@ -38,6 +38,7 @@ namespace RGB.NET.Devices.Msi.Native SetDllDirectory(Path.GetDirectoryName(Path.GetFullPath(dllPath))!); _dllHandle = LoadLibrary(dllPath); + if (_dllHandle == IntPtr.Zero) throw new RGBDeviceException($"MSI LoadLibrary failed with error code {Marshal.GetLastWin32Error()}"); _initializePointer = (InitializePointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "MLAPI_Initialize"), typeof(InitializePointer)); _getDeviceInfoPointer = (GetDeviceInfoPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "MLAPI_GetDeviceInfo"), typeof(GetDeviceInfoPointer)); diff --git a/RGB.NET.Devices.Razer/Native/_RazerSDK.cs b/RGB.NET.Devices.Razer/Native/_RazerSDK.cs index 37eb33d..47d9b18 100644 --- a/RGB.NET.Devices.Razer/Native/_RazerSDK.cs +++ b/RGB.NET.Devices.Razer/Native/_RazerSDK.cs @@ -36,6 +36,7 @@ namespace RGB.NET.Devices.Razer.Native if (dllPath == null) throw new RGBDeviceException($"Can't find the Razer-SDK at one of the expected locations:\r\n '{string.Join("\r\n", possiblePathList.Select(Path.GetFullPath))}'"); _dllHandle = LoadLibrary(dllPath); + if (_dllHandle == IntPtr.Zero) throw new RGBDeviceException($"Razer LoadLibrary failed with error code {Marshal.GetLastWin32Error()}"); _initPointer = (InitPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "Init"), typeof(InitPointer)); _unInitPointer = (UnInitPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "UnInit"), typeof(UnInitPointer)); diff --git a/RGB.NET.Devices.Razer/RazerDeviceProvider.cs b/RGB.NET.Devices.Razer/RazerDeviceProvider.cs index 0d3215c..c99e9f4 100644 --- a/RGB.NET.Devices.Razer/RazerDeviceProvider.cs +++ b/RGB.NET.Devices.Razer/RazerDeviceProvider.cs @@ -218,7 +218,7 @@ namespace RGB.NET.Devices.Razer RazerError error; if (((error = _RazerSDK.Init()) != RazerError.Success) && Enum.IsDefined(typeof(RazerError), error)) //HACK DarthAffe 08.02.2018: The x86-SDK seems to have a problem here ... - ThrowRazerError(error); + ThrowRazerError(error, true); } protected override IEnumerable GetLoadedDevices(RGBDeviceType loadFilter) @@ -265,7 +265,7 @@ namespace RGB.NET.Devices.Razer } } - private void ThrowRazerError(RazerError errorCode) => throw new RazerException(errorCode); + private void ThrowRazerError(RazerError errorCode, bool isCritical) => Throw(new RazerException(errorCode), isCritical); private void TryUnInit() { diff --git a/RGB.NET.Devices.Wooting/Native/_WootingSDK.cs b/RGB.NET.Devices.Wooting/Native/_WootingSDK.cs index e6baf08..88d39b4 100644 --- a/RGB.NET.Devices.Wooting/Native/_WootingSDK.cs +++ b/RGB.NET.Devices.Wooting/Native/_WootingSDK.cs @@ -38,6 +38,7 @@ namespace RGB.NET.Devices.Wooting.Native SetDllDirectory(Path.GetDirectoryName(Path.GetFullPath(dllPath))!); _dllHandle = LoadLibrary(dllPath); + if (_dllHandle == IntPtr.Zero) throw new RGBDeviceException($"Wooting LoadLibrary failed with error code {Marshal.GetLastWin32Error()}"); _getDeviceInfoPointer = (GetDeviceInfoPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "wooting_rgb_device_info"), typeof(GetDeviceInfoPointer)); _keyboardConnectedPointer = (KeyboardConnectedPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "wooting_rgb_kbd_connected"), typeof(KeyboardConnectedPointer));