using System; using System.Collections.Generic; using System.IO; using System.Linq; using RGB.NET.Layout; namespace Artemis.Core { /// /// Represents a device layout decorated with extra Artemis-specific data /// public class ArtemisLayout { /// /// Creates a new instance of the class /// /// The path of the layout XML file /// The source from where this layout is being loaded public ArtemisLayout(string filePath, LayoutSource source) { FilePath = filePath; Source = source; Leds = new List(); LoadLayout(); } /// /// Gets the file path the layout was (attempted to be) loaded from /// public string FilePath { get; } /// /// Gets the source from where this layout was loaded /// public LayoutSource Source { get; } /// /// Gets the device this layout is applied to /// public ArtemisDevice? Device { get; private set; } /// /// Gets a boolean indicating whether a valid layout was loaded /// public bool IsValid { get; private set; } /// /// Gets the image of the device /// public Uri? Image { get; private set; } /// /// Gets a list of LEDs this layout contains /// public List Leds { get; } /// /// Gets the RGB.NET device layout /// public DeviceLayout RgbLayout { get; private set; } = null!; /// /// Gets the custom layout data embedded in the RGB.NET layout /// public LayoutCustomDeviceData LayoutCustomDeviceData { get; private set; } = null!; internal void ApplyDevice(ArtemisDevice artemisDevice) { Device = artemisDevice; foreach (ArtemisLedLayout artemisLedLayout in Leds) artemisLedLayout.ApplyDevice(Device); } private void LoadLayout() { DeviceLayout? deviceLayout = DeviceLayout.Load(FilePath, typeof(LayoutCustomDeviceData), typeof(LayoutCustomLedData)); if (deviceLayout != null) { RgbLayout = deviceLayout; IsValid = true; } else { RgbLayout = new DeviceLayout(); IsValid = false; } if (IsValid) Leds.AddRange(RgbLayout.Leds.Select(l => new ArtemisLedLayout(this, l))); LayoutCustomDeviceData = (LayoutCustomDeviceData?) RgbLayout.CustomData ?? new LayoutCustomDeviceData(); ApplyCustomDeviceData(); } private void ApplyCustomDeviceData() { if (!IsValid) { Image = null; return; } Uri layoutDirectory = new(Path.GetDirectoryName(FilePath)! + "/", UriKind.Absolute); if (LayoutCustomDeviceData.DeviceImage != null) Image = new Uri(layoutDirectory, new Uri(LayoutCustomDeviceData.DeviceImage, UriKind.Relative)); else Image = null; } } /// /// Represents a source from where a layout came /// public enum LayoutSource { /// /// A layout loaded from config /// Configured, /// /// A layout loaded from the user layout folder /// User, /// /// A layout loaded from the plugin folder /// Plugin, /// /// A default layout loaded as a fallback option /// Default } }