using System.Collections.Generic; using System.Xml.Serialization; using SabreTools.Core; using Newtonsoft.Json; using Newtonsoft.Json.Converters; namespace SabreTools.DatItems.Formats { /// /// Represents a single device on the machine /// [JsonObject("device"), XmlRoot("device")] public class Device : DatItem { #region Fields /// /// Device type /// [JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore)] [JsonConverter(typeof(StringEnumConverter))] [XmlElement("type")] public DeviceType DeviceType { get; set; } [JsonIgnore] public bool DeviceTypeSpecified { get { return DeviceType != DeviceType.NULL; } } /// /// Device tag /// [JsonProperty("tag", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("tag")] public string Tag { get; set; } /// /// Fixed image format /// [JsonProperty("fixed_image", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("fixed_image")] public string FixedImage { get; set; } /// /// Determines if the devices is mandatory /// /// Only value used seems to be 1. Used like bool, but actually int [JsonProperty("mandatory", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("mandatory")] public long? Mandatory { get; set; } [JsonIgnore] public bool MandatorySpecified { get { return Mandatory != null; } } /// /// Device interface /// [JsonProperty("interface", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("interface")] public string Interface { get; set; } /// /// Device instances /// [JsonProperty("instances", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("instances")] public List Instances { get; set; } [JsonIgnore] public bool InstancesSpecified { get { return Instances != null && Instances.Count > 0; } } /// /// Device extensions /// [JsonProperty("extensions", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("extensions")] public List Extensions { get; set; } [JsonIgnore] public bool ExtensionsSpecified { get { return Extensions != null && Extensions.Count > 0; } } #endregion #region Constructors /// /// Create a default, empty Device object /// public Device() { ItemType = ItemType.Device; } #endregion #region Cloning Methods 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, Remove = this.Remove, DeviceType = this.DeviceType, Tag = this.Tag, FixedImage = this.FixedImage, Mandatory = this.Mandatory, Interface = this.Interface, Instances = this.Instances, Extensions = this.Extensions, }; } #endregion #region Comparision Methods public override bool Equals(DatItem other) { // If we don't have a Device, return false if (ItemType != other.ItemType) return false; // Otherwise, treat it as a Device Device newOther = other as Device; // If the Device information matches bool match = (DeviceType == newOther.DeviceType && Tag == newOther.Tag && FixedImage == newOther.FixedImage && Mandatory == newOther.Mandatory && Interface == newOther.Interface); if (!match) return match; // If the instances match if (InstancesSpecified) { foreach (Instance instance in Instances) { match &= newOther.Instances.Contains(instance); } } // If the extensions match if (ExtensionsSpecified) { foreach (Extension extension in Extensions) { match &= newOther.Extensions.Contains(extension); } } return match; } #endregion } }