Files
SabreTools/SabreTools.Library/DatFiles/Listrom.cs

508 lines
20 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using SabreTools.Library.Data;
using SabreTools.Library.DatItems;
2020-08-01 23:04:11 -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 MAME Listrom DAT
/// </summary>
internal class Listrom : DatFile
{
/// <summary>
/// Constructor designed for casting a base DatFile
/// </summary>
/// <param name="datFile">Parent DatFile to copy from</param>
public Listrom(DatFile datFile)
: base(datFile)
2019-01-11 13:43:15 -08:00
{
}
/// <summary>
/// Parse a MAME Listrom 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>
/// <remarks>
/// In a new style MAME listrom DAT, each game has the following format:
///
/// ROMs required for driver "005".
/// Name Size Checksum
/// 1346b.cpu-u25 2048 CRC(8e68533e) SHA1(a257c556d31691068ed5c991f1fb2b51da4826db)
/// 6331.sound-u8 32 BAD CRC(1d298cb0) SHA1(bb0bb62365402543e3154b9a77be9c75010e6abc) BAD_DUMP
/// 16v8h-blue.u24 279 NO GOOD DUMP KNOWN
/// </remarks>
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);
StreamReader sr = new StreamReader(FileExtensions.TryOpenRead(filename), enc);
2019-01-11 13:43:15 -08:00
string gamename = string.Empty;
2019-01-11 13:43:15 -08:00
while (!sr.EndOfStream)
{
string line = sr.ReadLine().Trim();
// If we have a blank line, we just skip it
if (string.IsNullOrWhiteSpace(line))
2019-01-11 13:43:15 -08:00
{
continue;
}
// If we have the descriptor line, ignore it
else if (line == "Name Size Checksum")
{
continue;
}
// If we have the beginning of a game, set the name of the game
else if (line.StartsWith("ROMs required for"))
{
gamename = Regex.Match(line, @"^ROMs required for \S*? string.Empty(.*?)string.Empty\.").Groups[1].Value;
2019-01-11 13:43:15 -08:00
}
// If we have a machine with no required roms (usually internal devices), skip it
else if (line.StartsWith("No ROMs required for"))
{
continue;
}
// Otherwise, we assume we have a rom that we need to add
else
{
// First, we preprocess the line so that the rom name is consistently correct
string[] split = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
// If the line doesn't have the 4 spaces of padding, check for 3
if (split.Length == 1)
split = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
// If the split is still unsuccessful, log it and skip
if (split.Length == 1)
Globals.Logger.Warning($"Possibly malformed line: '{line}'");
2019-01-11 13:43:15 -08:00
string romname = split[0];
2019-01-11 13:43:15 -08:00
line = line.Substring(romname.Length);
// Next we separate the ROM into pieces
split = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
// Standard Disks have 2 pieces (name, sha1)
if (split.Length == 1)
{
Disk disk = new Disk()
{
Name = romname,
SHA1 = Sanitizer.CleanListromHashData(split[0]),
2019-01-11 13:43:15 -08:00
2020-08-20 13:17:14 -07:00
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
2019-01-11 13:43:15 -08:00
};
ParseAddHelper(disk);
2019-01-11 13:43:15 -08:00
}
// Baddump Disks have 4 pieces (name, BAD, sha1, BAD_DUMP)
else if (split.Length == 3 && line.EndsWith("BAD_DUMP"))
{
Disk disk = new Disk()
{
Name = romname,
SHA1 = Sanitizer.CleanListromHashData(split[1]),
2019-01-11 13:43:15 -08:00
ItemStatus = ItemStatus.BadDump,
2020-08-20 13:17:14 -07:00
Machine = new Machine
{
Name = gamename,
},
2020-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2019-01-11 13:43:15 -08:00
};
ParseAddHelper(disk);
2019-01-11 13:43:15 -08:00
}
// Standard ROMs have 4 pieces (name, size, crc, sha1)
else if (split.Length == 3)
{
if (!Int64.TryParse(split[0], out long size))
size = 0;
Rom rom = new Rom()
{
Name = romname,
Size = size,
CRC = Sanitizer.CleanListromHashData(split[1]),
SHA1 = Sanitizer.CleanListromHashData(split[2]),
2019-01-11 13:43:15 -08:00
2020-08-20 13:17:14 -07:00
Machine = new Machine
{
Name = gamename,
},
2020-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2019-01-11 13:43:15 -08:00
};
ParseAddHelper(rom);
2019-01-11 13:43:15 -08:00
}
// Nodump Disks have 5 pieces (name, NO, GOOD, DUMP, KNOWN)
else if (split.Length == 4 && line.EndsWith("NO GOOD DUMP KNOWN"))
{
Disk disk = new Disk()
{
Name = romname,
ItemStatus = ItemStatus.Nodump,
2020-08-20 13:17:14 -07:00
Machine = new Machine
{
Name = gamename,
},
2020-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2019-01-11 13:43:15 -08:00
};
ParseAddHelper(disk);
2019-01-11 13:43:15 -08:00
}
// Baddump ROMs have 6 pieces (name, size, BAD, crc, sha1, BAD_DUMP)
else if (split.Length == 5 && line.EndsWith("BAD_DUMP"))
{
if (!Int64.TryParse(split[0], out long size))
size = 0;
Rom rom = new Rom()
{
Name = romname,
Size = size,
CRC = Sanitizer.CleanListromHashData(split[2]),
SHA1 = Sanitizer.CleanListromHashData(split[3]),
2019-01-11 13:43:15 -08:00
ItemStatus = ItemStatus.BadDump,
2020-08-20 13:17:14 -07:00
Machine = new Machine
{
Name = gamename,
},
2020-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2019-01-11 13:43:15 -08:00
};
ParseAddHelper(rom);
2019-01-11 13:43:15 -08:00
}
// Nodump ROMs have 6 pieces (name, size, NO, GOOD, DUMP, KNOWN)
else if (split.Length == 5 && line.EndsWith("NO GOOD DUMP KNOWN"))
{
if (!Int64.TryParse(split[0], out long size))
size = 0;
Rom rom = new Rom()
{
Name = romname,
Size = size,
ItemStatus = ItemStatus.Nodump,
2020-08-20 13:17:14 -07:00
Machine = new Machine
{
Name = gamename,
},
2020-08-20 13:17:14 -07:00
Source = new Source
{
Index = indexId,
Name = filename,
},
2019-01-11 13:43:15 -08:00
};
ParseAddHelper(rom);
2019-01-11 13:43:15 -08:00
}
// If we have something else, it's invalid
else
{
Globals.Logger.Warning($"Invalid line detected: '{romname} {line}'");
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;
}
StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(false));
// Write out each of the machines and roms
string lastgame = null;
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 different game and we're not at the start of the list, output the end of last item
2020-08-20 13:17:14 -07:00
if (lastgame != null && lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant())
2019-01-11 13:43:15 -08:00
WriteEndGame(sw);
// If we have a new game, output the beginning of the new item
2020-08-20 13:17:14 -07:00
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant())
2019-01-11 13:43:15 -08:00
WriteStartGame(sw, rom);
// If we have a "null" game (created by DATFromDir or something similar), log it to file
if (rom.ItemType == ItemType.Rom
&& ((Rom)rom).Size == -1
&& ((Rom)rom).CRC == "null")
{
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
rom.Name = (rom.Name == "null" ? "-" : rom.Name);
((Rom)rom).Size = Constants.SizeZero;
((Rom)rom).CRC = ((Rom)rom).CRC == "null" ? Constants.CRCZero : null;
((Rom)rom).MD5 = ((Rom)rom).MD5 == "null" ? Constants.MD5Zero : null;
((Rom)rom).SHA1 = ((Rom)rom).SHA1 == "null" ? Constants.SHA1Zero : null;
((Rom)rom).SHA256 = ((Rom)rom).SHA256 == "null" ? Constants.SHA256Zero : null;
((Rom)rom).SHA384 = ((Rom)rom).SHA384 == "null" ? Constants.SHA384Zero : null;
((Rom)rom).SHA512 = ((Rom)rom).SHA512 == "null" ? Constants.SHA512Zero : null;
}
// Now, output the rom data
WriteDatItem(sw, rom, ignoreblanks);
// Set the new data to compare against
2020-08-20 13:17:14 -07:00
lastgame = rom.Machine.Name;
2019-01-11 13:43:15 -08:00
}
}
Globals.Logger.Verbose("File written!" + Environment.NewLine);
sw.Dispose();
fs.Dispose();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out Game start using the supplied StreamWriter
/// </summary>
/// <param name="sw">StreamWriter to output to</param>
/// <param name="rom">DatItem object to be output</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteStartGame(StreamWriter sw, DatItem rom)
{
try
{
// No game should start with a path separator
2020-08-20 13:17:14 -07:00
rom.Machine.Name = rom.Machine.Name.TrimStart(Path.DirectorySeparatorChar);
2019-01-11 13:43:15 -08:00
2020-08-23 22:23:55 -07:00
// Build the state
sw.Write($"ROMs required for driver \"{rom.Machine.Name}\".\n");
2020-06-12 11:02:23 -07:00
sw.Write("Name Size Checksum\n");
2019-01-11 13:43:15 -08:00
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out Game end using the supplied StreamWriter
/// </summary>
/// <param name="sw">StreamWriter to output to</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteEndGame(StreamWriter sw)
{
try
{
2020-06-12 11:02:23 -07:00
// End driver
sw.Write("\n");
2019-01-11 13:43:15 -08:00
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out DatItem using the supplied StreamWriter
/// </summary>
/// <param name="sw">StreamWriter 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(StreamWriter sw, 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
{
// Pre-process the item name
ProcessItemName(datItem, true);
2019-01-11 13:43:15 -08:00
2020-08-23 22:23:55 -07:00
// Build the state
switch (datItem.ItemType)
2019-01-11 13:43:15 -08:00
{
case ItemType.Disk:
var disk = datItem as Disk;
2019-01-11 13:43:15 -08:00
// The name is padded out to a particular length
if (disk.Name.Length < 43)
2020-06-12 11:02:23 -07:00
sw.Write(disk.Name.PadRight(43, ' '));
2019-01-11 13:43:15 -08:00
else
2020-06-12 11:02:23 -07:00
sw.Write($"{disk.Name} ");
2019-01-11 13:43:15 -08:00
// If we have a baddump, put the first indicator
2020-08-23 22:23:55 -07:00
if (disk.ItemStatus == ItemStatus.BadDump)
2020-06-12 11:02:23 -07:00
sw.Write(" BAD");
2019-01-11 13:43:15 -08:00
// If we have a nodump, write out the indicator
2020-08-23 22:23:55 -07:00
if (disk.ItemStatus == ItemStatus.Nodump)
2020-06-12 11:02:23 -07:00
sw.Write(" NO GOOD DUMP KNOWN");
2019-01-11 13:43:15 -08:00
// Otherwise, write out the SHA-1 hash
2020-08-23 22:23:55 -07:00
else if (!string.IsNullOrWhiteSpace(disk.SHA1))
2020-08-24 11:56:49 -07:00
sw.Write($" SHA1({disk.SHA1 ?? string.Empty})");
2019-01-11 13:43:15 -08:00
// If we have a baddump, put the second indicator
2020-08-23 22:23:55 -07:00
if (disk.ItemStatus == ItemStatus.BadDump)
2020-06-12 11:02:23 -07:00
sw.Write(" BAD_DUMP");
2019-01-11 13:43:15 -08:00
2020-06-12 11:02:23 -07:00
sw.Write("\n");
2019-01-11 13:43:15 -08:00
break;
2019-01-11 13:43:15 -08:00
case ItemType.Rom:
var rom = datItem as Rom;
2019-01-11 13:43:15 -08:00
// The name is padded out to a particular length
2020-06-12 11:02:23 -07:00
if (rom.Name.Length < 43)
sw.Write(rom.Name.PadRight(43 - rom.Size.ToString().Length, ' '));
2019-01-11 13:43:15 -08:00
else
2020-06-12 11:02:23 -07:00
sw.Write($"{rom.Name} ");
2019-01-11 13:43:15 -08:00
// If we don't have a nodump, write out the size
if (rom.ItemStatus != ItemStatus.Nodump)
2020-06-12 11:02:23 -07:00
sw.Write(rom.Size);
2019-01-11 13:43:15 -08:00
// If we have a baddump, put the first indicator
2020-08-23 22:23:55 -07:00
if (rom.ItemStatus == ItemStatus.BadDump)
2020-06-12 11:02:23 -07:00
sw.Write(" BAD");
2019-01-11 13:43:15 -08:00
// If we have a nodump, write out the indicator
2020-08-23 22:23:55 -07:00
if (rom.ItemStatus == ItemStatus.Nodump)
2019-01-11 13:43:15 -08:00
{
2020-06-12 11:02:23 -07:00
sw.Write(" NO GOOD DUMP KNOWN");
2019-01-11 13:43:15 -08:00
}
// Otherwise, write out the CRC and SHA-1 hashes
else
{
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(rom.CRC))
2020-08-24 11:56:49 -07:00
sw.Write($" CRC({rom.CRC ?? string.Empty})");
2020-08-23 22:23:55 -07:00
if (!string.IsNullOrWhiteSpace(rom.SHA1))
2020-08-24 11:56:49 -07:00
sw.Write($" SHA1({rom.SHA1 ?? string.Empty})");
2019-01-11 13:43:15 -08:00
}
// If we have a baddump, put the second indicator
2020-08-23 22:23:55 -07:00
if (rom.ItemStatus == ItemStatus.BadDump)
2020-06-12 11:02:23 -07:00
sw.Write(" BAD_DUMP");
2019-01-11 13:43:15 -08:00
2020-06-12 11:02:23 -07:00
sw.Write("\n");
2019-01-11 13:43:15 -08:00
break;
}
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
}
}