1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 01:58:30 +00:00

Merge pull request #208 from DarthAffe/ExceptionHandling

Exception handling
This commit is contained in:
DarthAffe 2021-04-26 22:33:23 +02:00 committed by GitHub
commit 5fbf716a87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 75 additions and 24 deletions

View File

@ -22,7 +22,7 @@ namespace RGB.NET.Core
#region Events
public event EventHandler<Exception>? Exception;
public event EventHandler<ExceptionEventArgs>? Exception;
#endregion
@ -54,10 +54,15 @@ namespace RGB.NET.Core
IsInitialized = true;
}
catch (DeviceProviderException)
{
Reset();
throw;
}
catch (Exception ex)
{
Reset();
Throw(ex);
Throw(ex, true);
return false;
}
@ -66,13 +71,23 @@ namespace RGB.NET.Core
protected virtual IEnumerable<IRGBDevice> GetLoadedDevices(RGBDeviceType loadFilter)
{
List<IRGBDevice> 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();
@ -98,15 +113,16 @@ namespace RGB.NET.Core
IsInitialized = false;
}
protected virtual void Throw(Exception ex)
protected virtual void Throw(Exception ex, bool isCritical = false)
{
try { OnException(ex); } catch { /* we don't want to throw due to bad event handlers */ }
ExceptionEventArgs args = new(ex, isCritical, ThrowsExceptions);
try { OnException(args); } catch { /* we don't want to throw due to bad event handlers */ }
if (ThrowsExceptions)
throw ex;
if (args.Throw)
throw new DeviceProviderException(ex, isCritical);
}
protected virtual void OnException(Exception ex) => Exception?.Invoke(this, ex);
protected virtual void OnException(ExceptionEventArgs args) => Exception?.Invoke(this, args);
public virtual void Dispose()
{

View File

@ -27,7 +27,7 @@ namespace RGB.NET.Core
/// <summary>
/// Occurs when an exception is thrown in the device provider
/// </summary>
event EventHandler<Exception>? Exception;
event EventHandler<ExceptionEventArgs>? Exception;
#endregion

View File

@ -18,6 +18,10 @@ namespace RGB.NET.Core
/// </summary>
public Exception Exception { get; }
public bool IsCritical { get; }
public bool Throw { get; set; }
#endregion
#region Constructors
@ -27,9 +31,11 @@ namespace RGB.NET.Core
/// Initializes a new instance of the <see cref="T:RGB.NET.Core.ExceptionEventArgs" /> class.
/// </summary>
/// <param name="exception">The <see cref="T:System.Exception" /> which is responsible for the event-call.</param>
public ExceptionEventArgs(Exception exception)
public ExceptionEventArgs(Exception exception, bool isCritical = false, bool @throw = false)
{
this.Exception = exception;
this.IsCritical = isCritical;
this.Throw = @throw;
}
#endregion

View File

@ -0,0 +1,23 @@
using System;
namespace RGB.NET.Core
{
public class DeviceProviderException : ApplicationException
{
#region Properties & Fields
private bool IsCritical { get; }
#endregion
#region Constructors
public DeviceProviderException(Exception? innerException, bool isCritical)
: base(innerException?.Message, innerException)
{
this.IsCritical = isCritical;
}
#endregion
}
}

View File

@ -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<IRGBDevice> LoadDevices()

View File

@ -16,7 +16,7 @@ namespace RGB.NET.Devices.CoolerMaster.Native
#region Libary Management
private static IntPtr _dllHandle = IntPtr.Zero;
/// <summary>
/// Reloads the SDK.
/// </summary>
@ -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));

View File

@ -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);
}
/// <inheritdoc />

View File

@ -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));

View File

@ -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();
}

View File

@ -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));

View File

@ -59,14 +59,14 @@ namespace RGB.NET.Devices.Msi
int errorCode;
if ((errorCode = _MsiSDK.Initialize()) != 0)
ThrowMsiError(errorCode);
ThrowMsiError(errorCode, true);
}
protected override IEnumerable<IRGBDevice> 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);
/// <inheritdoc />
public override void Dispose()

View File

@ -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));

View File

@ -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));

View File

@ -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<IRGBDevice> 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()
{

View File

@ -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));