From 338936d42f067ca3181aebfa3c6680ebf83bd8ea Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sun, 19 Nov 2017 21:20:23 +0100 Subject: [PATCH] Changed all device-providers to load as much devices as possible without throwing exceptions --- RGB.NET.Devices.Asus/AsusDeviceProvider.cs | 115 +++++++---- .../CoolerMasterDeviceProvider.cs | 21 +- .../CorsairDeviceProvider.cs | 180 +++++++----------- .../LogitechDeviceProvider.cs | 37 ++-- RGB.NET.Devices.Msi/MsiDeviceProvider.cs | 12 +- .../NovationDeviceProvider.cs | 21 +- 6 files changed, 190 insertions(+), 196 deletions(-) diff --git a/RGB.NET.Devices.Asus/AsusDeviceProvider.cs b/RGB.NET.Devices.Asus/AsusDeviceProvider.cs index c6bc1e4..7fa246b 100644 --- a/RGB.NET.Devices.Asus/AsusDeviceProvider.cs +++ b/RGB.NET.Devices.Asus/AsusDeviceProvider.cs @@ -93,49 +93,68 @@ namespace RGB.NET.Devices.Asus #region Mainboard - //TODO DarthAffe 21.10.2017: Requesting mainboards seems to fail if only a non mb-device (tested with only a gpu) is connected - int mainboardCount = _AsusSDK.EnumerateMbController(IntPtr.Zero, 0); - if (mainboardCount > 0) + try { - IntPtr mainboardHandles = Marshal.AllocHGlobal(mainboardCount * IntPtr.Size); - _AsusSDK.EnumerateMbController(mainboardHandles, mainboardCount); - - for (int i = 0; i < mainboardCount; i++) + int mainboardCount = _AsusSDK.EnumerateMbController(IntPtr.Zero, 0); + if (mainboardCount > 0) { - IntPtr handle = Marshal.ReadIntPtr(mainboardHandles, i); - _AsusSDK.SetMbMode(handle, 1); - AsusMainboardRGBDevice device = new AsusMainboardRGBDevice(new AsusMainboardRGBDeviceInfo(RGBDeviceType.Mainboard, handle)); - device.Initialize(); - devices.Add(device); + IntPtr mainboardHandles = Marshal.AllocHGlobal(mainboardCount * IntPtr.Size); + _AsusSDK.EnumerateMbController(mainboardHandles, mainboardCount); + + for (int i = 0; i < mainboardCount; i++) + { + try + { + IntPtr handle = Marshal.ReadIntPtr(mainboardHandles, i); + _AsusSDK.SetMbMode(handle, 1); + AsusMainboardRGBDevice device = + new AsusMainboardRGBDevice(new AsusMainboardRGBDeviceInfo(RGBDeviceType.Mainboard, handle)); + device.Initialize(); + devices.Add(device); + } + catch { if (throwExceptions) throw; } + } } } + catch { if (throwExceptions) throw; } #endregion #region Graphics cards //TODO DarthAffe 21.10.2017: This somehow returns non-existant gpus (at least for me) which cause huge lags (if a real asus-ready gpu is connected this doesn't happen) - int graphicCardCount = _AsusSDK.EnumerateGPU(IntPtr.Zero, 0); - if (graphicCardCount > 0) - { - IntPtr grapicsCardHandles = Marshal.AllocHGlobal(graphicCardCount * IntPtr.Size); - _AsusSDK.EnumerateGPU(grapicsCardHandles, graphicCardCount); - for (int i = 0; i < graphicCardCount; i++) + try + { + int graphicCardCount = _AsusSDK.EnumerateGPU(IntPtr.Zero, 0); + if (graphicCardCount > 0) { - IntPtr handle = Marshal.ReadIntPtr(grapicsCardHandles, i); - _AsusSDK.SetGPUMode(handle, 1); - AsusGraphicsCardRGBDevice device = new AsusGraphicsCardRGBDevice(new AsusGraphicsCardRGBDeviceInfo(RGBDeviceType.GraphicsCard, handle)); - device.Initialize(); - devices.Add(device); + IntPtr grapicsCardHandles = Marshal.AllocHGlobal(graphicCardCount * IntPtr.Size); + _AsusSDK.EnumerateGPU(grapicsCardHandles, graphicCardCount); + + for (int i = 0; i < graphicCardCount; i++) + { + try + { + IntPtr handle = Marshal.ReadIntPtr(grapicsCardHandles, i); + _AsusSDK.SetGPUMode(handle, 1); + AsusGraphicsCardRGBDevice device = new AsusGraphicsCardRGBDevice(new AsusGraphicsCardRGBDeviceInfo(RGBDeviceType.GraphicsCard, handle)); + device.Initialize(); + devices.Add(device); + } + catch { if (throwExceptions) throw; } + } } } + catch { if (throwExceptions) throw; } #endregion #region DRAM //TODO DarthAffe 29.10.2017: I don't know why they are even documented, but the asus guy said they aren't in the SDK right now. + //try + //{ //int dramCount = _AsusSDK.EnumerateDram(IntPtr.Zero, 0); //if (dramCount > 0) //{ @@ -144,54 +163,66 @@ namespace RGB.NET.Devices.Asus // for (int i = 0; i < dramCount; i++) // { + //try + //{ // IntPtr handle = Marshal.ReadIntPtr(dramHandles, i); // _AsusSDK.SetDramMode(handle, 1); // AsusDramRGBDevice device = new AsusDramRGBDevice(new AsusDramRGBDeviceInfo(RGBDeviceType.DRAM, handle)); // device.Initialize(); // devices.Add(device); // } + //catch { if (throwExceptions) throw; } + // } //} + //} + // catch { if (throwExceptions) throw; } #endregion #region Keyboard - IntPtr keyboardHandle = Marshal.AllocHGlobal(IntPtr.Size); - if (_AsusSDK.CreateClaymoreKeyboard(keyboardHandle)) + try { - _AsusSDK.SetClaymoreKeyboardMode(keyboardHandle, 1); - AsusKeyboardRGBDevice device = new AsusKeyboardRGBDevice(new AsusKeyboardRGBDeviceInfo(RGBDeviceType.Keyboard, keyboardHandle, GetCulture())); - device.Initialize(); - devices.Add(device); + IntPtr keyboardHandle = Marshal.AllocHGlobal(IntPtr.Size); + if (_AsusSDK.CreateClaymoreKeyboard(keyboardHandle)) + { + _AsusSDK.SetClaymoreKeyboardMode(keyboardHandle, 1); + AsusKeyboardRGBDevice device = new AsusKeyboardRGBDevice(new AsusKeyboardRGBDeviceInfo(RGBDeviceType.Keyboard, keyboardHandle, GetCulture())); + device.Initialize(); + devices.Add(device); + } } + catch { if (throwExceptions) throw; } #endregion #region Mouse - IntPtr mouseHandle = Marshal.AllocHGlobal(IntPtr.Size); - if (_AsusSDK.CreateRogMouse(mouseHandle)) + try { - _AsusSDK.SetRogMouseMode(mouseHandle, 1); - AsusMouseRGBDevice device = new AsusMouseRGBDevice(new AsusMouseRGBDeviceInfo(RGBDeviceType.Mouse, mouseHandle)); - device.Initialize(); - devices.Add(device); + IntPtr mouseHandle = Marshal.AllocHGlobal(IntPtr.Size); + if (_AsusSDK.CreateRogMouse(mouseHandle)) + { + _AsusSDK.SetRogMouseMode(mouseHandle, 1); + AsusMouseRGBDevice device = new AsusMouseRGBDevice(new AsusMouseRGBDeviceInfo(RGBDeviceType.Mouse, mouseHandle)); + device.Initialize(); + devices.Add(device); + } } + catch { if (throwExceptions) throw; } #endregion Devices = new ReadOnlyCollection(devices); + IsInitialized = true; } catch { if (throwExceptions) throw; - else - return false; + return false; } - IsInitialized = true; - return true; } @@ -209,9 +240,9 @@ namespace RGB.NET.Devices.Asus case RGBDeviceType.GraphicsCard: _AsusSDK.SetGPUMode(deviceInfo.Handle, 0); break; - //case RGBDeviceType.DRAM: - // _AsusSDK.SetDramMode(deviceInfo.Handle, 0); - // break; + //case RGBDeviceType.DRAM: + // _AsusSDK.SetDramMode(deviceInfo.Handle, 0); + // break; } } } diff --git a/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs b/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs index 2a8043b..3e70d53 100644 --- a/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs +++ b/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs @@ -95,10 +95,11 @@ namespace RGB.NET.Devices.CoolerMaster foreach (CoolerMasterDevicesIndexes index in Enum.GetValues(typeof(CoolerMasterDevicesIndexes))) { - _CoolerMasterSDK.SetControlDevice(index); - if (_CoolerMasterSDK.IsDevicePlugged()) + + try { - try + _CoolerMasterSDK.SetControlDevice(index); + if (_CoolerMasterSDK.IsDevicePlugged()) { CoolerMasterRGBDevice device; switch (index.GetDeviceType()) @@ -119,28 +120,20 @@ namespace RGB.NET.Devices.CoolerMaster device.Initialize(); devices.Add(device); } - catch - { - if (throwExceptions) - throw; - else - continue; - } } + catch { if (throwExceptions) throw; } } Devices = new ReadOnlyCollection(devices); + IsInitialized = true; } catch { if (throwExceptions) throw; - else - return false; + return false; } - IsInitialized = true; - return true; } diff --git a/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs b/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs index ad89367..6307d47 100644 --- a/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs +++ b/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs @@ -96,123 +96,91 @@ namespace RGB.NET.Devices.Corsair try { _CUESDK.Reload(); + + ProtocolDetails = new CorsairProtocolDetails(_CUESDK.CorsairPerformProtocolHandshake()); + + CorsairError error = LastError; + if (error != CorsairError.Success) + throw new CUEException(error); + + 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})"); + + if (exclusiveAccessIfPossible) + { + if (!_CUESDK.CorsairRequestControl(CorsairAccessMode.ExclusiveLightingControl)) + throw new CUEException(LastError); + + HasExclusiveAccess = true; + } + else + HasExclusiveAccess = false; + + IList devices = new List(); + int deviceCount = _CUESDK.CorsairGetDeviceCount(); + for (int i = 0; i < deviceCount; i++) + { + try + { + _CorsairDeviceInfo nativeDeviceInfo = (_CorsairDeviceInfo)Marshal.PtrToStructure(_CUESDK.CorsairGetDeviceInfo(i), typeof(_CorsairDeviceInfo)); + CorsairRGBDeviceInfo info = new CorsairRGBDeviceInfo(i, RGBDeviceType.Unknown, nativeDeviceInfo); + if (!info.CapsMask.HasFlag(CorsairDeviceCaps.Lighting)) + continue; // Everything that doesn't support lighting control is useless + + CorsairRGBDevice device; + switch (info.CorsairDeviceType) + { + case CorsairDeviceType.Keyboard: + device = new CorsairKeyboardRGBDevice(new CorsairKeyboardRGBDeviceInfo(i, nativeDeviceInfo)); + break; + + case CorsairDeviceType.Mouse: + device = new CorsairMouseRGBDevice(new CorsairMouseRGBDeviceInfo(i, nativeDeviceInfo)); + break; + + case CorsairDeviceType.Headset: + device = new CorsairHeadsetRGBDevice(new CorsairHeadsetRGBDeviceInfo(i, nativeDeviceInfo)); + break; + + case CorsairDeviceType.Mousepad: + device = new CorsairMousepadRGBDevice(new CorsairMousepadRGBDeviceInfo(i, nativeDeviceInfo)); + break; + + // ReSharper disable once RedundantCaseLabel + case CorsairDeviceType.Unknown: + default: + throw new RGBDeviceException("Unknown Device-Type"); + } + device.Initialize(); + AddSpecialParts(device); + + error = LastError; + if (error != CorsairError.Success) + throw new CUEException(error); + + devices.Add(device); + } + catch { if (throwExceptions) throw; } + } + + Devices = new ReadOnlyCollection(devices); + IsInitialized = true; } catch - { - if (throwExceptions) - throw; - else - return false; - } - - ProtocolDetails = new CorsairProtocolDetails(_CUESDK.CorsairPerformProtocolHandshake()); - - CorsairError error = LastError; - if (error != CorsairError.Success) { Reset(); - if (throwExceptions) - throw new CUEException(error); - else - return false; + if (throwExceptions) throw; + return false; } - if (ProtocolDetails.BreakingChanges) - { - Reset(); - if (throwExceptions) - 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})"); - else - return false; - } - - if (exclusiveAccessIfPossible) - { - if (!_CUESDK.CorsairRequestControl(CorsairAccessMode.ExclusiveLightingControl)) - { - Reset(); - if (throwExceptions) - throw new CUEException(LastError); - else - return false; - } - HasExclusiveAccess = true; - } - else - HasExclusiveAccess = false; - - IList devices = new List(); - int deviceCount = _CUESDK.CorsairGetDeviceCount(); - for (int i = 0; i < deviceCount; i++) - { - _CorsairDeviceInfo nativeDeviceInfo = - (_CorsairDeviceInfo)Marshal.PtrToStructure(_CUESDK.CorsairGetDeviceInfo(i), typeof(_CorsairDeviceInfo)); - CorsairRGBDeviceInfo info = new CorsairRGBDeviceInfo(i, RGBDeviceType.Unknown, nativeDeviceInfo); - if (!info.CapsMask.HasFlag(CorsairDeviceCaps.Lighting)) - continue; // Everything that doesn't support lighting control is useless - - CorsairRGBDevice device; - switch (info.CorsairDeviceType) - { - case CorsairDeviceType.Keyboard: - device = new CorsairKeyboardRGBDevice(new CorsairKeyboardRGBDeviceInfo(i, nativeDeviceInfo)); - break; - case CorsairDeviceType.Mouse: - device = new CorsairMouseRGBDevice(new CorsairMouseRGBDeviceInfo(i, nativeDeviceInfo)); - break; - case CorsairDeviceType.Headset: - device = new CorsairHeadsetRGBDevice(new CorsairHeadsetRGBDeviceInfo(i, nativeDeviceInfo)); - break; - case CorsairDeviceType.Mousepad: - device = new CorsairMousepadRGBDevice(new CorsairMousepadRGBDeviceInfo(i, nativeDeviceInfo)); - break; - // ReSharper disable once RedundantCaseLabel - case CorsairDeviceType.Unknown: - default: - if (throwExceptions) - throw new RGBDeviceException("Unknown Device-Type"); - else - continue; - } - - try - { - device.Initialize(); - - AddSpecialParts(device); - } - catch - { - if (throwExceptions) - throw; - else - continue; - } - devices.Add(device); - - error = LastError; - if (error != CorsairError.Success) - { - Reset(); - if (throwExceptions) - throw new CUEException(error); - else - return false; - } - } - - Devices = new ReadOnlyCollection(devices); - - IsInitialized = true; - return true; } private void AddSpecialParts(CorsairRGBDevice device) { - //if (device.DeviceInfo.Model.Equals("K95 RGB Platinum", StringComparison.OrdinalIgnoreCase)) + if (device.DeviceInfo.Model.Equals("K95 RGB Platinum", StringComparison.OrdinalIgnoreCase)) device.AddSpecialDevicePart(new LightbarSpecialPart(device)); } diff --git a/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs b/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs index 8f1392d..2240ad1 100644 --- a/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs +++ b/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs @@ -82,8 +82,7 @@ namespace RGB.NET.Devices.Logitech if (IsInitialized) _LogitechGSDK.LogiLedRestoreLighting(); } - catch - { /* At least we tried ... */ } + catch { /* At least we tried ... */ } IsInitialized = false; @@ -97,34 +96,40 @@ namespace RGB.NET.Devices.Logitech IList devices = new List(); DeviceChecker.LoadDeviceList(); - if (DeviceChecker.IsPerKeyDeviceConnected) + try { - (string model, RGBDeviceType deviceType, int _, string imageBasePath, string imageLayout, string layoutPath) = DeviceChecker.PerKeyDeviceData; - LogitechRGBDevice device = new LogitechPerKeyRGBDevice(new LogitechRGBDeviceInfo(deviceType, model, LogitechDeviceCaps.PerKeyRGB, imageBasePath, imageLayout, layoutPath)); - device.Initialize(); - devices.Add(device); + if (DeviceChecker.IsPerKeyDeviceConnected) + { + (string model, RGBDeviceType deviceType, int _, string imageBasePath, string imageLayout, string layoutPath) = DeviceChecker.PerKeyDeviceData; + LogitechRGBDevice device = new LogitechPerKeyRGBDevice(new LogitechRGBDeviceInfo(deviceType, model, LogitechDeviceCaps.PerKeyRGB, imageBasePath, imageLayout, layoutPath)); + device.Initialize(); + devices.Add(device); + } } + catch { if (throwExceptions) throw; } - if (DeviceChecker.IsPerDeviceDeviceConnected) + try { - (string model, RGBDeviceType deviceType, int _, string imageBasePath, string imageLayout, string layoutPath) = DeviceChecker.PerDeviceDeviceData; - LogitechRGBDevice device = new LogitechPerDeviceRGBDevice(new LogitechRGBDeviceInfo(deviceType, model, LogitechDeviceCaps.DeviceRGB, imageBasePath, imageLayout, layoutPath)); - device.Initialize(); - devices.Add(device); + if (DeviceChecker.IsPerDeviceDeviceConnected) + { + (string model, RGBDeviceType deviceType, int _, string imageBasePath, string imageLayout, string layoutPath) = DeviceChecker.PerDeviceDeviceData; + LogitechRGBDevice device = new LogitechPerDeviceRGBDevice(new LogitechRGBDeviceInfo(deviceType, model, LogitechDeviceCaps.DeviceRGB, imageBasePath, imageLayout, layoutPath)); + device.Initialize(); + devices.Add(device); + } } + catch { if (throwExceptions) throw; } Devices = new ReadOnlyCollection(devices); + IsInitialized = true; } catch { if (throwExceptions) throw; - else - return false; + return false; } - IsInitialized = true; - return true; } diff --git a/RGB.NET.Devices.Msi/MsiDeviceProvider.cs b/RGB.NET.Devices.Msi/MsiDeviceProvider.cs index 1d69325..36e2eff 100644 --- a/RGB.NET.Devices.Msi/MsiDeviceProvider.cs +++ b/RGB.NET.Devices.Msi/MsiDeviceProvider.cs @@ -100,21 +100,23 @@ namespace RGB.NET.Devices.Msi for (int i = 0; i < deviceTypes.Length; i++) { - //TODO DarthAffe 11.11.2017: What is this deviceType? Find someone to try that out + try + { + //TODO DarthAffe 11.11.2017: What is this deviceType? Find someone to try that out + } + catch { if (throwExceptions) throw; } } Devices = new ReadOnlyCollection(devices); + IsInitialized = true; } catch { if (throwExceptions) throw; - else - return false; + return false; } - IsInitialized = true; - return true; } diff --git a/RGB.NET.Devices.Novation/NovationDeviceProvider.cs b/RGB.NET.Devices.Novation/NovationDeviceProvider.cs index cde7c45..c1297ff 100644 --- a/RGB.NET.Devices.Novation/NovationDeviceProvider.cs +++ b/RGB.NET.Devices.Novation/NovationDeviceProvider.cs @@ -66,15 +66,16 @@ namespace RGB.NET.Devices.Novation { IList devices = new List(); - try + for (int index = 0; index < OutputDeviceBase.DeviceCount; index++) { - for (int index = 0; index < OutputDeviceBase.DeviceCount; index++) + try { MidiOutCaps outCaps = OutputDeviceBase.GetDeviceCapabilities(index); if (outCaps.name == null) continue; - NovationDevices? deviceId = (NovationDevices?)Enum.GetValues(typeof(NovationDevices)).Cast() - .FirstOrDefault(x => string.Equals(x.GetDeviceId(), outCaps.name, StringComparison.OrdinalIgnoreCase)); + NovationDevices? deviceId = (NovationDevices?)Enum.GetValues(typeof(NovationDevices)) + .Cast() + .FirstOrDefault(x => string.Equals(x.GetDeviceId(), outCaps.name, StringComparison.OrdinalIgnoreCase)); if (deviceId == null) continue; @@ -82,25 +83,19 @@ namespace RGB.NET.Devices.Novation device.Initialize(); devices.Add(device); } - } - catch - { - if (throwExceptions) - throw; + catch { if (throwExceptions) throw; } } Devices = new ReadOnlyCollection(devices); + IsInitialized = true; } catch { if (throwExceptions) throw; - else - return false; + return false; } - IsInitialized = true; - return true; }