Files
SabreTools/SabreTools.DatItems/Port.cs

179 lines
4.7 KiB
C#
Raw Normal View History

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
2020-12-08 13:23:59 -08:00
using SabreTools.Core;
2020-09-02 17:22:31 -07:00
using Newtonsoft.Json;
2020-12-08 15:15:41 -08:00
namespace SabreTools.DatItems
2020-09-02 17:22:31 -07:00
{
/// <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
2020-12-13 13:22:06 -08:00
/// <inheritdoc/>
public override void SetFields(
Dictionary<DatItemField, string> datItemMappings,
Dictionary<MachineField, string> machineMappings)
2020-09-02 17:22:31 -07:00
{
// Set base fields
2020-12-13 13:22:06 -08:00
base.SetFields(datItemMappings, machineMappings);
2020-09-02 17:22:31 -07:00
// Handle Port-specific fields
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Tag))
Tag = datItemMappings[DatItemField.Tag];
2020-09-02 17:22:31 -07:00
2020-09-30 13:25:40 -07:00
if (AnalogsSpecified)
2020-09-03 15:02:59 -07:00
{
foreach (Analog analog in Analogs)
{
2020-12-13 13:22:06 -08:00
analog.SetFields(datItemMappings, machineMappings);
2020-09-03 15:02:59 -07:00
}
}
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
2020-09-30 13:25:40 -07:00
if (AnalogsSpecified)
2020-09-03 15:02:59 -07:00
{
foreach (Analog analog in Analogs)
{
match &= newOther.Analogs.Contains(analog);
}
}
return match;
2020-09-02 17:22:31 -07:00
}
#endregion
#region Filtering
2020-12-13 13:22:06 -08:00
/// <inheritdoc/>
public override void RemoveFields(
List<DatItemField> datItemFields,
List<MachineField> machineFields)
2020-09-02 17:22:31 -07:00
{
// Remove common fields first
2020-12-13 13:22:06 -08:00
base.RemoveFields(datItemFields, machineFields);
2020-09-02 17:22:31 -07:00
// Remove the fields
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Tag))
2020-09-02 17:22:31 -07:00
Tag = null;
2020-09-30 13:25:40 -07:00
if (AnalogsSpecified)
2020-09-03 15:02:59 -07:00
{
foreach (Analog analog in Analogs)
{
2020-12-13 13:22:06 -08:00
analog.RemoveFields(datItemFields, machineFields);
2020-09-03 15:02:59 -07:00
}
}
2020-09-02 17:22:31 -07:00
}
#endregion
#region Sorting and Merging
2020-12-13 13:22:06 -08:00
/// <inheritdoc/>
public override void ReplaceFields(
DatItem item,
List<DatItemField> datItemFields,
List<MachineField> machineFields)
2020-09-02 17:22:31 -07:00
{
// Replace common fields first
2020-12-13 13:22:06 -08:00
base.ReplaceFields(item, datItemFields, machineFields);
2020-09-02 17:22:31 -07:00
// 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
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Name))
2020-09-02 17:22:31 -07:00
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
}
}