Files
SabreTools/SabreTools.DatItems/Formats/ConfSetting.cs
Matt Nadareski b37aed389e Add nullable context to SabreTools.DatItems
This change also starts migrating the internals of the DatItem formats to the new internal models. Right now, it's basically just acting like a wrapper around those models.
2023-08-14 13:17:51 -04:00

130 lines
3.8 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using Newtonsoft.Json;
using SabreTools.Core;
namespace SabreTools.DatItems.Formats
{
/// <summary>
/// Represents one ListXML confsetting
/// </summary>
[JsonObject("confsetting"), XmlRoot("confsetting")]
public class ConfSetting : DatItem
{
#region Fields
/// <summary>
/// Setting name
/// </summary>
[JsonProperty("name"), XmlElement("name")]
public string? Name
{
get => _confSetting.ReadString(Models.Internal.ConfSetting.NameKey);
set => _confSetting[Models.Internal.ConfSetting.NameKey] = value;
}
/// <summary>
/// Setting value
/// </summary>
[JsonProperty("value", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("value")]
public string? Value
{
get => _confSetting.ReadString(Models.Internal.ConfSetting.ValueKey);
set => _confSetting[Models.Internal.ConfSetting.ValueKey] = value;
}
/// <summary>
/// Determines if the setting is default or not
/// </summary>
[JsonProperty("default", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("default")]
public bool? Default
{
get => _confSetting.ReadBool(Models.Internal.ConfSetting.DefaultKey);
set => _confSetting[Models.Internal.ConfSetting.DefaultKey] = value;
}
[JsonIgnore]
public bool DefaultSpecified { get { return Default != null; } }
/// <summary>
/// List of conditions on the setting
/// </summary>
[JsonProperty("conditions", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("conditions")]
public List<Condition>? Conditions
{
get => _confSetting.Read<Condition[]>(Models.Internal.ConfSetting.ConditionKey)?.ToList();
set => _confSetting[Models.Internal.ConfSetting.ConditionKey] = value?.ToArray();
}
[JsonIgnore]
public bool ConditionsSpecified { get { return Conditions != null && Conditions.Count > 0; } }
/// <summary>
/// Internal ConfSetting model
/// </summary>
[JsonIgnore]
private Models.Internal.ConfSetting _confSetting = new();
#endregion
#region Accessors
/// <inheritdoc/>
public override string? GetName() => Name;
/// <inheritdoc/>
public override void SetName(string? name) => Name = name;
#endregion
#region Constructors
/// <summary>
/// Create a default, empty ConfSetting object
/// </summary>
public ConfSetting()
{
Name = string.Empty;
ItemType = ItemType.ConfSetting;
}
#endregion
#region Cloning Methods
/// <inheritdoc/>
public override object Clone()
{
return new ConfSetting()
{
ItemType = this.ItemType,
DupeType = this.DupeType,
Machine = this.Machine?.Clone() as Machine,
Source = this.Source?.Clone() as Source,
Remove = this.Remove,
_confSetting = this._confSetting?.Clone() as Models.Internal.ConfSetting ?? new Models.Internal.ConfSetting(),
};
}
#endregion
#region Comparision Methods
/// <inheritdoc/>
public override bool Equals(DatItem? other)
{
// If we don't have a ConfSetting, return false
if (ItemType != other?.ItemType || other is not ConfSetting otherInternal)
return false;
// Compare the internal models
return _confSetting.EqualTo(otherInternal._confSetting);
}
#endregion
}
}