From 4a170e6af77918e87afa68cc594f57400255c76f Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sat, 2 Jan 2021 17:17:17 +0100 Subject: [PATCH] Extracted layout-logic to own project --- RGB.NET.Core/Color/RGBColor.cs | 7 ++- RGB.NET.Core/Devices/AbstractRGBDevice.cs | 3 - RGB.NET.Core/Devices/IRGBDeviceInfo.cs | 7 +-- RGB.NET.Core/Devices/RGBDeviceLighting.cs | 25 -------- RGB.NET.Core/Helper/ConversionHelper.cs | 2 +- RGB.NET.Core/Positioning/Shape.cs | 3 +- .../Layout => RGB.NET.Layout}/DeviceLayout.cs | 58 +++++++++---------- .../Layout => RGB.NET.Layout}/LedImage.cs | 7 ++- .../LedImageLayout.cs | 7 ++- .../Layout => RGB.NET.Layout}/LedLayout.cs | 17 +++--- RGB.NET.Layout/RGB.NET.Layout.csproj | 56 ++++++++++++++++++ RGB.NET.sln | 6 ++ 12 files changed, 112 insertions(+), 86 deletions(-) delete mode 100644 RGB.NET.Core/Devices/RGBDeviceLighting.cs rename {RGB.NET.Core/Devices/Layout => RGB.NET.Layout}/DeviceLayout.cs (72%) rename {RGB.NET.Core/Devices/Layout => RGB.NET.Layout}/LedImage.cs (80%) rename {RGB.NET.Core/Devices/Layout => RGB.NET.Layout}/LedImageLayout.cs (82%) rename {RGB.NET.Core/Devices/Layout => RGB.NET.Layout}/LedLayout.cs (93%) create mode 100644 RGB.NET.Layout/RGB.NET.Layout.csproj diff --git a/RGB.NET.Core/Color/RGBColor.cs b/RGB.NET.Core/Color/RGBColor.cs index ec7fbb2..edb3e1f 100644 --- a/RGB.NET.Core/Color/RGBColor.cs +++ b/RGB.NET.Core/Color/RGBColor.cs @@ -258,10 +258,11 @@ namespace RGB.NET.Core if ((hexString == null) || (hexString.Length < 6)) throw new ArgumentException("Invalid hex string", nameof(hexString)); - if (hexString[0] == '#') - hexString = hexString.Substring(1); + ReadOnlySpan span = hexString.AsSpan(); + if (span[0] == '#') + span = span[1..]; - byte[] data = ConversionHelper.HexToBytes(hexString); + byte[] data = ConversionHelper.HexToBytes(span); return data.Length switch { 3 => new Color(data[0], data[1], data[2]), diff --git a/RGB.NET.Core/Devices/AbstractRGBDevice.cs b/RGB.NET.Core/Devices/AbstractRGBDevice.cs index e79d582..bc8c0e6 100644 --- a/RGB.NET.Core/Devices/AbstractRGBDevice.cs +++ b/RGB.NET.Core/Devices/AbstractRGBDevice.cs @@ -2,12 +2,9 @@ // ReSharper disable UnusedMember.Global // ReSharper disable AutoPropertyCanBeMadeGetOnly.Global -using System; using System.Collections; using System.Collections.Generic; -using System.IO; using System.Linq; -using RGB.NET.Core.Layout; namespace RGB.NET.Core { diff --git a/RGB.NET.Core/Devices/IRGBDeviceInfo.cs b/RGB.NET.Core/Devices/IRGBDeviceInfo.cs index 00610bd..41379ff 100644 --- a/RGB.NET.Core/Devices/IRGBDeviceInfo.cs +++ b/RGB.NET.Core/Devices/IRGBDeviceInfo.cs @@ -28,12 +28,7 @@ namespace RGB.NET.Core /// Gets the model-name of the . /// string Model { get; } - - /// - /// Gets the lighting capability of the - /// - RGBDeviceLighting Lighting { get; } - + /// /// Gets the URI of an image of the or null if there is no image. /// diff --git a/RGB.NET.Core/Devices/RGBDeviceLighting.cs b/RGB.NET.Core/Devices/RGBDeviceLighting.cs deleted file mode 100644 index 70a0607..0000000 --- a/RGB.NET.Core/Devices/RGBDeviceLighting.cs +++ /dev/null @@ -1,25 +0,0 @@ -using RGB.NET.Core.Layout; - -namespace RGB.NET.Core -{ - /// - /// Contains a list of different lightning-modes used by . - /// - public enum RGBDeviceLighting - { - /// - /// The doesn't support lighting, - /// - None = 0, - - /// - /// The supports per-key-lightning. - /// - Key = 1, - - /// - /// The supports per-device-lightning. - /// - Device = 2, - } -} diff --git a/RGB.NET.Core/Helper/ConversionHelper.cs b/RGB.NET.Core/Helper/ConversionHelper.cs index b6355af..f1dfe70 100644 --- a/RGB.NET.Core/Helper/ConversionHelper.cs +++ b/RGB.NET.Core/Helper/ConversionHelper.cs @@ -37,7 +37,7 @@ namespace RGB.NET.Core /// /// The HEX-string to convert. /// The correspondending byte array. - public static byte[] HexToBytes(string hexString) + public static byte[] HexToBytes(ReadOnlySpan hexString) { if ((hexString.Length == 0) || ((hexString.Length % 2) != 0)) return Array.Empty(); diff --git a/RGB.NET.Core/Positioning/Shape.cs b/RGB.NET.Core/Positioning/Shape.cs index f4cdfe0..1965533 100644 --- a/RGB.NET.Core/Positioning/Shape.cs +++ b/RGB.NET.Core/Positioning/Shape.cs @@ -1,10 +1,9 @@ using System; -using RGB.NET.Core.Layout; namespace RGB.NET.Core { /// - /// Contains a list of different shapes used by . + /// Contains a list of different shapes used to define the layout of a LED. /// [Serializable] public enum Shape diff --git a/RGB.NET.Core/Devices/Layout/DeviceLayout.cs b/RGB.NET.Layout/DeviceLayout.cs similarity index 72% rename from RGB.NET.Core/Devices/Layout/DeviceLayout.cs rename to RGB.NET.Layout/DeviceLayout.cs index 267c7d0..dc87418 100644 --- a/RGB.NET.Core/Devices/Layout/DeviceLayout.cs +++ b/RGB.NET.Layout/DeviceLayout.cs @@ -3,8 +3,9 @@ using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Xml.Serialization; +using RGB.NET.Core; -namespace RGB.NET.Core.Layout +namespace RGB.NET.Layout { /// /// Represents the serializable layout of a . @@ -19,13 +20,13 @@ namespace RGB.NET.Core.Layout /// Gets or sets the name of the . /// [XmlElement("Name")] - public string Name { get; set; } + public string? Name { get; set; } /// /// Gets or sets the description of the . /// [XmlElement("Description")] - public string Description { get; set; } + public string? Description { get; set; } /// /// Gets or sets the of the . @@ -33,23 +34,17 @@ namespace RGB.NET.Core.Layout [XmlElement("Type")] public RGBDeviceType Type { get; set; } - /// - /// Gets or sets the of the . - /// - [XmlElement("Lighting")] - public RGBDeviceLighting Lighting { get; set; } - /// /// Gets or sets the vendor of the . /// [XmlElement("Vendor")] - public string Vendor { get; set; } + public string? Vendor { get; set; } /// /// Gets or sets the model of the . /// [XmlElement("Model")] - public string Model { get; set; } + public string? Model { get; set; } /// /// Gets or sets the of the . @@ -62,13 +57,13 @@ namespace RGB.NET.Core.Layout /// Gets or sets the width of the . /// [XmlElement("Width")] - public double Width { get; set; } + public double? Width { get; set; } /// /// Gets or sets the height of the . /// [XmlElement("Height")] - public double Height { get; set; } + public double? Height { get; set; } /// /// Gets or sets the width of one 'unit' used for the calculation of led positions and sizes. @@ -88,25 +83,25 @@ namespace RGB.NET.Core.Layout /// The path images for this device are collected in. /// [XmlElement("ImageBasePath")] - public string ImageBasePath { get; set; } + public string? ImageBasePath { get; set; } /// /// The image file for this device. /// [XmlElement("DeviceImage")] - public string DeviceImage { get; set; } + public string? DeviceImage { get; set; } /// /// Gets or sets a list of representing all the of the . /// [XmlArray("Leds")] - public List Leds { get; set; } = new List(); + public List Leds { get; set; } = new(); /// /// Gets or sets a list of representing the layouts for the images of all the of the . /// [XmlArray("LedImageLayouts")] - public List LedImageLayouts { get; set; } = new List(); + public List LedImageLayouts { get; set; } = new(); #endregion @@ -117,28 +112,27 @@ namespace RGB.NET.Core.Layout /// /// The path to the xml file. /// The deserialized . - public static DeviceLayout Load(string path) + public static DeviceLayout? Load(string path) { if (!File.Exists(path)) return null; try { - XmlSerializer serializer = new XmlSerializer(typeof(DeviceLayout)); - using (StreamReader reader = new StreamReader(path)) - { - DeviceLayout layout = serializer.Deserialize(reader) as DeviceLayout; - if (layout?.Leds != null) - { - LedLayout lastLed = null; - foreach (LedLayout led in layout.Leds) - { - led.CalculateValues(layout, lastLed); - lastLed = led; - } - } + XmlSerializer serializer = new(typeof(DeviceLayout)); + using StreamReader reader = new(path); - return layout; + DeviceLayout? layout = serializer.Deserialize(reader) as DeviceLayout; + if (layout?.Leds != null) + { + LedLayout? lastLed = null; + foreach (LedLayout led in layout.Leds) + { + led.CalculateValues(layout, lastLed); + lastLed = led; + } } + + return layout; } catch { diff --git a/RGB.NET.Core/Devices/Layout/LedImage.cs b/RGB.NET.Layout/LedImage.cs similarity index 80% rename from RGB.NET.Core/Devices/Layout/LedImage.cs rename to RGB.NET.Layout/LedImage.cs index c76d95a..997ac52 100644 --- a/RGB.NET.Core/Devices/Layout/LedImage.cs +++ b/RGB.NET.Layout/LedImage.cs @@ -1,7 +1,8 @@ using System; using System.Xml.Serialization; +using RGB.NET.Core; -namespace RGB.NET.Core.Layout +namespace RGB.NET.Layout { /// /// Represents the serializable image-data of a specific . @@ -14,12 +15,12 @@ namespace RGB.NET.Core.Layout /// Gets or sets the Id of the . /// [XmlAttribute("Id")] - public string Id { get; set; } + public string? Id { get; set; } /// /// Gets or sets the image of the . /// [XmlAttribute("Image")] - public string Image { get; set; } + public string? Image { get; set; } } } diff --git a/RGB.NET.Core/Devices/Layout/LedImageLayout.cs b/RGB.NET.Layout/LedImageLayout.cs similarity index 82% rename from RGB.NET.Core/Devices/Layout/LedImageLayout.cs rename to RGB.NET.Layout/LedImageLayout.cs index 2c3a491..8c0b776 100644 --- a/RGB.NET.Core/Devices/Layout/LedImageLayout.cs +++ b/RGB.NET.Layout/LedImageLayout.cs @@ -2,8 +2,9 @@ using System.Collections.Generic; using System.ComponentModel; using System.Xml.Serialization; +using RGB.NET.Core; -namespace RGB.NET.Core.Layout +namespace RGB.NET.Layout { /// /// Represents the serializable collection of for a specific layout. @@ -17,12 +18,12 @@ namespace RGB.NET.Core.Layout /// [XmlAttribute("Layout")] [DefaultValue(null)] - public string Layout { get; set; } + public string? Layout { get; set; } /// /// Gets or sets a list of representing the images of all the of the represented layout. /// [XmlArray("LedImages")] - public List LedImages { get; set; } = new List(); + public List LedImages { get; set; } = new(); } } diff --git a/RGB.NET.Core/Devices/Layout/LedLayout.cs b/RGB.NET.Layout/LedLayout.cs similarity index 93% rename from RGB.NET.Core/Devices/Layout/LedLayout.cs rename to RGB.NET.Layout/LedLayout.cs index c382520..3447f61 100644 --- a/RGB.NET.Core/Devices/Layout/LedLayout.cs +++ b/RGB.NET.Layout/LedLayout.cs @@ -2,8 +2,9 @@ using System.ComponentModel; using System.Globalization; using System.Xml.Serialization; +using RGB.NET.Core; -namespace RGB.NET.Core.Layout +namespace RGB.NET.Layout { /// /// Represents the serializable layout of a . @@ -18,7 +19,7 @@ namespace RGB.NET.Core.Layout /// Gets or sets the Id of the . /// [XmlAttribute("Id")] - public string Id { get; set; } + public string? Id { get; set; } /// /// Gets or sets the descriptive of the . @@ -70,7 +71,7 @@ namespace RGB.NET.Core.Layout /// Gets or sets the vecor-data representing a custom-shape of the . /// [XmlIgnore] - public string ShapeData { get; set; } + public string? ShapeData { get; set; } /// /// Gets or sets the x-position of the . @@ -105,7 +106,7 @@ namespace RGB.NET.Core.Layout /// /// The this belongs to. /// The previously calculated. - public void CalculateValues(DeviceLayout device, LedLayout lastLed) + public void CalculateValues(DeviceLayout device, LedLayout? lastLed) { if (!Enum.TryParse(DescriptiveShape, true, out Shape shape)) { @@ -136,19 +137,19 @@ namespace RGB.NET.Core.Layout return lastValue + lastSize; if (value.StartsWith("+", StringComparison.Ordinal)) - return lastValue + lastSize + double.Parse(value.Substring(1), CultureInfo.InvariantCulture); + return lastValue + lastSize + double.Parse(value[1..], CultureInfo.InvariantCulture); if (string.Equals(value, "-", StringComparison.Ordinal)) return lastValue - currentSize; if (value.StartsWith("-", StringComparison.Ordinal)) - return lastValue - currentSize - double.Parse(value.Substring(1), CultureInfo.InvariantCulture); + return lastValue - currentSize - double.Parse(value[1..], CultureInfo.InvariantCulture); if (string.Equals(value, "~", StringComparison.Ordinal)) return (lastValue + lastSize) - currentSize; if (value.StartsWith("~", StringComparison.Ordinal)) - return (lastValue + lastSize) - currentSize - double.Parse(value.Substring(1), CultureInfo.InvariantCulture); + return (lastValue + lastSize) - currentSize - double.Parse(value[1..], CultureInfo.InvariantCulture); return double.Parse(value, CultureInfo.InvariantCulture); } @@ -167,7 +168,7 @@ namespace RGB.NET.Core.Layout value = value.Replace(" ", string.Empty); if (value.EndsWith("mm", StringComparison.OrdinalIgnoreCase)) - return double.Parse(value.Substring(0, value.Length - 2), CultureInfo.InvariantCulture); + return double.Parse(value[..^2], CultureInfo.InvariantCulture); return unitSize * double.Parse(value, CultureInfo.InvariantCulture); } diff --git a/RGB.NET.Layout/RGB.NET.Layout.csproj b/RGB.NET.Layout/RGB.NET.Layout.csproj new file mode 100644 index 0000000..118af56 --- /dev/null +++ b/RGB.NET.Layout/RGB.NET.Layout.csproj @@ -0,0 +1,56 @@ + + + net5.0 + latest + enable + + Darth Affe + Wyrez + en-US + en-US + RGB.NET.Layout + RGB.NET.Layout + RGB.NET.Layout + RGB.NET.Layout + RGB.NET.Layout + Layout-Module of RGB.NET + Layout-Module of RGB.NET, a C# (.NET) library for accessing various RGB-peripherals + Copyright © Darth Affe 2021 + Copyright © Darth Affe 2021 + http://lib.arge.be/icon.png + https://github.com/DarthAffe/RGB.NET + https://raw.githubusercontent.com/DarthAffe/RGB.NET/master/LICENSE + Github + https://github.com/DarthAffe/RGB.NET + True + + + + 0.0.1 + 0.0.1 + 0.0.1 + + ..\bin\ + true + True + True + + + + $(DefineConstants);TRACE;DEBUG + true + full + false + + + + pdbonly + true + $(NoWarn);CS1591;CS1572;CS1573 + $(DefineConstants);RELEASE + + + + + + \ No newline at end of file diff --git a/RGB.NET.sln b/RGB.NET.sln index 9e742b4..39745c5 100644 --- a/RGB.NET.sln +++ b/RGB.NET.sln @@ -45,6 +45,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RGB.NET.Devices.Asus", "RGB EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RGB.NET.Devices.Wooting", "RGB.NET.Devices.Wooting\RGB.NET.Devices.Wooting.csproj", "{DD46DB2D-85BE-4962-86AE-E38C9053A548}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RGB.NET.Layout", "RGB.NET.Layout\RGB.NET.Layout.csproj", "{8AAB3736-B443-402C-B8AC-63D1A6DAFCCB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -123,6 +125,10 @@ Global {DD46DB2D-85BE-4962-86AE-E38C9053A548}.Debug|Any CPU.Build.0 = Debug|Any CPU {DD46DB2D-85BE-4962-86AE-E38C9053A548}.Release|Any CPU.ActiveCfg = Release|Any CPU {DD46DB2D-85BE-4962-86AE-E38C9053A548}.Release|Any CPU.Build.0 = Release|Any CPU + {8AAB3736-B443-402C-B8AC-63D1A6DAFCCB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8AAB3736-B443-402C-B8AC-63D1A6DAFCCB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8AAB3736-B443-402C-B8AC-63D1A6DAFCCB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8AAB3736-B443-402C-B8AC-63D1A6DAFCCB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE