Files
SabreTools/SabreTools.DatItems/Formats/Chip.cs

150 lines
4.4 KiB
C#
Raw Normal View History

using System.Xml.Serialization;
2020-08-25 22:48:46 -07:00
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
2022-11-03 12:22:17 -07:00
using SabreTools.Core;
using SabreTools.Core.Tools;
2024-03-05 01:42:42 -05:00
using SabreTools.Filter;
2020-08-25 22:48:46 -07:00
2021-02-02 10:23:43 -08:00
namespace SabreTools.DatItems.Formats
2020-08-25 22:48:46 -07:00
{
/// <summary>
/// Represents which Chip(s) is associated with a set
/// </summary>
2020-09-08 10:12:41 -07:00
[JsonObject("chip"), XmlRoot("chip")]
2020-08-25 22:48:46 -07:00
public class Chip : DatItem
{
#region Fields
2020-09-02 12:19:12 -07:00
/// <summary>
/// Name of the item
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("name"), XmlElement("name")]
public string? Name
{
2023-09-04 23:51:37 -04:00
get => _internal.ReadString(Models.Metadata.Chip.NameKey);
set => _internal[Models.Metadata.Chip.NameKey] = value;
}
2020-09-02 12:19:12 -07:00
2020-08-25 22:48:46 -07:00
/// <summary>
/// Internal tag
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("tag", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("tag")]
public string? Tag
{
2023-09-04 23:51:37 -04:00
get => _internal.ReadString(Models.Metadata.Chip.TagKey);
set => _internal[Models.Metadata.Chip.TagKey] = value;
}
2020-08-25 22:48:46 -07:00
/// <summary>
/// Type of the chip
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("type")]
[JsonConverter(typeof(StringEnumConverter))]
public ChipType ChipType
{
2023-09-04 23:51:37 -04:00
get => _internal.ReadString(Models.Metadata.Chip.ChipTypeKey).AsChipType();
set => _internal[Models.Metadata.Chip.ChipTypeKey] = value.FromChipType();
}
2020-08-25 22:48:46 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool ChipTypeSpecified { get { return ChipType != ChipType.NULL; } }
2020-08-25 22:48:46 -07:00
/// <summary>
/// Clock speed
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("clock", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("clock")]
public long? Clock
{
2023-09-04 23:51:37 -04:00
get => _internal.ReadLong(Models.Metadata.Chip.ClockKey);
set => _internal[Models.Metadata.Chip.ClockKey] = value;
}
2020-08-25 22:48:46 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool ClockSpecified { get { return Clock != null; } }
2020-09-07 22:00:02 -07:00
2020-08-25 22:48:46 -07:00
#endregion
#region Accessors
/// <inheritdoc/>
public override string? GetName() => Name;
2020-09-02 12:19:12 -07:00
/// <inheritdoc/>
public override void SetName(string? name) => Name = name;
2020-08-25 22:48:46 -07:00
#endregion
#region Constructors
/// <summary>
/// Create a default, empty Chip object
/// </summary>
public Chip()
{
2023-09-04 23:51:37 -04:00
_internal = new Models.Metadata.Chip();
2023-08-15 01:38:01 -04:00
Machine = new Machine();
2020-08-25 22:48:46 -07:00
Name = string.Empty;
ItemType = ItemType.Chip;
}
#endregion
#region Cloning Methods
2022-11-03 12:22:17 -07:00
/// <inheritdoc/>
2020-08-25 22:48:46 -07:00
public override object Clone()
{
return new Chip()
{
ItemType = this.ItemType,
DupeType = this.DupeType,
2023-08-15 01:38:01 -04:00
Machine = this.Machine.Clone() as Machine ?? new Machine(),
Source = this.Source?.Clone() as Source,
2020-08-25 22:48:46 -07:00
Remove = this.Remove,
2024-02-28 19:19:50 -05:00
_internal = this._internal?.Clone() as Models.Metadata.Chip ?? [],
2020-08-25 22:48:46 -07:00
};
}
#endregion
2024-03-05 01:42:42 -05:00
#region Manipulation
/// <inheritdoc/>
public override bool RemoveField(DatItemField datItemField)
{
// Get the correct internal field name
string? fieldName = datItemField switch
{
DatItemField.ChipType => Models.Metadata.Chip.ChipTypeKey,
DatItemField.Clock => Models.Metadata.Chip.ClockKey,
DatItemField.Tag => Models.Metadata.Chip.TagKey,
_ => null,
};
// Remove the field and return
return FieldManipulator.RemoveField(_internal, fieldName);
}
2024-03-05 02:20:12 -05:00
/// <inheritdoc/>
public override bool SetField(DatItemField datItemField, string value)
{
// Get the correct internal field name
string? fieldName = datItemField switch
{
DatItemField.ChipType => Models.Metadata.Chip.ChipTypeKey,
DatItemField.Clock => Models.Metadata.Chip.ClockKey,
DatItemField.Tag => Models.Metadata.Chip.TagKey,
_ => null,
};
// Set the field and return
return FieldManipulator.SetField(_internal, fieldName, value);
}
2024-03-05 01:42:42 -05:00
#endregion
2020-08-25 22:48:46 -07:00
}
}