2017-10-09 18:04:49 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2020-06-11 11:44:46 -07:00
|
|
|
|
using System.IO;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
using System.Text;
|
2020-06-11 11:44:46 -07:00
|
|
|
|
|
2020-12-08 13:23:59 -08:00
|
|
|
|
using SabreTools.Core;
|
2020-12-08 16:37:08 -08:00
|
|
|
|
using SabreTools.Core.Tools;
|
2020-12-08 15:15:41 -08:00
|
|
|
|
using SabreTools.DatItems;
|
2020-12-07 15:08:57 -08:00
|
|
|
|
using SabreTools.IO;
|
2020-12-09 23:11:10 -08:00
|
|
|
|
using SabreTools.IO.Readers;
|
|
|
|
|
|
using SabreTools.IO.Writers;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
|
2020-12-09 22:11:35 -08:00
|
|
|
|
namespace SabreTools.DatFiles.Formats
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
2019-01-11 13:43:15 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents parsing and writing of a value-separated DAT
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal class SeparatedValue : DatFile
|
|
|
|
|
|
{
|
|
|
|
|
|
// Private instance variables specific to Separated Value DATs
|
2020-06-10 22:37:19 -07:00
|
|
|
|
private readonly char _delim;
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor designed for casting a base DatFile
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="datFile">Parent DatFile to copy from</param>
|
|
|
|
|
|
/// <param name="delim">Delimiter for parsing individual lines</param>
|
|
|
|
|
|
public SeparatedValue(DatFile datFile, char delim)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
: base(datFile)
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
|
|
|
|
|
_delim = delim;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parse a character-separated value DAT and return all found games and roms 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-01-11 13:43:15 -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>
|
2020-12-10 13:30:08 -08:00
|
|
|
|
public override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false)
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
|
|
|
|
|
// Open a file reader
|
2020-12-10 22:16:53 -08:00
|
|
|
|
Encoding enc = filename.GetEncoding();
|
2020-12-08 00:13:22 -08:00
|
|
|
|
SeparatedValueReader svr = new SeparatedValueReader(File.OpenRead(filename), enc)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
|
|
|
|
|
Header = true,
|
|
|
|
|
|
Quotes = true,
|
|
|
|
|
|
Separator = _delim,
|
2020-09-21 13:20:22 -07:00
|
|
|
|
VerifyFieldCount = true,
|
2020-07-15 09:41:59 -07:00
|
|
|
|
};
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-06-14 22:50:58 -07:00
|
|
|
|
// If we're somehow at the end of the stream already, we can't do anything
|
|
|
|
|
|
if (svr.EndOfStream)
|
|
|
|
|
|
return;
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-06-14 22:50:58 -07:00
|
|
|
|
// Read in the header
|
|
|
|
|
|
svr.ReadHeader();
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-06-14 22:50:58 -07:00
|
|
|
|
// Loop through all of the data lines
|
|
|
|
|
|
while (!svr.EndOfStream)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
2020-06-14 22:50:58 -07:00
|
|
|
|
// Get the current line, split and parse
|
|
|
|
|
|
svr.ReadNextLine();
|
2020-09-21 13:20:22 -07:00
|
|
|
|
|
2020-12-13 13:22:06 -08:00
|
|
|
|
// Create mapping dictionaries
|
|
|
|
|
|
var datHeaderMappings = new Dictionary<DatHeaderField, string>();
|
|
|
|
|
|
var machineMappings = new Dictionary<MachineField, string>();
|
|
|
|
|
|
var datItemMappings = new Dictionary<DatItemField, string>();
|
2020-09-21 13:20:22 -07:00
|
|
|
|
|
|
|
|
|
|
// Now we loop through and get values for everything
|
|
|
|
|
|
for (int i = 0; i < svr.HeaderValues.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
string value = svr.Line[i];
|
2020-12-13 13:22:06 -08:00
|
|
|
|
DatHeaderField dhf = svr.HeaderValues[i].AsDatHeaderField();
|
|
|
|
|
|
if (dhf != DatHeaderField.NULL)
|
|
|
|
|
|
{
|
|
|
|
|
|
datHeaderMappings[dhf] = value;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MachineField mf = svr.HeaderValues[i].AsMachineField();
|
|
|
|
|
|
if (mf != MachineField.NULL)
|
|
|
|
|
|
{
|
|
|
|
|
|
machineMappings[mf] = value;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DatItemField dif = svr.HeaderValues[i].AsDatItemField();
|
|
|
|
|
|
if (dif != DatItemField.NULL)
|
|
|
|
|
|
{
|
|
|
|
|
|
datItemMappings[dif] = value;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2020-09-21 13:20:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set DatHeader fields
|
|
|
|
|
|
DatHeader header = new DatHeader();
|
2020-12-13 13:22:06 -08:00
|
|
|
|
header.SetFields(datHeaderMappings);
|
2020-09-21 13:20:22 -07:00
|
|
|
|
Header.ConditionalCopy(header);
|
|
|
|
|
|
|
|
|
|
|
|
// Set Machine and DatItem fields
|
2020-12-13 13:22:06 -08:00
|
|
|
|
if (datItemMappings.ContainsKey(DatItemField.Type))
|
2020-09-21 13:20:22 -07:00
|
|
|
|
{
|
2020-12-13 13:22:06 -08:00
|
|
|
|
DatItem datItem = DatItem.Create(datItemMappings[DatItemField.Type].AsItemType());
|
|
|
|
|
|
datItem.SetFields(datItemMappings, machineMappings);
|
2020-09-21 13:20:22 -07:00
|
|
|
|
datItem.Source = new Source(indexId, filename);
|
|
|
|
|
|
ParseAddHelper(datItem);
|
|
|
|
|
|
}
|
2019-01-11 13:43:15 -08:00
|
|
|
|
}
|
2020-09-21 13:20:22 -07:00
|
|
|
|
catch (Exception ex)
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
2020-09-21 13:04:11 -07:00
|
|
|
|
string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";
|
2020-10-07 15:42:30 -07:00
|
|
|
|
logger.Error(ex, message);
|
2020-09-21 13:04:11 -07:00
|
|
|
|
if (throwOnError)
|
|
|
|
|
|
{
|
|
|
|
|
|
svr.Dispose();
|
|
|
|
|
|
throw new Exception(message, ex);
|
|
|
|
|
|
}
|
2019-01-11 13:43:15 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-09-07 12:58:55 -07:00
|
|
|
|
|
|
|
|
|
|
svr.Dispose();
|
2019-01-11 13:43:15 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-18 17:12:31 -07:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
protected override ItemType[] GetSupportedTypes()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new ItemType[] { ItemType.Disk, ItemType.Media, ItemType.Rom };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-11 13:43:15 -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-01-11 13:43:15 -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-01-11 13:43:15 -08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2020-10-07 15:42:30 -07:00
|
|
|
|
logger.User($"Opening file for writing: {outfile}");
|
2020-12-08 00:13:22 -08:00
|
|
|
|
FileStream fs = File.Create(outfile);
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
|
|
|
|
|
// If we get back null for some reason, just log and return
|
|
|
|
|
|
if (fs == null)
|
|
|
|
|
|
{
|
2020-10-07 15:42:30 -07:00
|
|
|
|
logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
|
2019-01-11 13:43:15 -08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
SeparatedValueWriter svw = new SeparatedValueWriter(fs, new UTF8Encoding(false))
|
|
|
|
|
|
{
|
|
|
|
|
|
Quotes = true,
|
|
|
|
|
|
Separator = this._delim,
|
|
|
|
|
|
VerifyFieldCount = true
|
|
|
|
|
|
};
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
|
|
|
|
|
// Write out the header
|
2020-06-13 22:15:21 -07:00
|
|
|
|
WriteHeader(svw);
|
2019-01-11 13:43:15 -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-01-11 13:43:15 -08:00
|
|
|
|
{
|
2020-08-28 15:06:07 -07:00
|
|
|
|
List<DatItem> datItems = Items.FilteredItems(key);
|
2019-01-11 13:43:15 -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-01-11 13:43:15 -08:00
|
|
|
|
// Resolve the names in the block
|
2020-08-28 15:06:07 -07:00
|
|
|
|
datItems = DatItem.ResolveNames(datItems);
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-08-28 15:06:07 -07:00
|
|
|
|
for (int index = 0; index < datItems.Count; index++)
|
2019-01-11 13:43:15 -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-01-11 13:43:15 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-07 15:42:30 -07:00
|
|
|
|
logger.Verbose("File written!" + Environment.NewLine);
|
2020-06-13 22:15:21 -07:00
|
|
|
|
svw.Dispose();
|
2019-01-11 13:43:15 -08:00
|
|
|
|
fs.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2020-10-07 15:42:30 -07:00
|
|
|
|
logger.Error(ex);
|
2020-09-15 14:23:40 -07:00
|
|
|
|
if (throwOnError) throw ex;
|
2019-01-11 13:43:15 -08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write out DAT header using the supplied StreamWriter
|
|
|
|
|
|
/// </summary>
|
2020-06-13 22:15:21 -07:00
|
|
|
|
/// <param name="svw">SeparatedValueWriter to output to</param>
|
2020-09-15 14:23:40 -07:00
|
|
|
|
private void WriteHeader(SeparatedValueWriter svw)
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
2020-09-15 14:23:40 -07:00
|
|
|
|
string[] headers = new string[]
|
|
|
|
|
|
{
|
2020-06-13 22:15:21 -07:00
|
|
|
|
"File Name",
|
|
|
|
|
|
"Internal Name",
|
|
|
|
|
|
"Description",
|
|
|
|
|
|
"Game Name",
|
|
|
|
|
|
"Game Description",
|
|
|
|
|
|
"Type",
|
|
|
|
|
|
"Rom Name",
|
|
|
|
|
|
"Disk Name",
|
|
|
|
|
|
"Size",
|
|
|
|
|
|
"CRC",
|
|
|
|
|
|
"MD5",
|
|
|
|
|
|
//"RIPEMD160",
|
|
|
|
|
|
"SHA1",
|
|
|
|
|
|
"SHA256",
|
|
|
|
|
|
//"SHA384",
|
|
|
|
|
|
//"SHA512",
|
2020-09-04 15:02:15 -07:00
|
|
|
|
//"SpamSum",
|
2020-06-13 22:15:21 -07:00
|
|
|
|
"Nodump",
|
2020-09-15 14:23:40 -07:00
|
|
|
|
};
|
2020-06-13 22:15:21 -07:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
svw.WriteHeader(headers);
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
svw.Flush();
|
2019-01-11 13:43:15 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write out DatItem 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:23:40 -07:00
|
|
|
|
private void WriteDatItem(SeparatedValueWriter svw, DatItem datItem)
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
2020-09-15 14:23:40 -07:00
|
|
|
|
// Separated values should only output Rom and Disk
|
|
|
|
|
|
if (datItem.ItemType != ItemType.Disk && datItem.ItemType != ItemType.Rom)
|
|
|
|
|
|
return;
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
// Build the state
|
|
|
|
|
|
// TODO: Can we have some way of saying what fields to write out? Support for read extends to all fields now
|
|
|
|
|
|
string[] fields = new string[14]; // 18;
|
|
|
|
|
|
fields[0] = Header.FileName;
|
|
|
|
|
|
fields[1] = Header.Name;
|
|
|
|
|
|
fields[2] = Header.Description;
|
|
|
|
|
|
fields[3] = datItem.Machine.Name;
|
|
|
|
|
|
fields[4] = datItem.Machine.Description;
|
2020-06-12 11:02:23 -07:00
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
switch (datItem.ItemType)
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
2020-09-15 14:23:40 -07:00
|
|
|
|
case ItemType.Disk:
|
|
|
|
|
|
var disk = datItem as Disk;
|
|
|
|
|
|
fields[5] = "disk";
|
|
|
|
|
|
fields[6] = string.Empty;
|
|
|
|
|
|
fields[7] = disk.Name;
|
|
|
|
|
|
fields[8] = string.Empty;
|
|
|
|
|
|
fields[9] = string.Empty;
|
|
|
|
|
|
fields[10] = disk.MD5?.ToLowerInvariant();
|
|
|
|
|
|
//fields[11] = string.Empty;
|
|
|
|
|
|
fields[11] = disk.SHA1?.ToLowerInvariant();
|
|
|
|
|
|
fields[12] = string.Empty;
|
|
|
|
|
|
//fields[13] = string.Empty;
|
|
|
|
|
|
//fields[14] = string.Empty;
|
|
|
|
|
|
//fields[15] = string.Empty;
|
|
|
|
|
|
fields[13] = disk.ItemStatus.ToString();
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case ItemType.Media:
|
|
|
|
|
|
var media = datItem as Media;
|
|
|
|
|
|
fields[5] = "media";
|
|
|
|
|
|
fields[6] = string.Empty;
|
|
|
|
|
|
fields[7] = media.Name;
|
|
|
|
|
|
fields[8] = string.Empty;
|
|
|
|
|
|
fields[9] = string.Empty;
|
|
|
|
|
|
fields[10] = media.MD5?.ToLowerInvariant();
|
|
|
|
|
|
//fields[11] = string.Empty;
|
|
|
|
|
|
fields[11] = media.SHA1?.ToLowerInvariant();
|
|
|
|
|
|
fields[12] = media.SHA256?.ToLowerInvariant();
|
|
|
|
|
|
//fields[13] = string.Empty;
|
|
|
|
|
|
//fields[14] = string.Empty;
|
|
|
|
|
|
//fields[15] = media.SpamSum?.ToLowerInvariant();
|
|
|
|
|
|
fields[13] = string.Empty;
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case ItemType.Rom:
|
|
|
|
|
|
var rom = datItem as Rom;
|
|
|
|
|
|
fields[5] = "rom";
|
|
|
|
|
|
fields[6] = rom.Name;
|
|
|
|
|
|
fields[7] = string.Empty;
|
|
|
|
|
|
fields[8] = rom.Size?.ToString();
|
|
|
|
|
|
fields[9] = rom.CRC?.ToLowerInvariant();
|
|
|
|
|
|
fields[10] = rom.MD5?.ToLowerInvariant();
|
|
|
|
|
|
//fields[11] = rom.RIPEMD160?.ToLowerInvariant();
|
|
|
|
|
|
fields[11] = rom.SHA1?.ToLowerInvariant();
|
|
|
|
|
|
fields[12] = rom.SHA256?.ToLowerInvariant();
|
|
|
|
|
|
//fields[13] = rom.SHA384?.ToLowerInvariant();
|
|
|
|
|
|
//fields[14] = rom.SHA512?.ToLowerInvariant();
|
|
|
|
|
|
//fields[15] = rom.SpamSum?.ToLowerInvariant();
|
|
|
|
|
|
fields[13] = rom.ItemStatus.ToString();
|
|
|
|
|
|
break;
|
2019-01-11 13:43:15 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-15 14:23:40 -07:00
|
|
|
|
svw.WriteString(CreatePrefixPostfix(datItem, true));
|
|
|
|
|
|
svw.WriteValues(fields, false);
|
|
|
|
|
|
svw.WriteString(CreatePrefixPostfix(datItem, false));
|
|
|
|
|
|
svw.WriteLine();
|
|
|
|
|
|
|
|
|
|
|
|
svw.Flush();
|
2019-01-11 13:43:15 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-10-09 18:04:49 -07:00
|
|
|
|
}
|