2020-09-02 17:22:31 -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:22:31 -07:00
|
|
|
|
|
|
|
|
|
|
using SabreTools.Library.Filtering;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.Library.DatItems
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents a single port on a machine
|
|
|
|
|
|
/// </summary>
|
2020-09-08 10:12:41 -07:00
|
|
|
|
[JsonObject("port"), XmlRoot("port")]
|
2020-09-02 17:22:31 -07:00
|
|
|
|
public class Port : DatItem
|
|
|
|
|
|
{
|
|
|
|
|
|
#region Fields
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Tag for the port
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonProperty("tag", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
2020-09-07 22:00:02 -07:00
|
|
|
|
[XmlElement("tag")]
|
2020-09-02 17:22:31 -07:00
|
|
|
|
public string Tag { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// List of analogs on the port
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonProperty("analogs", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
2020-09-07 22:00:02 -07:00
|
|
|
|
[XmlElement("analogs")]
|
2020-09-02 17:22:31 -07:00
|
|
|
|
public List<Analog> Analogs { get; set; }
|
|
|
|
|
|
|
2020-09-07 22:00:02 -07:00
|
|
|
|
[JsonIgnore]
|
|
|
|
|
|
public bool AnalogsSpecified { get { return Analogs != null && Analogs.Count > 0; } }
|
|
|
|
|
|
|
2020-09-02 17:22:31 -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 Port-specific fields
|
2020-09-02 21:36:14 -07:00
|
|
|
|
if (mappings.Keys.Contains(Field.DatItem_Tag))
|
|
|
|
|
|
Tag = mappings[Field.DatItem_Tag];
|
2020-09-02 17:22:31 -07:00
|
|
|
|
|
2020-09-03 15:02:59 -07:00
|
|
|
|
if (Analogs != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (Analog analog in Analogs)
|
|
|
|
|
|
{
|
|
|
|
|
|
analog.SetFields(mappings);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-09-02 17:22:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a default, empty Port object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Port()
|
|
|
|
|
|
{
|
|
|
|
|
|
ItemType = ItemType.Port;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Cloning Methods
|
|
|
|
|
|
|
|
|
|
|
|
public override object Clone()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Port()
|
|
|
|
|
|
{
|
|
|
|
|
|
ItemType = this.ItemType,
|
|
|
|
|
|
DupeType = this.DupeType,
|
|
|
|
|
|
|
|
|
|
|
|
Machine = this.Machine.Clone() as Machine,
|
|
|
|
|
|
Source = this.Source.Clone() as Source,
|
|
|
|
|
|
Remove = this.Remove,
|
|
|
|
|
|
|
|
|
|
|
|
Tag = this.Tag,
|
|
|
|
|
|
Analogs = this.Analogs,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Comparision Methods
|
|
|
|
|
|
|
|
|
|
|
|
public override bool Equals(DatItem other)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If we don't have a Port, return false
|
|
|
|
|
|
if (ItemType != other.ItemType)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Otherwise, treat it as a Port
|
|
|
|
|
|
Port newOther = other as Port;
|
|
|
|
|
|
|
|
|
|
|
|
// If the Port information matches
|
2020-09-03 15:02:59 -07:00
|
|
|
|
bool match = (Tag == newOther.Tag);
|
|
|
|
|
|
if (!match)
|
|
|
|
|
|
return match;
|
|
|
|
|
|
|
|
|
|
|
|
// If the analogs match
|
|
|
|
|
|
if (Analogs != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (Analog analog in Analogs)
|
|
|
|
|
|
{
|
|
|
|
|
|
match &= newOther.Analogs.Contains(analog);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return match;
|
2020-09-02 17:22:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Filtering
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Check to see if a DatItem passes the filter
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filter">Filter to check against</param>
|
|
|
|
|
|
/// <returns>True if the item passed the filter, false otherwise</returns>
|
|
|
|
|
|
public override bool PassesFilter(Filter filter)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Check common fields first
|
|
|
|
|
|
if (!base.PassesFilter(filter))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Filter on tag
|
2020-09-08 12:54:41 -07:00
|
|
|
|
if (!filter.PassStringFilter(filter.DatItem_Tag, Tag))
|
2020-09-02 17:22:31 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
|
2020-09-03 15:02:59 -07:00
|
|
|
|
// Filter on individual analogs
|
|
|
|
|
|
if (Analogs != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (Analog analog in Analogs)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!analog.PassesFilter(filter))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-09-02 17:22:31 -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_Tag))
|
|
|
|
|
|
Tag = null;
|
|
|
|
|
|
|
2020-09-03 15:02:59 -07:00
|
|
|
|
if (Analogs != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (Analog analog in Analogs)
|
|
|
|
|
|
{
|
|
|
|
|
|
analog.RemoveFields(fields);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-09-02 17:22:31 -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 Port to replace from, ignore specific fields
|
|
|
|
|
|
if (item.ItemType != ItemType.Port)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// Cast for easier access
|
|
|
|
|
|
Port newItem = item as Port;
|
|
|
|
|
|
|
|
|
|
|
|
// Replace the fields
|
|
|
|
|
|
if (fields.Contains(Field.DatItem_Name))
|
|
|
|
|
|
Tag = newItem.Tag;
|
|
|
|
|
|
|
2020-09-03 15:02:59 -07:00
|
|
|
|
// DatItem_Analog_* doesn't make sense here
|
|
|
|
|
|
// since not every analog under the other item
|
|
|
|
|
|
// can replace every analog under this item
|
2020-09-02 17:22:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|