2023-07-30 15:13:16 -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 a DosCenter DAT
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal partial class DosCenter : DatFile
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
protected override ItemType[] GetSupportedTypes()
|
|
|
|
|
{
|
2024-02-28 19:19:50 -05:00
|
|
|
return
|
|
|
|
|
[
|
2023-07-30 15:13:16 -04:00
|
|
|
ItemType.Rom
|
2024-02-28 19:19:50 -05:00
|
|
|
];
|
2023-07-30 15:13:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2024-03-05 23:41:00 -05:00
|
|
|
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
|
2023-07-30 15:13:16 -04:00
|
|
|
{
|
2024-03-05 23:41:00 -05:00
|
|
|
var missingFields = new List<string>();
|
2023-07-30 15:13:16 -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-30 15:13:16 -04:00
|
|
|
|
|
|
|
|
switch (datItem)
|
|
|
|
|
{
|
|
|
|
|
case Rom rom:
|
2024-03-09 21:34:26 -05:00
|
|
|
if (rom.GetFieldValue<long?>(Models.Metadata.Rom.SizeKey) == null)
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.Rom.SizeKey);
|
2024-02-28 22:54:56 -05:00
|
|
|
// if (string.IsNullOrEmpty(rom.Date))
|
2024-03-05 23:41:00 -05:00
|
|
|
// missingFields.Add(Models.Metadata.Rom.DateKey);
|
2024-03-09 21:34:26 -05:00
|
|
|
if (string.IsNullOrEmpty(rom.GetFieldValue<string?>(Models.Metadata.Rom.CRCKey)))
|
2024-03-05 23:41:00 -05:00
|
|
|
missingFields.Add(Models.Metadata.Rom.CRCKey);
|
2023-07-30 15:13:16 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.DosCenter().Serialize(metadataFile, outfile)))
|
2023-07-30 15:13:16 -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-30 15:13:16 -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.DosCenter.MetadataFile CreateMetadataFile(bool ignoreblanks)
|
|
|
|
|
{
|
|
|
|
|
var metadataFile = new Models.DosCenter.MetadataFile
|
|
|
|
|
{
|
|
|
|
|
DosCenter = CreateDosCenter(),
|
|
|
|
|
Game = CreateGames(ignoreblanks)
|
|
|
|
|
};
|
|
|
|
|
return metadataFile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a DosCenter from the current internal information
|
|
|
|
|
/// <summary>
|
|
|
|
|
private Models.DosCenter.DosCenter? CreateDosCenter()
|
|
|
|
|
{
|
|
|
|
|
// If we don't have a header, we can't do anything
|
|
|
|
|
if (this.Header == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var clrMamePro = new Models.DosCenter.DosCenter
|
|
|
|
|
{
|
|
|
|
|
Name = Header.Name,
|
|
|
|
|
Description = Header.Description,
|
|
|
|
|
Version = Header.Version,
|
|
|
|
|
Date = Header.Date,
|
|
|
|
|
Author = Header.Author,
|
|
|
|
|
Homepage = Header.Homepage,
|
|
|
|
|
Comment = Header.Comment,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return clrMamePro;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create an array of GameBase from the current internal information
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise</param>
|
|
|
|
|
private Models.DosCenter.Game[]? CreateGames(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 games
|
|
|
|
|
var games = new List<Models.DosCenter.Game>();
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
|
|
// Get the first item for game information
|
|
|
|
|
var machine = items[0].Machine;
|
|
|
|
|
|
|
|
|
|
// We re-add the missing parts of the game name
|
|
|
|
|
var game = new Models.DosCenter.Game
|
|
|
|
|
{
|
2023-08-14 13:17:51 -04:00
|
|
|
Name = $"\"{machine?.Name ?? string.Empty}.zip\""
|
2023-07-30 15:13:16 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Create holders for all item types
|
|
|
|
|
var files = new List<Models.DosCenter.File>();
|
|
|
|
|
|
|
|
|
|
// Loop through and convert the items to respective lists
|
2023-07-30 23:50:55 -04:00
|
|
|
for (int index = 0; index < items.Count; index++)
|
2023-07-30 15:13:16 -04:00
|
|
|
{
|
2023-07-30 23:50:55 -04:00
|
|
|
// Get the item
|
|
|
|
|
var item = items[index];
|
|
|
|
|
|
|
|
|
|
// Check for a "null" item
|
|
|
|
|
item = ProcessNullifiedItem(item);
|
|
|
|
|
|
2023-07-30 15:13:16 -04:00
|
|
|
// Skip if we're ignoring the item
|
|
|
|
|
if (ShouldIgnore(item, ignoreblanks))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
switch (item)
|
|
|
|
|
{
|
|
|
|
|
case Rom rom:
|
|
|
|
|
files.Add(CreateFile(rom));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Assign the values to the game
|
2024-02-28 19:19:50 -05:00
|
|
|
game.File = [.. files];
|
2023-07-30 15:13:16 -04:00
|
|
|
|
|
|
|
|
// Add the game to the list
|
|
|
|
|
games.Add(game);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 19:19:50 -05:00
|
|
|
return [.. games];
|
2023-07-30 15:13:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a File from the current Rom DatItem
|
|
|
|
|
/// <summary>
|
|
|
|
|
private static Models.DosCenter.File CreateFile(Rom item)
|
|
|
|
|
{
|
|
|
|
|
var rom = new Models.DosCenter.File
|
|
|
|
|
{
|
2024-03-08 20:42:24 -05:00
|
|
|
Name = item.GetName(),
|
2024-03-09 21:34:26 -05:00
|
|
|
Size = item.GetFieldValue<long?>(Models.Metadata.Rom.SizeKey)?.ToString(),
|
|
|
|
|
CRC = item.GetFieldValue<string?>(Models.Metadata.Rom.CRCKey),
|
|
|
|
|
Date = item.GetFieldValue<string?>(Models.Metadata.Rom.DateKey),
|
2023-07-30 15:13:16 -04:00
|
|
|
};
|
|
|
|
|
return rom;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|