Files
SabreTools/SabreTools.DatFiles/Formats/AttractMode.Writer.cs

152 lines
5.7 KiB
C#
Raw Normal View History

2023-07-29 00:06:31 -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 parsing and writing of an AttractMode DAT
/// </summary>
internal partial class AttractMode : DatFile
{
/// <inheritdoc/>
protected override ItemType[] GetSupportedTypes()
{
2024-02-28 19:19:50 -05:00
return
[
ItemType.Rom
];
2023-07-29 00:06:31 -04:00
}
/// <inheritdoc/>
2024-03-05 23:41:00 -05:00
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
2023-07-29 00:06:31 -04:00
{
2024-03-05 23:41:00 -05:00
var missingFields = new List<string>();
2023-07-29 00:06:31 -04:00
// Check item name
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(datItem.GetName()))
2024-03-05 23:41:00 -05:00
missingFields.Add(Models.Metadata.Rom.NameKey);
2023-07-29 00:06:31 -04:00
return missingFields;
}
/// <inheritdoc/>
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
{
try
{
logger.User($"Writing to '{outfile}'...");
var metadataFile = CreateMetadataFile(ignoreblanks);
2023-09-11 01:20:21 -04:00
if (!(new Serialization.Files.AttractMode().Serialize(metadataFile, outfile)))
2023-07-29 00:06:31 -04:00
{
logger.Warning($"File '{outfile}' could not be written! See the log for more details.");
return false;
}
}
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
return false;
}
2023-07-31 11:33:03 -04:00
logger.User($"'{outfile}' written!{Environment.NewLine}");
2023-07-29 00:06:31 -04:00
return true;
}
#region Converters
/// <summary>
/// Create a MetadataFile from the current internal information
/// <summary>
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise</param>
private Models.AttractMode.MetadataFile CreateMetadataFile(bool ignoreblanks)
{
var metadataFile = new Models.AttractMode.MetadataFile
{
Row = CreateRows(ignoreblanks)
};
return metadataFile;
}
/// <summary>
/// Create an array of Row from the current internal information
/// <summary>
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise</param>
private Models.AttractMode.Row[]? CreateRows(bool ignoreblanks)
{
// If we don't have items, we can't do anything
if (this.Items == null || !this.Items.Any())
return null;
// Create a list of hold the rows
var rows = new List<Models.AttractMode.Row>();
// Loop through the sorted items and create games for them
foreach (string key in Items.SortedKeys)
{
var items = Items.FilteredItems(key);
if (items == null || !items.Any())
continue;
// Loop through and convert the items to respective lists
for (int index = 0; index < items.Count; index++)
2023-07-29 00:06:31 -04:00
{
// Get the item
var item = items[index];
// Check for a "null" item
item = ProcessNullifiedItem(item);
2023-07-29 00:06:31 -04:00
// Skip if we're ignoring the item
if (ShouldIgnore(item, ignoreblanks))
continue;
switch (item)
{
case Rom rom:
rows.Add(CreateRow(rom));
break;
}
}
}
2024-02-28 19:19:50 -05:00
return [.. rows];
2023-07-29 00:06:31 -04:00
}
/// <summary>
/// Create a Row from the current Rom DatItem
/// <summary>
private Models.AttractMode.Row CreateRow(Rom rom)
{
var row = new Models.AttractMode.Row
{
2024-03-09 23:43:43 -05:00
Name = rom.Machine.GetFieldValue<string?>(Models.Metadata.Machine.NameKey),
Title = rom.Machine.GetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey),
2023-07-29 00:06:31 -04:00
Emulator = Header.FileName,
2024-03-09 23:43:43 -05:00
CloneOf = rom.Machine.GetFieldValue<string?>(Models.Metadata.Machine.CloneOfKey),
Year = rom.Machine.GetFieldValue<string?>(Models.Metadata.Machine.YearKey),
Manufacturer = rom.Machine.GetFieldValue<string?>(Models.Metadata.Machine.ManufacturerKey),
Category = rom.Machine.GetFieldValue<string?>(Models.Metadata.Machine.CategoryKey),
Players = rom.Machine.GetFieldValue<string?>(Models.Metadata.Machine.PlayersKey),
Rotation = rom.Machine.GetFieldValue<string?>(Models.Metadata.Machine.RotationKey),
Control = rom.Machine.GetFieldValue<string?>(Models.Metadata.Machine.ControlKey),
Status = rom.Machine.GetFieldValue<string?>(Models.Metadata.Machine.StatusKey),
DisplayCount = rom.Machine.GetFieldValue<string?>(Models.Metadata.Machine.DisplayCountKey),
DisplayType = rom.Machine.GetFieldValue<string?>(Models.Metadata.Machine.DisplayTypeKey),
2024-03-09 21:34:26 -05:00
AltRomname = rom.GetFieldValue<string?>(Models.Metadata.Rom.AltRomnameKey),
AltTitle = rom.GetFieldValue<string?>(Models.Metadata.Rom.AltTitleKey),
2024-03-09 23:43:43 -05:00
Extra = rom.Machine.GetFieldValue<string?>(Models.Metadata.Machine.CommentKey),
Buttons = rom.Machine.GetFieldValue<string?>(Models.Metadata.Machine.ButtonsKey),
2023-07-29 00:06:31 -04:00
// TODO: Add extended fields
};
return row;
}
#endregion
}
}