Files
SabreTools.Serialization/SabreTools.Metadata.DatItems/Formats/Part.cs
2026-04-05 01:08:41 -04:00

154 lines
4.7 KiB
C#

using System;
using System.Xml.Serialization;
using Newtonsoft.Json;
namespace SabreTools.Metadata.DatItems.Formats
{
/// <summary>
/// PartList part information
/// </summary>
/// <remarks>One Part can contain multiple PartFeature, DataArea, DiskArea, and DipSwitch items</remarks>
[JsonObject("part"), XmlRoot("part")]
public sealed class Part : DatItem<Data.Models.Metadata.Part>
{
#region Properties
public DataArea[]? DataArea { get; set; }
[JsonIgnore]
public bool DataAreaSpecified => DataArea is not null && DataArea.Length > 0;
public DiskArea[]? DiskArea { get; set; }
[JsonIgnore]
public bool DiskAreaSpecified => DiskArea is not null && DiskArea.Length > 0;
public DipSwitch[]? DipSwitch { get; set; }
[JsonIgnore]
public bool DipSwitchSpecified => DipSwitch is not null && DipSwitch.Length > 0;
public PartFeature[]? Feature { get; set; }
[JsonIgnore]
public bool FeatureSpecified => Feature is not null && Feature.Length > 0;
public string? Interface
{
get => (_internal as Data.Models.Metadata.Part)?.Interface;
set => (_internal as Data.Models.Metadata.Part)?.Interface = value;
}
/// <inheritdoc>/>
public override Data.Models.Metadata.ItemType ItemType
=> Data.Models.Metadata.ItemType.Part;
public string? Name
{
get => (_internal as Data.Models.Metadata.Part)?.Name;
set => (_internal as Data.Models.Metadata.Part)?.Name = value;
}
#endregion
#region Constructors
public Part() : base() { }
public Part(Data.Models.Metadata.Part item) : base(item)
{
// Handle subitems
if (item.DataArea is not null)
DataArea = Array.ConvertAll(item.DataArea, dataArea => new DataArea(dataArea));
if (item.DiskArea is not null)
DiskArea = Array.ConvertAll(item.DiskArea, diskArea => new DiskArea(diskArea));
if (item.DipSwitch is not null)
DipSwitch = Array.ConvertAll(item.DipSwitch, dipSwitch => new DipSwitch(dipSwitch));
if (item.Feature is not null)
Feature = Array.ConvertAll(item.Feature, feature => new PartFeature(feature));
}
public Part(Data.Models.Metadata.Part item, Machine machine, Source source) : this(item)
{
Source = source;
CopyMachineInformation(machine);
}
#endregion
#region Accessors
/// <inheritdoc/>
public override string? GetName() => Name;
/// <inheritdoc/>
public override void SetName(string? name) => Name = name;
#endregion
#region Cloning Methods
/// <inheritdoc/>
public override object Clone() => new Part(GetInternalClone());
/// <inheritdoc/>
public override Data.Models.Metadata.Part GetInternalClone()
{
var partItem = (_internal as Data.Models.Metadata.Part)?.Clone() as Data.Models.Metadata.Part ?? new();
if (DataArea is not null)
partItem.DataArea = Array.ConvertAll(DataArea, dataArea => dataArea.GetInternalClone());
if (DiskArea is not null)
partItem.DiskArea = Array.ConvertAll(DiskArea, diskArea => diskArea.GetInternalClone());
if (DipSwitch is not null)
partItem.DipSwitch = Array.ConvertAll(DipSwitch, dipSwitch => dipSwitch.GetInternalClone());
if (Feature is not null)
partItem.Feature = Array.ConvertAll(Feature, feature => feature.GetInternalClone());
return partItem;
}
#endregion
#region Comparision Methods
/// <inheritdoc/>
public override bool Equals(DatItem? other)
{
// If the other item is null
if (other is null)
return false;
// If the type matches
if (other is Part otherPart)
return ((Data.Models.Metadata.Part)_internal).Equals((Data.Models.Metadata.Part)otherPart._internal);
// Everything else fails
return false;
}
/// <inheritdoc/>
public override bool Equals(DatItem<Data.Models.Metadata.Part>? other)
{
// If the other value is invalid
if (other is null)
return false;
// If the type matches
if (other is Part otherPart)
return ((Data.Models.Metadata.Part)_internal).Equals((Data.Models.Metadata.Part)otherPart._internal);
// Everything else fails
return false;
}
#endregion
}
}