2023-07-30 15:13:16 -04:00
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using SabreTools.Core.Tools;
|
|
|
|
|
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/>
|
|
|
|
|
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Deserialize the input file
|
2023-09-11 01:20:21 -04:00
|
|
|
var metadataFile = new Serialization.Files.DosCenter().Deserialize(filename);
|
2023-07-30 15:13:16 -04:00
|
|
|
|
|
|
|
|
// Convert the header to the internal format
|
|
|
|
|
ConvertHeader(metadataFile?.DosCenter, keep);
|
|
|
|
|
|
|
|
|
|
// Convert the game data to the internal format
|
|
|
|
|
ConvertGames(metadataFile?.Game, filename, indexId, statsOnly);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) when (!throwOnError)
|
|
|
|
|
{
|
|
|
|
|
string message = $"'{filename}' - An error occurred during parsing";
|
|
|
|
|
logger.Error(ex, message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Converters
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert header information
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="doscenter">Deserialized model to convert</param>
|
|
|
|
|
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
|
|
|
|
|
private void ConvertHeader(Models.DosCenter.DosCenter? doscenter, bool keep)
|
|
|
|
|
{
|
|
|
|
|
// If the header is missing, we can't do anything
|
|
|
|
|
if (doscenter == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Header.Name ??= doscenter.Name;
|
|
|
|
|
Header.Description ??= doscenter.Description;
|
|
|
|
|
Header.Version ??= doscenter.Version;
|
|
|
|
|
Header.Date ??= doscenter.Date;
|
|
|
|
|
Header.Author ??= doscenter.Author;
|
|
|
|
|
Header.Homepage ??= doscenter.Homepage;
|
|
|
|
|
Header.Comment ??= doscenter.Comment;
|
|
|
|
|
|
|
|
|
|
// Handle implied SuperDAT
|
2023-07-31 13:44:15 -04:00
|
|
|
if (doscenter.Name?.Contains(" - SuperDAT") == true && keep)
|
2023-07-30 15:13:16 -04:00
|
|
|
Header.Type ??= "SuperDAT";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert games information
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="games">Array of deserialized models to convert</param>
|
|
|
|
|
/// <param name="filename">Name of the file to be parsed</param>
|
|
|
|
|
/// <param name="indexId">Index ID for the DAT</param>
|
|
|
|
|
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
|
|
|
|
private void ConvertGames(Models.DosCenter.Game[]? games, string filename, int indexId, bool statsOnly)
|
|
|
|
|
{
|
|
|
|
|
// If the game array is missing, we can't do anything
|
|
|
|
|
if (games == null || !games.Any())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Loop through the games and add
|
|
|
|
|
foreach (var game in games)
|
|
|
|
|
{
|
|
|
|
|
ConvertGame(game, filename, indexId, statsOnly);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert game information
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="game">Deserialized model to convert</param>
|
|
|
|
|
/// <param name="filename">Name of the file to be parsed</param>
|
|
|
|
|
/// <param name="indexId">Index ID for the DAT</param>
|
|
|
|
|
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
|
|
|
|
private void ConvertGame(Models.DosCenter.Game game, string filename, int indexId, bool statsOnly)
|
|
|
|
|
{
|
|
|
|
|
// If the game is missing, we can't do anything
|
|
|
|
|
if (game == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Create the machine for copying information
|
2023-08-10 23:22:14 -04:00
|
|
|
string? machineName = game.Name?.Trim('"');
|
|
|
|
|
if (machineName?.EndsWith(".zip") == true)
|
2023-07-30 15:13:16 -04:00
|
|
|
machineName = System.IO.Path.GetFileNameWithoutExtension(machineName);
|
|
|
|
|
|
|
|
|
|
var machine = new Machine { Name = machineName };
|
|
|
|
|
|
|
|
|
|
// Check if there are any items
|
|
|
|
|
bool containsItems = false;
|
|
|
|
|
|
|
|
|
|
// Loop through each type of item
|
|
|
|
|
ConvertFiles(game.File, machine, filename, indexId, statsOnly, ref containsItems);
|
|
|
|
|
|
|
|
|
|
// If we had no items, create a Blank placeholder
|
|
|
|
|
if (!containsItems)
|
|
|
|
|
{
|
|
|
|
|
var blank = new Blank
|
|
|
|
|
{
|
2024-03-08 21:12:13 -05:00
|
|
|
Source = new Source { Index = indexId, Name = filename },
|
2023-07-30 15:13:16 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
blank.CopyMachineInformation(machine);
|
|
|
|
|
ParseAddHelper(blank, statsOnly);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert Rom information
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="files">Array of deserialized models to convert</param>
|
|
|
|
|
/// <param name="machine">Prefilled machine to use</param>
|
|
|
|
|
/// <param name="filename">Name of the file to be parsed</param>
|
|
|
|
|
/// <param name="indexId">Index ID for the DAT</param>
|
|
|
|
|
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
|
|
|
|
/// <param name="containsItems">True if there were any items in the array, false otherwise</param>
|
|
|
|
|
private void ConvertFiles(Models.DosCenter.File[]? files, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems)
|
|
|
|
|
{
|
|
|
|
|
// If the files array is missing, we can't do anything
|
|
|
|
|
if (files == null || !files.Any())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
containsItems = true;
|
|
|
|
|
foreach (var rom in files)
|
|
|
|
|
{
|
|
|
|
|
var item = new Rom
|
|
|
|
|
{
|
2023-08-11 14:30:31 -04:00
|
|
|
Size = NumberHelper.ConvertToInt64(rom.Size),
|
2023-07-30 15:13:16 -04:00
|
|
|
CRC = rom.CRC,
|
|
|
|
|
Date = rom.Date,
|
|
|
|
|
|
2024-03-08 21:12:13 -05:00
|
|
|
Source = new Source { Index = indexId, Name = filename },
|
2023-07-30 15:13:16 -04:00
|
|
|
};
|
2024-03-08 20:42:24 -05:00
|
|
|
item.SetName(rom.Name);
|
2023-07-30 15:13:16 -04:00
|
|
|
|
|
|
|
|
item.CopyMachineInformation(machine);
|
|
|
|
|
ParseAddHelper(item, statsOnly);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|