Files
SabreTools.Serialization/SabreTools.Metadata.DatItems/Formats/Device.cs
Matt Nadareski 5ffc9fef43 Cleanup around DatItem and DatItem<T>
This is both big and not big. The not big part of this is that I essentially just moved some stuff up to the typed type that used to live in the untyped type. The big part is that this allows every single DatItem implementation to be significantly cleaner with their implementations of the methods and internal model.
2026-04-05 01:36:32 -04:00

155 lines
4.1 KiB
C#

using System;
using System.Xml.Serialization;
using Newtonsoft.Json;
namespace SabreTools.Metadata.DatItems.Formats
{
/// <summary>
/// Represents a single device on the machine
/// </summary>
[JsonObject("device"), XmlRoot("device")]
public sealed class Device : DatItem<Data.Models.Metadata.Device>
{
#region Properties
public Data.Models.Metadata.DeviceType? DeviceType
{
get => _internal.DeviceType;
set => _internal.DeviceType = value;
}
public Extension[]? Extension { get; set; }
[JsonIgnore]
public bool ExtensionSpecified => Extension is not null && Extension.Length > 0;
public string? FixedImage
{
get => _internal.FixedImage;
set => _internal.FixedImage = value;
}
public Instance? Instance { get; set; }
[JsonIgnore]
public bool InstanceSpecified => Instance is not null;
public string? Interface
{
get => _internal.Interface;
set => _internal.Interface = value;
}
/// <inheritdoc>/>
public override Data.Models.Metadata.ItemType ItemType
=> Data.Models.Metadata.ItemType.Device;
public bool? Mandatory
{
get => _internal.Mandatory;
set => _internal.Mandatory = value;
}
public string? Tag
{
get => _internal.Tag;
set => _internal.Tag = value;
}
#endregion
#region Constructors
public Device() : base() { }
public Device(Data.Models.Metadata.Device item) : base(item)
{
// Handle subitems
if (item.Extension is not null)
Extension = Array.ConvertAll(item.Extension, extension => new Extension(extension)); ;
if (item.Instance is not null)
Instance = new Instance(item.Instance);
}
public Device(Data.Models.Metadata.Device item, Machine machine, Source source) : this(item)
{
Source = source;
CopyMachineInformation(machine);
}
#endregion
#region Accessors
/// <inheritdoc/>
public override string? GetName() => null;
/// <inheritdoc/>
public override void SetName(string? name) { }
#endregion
#region Cloning Methods
/// <inheritdoc/>
public override object Clone() => new Device(GetInternalClone());
/// <inheritdoc/>
public override Data.Models.Metadata.Device GetInternalClone()
{
var deviceItem = _internal.Clone() as Data.Models.Metadata.Device ?? new();
deviceItem.DeviceType = DeviceType;
deviceItem.FixedImage = FixedImage;
deviceItem.Interface = Interface;
deviceItem.Mandatory = Mandatory;
deviceItem.Tag = Tag;
if (Instance is not null)
deviceItem.Instance = Instance.GetInternalClone();
if (Extension is not null)
deviceItem.Extension = Array.ConvertAll(Extension, extension => extension.GetInternalClone()); ;
return deviceItem;
}
#endregion
#region Comparision Methods
/// <inheritdoc/>
public override bool Equals(DatItem? other)
{
// If the other item is null
if (other is null)
return false;
// If the type matches
if (other is Device otherDevice)
return _internal.Equals(otherDevice._internal);
// Everything else fails
return false;
}
/// <inheritdoc/>
public override bool Equals(DatItem<Data.Models.Metadata.Device>? other)
{
// If the other value is invalid
if (other is null)
return false;
// If the type matches
if (other is Device otherDevice)
return _internal.Equals(otherDevice._internal);
// Everything else fails
return false;
}
#endregion
}
}