using System;
using System.Linq;
using SabreTools.Core;
using SabreTools.Core.Tools;
using SabreTools.DatItems;
using SabreTools.DatItems.Formats;
namespace SabreTools.DatFiles.Formats
{
///
/// Represents parsing a RomCenter INI file
///
internal partial class RomCenter : DatFile
{
///
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
{
try
{
// Deserialize the input file
var metadataFile = new Serialization.Files.RomCenter().Deserialize(filename);
// Convert the credits data to the internal format
ConvertCredits(metadataFile?.Credits);
// Convert the dat data to the internal format
ConvertDat(metadataFile?.Dat);
// Convert the emulator data to the internal format
ConvertEmulator(metadataFile?.Emulator);
// Convert the games data to the internal format
ConvertGames(metadataFile?.Games, filename, indexId, statsOnly);
}
catch (Exception ex) when (!throwOnError)
{
string message = $"'{filename}' - An error occurred during parsing";
logger.Error(ex, message);
}
}
#region Converters
///
/// Convert credits information
///
/// Deserialized model to convert
private void ConvertCredits(Models.RomCenter.Credits? credits)
{
// If the credits is missing, we can't do anything
if (credits == null)
return;
Header.Author ??= credits.Author;
Header.Version ??= credits.Version;
Header.Email ??= credits.Email;
Header.Homepage ??= credits.Homepage;
Header.Url ??= credits.Url;
Header.Date ??= credits.Date;
Header.Comment ??= credits.Comment;
}
///
/// Convert dat information
///
/// Deserialized model to convert
private void ConvertDat(Models.RomCenter.Dat? dat)
{
// If the dat is missing, we can't do anything
if (dat == null)
return;
Header.RomCenterVersion ??= dat.Version;
Header.System ??= dat.Plugin;
if (Header.ForceMerging == MergingFlag.None && dat.Split == "1")
Header.ForceMerging = MergingFlag.Split;
if (Header.ForceMerging == MergingFlag.None && dat.Merge == "1")
Header.ForceMerging = MergingFlag.Merged;
}
///
/// Convert emulator information
///
/// Deserialized model to convert
private void ConvertEmulator(Models.RomCenter.Emulator? emulator)
{
// If the emulator is missing, we can't do anything
if (emulator == null)
return;
Header.Name ??= emulator.RefName;
Header.Description ??= emulator.Version;
}
///
/// Convert games information
///
/// Deserialized model to convert
/// Name of the file to be parsed
/// Index ID for the DAT
/// True to only add item statistics while parsing, false otherwise
private void ConvertGames(Models.RomCenter.Games? games, string filename, int indexId, bool statsOnly)
{
// If the games is missing, we can't do anything
if (games?.Rom == null || !games.Rom.Any())
return;
foreach (var rom in games.Rom)
{
var machine = new Machine
{
Name = rom.GameName,
Description = rom.GameDescription,
CloneOf = rom.ParentName,
//CloneOfDescription = rom.ParentDescription, // TODO: Add to internal model or find mapping
RomOf = rom.RomOf,
};
var item = new Rom
{
Source = new Source { Index = indexId, Name = filename },
};
item.SetName(rom.RomName);
item.SetFieldValue(Models.Metadata.Rom.CRCKey, rom.RomCRC);
item.SetFieldValue(Models.Metadata.Rom.MergeKey, rom.MergeName);
item.SetFieldValue(Models.Metadata.Rom.SizeKey, NumberHelper.ConvertToInt64(rom.RomSize));
item.SetFieldValue(Models.Metadata.Rom.StatusKey, ItemStatus.None);
// Now process and add the item
item.CopyMachineInformation(machine);
ParseAddHelper(item, statsOnly);
}
}
#endregion
}
}