2019-02-08 15:31:44 -08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2020-06-10 22:37:19 -07:00
|
|
|
|
using System.IO;
|
2019-02-08 15:31:44 -08:00
|
|
|
|
using System.Text;
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2019-02-08 15:31:44 -08:00
|
|
|
|
using SabreTools.Library.Data;
|
|
|
|
|
|
using SabreTools.Library.DatItems;
|
2020-08-01 22:46:28 -07:00
|
|
|
|
using SabreTools.Library.IO;
|
2019-02-08 15:31:44 -08:00
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.Library.DatFiles
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents parsing and writing of an Everdrive SMDB file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal class EverdriveSMDB : DatFile
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor designed for casting a base DatFile
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="datFile">Parent DatFile to copy from</param>
|
|
|
|
|
|
public EverdriveSMDB(DatFile datFile)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
: base(datFile)
|
2019-02-08 15:31:44 -08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parse an Everdrive SMDB file and return all found games within
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filename">Name of the file to be parsed</param>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <param name="indexId">Index ID for the DAT</param>
|
2019-02-08 15:31:44 -08:00
|
|
|
|
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
|
2020-09-15 14:23:40 -07:00
|
|
|
|
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
|
|
|
|
|
|
protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false)
|
2019-02-08 15:31:44 -08:00
|
|
|
|
{
|
|
|
|
|
|
// Open a file reader
|
2020-07-15 09:41:59 -07:00
|
|
|
|
Encoding enc = FileExtensions.GetEncoding(filename);
|
2020-09-21 13:20:22 -07:00
|
|
|
|
SeparatedValueReader svr = new SeparatedValueReader(FileExtensions.TryOpenRead(filename), enc)
|
|
|
|
|
|
{
|
|
|
|
|
|
Header = false,
|
|
|
|
|
|
Quotes = false,
|
|
|
|
|
|
Separator = '\t',
|
|
|
|
|
|
VerifyFieldCount = false,
|
|
|
|
|
|
};
|
2019-02-08 15:31:44 -08:00
|
|
|
|
|
2020-09-21 13:20:22 -07:00
|
|
|
|
while (!svr.EndOfStream)
|
2019-02-08 15:31:44 -08:00
|
|
|
|
{
|
2020-09-21 13:04:11 -07:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
/*
|
|
|
|
|
|
The gameinfo order is as follows
|
|
|
|
|
|
0 - SHA-256
|
|
|
|
|
|
1 - Machine Name/Filename
|
|
|
|
|
|
2 - SHA-1
|
|
|
|
|
|
3 - MD5
|
|
|
|
|
|
4 - CRC32
|
|
|
|
|
|
*/
|
2020-09-21 13:20:22 -07:00
|
|
|
|
|
|
|
|
|
|
string[] fullname = svr.Line[1].Split('/');
|
2019-02-08 15:31:44 -08:00
|
|
|
|
|
2020-09-21 13:04:11 -07:00
|
|
|
|
Rom rom = new Rom
|
2020-08-20 13:17:14 -07:00
|
|
|
|
{
|
2020-09-21 13:20:22 -07:00
|
|
|
|
Name = svr.Line[1].Substring(fullname[0].Length + 1),
|
2020-09-21 13:04:11 -07:00
|
|
|
|
Size = null, // No size provided, but we don't want the size being 0
|
2020-09-21 13:20:22 -07:00
|
|
|
|
CRC = svr.Line[4],
|
|
|
|
|
|
MD5 = svr.Line[3],
|
|
|
|
|
|
SHA1 = svr.Line[2],
|
|
|
|
|
|
SHA256 = svr.Line[0],
|
2020-09-21 13:04:11 -07:00
|
|
|
|
ItemStatus = ItemStatus.None,
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
2020-09-21 13:04:11 -07:00
|
|
|
|
Machine = new Machine
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = fullname[0],
|
|
|
|
|
|
Description = fullname[0],
|
|
|
|
|
|
},
|
2019-02-08 15:31:44 -08:00
|
|
|
|
|
2020-09-21 13:04:11 -07:00
|
|
|
|
Source = new Source
|
|
|
|
|
|
{
|
|
|
|
|
|
Index = indexId,
|
|
|
|
|
|
Name = filename,
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Now process and add the rom
|
|
|
|
|
|
ParseAddHelper(rom);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2020-09-21 13:20:22 -07:00
|
|
|
|
string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";
|
2020-09-21 13:04:11 -07:00
|
|
|
|
Globals.Logger.Error(ex, message);
|
|
|
|
|
|
if (throwOnError)
|
|
|
|
|
|
{
|
2020-09-21 13:20:22 -07:00
|
|
|
|
svr.Dispose();
|
2020-09-21 13:04:11 -07:00
|
|
|
|
throw new Exception(message, ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-02-08 15:31:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-21 13:20:22 -07:00
|
|
|
|
svr.Dispose();
|
2019-02-08 15:31:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-18 17:12:31 -07:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
protected override ItemType[] GetSupportedTypes()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new ItemType[] { ItemType.Rom };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-08 15:31:44 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create and open an output file for writing direct from a dictionary
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="outfile">Name of the file to write to</param>
|
|
|
|
|
|
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
|
2020-09-15 14:23:40 -07:00
|
|
|
|
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
|
2019-02-08 15:31:44 -08:00
|
|
|
|
/// <returns>True if the DAT was written correctly, false otherwise</returns>
|
2020-09-15 14:23:40 -07:00
|
|
|
|
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
|
2019-02-08 15:31:44 -08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2020-06-10 22:37:19 -07:00
|
|
|
|
Globals.Logger.User($"Opening file for writing: {outfile}");
|
2020-07-15 09:41:59 -07:00
|
|
|
|
FileStream fs = FileExtensions.TryCreate(outfile);
|
2019-02-08 15:31:44 -08:00
|
|
|
|
|
|
|
|
|
|
// If we get back null for some reason, just log and return
|
|
|
|
|
|
if (fs == null)
|
|
|
|
|
|
{
|
2020-06-10 22:37:19 -07:00
|
|
|
|
Globals.Logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
|
2019-02-08 15:31:44 -08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
SeparatedValueWriter svw = new SeparatedValueWriter(fs, new UTF8Encoding(false))
|
|
|
|
|
|
{
|
|
|
|
|
|
Quotes = false,
|
|
|
|
|
|
Separator = '\t',
|
|
|
|
|
|
VerifyFieldCount = true
|
|
|
|
|
|
};
|
2019-02-08 15:31:44 -08:00
|
|
|
|
|
2020-07-26 21:00:30 -07:00
|
|
|
|
// Use a sorted list of games to output
|
2020-07-26 22:34:45 -07:00
|
|
|
|
foreach (string key in Items.SortedKeys)
|
2019-02-08 15:31:44 -08:00
|
|
|
|
{
|
2020-08-28 15:06:07 -07:00
|
|
|
|
List<DatItem> datItems = Items.FilteredItems(key);
|
2019-02-08 15:31:44 -08:00
|
|
|
|
|
2020-09-25 20:25:29 -07:00
|
|
|
|
// If this machine doesn't contain any writable items, skip
|
|
|
|
|
|
if (!ContainsWritable(datItems))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
2019-02-08 15:31:44 -08:00
|
|
|
|
// Resolve the names in the block
|
2020-08-28 15:06:07 -07:00
|
|
|
|
datItems = DatItem.ResolveNames(datItems);
|
2019-02-08 15:31:44 -08:00
|
|
|
|
|
2020-08-28 15:06:07 -07:00
|
|
|
|
for (int index = 0; index < datItems.Count; index++)
|
2019-02-08 15:31:44 -08:00
|
|
|
|
{
|
2020-08-28 15:06:07 -07:00
|
|
|
|
DatItem datItem = datItems[index];
|
|
|
|
|
|
|
|
|
|
|
|
// Check for a "null" item
|
|
|
|
|
|
datItem = ProcessNullifiedItem(datItem);
|
|
|
|
|
|
|
|
|
|
|
|
// Write out the item if we're not ignoring
|
|
|
|
|
|
if (!ShouldIgnore(datItem, ignoreblanks))
|
|
|
|
|
|
WriteDatItem(svw, datItem);
|
2019-02-08 15:31:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-10 22:37:19 -07:00
|
|
|
|
Globals.Logger.Verbose($"File written!{Environment.NewLine}");
|
2020-06-13 22:15:21 -07:00
|
|
|
|
svw.Dispose();
|
2019-02-08 15:31:44 -08:00
|
|
|
|
fs.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2020-09-15 14:38:37 -07:00
|
|
|
|
Globals.Logger.Error(ex);
|
2020-09-15 14:46:39 -07:00
|
|
|
|
if (throwOnError) throw ex;
|
2019-02-08 15:31:44 -08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write out Game start using the supplied StreamWriter
|
|
|
|
|
|
/// </summary>
|
2020-06-13 22:15:21 -07:00
|
|
|
|
/// <param name="svw">SeparatedValueWriter to output to</param>
|
2020-06-10 22:37:19 -07:00
|
|
|
|
/// <param name="datItem">DatItem object to be output</param>
|
2020-09-15 14:46:39 -07:00
|
|
|
|
private void WriteDatItem(SeparatedValueWriter svw, DatItem datItem)
|
2019-02-08 15:31:44 -08:00
|
|
|
|
{
|
2020-09-15 14:46:39 -07:00
|
|
|
|
// No game should start with a path separator
|
|
|
|
|
|
datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar);
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2020-09-15 14:46:39 -07:00
|
|
|
|
// Pre-process the item name
|
|
|
|
|
|
ProcessItemName(datItem, true);
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2020-09-15 14:46:39 -07:00
|
|
|
|
// Build the state
|
|
|
|
|
|
switch (datItem.ItemType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case ItemType.Rom:
|
|
|
|
|
|
var rom = datItem as Rom;
|
2020-06-13 22:15:21 -07:00
|
|
|
|
|
2020-09-15 14:46:39 -07:00
|
|
|
|
string[] fields = new string[]
|
|
|
|
|
|
{
|
2020-08-24 11:56:49 -07:00
|
|
|
|
rom.SHA256 ?? string.Empty,
|
|
|
|
|
|
$"{rom.Machine.Name ?? string.Empty}/",
|
|
|
|
|
|
rom.Name ?? string.Empty,
|
|
|
|
|
|
rom.SHA1 ?? string.Empty,
|
|
|
|
|
|
rom.MD5 ?? string.Empty,
|
|
|
|
|
|
rom.CRC ?? string.Empty,
|
2020-09-15 14:46:39 -07:00
|
|
|
|
};
|
2020-06-13 22:15:21 -07:00
|
|
|
|
|
2020-09-15 14:46:39 -07:00
|
|
|
|
svw.WriteValues(fields);
|
2019-02-08 15:31:44 -08:00
|
|
|
|
|
2020-09-15 14:46:39 -07:00
|
|
|
|
break;
|
2019-02-08 15:31:44 -08:00
|
|
|
|
}
|
2020-09-15 12:12:13 -07:00
|
|
|
|
|
2020-09-15 14:46:39 -07:00
|
|
|
|
svw.Flush();
|
2019-02-08 15:31:44 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|