mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Convert EverdriveSMDB
This commit is contained in:
@@ -1,10 +1,4 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Core.Tools;
|
||||
using SabreTools.DatItems;
|
||||
using SabreTools.DatItems.Formats;
|
||||
|
||||
namespace SabreTools.DatFiles.Formats
|
||||
{
|
||||
@@ -20,9 +14,10 @@ namespace SabreTools.DatFiles.Formats
|
||||
{
|
||||
// Deserialize the input file
|
||||
var metadataFile = new Serialization.Files.EverdriveSMDB().Deserialize(filename);
|
||||
var metadata = new Serialization.CrossModel.EverdriveSMDB().Serialize(metadataFile);
|
||||
|
||||
// Convert the row data to the internal format
|
||||
ConvertRows(metadataFile?.Row, filename, indexId, statsOnly);
|
||||
// Convert to the internal format
|
||||
ConvertMetadata(metadata, filename, indexId, statsOnly);
|
||||
}
|
||||
catch (Exception ex) when (!throwOnError)
|
||||
{
|
||||
@@ -30,97 +25,5 @@ namespace SabreTools.DatFiles.Formats
|
||||
logger.Error(ex, message);
|
||||
}
|
||||
}
|
||||
|
||||
#region Converters
|
||||
|
||||
/// <summary>
|
||||
/// Create a machine from the filename
|
||||
/// </summary>
|
||||
/// <param name="filename">Filename to derive from</param>
|
||||
/// <returns>Filled machine and new filename on success, null on error</returns>
|
||||
private static (Machine?, string?) DeriveMachine(string? filename)
|
||||
{
|
||||
// If the filename is missing, we can't do anything
|
||||
if (string.IsNullOrEmpty(filename))
|
||||
return (null, null);
|
||||
|
||||
string machineName = Path.GetFileNameWithoutExtension(filename);
|
||||
if (filename.Contains('/'))
|
||||
{
|
||||
string[] split = filename!.Split('/');
|
||||
machineName = split[0];
|
||||
filename = filename.Substring(machineName.Length + 1);
|
||||
}
|
||||
else if (filename.Contains('\\'))
|
||||
{
|
||||
string[] split = filename!.Split('\\');
|
||||
machineName = split[0];
|
||||
filename = filename.Substring(machineName.Length + 1);
|
||||
}
|
||||
|
||||
var machine = new Machine();
|
||||
machine.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, machineName);
|
||||
|
||||
return (machine, filename);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert rows information
|
||||
/// </summary>
|
||||
/// <param name="rows">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 ConvertRows(Models.EverdriveSMDB.Row[]? rows, string filename, int indexId, bool statsOnly)
|
||||
{
|
||||
// If the rows array is missing, we can't do anything
|
||||
if (rows == null || !rows.Any())
|
||||
return;
|
||||
|
||||
// Loop through the rows and add
|
||||
foreach (var row in rows)
|
||||
{
|
||||
ConvertRow(row, filename, indexId, statsOnly);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert row information
|
||||
/// </summary>
|
||||
/// <param name="row">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 ConvertRow(Models.EverdriveSMDB.Row? row, string filename, int indexId, bool statsOnly)
|
||||
{
|
||||
// If the row is missing, we can't do anything
|
||||
if (row == null)
|
||||
return;
|
||||
|
||||
(var machine, string? name) = DeriveMachine(row.Name);
|
||||
if (machine == null)
|
||||
{
|
||||
machine = new Machine();
|
||||
machine.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, Path.GetFileNameWithoutExtension(row.Name));
|
||||
}
|
||||
|
||||
var rom = new Rom()
|
||||
{
|
||||
Source = new Source { Index = indexId, Name = filename },
|
||||
};
|
||||
rom.SetName(name);
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.CRCKey, row.CRC32);
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.MD5Key, row.MD5);
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA1Key, row.SHA1);
|
||||
rom.SetFieldValue<string?>(Models.Metadata.Rom.SHA256Key, row.SHA256);
|
||||
rom.SetFieldValue<long?>(Models.Metadata.Rom.SizeKey, NumberHelper.ConvertToInt64(row.Size));
|
||||
rom.SetFieldValue<ItemStatus>(Models.Metadata.Rom.StatusKey, ItemStatus.None);
|
||||
|
||||
// Now process and add the rom
|
||||
rom.CopyMachineInformation(machine);
|
||||
ParseAddHelper(rom, statsOnly);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user