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.
This commit is contained in:
Matt Nadareski
2023-08-14 13:17:51 -04:00
parent 1752b1a0ac
commit b37aed389e
87 changed files with 3266 additions and 2199 deletions

View File

@@ -1,8 +1,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using SabreTools.Core;
using SabreTools.Core.Tools;
namespace SabreTools.DatItems.Formats
{
@@ -19,7 +21,11 @@ namespace SabreTools.DatItems.Formats
/// </summary>
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("type")]
[JsonConverter(typeof(StringEnumConverter))]
public DeviceType DeviceType { get; set; }
public DeviceType DeviceType
{
get => _device.ReadString(Models.Internal.Device.DeviceTypeKey).AsDeviceType();
set => _device[Models.Internal.Device.DeviceTypeKey] = value.FromDeviceType();
}
[JsonIgnore]
public bool DeviceTypeSpecified { get { return DeviceType != DeviceType.NULL; } }
@@ -28,20 +34,32 @@ namespace SabreTools.DatItems.Formats
/// Device tag
/// </summary>
[JsonProperty("tag", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("tag")]
public string Tag { get; set; }
public string? Tag
{
get => _device.ReadString(Models.Internal.Device.TagKey);
set => _device[Models.Internal.Device.TagKey] = value;
}
/// <summary>
/// Fixed image format
/// </summary>
[JsonProperty("fixed_image", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("fixed_image")]
public string FixedImage { get; set; }
public string? FixedImage
{
get => _device.ReadString(Models.Internal.Device.FixedImageKey);
set => _device[Models.Internal.Device.FixedImageKey] = value;
}
/// <summary>
/// Determines if the devices is mandatory
/// </summary>
/// <remarks>Only value used seems to be 1. Used like bool, but actually int</remarks>
[JsonProperty("mandatory", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("mandatory")]
public long? Mandatory { get; set; }
public long? Mandatory
{
get => _device.ReadLong(Models.Internal.Device.MandatoryKey);
set => _device[Models.Internal.Device.MandatoryKey] = value;
}
[JsonIgnore]
public bool MandatorySpecified { get { return Mandatory != null; } }
@@ -50,13 +68,21 @@ namespace SabreTools.DatItems.Formats
/// Device interface
/// </summary>
[JsonProperty("interface", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("interface")]
public string Interface { get; set; }
public string? Interface
{
get => _device.ReadString(Models.Internal.Device.InterfaceKey);
set => _device[Models.Internal.Device.InterfaceKey] = value;
}
/// <summary>
/// Device instances
/// </summary>
[JsonProperty("instances", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("instances")]
public List<Instance> Instances { get; set; }
public List<Instance>? Instances
{
get => _device.Read<Instance[]>(Models.Internal.Device.InstanceKey)?.ToList();
set => _device[Models.Internal.Device.InstanceKey] = value?.ToArray();
}
[JsonIgnore]
public bool InstancesSpecified { get { return Instances != null && Instances.Count > 0; } }
@@ -65,11 +91,21 @@ namespace SabreTools.DatItems.Formats
/// Device extensions
/// </summary>
[JsonProperty("extensions", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("extensions")]
public List<Extension> Extensions { get; set; }
public List<Extension>? Extensions
{
get => _device.Read<Extension[]>(Models.Internal.Device.ExtensionKey)?.ToList();
set => _device[Models.Internal.Device.ExtensionKey] = value?.ToArray();
}
[JsonIgnore]
public bool ExtensionsSpecified { get { return Extensions != null && Extensions.Count > 0; } }
/// <summary>
/// Internal Device model
/// </summary>
[JsonIgnore]
private Models.Internal.Device _device = new();
#endregion
#region Constructors
@@ -94,17 +130,11 @@ namespace SabreTools.DatItems.Formats
ItemType = this.ItemType,
DupeType = this.DupeType,
Machine = this.Machine.Clone() as Machine,
Source = this.Source.Clone() as Source,
Machine = this.Machine?.Clone() as Machine,
Source = this.Source?.Clone() as Source,
Remove = this.Remove,
DeviceType = this.DeviceType,
Tag = this.Tag,
FixedImage = this.FixedImage,
Mandatory = this.Mandatory,
Interface = this.Interface,
Instances = this.Instances,
Extensions = this.Extensions,
_device = this._device?.Clone() as Models.Internal.Device ?? new Models.Internal.Device(),
};
}
@@ -113,43 +143,14 @@ namespace SabreTools.DatItems.Formats
#region Comparision Methods
/// <inheritdoc/>
public override bool Equals(DatItem other)
public override bool Equals(DatItem? other)
{
// If we don't have a Device, return false
if (ItemType != other.ItemType)
// If we don't have a Adjuster, return false
if (ItemType != other?.ItemType || other is not Device otherInternal)
return false;
// Otherwise, treat it as a Device
Device newOther = other as Device;
// If the Device information matches
bool match = (DeviceType == newOther.DeviceType
&& Tag == newOther.Tag
&& FixedImage == newOther.FixedImage
&& Mandatory == newOther.Mandatory
&& Interface == newOther.Interface);
if (!match)
return match;
// If the instances match
if (InstancesSpecified)
{
foreach (Instance instance in Instances)
{
match &= newOther.Instances.Contains(instance);
}
}
// If the extensions match
if (ExtensionsSpecified)
{
foreach (Extension extension in Extensions)
{
match &= newOther.Extensions.Contains(extension);
}
}
return match;
// Compare the internal models
return _device.EqualTo(otherInternal._device);
}
#endregion