diff --git a/RGB.NET.Layout/LayoutExtension.cs b/RGB.NET.Layout/LayoutExtension.cs
index a666637..915380b 100644
--- a/RGB.NET.Layout/LayoutExtension.cs
+++ b/RGB.NET.Layout/LayoutExtension.cs
@@ -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);
}
}
+
+ ///
+ /// Saves the specified layout to the given location.
+ ///
+ /// The layout to save.
+ /// The location to save to.
+ public static void Save(this IDeviceLayout layout, string targetFile)
+ {
+ using FileStream fs = new(targetFile, FileMode.Create);
+ layout.Save(fs);
+ }
+
+ ///
+ /// Saves the specified layout to the given stream.
+ ///
+ /// The layout to save.
+ /// The stream to save to.
+ 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();
+
+ 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);
+ }
}
\ No newline at end of file