From cdace494bc02de0d0c47cbdc1a1f3609ca1f6d41 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Mon, 29 Jan 2018 12:41:43 +0100 Subject: [PATCH] Changed device-providers to be disposable --- RGB.NET.Core/Devices/IRGBDeviceProvider.cs | 5 +++-- RGB.NET.Core/RGBSurface.cs | 4 ++++ RGB.NET.Devices.Asus/AsusDeviceProvider.cs | 4 ++++ .../CoolerMasterDeviceProvider.cs | 21 +++++++++++++++---- .../CorsairDeviceProvider.cs | 9 ++++---- RGB.NET.Devices.Debug/DebugDeviceProvider.cs | 11 ++++++++-- .../LogitechDeviceProvider.cs | 8 +++---- RGB.NET.Devices.Msi/MsiDeviceProvider.cs | 4 ++++ .../NovationDeviceProvider.cs | 4 ++++ RGB.NET.Devices.Razer/RazerDeviceProvider.cs | 8 +++---- 10 files changed, 58 insertions(+), 20 deletions(-) diff --git a/RGB.NET.Core/Devices/IRGBDeviceProvider.cs b/RGB.NET.Core/Devices/IRGBDeviceProvider.cs index 475ab92..dca3645 100644 --- a/RGB.NET.Core/Devices/IRGBDeviceProvider.cs +++ b/RGB.NET.Core/Devices/IRGBDeviceProvider.cs @@ -1,11 +1,12 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace RGB.NET.Core { /// /// Represents a generic device provider. /// - public interface IRGBDeviceProvider + public interface IRGBDeviceProvider : IDisposable { #region Properties & Fields diff --git a/RGB.NET.Core/RGBSurface.cs b/RGB.NET.Core/RGBSurface.cs index 354f248..36e5f3f 100644 --- a/RGB.NET.Core/RGBSurface.cs +++ b/RGB.NET.Core/RGBSurface.cs @@ -112,6 +112,10 @@ namespace RGB.NET.Core try { device.Dispose(); } catch { /* We do what we can */ } + foreach (IRGBDeviceProvider deviceProvider in _deviceProvider) + try { deviceProvider.Dispose(); } + catch { /* We do what we can */ } + _ledGroups.Clear(); _devices = null; _deviceProvider = null; diff --git a/RGB.NET.Devices.Asus/AsusDeviceProvider.cs b/RGB.NET.Devices.Asus/AsusDeviceProvider.cs index 3f8cae2..d485023 100644 --- a/RGB.NET.Devices.Asus/AsusDeviceProvider.cs +++ b/RGB.NET.Devices.Asus/AsusDeviceProvider.cs @@ -253,6 +253,10 @@ namespace RGB.NET.Devices.Asus } } + /// + public void Dispose() + { } + #endregion } } diff --git a/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs b/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs index 23bae1d..9537451 100644 --- a/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs +++ b/RGB.NET.Devices.CoolerMaster/CoolerMasterDeviceProvider.cs @@ -144,19 +144,32 @@ namespace RGB.NET.Devices.CoolerMaster public void ResetDevices() { if (IsInitialized) - try + foreach (IRGBDevice device in Devices) { - foreach (IRGBDevice device in Devices) + try { CoolerMasterRGBDeviceInfo deviceInfo = (CoolerMasterRGBDeviceInfo)device.DeviceInfo; _CoolerMasterSDK.SetControlDevice(deviceInfo.DeviceIndex); _CoolerMasterSDK.EnableLedControl(false); _CoolerMasterSDK.EnableLedControl(true); } + catch {/* shit happens */} } - catch + } + + /// + public void Dispose() + { + if (IsInitialized) + foreach (IRGBDevice device in Devices) { - // shit happens ... + try + { + CoolerMasterRGBDeviceInfo deviceInfo = (CoolerMasterRGBDeviceInfo)device.DeviceInfo; + _CoolerMasterSDK.SetControlDevice(deviceInfo.DeviceIndex); + _CoolerMasterSDK.EnableLedControl(false); + } + catch {/* shit happens */} } } diff --git a/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs b/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs index e0f7413..8193a89 100644 --- a/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs +++ b/RGB.NET.Devices.Corsair/CorsairDeviceProvider.cs @@ -195,10 +195,7 @@ namespace RGB.NET.Devices.Corsair { _CUESDK.Reload(); } - catch - { - // shit happens ... - } + catch {/* shit happens */} } private void Reset() @@ -209,6 +206,10 @@ namespace RGB.NET.Devices.Corsair IsInitialized = false; } + /// + public void Dispose() + { } + #endregion } } diff --git a/RGB.NET.Devices.Debug/DebugDeviceProvider.cs b/RGB.NET.Devices.Debug/DebugDeviceProvider.cs index 8e802f8..ca6561e 100644 --- a/RGB.NET.Devices.Debug/DebugDeviceProvider.cs +++ b/RGB.NET.Devices.Debug/DebugDeviceProvider.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using RGB.NET.Core; namespace RGB.NET.Devices.Debug @@ -73,7 +74,6 @@ namespace RGB.NET.Devices.Debug try { HasExclusiveAccess = exclusiveAccessIfPossible; - IsInitialized = true; List devices = new List(); foreach ((string layout, string imageLayout, Func> syncBackFunc, Action> updateLedsAction) in _fakeDeviceDefinitions) @@ -83,7 +83,8 @@ namespace RGB.NET.Devices.Debug devices.Add(device); } - Devices = devices; + Devices = new ReadOnlyCollection(devices); + IsInitialized = true; return true; } @@ -98,6 +99,12 @@ namespace RGB.NET.Devices.Debug public void ResetDevices() { } + /// + public void Dispose() + { + _fakeDeviceDefinitions.Clear(); + } + #endregion } } diff --git a/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs b/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs index 3d6beb0..80575c7 100644 --- a/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs +++ b/RGB.NET.Devices.Logitech/LogitechDeviceProvider.cs @@ -140,10 +140,10 @@ namespace RGB.NET.Devices.Logitech } /// - public void ResetDevices() - { - _LogitechGSDK.LogiLedRestoreLighting(); - } + public void ResetDevices() => _LogitechGSDK.LogiLedRestoreLighting(); + + /// + public void Dispose() => _LogitechGSDK.LogiLedRestoreLighting(); #endregion } diff --git a/RGB.NET.Devices.Msi/MsiDeviceProvider.cs b/RGB.NET.Devices.Msi/MsiDeviceProvider.cs index c1685d5..2eba9a0 100644 --- a/RGB.NET.Devices.Msi/MsiDeviceProvider.cs +++ b/RGB.NET.Devices.Msi/MsiDeviceProvider.cs @@ -128,6 +128,10 @@ namespace RGB.NET.Devices.Msi //TODO DarthAffe 11.11.2017: Implement } + /// + public void Dispose() + { } + #endregion } } diff --git a/RGB.NET.Devices.Novation/NovationDeviceProvider.cs b/RGB.NET.Devices.Novation/NovationDeviceProvider.cs index 3a9757e..ce2b87b 100644 --- a/RGB.NET.Devices.Novation/NovationDeviceProvider.cs +++ b/RGB.NET.Devices.Novation/NovationDeviceProvider.cs @@ -110,6 +110,10 @@ namespace RGB.NET.Devices.Novation } } + /// + public void Dispose() + { } + #endregion } } diff --git a/RGB.NET.Devices.Razer/RazerDeviceProvider.cs b/RGB.NET.Devices.Razer/RazerDeviceProvider.cs index 1114dd0..85615aa 100644 --- a/RGB.NET.Devices.Razer/RazerDeviceProvider.cs +++ b/RGB.NET.Devices.Razer/RazerDeviceProvider.cs @@ -206,13 +206,13 @@ namespace RGB.NET.Devices.Razer private void TryUnInit() { - try - { - _RazerSDK.UnInit(); - } + try { _RazerSDK.UnInit(); } catch { /* We tried our best */ } } + /// + public void Dispose() => TryUnInit(); + #endregion } }