1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-12 17:48:31 +00:00

Added extension to save layouts

This commit is contained in:
Darth Affe 2023-10-31 22:14:05 +01:00
parent 188de3c558
commit be252790d4

View File

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using RGB.NET.Core;
namespace RGB.NET.Layout;
@ -51,4 +53,48 @@ public static class LayoutExtension
device.RemoveLed(led);
}
}
/// <summary>
/// Saves the specified layout to the given location.
/// </summary>
/// <param name="layout">The layout to save.</param>
/// <param name="targetFile">The location to save to.</param>
public static void Save(this IDeviceLayout layout, string targetFile)
{
using FileStream fs = new(targetFile, FileMode.Create);
layout.Save(fs);
}
/// <summary>
/// Saves the specified layout to the given stream.
/// </summary>
/// <param name="layout">The layout to save.</param>
/// <param name="stream">The stream to save to.</param>
public static void Save(this IDeviceLayout layout, Stream stream)
{
Type? customDataType = layout.CustomData?.GetType();
Type? customLedDataType = layout.Leds.FirstOrDefault(x => x.CustomData != null)?.GetType();
Type[] customTypes;
if ((customDataType != null) && (customLedDataType != null))
customTypes = new[] { customDataType, customLedDataType };
else if (customDataType != null)
customTypes = new[] { customDataType };
else if (customLedDataType != null)
customTypes = new[] { customLedDataType };
else
customTypes = Array.Empty<Type>();
if (layout is DeviceLayout deviceLayout)
{
deviceLayout.InternalCustomData = deviceLayout.CustomData;
foreach (ILedLayout led in deviceLayout.Leds)
if (led is LedLayout ledLayout)
ledLayout.InternalCustomData = ledLayout.CustomData;
}
XmlSerializer serializer = new(typeof(DeviceLayout), null, customTypes, null, null);
serializer.Serialize(stream, layout);
}
}