1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-12 09:38:31 +00:00

Merge pull request #191 from DarthAffe/UniqueDeviceNames

Added Helper to create unique device names for all devices
This commit is contained in:
DarthAffe 2021-03-26 23:38:30 +01:00 committed by GitHub
commit eb3b91d8d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 74 additions and 72 deletions

View File

@ -0,0 +1,33 @@
using System.Collections.Generic;
namespace RGB.NET.Core
{
public static class DeviceHelper
{
#region Properties & Fields
private static readonly Dictionary<string, int> MODEL_COUNTER = new();
#endregion
#region Methods
public static string CreateDeviceName(string manufacturer, string model) => $"{manufacturer} {MakeUnique(model)}";
public static string MakeUnique(string model)
{
if (MODEL_COUNTER.TryGetValue(model, out int _))
{
int counter = ++MODEL_COUNTER[model];
return $"{model} {counter}";
}
else
{
MODEL_COUNTER.Add(model, 1);
return model;
}
}
#endregion
}
}

View File

@ -45,7 +45,7 @@ namespace RGB.NET.Devices.Asus
this.Model = model ?? device.Name;
this.Manufacturer = manufacturer;
DeviceName = $"{Manufacturer} {Model}";
DeviceName = DeviceHelper.CreateDeviceName(Manufacturer, Model);
}
#endregion

View File

@ -45,7 +45,7 @@ namespace RGB.NET.Devices.CoolerMaster
this.DeviceIndex = deviceIndex;
Model = deviceIndex.GetDescription() ?? "Unknown";
DeviceName = $"{Manufacturer} {Model}";
DeviceName = DeviceHelper.CreateDeviceName(Manufacturer, Model);
}
#endregion

View File

@ -87,12 +87,11 @@ namespace RGB.NET.Devices.Corsair
protected override IEnumerable<IRGBDevice> LoadDevices()
{
Dictionary<string, int> modelCounter = new();
int deviceCount = _CUESDK.CorsairGetDeviceCount();
for (int i = 0; i < deviceCount; i++)
{
_CorsairDeviceInfo nativeDeviceInfo = (_CorsairDeviceInfo)Marshal.PtrToStructure(_CUESDK.CorsairGetDeviceInfo(i), typeof(_CorsairDeviceInfo))!;
CorsairRGBDeviceInfo info = new(i, RGBDeviceType.Unknown, nativeDeviceInfo, modelCounter);
CorsairRGBDeviceInfo info = new(i, RGBDeviceType.Unknown, nativeDeviceInfo);
if (!info.CapsMask.HasFlag(CorsairDeviceCaps.Lighting))
continue; // Everything that doesn't support lighting control is useless
@ -100,27 +99,27 @@ namespace RGB.NET.Devices.Corsair
switch (info.CorsairDeviceType)
{
case CorsairDeviceType.Keyboard:
yield return new CorsairKeyboardRGBDevice(new CorsairKeyboardRGBDeviceInfo(i, nativeDeviceInfo, modelCounter), updateQueue);
yield return new CorsairKeyboardRGBDevice(new CorsairKeyboardRGBDeviceInfo(i, nativeDeviceInfo), updateQueue);
break;
case CorsairDeviceType.Mouse:
yield return new CorsairMouseRGBDevice(new CorsairMouseRGBDeviceInfo(i, nativeDeviceInfo, modelCounter), updateQueue);
yield return new CorsairMouseRGBDevice(new CorsairMouseRGBDeviceInfo(i, nativeDeviceInfo), updateQueue);
break;
case CorsairDeviceType.Headset:
yield return new CorsairHeadsetRGBDevice(new CorsairHeadsetRGBDeviceInfo(i, nativeDeviceInfo, modelCounter), updateQueue);
yield return new CorsairHeadsetRGBDevice(new CorsairHeadsetRGBDeviceInfo(i, nativeDeviceInfo), updateQueue);
break;
case CorsairDeviceType.Mousepad:
yield return new CorsairMousepadRGBDevice(new CorsairMousepadRGBDeviceInfo(i, nativeDeviceInfo, modelCounter), updateQueue);
yield return new CorsairMousepadRGBDevice(new CorsairMousepadRGBDeviceInfo(i, nativeDeviceInfo), updateQueue);
break;
case CorsairDeviceType.HeadsetStand:
yield return new CorsairHeadsetStandRGBDevice(new CorsairHeadsetStandRGBDeviceInfo(i, nativeDeviceInfo, modelCounter), updateQueue);
yield return new CorsairHeadsetStandRGBDevice(new CorsairHeadsetStandRGBDeviceInfo(i, nativeDeviceInfo), updateQueue);
break;
case CorsairDeviceType.MemoryModule:
yield return new CorsairMemoryRGBDevice(new CorsairMemoryRGBDeviceInfo(i, nativeDeviceInfo, modelCounter), updateQueue);
yield return new CorsairMemoryRGBDevice(new CorsairMemoryRGBDeviceInfo(i, nativeDeviceInfo), updateQueue);
break;
case CorsairDeviceType.Cooler:
@ -145,7 +144,7 @@ namespace RGB.NET.Devices.Corsair
{
_CorsairChannelDeviceInfo channelDeviceInfo = (_CorsairChannelDeviceInfo)Marshal.PtrToStructure(channelDeviceInfoPtr, typeof(_CorsairChannelDeviceInfo))!;
yield return new CorsairCustomRGBDevice(new CorsairCustomRGBDeviceInfo(info, nativeDeviceInfo, channelDeviceInfo, referenceLed, modelCounter), updateQueue);
yield return new CorsairCustomRGBDevice(new CorsairCustomRGBDeviceInfo(info, nativeDeviceInfo, channelDeviceInfo, referenceLed), updateQueue);
referenceLed += channelDeviceInfo.deviceLedCount;
channelDeviceInfoPtr = new IntPtr(channelDeviceInfoPtr.ToInt64() + channelDeviceInfoStructSize);

View File

@ -2,7 +2,6 @@
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using RGB.NET.Core;
using RGB.NET.Devices.Corsair.Native;
@ -33,11 +32,8 @@ namespace RGB.NET.Devices.Corsair
/// <param name="channelDeviceInfo">The native <see cref="T:RGB.NET.Devices.Corsair.Native._CorsairChannelDeviceInfo"/> representing this device.</param>
/// <param name="referenceCorsairLed">The id of the first led of this device.</param>
/// <param name="modelCounter">A dictionary containing counters to create unique names for equal devices models.</param>
internal CorsairCustomRGBDeviceInfo(CorsairRGBDeviceInfo info, _CorsairDeviceInfo nativeInfo,
_CorsairChannelDeviceInfo channelDeviceInfo,
CorsairLedId referenceCorsairLed, Dictionary<string, int> modelCounter)
: base(info.CorsairDeviceIndex, GetDeviceType(channelDeviceInfo.type), nativeInfo,
GetModelName(info, channelDeviceInfo), modelCounter)
internal CorsairCustomRGBDeviceInfo(CorsairRGBDeviceInfo info, _CorsairDeviceInfo nativeInfo, _CorsairChannelDeviceInfo channelDeviceInfo, CorsairLedId referenceCorsairLed)
: base(info.CorsairDeviceIndex, GetDeviceType(channelDeviceInfo.type), nativeInfo, GetModelName(info, channelDeviceInfo))
{
this.ReferenceCorsairLed = referenceCorsairLed;

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using RGB.NET.Core;
@ -55,7 +54,7 @@ namespace RGB.NET.Devices.Corsair
/// <param name="deviceType">The type of the <see cref="IRGBDevice"/>.</param>
/// <param name="nativeInfo">The native <see cref="_CorsairDeviceInfo" />-struct</param>
/// <param name="modelCounter">A dictionary containing counters to create unique names for equal devices models.</param>
internal CorsairRGBDeviceInfo(int deviceIndex, RGBDeviceType deviceType, _CorsairDeviceInfo nativeInfo, Dictionary<string, int> modelCounter)
internal CorsairRGBDeviceInfo(int deviceIndex, RGBDeviceType deviceType, _CorsairDeviceInfo nativeInfo)
{
this.CorsairDeviceIndex = deviceIndex;
this.DeviceType = deviceType;
@ -63,7 +62,7 @@ namespace RGB.NET.Devices.Corsair
this.Model = nativeInfo.model == IntPtr.Zero ? string.Empty : Regex.Replace(Marshal.PtrToStringAnsi(nativeInfo.model) ?? string.Empty, " ?DEMO", string.Empty, RegexOptions.IgnoreCase);
this.CapsMask = (CorsairDeviceCaps)nativeInfo.capsMask;
DeviceName = GetUniqueModelName(modelCounter);
DeviceName = DeviceHelper.CreateDeviceName(Manufacturer, Model);
}
/// <summary>
@ -74,7 +73,7 @@ namespace RGB.NET.Devices.Corsair
/// <param name="nativeInfo">The native <see cref="_CorsairDeviceInfo" />-struct</param>
/// <param name="modelName">The name of the device-model (overwrites the one provided with the device info).</param>
/// <param name="modelCounter">A dictionary containing counters to create unique names for equal devices models.</param>
internal CorsairRGBDeviceInfo(int deviceIndex, RGBDeviceType deviceType, _CorsairDeviceInfo nativeInfo, string modelName, Dictionary<string, int> modelCounter)
internal CorsairRGBDeviceInfo(int deviceIndex, RGBDeviceType deviceType, _CorsairDeviceInfo nativeInfo, string modelName)
{
this.CorsairDeviceIndex = deviceIndex;
this.DeviceType = deviceType;
@ -82,26 +81,13 @@ namespace RGB.NET.Devices.Corsair
this.Model = modelName;
this.CapsMask = (CorsairDeviceCaps)nativeInfo.capsMask;
DeviceName = GetUniqueModelName(modelCounter);
DeviceName = DeviceHelper.CreateDeviceName(Manufacturer, Model);
}
#endregion
#region Methods
private string GetUniqueModelName(Dictionary<string, int> modelCounter)
{
if (modelCounter.TryGetValue(Model, out int _))
{
int counter = ++modelCounter[Model];
return $"{Manufacturer} {Model} {counter}";
}
else
{
modelCounter.Add(Model, 1);
return $"{Manufacturer} {Model}";
}
}
#endregion
}

View File

@ -1,5 +1,4 @@
using System.Collections.Generic;
using RGB.NET.Core;
using RGB.NET.Core;
using RGB.NET.Devices.Corsair.Native;
namespace RGB.NET.Devices.Corsair
@ -18,9 +17,8 @@ namespace RGB.NET.Devices.Corsair
/// </summary>
/// <param name="deviceIndex">The index of the <see cref="T:RGB.NET.Devices.Corsair.CorsairHeadsetRGBDevice" />.</param>
/// <param name="nativeInfo">The native <see cref="T:RGB.NET.Devices.Corsair.Native._CorsairDeviceInfo" />-struct</param>
/// <param name="modelCounter">A dictionary containing counters to create unique names for equal devices models.</param>
internal CorsairHeadsetRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo, Dictionary<string, int> modelCounter)
: base(deviceIndex, RGBDeviceType.Headset, nativeInfo, modelCounter)
internal CorsairHeadsetRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo)
: base(deviceIndex, RGBDeviceType.Headset, nativeInfo)
{ }
#endregion

View File

@ -1,5 +1,4 @@
using System.Collections.Generic;
using RGB.NET.Core;
using RGB.NET.Core;
using RGB.NET.Devices.Corsair.Native;
namespace RGB.NET.Devices.Corsair
@ -18,9 +17,8 @@ namespace RGB.NET.Devices.Corsair
/// </summary>
/// <param name="deviceIndex">The index of the <see cref="T:RGB.NET.Devices.Corsair.CorsairHeadsetStandRGBDevice" />.</param>
/// <param name="nativeInfo">The native <see cref="T:RGB.NET.Devices.Corsair.Native._CorsairDeviceInfo" />-struct</param>
/// <param name="modelCounter">A dictionary containing counters to create unique names for equal devices models.</param>
internal CorsairHeadsetStandRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo, Dictionary<string, int> modelCounter)
: base(deviceIndex, RGBDeviceType.HeadsetStand, nativeInfo, modelCounter)
internal CorsairHeadsetStandRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo)
: base(deviceIndex, RGBDeviceType.HeadsetStand, nativeInfo)
{ }
#endregion

View File

@ -1,7 +1,6 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System.Collections.Generic;
using RGB.NET.Core;
using RGB.NET.Devices.Corsair.Native;
@ -37,9 +36,8 @@ namespace RGB.NET.Devices.Corsair
/// </summary>
/// <param name="deviceIndex">The index of the <see cref="T:RGB.NET.Devices.Corsair.CorsairKeyboardRGBDevice" />.</param>
/// <param name="nativeInfo">The native <see cref="T:RGB.NET.Devices.Corsair.Native._CorsairDeviceInfo" />-struct</param>
/// <param name="modelCounter">A dictionary containing counters to create unique names for equal devices models.</param>
internal CorsairKeyboardRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo, Dictionary<string, int> modelCounter)
: base(deviceIndex, RGBDeviceType.Keyboard, nativeInfo, modelCounter)
internal CorsairKeyboardRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo)
: base(deviceIndex, RGBDeviceType.Keyboard, nativeInfo)
{
this.PhysicalLayout = (CorsairPhysicalKeyboardLayout)nativeInfo.physicalLayout;
this.LogicalLayout = (CorsairLogicalKeyboardLayout)nativeInfo.logicalLayout;

View File

@ -1,7 +1,6 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System.Collections.Generic;
using RGB.NET.Core;
using RGB.NET.Devices.Corsair.Native;
@ -21,9 +20,8 @@ namespace RGB.NET.Devices.Corsair
/// </summary>
/// <param name="deviceIndex">The index of the <see cref="T:RGB.NET.Devices.Corsair.CorsairMemoryRGBDevice" />.</param>
/// <param name="nativeInfo">The native <see cref="T:RGB.NET.Devices.Corsair.Native._CorsairDeviceInfo" />-struct</param>
/// <param name="modelCounter">A dictionary containing counters to create unique names for equal devices models.</param>
internal CorsairMemoryRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo, Dictionary<string, int> modelCounter)
: base(deviceIndex, RGBDeviceType.DRAM, nativeInfo, modelCounter)
internal CorsairMemoryRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo)
: base(deviceIndex, RGBDeviceType.DRAM, nativeInfo)
{ }
#endregion

View File

@ -1,5 +1,4 @@
using System.Collections.Generic;
using RGB.NET.Core;
using RGB.NET.Core;
using RGB.NET.Devices.Corsair.Native;
namespace RGB.NET.Devices.Corsair
@ -27,9 +26,8 @@ namespace RGB.NET.Devices.Corsair
/// </summary>
/// <param name="deviceIndex">The index of the <see cref="T:RGB.NET.Devices.Corsair.CorsairMouseRGBDevice" />.</param>
/// <param name="nativeInfo">The native <see cref="T:RGB.NET.Devices.Corsair.Native._CorsairDeviceInfo" />-struct</param>
/// <param name="modelCounter">A dictionary containing counters to create unique names for equal devices models.</param>
internal CorsairMouseRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo, Dictionary<string, int> modelCounter)
: base(deviceIndex, RGBDeviceType.Mouse, nativeInfo, modelCounter)
internal CorsairMouseRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo)
: base(deviceIndex, RGBDeviceType.Mouse, nativeInfo)
{
this.PhysicalLayout = (CorsairPhysicalMouseLayout)nativeInfo.physicalLayout;
}

View File

@ -1,5 +1,4 @@
using System.Collections.Generic;
using RGB.NET.Core;
using RGB.NET.Core;
using RGB.NET.Devices.Corsair.Native;
namespace RGB.NET.Devices.Corsair
@ -18,9 +17,8 @@ namespace RGB.NET.Devices.Corsair
/// </summary>
/// <param name="deviceIndex">The index if the <see cref="T:RGB.NET.Devices.Corsair.CorsairMousepadRGBDevice" />.</param>
/// <param name="nativeInfo">The native <see cref="T:RGB.NET.Devices.Corsair.Native._CorsairDeviceInfo" />-struct</param>
/// <param name="modelCounter">A dictionary containing counters to create unique names for equal devices models.</param>
internal CorsairMousepadRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo, Dictionary<string, int> modelCounter)
: base(deviceIndex, RGBDeviceType.Mousepad, nativeInfo, modelCounter)
internal CorsairMousepadRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo)
: base(deviceIndex, RGBDeviceType.Mousepad, nativeInfo)
{ }
#endregion

View File

@ -77,7 +77,7 @@ namespace RGB.NET.Devices.DMX.E131
CID = cid!;
DeviceName = $"{Manufacturer} {Model}";
DeviceName = DeviceHelper.CreateDeviceName(Manufacturer, Model);
}
#endregion

View File

@ -41,7 +41,7 @@ namespace RGB.NET.Devices.Debug
this.Model = model;
this.LayoutMetadata = customData;
DeviceName = $"{Manufacturer} {Model}";
DeviceName = DeviceHelper.CreateDeviceName(Manufacturer, Model);
}
#endregion

View File

@ -54,7 +54,7 @@ namespace RGB.NET.Devices.Logitech
this.DeviceCaps = deviceCaps;
this.Zones = zones;
DeviceName = $"{Manufacturer} {Model}";
DeviceName = DeviceHelper.CreateDeviceName(Manufacturer, Model);
}
#endregion

View File

@ -48,7 +48,7 @@ namespace RGB.NET.Devices.Msi
this.Manufacturer = manufacturer;
this.Model = model;
DeviceName = $"{Manufacturer} {Model}";
DeviceName = DeviceHelper.CreateDeviceName(Manufacturer, Model);
}
#endregion

View File

@ -53,7 +53,7 @@ namespace RGB.NET.Devices.Novation
this.DeviceId = deviceId;
this.ColorCapabilities = colorCapabilities;
DeviceName = $"{Manufacturer} {Model}";
DeviceName = DeviceHelper.CreateDeviceName(Manufacturer, Model);
}
#endregion

View File

@ -46,7 +46,7 @@ namespace RGB.NET.Devices.Razer
this.EndpointType = endpointType;
this.Model = model;
DeviceName = $"{Manufacturer} {Model}";
DeviceName = DeviceHelper.CreateDeviceName(Manufacturer, Model);
}
#endregion

View File

@ -26,7 +26,7 @@ namespace RGB.NET.Devices.SteelSeries
public object? LayoutMetadata { get; set; }
public SteelSeriesDeviceType SteelSeriesDeviceType { get; }
#endregion
#region Constructors
@ -45,7 +45,7 @@ namespace RGB.NET.Devices.SteelSeries
this.Model = model;
this.SteelSeriesDeviceType = steelSeriesDeviceType;
DeviceName = $"{Manufacturer} {Model}";
DeviceName = DeviceHelper.CreateDeviceName(Manufacturer, Model);
}
#endregion

View File

@ -47,7 +47,7 @@ namespace RGB.NET.Devices.Wooting.Generic
this.DeviceIndex = deviceIndex;
Model = deviceIndex.GetDescription();
DeviceName = $"{Manufacturer} {Model}";
DeviceName = DeviceHelper.CreateDeviceName(Manufacturer, Model);
}
#endregion