mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-07-08 18:06:41 +00:00
Convert Device fully over to properties
This commit is contained in:
@@ -163,6 +163,8 @@ namespace SabreTools.Data.Extensions
|
||||
return confSetting.Clone() as ConfSetting;
|
||||
else if (self is Control control)
|
||||
return control.Clone() as Control;
|
||||
else if (self is Device device)
|
||||
return device.Clone() as Device;
|
||||
else if (self is DeviceRef deviceRef)
|
||||
return deviceRef.Clone() as DeviceRef;
|
||||
else if (self is DipLocation dipLocation)
|
||||
@@ -222,14 +224,6 @@ namespace SabreTools.Data.Extensions
|
||||
cloneDataArea.Size = selfDataArea.Size;
|
||||
cloneDataArea.Width = selfDataArea.Width;
|
||||
}
|
||||
else if (self is Device selfDevice && clone is Device cloneDevice)
|
||||
{
|
||||
cloneDevice.DeviceType = selfDevice.DeviceType;
|
||||
cloneDevice.FixedImage = selfDevice.FixedImage;
|
||||
cloneDevice.Interface = selfDevice.Interface;
|
||||
cloneDevice.Mandatory = selfDevice.Mandatory;
|
||||
cloneDevice.Tag = selfDevice.Tag;
|
||||
}
|
||||
else if (self is Disk selfDisk && clone is Disk cloneDisk)
|
||||
{
|
||||
cloneDisk.Optional = selfDisk.Optional;
|
||||
|
||||
@@ -1,18 +1,23 @@
|
||||
using System;
|
||||
using System.Xml.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SabreTools.Data.Models.Metadata
|
||||
{
|
||||
[JsonObject("device"), XmlRoot("device")]
|
||||
public class Device : DatItem
|
||||
public class Device : DatItem, ICloneable, IEquatable<Device>
|
||||
{
|
||||
#region Properties
|
||||
|
||||
/// <remarks>(unknown|cartridge|floppydisk|harddisk|cylinder|cassette|punchcard|punchtape|printout|serial|parallel|snapshot|quickload|memcard|cdrom|magtape|romimage|midiin|midiout|picture|vidfile)</remarks>
|
||||
public DeviceType? DeviceType { get; set; }
|
||||
|
||||
public Extension[]? Extension { get; set; }
|
||||
|
||||
public string? FixedImage { get; set; }
|
||||
|
||||
public Instance? Instance { get; set; }
|
||||
|
||||
public string? Interface { get; set; }
|
||||
|
||||
/// <remarks>(0|1) "0"</remarks>
|
||||
@@ -22,18 +27,63 @@ namespace SabreTools.Data.Models.Metadata
|
||||
|
||||
#endregion
|
||||
|
||||
#region Keys
|
||||
|
||||
/// <remarks>Extension[]</remarks>
|
||||
[NoFilter]
|
||||
public const string ExtensionKey = "extension";
|
||||
|
||||
/// <remarks>Instance</remarks>
|
||||
[NoFilter]
|
||||
public const string InstanceKey = "instance";
|
||||
|
||||
#endregion
|
||||
|
||||
public Device() => ItemType = ItemType.Device;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public object Clone()
|
||||
{
|
||||
var obj = new Device();
|
||||
|
||||
obj.DeviceType = DeviceType;
|
||||
if (Extension is not null)
|
||||
obj.Extension = Array.ConvertAll(Extension, i => (Extension)i.Clone());
|
||||
obj.FixedImage = FixedImage;
|
||||
obj.Instance = Instance?.Clone() as Instance;
|
||||
obj.Interface = Interface;
|
||||
obj.Mandatory = Mandatory;
|
||||
obj.Tag = Tag;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Equals(Device? other)
|
||||
{
|
||||
// Null never matches
|
||||
if (other is null)
|
||||
return false;
|
||||
|
||||
// Properties
|
||||
if (DeviceType != other.DeviceType)
|
||||
return false;
|
||||
|
||||
if ((FixedImage is null) ^ (other.FixedImage is null))
|
||||
return false;
|
||||
else if (FixedImage is not null && !FixedImage.Equals(other.FixedImage, StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
|
||||
if ((Interface is null) ^ (other.Interface is null))
|
||||
return false;
|
||||
else if (Interface is not null && !Interface.Equals(other.Interface, StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
|
||||
if (Mandatory != other.Mandatory)
|
||||
return false;
|
||||
|
||||
if ((Tag is null) ^ (other.Tag is null))
|
||||
return false;
|
||||
else if (Tag is not null && !Tag.Equals(other.Tag, StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
|
||||
// Sub-items
|
||||
if ((Instance is null) ^ (other.Instance is null))
|
||||
return false;
|
||||
else if (Instance is not null && other.Instance is not null && Instance.Equals(other.Instance))
|
||||
return false;
|
||||
|
||||
// TODO: Figure out how to properly check arrays
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -481,9 +481,9 @@ namespace SabreTools.Metadata.DatFiles.Test
|
||||
{
|
||||
return new Data.Models.Metadata.Device
|
||||
{
|
||||
[Data.Models.Metadata.Device.ExtensionKey] = new Data.Models.Metadata.Extension[] { CreateMetadataExtension() },
|
||||
Extension = [CreateMetadataExtension()],
|
||||
FixedImage = "fixedimage",
|
||||
[Data.Models.Metadata.Device.InstanceKey] = CreateMetadataInstance(),
|
||||
Instance = CreateMetadataInstance(),
|
||||
Interface = "interface",
|
||||
Mandatory = true,
|
||||
Tag = "tag",
|
||||
@@ -1246,13 +1246,12 @@ namespace SabreTools.Metadata.DatFiles.Test
|
||||
Assert.Equal("tag", device.Tag);
|
||||
Assert.Equal(Data.Models.Metadata.DeviceType.PunchTape, device.DeviceType);
|
||||
|
||||
Extension[]? extensions = device.Read<Extension[]>(Data.Models.Metadata.Device.ExtensionKey);
|
||||
Extension[]? extensions = device.Extension;
|
||||
Assert.NotNull(extensions);
|
||||
Extension? extension = Assert.Single(extensions);
|
||||
ValidateExtension(extension);
|
||||
|
||||
Instance? instance = device.Read<Instance>(Data.Models.Metadata.Device.InstanceKey);
|
||||
ValidateInstance(instance);
|
||||
ValidateInstance(device.Instance);
|
||||
}
|
||||
|
||||
private static void ValidateDeviceRef(DeviceRef? deviceRef)
|
||||
|
||||
@@ -776,13 +776,12 @@ namespace SabreTools.Metadata.DatFiles.Test
|
||||
Assert.Equal("tag", device.Tag);
|
||||
Assert.Equal(Data.Models.Metadata.DeviceType.PunchTape, device.DeviceType);
|
||||
|
||||
Data.Models.Metadata.Extension[]? extensions = device.ReadArray<Data.Models.Metadata.Extension>(Data.Models.Metadata.Device.ExtensionKey);
|
||||
Data.Models.Metadata.Extension[]? extensions = device.Extension;
|
||||
Assert.NotNull(extensions);
|
||||
Data.Models.Metadata.Extension? extension = Assert.Single(extensions);
|
||||
ValidateMetadataExtension(extension);
|
||||
|
||||
Data.Models.Metadata.Instance? instance = device.Read<Data.Models.Metadata.Instance>(Data.Models.Metadata.Device.InstanceKey);
|
||||
ValidateMetadataInstance(instance);
|
||||
ValidateMetadataInstance(device.Instance);
|
||||
}
|
||||
|
||||
private static void ValidateMetadataDeviceRef(Data.Models.Metadata.DeviceRef? deviceRef)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Xml.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using SabreTools.Data.Extensions;
|
||||
@@ -19,15 +19,10 @@ namespace SabreTools.Metadata.DatItems.Formats
|
||||
set => (_internal as Data.Models.Metadata.Device)?.DeviceType = value;
|
||||
}
|
||||
|
||||
public Extension[]? Extension { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ExtensionsSpecified
|
||||
{
|
||||
get
|
||||
{
|
||||
var extensions = Read<Extension[]?>(Data.Models.Metadata.Device.ExtensionKey);
|
||||
return extensions is not null && extensions.Length > 0;
|
||||
}
|
||||
}
|
||||
public bool ExtensionSpecified => Extension is not null && Extension.Length > 0;
|
||||
|
||||
public string? FixedImage
|
||||
{
|
||||
@@ -35,15 +30,10 @@ namespace SabreTools.Metadata.DatItems.Formats
|
||||
set => (_internal as Data.Models.Metadata.Device)?.FixedImage = value;
|
||||
}
|
||||
|
||||
public Instance? Instance { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool InstancesSpecified
|
||||
{
|
||||
get
|
||||
{
|
||||
var instances = Read<Instance[]?>(Data.Models.Metadata.Device.InstanceKey);
|
||||
return instances is not null && instances.Length > 0;
|
||||
}
|
||||
}
|
||||
public bool InstanceSpecified => Instance is not null;
|
||||
|
||||
public string? Interface
|
||||
{
|
||||
@@ -76,16 +66,11 @@ namespace SabreTools.Metadata.DatItems.Formats
|
||||
public Device(Data.Models.Metadata.Device item) : base(item)
|
||||
{
|
||||
// Handle subitems
|
||||
var instance = item.Read<Data.Models.Metadata.Instance>(Data.Models.Metadata.Device.InstanceKey);
|
||||
if (instance is not null)
|
||||
Write<Instance?>(Data.Models.Metadata.Device.InstanceKey, new Instance(instance));
|
||||
if (item.Extension is not null)
|
||||
Extension = Array.ConvertAll(item.Extension, extension => new Extension(extension)); ;
|
||||
|
||||
var extensions = item.ReadArray<Data.Models.Metadata.Extension>(Data.Models.Metadata.Device.ExtensionKey);
|
||||
if (extensions is not null)
|
||||
{
|
||||
Extension[] extensionItems = Array.ConvertAll(extensions, extension => new Extension(extension));
|
||||
Write<Extension[]?>(Data.Models.Metadata.Device.ExtensionKey, extensionItems);
|
||||
}
|
||||
if (item.Instance is not null)
|
||||
Instance = new Instance(item.Instance);
|
||||
}
|
||||
|
||||
public Device(Data.Models.Metadata.Device item, Machine machine, Source source) : this(item)
|
||||
@@ -112,16 +97,11 @@ namespace SabreTools.Metadata.DatItems.Formats
|
||||
deviceItem.Mandatory = Mandatory;
|
||||
deviceItem.Tag = Tag;
|
||||
|
||||
var instance = Read<Instance?>(Data.Models.Metadata.Device.InstanceKey);
|
||||
if (instance is not null)
|
||||
deviceItem[Data.Models.Metadata.Device.InstanceKey] = instance.GetInternalClone();
|
||||
if (Instance is not null)
|
||||
deviceItem.Instance = Instance.GetInternalClone();
|
||||
|
||||
var extensions = Read<Extension[]?>(Data.Models.Metadata.Device.ExtensionKey);
|
||||
if (extensions is not null)
|
||||
{
|
||||
Data.Models.Metadata.Extension[] extensionItems = Array.ConvertAll(extensions, extension => extension.GetInternalClone());
|
||||
deviceItem[Data.Models.Metadata.Device.ExtensionKey] = extensionItems;
|
||||
}
|
||||
if (Extension is not null)
|
||||
deviceItem.Extension = Array.ConvertAll(Extension, extension => extension.GetInternalClone()); ;
|
||||
|
||||
return deviceItem;
|
||||
}
|
||||
|
||||
@@ -123,15 +123,7 @@ namespace SabreTools.Metadata
|
||||
}
|
||||
else if (self is Device selfDevice && other is Device otherDevice)
|
||||
{
|
||||
if (selfDevice.DeviceType != otherDevice.DeviceType)
|
||||
return false;
|
||||
if (selfDevice.FixedImage != otherDevice.FixedImage)
|
||||
return false;
|
||||
if (selfDevice.Interface != otherDevice.Interface)
|
||||
return false;
|
||||
if (selfDevice.Mandatory != otherDevice.Mandatory)
|
||||
return false;
|
||||
if (selfDevice.Tag != otherDevice.Tag)
|
||||
if (!selfDevice.Equals(otherDevice))
|
||||
return false;
|
||||
}
|
||||
else if (self is DipLocation selfDipLocation && other is DipLocation otherDipLocation)
|
||||
@@ -141,23 +133,8 @@ namespace SabreTools.Metadata
|
||||
}
|
||||
else if (self is DipSwitch selfDipSwitch && other is DipSwitch otherDipSwitch)
|
||||
{
|
||||
if (selfDipSwitch.Default != otherDipSwitch.Default)
|
||||
if (!selfDipSwitch.Equals(otherDipSwitch))
|
||||
return false;
|
||||
if (selfDipSwitch.Mask != otherDipSwitch.Mask)
|
||||
return false;
|
||||
if (selfDipSwitch.Tag != otherDipSwitch.Tag)
|
||||
return false;
|
||||
|
||||
if ((selfDipSwitch.Condition is null) ^ (otherDipSwitch.Condition is null))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (selfDipSwitch.Condition is not null
|
||||
&& otherDipSwitch.Condition is not null
|
||||
&& selfDipSwitch.Condition.EqualTo(otherDipSwitch.Condition))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (self is DipValue selfDipValue && other is DipValue otherDipValue)
|
||||
{
|
||||
|
||||
@@ -313,11 +313,11 @@ namespace SabreTools.Serialization.CrossModel
|
||||
Interface = item.Interface,
|
||||
};
|
||||
|
||||
var instance = item.Read<Data.Models.Metadata.Instance>(Data.Models.Metadata.Device.InstanceKey);
|
||||
var instance = item.Instance;
|
||||
if (instance is not null)
|
||||
device.Instance = ConvertFromInternalModel(instance);
|
||||
|
||||
var extensions = item.Read<Data.Models.Metadata.Extension[]>(Data.Models.Metadata.Device.ExtensionKey);
|
||||
var extensions = item.Extension;
|
||||
if (extensions is not null && extensions.Length > 0)
|
||||
device.Extension = Array.ConvertAll(extensions, ConvertFromInternalModel);
|
||||
|
||||
|
||||
@@ -343,13 +343,10 @@ namespace SabreTools.Serialization.CrossModel
|
||||
};
|
||||
|
||||
if (item.Instance is not null)
|
||||
device[Data.Models.Metadata.Device.InstanceKey] = ConvertToInternalModel(item.Instance);
|
||||
device.Instance = ConvertToInternalModel(item.Instance);
|
||||
|
||||
if (item.Extension is not null && item.Extension.Length > 0)
|
||||
{
|
||||
device[Data.Models.Metadata.Device.ExtensionKey]
|
||||
= Array.ConvertAll(item.Extension, ConvertToInternalModel);
|
||||
}
|
||||
device.Extension = Array.ConvertAll(item.Extension, ConvertToInternalModel);
|
||||
|
||||
return device;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user