[ALL] Add SHA-256 as a future option for pretty much everything

This commit is contained in:
Matt Nadareski
2017-02-23 14:23:41 -08:00
parent 10ac547646
commit 2c450b34e7
17 changed files with 505 additions and 55 deletions

View File

@@ -88,7 +88,7 @@ namespace SabreTools.Helper.Dats
{
ext = ext.Substring(1);
}
if (ext != "dat" && ext != "md5" && ext != "sfv" && ext != "sha1" && ext != "txt" && ext != "xml")
if (ext != "dat" && ext != "md5" && ext != "sfv" && ext != "sha1" && ext != "sha256" && ext != "txt" && ext != "xml")
{
return;
}
@@ -124,6 +124,9 @@ namespace SabreTools.Helper.Dats
case DatFormat.RedumpSHA1:
ParseRedumpSHA1(filename, sysid, srcid, filter, trim, single, root, logger, clean);
break;
case DatFormat.RedumpSHA256:
ParseRedumpSHA256(filename, sysid, srcid, filter, trim, single, root, logger, clean);
break;
case DatFormat.RomCenter:
ParseRC(filename, sysid, srcid, filter, trim, single, root, logger, clean);
break;
@@ -459,6 +462,27 @@ namespace SabreTools.Helper.Dats
i++;
((Rom)item).CRC = gc[i].Replace("\"", "").ToLowerInvariant();
}
// Get the MD5 from the next part
else if (gc[i] == "md5")
{
i++;
((Rom)item).MD5 = gc[i].Replace("\"", "").ToLowerInvariant();
}
// Get the SHA1 from the next part
else if (gc[i] == "sha1")
{
i++;
((Rom)item).SHA1 = gc[i].Replace("\"", "").ToLowerInvariant();
}
// Get the SHA256 from the next part
else if (gc[i] == "sha256")
{
i++;
((Rom)item).SHA256 = gc[i].Replace("\"", "").ToLowerInvariant();
}
}
// Now process and add the rom
@@ -575,6 +599,20 @@ namespace SabreTools.Helper.Dats
((Disk)item).SHA1 = quoteless.ToLowerInvariant();
}
break;
case "sha256":
if (item.Type == ItemType.Rom)
{
i++;
quoteless = gc[i].Replace("\"", "");
((Rom)item).SHA256 = quoteless.ToLowerInvariant();
}
else if (item.Type == ItemType.Disk)
{
i++;
quoteless = gc[i].Replace("\"", "");
((Disk)item).SHA256 = quoteless.ToLowerInvariant();
}
break;
case "status":
case "flags":
i++;
@@ -1712,6 +1750,7 @@ namespace SabreTools.Helper.Dats
Name = subreader.GetAttribute("name"),
MD5 = subreader.GetAttribute("md5")?.ToLowerInvariant(),
SHA1 = subreader.GetAttribute("sha1")?.ToLowerInvariant(),
SHA256 = subreader.GetAttribute("sha256")?.ToLowerInvariant(),
MergeTag = merge,
ItemStatus = its,
@@ -1740,6 +1779,7 @@ namespace SabreTools.Helper.Dats
CRC = subreader.GetAttribute("crc"),
MD5 = subreader.GetAttribute("md5")?.ToLowerInvariant(),
SHA1 = subreader.GetAttribute("sha1")?.ToLowerInvariant(),
SHA256 = subreader.GetAttribute("sha256")?.ToLowerInvariant(),
ItemStatus = its,
MergeTag = merge,
Date = date,
@@ -1905,6 +1945,7 @@ namespace SabreTools.Helper.Dats
Name = xtr.GetAttribute("name"),
MD5 = xtr.GetAttribute("md5")?.ToLowerInvariant(),
SHA1 = xtr.GetAttribute("sha1")?.ToLowerInvariant(),
SHA256 = xtr.GetAttribute("sha256")?.ToLowerInvariant(),
ItemStatus = its,
Machine = dir,
@@ -1923,6 +1964,7 @@ namespace SabreTools.Helper.Dats
CRC = xtr.GetAttribute("crc")?.ToLowerInvariant(),
MD5 = xtr.GetAttribute("md5")?.ToLowerInvariant(),
SHA1 = xtr.GetAttribute("sha1")?.ToLowerInvariant(),
SHA256 = xtr.GetAttribute("sha256")?.ToLowerInvariant(),
ItemStatus = its,
Date = date,
@@ -2143,6 +2185,68 @@ namespace SabreTools.Helper.Dats
sr.Dispose();
}
/// <summary>
/// Parse a Redump SHA-256 and return all found games and roms within
/// </summary>
/// <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="filter">Filter object for passing to the DatItem level</param>
/// <param name="trim">True if we are supposed to trim names to NTFS length, false otherwise</param>
/// <param name="single">True if all games should be replaced by '!', false otherwise</param>
/// <param name="root">String representing root directory to compare against for length calculation</param>
/// <param name="logger">Logger object for console and/or file output</param>
/// <param name="clean">True if game names are sanitized, false otherwise (default)</param>
private void ParseRedumpSHA256(
// Standard Dat parsing
string filename,
int sysid,
int srcid,
// Rom filtering
Filter filter,
// Rom renaming
bool trim,
bool single,
string root,
// Miscellaneous
Logger logger,
bool clean)
{
// Open a file reader
Encoding enc = Style.GetEncoding(filename);
StreamReader sr = new StreamReader(File.OpenRead(filename), enc);
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
Rom rom = new Rom
{
Name = line.Split(' ')[1].Replace("*", String.Empty),
Size = -1,
SHA256 = line.Split(' ')[0],
ItemStatus = ItemStatus.None,
Machine = new Machine
{
Name = Path.GetFileNameWithoutExtension(filename),
},
SystemID = sysid,
SourceID = srcid,
};
// Now process and add the rom
string key = "";
ParseAddHelper(rom, filter, trim, single, root, clean, logger, out key);
}
sr.Dispose();
}
/// <summary>
/// Parse a RomCenter DAT and return all found games and roms within
/// </summary>
@@ -2353,17 +2457,20 @@ namespace SabreTools.Helper.Dats
itemRom.CRC = Style.CleanHashData(itemRom.CRC, Constants.CRCLength);
itemRom.MD5 = Style.CleanHashData(itemRom.MD5, Constants.MD5Length);
itemRom.SHA1 = Style.CleanHashData(itemRom.SHA1, Constants.SHA1Length);
itemRom.SHA256 = Style.CleanHashData(itemRom.SHA256, Constants.SHA256Length);
// 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)
&& ((itemRom.CRC == Constants.CRCZero || String.IsNullOrEmpty(itemRom.CRC))
|| itemRom.MD5 == Constants.MD5Zero
|| itemRom.SHA1 == Constants.SHA1Zero))
|| itemRom.SHA1 == Constants.SHA1Zero
|| itemRom.SHA256 == Constants.SHA256Zero))
{
itemRom.Size = Constants.SizeZero;
itemRom.CRC = Constants.CRCZero;
itemRom.MD5 = Constants.MD5Zero;
itemRom.SHA1 = Constants.SHA1Zero;
itemRom.SHA256 = Constants.SHA256Zero;
}
// If the file has no size and it's not the above case, skip and log
else if (itemRom.ItemStatus != ItemStatus.Nodump && (itemRom.Size == 0 || itemRom.Size == -1))
@@ -2376,7 +2483,8 @@ namespace SabreTools.Helper.Dats
&& itemRom.Size > 0
&& String.IsNullOrEmpty(itemRom.CRC)
&& String.IsNullOrEmpty(itemRom.MD5)
&& String.IsNullOrEmpty(itemRom.SHA1))
&& String.IsNullOrEmpty(itemRom.SHA1)
&& String.IsNullOrEmpty(itemRom.SHA256))
{
logger.Verbose("Incomplete entry for \"" + itemRom.Name + "\" will be output as nodump");
itemRom.ItemStatus = ItemStatus.Nodump;
@@ -2391,11 +2499,13 @@ namespace SabreTools.Helper.Dats
// Sanitize the hashes from null, hex sizes, and "true blank" strings
itemDisk.MD5 = Style.CleanHashData(itemDisk.MD5, Constants.MD5Length);
itemDisk.SHA1 = Style.CleanHashData(itemDisk.SHA1, Constants.SHA1Length);
itemDisk.SHA256 = Style.CleanHashData(itemRom.SHA256, Constants.SHA256Length);
// If the file has aboslutely no hashes, skip and log
if (itemDisk.ItemStatus != ItemStatus.Nodump
&& String.IsNullOrEmpty(itemDisk.MD5)
&& String.IsNullOrEmpty(itemDisk.SHA1))
&& String.IsNullOrEmpty(itemDisk.SHA1)
&& String.IsNullOrEmpty(itemDisk.SHA256))
{
logger.Verbose("Incomplete entry for \"" + itemDisk.Name + "\" will be output as nodump");
itemDisk.ItemStatus = ItemStatus.Nodump;
@@ -2443,6 +2553,7 @@ namespace SabreTools.Helper.Dats
TotalSize += 0;
MD5Count += (String.IsNullOrEmpty(((Disk)item).MD5) ? 0 : 1);
SHA1Count += (String.IsNullOrEmpty(((Disk)item).SHA1) ? 0 : 1);
SHA256Count += (String.IsNullOrEmpty((Disk)item).SHA256) ? 0 : 1);
BaddumpCount += (((Disk)item).ItemStatus == ItemStatus.BadDump ? 1 : 0);
NodumpCount += (((Disk)item).ItemStatus == ItemStatus.Nodump ? 1 : 0);
break;
@@ -2455,6 +2566,7 @@ namespace SabreTools.Helper.Dats
CRCCount += (String.IsNullOrEmpty(((Rom)item).CRC) ? 0 : 1);
MD5Count += (String.IsNullOrEmpty(((Rom)item).MD5) ? 0 : 1);
SHA1Count += (String.IsNullOrEmpty(((Rom)item).SHA1) ? 0 : 1);
SHA256Count += (String.IsNullOrEmpty((Rom)item).SHA256) ? 0 : 1);
BaddumpCount += (((Rom)item).ItemStatus == ItemStatus.BadDump ? 1 : 0);
NodumpCount += (((Rom)item).ItemStatus == ItemStatus.Nodump ? 1 : 0);
break;