[DatFile, OpenMSX] Make sure data is clean for writing

This commit is contained in:
Matt Nadareski
2018-03-15 20:25:11 -07:00
parent fddd979287
commit a3a763e237
2 changed files with 37 additions and 15 deletions

View File

@@ -3265,8 +3265,21 @@ namespace SabreTools.Library.DatFiles
itemRom.SHA384 = Utilities.CleanHashData(itemRom.SHA384, Constants.SHA384Length);
itemRom.SHA512 = Utilities.CleanHashData(itemRom.SHA512, Constants.SHA512Length);
// If we have the case where there is SHA-1 and nothing else, we don't fill in any other part of the data
if (itemRom.Size == -1
&& String.IsNullOrWhiteSpace(itemRom.CRC)
&& String.IsNullOrWhiteSpace(itemRom.MD5)
&& !String.IsNullOrWhiteSpace(itemRom.SHA1)
&& String.IsNullOrWhiteSpace(itemRom.SHA256)
&& String.IsNullOrWhiteSpace(itemRom.SHA384)
&& String.IsNullOrWhiteSpace(itemRom.SHA512))
{
// No-op, just catch it so it doesn't go further
Globals.Logger.Verbose("{0}: Entry with only SHA-1 found - '{1}'", FileName, itemRom.Name);
}
// If we have a rom and it's missing size AND the hashes match a 0-byte file, fill in the rest of the info
if ((itemRom.Size == 0 || itemRom.Size == -1)
else if ((itemRom.Size == 0 || itemRom.Size == -1)
&& ((itemRom.CRC == Constants.CRCZero || String.IsNullOrWhiteSpace(itemRom.CRC))
|| itemRom.MD5 == Constants.MD5Zero
|| itemRom.SHA1 == Constants.SHA1Zero

View File

@@ -144,6 +144,7 @@ namespace SabreTools.Library.DatFiles
// Otherwise, add what is possible
reader.MoveToContent();
int diskno = 0;
bool containsItems = false;
// Create a new machine
@@ -183,7 +184,8 @@ namespace SabreTools.Library.DatFiles
reader.Read();
break;
case "dump":
containsItems = ReadDump(reader.ReadSubtree(), machine, filename, sysid, srcid, keep, clean, remUnicode);
containsItems = ReadDump(reader.ReadSubtree(), machine, diskno, filename, sysid, srcid, keep, clean, remUnicode);
diskno++;
// Skip the dump now that we've processed it
reader.Skip();
@@ -218,6 +220,7 @@ namespace SabreTools.Library.DatFiles
/// </summary>
/// <param name="reader">XmlReader representing a part block</param>
/// <param name="machine">Machine information to pass to contained items</param>
/// <param name="diskno">Disk number to use when outputting to other DAT formats</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="sysid">System ID for the DAT</param>
/// <param name="srcid">Source ID for the DAT</param>
@@ -227,6 +230,7 @@ namespace SabreTools.Library.DatFiles
private bool ReadDump(
XmlReader reader,
Machine machine,
int diskno,
// Standard Dat parsing
string filename,
@@ -238,7 +242,6 @@ namespace SabreTools.Library.DatFiles
bool clean,
bool remUnicode)
{
string temptype = reader.Name;
bool containsItems = false;
while (!reader.EOF)
@@ -254,19 +257,19 @@ namespace SabreTools.Library.DatFiles
switch (reader.Name)
{
case "rom":
containsItems = ReadRom(reader.ReadSubtree(), machine, filename, sysid, srcid, keep, clean, remUnicode);
containsItems = ReadRom(reader.ReadSubtree(), machine, diskno, filename, sysid, srcid, keep, clean, remUnicode);
// Skip the rom now that we've processed it
reader.Skip();
break;
case "megarom":
containsItems = ReadMegaRom(reader.ReadSubtree(), machine, filename, sysid, srcid, keep, clean, remUnicode);
containsItems = ReadMegaRom(reader.ReadSubtree(), machine, diskno, filename, sysid, srcid, keep, clean, remUnicode);
// Skip the megarom now that we've processed it
reader.Skip();
break;
case "sccpluscart":
containsItems = ReadSccPlusCart(reader.ReadSubtree(), machine, filename, sysid, srcid, keep, clean, remUnicode);
containsItems = ReadSccPlusCart(reader.ReadSubtree(), machine, diskno, filename, sysid, srcid, keep, clean, remUnicode);
// Skip the sccpluscart now that we've processed it
reader.Skip();
@@ -288,18 +291,19 @@ namespace SabreTools.Library.DatFiles
/// <summary>
/// Read rom information
/// </summary>
/// <param name="reader">XmlReader representing a part block</param>
/// <param name="reader">XmlReader representing a rom block</param>
/// <param name="machine">Machine information to pass to contained items</param>
/// <param name="diskno">Disk number to use when outputting to other DAT formats</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="sysid">System ID for the DAT</param>
/// <param name="srcid">Source ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
/// <param name="clean">True if game names are sanitized, false otherwise (default)</param>
/// <param name="remUnicode">True if we should remove non-ASCII characters from output, false otherwise (default)</param>
/// TODO: Make sure that this outputs proper values since only SHA-1 is included
private bool ReadRom(
XmlReader reader,
Machine machine,
int diskno,
// Standard Dat parsing
string filename,
@@ -348,8 +352,9 @@ namespace SabreTools.Library.DatFiles
// Create and add the new rom
Rom rom = new Rom
{
Name = machine.Name + (!String.IsNullOrWhiteSpace(remark) ? " " + remark : ""),
Name = machine.Name + "_" + diskno + (!String.IsNullOrWhiteSpace(remark) ? " " + remark : ""),
Offset = offset,
Size = -1,
SHA1 = Utilities.CleanHashData(hash, Constants.SHA1Length),
};
@@ -362,18 +367,19 @@ namespace SabreTools.Library.DatFiles
/// <summary>
/// Read megarom information
/// </summary>
/// <param name="reader">XmlReader representing a part block</param>
/// <param name="reader">XmlReader representing a megarom block</param>
/// <param name="machine">Machine information to pass to contained items</param>
/// <param name="diskno">Disk number to use when outputting to other DAT formats</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="sysid">System ID for the DAT</param>
/// <param name="srcid">Source ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
/// <param name="clean">True if game names are sanitized, false otherwise (default)</param>
/// <param name="remUnicode">True if we should remove non-ASCII characters from output, false otherwise (default)</param>
/// TODO: Make sure that this outputs proper values since only SHA-1 is included
private bool ReadMegaRom(
XmlReader reader,
Machine machine,
int diskno,
// Standard Dat parsing
string filename,
@@ -422,8 +428,9 @@ namespace SabreTools.Library.DatFiles
// Create and add the new rom
Rom rom = new Rom
{
Name = machine.Name + (!String.IsNullOrWhiteSpace(remark) ? " " + remark : ""),
Name = machine.Name + "_" + diskno + (!String.IsNullOrWhiteSpace(remark) ? " " + remark : ""),
Offset = offset,
Size = -1,
SHA1 = Utilities.CleanHashData(hash, Constants.SHA1Length),
};
@@ -436,18 +443,19 @@ namespace SabreTools.Library.DatFiles
/// <summary>
/// Read sccpluscart information
/// </summary>
/// <param name="reader">XmlReader representing a part block</param>
/// <param name="reader">XmlReader representing a sccpluscart block</param>
/// <param name="machine">Machine information to pass to contained items</param>
/// <param name="diskno">Disk number to use when outputting to other DAT formats</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="sysid">System ID for the DAT</param>
/// <param name="srcid">Source ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
/// <param name="clean">True if game names are sanitized, false otherwise (default)</param>
/// <param name="remUnicode">True if we should remove non-ASCII characters from output, false otherwise (default)</param>
/// TODO: Make sure that this outputs proper values since only SHA-1 is included
private bool ReadSccPlusCart(
XmlReader reader,
Machine machine,
int diskno,
// Standard Dat parsing
string filename,
@@ -493,7 +501,8 @@ namespace SabreTools.Library.DatFiles
// Create and add the new rom
Rom rom = new Rom
{
Name = machine.Name + (!String.IsNullOrWhiteSpace(remark) ? " " + remark : ""),
Name = machine.Name + "_" + diskno + (!String.IsNullOrWhiteSpace(remark) ? " " + remark : ""),
Size = -1,
SHA1 = Utilities.CleanHashData(hash, Constants.SHA1Length),
};