-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathCorsairRGBDeviceInfo.cs
More file actions
130 lines (109 loc) · 4.37 KB
/
CorsairRGBDeviceInfo.cs
File metadata and controls
130 lines (109 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using RGB.NET.Core;
using RGB.NET.Devices.Corsair.Native;
namespace RGB.NET.Devices.Corsair;
/// <inheritdoc />
/// <summary>
/// Represents a generic information for a Corsair-<see cref="T:RGB.NET.Core.IRGBDevice" />.
/// </summary>
public class CorsairRGBDeviceInfo : IRGBDeviceInfo
{
#region Properties & Fields
/// <summary>
/// Gets the corsair specific device type.
/// </summary>
public CorsairDeviceType CorsairDeviceType { get; }
/// <inheritdoc />
public RGBDeviceType DeviceType { get; }
/// <inheritdoc />
public string DeviceName { get; }
/// <inheritdoc />
public string Manufacturer => "Corsair";
/// <inheritdoc />
public string Model { get; }
/// <summary>
/// Returns the unique ID provided by the Corsair-SDK.
/// Returns string.Empty for Custom devices.
/// </summary>
public string DeviceId { get; init; }
/// <inheritdoc />
public object? LayoutMetadata { get; set; }
/// <summary>
/// Gets the amount of LEDs this device contains.
/// </summary>
public int LedCount { get; }
/// <summary>
/// Gets the offset used to access the LEDs of this device.
/// </summary>
internal int LedOffset { get; }
#endregion
#region Constructors
/// <summary>
/// Internal constructor of managed <see cref="CorsairRGBDeviceInfo"/>.
/// </summary>
/// <param name="deviceIndex">The index of the <see cref="CorsairRGBDevice{TDeviceInfo}"/>.</param>
/// <param name="deviceType">The type of the <see cref="IRGBDevice"/>.</param>
/// <param name="nativeInfo">The native <see cref="_CorsairDeviceInfo" />-struct</param>
internal CorsairRGBDeviceInfo(RGBDeviceType deviceType, _CorsairDeviceInfo nativeInfo, int ledCount, int ledOffset)
{
this.DeviceType = deviceType;
this.CorsairDeviceType = nativeInfo.type;
this.Model = nativeInfo.model == null ? string.Empty : Regex.Replace(nativeInfo.model ?? string.Empty, " ?DEMO", string.Empty, RegexOptions.IgnoreCase);
this.DeviceId = nativeInfo.id ?? string.Empty;
this.LedCount = ledCount;
this.LedOffset = ledOffset;
if (nativeInfo.id == null) // this device is 99% unpluggable
{
DeviceName = IdGenerator.MakeUnique(typeof(CorsairDeviceProvider), Manufacturer + " " + Model);
}
else
{
DeviceName = Manufacturer + " " + Model + " #" + HashAndShorten(DeviceId);
}
}
/// <summary>
/// Internal constructor of managed <see cref="CorsairRGBDeviceInfo"/>.
/// </summary>
/// <param name="deviceIndex">The index of the <see cref="CorsairRGBDevice{TDeviceInfo}"/>.</param>
/// <param name="deviceType">The type of the <see cref="IRGBDevice"/>.</param>
/// <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>
internal CorsairRGBDeviceInfo(RGBDeviceType deviceType, _CorsairDeviceInfo nativeInfo, int ledCount, int ledOffset, string modelName)
{
this.DeviceType = deviceType;
this.CorsairDeviceType = nativeInfo.type;
this.Model = modelName;
this.DeviceId = nativeInfo.id ?? string.Empty;
this.LedCount = ledCount;
this.LedOffset = ledOffset;
if (nativeInfo.id == null)
{
DeviceName = IdGenerator.MakeUnique(typeof(CorsairDeviceProvider),Manufacturer + " " + Model) + " " + ledOffset;;
}
else
{
DeviceName = Manufacturer + " " + Model + " #" + HashAndShorten(DeviceId) + " " + ledOffset;
}
}
#endregion
#region Methods
private static string HashAndShorten(string input)
{
using SHA256 sha256Hash = SHA256.Create();
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
// Take the first 4 bytes of the hash
byte[] shortenedBytes = new byte[4];
Array.Copy(bytes, shortenedBytes, 4);
// Convert the bytes to a string
StringBuilder shortenedHash = new();
foreach (byte b in shortenedBytes)
{
shortenedHash.Append(b.ToString("X2"));
}
return shortenedHash.ToString();
}
#endregion
}