2023-08-01 00:07:36 -04:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using SabreTools.Core;
|
|
|
|
|
using SabreTools.DatItems;
|
|
|
|
|
using SabreTools.DatItems.Formats;
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.DatFiles.Formats
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents writing a SoftwareList
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal partial class SoftwareList : DatFile
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
protected override ItemType[] GetSupportedTypes()
|
|
|
|
|
{
|
2024-02-28 19:19:50 -05:00
|
|
|
return
|
|
|
|
|
[
|
2023-08-01 00:07:36 -04:00
|
|
|
ItemType.DipSwitch,
|
|
|
|
|
ItemType.Disk,
|
|
|
|
|
ItemType.Info,
|
2024-03-12 16:17:37 -04:00
|
|
|
ItemType.PartFeature,
|
2023-08-01 00:07:36 -04:00
|
|
|
ItemType.Rom,
|
2024-03-10 20:39:54 -04:00
|
|
|
ItemType.SharedFeat,
|
2024-02-28 19:19:50 -05:00
|
|
|
];
|
2023-08-01 00:07:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2024-03-05 23:41:00 -05:00
|
|
|
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
|
2023-08-01 00:07:36 -04:00
|
|
|
{
|
2024-03-05 23:41:00 -05:00
|
|
|
var missingFields = new List<string>();
|
2023-08-01 00:07:36 -04:00
|
|
|
|
2023-08-10 23:22:14 -04:00
|
|
|
switch (datItem)
|
2023-08-01 00:07:36 -04:00
|
|
|
{
|
2023-08-10 23:22:14 -04:00
|
|
|
case DipSwitch dipSwitch:
|
2023-08-01 00:07:36 -04:00
|
|
|
if (!dipSwitch.PartSpecified)
|
|
|
|
|
{
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.Part.NameKey);
|
|
|
|
|
missingFields.Add(Models.Metadata.Part.InterfaceKey);
|
2023-08-01 00:07:36 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-10 16:49:07 -04:00
|
|
|
if (string.IsNullOrEmpty(dipSwitch.GetFieldValue<Part?>(DipSwitch.PartKey)!.GetName()))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.Part.NameKey);
|
2024-03-11 15:46:44 -04:00
|
|
|
if (string.IsNullOrEmpty(dipSwitch.GetFieldValue<Part?>(DipSwitch.PartKey)!.GetStringFieldValue(Models.Metadata.Part.InterfaceKey)))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.Part.InterfaceKey);
|
2023-08-01 00:07:36 -04:00
|
|
|
}
|
2024-03-08 20:42:24 -05:00
|
|
|
if (string.IsNullOrEmpty(dipSwitch.GetName()))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.DipSwitch.NameKey);
|
2024-03-11 15:46:44 -04:00
|
|
|
if (string.IsNullOrEmpty(dipSwitch.GetStringFieldValue(Models.Metadata.DipSwitch.TagKey)))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.DipSwitch.TagKey);
|
2024-03-11 15:46:44 -04:00
|
|
|
if (string.IsNullOrEmpty(dipSwitch.GetStringFieldValue(Models.Metadata.DipSwitch.MaskKey)))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.DipSwitch.MaskKey);
|
2023-08-01 00:07:36 -04:00
|
|
|
if (dipSwitch.ValuesSpecified)
|
|
|
|
|
{
|
2024-03-09 21:34:26 -05:00
|
|
|
if (dipSwitch.GetFieldValue<DipValue[]?>(Models.Metadata.DipSwitch.DipValueKey)!.Any(dv => string.IsNullOrEmpty(dv.GetName())))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.DipValue.NameKey);
|
2024-03-11 15:46:44 -04:00
|
|
|
if (dipSwitch.GetFieldValue<DipValue[]?>(Models.Metadata.DipSwitch.DipValueKey)!.Any(dv => string.IsNullOrEmpty(dv.GetStringFieldValue(Models.Metadata.DipValue.ValueKey))))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.DipValue.ValueKey);
|
2023-08-01 00:07:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
2023-08-01 01:04:21 -04:00
|
|
|
|
2023-08-10 23:22:14 -04:00
|
|
|
case Disk disk:
|
2023-08-01 00:07:36 -04:00
|
|
|
if (!disk.PartSpecified)
|
|
|
|
|
{
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.Part.NameKey);
|
|
|
|
|
missingFields.Add(Models.Metadata.Part.InterfaceKey);
|
2023-08-01 00:07:36 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-10 16:49:07 -04:00
|
|
|
if (string.IsNullOrEmpty(disk.GetFieldValue<Part?>(Disk.PartKey)!.GetName()))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.Part.NameKey);
|
2024-03-11 15:46:44 -04:00
|
|
|
if (string.IsNullOrEmpty(disk.GetFieldValue<Part?>(Disk.PartKey)!.GetStringFieldValue(Models.Metadata.Part.InterfaceKey)))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.Part.InterfaceKey);
|
2023-08-01 00:07:36 -04:00
|
|
|
}
|
|
|
|
|
if (!disk.DiskAreaSpecified)
|
|
|
|
|
{
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.DiskArea.NameKey);
|
2023-08-01 00:07:36 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-10 16:49:07 -04:00
|
|
|
if (string.IsNullOrEmpty(disk.GetFieldValue<DiskArea?>(Disk.DiskAreaKey)!.GetName()))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.DiskArea.NameKey);
|
2023-08-01 00:07:36 -04:00
|
|
|
}
|
2024-03-08 20:42:24 -05:00
|
|
|
if (string.IsNullOrEmpty(disk.GetName()))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.Disk.NameKey);
|
2023-08-01 00:07:36 -04:00
|
|
|
break;
|
2023-08-01 01:04:21 -04:00
|
|
|
|
2023-08-10 23:22:14 -04:00
|
|
|
case Info info:
|
2024-03-08 20:42:24 -05:00
|
|
|
if (string.IsNullOrEmpty(info.GetName()))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.Info.NameKey);
|
2023-08-01 00:07:36 -04:00
|
|
|
break;
|
2023-08-01 01:04:21 -04:00
|
|
|
|
2023-08-10 23:22:14 -04:00
|
|
|
case Rom rom:
|
2023-08-01 00:07:36 -04:00
|
|
|
if (!rom.PartSpecified)
|
|
|
|
|
{
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.Part.NameKey);
|
|
|
|
|
missingFields.Add(Models.Metadata.Part.InterfaceKey);
|
2023-08-01 00:07:36 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-10 16:49:07 -04:00
|
|
|
if (string.IsNullOrEmpty(rom.GetFieldValue<Part?>(Rom.PartKey)!.GetName()))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.Part.NameKey);
|
2024-03-11 15:46:44 -04:00
|
|
|
if (string.IsNullOrEmpty(rom.GetFieldValue<Part?>(Rom.PartKey)!.GetStringFieldValue(Models.Metadata.Part.InterfaceKey)))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.Part.InterfaceKey);
|
2023-08-01 00:07:36 -04:00
|
|
|
}
|
|
|
|
|
if (!rom.DataAreaSpecified)
|
|
|
|
|
{
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.DataArea.NameKey);
|
|
|
|
|
missingFields.Add(Models.Metadata.DataArea.SizeKey);
|
2023-08-01 00:07:36 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-10 16:49:07 -04:00
|
|
|
if (string.IsNullOrEmpty(rom.GetFieldValue<DataArea?>(Rom.DataAreaKey)!.GetName()))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.DataArea.NameKey);
|
2024-03-11 15:46:44 -04:00
|
|
|
if (rom.GetFieldValue<DataArea?>(Rom.DataAreaKey)!.GetInt64FieldValue(Models.Metadata.DataArea.SizeKey) == null)
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.DataArea.SizeKey);
|
2023-08-01 00:07:36 -04:00
|
|
|
}
|
|
|
|
|
break;
|
2023-08-01 01:04:21 -04:00
|
|
|
|
2024-03-10 20:39:54 -04:00
|
|
|
case SharedFeat sharedFeat:
|
2024-03-08 20:42:24 -05:00
|
|
|
if (string.IsNullOrEmpty(sharedFeat.GetName()))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.SharedFeat.NameKey);
|
2023-08-01 00:07:36 -04:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// Unsupported ItemTypes should be caught already
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return missingFields;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logger.User($"Writing to '{outfile}'...");
|
|
|
|
|
|
2024-03-12 16:17:37 -04:00
|
|
|
// Serialize the input file
|
|
|
|
|
var metadata = ConvertMetadata(ignoreblanks);
|
|
|
|
|
var softwarelist = new Serialization.CrossModel.SoftwareList().Deserialize(metadata);
|
|
|
|
|
if (!(new Serialization.Files.SoftwareList().SerializeToFileWithDocType(softwarelist!, outfile)))
|
2023-08-01 00:07:36 -04:00
|
|
|
{
|
2023-08-01 01:49:34 -04:00
|
|
|
logger.Warning($"File '{outfile}' could not be written! See the log for more details.");
|
2023-08-01 00:07:36 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
2023-08-01 01:49:34 -04:00
|
|
|
}
|
|
|
|
|
catch (Exception ex) when (!throwOnError)
|
|
|
|
|
{
|
|
|
|
|
logger.Error(ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-08-01 00:07:36 -04:00
|
|
|
|
2023-08-01 01:49:34 -04:00
|
|
|
logger.User($"'{outfile}' written!{Environment.NewLine}");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-08-01 00:07:36 -04:00
|
|
|
}
|
|
|
|
|
}
|