Files
SabreTools/SabreTools.Library/DatFiles/SeparatedValue.cs

318 lines
12 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2020-06-11 11:44:46 -07:00
using System.IO;
using System.Text;
2020-06-11 11:44:46 -07:00
using SabreTools.Library.Data;
using SabreTools.Library.DatItems;
2020-08-01 22:46:28 -07:00
using SabreTools.Library.IO;
using SabreTools.Library.Tools;
namespace SabreTools.Library.DatFiles
{
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
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)
: 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>
/// <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>
protected override void ParseFile(
2019-01-11 13:43:15 -08:00
// Standard Dat parsing
string filename,
int indexId,
2019-01-11 13:43:15 -08:00
// Miscellaneous
bool keep)
2019-01-11 13:43:15 -08:00
{
// Open a file reader
Encoding enc = FileExtensions.GetEncoding(filename);
SeparatedValueReader svr = new SeparatedValueReader(FileExtensions.TryOpenRead(filename), enc)
{
Header = true,
Quotes = true,
Separator = _delim,
VerifyFieldCount = true
};
2019-01-11 13:43:15 -08: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
// Read in the header
svr.ReadHeader();
2019-01-11 13:43:15 -08:00
// Loop through all of the data lines
while (!svr.EndOfStream)
{
try
2019-01-11 13:43:15 -08:00
{
// Get the current line, split and parse
svr.ReadNextLine();
2019-01-11 13:43:15 -08:00
}
catch (InvalidDataException)
2019-01-11 13:43:15 -08:00
{
Globals.Logger.Warning($"Malformed line found in '{filename}' at line {svr.LineNumber}");
2019-01-11 13:43:15 -08:00
continue;
}
2020-08-25 11:20:50 -07:00
// Create mapping dictionary
var mappings = new Dictionary<Field, string>();
2019-01-11 13:43:15 -08:00
// Now we loop through and get values for everything
for (int i = 0; i < svr.HeaderValues.Count; i++)
2019-01-11 13:43:15 -08:00
{
2020-08-25 11:20:50 -07:00
Field key = svr.HeaderValues[i].AsField();
string value = svr.Line[i];
2020-08-25 11:20:50 -07:00
mappings[key] = value;
2019-01-11 13:43:15 -08:00
}
2020-08-25 11:20:50 -07:00
// Set DatHeader fields
DatHeader header = new DatHeader();
header.SetFields(mappings);
Header.ConditionalCopy(header);
2020-08-21 13:31:22 -07:00
2020-08-25 11:20:50 -07:00
// Set Machine and DatItem fields
if (mappings.ContainsKey(Field.DatItem_Type))
{
DatItem datItem = DatItem.Create(mappings[Field.DatItem_Type].AsItemType());
datItem.SetFields(mappings);
datItem.Source = new Source(indexId, filename);
ParseAddHelper(datItem);
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>
/// <returns>True if the DAT was written correctly, false otherwise</returns>
public override bool WriteToFile(string outfile, bool ignoreblanks = false)
{
try
{
Globals.Logger.User($"Opening file for writing: {outfile}");
FileStream fs = FileExtensions.TryCreate(outfile);
2019-01-11 13:43:15 -08:00
// If we get back null for some reason, just log and return
if (fs == null)
{
Globals.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;
}
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
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-07-26 22:34:45 -07:00
List<DatItem> roms = Items[key];
2019-01-11 13:43:15 -08:00
// Resolve the names in the block
roms = DatItem.ResolveNames(roms);
for (int index = 0; index < roms.Count; index++)
{
DatItem rom = roms[index];
// There are apparently times when a null rom can skip by, skip them
2020-08-20 13:17:14 -07:00
if (rom.Name == null || rom.Machine.Name == null)
2019-01-11 13:43:15 -08:00
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a "null" game (created by DATFromDir or something similar), log it to file
if (rom.ItemType == ItemType.Rom
&& (rom as Rom).Size == -1
&& (rom as Rom).CRC == "null")
2019-01-11 13:43:15 -08:00
{
2020-08-20 13:17:14 -07:00
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
2019-01-11 13:43:15 -08:00
}
// Now, output the rom data
WriteDatItem(svw, rom, ignoreblanks);
2019-01-11 13:43:15 -08:00
}
}
Globals.Logger.Verbose("File written!" + Environment.NewLine);
svw.Dispose();
2019-01-11 13:43:15 -08:00
fs.Dispose();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out DAT header using the supplied StreamWriter
/// </summary>
/// <param name="svw">SeparatedValueWriter to output to</param>
2019-01-11 13:43:15 -08:00
/// <returns>True if the data was written, false on error</returns>
private bool WriteHeader(SeparatedValueWriter svw)
2019-01-11 13:43:15 -08:00
{
try
{
string[] headers = new string[]
{
"File Name",
"Internal Name",
"Description",
"Game Name",
"Game Description",
"Type",
"Rom Name",
"Disk Name",
"Size",
"CRC",
"MD5",
//"RIPEMD160",
"SHA1",
"SHA256",
//"SHA384",
//"SHA512",
"Nodump",
};
svw.WriteHeader(headers);
svw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out DatItem using the supplied StreamWriter
/// </summary>
/// <param name="svw">SeparatedValueWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
2019-01-11 13:43:15 -08:00
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(SeparatedValueWriter svw, DatItem datItem, bool ignoreblanks = false)
2019-01-11 13:43:15 -08:00
{
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
2019-01-11 13:43:15 -08:00
return true;
try
{
// Separated values should only output Rom and Disk
if (datItem.ItemType != ItemType.Disk && datItem.ItemType != ItemType.Rom)
2019-01-11 13:43:15 -08:00
return true;
2020-08-23 22:23:55 -07:00
// Build the state
2020-06-14 22:51:55 -07:00
// 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]; // 17;
fields[0] = Header.FileName;
fields[1] = Header.Name;
fields[2] = Header.Description;
2020-08-23 22:23:55 -07:00
fields[3] = datItem.Machine.Name;
fields[4] = datItem.Machine.Description;
2020-06-12 11:02:23 -07:00
switch (datItem.ItemType)
2019-01-11 13:43:15 -08:00
{
2020-06-12 11:02:23 -07:00
case ItemType.Disk:
var disk = datItem as Disk;
fields[5] = "disk";
fields[6] = string.Empty;
2020-08-23 22:23:55 -07:00
fields[7] = disk.Name;
fields[8] = string.Empty;
fields[9] = string.Empty;
2020-08-23 22:23:55 -07:00
fields[10] = disk.MD5.ToLowerInvariant();
//fields[11] = string.Empty;
2020-08-23 22:23:55 -07:00
fields[11] = disk.SHA1.ToLowerInvariant();
fields[12] = string.Empty;
//fields[13] = string.Empty;
//fields[14] = string.Empty;
2020-08-23 22:23:55 -07:00
fields[13] = disk.ItemStatus.ToString();
2020-06-12 11:02:23 -07:00
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[13] = string.Empty;
break;
2020-06-12 11:02:23 -07:00
case ItemType.Rom:
var rom = datItem as Rom;
fields[5] = "rom";
2020-08-23 22:23:55 -07:00
fields[6] = rom.Name;
fields[7] = string.Empty;
2020-08-23 22:23:55 -07:00
fields[8] = rom.Size.ToString();
2020-08-24 11:56:49 -07:00
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();
2020-08-23 22:23:55 -07:00
fields[13] = rom.ItemStatus.ToString();
2020-06-12 11:02:23 -07:00
break;
2019-01-11 13:43:15 -08:00
}
svw.WriteString(CreatePrefixPostfix(datItem, true));
svw.WriteValues(fields, false);
svw.WriteString(CreatePrefixPostfix(datItem, false));
svw.WriteLine();
2020-06-12 11:02:23 -07:00
svw.Flush();
2019-01-11 13:43:15 -08:00
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
}
}