[DatFile] Use DatHeader and DatStats

This commit is contained in:
Matt Nadareski
2017-10-06 15:49:32 -07:00
parent ada498b415
commit a20be6fc43
6 changed files with 229 additions and 479 deletions

View File

@@ -28,146 +28,13 @@ namespace SabreTools.Library.Dats
#region Statistics
/// <summary>
/// Add to the internal statistics given a DatItem
/// </summary>
/// <param name="item">Item to add info from</param>
private void AddItemStatistics(DatItem item)
{
// No matter what the item is, we increate the count
lock (_statslock)
{
_count += 1;
// Now we do different things for each item type
switch (item.Type)
{
case ItemType.Archive:
break;
case ItemType.BiosSet:
break;
case ItemType.Disk:
_diskCount += 1;
if (((Disk)item).ItemStatus != ItemStatus.Nodump)
{
_md5Count += (String.IsNullOrEmpty(((Disk)item).MD5) ? 0 : 1);
_sha1Count += (String.IsNullOrEmpty(((Disk)item).SHA1) ? 0 : 1);
_sha256Count += (String.IsNullOrEmpty(((Disk)item).SHA256) ? 0 : 1);
_sha384Count += (String.IsNullOrEmpty(((Disk)item).SHA384) ? 0 : 1);
_sha512Count += (String.IsNullOrEmpty(((Disk)item).SHA512) ? 0 : 1);
}
_baddumpCount += (((Disk)item).ItemStatus == ItemStatus.BadDump ? 1 : 0);
_nodumpCount += (((Disk)item).ItemStatus == ItemStatus.Nodump ? 1 : 0);
break;
case ItemType.Release:
break;
case ItemType.Rom:
_romCount += 1;
if (((Rom)item).ItemStatus != ItemStatus.Nodump)
{
_totalSize += ((Rom)item).Size;
_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);
_sha384Count += (String.IsNullOrEmpty(((Rom)item).SHA384) ? 0 : 1);
_sha512Count += (String.IsNullOrEmpty(((Rom)item).SHA512) ? 0 : 1);
}
_baddumpCount += (((Rom)item).ItemStatus == ItemStatus.BadDump ? 1 : 0);
_nodumpCount += (((Rom)item).ItemStatus == ItemStatus.Nodump ? 1 : 0);
break;
case ItemType.Sample:
break;
}
}
}
/// <summary>
/// Remove from the internal statistics given a DatItem
/// </summary>
/// <param name="item">Item to remove info for</param>
private void RemoveItemStatistics(DatItem item)
{
// No matter what the item is, we increate the count
lock (_statslock)
{
_count -= 1;
// Now we do different things for each item type
switch (item.Type)
{
case ItemType.Archive:
break;
case ItemType.BiosSet:
break;
case ItemType.Disk:
_diskCount -= 1;
if (((Disk)item).ItemStatus != ItemStatus.Nodump)
{
_md5Count -= (String.IsNullOrEmpty(((Disk)item).MD5) ? 0 : 1);
_sha1Count -= (String.IsNullOrEmpty(((Disk)item).SHA1) ? 0 : 1);
_sha256Count -= (String.IsNullOrEmpty(((Disk)item).SHA256) ? 0 : 1);
_sha384Count -= (String.IsNullOrEmpty(((Disk)item).SHA384) ? 0 : 1);
_sha512Count -= (String.IsNullOrEmpty(((Disk)item).SHA512) ? 0 : 1);
}
_baddumpCount -= (((Disk)item).ItemStatus == ItemStatus.BadDump ? 1 : 0);
_nodumpCount -= (((Disk)item).ItemStatus == ItemStatus.Nodump ? 1 : 0);
break;
case ItemType.Release:
break;
case ItemType.Rom:
_romCount -= 1;
if (((Rom)item).ItemStatus != ItemStatus.Nodump)
{
_totalSize -= ((Rom)item).Size;
_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);
_sha384Count -= (String.IsNullOrEmpty(((Rom)item).SHA384) ? 0 : 1);
_sha512Count -= (String.IsNullOrEmpty(((Rom)item).SHA512) ? 0 : 1);
}
_baddumpCount -= (((Rom)item).ItemStatus == ItemStatus.BadDump ? 1 : 0);
_nodumpCount -= (((Rom)item).ItemStatus == ItemStatus.Nodump ? 1 : 0);
break;
case ItemType.Sample:
break;
}
}
}
/// <summary>
/// Reset all statistics
/// </summary>
private void ResetStatistics()
{
_count = 0;
_romCount = 0;
_diskCount = 0;
_totalSize = 0;
_crcCount = 0;
_md5Count = 0;
_sha1Count = 0;
_sha256Count = 0;
_sha384Count = 0;
_sha512Count = 0;
_baddumpCount = 0;
_nodumpCount = 0;
}
/// <summary>
/// Recalculate the statistics for the Dat
/// </summary>
public void RecalculateStats()
{
// Wipe out any stats already there
ResetStatistics();
_datStats.Reset();
// If we have a blank Dat in any way, return
if (this == null || Count == 0)
@@ -182,7 +49,7 @@ namespace SabreTools.Library.Dats
List<DatItem> items = this[key];
foreach(DatItem item in items)
{
AddItemStatistics(item);
_datStats.AddItem(item);
}
});
}
@@ -206,32 +73,32 @@ namespace SabreTools.Library.Dats
}
BucketBy(SortedBy.Game, DedupeType.None, norename: true);
if (_totalSize < 0)
if (TotalSize < 0)
{
_totalSize = Int64.MaxValue + _totalSize;
TotalSize = Int64.MaxValue + TotalSize;
}
// Log the results to screen
string results = @"For '" + _fileName + @"':
string results = @"For '" + FileName + @"':
--------------------------------------------------
Uncompressed size: " + Style.GetBytesReadable(_totalSize) + @"
Uncompressed size: " + Style.GetBytesReadable(TotalSize) + @"
Games found: " + (game == -1 ? Keys.Count() : game) + @"
Roms found: " + _romCount + @"
Disks found: " + _diskCount + @"
Roms with CRC: " + _crcCount + @"
Roms with MD5: " + _md5Count + @"
Roms with SHA-1: " + _sha1Count + @"
Roms with SHA-256: " + _sha256Count + @"
Roms with SHA-384: " + _sha384Count + @"
Roms with SHA-512: " + _sha512Count + "\n";
Roms found: " + RomCount + @"
Disks found: " + DiskCount + @"
Roms with CRC: " + CRCCount + @"
Roms with MD5: " + MD5Count + @"
Roms with SHA-1: " + SHA1Count + @"
Roms with SHA-256: " + SHA256Count + @"
Roms with SHA-384: " + SHA384Count + @"
Roms with SHA-512: " + SHA512Count + "\n";
if (baddumpCol)
{
results += " Roms with BadDump status: " + _baddumpCount + "\n";
results += " Roms with BadDump status: " + BaddumpCount + "\n";
}
if (nodumpCol)
{
results += " Roms with Nodump status: " + _nodumpCount + "\n";
results += " Roms with Nodump status: " + NodumpCount + "\n";
}
// For spacing between DATs
@@ -243,25 +110,25 @@ namespace SabreTools.Library.Dats
string line = "";
if (outputs.ContainsKey(StatDatFormat.None))
{
line = @"'" + _fileName + @"':
line = @"'" + FileName + @"':
--------------------------------------------------
Uncompressed size: " + Style.GetBytesReadable(_totalSize) + @"
Uncompressed size: " + Style.GetBytesReadable(TotalSize) + @"
Games found: " + (game == -1 ? Keys.Count() : game) + @"
Roms found: " + _romCount + @"
Disks found: " + _diskCount + @"
Roms with CRC: " + _crcCount + @"
Roms with SHA-1: " + _sha1Count + @"
Roms with SHA-256: " + _sha256Count + @"
Roms with SHA-384: " + _sha384Count + @"
Roms with SHA-512: " + _sha512Count + "\n";
Roms found: " + RomCount + @"
Disks found: " + DiskCount + @"
Roms with CRC: " + CRCCount + @"
Roms with SHA-1: " + SHA1Count + @"
Roms with SHA-256: " + SHA256Count + @"
Roms with SHA-384: " + SHA384Count + @"
Roms with SHA-512: " + SHA512Count + "\n";
if (baddumpCol)
{
line += " Roms with BadDump status: " + _baddumpCount + "\n";
line += " Roms with BadDump status: " + BaddumpCount + "\n";
}
if (nodumpCol)
{
line += " Roms with Nodump status: " + _nodumpCount + "\n";
line += " Roms with Nodump status: " + NodumpCount + "\n";
}
// For spacing between DATs
@@ -271,25 +138,25 @@ namespace SabreTools.Library.Dats
}
if (outputs.ContainsKey(StatDatFormat.CSV))
{
line = "\"" + _fileName + "\","
+ "\"" + _totalSize + "\","
line = "\"" + FileName + "\","
+ "\"" + TotalSize + "\","
+ "\"" + (game == -1 ? Keys.Count() : game) + "\","
+ "\"" + _romCount + "\","
+ "\"" + _diskCount + "\","
+ "\"" + _crcCount + "\","
+ "\"" + _md5Count + "\","
+ "\"" + _sha1Count + "\","
+ "\"" + _sha256Count + "\","
+ "\"" + _sha384Count + "\","
+ "\"" + _sha512Count + "\"";
+ "\"" + RomCount + "\","
+ "\"" + DiskCount + "\","
+ "\"" + CRCCount + "\","
+ "\"" + MD5Count + "\","
+ "\"" + SHA1Count + "\","
+ "\"" + SHA256Count + "\","
+ "\"" + SHA384Count + "\","
+ "\"" + SHA512Count + "\"";
if (baddumpCol)
{
line += ",\"" + _baddumpCount + "\"";
line += ",\"" + BaddumpCount + "\"";
}
if (nodumpCol)
{
line += ",\"" + _nodumpCount + "\"";
line += ",\"" + NodumpCount + "\"";
}
line += "\n";
@@ -297,25 +164,25 @@ namespace SabreTools.Library.Dats
}
if (outputs.ContainsKey(StatDatFormat.HTML))
{
line = "\t\t\t<tr" + (_fileName.StartsWith("DIR: ")
? " class=\"dir\"><td>" + HttpUtility.HtmlEncode(_fileName.Remove(0, 5))
: "><td>" + HttpUtility.HtmlEncode(_fileName)) + "</td>"
+ "<td align=\"right\">" + Style.GetBytesReadable(_totalSize) + "</td>"
line = "\t\t\t<tr" + (FileName.StartsWith("DIR: ")
? " class=\"dir\"><td>" + HttpUtility.HtmlEncode(FileName.Remove(0, 5))
: "><td>" + HttpUtility.HtmlEncode(FileName)) + "</td>"
+ "<td align=\"right\">" + Style.GetBytesReadable(TotalSize) + "</td>"
+ "<td align=\"right\">" + (game == -1 ? Keys.Count() : game) + "</td>"
+ "<td align=\"right\">" + _romCount + "</td>"
+ "<td align=\"right\">" + _diskCount + "</td>"
+ "<td align=\"right\">" + _crcCount + "</td>"
+ "<td align=\"right\">" + _md5Count + "</td>"
+ "<td align=\"right\">" + _sha1Count + "</td>"
+ "<td align=\"right\">" + _sha256Count + "</td>";
+ "<td align=\"right\">" + RomCount + "</td>"
+ "<td align=\"right\">" + DiskCount + "</td>"
+ "<td align=\"right\">" + CRCCount + "</td>"
+ "<td align=\"right\">" + MD5Count + "</td>"
+ "<td align=\"right\">" + SHA1Count + "</td>"
+ "<td align=\"right\">" + SHA256Count + "</td>";
if (baddumpCol)
{
line += "<td align=\"right\">" + _baddumpCount + "</td>";
line += "<td align=\"right\">" + BaddumpCount + "</td>";
}
if (nodumpCol)
{
line += "<td align=\"right\">" + _nodumpCount + "</td>";
line += "<td align=\"right\">" + NodumpCount + "</td>";
}
line += "</tr>\n";
@@ -323,25 +190,25 @@ namespace SabreTools.Library.Dats
}
if (outputs.ContainsKey(StatDatFormat.TSV))
{
line = "\"" + _fileName + "\"\t"
+ "\"" + _totalSize + "\"\t"
line = "\"" + FileName + "\"\t"
+ "\"" + TotalSize + "\"\t"
+ "\"" + (game == -1 ? Keys.Count() : game) + "\"\t"
+ "\"" + _romCount + "\"\t"
+ "\"" + _diskCount + "\"\t"
+ "\"" + _crcCount + "\"\t"
+ "\"" + _md5Count + "\"\t"
+ "\"" + _sha1Count + "\"\t"
+ "\"" + _sha256Count + "\"\t"
+ "\"" + _sha384Count + "\"\t"
+ "\"" + _sha512Count + "\"";
+ "\"" + RomCount + "\"\t"
+ "\"" + DiskCount + "\"\t"
+ "\"" + CRCCount + "\"\t"
+ "\"" + MD5Count + "\"\t"
+ "\"" + SHA1Count + "\"\t"
+ "\"" + SHA256Count + "\"\t"
+ "\"" + SHA384Count + "\"\t"
+ "\"" + SHA512Count + "\"";
if (baddumpCol)
{
line += "\t\"" + _baddumpCount + "\"";
line += "\t\"" + BaddumpCount + "\"";
}
if (nodumpCol)
{
line += "\t\"" + _nodumpCount + "\"";
line += "\t\"" + NodumpCount + "\"";
}
line += "\n";
@@ -417,35 +284,15 @@ namespace SabreTools.Library.Dats
OutputStatsWriteHeader(outputs, statDatFormat, baddumpCol, nodumpCol);
// Init all total variables
long totalSize = 0;
DatStats totalStats = new DatStats();
long totalGame = 0;
long totalRom = 0;
long totalDisk = 0;
long totalCRC = 0;
long totalMD5 = 0;
long totalSHA1 = 0;
long totalSHA256 = 0;
long totalSHA384 = 0;
long totalSHA512 = 0;
long totalBaddump = 0;
long totalNodump = 0;
// Init directory-level variables
string lastdir = null;
string basepath = null;
long dirSize = 0;
DatStats dirStats = new DatStats();
long dirGame = 0;
long dirRom = 0;
long dirDisk = 0;
long dirCRC = 0;
long dirMD5 = 0;
long dirSHA1 = 0;
long dirSHA256 = 0;
long dirSHA384 = 0;
long dirSHA512 = 0;
long dirBaddump = 0;
long dirNodump = 0;
// Now process each of the input files
foreach (Tuple<string, string> filename in newinputs)
{
@@ -461,19 +308,10 @@ namespace SabreTools.Library.Dats
DatFile lastdirdat = new DatFile
{
_fileName = "DIR: " + HttpUtility.HtmlEncode(lastdir.Remove(0, basepath.Length + (basepath.Length == 0 ? 0 : 1))),
_totalSize = dirSize,
_romCount = dirRom,
_diskCount = dirDisk,
_crcCount = dirCRC,
_md5Count = dirMD5,
_sha1Count = dirSHA1,
_sha256Count = dirSHA256,
_sha384Count = dirSHA384,
_sha512Count = dirSHA512,
_baddumpCount = dirBaddump,
_nodumpCount = dirNodump,
FileName = "DIR: " + HttpUtility.HtmlEncode(lastdir.Remove(0, basepath.Length + (basepath.Length == 0 ? 0 : 1))),
_datStats = dirStats,
};
lastdirdat.OutputStats(outputs, statDatFormat,
game: dirGame, baddumpCol: baddumpCol, nodumpCol: nodumpCol);
@@ -484,18 +322,8 @@ namespace SabreTools.Library.Dats
OutputStatsWriteMidHeader(outputs, statDatFormat, baddumpCol, nodumpCol);
// Reset the directory stats
dirSize = 0;
dirStats.Reset();
dirGame = 0;
dirRom = 0;
dirDisk = 0;
dirCRC = 0;
dirMD5 = 0;
dirSHA1 = 0;
dirSHA256 = 0;
dirSHA384 = 0;
dirSHA512 = 0;
dirBaddump = 0;
dirNodump = 0;
}
Globals.Logger.Verbose("Beginning stat collection for '{0}'", false, filename.Item1);
@@ -513,32 +341,12 @@ namespace SabreTools.Library.Dats
}
// Add single DAT stats to dir
dirSize += datdata.TotalSize;
dirStats.AddStats(datdata._datStats);
dirGame += datdata.Keys.Count();
dirRom += datdata.RomCount;
dirDisk += datdata.DiskCount;
dirCRC += datdata.CRCCount;
dirMD5 += datdata.MD5Count;
dirSHA1 += datdata.SHA1Count;
dirSHA256 += datdata.SHA256Count;
dirSHA384 += datdata.SHA384Count;
dirSHA512 += datdata.SHA512Count;
dirBaddump += datdata.BaddumpCount;
dirNodump += datdata.NodumpCount;
// Add single DAT stats to totals
totalSize += datdata.TotalSize;
totalStats.AddStats(datdata._datStats);
totalGame += datdata.Keys.Count();
totalRom += datdata.RomCount;
totalDisk += datdata.DiskCount;
totalCRC += datdata.CRCCount;
totalMD5 += datdata.MD5Count;
totalSHA1 += datdata.SHA1Count;
totalSHA256 += datdata.SHA256Count;
totalSHA384 += datdata.SHA384Count;
totalSHA512 += datdata.SHA512Count;
totalBaddump += datdata.BaddumpCount;
totalNodump += datdata.NodumpCount;
// Make sure to assign the new directory
lastdir = thisdir;
@@ -551,19 +359,10 @@ namespace SabreTools.Library.Dats
{
DatFile dirdat = new DatFile
{
_fileName = "DIR: " + HttpUtility.HtmlEncode(lastdir.Remove(0, basepath.Length + (basepath.Length == 0 ? 0 : 1))),
_totalSize = dirSize,
_romCount = dirRom,
_diskCount = dirDisk,
_crcCount = dirCRC,
_md5Count = dirMD5,
_sha1Count = dirSHA1,
_sha256Count = dirSHA256,
_sha384Count = dirSHA384,
_sha512Count = dirSHA512,
_baddumpCount = dirBaddump,
_nodumpCount = dirNodump,
FileName = "DIR: " + HttpUtility.HtmlEncode(lastdir.Remove(0, basepath.Length + (basepath.Length == 0 ? 0 : 1))),
_datStats = dirStats,
};
dirdat.OutputStats(outputs, statDatFormat,
game: dirGame, baddumpCol: baddumpCol, nodumpCol: nodumpCol);
}
@@ -575,34 +374,16 @@ namespace SabreTools.Library.Dats
OutputStatsWriteMidHeader(outputs, statDatFormat, baddumpCol, nodumpCol);
// Reset the directory stats
dirSize = 0;
dirStats.Reset();
dirGame = 0;
dirRom = 0;
dirDisk = 0;
dirCRC = 0;
dirMD5 = 0;
dirSHA1 = 0;
dirSHA256 = 0;
dirSHA384 = 0;
dirSHA512 = 0;
dirNodump = 0;
// Output total DAT stats
DatFile totaldata = new DatFile
{
_fileName = "DIR: All DATs",
_totalSize = totalSize,
_romCount = totalRom,
_diskCount = totalDisk,
_crcCount = totalCRC,
_md5Count = totalMD5,
_sha1Count = totalSHA1,
_sha256Count = totalSHA256,
_sha384Count = totalSHA384,
_sha512Count = totalSHA512,
_baddumpCount = totalBaddump,
_nodumpCount = totalNodump,
FileName = "DIR: All DATs",
_datStats = totalStats,
};
totaldata.OutputStats(outputs, statDatFormat,
game: totalGame, baddumpCol: baddumpCol, nodumpCol: nodumpCol);