Files
SabreTools/SabreTools.DatItems/Device.cs

361 lines
11 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
2020-12-08 13:23:59 -08:00
using SabreTools.Core;
2020-12-08 13:48:57 -08:00
using SabreTools.Filtering;
2020-09-02 17:09:19 -07:00
using Newtonsoft.Json;
2020-09-07 22:00:02 -07:00
using Newtonsoft.Json.Converters;
2020-09-02 17:09:19 -07:00
2020-12-08 15:15:41 -08:00
namespace SabreTools.DatItems
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>
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[JsonConverter(typeof(StringEnumConverter))]
[XmlElement("type")]
2020-09-07 00:39:59 -07:00
public DeviceType DeviceType { get; set; }
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>
[JsonProperty("tag", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("tag")]
2020-09-02 17:09:19 -07:00
public string Tag { get; set; }
/// <summary>
/// Fixed image format
/// </summary>
[JsonProperty("fixed_image", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("fixed_image")]
2020-09-02 17:09:19 -07:00
public string FixedImage { get; set; }
/// <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>
2020-09-02 17:09:19 -07:00
[JsonProperty("mandatory", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("mandatory")]
2020-09-07 12:34:18 -07:00
public long? Mandatory { get; set; }
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>
[JsonProperty("interface", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("interface")]
2020-09-02 17:09:19 -07:00
public string Interface { get; set; }
/// <summary>
/// Device instances
/// </summary>
[JsonProperty("instances", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("instances")]
2020-09-02 17:09:19 -07:00
public List<Instance> Instances { get; set; }
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>
[JsonProperty("extensions", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("extensions")]
2020-09-02 17:09:19 -07:00
public List<Extension> Extensions { get; set; }
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool ExtensionsSpecified { get { return Extensions != null && Extensions.Count > 0; } }
2020-09-02 17:09:19 -07:00
#endregion
#region Accessors
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="mappings">Mappings dictionary</param>
public override void SetFields(Dictionary<Field, string> mappings)
{
// Set base fields
base.SetFields(mappings);
// Handle Device-specific fields
if (mappings.Keys.Contains(Field.DatItem_DeviceType))
2020-09-07 00:39:59 -07:00
DeviceType = mappings[Field.DatItem_DeviceType].AsDeviceType();
2020-09-02 17:09:19 -07:00
if (mappings.Keys.Contains(Field.DatItem_Tag))
Tag = mappings[Field.DatItem_Tag];
if (mappings.Keys.Contains(Field.DatItem_FixedImage))
FixedImage = mappings[Field.DatItem_FixedImage];
if (mappings.Keys.Contains(Field.DatItem_Mandatory))
2020-09-07 12:34:18 -07:00
Mandatory = Sanitizer.CleanLong(mappings[Field.DatItem_Mandatory]);
2020-09-02 17:09:19 -07:00
if (mappings.Keys.Contains(Field.DatItem_Interface))
Interface = mappings[Field.DatItem_Interface];
2020-09-30 13:25:40 -07:00
if (InstancesSpecified)
2020-09-03 15:02:59 -07:00
{
foreach (Instance instance in Instances)
{
instance.SetFields(mappings);
}
}
2020-09-30 13:25:40 -07:00
if (ExtensionsSpecified)
2020-09-03 15:02:59 -07:00
{
foreach (Extension extension in Extensions)
{
extension.SetFields(mappings);
}
}
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
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
2020-09-03 15:02:59 -07:00
bool match = (DeviceType == newOther.DeviceType
2020-09-02 17:09:19 -07:00
&& Tag == newOther.Tag
&& FixedImage == newOther.FixedImage
&& Mandatory == newOther.Mandatory
&& Interface == newOther.Interface);
2020-09-03 15:02:59 -07:00
if (!match)
return match;
// If the instances match
2020-09-30 13:25:40 -07:00
if (InstancesSpecified)
2020-09-03 15:02:59 -07:00
{
foreach (Instance instance in Instances)
{
match &= newOther.Instances.Contains(instance);
}
}
// If the extensions match
2020-09-30 13:25:40 -07:00
if (ExtensionsSpecified)
2020-09-03 15:02:59 -07:00
{
foreach (Extension extension in Extensions)
{
match &= newOther.Extensions.Contains(extension);
}
}
2020-09-02 17:09:19 -07:00
2020-09-03 15:02:59 -07:00
return match;
2020-09-02 17:09:19 -07:00
}
#endregion
#region Filtering
/// <summary>
/// Check to see if a DatItem passes the filter
/// </summary>
/// <param name="filter">Filter to check against</param>
/// <param name="sub">True if this is a subitem, false otherwise</param>
2020-09-02 17:09:19 -07:00
/// <returns>True if the item passed the filter, false otherwise</returns>
public override bool PassesFilter(Filter filter, bool sub = false)
2020-09-02 17:09:19 -07:00
{
// Check common fields first
if (!base.PassesFilter(filter, sub))
2020-09-02 17:09:19 -07:00
return false;
// Filter on device type
2020-09-07 00:39:59 -07:00
if (filter.DatItem_DeviceType.MatchesPositive(DeviceType.NULL, DeviceType) == false)
2020-09-02 17:09:19 -07:00
return false;
2020-09-07 00:39:59 -07:00
if (filter.DatItem_DeviceType.MatchesNegative(DeviceType.NULL, DeviceType) == true)
2020-09-02 17:09:19 -07:00
return false;
// Filter on tag
if (!filter.PassStringFilter(filter.DatItem_Tag, Tag))
2020-09-02 17:09:19 -07:00
return false;
// Filter on fixed image
if (!filter.PassStringFilter(filter.DatItem_FixedImage, FixedImage))
2020-09-02 17:09:19 -07:00
return false;
// Filter on mandatory
if (!filter.PassLongFilter(filter.DatItem_Mandatory, Mandatory))
2020-09-02 17:09:19 -07:00
return false;
// Filter on interface
if (!filter.PassStringFilter(filter.DatItem_Interface, Interface))
2020-09-02 17:09:19 -07:00
return false;
2020-09-03 15:02:59 -07:00
// Filter on individual instances
2020-09-30 13:25:40 -07:00
if (InstancesSpecified)
2020-09-03 15:02:59 -07:00
{
foreach (Instance instance in Instances)
{
2020-09-30 13:25:40 -07:00
if (!instance.PassesFilter(filter, true))
2020-09-03 15:02:59 -07:00
return false;
}
}
// Filter on individual extensions
2020-09-30 13:25:40 -07:00
if (ExtensionsSpecified)
2020-09-03 15:02:59 -07:00
{
foreach (Extension extension in Extensions)
{
2020-09-30 13:25:40 -07:00
if (!extension.PassesFilter(filter, true))
2020-09-03 15:02:59 -07:00
return false;
}
}
2020-09-02 17:09:19 -07:00
return true;
}
/// <summary>
/// Remove fields from the DatItem
/// </summary>
/// <param name="fields">List of Fields to remove</param>
public override void RemoveFields(List<Field> fields)
{
// Remove common fields first
base.RemoveFields(fields);
// Remove the fields
if (fields.Contains(Field.DatItem_DeviceType))
2020-09-07 00:39:59 -07:00
DeviceType = DeviceType.NULL;
2020-09-02 17:09:19 -07:00
if (fields.Contains(Field.DatItem_Tag))
Tag = null;
if (fields.Contains(Field.DatItem_FixedImage))
FixedImage = null;
if (fields.Contains(Field.DatItem_Mandatory))
Mandatory = null;
if (fields.Contains(Field.DatItem_Interface))
Interface = null;
2020-09-30 13:25:40 -07:00
if (InstancesSpecified)
2020-09-03 15:02:59 -07:00
{
foreach (Instance instance in Instances)
{
instance.RemoveFields(fields);
}
}
2020-09-30 13:25:40 -07:00
if (ExtensionsSpecified)
2020-09-03 15:02:59 -07:00
{
foreach (Extension extension in Extensions)
{
extension.RemoveFields(fields);
}
}
2020-09-02 17:09:19 -07:00
}
#endregion
#region Sorting and Merging
/// <summary>
/// Replace fields from another item
/// </summary>
/// <param name="item">DatItem to pull new information from</param>
/// <param name="fields">List of Fields representing what should be updated</param>
public override void ReplaceFields(DatItem item, List<Field> fields)
{
// Replace common fields first
base.ReplaceFields(item, fields);
// If we don't have a Device to replace from, ignore specific fields
if (item.ItemType != ItemType.Device)
return;
// Cast for easier access
Device newItem = item as Device;
// Replace the fields
if (fields.Contains(Field.DatItem_DeviceType))
DeviceType = newItem.DeviceType;
if (fields.Contains(Field.DatItem_Tag))
Tag = newItem.Tag;
if (fields.Contains(Field.DatItem_FixedImage))
FixedImage = newItem.FixedImage;
if (fields.Contains(Field.DatItem_Mandatory))
Mandatory = newItem.Mandatory;
if (fields.Contains(Field.DatItem_Interface))
Interface = newItem.Interface;
2020-09-03 15:02:59 -07:00
// DatItem_Instance_* doesn't make sense here
// since not every instance under the other item
// can replace every instance under this item
// DatItem_Extension_* doesn't make sense here
// since not every extension under the other item
// can replace every extension under this item
2020-09-02 17:09:19 -07:00
}
#endregion
}
}