-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathDeviceLayout.cs
191 lines (161 loc) · 6.28 KB
/
DeviceLayout.cs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
using RGB.NET.Core;
namespace RGB.NET.Layout;
/// <summary>
/// Represents the serializable layout of a <see cref="IRGBDevice"/>.
/// </summary>
[Serializable]
[XmlRoot("Device")]
public class DeviceLayout : IDeviceLayout
{
#region Properties & Fields
/// <summary>
/// Gets or sets the name of the <see cref="DeviceLayout"/>.
/// </summary>
[XmlElement("Name")]
public string? Name { get; set; }
/// <summary>
/// Gets or sets the description of the <see cref="DeviceLayout"/>.
/// </summary>
[XmlElement("Description")]
public string? Description { get; set; }
/// <summary>
/// Gets or sets the author of the <see cref="DeviceLayout"/>.
/// </summary>
[XmlElement("Author")]
public string? Author { get; set; }
/// <summary>
/// Gets or sets the <see cref="RGBDeviceType"/> of the <see cref="DeviceLayout"/>.
/// </summary>
[XmlElement("Type")]
public RGBDeviceType Type { get; set; }
/// <summary>
/// Gets or sets the vendor of the <see cref="DeviceLayout"/>.
/// </summary>
[XmlElement("Vendor")]
public string? Vendor { get; set; }
/// <summary>
/// Gets or sets the model of the <see cref="DeviceLayout"/>.
/// </summary>
[XmlElement("Model")]
public string? Model { get; set; }
/// <summary>
/// Gets or sets the <see cref="Core.Shape"/> of the <see cref="DeviceLayout"/>.
/// </summary>
[XmlElement("Shape")]
[DefaultValue(Shape.Rectangle)]
public Shape Shape { get; set; } = Shape.Rectangle;
/// <summary>
/// Gets or sets the width of the <see cref="DeviceLayout"/>.
/// </summary>
[XmlElement("Width")]
public float Width { get; set; }
/// <summary>
/// Gets or sets the height of the <see cref="DeviceLayout"/>.
/// </summary>
[XmlElement("Height")]
public float Height { get; set; }
/// <summary>
/// Gets or sets the width of one 'unit' used for the calculation of led positions and sizes.
/// </summary>
[XmlElement("LedUnitWidth")]
[DefaultValue(19.0)]
public float LedUnitWidth { get; set; } = 19.0f;
/// <summary>
/// Gets or sets the height of one 'unit' used for the calculation of led positions and sizes.
/// </summary>
[XmlElement("LedUnitHeight")]
[DefaultValue(19.0)]
public float LedUnitHeight { get; set; } = 19.0f;
/// <summary>
/// Gets or sets the internal list of led layouts.
/// Normally you should use <see cref="Leds"/> to access this data.
/// </summary>
[XmlArray("Leds")]
public List<LedLayout> InternalLeds { get; set; } = [];
/// <summary>
/// Gets or sets a list of <see cref="LedLayout"/> representing all the <see cref="Led"/> of the <see cref="DeviceLayout"/>.
/// </summary>
[XmlIgnore]
public IEnumerable<ILedLayout> Leds => InternalLeds;
/// <summary>
/// Gets or sets the internal custom data of this layout.
/// Normally you should use <see cref="CustomData"/> to access or set this data.
/// </summary>
[XmlElement("CustomData")]
public object? InternalCustomData { get; set; }
/// <inheritdoc />
[XmlIgnore]
public object? CustomData { get; set; }
#endregion
#region Methods
/// <summary>
/// Creates a new <see cref="DeviceLayout"/> from the specified xml.
/// </summary>
/// <param name="stream">The stream that contains the layout to be loaded.</param>
/// <param name="customDeviceDataType">The type of the custom data.</param>
/// <param name="customLedDataType">The type of the custom data of the leds.</param>
/// <returns>The deserialized <see cref="DeviceLayout"/>.</returns>
public static DeviceLayout? Load(Stream stream, Type? customDeviceDataType = null, Type? customLedDataType = null)
{
try
{
XmlSerializer serializer = new(typeof(DeviceLayout));
DeviceLayout? layout = serializer.Deserialize(stream) as DeviceLayout;
if (layout != null)
layout.CustomData = layout.GetCustomData(layout.InternalCustomData, customDeviceDataType);
if (layout?.InternalLeds != null)
{
LedLayout? lastLed = null;
foreach (LedLayout led in layout.InternalLeds)
{
led.CalculateValues(layout, lastLed);
lastLed = led;
led.CustomData = layout.GetCustomData(led.InternalCustomData, customLedDataType);
}
}
return layout;
}
catch
{
return null;
}
}
/// <summary>
/// Creates a new <see cref="DeviceLayout"/> from the specified xml.
/// </summary>
/// <param name="path">The path to the xml file.</param>
/// <param name="customDeviceDataType">The type of the custom data.</param>
/// <param name="customLedDataType">The type of the custom data of the leds.</param>
/// <returns>The deserialized <see cref="DeviceLayout"/>.</returns>
public static DeviceLayout? Load(string path, Type? customDeviceDataType = null, Type? customLedDataType = null)
{
if (!File.Exists(path)) return null;
using Stream stream = File.OpenRead(path);
return Load(stream, customDeviceDataType, customLedDataType);
}
/// <summary>
/// Gets the deserialized custom data.
/// </summary>
/// <param name="customData">The internal custom data node.</param>
/// <param name="type">The type of the custom data.</param>
/// <returns>The deserialized custom data object.</returns>
protected virtual object? GetCustomData(object? customData, Type? type)
{
XmlNode? node = (customData as XmlNode) ?? (customData as IEnumerable<XmlNode>)?.FirstOrDefault(x => x.ParentNode != null)?.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);
}
#endregion
}