From ce860d51ce164deeb7bf15f4414870466c717b3e Mon Sep 17 00:00:00 2001 From: RobertBeekman Date: Tue, 31 Oct 2023 21:10:35 +0100 Subject: [PATCH] Made custom data in layouts typed --- RGB.NET.Devices.Debug/DebugRGBDevice.cs | 2 +- RGB.NET.Layout/DeviceLayout.cs | 77 ++++++++++--------------- RGB.NET.Layout/IDeviceLayout.cs | 16 ++++- RGB.NET.Layout/ILedLayout.cs | 4 +- RGB.NET.Layout/LayoutExtension.cs | 4 +- RGB.NET.Layout/LedLayout.cs | 37 +++++++----- 6 files changed, 73 insertions(+), 67 deletions(-) diff --git a/RGB.NET.Devices.Debug/DebugRGBDevice.cs b/RGB.NET.Devices.Debug/DebugRGBDevice.cs index 61296ab6..77463bb6 100644 --- a/RGB.NET.Devices.Debug/DebugRGBDevice.cs +++ b/RGB.NET.Devices.Debug/DebugRGBDevice.cs @@ -27,7 +27,7 @@ public sealed class DebugRGBDevice : AbstractRGBDevice, IUnk /// Internal constructor of . /// internal DebugRGBDevice(IDeviceLayout layout, Action>? updateLedsAction = null) - : base(new DebugRGBDeviceInfo(layout.Type, layout.Vendor ?? "RGB.NET", layout.Model ?? "Debug", layout.CustomData), new DebugDeviceUpdateQueue()) + : base(new DebugRGBDeviceInfo(layout.Type, layout.Vendor ?? "RGB.NET", layout.Model ?? "Debug", layout.UntypedCustomData), new DebugDeviceUpdateQueue()) { this.Layout = layout; this._updateLedsAction = updateLedsAction; diff --git a/RGB.NET.Layout/DeviceLayout.cs b/RGB.NET.Layout/DeviceLayout.cs index 24efd371..6858ec60 100644 --- a/RGB.NET.Layout/DeviceLayout.cs +++ b/RGB.NET.Layout/DeviceLayout.cs @@ -14,7 +14,23 @@ namespace RGB.NET.Layout; /// [Serializable] [XmlRoot("Device")] -public class DeviceLayout : IDeviceLayout +public class DeviceLayout : DeviceLayout +{ } + +/// +/// Represents the serializable layout of a . +/// +[Serializable] +[XmlRoot("Device")] +public class DeviceLayout : DeviceLayout +{ } + +/// +/// Represents the serializable layout of a . +/// +[Serializable] +[XmlRoot("Device")] +public class DeviceLayout : IDeviceLayout { #region Properties & Fields @@ -92,7 +108,7 @@ public class DeviceLayout : IDeviceLayout /// Normally you should use to access this data. /// [XmlArray("Leds")] - public List InternalLeds { get; set; } = new(); + public List> InternalLeds { get; set; } = new(); /// /// Gets or sets a list of representing all the of the . @@ -101,15 +117,15 @@ public class DeviceLayout : IDeviceLayout public IEnumerable Leds => InternalLeds; /// - /// Gets or sets the internal custom data of this layout. - /// Normally you should use to access or set this data. + /// Gets or sets the custom data of this layout. /// [XmlElement("CustomData")] - public object? InternalCustomData { get; set; } + public TCustomData? CustomData { get; set; } - /// - [XmlIgnore] - public object? CustomData { get; set; } + /// + /// Gets the untyped custom data of this layout. + /// + public object? UntypedCustomData => CustomData; #endregion @@ -119,27 +135,21 @@ public class DeviceLayout : IDeviceLayout /// Creates a new from the specified xml. /// /// The stream that contains the layout to be loaded. - /// The type of the custom data. - /// The type of the custom data of the leds. - /// The deserialized . - public static DeviceLayout? Load(Stream stream, Type? customDeviceDataType = null, Type? customLedDataType = null) + /// The deserialized . + public static DeviceLayout? Load(Stream stream) { try { - XmlSerializer serializer = new(typeof(DeviceLayout)); - DeviceLayout? layout = serializer.Deserialize(stream) as DeviceLayout; - if (layout != null) - layout.CustomData = layout.GetCustomData(layout.InternalCustomData, customDeviceDataType); + XmlSerializer serializer = new(typeof(DeviceLayout)); + DeviceLayout? layout = serializer.Deserialize(stream) as DeviceLayout; if (layout?.InternalLeds != null) { - LedLayout? lastLed = null; - foreach (LedLayout led in layout.InternalLeds) + LedLayout? lastLed = null; + foreach (LedLayout led in layout.InternalLeds) { led.CalculateValues(layout, lastLed); lastLed = led; - - led.CustomData = layout.GetCustomData(led.InternalCustomData, customLedDataType); } } @@ -155,36 +165,13 @@ public class DeviceLayout : IDeviceLayout /// Creates a new from the specified xml. /// /// The path to the xml file. - /// The type of the custom data. - /// The type of the custom data of the leds. /// The deserialized . - public static DeviceLayout? Load(string path, Type? customDeviceDataType = null, Type? customLedDataType = null) + public static DeviceLayout? Load(string path) { if (!File.Exists(path)) return null; using Stream stream = File.OpenRead(path); - return Load(stream, customDeviceDataType, customLedDataType); - } - - /// - /// Gets the deserialized custom data. - /// - /// The internal custom data node. - /// The type of the custom data. - /// The deserialized custom data object. - protected virtual object? GetCustomData(object? customData, Type? type) - { - XmlNode? node = (customData as XmlNode) ?? (customData as IEnumerable)?.FirstOrDefault()?.ParentNode; //HACK DarthAffe 16.01.2021: This gives us the CustomData-Node - if ((node == null) || (type == null)) return null; - - using MemoryStream ms = new(); - using StreamWriter writer = new(ms); - - writer.Write(node.OuterXml); - writer.Flush(); - ms.Seek(0, SeekOrigin.Begin); - - return new XmlSerializer(type).Deserialize(ms); + return Load(stream); } #endregion diff --git a/RGB.NET.Layout/IDeviceLayout.cs b/RGB.NET.Layout/IDeviceLayout.cs index d55a7abf..c5f6760b 100644 --- a/RGB.NET.Layout/IDeviceLayout.cs +++ b/RGB.NET.Layout/IDeviceLayout.cs @@ -52,14 +52,24 @@ public interface IDeviceLayout /// Gets or sets the height of the . /// float Height { get; } + + /// + /// Gets or sets the width of one 'unit' used for the calculation of led positions and sizes. + /// + float LedUnitWidth { get; set; } + /// + /// Gets or sets the height of one 'unit' used for the calculation of led positions and sizes. + /// + float LedUnitHeight { get; set; } + /// /// Gets or sets a list of representing all the of the . /// IEnumerable Leds { get; } - + /// - /// Gets the the custom data associated with the device. + /// Gets the untyped custom data of this layout. /// - object? CustomData { get; } + object? UntypedCustomData { get; } } \ No newline at end of file diff --git a/RGB.NET.Layout/ILedLayout.cs b/RGB.NET.Layout/ILedLayout.cs index 65c1ab79..b5f8965f 100644 --- a/RGB.NET.Layout/ILedLayout.cs +++ b/RGB.NET.Layout/ILedLayout.cs @@ -43,7 +43,7 @@ public interface ILedLayout float Height { get; } /// - /// Gets the the custom data associated with the LED. + /// Gets the untyped custom data of this layout. /// - object? CustomData { get; } + object? UntypedCustomData { get; } } \ No newline at end of file diff --git a/RGB.NET.Layout/LayoutExtension.cs b/RGB.NET.Layout/LayoutExtension.cs index a6666372..495592ad 100644 --- a/RGB.NET.Layout/LayoutExtension.cs +++ b/RGB.NET.Layout/LayoutExtension.cs @@ -20,7 +20,7 @@ public static class LayoutExtension public static void ApplyTo(this IDeviceLayout layout, IRGBDevice device, bool createMissingLeds = false, bool removeExcessiveLeds = false) { device.Size = new Size(layout.Width, layout.Height); - device.DeviceInfo.LayoutMetadata = layout.CustomData; + device.DeviceInfo.LayoutMetadata = layout.UntypedCustomData; HashSet ledIds = new(); foreach (ILedLayout layoutLed in layout.Leds) @@ -39,7 +39,7 @@ public static void ApplyTo(this IDeviceLayout layout, IRGBDevice device, bool cr led.Size = new Size(layoutLed.Width, layoutLed.Height); led.Shape = layoutLed.Shape; led.ShapeData = layoutLed.ShapeData; - led.LayoutMetadata = layoutLed.CustomData; + led.LayoutMetadata = layoutLed.UntypedCustomData; } } } diff --git a/RGB.NET.Layout/LedLayout.cs b/RGB.NET.Layout/LedLayout.cs index 57fdd361..c8584c64 100644 --- a/RGB.NET.Layout/LedLayout.cs +++ b/RGB.NET.Layout/LedLayout.cs @@ -11,7 +11,15 @@ namespace RGB.NET.Layout; /// [Serializable] [XmlType("Led")] -public class LedLayout : ILedLayout +public class LedLayout : LedLayout +{ } + +/// +/// Represents the serializable layout of a . +/// +[Serializable] +[XmlType("Led")] +public class LedLayout : ILedLayout { #region Properties & Fields @@ -61,17 +69,6 @@ public class LedLayout : ILedLayout [DefaultValue("1.0")] public string DescriptiveHeight { get; set; } = "1.0"; - /// - /// Gets or sets the internal custom data of this layout. - /// Normally you should use to access or set this data. - /// - [XmlElement("CustomData")] - public object? InternalCustomData { get; set; } - - /// - [XmlIgnore] - public object? CustomData { get; set; } - /// /// Gets or sets the of the . /// @@ -108,6 +105,17 @@ public class LedLayout : ILedLayout [XmlIgnore] public float Height { get; private set; } + /// + /// Gets or sets the custom data of this layout. + /// + [XmlElement("CustomData")] + public TCustomLedData? CustomData { get; set; } + + /// + /// Gets the untyped custom data of this layout. + /// + public object? UntypedCustomData => CustomData; + #endregion #region Methods @@ -117,13 +125,14 @@ public class LedLayout : ILedLayout /// /// The this belongs to. /// The previously calculated. - public virtual void CalculateValues(DeviceLayout device, LedLayout? lastLed) + public virtual void CalculateValues(IDeviceLayout device, ILedLayout? lastLed) { if (!Enum.TryParse(DescriptiveShape, true, out Shape shape)) { shape = Shape.Custom; ShapeData = DescriptiveShape; } + Shape = shape; Width = GetSizeValue(DescriptiveWidth, device.LedUnitWidth); @@ -204,4 +213,4 @@ protected virtual float GetSizeValue(string value, float unitSize) } #endregion -} \ No newline at end of file +}