private void AddSpecialParts(ICorsairRGBDevice device)
 {
     if (device.DeviceInfo.Model.Equals("K95 RGB Platinum", StringComparison.OrdinalIgnoreCase))
     {
         device.AddSpecialDevicePart(new LightbarSpecialPart(device));
     }
 }
Esempio n. 2
0
        /// <inheritdoc />
        /// <exception cref="RGBDeviceException">Thrown if the SDK is already initialized or if the SDK is not compatible to CUE.</exception>
        /// <exception cref="CUEException">Thrown if the CUE-SDK provides an error.</exception>
        public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false)
        {
            IsInitialized = false;

            try
            {
                UpdateTrigger?.Stop();

                _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 <IRGBDevice> devices = new List <IRGBDevice>();
                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
                        }
                        ICorsairRGBDevice device = GetRGBDevice(info, i, nativeDeviceInfo);
                        if ((device == null) || !loadFilter.HasFlag(device.DeviceInfo.DeviceType))
                        {
                            continue;
                        }

                        device.Initialize(_updateQueue);
                        AddSpecialParts(device);

                        error = LastError;
                        if (error != CorsairError.Success)
                        {
                            throw new CUEException(error);
                        }

                        devices.Add(device);
                    }
                    catch { if (throwExceptions)
                            {
                                throw;
                            }
                    }
                }

                UpdateTrigger?.Start();

                Devices       = new ReadOnlyCollection <IRGBDevice>(devices);
                IsInitialized = true;
            }
            catch
            {
                Reset();
                if (throwExceptions)
                {
                    throw;
                }
                return(false);
            }

            return(true);
        }