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

457 lines
18 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>
/// <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
/// <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(string filename, int indexId, bool keep, bool throwOnError = false)
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)
{
try
2019-01-11 13:43:15 -08:00
{
string line = sr.ReadLine().Trim();
2019-01-11 13:43:15 -08:00
// If we have a blank line, we just skip it
if (string.IsNullOrWhiteSpace(line))
2019-01-11 13:43:15 -08:00
{
continue;
2019-01-11 13:43:15 -08:00
}
// If we have the descriptor line, ignore it
else if (line == "Name Size Checksum")
2019-01-11 13:43:15 -08:00
{
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;
2019-01-11 13:43:15 -08:00
}
// Otherwise, we assume we have a rom that we need to add
else
2019-01-11 13:43:15 -08:00
{
// First, we preprocess the line so that the rom name is consistently correct
string[] split = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
2019-01-11 13:43:15 -08:00
// 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];
line = line.Substring(romname.Length);
2019-01-11 13:43:15 -08:00
// Next we separate the ROM into pieces
split = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
2019-01-11 13:43:15 -08:00
// Standard Disks have 2 pieces (name, sha1)
if (split.Length == 1)
{
Disk disk = new Disk()
2020-08-20 13:17:14 -07:00
{
Name = romname,
SHA1 = Sanitizer.CleanListromHashData(split[0]),
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(disk);
}
// Baddump Disks have 4 pieces (name, BAD, sha1, BAD_DUMP)
else if (split.Length == 3 && line.EndsWith("BAD_DUMP"))
{
Disk disk = new Disk()
2020-08-20 13:17:14 -07:00
{
Name = romname,
SHA1 = Sanitizer.CleanListromHashData(split[1]),
ItemStatus = ItemStatus.BadDump,
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(disk);
}
// Standard ROMs have 4 pieces (name, size, crc, sha1)
else if (split.Length == 3)
2019-01-11 13:43:15 -08:00
{
Rom rom = new Rom()
2020-08-20 13:17:14 -07:00
{
Name = romname,
Size = Sanitizer.CleanLong(split[0]),
CRC = Sanitizer.CleanListromHashData(split[1]),
SHA1 = Sanitizer.CleanListromHashData(split[2]),
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom);
}
// 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()
2020-08-20 13:17:14 -07:00
{
Name = romname,
ItemStatus = ItemStatus.Nodump,
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(disk);
}
// Baddump ROMs have 6 pieces (name, size, BAD, crc, sha1, BAD_DUMP)
else if (split.Length == 5 && line.EndsWith("BAD_DUMP"))
2019-01-11 13:43:15 -08:00
{
Rom rom = new Rom()
2020-08-20 13:17:14 -07:00
{
Name = romname,
Size = Sanitizer.CleanLong(split[0]),
CRC = Sanitizer.CleanListromHashData(split[2]),
SHA1 = Sanitizer.CleanListromHashData(split[3]),
ItemStatus = ItemStatus.BadDump,
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom);
}
// Nodump ROMs have 6 pieces (name, size, NO, GOOD, DUMP, KNOWN)
else if (split.Length == 5 && line.EndsWith("NO GOOD DUMP KNOWN"))
{
Rom rom = new Rom()
2020-08-20 13:17:14 -07:00
{
Name = romname,
Size = Sanitizer.CleanLong(split[0]),
ItemStatus = ItemStatus.Nodump,
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom);
}
// 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
}
}
catch (Exception ex)
{
string message = $"'{filename}' - There was an error parsing at position {sr.BaseStream.Position}";
Globals.Logger.Error(ex, message);
if (throwOnError)
2019-01-11 13:43:15 -08:00
{
sr.Dispose();
throw new Exception(message, ex);
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.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>
/// <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>
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
2019-01-11 13:43:15 -08:00
{
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-08-28 15:06:07 -07:00
List<DatItem> datItems = Items.FilteredItems(key);
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];
2019-01-11 13:43:15 -08:00
// If we have a different game and we're not at the start of the list, output the end of last item
2020-08-28 15:06:07 -07:00
if (lastgame != null && lastgame.ToLowerInvariant() != datItem.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-28 15:06:07 -07:00
if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteStartGame(sw, datItem);
2019-01-11 13:43:15 -08:00
2020-08-28 15:06:07 -07:00
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
2019-01-11 13:43:15 -08:00
2020-08-28 15:06:07 -07:00
// Write out the item if we're not ignoring
if (!ShouldIgnore(datItem, ignoreblanks))
WriteDatItem(sw, datItem);
2019-01-11 13:43:15 -08:00
// Set the new data to compare against
2020-08-28 15:06:07 -07:00
lastgame = datItem.Machine.Name;
2019-01-11 13:43:15 -08:00
}
}
Globals.Logger.Verbose("File written!" + Environment.NewLine);
sw.Dispose();
fs.Dispose();
}
catch (Exception ex)
{
2020-09-15 14:38:37 -07:00
Globals.Logger.Error(ex);
if (throwOnError) throw ex;
2019-01-11 13:43:15 -08:00
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>
private void WriteStartGame(StreamWriter sw, DatItem rom)
2019-01-11 13:43:15 -08:00
{
// No game should start with a path separator
rom.Machine.Name = rom.Machine.Name.TrimStart(Path.DirectorySeparatorChar);
2019-01-11 13:43:15 -08:00
// Build the state
sw.Write($"ROMs required for driver \"{rom.Machine.Name}\".\n");
sw.Write("Name Size Checksum\n");
2019-01-11 13:43:15 -08:00
sw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Write out Game end using the supplied StreamWriter
/// </summary>
/// <param name="sw">StreamWriter to output to</param>
private void WriteEndGame(StreamWriter sw)
2019-01-11 13:43:15 -08:00
{
// End driver
sw.Write("\n");
2019-01-11 13:43:15 -08:00
sw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <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>
private void WriteDatItem(StreamWriter sw, DatItem datItem)
2019-01-11 13:43:15 -08:00
{
// Pre-process the item name
ProcessItemName(datItem, true);
2019-01-11 13:43:15 -08:00
// Build the state
switch (datItem.ItemType)
{
case ItemType.Disk:
var disk = datItem as Disk;
// The name is padded out to a particular length
if (disk.Name.Length < 43)
sw.Write(disk.Name.PadRight(43, ' '));
else
sw.Write($"{disk.Name} ");
2019-01-11 13:43:15 -08:00
// If we have a baddump, put the first indicator
if (disk.ItemStatus == ItemStatus.BadDump)
sw.Write(" BAD");
2019-01-11 13:43:15 -08:00
// If we have a nodump, write out the indicator
if (disk.ItemStatus == ItemStatus.Nodump)
sw.Write(" NO GOOD DUMP KNOWN");
// Otherwise, write out the SHA-1 hash
else if (!string.IsNullOrWhiteSpace(disk.SHA1))
sw.Write($" SHA1({disk.SHA1 ?? string.Empty})");
2019-01-11 13:43:15 -08:00
// If we have a baddump, put the second indicator
if (disk.ItemStatus == ItemStatus.BadDump)
sw.Write(" BAD_DUMP");
2019-01-11 13:43:15 -08:00
sw.Write("\n");
break;
case ItemType.Rom:
var rom = datItem as Rom;
// The name is padded out to a particular length
if (rom.Name.Length < 43)
sw.Write(rom.Name.PadRight(43 - rom.Size?.ToString().Length ?? 0, ' '));
else
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)
sw.Write(rom.Size?.ToString() ?? string.Empty);
2019-01-11 13:43:15 -08:00
// If we have a baddump, put the first indicator
if (rom.ItemStatus == ItemStatus.BadDump)
sw.Write(" BAD");
2019-01-11 13:43:15 -08:00
// If we have a nodump, write out the indicator
if (rom.ItemStatus == ItemStatus.Nodump)
{
sw.Write(" NO GOOD DUMP KNOWN");
}
// Otherwise, write out the CRC and SHA-1 hashes
else
{
if (!string.IsNullOrWhiteSpace(rom.CRC))
sw.Write($" CRC({rom.CRC ?? string.Empty})");
if (!string.IsNullOrWhiteSpace(rom.SHA1))
sw.Write($" SHA1({rom.SHA1 ?? string.Empty})");
}
2019-01-11 13:43:15 -08:00
// If we have a baddump, put the second indicator
if (rom.ItemStatus == ItemStatus.BadDump)
sw.Write(" BAD_DUMP");
2020-09-15 12:12:13 -07:00
sw.Write("\n");
break;
2019-01-11 13:43:15 -08:00
}
sw.Flush();
2019-01-11 13:43:15 -08:00
}
}
}