Files
SabreTools/SabreTools.DatItems/Formats/Device.cs

159 lines
5.1 KiB
C#
Raw Normal View History

2020-09-02 17:09:19 -07:00
using System.Collections.Generic;
using System.Linq;
2020-09-07 22:00:02 -07:00
using System.Xml.Serialization;
2020-09-02 17:09:19 -07:00
using Newtonsoft.Json;
2020-09-07 22:00:02 -07:00
using Newtonsoft.Json.Converters;
2022-11-03 12:22:17 -07:00
using SabreTools.Core;
using SabreTools.Core.Tools;
2020-09-02 17:09:19 -07:00
2021-02-02 10:23:43 -08:00
namespace SabreTools.DatItems.Formats
2020-09-02 17:09:19 -07:00
{
/// <summary>
/// Represents a single device on the machine
/// </summary>
2020-09-08 10:12:41 -07:00
[JsonObject("device"), XmlRoot("device")]
2020-09-02 17:09:19 -07:00
public class Device : DatItem
{
#region Fields
/// <summary>
/// Device type
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("type")]
2020-09-07 22:00:02 -07:00
[JsonConverter(typeof(StringEnumConverter))]
public DeviceType DeviceType
{
get => _device.ReadString(Models.Internal.Device.DeviceTypeKey).AsDeviceType();
set => _device[Models.Internal.Device.DeviceTypeKey] = value.FromDeviceType();
}
2020-09-02 17:09:19 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool DeviceTypeSpecified { get { return DeviceType != DeviceType.NULL; } }
2020-09-02 17:09:19 -07:00
/// <summary>
/// Device tag
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("tag", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("tag")]
public string? Tag
{
get => _device.ReadString(Models.Internal.Device.TagKey);
set => _device[Models.Internal.Device.TagKey] = value;
}
2020-09-02 17:09:19 -07:00
/// <summary>
/// Fixed image format
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("fixed_image", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("fixed_image")]
public string? FixedImage
{
get => _device.ReadString(Models.Internal.Device.FixedImageKey);
set => _device[Models.Internal.Device.FixedImageKey] = value;
}
2020-09-02 17:09:19 -07:00
/// <summary>
/// Determines if the devices is mandatory
/// </summary>
2020-09-04 10:28:25 -07:00
/// <remarks>Only value used seems to be 1. Used like bool, but actually int</remarks>
2022-11-03 12:22:17 -07:00
[JsonProperty("mandatory", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("mandatory")]
public long? Mandatory
{
get => _device.ReadLong(Models.Internal.Device.MandatoryKey);
set => _device[Models.Internal.Device.MandatoryKey] = value;
}
2020-09-02 17:09:19 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool MandatorySpecified { get { return Mandatory != null; } }
2020-09-02 17:09:19 -07:00
/// <summary>
/// Device interface
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("interface", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("interface")]
public string? Interface
{
get => _device.ReadString(Models.Internal.Device.InterfaceKey);
set => _device[Models.Internal.Device.InterfaceKey] = value;
}
2020-09-02 17:09:19 -07:00
/// <summary>
/// Device instances
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("instances", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("instances")]
public List<Instance>? Instances
{
get => _device.Read<Instance[]>(Models.Internal.Device.InstanceKey)?.ToList();
set => _device[Models.Internal.Device.InstanceKey] = value?.ToArray();
}
2020-09-02 17:09:19 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool InstancesSpecified { get { return Instances != null && Instances.Count > 0; } }
2020-09-02 17:09:19 -07:00
/// <summary>
/// Device extensions
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("extensions", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("extensions")]
public List<Extension>? Extensions
{
get => _device.Read<Extension[]>(Models.Internal.Device.ExtensionKey)?.ToList();
set => _device[Models.Internal.Device.ExtensionKey] = value?.ToArray();
}
2020-09-02 17:09:19 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool ExtensionsSpecified { get { return Extensions != null && Extensions.Count > 0; } }
/// <summary>
/// Internal Device model
/// </summary>
[JsonIgnore]
private Models.Internal.Device _device = new();
2020-09-02 17:09:19 -07:00
#endregion
#region Constructors
/// <summary>
/// Create a default, empty Device object
/// </summary>
public Device()
{
ItemType = ItemType.Device;
}
#endregion
#region Cloning Methods
2022-11-03 12:22:17 -07:00
/// <inheritdoc/>
2020-09-02 17:09:19 -07:00
public override object Clone()
{
return new Device()
{
ItemType = this.ItemType,
DupeType = this.DupeType,
Machine = this.Machine?.Clone() as Machine,
Source = this.Source?.Clone() as Source,
2020-09-02 17:09:19 -07:00
Remove = this.Remove,
_device = this._device?.Clone() as Models.Internal.Device ?? new Models.Internal.Device(),
2020-09-02 17:09:19 -07:00
};
}
#endregion
#region Comparision Methods
2022-11-03 12:22:17 -07:00
/// <inheritdoc/>
public override bool Equals(DatItem? other)
2020-09-02 17:09:19 -07:00
{
// If we don't have a Adjuster, return false
if (ItemType != other?.ItemType || other is not Device otherInternal)
2020-09-02 17:09:19 -07:00
return false;
// Compare the internal models
return _device.EqualTo(otherInternal._device);
2020-09-02 17:09:19 -07:00
}
#endregion
}
}