From 2222808c23c6323eb8164f6cdff9f5400cea0b7e Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Wed, 3 Mar 2021 23:06:22 +0100 Subject: [PATCH] Changed update-Pprameter away from dictionary --- .../Events/SurfaceLayoutChangedEventArgs.cs | 1 - .../Rendering/Brushes/AbstractBrush.cs | 1 - RGB.NET.Core/Update/Devices/UpdateQueue.cs | 41 +++++++++++-------- .../Generic/AsusUpdateQueue.cs | 4 +- .../Generic/CoolerMasterUpdateQueue.cs | 12 +++--- .../Generic/CorsairDeviceUpdateQueue.cs | 23 +++++------ RGB.NET.Devices.DMX/E131/E131UpdateQueue.cs | 9 ++-- .../PerDevice/LogitechPerDeviceUpdateQueue.cs | 6 +-- .../PerKey/LogitechPerKeyUpdateQueue.cs | 15 ++++--- .../Zone/LogitechZoneUpdateQueue.cs | 24 +++++------ .../Generic/MsiDeviceUpdateQueue.cs | 8 ++-- .../Generic/LimitedColorUpdateQueue.cs | 11 +++-- .../Generic/MidiUpdateQueue.cs | 9 ++-- .../Generic/RGBColorUpdateQueue.cs | 9 ++-- .../ChromaLink/RazerChromaLinkUpdateQueue.cs | 9 ++-- .../Generic/RazerUpdateQueue.cs | 5 +-- .../Headset/RazerHeadsetUpdateQueue.cs | 11 +++-- .../Keyboard/RazerKeyboardUpdateQueue.cs | 10 ++--- .../Keypad/RazerKeypadUpdateQueue.cs | 10 ++--- .../Mouse/RazerMouseUpdateQueue.cs | 10 ++--- .../Mousepad/RazerMousepadUpdateQueue.cs | 10 ++--- .../Generic/SteelSeriesDeviceUpdateQueue.cs | 6 +-- .../Arduino/ArduinoWS2812USBUpdateQueue.cs | 4 +- .../BitwizardWS2812USBUpdateQueue.cs | 2 +- .../Generic/SerialPortUpdateQueue.cs | 9 ++-- .../NodeMCU/NodeMCUWS2812USBUpdateQueue.cs | 11 +++-- .../Generic/WootingUpdateQueue.cs | 12 +++--- 27 files changed, 133 insertions(+), 149 deletions(-) diff --git a/RGB.NET.Core/Events/SurfaceLayoutChangedEventArgs.cs b/RGB.NET.Core/Events/SurfaceLayoutChangedEventArgs.cs index 64a91df..e591385 100644 --- a/RGB.NET.Core/Events/SurfaceLayoutChangedEventArgs.cs +++ b/RGB.NET.Core/Events/SurfaceLayoutChangedEventArgs.cs @@ -2,7 +2,6 @@ // ReSharper disable UnusedAutoPropertyAccessor.Global using System; -using System.Collections.Generic; namespace RGB.NET.Core { diff --git a/RGB.NET.Core/Rendering/Brushes/AbstractBrush.cs b/RGB.NET.Core/Rendering/Brushes/AbstractBrush.cs index 782c496..e4d7274 100644 --- a/RGB.NET.Core/Rendering/Brushes/AbstractBrush.cs +++ b/RGB.NET.Core/Rendering/Brushes/AbstractBrush.cs @@ -3,7 +3,6 @@ // ReSharper disable VirtualMemberNeverOverridden.Global using System.Collections.Generic; -using System.Linq; namespace RGB.NET.Core { diff --git a/RGB.NET.Core/Update/Devices/UpdateQueue.cs b/RGB.NET.Core/Update/Devices/UpdateQueue.cs index d64584e..76e6180 100644 --- a/RGB.NET.Core/Update/Devices/UpdateQueue.cs +++ b/RGB.NET.Core/Update/Devices/UpdateQueue.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Collections.Generic; using System.Linq; @@ -16,7 +17,7 @@ namespace RGB.NET.Core private readonly object _dataLock = new(); private readonly IDeviceUpdateTrigger _updateTrigger; - private Dictionary? _currentDataSet; + private readonly Dictionary _currentDataSet = new(); #endregion @@ -45,17 +46,25 @@ namespace RGB.NET.Core /// provided by the trigger. protected virtual void OnUpdate(object? sender, CustomUpdateData customData) { - Dictionary dataSet; + (TIdentifier, TData)[] dataSet; lock (_dataLock) { - if (_currentDataSet == null) return; + if (_currentDataSet.Count == 0) return; - dataSet = _currentDataSet; - _currentDataSet = null; + dataSet = ArrayPool<(TIdentifier, TData)>.Shared.Rent(_currentDataSet.Count); + Span<(TIdentifier, TData)> data = new Span<(TIdentifier, TData)>(dataSet).Slice(0, _currentDataSet.Count); + + int i = 0; + foreach ((TIdentifier key, TData value) in _currentDataSet) + data[i++] = (key, value); + + _currentDataSet.Clear(); } - if (dataSet.Count != 0) + if (dataSet.Length != 0) Update(dataSet); + + ArrayPool<(TIdentifier, TData)>.Shared.Return(dataSet); } /// @@ -69,26 +78,22 @@ namespace RGB.NET.Core /// Performs the update this queue is responsible for. /// /// The set of data that needs to be updated. - protected abstract void Update(Dictionary dataSet); + protected abstract void Update(in ReadOnlySpan<(TIdentifier key, TData color)> dataSet); /// /// Sets or merges the provided data set in the current dataset and notifies the trigger that there is new data available. /// /// The set of data. // ReSharper disable once MemberCanBeProtected.Global - public virtual void SetData(Dictionary dataSet) + public virtual void SetData(IEnumerable<(TIdentifier, TData)> dataSet) { - if (dataSet.Count == 0) return; + IList<(TIdentifier, TData)> data = dataSet.ToList(); + if (data.Count == 0) return; lock (_dataLock) { - if (_currentDataSet == null) - _currentDataSet = dataSet; - else - { - foreach ((TIdentifier key, TData value) in dataSet) - _currentDataSet[key] = value; - } + foreach ((TIdentifier key, TData value) in data) + _currentDataSet[key] = value; } _updateTrigger.TriggerHasData(); @@ -100,7 +105,7 @@ namespace RGB.NET.Core public virtual void Reset() { lock (_dataLock) - _currentDataSet = null; + _currentDataSet.Clear(); } /// @@ -135,7 +140,7 @@ namespace RGB.NET.Core /// Calls for a data set created out of the provided list of . /// /// - public void SetData(IEnumerable leds) => SetData(leds.ToDictionary(x => x.CustomData ?? x.Id, x => x.Color)); + public void SetData(IEnumerable leds) => SetData(leds.Select(x => (x.CustomData ?? x.Id, x.Color))); #endregion } diff --git a/RGB.NET.Devices.Asus/Generic/AsusUpdateQueue.cs b/RGB.NET.Devices.Asus/Generic/AsusUpdateQueue.cs index 1623fa7..73cbc31 100644 --- a/RGB.NET.Devices.Asus/Generic/AsusUpdateQueue.cs +++ b/RGB.NET.Devices.Asus/Generic/AsusUpdateQueue.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System; using AuraServiceLib; using RGB.NET.Core; @@ -43,7 +43,7 @@ namespace RGB.NET.Devices.Asus } /// - protected override void Update(Dictionary dataSet) + protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) { if (Device == null) return; diff --git a/RGB.NET.Devices.CoolerMaster/Generic/CoolerMasterUpdateQueue.cs b/RGB.NET.Devices.CoolerMaster/Generic/CoolerMasterUpdateQueue.cs index 21b26ce..42c96fe 100644 --- a/RGB.NET.Devices.CoolerMaster/Generic/CoolerMasterUpdateQueue.cs +++ b/RGB.NET.Devices.CoolerMaster/Generic/CoolerMasterUpdateQueue.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System; using RGB.NET.Core; using RGB.NET.Devices.CoolerMaster.Native; @@ -28,7 +28,7 @@ namespace RGB.NET.Devices.CoolerMaster : base(updateTrigger) { this._deviceIndex = deviceIndex; - + _deviceMatrix = new _CoolerMasterColorMatrix(); _deviceMatrix.KeyColor = new _CoolerMasterKeyColor[_CoolerMasterColorMatrix.ROWS, _CoolerMasterColorMatrix.COLUMNS]; } @@ -38,12 +38,12 @@ namespace RGB.NET.Devices.CoolerMaster #region Methods /// - protected override void Update(Dictionary dataSet) + protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) { - foreach (KeyValuePair data in dataSet) + foreach ((object key, Color color) in dataSet) { - (int row, int column) = ((int, int))data.Key; - _deviceMatrix.KeyColor[row, column] = new _CoolerMasterKeyColor(data.Value.GetR(), data.Value.GetG(), data.Value.GetB()); + (int row, int column) = ((int, int))key; + _deviceMatrix.KeyColor[row, column] = new _CoolerMasterKeyColor(color.GetR(), color.GetG(), color.GetB()); } _CoolerMasterSDK.SetAllLedColor(_deviceMatrix, _deviceIndex); diff --git a/RGB.NET.Devices.Corsair/Generic/CorsairDeviceUpdateQueue.cs b/RGB.NET.Devices.Corsair/Generic/CorsairDeviceUpdateQueue.cs index ae1859a..340fdee 100644 --- a/RGB.NET.Devices.Corsair/Generic/CorsairDeviceUpdateQueue.cs +++ b/RGB.NET.Devices.Corsair/Generic/CorsairDeviceUpdateQueue.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Runtime.InteropServices; using RGB.NET.Core; using RGB.NET.Devices.Corsair.Native; @@ -36,26 +35,26 @@ namespace RGB.NET.Devices.Corsair #region Methods /// - protected override void Update(Dictionary dataSet) + protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) { int structSize = Marshal.SizeOf(typeof(_CorsairLedColor)); - IntPtr ptr = Marshal.AllocHGlobal(structSize * dataSet.Count); + IntPtr ptr = Marshal.AllocHGlobal(structSize * dataSet.Length); IntPtr addPtr = new(ptr.ToInt64()); - foreach (KeyValuePair data in dataSet) + foreach ((object key, Color color) in dataSet) { - _CorsairLedColor color = new() - { - ledId = (int)data.Key, - r = data.Value.GetR(), - g = data.Value.GetG(), - b = data.Value.GetB() + _CorsairLedColor corsairColor = new() + { + ledId = (int)key, + r = color.GetR(), + g = color.GetG(), + b = color.GetB() }; - Marshal.StructureToPtr(color, addPtr, false); + Marshal.StructureToPtr(corsairColor, addPtr, false); addPtr = new IntPtr(addPtr.ToInt64() + structSize); } - _CUESDK.CorsairSetLedsColorsBufferByDeviceIndex(_deviceIndex, dataSet.Count, ptr); + _CUESDK.CorsairSetLedsColorsBufferByDeviceIndex(_deviceIndex, dataSet.Length, ptr); _CUESDK.CorsairSetLedsColorsFlushBuffer(); Marshal.FreeHGlobal(ptr); } diff --git a/RGB.NET.Devices.DMX/E131/E131UpdateQueue.cs b/RGB.NET.Devices.DMX/E131/E131UpdateQueue.cs index dcf301e..e7a3f96 100644 --- a/RGB.NET.Devices.DMX/E131/E131UpdateQueue.cs +++ b/RGB.NET.Devices.DMX/E131/E131UpdateQueue.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Net.Sockets; using RGB.NET.Core; @@ -52,15 +51,15 @@ namespace RGB.NET.Devices.DMX.E131 #region Methods /// - protected override void Update(Dictionary dataSet) + protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) { DataPacket.SetSequenceNumber(GetNextSequenceNumber()); - foreach (KeyValuePair data in dataSet) + foreach ((object key, Color color) in dataSet) { - LedChannelMapping mapping = (LedChannelMapping)data.Key; + LedChannelMapping mapping = (LedChannelMapping)key; foreach ((int channel, Func getValue) in mapping) - DataPacket.SetChannel(channel, getValue(data.Value)); + DataPacket.SetChannel(channel, getValue(color)); } _socket.Send(DataPacket, DataPacket.Length); diff --git a/RGB.NET.Devices.Logitech/PerDevice/LogitechPerDeviceUpdateQueue.cs b/RGB.NET.Devices.Logitech/PerDevice/LogitechPerDeviceUpdateQueue.cs index 8ea4c6a..a1ef25e 100644 --- a/RGB.NET.Devices.Logitech/PerDevice/LogitechPerDeviceUpdateQueue.cs +++ b/RGB.NET.Devices.Logitech/PerDevice/LogitechPerDeviceUpdateQueue.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using RGB.NET.Core; using RGB.NET.Devices.Logitech.Native; @@ -27,9 +25,9 @@ namespace RGB.NET.Devices.Logitech #region Methods /// - protected override void Update(Dictionary dataSet) + protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) { - Color color = dataSet.Values.First(); + Color color = dataSet[0].color; _LogitechGSDK.LogiLedSetTargetDevice(LogitechDeviceCaps.DeviceRGB); _LogitechGSDK.LogiLedSetLighting((int)Math.Round(color.R * 100), diff --git a/RGB.NET.Devices.Logitech/PerKey/LogitechPerKeyUpdateQueue.cs b/RGB.NET.Devices.Logitech/PerKey/LogitechPerKeyUpdateQueue.cs index a166a3a..6077ed6 100644 --- a/RGB.NET.Devices.Logitech/PerKey/LogitechPerKeyUpdateQueue.cs +++ b/RGB.NET.Devices.Logitech/PerKey/LogitechPerKeyUpdateQueue.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using RGB.NET.Core; using RGB.NET.Devices.Logitech.Native; @@ -33,27 +32,27 @@ namespace RGB.NET.Devices.Logitech #region Methods /// - protected override void Update(Dictionary dataSet) + protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) { _LogitechGSDK.LogiLedSetTargetDevice(LogitechDeviceCaps.PerKeyRGB); Array.Clear(_bitmap, 0, _bitmap.Length); bool usesBitmap = false; - foreach (KeyValuePair data in dataSet) + foreach ((object key, Color color) in dataSet) { - (LedId id, LogitechLedId customData) = ((LedId, LogitechLedId))data.Key; + (LedId id, LogitechLedId customData) = ((LedId, LogitechLedId))key; // DarthAffe 26.03.2017: This is only needed since update by name doesn't work as expected for all keys ... if (BitmapMapping.BitmapOffset.TryGetValue(id, out int bitmapOffset)) { - BitmapMapping.SetColor(_bitmap, bitmapOffset, data.Value); + BitmapMapping.SetColor(_bitmap, bitmapOffset, color); usesBitmap = true; } else _LogitechGSDK.LogiLedSetLightingForKeyWithKeyName((int)customData, - (int)Math.Round(data.Value.R * 100), - (int)Math.Round(data.Value.G * 100), - (int)Math.Round(data.Value.B * 100)); + (int)MathF.Round(color.R * 100), + (int)MathF.Round(color.G * 100), + (int)MathF.Round(color.B * 100)); } if (usesBitmap) diff --git a/RGB.NET.Devices.Logitech/Zone/LogitechZoneUpdateQueue.cs b/RGB.NET.Devices.Logitech/Zone/LogitechZoneUpdateQueue.cs index c3fb9ef..7c34363 100644 --- a/RGB.NET.Devices.Logitech/Zone/LogitechZoneUpdateQueue.cs +++ b/RGB.NET.Devices.Logitech/Zone/LogitechZoneUpdateQueue.cs @@ -13,12 +13,12 @@ namespace RGB.NET.Devices.Logitech #region Constants private static readonly Dictionary DEVICE_TYPE_MAPPING = new() - { - {RGBDeviceType.Keyboard, LogitechDeviceType.Keyboard}, - {RGBDeviceType.Mouse, LogitechDeviceType.Mouse}, - {RGBDeviceType.Headset, LogitechDeviceType.Headset}, - {RGBDeviceType.Mousepad, LogitechDeviceType.Mousemat}, - {RGBDeviceType.Speaker, LogitechDeviceType.Speaker} + { + { RGBDeviceType.Keyboard, LogitechDeviceType.Keyboard }, + { RGBDeviceType.Mouse, LogitechDeviceType.Mouse }, + { RGBDeviceType.Headset, LogitechDeviceType.Headset }, + { RGBDeviceType.Mousepad, LogitechDeviceType.Mousemat }, + { RGBDeviceType.Speaker, LogitechDeviceType.Speaker } }; #endregion @@ -48,17 +48,17 @@ namespace RGB.NET.Devices.Logitech #region Methods /// - protected override void Update(Dictionary dataSet) + protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) { _LogitechGSDK.LogiLedSetTargetDevice(LogitechDeviceCaps.All); - foreach (KeyValuePair data in dataSet) + foreach ((object key, Color color) in dataSet) { - int zone = (int)data.Key; + int zone = (int)key; _LogitechGSDK.LogiLedSetLightingForTargetZone(_deviceType, zone, - (int)Math.Round(data.Value.R * 100), - (int)Math.Round(data.Value.G * 100), - (int)Math.Round(data.Value.B * 100)); + (int)MathF.Round(color.R * 100), + (int)MathF.Round(color.G * 100), + (int)MathF.Round(color.B * 100)); } } diff --git a/RGB.NET.Devices.Msi/Generic/MsiDeviceUpdateQueue.cs b/RGB.NET.Devices.Msi/Generic/MsiDeviceUpdateQueue.cs index b2dffc0..6f6bf73 100644 --- a/RGB.NET.Devices.Msi/Generic/MsiDeviceUpdateQueue.cs +++ b/RGB.NET.Devices.Msi/Generic/MsiDeviceUpdateQueue.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System; using RGB.NET.Core; using RGB.NET.Devices.Msi.Native; @@ -34,10 +34,10 @@ namespace RGB.NET.Devices.Msi #region Methods /// - protected override void Update(Dictionary dataSet) + protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) { - foreach (KeyValuePair data in dataSet) - _MsiSDK.SetLedColor(_deviceType, (int)data.Key, data.Value.GetR(), data.Value.GetG(), data.Value.GetB()); + foreach ((object key, Color color) in dataSet) + _MsiSDK.SetLedColor(_deviceType, (int)key, color.GetR(), color.GetG(), color.GetB()); } #endregion diff --git a/RGB.NET.Devices.Novation/Generic/LimitedColorUpdateQueue.cs b/RGB.NET.Devices.Novation/Generic/LimitedColorUpdateQueue.cs index 588e359..959e360 100644 --- a/RGB.NET.Devices.Novation/Generic/LimitedColorUpdateQueue.cs +++ b/RGB.NET.Devices.Novation/Generic/LimitedColorUpdateQueue.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using RGB.NET.Core; using Sanford.Multimedia.Midi; @@ -26,10 +25,10 @@ namespace RGB.NET.Devices.Novation #region Methods /// - protected override ShortMessage CreateMessage(KeyValuePair data) + protected override ShortMessage CreateMessage(object key, in Color color) { - (byte mode, byte id) = ((byte, byte))data.Key; - return new ShortMessage(mode, id, Convert.ToByte(ConvertColor(data.Value))); + (byte mode, byte id) = ((byte, byte))key; + return new ShortMessage(mode, id, Convert.ToByte(ConvertColor(color))); } /// @@ -38,9 +37,9 @@ namespace RGB.NET.Devices.Novation /// /// The to convert. /// The novation-representation of the . - protected virtual int ConvertColor(Color color) + protected virtual int ConvertColor(in Color color) { - (double hue, double saturation, double value) = color.GetHSV(); + (double hue, double _, double value) = color.GetHSV(); if ((hue >= 330) || (hue < 30)) return (int)Math.Ceiling(value * 3); // red with brightness 1, 2 or 3 diff --git a/RGB.NET.Devices.Novation/Generic/MidiUpdateQueue.cs b/RGB.NET.Devices.Novation/Generic/MidiUpdateQueue.cs index 5ce5547..f29241b 100644 --- a/RGB.NET.Devices.Novation/Generic/MidiUpdateQueue.cs +++ b/RGB.NET.Devices.Novation/Generic/MidiUpdateQueue.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using RGB.NET.Core; using Sanford.Multimedia.Midi; @@ -37,10 +36,10 @@ namespace RGB.NET.Devices.Novation #region Methods /// - protected override void Update(Dictionary dataSet) + protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) { - foreach (KeyValuePair data in dataSet) - SendMessage(CreateMessage(data)); + foreach ((object key, Color color) in dataSet) + SendMessage(CreateMessage(key, color)); } /// @@ -59,7 +58,7 @@ namespace RGB.NET.Devices.Novation /// /// The data set to create the message from. /// The message created out of the data set. - protected abstract ShortMessage? CreateMessage(KeyValuePair data); + protected abstract ShortMessage? CreateMessage(object key, in Color color); /// public override void Dispose() diff --git a/RGB.NET.Devices.Novation/Generic/RGBColorUpdateQueue.cs b/RGB.NET.Devices.Novation/Generic/RGBColorUpdateQueue.cs index 2998059..024fe58 100644 --- a/RGB.NET.Devices.Novation/Generic/RGBColorUpdateQueue.cs +++ b/RGB.NET.Devices.Novation/Generic/RGBColorUpdateQueue.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using RGB.NET.Core; using Sanford.Multimedia.Midi; @@ -152,12 +151,12 @@ namespace RGB.NET.Devices.Novation #region Methods /// - protected override ShortMessage? CreateMessage(KeyValuePair data) + protected override ShortMessage? CreateMessage(object key, in Color color) { - (byte mode, byte id) = ((byte, byte))data.Key; + (byte mode, byte id) = ((byte, byte))key; if (mode == 0x00) return null; - return new ShortMessage(mode, id, Convert.ToByte(ConvertColor(data.Value))); + return new ShortMessage(mode, id, Convert.ToByte(ConvertColor(color))); } /// @@ -166,7 +165,7 @@ namespace RGB.NET.Devices.Novation /// /// The to convert. /// The novation-representation of the . - protected virtual int ConvertColor(Color color) + protected virtual int ConvertColor(in Color color) { int bestVelocity = 0; double bestMatchDistance = double.MaxValue; diff --git a/RGB.NET.Devices.Razer/ChromaLink/RazerChromaLinkUpdateQueue.cs b/RGB.NET.Devices.Razer/ChromaLink/RazerChromaLinkUpdateQueue.cs index 7455aa4..94a3823 100644 --- a/RGB.NET.Devices.Razer/ChromaLink/RazerChromaLinkUpdateQueue.cs +++ b/RGB.NET.Devices.Razer/ChromaLink/RazerChromaLinkUpdateQueue.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Runtime.InteropServices; using RGB.NET.Core; using RGB.NET.Devices.Razer.Native; @@ -27,15 +26,15 @@ namespace RGB.NET.Devices.Razer #region Methods /// - protected override IntPtr CreateEffectParams(Dictionary dataSet) + protected override IntPtr CreateEffectParams(in ReadOnlySpan<(object key, Color color)> dataSet) { _Color[] colors = new _Color[_Defines.CHROMALINK_MAX_LEDS]; - foreach (KeyValuePair data in dataSet) - colors[(int)data.Key] = new _Color(data.Value); + foreach ((object key, Color color) in dataSet) + colors[(int)key] = new _Color(color); _ChromaLinkCustomEffect effectParams = new() - { Color = colors }; + { Color = colors }; IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(effectParams)); Marshal.StructureToPtr(effectParams, ptr, false); diff --git a/RGB.NET.Devices.Razer/Generic/RazerUpdateQueue.cs b/RGB.NET.Devices.Razer/Generic/RazerUpdateQueue.cs index de99795..32a4287 100644 --- a/RGB.NET.Devices.Razer/Generic/RazerUpdateQueue.cs +++ b/RGB.NET.Devices.Razer/Generic/RazerUpdateQueue.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using RGB.NET.Core; using RGB.NET.Devices.Razer.Native; @@ -35,7 +34,7 @@ namespace RGB.NET.Devices.Razer #region Methods /// - protected override void Update(Dictionary dataSet) + protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) { IntPtr effectParams = CreateEffectParams(dataSet); Guid effectId = Guid.NewGuid(); @@ -71,7 +70,7 @@ namespace RGB.NET.Devices.Razer /// /// The data to be updated. /// An pointing to the effect parameter struct. - protected abstract IntPtr CreateEffectParams(Dictionary dataSet); + protected abstract IntPtr CreateEffectParams(in ReadOnlySpan<(object key, Color color)> dataSet); #endregion } diff --git a/RGB.NET.Devices.Razer/Headset/RazerHeadsetUpdateQueue.cs b/RGB.NET.Devices.Razer/Headset/RazerHeadsetUpdateQueue.cs index 7e5d9b7..2442490 100644 --- a/RGB.NET.Devices.Razer/Headset/RazerHeadsetUpdateQueue.cs +++ b/RGB.NET.Devices.Razer/Headset/RazerHeadsetUpdateQueue.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Runtime.InteropServices; using RGB.NET.Core; using RGB.NET.Devices.Razer.Native; @@ -27,16 +26,16 @@ namespace RGB.NET.Devices.Razer #region Methods /// - protected override IntPtr CreateEffectParams(Dictionary dataSet) + protected override IntPtr CreateEffectParams(in ReadOnlySpan<(object key, Color color)> dataSet) { _Color[] colors = new _Color[_Defines.HEADSET_MAX_LEDS]; - foreach (KeyValuePair data in dataSet) - colors[(int)data.Key] = new _Color(data.Value); + foreach ((object key, Color color) in dataSet) + colors[(int)key] = new _Color(color); _HeadsetCustomEffect effectParams = new() - { Color = colors }; - + { Color = colors }; + IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(effectParams)); Marshal.StructureToPtr(effectParams, ptr, false); diff --git a/RGB.NET.Devices.Razer/Keyboard/RazerKeyboardUpdateQueue.cs b/RGB.NET.Devices.Razer/Keyboard/RazerKeyboardUpdateQueue.cs index e87a987..bc08035 100644 --- a/RGB.NET.Devices.Razer/Keyboard/RazerKeyboardUpdateQueue.cs +++ b/RGB.NET.Devices.Razer/Keyboard/RazerKeyboardUpdateQueue.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Runtime.InteropServices; using RGB.NET.Core; using RGB.NET.Devices.Razer.Native; @@ -27,15 +26,14 @@ namespace RGB.NET.Devices.Razer #region Methods /// - protected override IntPtr CreateEffectParams(Dictionary dataSet) + protected override IntPtr CreateEffectParams(in ReadOnlySpan<(object key, Color color)> dataSet) { _Color[] colors = new _Color[_Defines.KEYBOARD_MAX_LEDS]; - foreach (KeyValuePair data in dataSet) - colors[(int)data.Key] = new _Color(data.Value); + foreach ((object key, Color color) in dataSet) + colors[(int)key] = new _Color(color); - _KeyboardCustomEffect effectParams = new() - { Color = colors }; + _KeyboardCustomEffect effectParams = new() { Color = colors }; IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(effectParams)); Marshal.StructureToPtr(effectParams, ptr, false); diff --git a/RGB.NET.Devices.Razer/Keypad/RazerKeypadUpdateQueue.cs b/RGB.NET.Devices.Razer/Keypad/RazerKeypadUpdateQueue.cs index f0e884d..74edb8e 100644 --- a/RGB.NET.Devices.Razer/Keypad/RazerKeypadUpdateQueue.cs +++ b/RGB.NET.Devices.Razer/Keypad/RazerKeypadUpdateQueue.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Runtime.InteropServices; using RGB.NET.Core; using RGB.NET.Devices.Razer.Native; @@ -27,15 +26,14 @@ namespace RGB.NET.Devices.Razer #region Methods /// - protected override IntPtr CreateEffectParams(Dictionary dataSet) + protected override IntPtr CreateEffectParams(in ReadOnlySpan<(object key, Color color)> dataSet) { _Color[] colors = new _Color[_Defines.KEYPAD_MAX_LEDS]; - foreach (KeyValuePair data in dataSet) - colors[(int)data.Key] = new _Color(data.Value); + foreach ((object key, Color color) in dataSet) + colors[(int)key] = new _Color(color); - _KeypadCustomEffect effectParams = new() - { Color = colors }; + _KeypadCustomEffect effectParams = new() { Color = colors }; IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(effectParams)); Marshal.StructureToPtr(effectParams, ptr, false); diff --git a/RGB.NET.Devices.Razer/Mouse/RazerMouseUpdateQueue.cs b/RGB.NET.Devices.Razer/Mouse/RazerMouseUpdateQueue.cs index 95547d8..6e10732 100644 --- a/RGB.NET.Devices.Razer/Mouse/RazerMouseUpdateQueue.cs +++ b/RGB.NET.Devices.Razer/Mouse/RazerMouseUpdateQueue.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Runtime.InteropServices; using RGB.NET.Core; using RGB.NET.Devices.Razer.Native; @@ -27,15 +26,14 @@ namespace RGB.NET.Devices.Razer #region Methods /// - protected override IntPtr CreateEffectParams(Dictionary dataSet) + protected override IntPtr CreateEffectParams(in ReadOnlySpan<(object key, Color color)> dataSet) { _Color[] colors = new _Color[_Defines.MOUSE_MAX_LEDS]; - foreach (KeyValuePair data in dataSet) - colors[(int)data.Key] = new _Color(data.Value); + foreach ((object key, Color color) in dataSet) + colors[(int)key] = new _Color(color); - _MouseCustomEffect effectParams = new() - { Color = colors }; + _MouseCustomEffect effectParams = new() { Color = colors }; IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(effectParams)); Marshal.StructureToPtr(effectParams, ptr, false); diff --git a/RGB.NET.Devices.Razer/Mousepad/RazerMousepadUpdateQueue.cs b/RGB.NET.Devices.Razer/Mousepad/RazerMousepadUpdateQueue.cs index 419099b..d9388f7 100644 --- a/RGB.NET.Devices.Razer/Mousepad/RazerMousepadUpdateQueue.cs +++ b/RGB.NET.Devices.Razer/Mousepad/RazerMousepadUpdateQueue.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Runtime.InteropServices; using RGB.NET.Core; using RGB.NET.Devices.Razer.Native; @@ -27,15 +26,14 @@ namespace RGB.NET.Devices.Razer #region Methods /// - protected override IntPtr CreateEffectParams(Dictionary dataSet) + protected override IntPtr CreateEffectParams(in ReadOnlySpan<(object key, Color color)> dataSet) { _Color[] colors = new _Color[_Defines.MOUSEPAD_MAX_LEDS]; - foreach (KeyValuePair data in dataSet) - colors[(int)data.Key] = new _Color(data.Value); + foreach ((object key, Color color) in dataSet) + colors[(int)key] = new _Color(color); - _MousepadCustomEffect effectParams = new() - { Color = colors }; + _MousepadCustomEffect effectParams = new() { Color = colors }; IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(effectParams)); Marshal.StructureToPtr(effectParams, ptr, false); diff --git a/RGB.NET.Devices.SteelSeries/Generic/SteelSeriesDeviceUpdateQueue.cs b/RGB.NET.Devices.SteelSeries/Generic/SteelSeriesDeviceUpdateQueue.cs index 25de272..b3b966d 100644 --- a/RGB.NET.Devices.SteelSeries/Generic/SteelSeriesDeviceUpdateQueue.cs +++ b/RGB.NET.Devices.SteelSeries/Generic/SteelSeriesDeviceUpdateQueue.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System; using System.Linq; using RGB.NET.Core; using RGB.NET.Devices.SteelSeries.API; @@ -44,8 +44,8 @@ namespace RGB.NET.Devices.SteelSeries } /// - protected override void Update(Dictionary dataSet) - => SteelSeriesSDK.UpdateLeds(_deviceType, dataSet.Select(x => (((SteelSeriesLedId)x.Key).GetAPIName(), x.Value.ToIntArray())).Where(x => x.Item1 != null).ToList()!); + protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) + => SteelSeriesSDK.UpdateLeds(_deviceType, dataSet.ToArray().Select(x => (((SteelSeriesLedId)x.key).GetAPIName(), x.color.ToIntArray())).Where(x => x.Item1 != null).ToList()!); #endregion } diff --git a/RGB.NET.Devices.WS281X/Arduino/ArduinoWS2812USBUpdateQueue.cs b/RGB.NET.Devices.WS281X/Arduino/ArduinoWS2812USBUpdateQueue.cs index ebe5a76..9263e64 100644 --- a/RGB.NET.Devices.WS281X/Arduino/ArduinoWS2812USBUpdateQueue.cs +++ b/RGB.NET.Devices.WS281X/Arduino/ArduinoWS2812USBUpdateQueue.cs @@ -50,9 +50,9 @@ namespace RGB.NET.Devices.WS281X.Arduino } /// - protected override IEnumerable GetCommands(Dictionary dataSet) + protected override IEnumerable GetCommands(IList<(object key, Color color)> dataSet) { - foreach (IGrouping channelData in dataSet.Select(x => (((int channel, int key))x.Key, x.Value)) + foreach (IGrouping channelData in dataSet.Select(x => (((int channel, int key))x.key, x.color)) .GroupBy(x => x.Item1.channel)) { int channel = channelData.Key; diff --git a/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS2812USBUpdateQueue.cs b/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS2812USBUpdateQueue.cs index 242b6d1..623f0fe 100644 --- a/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS2812USBUpdateQueue.cs +++ b/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS2812USBUpdateQueue.cs @@ -36,7 +36,7 @@ namespace RGB.NET.Devices.WS281X.Bitwizard } /// - protected override IEnumerable GetCommands(Dictionary dataSet) + protected override IEnumerable GetCommands(IList<(object key, Color color)> dataSet) { foreach ((object key, Color value) in dataSet) yield return $"pix {(int)key} {value.AsRGBHexString(false)}"; diff --git a/RGB.NET.Devices.WS281X/Generic/SerialPortUpdateQueue.cs b/RGB.NET.Devices.WS281X/Generic/SerialPortUpdateQueue.cs index b3f2b4e..7bee162 100644 --- a/RGB.NET.Devices.WS281X/Generic/SerialPortUpdateQueue.cs +++ b/RGB.NET.Devices.WS281X/Generic/SerialPortUpdateQueue.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using RGB.NET.Core; namespace RGB.NET.Devices.WS281X @@ -56,9 +57,9 @@ namespace RGB.NET.Devices.WS281X } /// - protected override void Update(Dictionary dataSet) + protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) { - foreach (TData command in GetCommands(dataSet)) + foreach (TData command in GetCommands(dataSet.ToArray())) { SerialConnection.ReadTo(Prompt); SendCommand(command); @@ -70,7 +71,7 @@ namespace RGB.NET.Devices.WS281X /// /// The set of data that needs to be updated. /// The commands to be sent. - protected abstract IEnumerable GetCommands(Dictionary dataSet); + protected abstract IEnumerable GetCommands(IList<(object key, Color color)> dataSet); /// /// Sends a command as a string followed by a line-break to the device. diff --git a/RGB.NET.Devices.WS281X/NodeMCU/NodeMCUWS2812USBUpdateQueue.cs b/RGB.NET.Devices.WS281X/NodeMCU/NodeMCUWS2812USBUpdateQueue.cs index 2c020c4..ef25700 100644 --- a/RGB.NET.Devices.WS281X/NodeMCU/NodeMCUWS2812USBUpdateQueue.cs +++ b/RGB.NET.Devices.WS281X/NodeMCU/NodeMCUWS2812USBUpdateQueue.cs @@ -77,10 +77,9 @@ namespace RGB.NET.Devices.WS281X.NodeMCU } /// - protected override void Update(Dictionary dataSet) + protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) { - foreach (IGrouping channelData in dataSet.Select(x => (((int channel, int key))x.Key, x.Value)) - .GroupBy(x => x.Item1.channel)) + foreach (IGrouping channelData in dataSet.ToArray().Select(x => (((int channel, int key))x.key, x.color)).GroupBy(x => x.Item1.channel)) { byte[] buffer = GetBuffer(channelData); _sendDataAction(buffer); @@ -98,7 +97,7 @@ namespace RGB.NET.Devices.WS281X.NodeMCU _udpClient?.Send(buffer, buffer.Length); } - private byte[] GetBuffer(IGrouping data) + private byte[] GetBuffer(IGrouping data) { int channel = data.Key; byte[] buffer = _dataBuffer[channel]; @@ -106,8 +105,8 @@ namespace RGB.NET.Devices.WS281X.NodeMCU buffer[0] = GetSequenceNumber(channel); buffer[1] = (byte)channel; int i = 2; - foreach ((byte _, byte r, byte g, byte b) in data.OrderBy(x => x.Item1.key) - .Select(x => x.Value.GetRGBBytes())) + foreach ((byte _, byte r, byte g, byte b) in data.OrderBy(x => x.identifier.key) + .Select(x => x.color.GetRGBBytes())) { buffer[i++] = r; buffer[i++] = g; diff --git a/RGB.NET.Devices.Wooting/Generic/WootingUpdateQueue.cs b/RGB.NET.Devices.Wooting/Generic/WootingUpdateQueue.cs index 8f89610..da4b3bf 100644 --- a/RGB.NET.Devices.Wooting/Generic/WootingUpdateQueue.cs +++ b/RGB.NET.Devices.Wooting/Generic/WootingUpdateQueue.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System; using RGB.NET.Core; using RGB.NET.Devices.Wooting.Native; @@ -26,17 +26,17 @@ namespace RGB.NET.Devices.Wooting.Generic #region Methods /// - protected override void Update(Dictionary dataSet) + protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet) { - foreach (KeyValuePair data in dataSet) + foreach ((object key, Color color) in dataSet) { - (int row, int column) = ((int, int))data.Key; - _WootingSDK.ArraySetSingle((byte)row, (byte)column, data.Value.GetR(), data.Value.GetG(), data.Value.GetB()); + (int row, int column) = ((int, int))key; + _WootingSDK.ArraySetSingle((byte)row, (byte)column, color.GetR(), color.GetG(), color.GetB()); } _WootingSDK.ArrayUpdateKeyboard(); } - + #endregion } }