Applied some C#12 stuff

This commit is contained in:
DarthAffe committed 2023-12-22 20:56:56 +01:00
1 parent 0444730aca
commit d85f1559b3
105 files changed
+329 -422

No files matched your search

@@ -38,13 +38,13 @@ public sealed class CorsairLegacyDeviceProvider : AbstractRGBDeviceProvider
/// Gets a modifiable list of paths used to find the native SDK-dlls for x86 applications.
/// The first match will be used.
/// </summary>
public static List<string> PossibleX86NativePaths { get; } = new() { "x86/CUESDK.dll", "x86/CUESDK_2019.dll", "x86/CUESDK_2017.dll", "x86/CUESDK_2015.dll", "x86/CUESDK_2013.dll" };
public static List<string> PossibleX86NativePaths { get; } = ["x86/CUESDK.dll", "x86/CUESDK_2019.dll", "x86/CUESDK_2017.dll", "x86/CUESDK_2015.dll", "x86/CUESDK_2013.dll"];
/// <summary>
/// Gets a modifiable list of paths used to find the native SDK-dlls for x64 applications.
/// The first match will be used.
/// </summary>
public static List<string> PossibleX64NativePaths { get; } = new() { "x64/CUESDK.dll", "x64/CUESDK.x64_2019.dll", "x64/CUESDK.x64_2017.dll", "x64/CUESDK_2019.dll", "x64/CUESDK_2017.dll", "x64/CUESDK_2015.dll", "x64/CUESDK_2013.dll" };
public static List<string> PossibleX64NativePaths { get; } = ["x64/CUESDK.dll", "x64/CUESDK.x64_2019.dll", "x64/CUESDK.x64_2017.dll", "x64/CUESDK_2019.dll", "x64/CUESDK_2017.dll", "x64/CUESDK_2015.dll", "x64/CUESDK_2013.dll"];
/// <summary>
/// Gets the protocol details for the current SDK-connection.
@@ -170,7 +170,7 @@ public sealed class CorsairLegacyDeviceProvider : AbstractRGBDeviceProvider
foreach (_CorsairChannelInfo channelInfo in channels)
{
int channelDeviceInfoStructSize = Marshal.SizeOf(typeof(_CorsairChannelDeviceInfo));
IntPtr channelDeviceInfoPtr = channelInfo.devices;
nint channelDeviceInfoPtr = channelInfo.devices;
for (int device = 0; (device < channelInfo.devicesCount) && (ledOffset < nativeDeviceInfo.ledsCount); device++)
{
_CorsairChannelDeviceInfo channelDeviceInfo = (_CorsairChannelDeviceInfo)Marshal.PtrToStructure(channelDeviceInfoPtr, typeof(_CorsairChannelDeviceInfo))!;
@@ -178,7 +178,7 @@ public sealed class CorsairLegacyDeviceProvider : AbstractRGBDeviceProvider
yield return new CorsairCustomRGBDevice(new CorsairCustomRGBDeviceInfo(i, nativeDeviceInfo, channelDeviceInfo, ledOffset), updateQueue);
ledOffset += channelDeviceInfo.deviceLedCount;
channelDeviceInfoPtr = new IntPtr(channelDeviceInfoPtr.ToInt64() + channelDeviceInfoStructSize);
channelDeviceInfoPtr += channelDeviceInfoStructSize;
}
}
break;
@@ -195,13 +195,13 @@ public sealed class CorsairLegacyDeviceProvider : AbstractRGBDeviceProvider
_CorsairChannelsInfo? channelsInfo = deviceInfo.channels;
if (channelsInfo == null) yield break;
IntPtr channelInfoPtr = channelsInfo.channels;
nint channelInfoPtr = channelsInfo.channels;
for (int channel = 0; channel < channelsInfo.channelsCount; channel++)
{
yield return (_CorsairChannelInfo)Marshal.PtrToStructure(channelInfoPtr, typeof(_CorsairChannelInfo))!;
int channelInfoStructSize = Marshal.SizeOf(typeof(_CorsairChannelInfo));
channelInfoPtr = new IntPtr(channelInfoPtr.ToInt64() + channelInfoStructSize);
channelInfoPtr += channelInfoStructSize;
}
}
@@ -1,7 +1,6 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Linq;
using System.Runtime.InteropServices;
using RGB.NET.Core;
@@ -24,7 +23,7 @@ public class CorsairCustomRGBDevice : CorsairRGBDevice<CorsairCustomRGBDeviceInf
/// <param name="info">The specific information provided by CUE for the custom-device.</param>
/// <param name="updateQueue">The queue used to update this device.</param>
internal CorsairCustomRGBDevice(CorsairCustomRGBDeviceInfo info, CorsairDeviceUpdateQueue updateQueue)
: base(info, new LedMapping<CorsairLedId>(), updateQueue)
: base(info, [], updateQueue)
{ }
#endregion
@@ -40,7 +39,7 @@ public class CorsairCustomRGBDevice : CorsairRGBDevice<CorsairCustomRGBDeviceInf
if (nativeLedPositions == null) return;
int structSize = Marshal.SizeOf(typeof(_CorsairLedPosition));
IntPtr ptr = new(nativeLedPositions.pLedPosition.ToInt64() + (structSize * DeviceInfo.LedOffset));
nint ptr = nativeLedPositions.pLedPosition + (structSize * DeviceInfo.LedOffset);
LedId referenceLedId = GetReferenceLed(DeviceInfo.DeviceType);
for (int i = 0; i < DeviceInfo.LedCount; i++)
@@ -49,7 +48,7 @@ public class CorsairCustomRGBDevice : CorsairRGBDevice<CorsairCustomRGBDeviceInf
_CorsairLedPosition? ledPosition = (_CorsairLedPosition?)Marshal.PtrToStructure(ptr, typeof(_CorsairLedPosition));
if (ledPosition == null)
{
ptr = new IntPtr(ptr.ToInt64() + structSize);
ptr += structSize;
continue;
}
@@ -60,7 +59,7 @@ public class CorsairCustomRGBDevice : CorsairRGBDevice<CorsairCustomRGBDeviceInf
Rectangle rectangle = ledPosition.ToRectangle();
AddLed(ledId, rectangle.Location, rectangle.Size);
ptr = new IntPtr(ptr.ToInt64() + structSize);
ptr += structSize;
}
if (DeviceInfo.LedOffset > 0)
@@ -41,7 +41,7 @@ public class CorsairCustomRGBDeviceInfo : CorsairRGBDeviceInfo
/// <param name="ledOffset">The offset used to find the LEDs of this device.</param>
internal CorsairCustomRGBDeviceInfo(int deviceIndex, _CorsairDeviceInfo nativeInfo, _CorsairChannelDeviceInfo channelDeviceInfo, int ledOffset)
: base(deviceIndex, GetDeviceType(channelDeviceInfo.type), nativeInfo,
GetModelName(nativeInfo.model == IntPtr.Zero ? string.Empty : Regex.Replace(Marshal.PtrToStringAnsi(nativeInfo.model) ?? string.Empty, " ?DEMO", string.Empty, RegexOptions.IgnoreCase), channelDeviceInfo))
GetModelName(nativeInfo.model == 0 ? string.Empty : Regex.Replace(Marshal.PtrToStringAnsi(nativeInfo.model) ?? string.Empty, " ?DEMO", string.Empty, RegexOptions.IgnoreCase), channelDeviceInfo))
{
this.LedOffset = ledOffset;
@@ -40,8 +40,8 @@ public class CorsairDeviceUpdateQueue : UpdateQueue
try
{
int structSize = Marshal.SizeOf(typeof(_CorsairLedColor));
IntPtr ptr = Marshal.AllocHGlobal(structSize * dataSet.Length);
IntPtr addPtr = new(ptr.ToInt64());
nint ptr = Marshal.AllocHGlobal(structSize * dataSet.Length);
nint addPtr = ptr;
try
{
foreach ((object key, Color color) in dataSet)
@@ -55,7 +55,7 @@ public class CorsairDeviceUpdateQueue : UpdateQueue
};
Marshal.StructureToPtr(corsairColor, addPtr, false);
addPtr = new IntPtr(addPtr.ToInt64() + structSize);
addPtr += structSize;
}
_CUESDK.CorsairSetLedsColorsBufferByDeviceIndex(_deviceIndex, dataSet.Length, ptr);
@@ -1,7 +1,6 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
using System;
using System.Runtime.InteropServices;
using RGB.NET.Devices.CorsairLegacy.Native;
@@ -54,8 +53,8 @@ public class CorsairProtocolDetails
/// <param name="nativeDetails">The native CorsairProtocolDetails-struct</param>
internal CorsairProtocolDetails(_CorsairProtocolDetails nativeDetails)
{
this.SdkVersion = nativeDetails.sdkVersion == IntPtr.Zero ? null : Marshal.PtrToStringAnsi(nativeDetails.sdkVersion);
this.ServerVersion = nativeDetails.serverVersion == IntPtr.Zero ? null : Marshal.PtrToStringAnsi(nativeDetails.serverVersion);
this.SdkVersion = nativeDetails.sdkVersion == 0 ? null : Marshal.PtrToStringAnsi(nativeDetails.sdkVersion);
this.ServerVersion = nativeDetails.serverVersion == 0 ? null : Marshal.PtrToStringAnsi(nativeDetails.serverVersion);
this.SdkProtocolVersion = nativeDetails.sdkProtocolVersion;
this.ServerProtocolVersion = nativeDetails.serverProtocolVersion;
this.BreakingChanges = nativeDetails.breakingChanges != 0;
@@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using RGB.NET.Core;
using RGB.NET.Devices.CorsairLegacy.Native;
@@ -50,14 +49,14 @@ public abstract class CorsairRGBDevice<TDeviceInfo> : AbstractRGBDevice<TDeviceI
if (nativeLedPositions == null) return;
int structSize = Marshal.SizeOf(typeof(_CorsairLedPosition));
IntPtr ptr = nativeLedPositions.pLedPosition;
nint ptr = nativeLedPositions.pLedPosition;
for (int i = 0; i < nativeLedPositions.numberOfLed; i++)
{
_CorsairLedPosition? ledPosition = (_CorsairLedPosition?)Marshal.PtrToStructure(ptr, typeof(_CorsairLedPosition));
if (ledPosition == null)
{
ptr = new IntPtr(ptr.ToInt64() + structSize);
ptr += structSize;
continue;
}
@@ -65,7 +64,7 @@ public abstract class CorsairRGBDevice<TDeviceInfo> : AbstractRGBDevice<TDeviceI
Rectangle rectangle = ledPosition.ToRectangle();
AddLed(ledId, rectangle.Location, rectangle.Size);
ptr = new IntPtr(ptr.ToInt64() + structSize);
ptr += structSize;
}
}
@@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using RGB.NET.Core;
using RGB.NET.Devices.CorsairLegacy.Native;
@@ -65,7 +64,7 @@ public class CorsairRGBDeviceInfo : IRGBDeviceInfo
this.CorsairDeviceIndex = deviceIndex;
this.DeviceType = deviceType;
this.CorsairDeviceType = nativeInfo.type;
this.Model = nativeInfo.model == IntPtr.Zero ? string.Empty : Regex.Replace(Marshal.PtrToStringAnsi(nativeInfo.model) ?? string.Empty, " ?DEMO", string.Empty, RegexOptions.IgnoreCase);
this.Model = nativeInfo.model == 0 ? string.Empty : Regex.Replace(Marshal.PtrToStringAnsi(nativeInfo.model) ?? string.Empty, " ?DEMO", string.Empty, RegexOptions.IgnoreCase);
this.DeviceId = nativeInfo.deviceId ?? string.Empty;
this.CapsMask = (CorsairDeviceCaps)nativeInfo.capsMask;
@@ -34,27 +34,27 @@ public static class LedMappings
/// <summary>
/// Gets the mapping for graphics cards.
/// </summary>
public static LedMapping<CorsairLedId> GraphicsCard { get; } = new();
public static LedMapping<CorsairLedId> GraphicsCard { get; } = [];
/// <summary>
/// Gets the mapping for headsets.
/// </summary>
public static LedMapping<CorsairLedId> HeadsetStand { get; } = new();
public static LedMapping<CorsairLedId> HeadsetStand { get; } = [];
/// <summary>
/// Gets the mapping for mainboards.
/// </summary>
public static LedMapping<CorsairLedId> Mainboard { get; } = new();
public static LedMapping<CorsairLedId> Mainboard { get; } = [];
/// <summary>
/// Gets the mapping for memory.
/// </summary>
public static LedMapping<CorsairLedId> Memory { get; } = new();
public static LedMapping<CorsairLedId> Memory { get; } = [];
/// <summary>
/// Gets the mapping for mousepads.
/// </summary>
public static LedMapping<CorsairLedId> Mousepad { get; } = new();
public static LedMapping<CorsairLedId> Mousepad { get; } = [];
/// <summary>
/// Gets the mapping for headsets.
@@ -7,7 +7,7 @@ internal static class NativeExtensions
{
internal static Rectangle ToRectangle(this _CorsairLedPosition position)
{
//HACK DarthAffe 08.07.2018: It seems like corsair introduced a bug here - it's always 0.
//HACK DarthAffe 08.07.2018: It seems like corsair introduced a issue here - it's always 0.
float width = position.width < 0.5f ? 10 : (float)position.width;
float height = position.height < 0.5f ? 10 : (float)position.height;
float posX = (float)position.left;
@@ -16,7 +16,7 @@ internal static class _CUESDK
{
#region Libary Management
private static IntPtr _handle = IntPtr.Zero;
private static nint _handle = 0;
/// <summary>
/// Reloads the SDK.
@@ -29,7 +29,7 @@ internal static class _CUESDK
private static void LoadCUESDK()
{
if (_handle != IntPtr.Zero) return;
if (_handle != 0) return;
List<string> possiblePathList = GetPossibleLibraryPaths().ToList();
@@ -71,23 +71,23 @@ internal static class _CUESDK
internal static void UnloadCUESDK()
{
if (_handle == IntPtr.Zero) return;
if (_handle == 0) return;
_corsairSetLedsColorsBufferByDeviceIndexPointer = IntPtr.Zero;
_corsairSetLedsColorsFlushBufferPointer = IntPtr.Zero;
_corsairGetLedsColorsByDeviceIndexPointer = IntPtr.Zero;
_corsairSetLayerPriorityPointer = IntPtr.Zero;
_corsairGetDeviceCountPointer = IntPtr.Zero;
_corsairGetDeviceInfoPointer = IntPtr.Zero;
_corsairGetLedIdForKeyNamePointer = IntPtr.Zero;
_corsairGetLedPositionsByDeviceIndexPointer = IntPtr.Zero;
_corsairRequestControlPointer = IntPtr.Zero;
_corsairReleaseControlPointer = IntPtr.Zero;
_corsairPerformProtocolHandshakePointer = IntPtr.Zero;
_corsairGetLastErrorPointer = IntPtr.Zero;
_corsairSetLedsColorsBufferByDeviceIndexPointer = 0;
_corsairSetLedsColorsFlushBufferPointer = 0;
_corsairGetLedsColorsByDeviceIndexPointer = 0;
_corsairSetLayerPriorityPointer = 0;
_corsairGetDeviceCountPointer = 0;
_corsairGetDeviceInfoPointer = 0;
_corsairGetLedIdForKeyNamePointer = 0;
_corsairGetLedPositionsByDeviceIndexPointer = 0;
_corsairRequestControlPointer = 0;
_corsairReleaseControlPointer = 0;
_corsairPerformProtocolHandshakePointer = 0;
_corsairGetLastErrorPointer = 0;
NativeLibrary.Free(_handle);
_handle = IntPtr.Zero;
_handle = 0;
}
#endregion
@@ -96,18 +96,18 @@ internal static class _CUESDK
#region Pointers
private static IntPtr _corsairSetLedsColorsBufferByDeviceIndexPointer;
private static IntPtr _corsairSetLedsColorsFlushBufferPointer;
private static IntPtr _corsairGetLedsColorsByDeviceIndexPointer;
private static IntPtr _corsairSetLayerPriorityPointer;
private static IntPtr _corsairGetDeviceCountPointer;
private static IntPtr _corsairGetDeviceInfoPointer;
private static IntPtr _corsairGetLedIdForKeyNamePointer;
private static IntPtr _corsairGetLedPositionsByDeviceIndexPointer;
private static IntPtr _corsairRequestControlPointer;
private static IntPtr _corsairReleaseControlPointer;
private static IntPtr _corsairPerformProtocolHandshakePointer;
private static IntPtr _corsairGetLastErrorPointer;
private static nint _corsairSetLedsColorsBufferByDeviceIndexPointer;
private static nint _corsairSetLedsColorsFlushBufferPointer;
private static nint _corsairGetLedsColorsByDeviceIndexPointer;
private static nint _corsairSetLayerPriorityPointer;
private static nint _corsairGetDeviceCountPointer;
private static nint _corsairGetDeviceInfoPointer;
private static nint _corsairGetLedIdForKeyNamePointer;
private static nint _corsairGetLedPositionsByDeviceIndexPointer;
private static nint _corsairRequestControlPointer;
private static nint _corsairReleaseControlPointer;
private static nint _corsairPerformProtocolHandshakePointer;
private static nint _corsairGetLastErrorPointer;
#endregion
@@ -118,8 +118,8 @@ internal static class _CUESDK
/// and follows after one or more calls of CorsairSetLedsColorsBufferByDeviceIndex to set the LEDs buffer.
/// This function does not take logical layout into account.
/// </summary>
internal static unsafe bool CorsairSetLedsColorsBufferByDeviceIndex(int deviceIndex, int size, IntPtr ledsColors)
=> ((delegate* unmanaged[Cdecl]<int, int, IntPtr, bool>)ThrowIfZero(_corsairSetLedsColorsBufferByDeviceIndexPointer))(deviceIndex, size, ledsColors);
internal static unsafe bool CorsairSetLedsColorsBufferByDeviceIndex(int deviceIndex, int size, nint ledsColors)
=> ((delegate* unmanaged[Cdecl]<int, int, nint, bool>)ThrowIfZero(_corsairSetLedsColorsBufferByDeviceIndexPointer))(deviceIndex, size, ledsColors);
/// <summary>
/// CUE-SDK: writes to the devices LEDs colors buffer which is previously filled by the CorsairSetLedsColorsBufferByDeviceIndex function.
@@ -132,8 +132,8 @@ internal static class _CUESDK
/// The color should represent the actual state of the hardware LED, which could be a combination of SDK and/or CUE input.
/// This function works for keyboard, mouse, mousemat, headset, headset stand and DIY-devices.
/// </summary>
internal static unsafe bool CorsairGetLedsColorsByDeviceIndex(int deviceIndex, int size, IntPtr ledsColors)
=> ((delegate* unmanaged[Cdecl]<int, int, IntPtr, bool>)ThrowIfZero(_corsairGetLedsColorsByDeviceIndexPointer))(deviceIndex, size, ledsColors);
internal static unsafe bool CorsairGetLedsColorsByDeviceIndex(int deviceIndex, int size, nint ledsColors)
=> ((delegate* unmanaged[Cdecl]<int, int, nint, bool>)ThrowIfZero(_corsairGetLedsColorsByDeviceIndexPointer))(deviceIndex, size, ledsColors);
/// <summary>
/// CUE-SDK: set layer priority for this shared client.
@@ -150,12 +150,12 @@ internal static class _CUESDK
/// <summary>
/// CUE-SDK: returns information about device at provided index.
/// </summary>
internal static unsafe IntPtr CorsairGetDeviceInfo(int deviceIndex) => ((delegate* unmanaged[Cdecl]<int, IntPtr>)ThrowIfZero(_corsairGetDeviceInfoPointer))(deviceIndex);
internal static unsafe nint CorsairGetDeviceInfo(int deviceIndex) => ((delegate* unmanaged[Cdecl]<int, nint>)ThrowIfZero(_corsairGetDeviceInfoPointer))(deviceIndex);
/// <summary>
/// CUE-SDK: provides list of keyboard or mousepad LEDs with their physical positions.
/// </summary>
internal static unsafe IntPtr CorsairGetLedPositionsByDeviceIndex(int deviceIndex) => ((delegate* unmanaged[Cdecl]<int, IntPtr>)ThrowIfZero(_corsairGetLedPositionsByDeviceIndexPointer))(deviceIndex);
internal static unsafe nint CorsairGetLedPositionsByDeviceIndex(int deviceIndex) => ((delegate* unmanaged[Cdecl]<int, nint>)ThrowIfZero(_corsairGetLedPositionsByDeviceIndexPointer))(deviceIndex);
/// <summary>
/// CUE-SDK: retrieves led id for key name taking logical layout into account.
@@ -183,9 +183,9 @@ internal static class _CUESDK
/// </summary>
internal static unsafe CorsairError CorsairGetLastError() => ((delegate* unmanaged[Cdecl]<CorsairError>)ThrowIfZero(_corsairGetLastErrorPointer))();
private static IntPtr ThrowIfZero(IntPtr ptr)
private static nint ThrowIfZero(nint ptr)
{
if (ptr == IntPtr.Zero) throw new RGBDeviceException("The Corsair-SDK is not initialized.");
if (ptr == 0) throw new RGBDeviceException("The Corsair-SDK is not initialized.");
return ptr;
}
@@ -3,7 +3,6 @@
#pragma warning disable 649 // Field 'x' is never assigned
#pragma warning disable IDE1006 // Naming Styles
using System;
using System.Runtime.InteropServices;
namespace RGB.NET.Devices.CorsairLegacy.Native;
@@ -29,5 +28,5 @@ internal class _CorsairChannelInfo
/// CUE-SDK: array containing information about each separate LED-device connected to the channel controlled by the DIY device.
/// Index of the LED-device in array is same as the index of the LED-device connected to the DIY-device.
/// </summary>
internal IntPtr devices;
internal nint devices;
}
@@ -3,7 +3,6 @@
#pragma warning disable 649 // Field 'x' is never assigned
#pragma warning disable IDE1006 // Naming Styles
using System;
using System.Runtime.InteropServices;
namespace RGB.NET.Devices.CorsairLegacy.Native;
@@ -24,5 +23,5 @@ internal class _CorsairChannelsInfo
/// CUE-SDK: array containing information about each separate channel of the DIY-device.
/// Index of the channel in the array is same as index of the channel on the DIY-device.
/// </summary>
internal IntPtr channels;
internal nint channels;
}
@@ -3,7 +3,6 @@
#pragma warning disable 649 // Field 'x' is never assigned
#pragma warning disable IDE1006 // Naming Styles
using System;
using System.Runtime.InteropServices;
namespace RGB.NET.Devices.CorsairLegacy.Native;
@@ -23,7 +22,7 @@ internal class _CorsairDeviceInfo
/// <summary>
/// CUE-SDK: null - terminated device model(like “K95RGB”)
/// </summary>
internal IntPtr model;
internal nint model;
/// <summary>
/// CUE-SDK: enum describing physical layout of the keyboard or mouse
@@ -2,7 +2,6 @@
#pragma warning disable 414 // Field 'x' is assigned but its value never used
#pragma warning disable 649 // Field 'x' is never assigned
using System;
using System.Runtime.InteropServices;
namespace RGB.NET.Devices.CorsairLegacy.Native;
@@ -22,5 +21,5 @@ internal class _CorsairLedPositions
/// <summary>
/// CUE-SDK: array of led positions
/// </summary>
internal IntPtr pLedPosition;
internal nint pLedPosition;
}
@@ -2,7 +2,6 @@
#pragma warning disable 414 // Field 'x' is assigned but its value never used
#pragma warning disable 649 // Field 'x' is never assigned
using System;
using System.Runtime.InteropServices;
namespace RGB.NET.Devices.CorsairLegacy.Native;
@@ -17,12 +16,12 @@ internal struct _CorsairProtocolDetails
/// <summary>
/// CUE-SDK: null - terminated string containing version of SDK(like “1.0.0.1”). Always contains valid value even if there was no CUE found
/// </summary>
internal IntPtr sdkVersion;
internal nint sdkVersion;
/// <summary>
/// CUE-SDK: null - terminated string containing version of CUE(like “1.0.0.1”) or NULL if CUE was not found.
/// </summary>
internal IntPtr serverVersion;
internal nint serverVersion;
/// <summary>
/// CUE-SDK: integer number that specifies version of protocol that is implemented by current SDK.