// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using RGB.NET.Core;
using RGB.NET.Devices.Razer.Native;
namespace RGB.NET.Devices.Razer
{
///
///
/// Represents a device provider responsible for razer devices.
///
public class RazerDeviceProvider : IRGBDeviceProvider
{
#region Properties & Fields
private static RazerDeviceProvider _instance;
///
/// Gets the singleton instance.
///
public static RazerDeviceProvider Instance => _instance ?? new RazerDeviceProvider();
///
/// 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/RzChromaSDK.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/RzChromaSDK.dll", "x64/RzChromaSDK64.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 => _RazerSDK.LoadedArchitecture;
///
///
/// Gets whether the application has exclusive access to the SDK or not.
///
public bool HasExclusiveAccess => false;
///
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;
///
/// Forces to load the devices represented by the emulator even if they aren't reported to exist.
///
public bool LoadEmulatorDevices { get; set; } = false;
#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 RazerDeviceProvider()
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(RazerDeviceProvider)}");
_instance = this;
}
#endregion
#region Methods
///
public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false)
{
if (IsInitialized)
TryUnInit();
IsInitialized = false;
try
{
_RazerSDK.Reload();
RazerError error;
if ((error = _RazerSDK.Init()) != RazerError.Success)
ThrowRazerError(error);
IList devices = new List();
if (loadFilter.HasFlag(RGBDeviceType.Keyboard))
foreach ((Guid guid, string model) in Razer.Devices.KEYBOARDS)
try
{
if (((_RazerSDK.QueryDevice(guid, out _DeviceInfo deviceInfo) != RazerError.Success) || (deviceInfo.Connected < 1))
&& (!LoadEmulatorDevices || (Razer.Devices.KEYBOARDS.FirstOrDefault().guid != guid))) continue;
RazerKeyboardRGBDevice device = new RazerKeyboardRGBDevice(new RazerKeyboardRGBDeviceInfo(guid, model, GetCulture()));
device.Initialize();
devices.Add(device);
}
catch { if (throwExceptions) throw; }
if (loadFilter.HasFlag(RGBDeviceType.Mouse))
foreach ((Guid guid, string model) in Razer.Devices.MICE)
try
{
if (((_RazerSDK.QueryDevice(guid, out _DeviceInfo deviceInfo) != RazerError.Success) || (deviceInfo.Connected < 1))
&& (!LoadEmulatorDevices || (Razer.Devices.MICE.FirstOrDefault().guid != guid))) continue;
RazerMouseRGBDevice device = new RazerMouseRGBDevice(new RazerMouseRGBDeviceInfo(guid, model));
device.Initialize();
devices.Add(device);
}
catch { if (throwExceptions) throw; }
if (loadFilter.HasFlag(RGBDeviceType.Headset))
foreach ((Guid guid, string model) in Razer.Devices.HEADSETS)
try
{
if (((_RazerSDK.QueryDevice(guid, out _DeviceInfo deviceInfo) != RazerError.Success) || (deviceInfo.Connected < 1))
&& (!LoadEmulatorDevices || (Razer.Devices.HEADSETS.FirstOrDefault().guid != guid))) continue;
RazerHeadsetRGBDevice device = new RazerHeadsetRGBDevice(new RazerHeadsetRGBDeviceInfo(guid, model));
device.Initialize();
devices.Add(device);
}
catch { if (throwExceptions) throw; }
if (loadFilter.HasFlag(RGBDeviceType.Mousepad))
foreach ((Guid guid, string model) in Razer.Devices.MOUSEMATS)
try
{
if (((_RazerSDK.QueryDevice(guid, out _DeviceInfo deviceInfo) != RazerError.Success) || (deviceInfo.Connected < 1))
&& (!LoadEmulatorDevices || (Razer.Devices.MOUSEMATS.FirstOrDefault().guid != guid))) continue;
RazerMousepadRGBDevice device = new RazerMousepadRGBDevice(new RazerMousepadRGBDeviceInfo(guid, model));
device.Initialize();
devices.Add(device);
}
catch { if (throwExceptions) throw; }
if (loadFilter.HasFlag(RGBDeviceType.Keypad))
foreach ((Guid guid, string model) in Razer.Devices.KEYPADS)
try
{
if (((_RazerSDK.QueryDevice(guid, out _DeviceInfo deviceInfo) != RazerError.Success) || (deviceInfo.Connected < 1))
&& (!LoadEmulatorDevices || (Razer.Devices.KEYPADS.FirstOrDefault().guid != guid))) continue;
RazerKeypadRGBDevice device = new RazerKeypadRGBDevice(new RazerKeypadRGBDeviceInfo(guid, model));
device.Initialize();
devices.Add(device);
}
catch { if (throwExceptions) throw; }
if (loadFilter.HasFlag(RGBDeviceType.Keyboard))
foreach ((Guid guid, string model) in Razer.Devices.CHROMALINKS)
try
{
if (((_RazerSDK.QueryDevice(guid, out _DeviceInfo deviceInfo) != RazerError.Success) || (deviceInfo.Connected < 1))
&& (!LoadEmulatorDevices || (Razer.Devices.CHROMALINKS.FirstOrDefault().guid != guid))) continue;
RazerChromaLinkRGBDevice device = new RazerChromaLinkRGBDevice(new RazerChromaLinkRGBDeviceInfo(guid, model));
device.Initialize();
devices.Add(device);
}
catch { if (throwExceptions) throw; }
Devices = new ReadOnlyCollection(devices);
IsInitialized = true;
}
catch
{
TryUnInit();
if (throwExceptions) throw;
return false;
}
return true;
}
///
public void ResetDevices()
{
foreach (IRGBDevice device in Devices)
((IRazerRGBDevice)device).Reset();
}
private void ThrowRazerError(RazerError errorCode) => throw new RazerException(errorCode);
private void TryUnInit()
{
try
{
_RazerSDK.UnInit();
}
catch { /* We tried our best */ }
}
#endregion
}
}