// ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedMember.Global using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; using RGB.NET.Core; using RGB.NET.Devices.Msi.Exceptions; using RGB.NET.Devices.Msi.Native; namespace RGB.NET.Devices.Msi { /// /// /// Represents a device provider responsible for Cooler Master devices. /// public class MsiDeviceProvider : IRGBDeviceProvider { #region Properties & Fields private static MsiDeviceProvider _instance; /// /// Gets the singleton instance. /// public static MsiDeviceProvider Instance => _instance ?? new MsiDeviceProvider(); /// /// Gets a modifiable list of paths used to find the native SDK-dlls for x86 applications. /// The first match will be used. /// public static List PossibleX86NativePaths { get; } = new List { "x86/MysticLight_SDK.dll" }; /// /// Gets a modifiable list of paths used to find the native SDK-dlls for x64 applications. /// The first match will be used. /// public static List PossibleX64NativePaths { get; } = new List { "x64/MysticLight_SDK.dll" }; /// /// /// Indicates if the SDK is initialized and ready to use. /// public bool IsInitialized { get; private set; } /// /// Gets the loaded architecture (x64/x86). /// public string LoadedArchitecture => _MsiSDK.LoadedArchitecture; /// /// /// Gets whether the application has exclusive access to the SDK or not. /// public bool HasExclusiveAccess { get; private set; } /// public IEnumerable Devices { get; private set; } /// /// Gets or sets a function to get the culture for a specific device. /// public Func GetCulture { get; set; } = CultureHelper.GetCurrentCulture; #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// Thrown if this constructor is called even if there is already an instance of this class. public MsiDeviceProvider() { if (_instance != null) throw new InvalidOperationException($"There can be only one instanc of type {nameof(MsiDeviceProvider)}"); _instance = this; } #endregion #region Methods /// public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false) { IsInitialized = false; try { _MsiSDK.Reload(); IList devices = new List(); int errorCode; if ((errorCode = _MsiSDK.Initialize()) != 0) ThrowMsiError(errorCode); if ((errorCode = _MsiSDK.GetDeviceInfo(out string[] deviceTypes, out int[] ledCounts)) != 0) ThrowMsiError(errorCode); for (int i = 0; i < deviceTypes.Length; i++) { 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; return false; } return true; } private void ThrowMsiError(int errorCode) => throw new MysticLightException(errorCode, _MsiSDK.GetErrorMessage(errorCode)); /// public void ResetDevices() { //TODO DarthAffe 11.11.2017: Implement } #endregion } }