From 0ed44d0b458a8972576cfaf9de2bc167b22a75aa Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 22 Sep 2016 17:00:34 -0700 Subject: [PATCH] [Stats, DatFile] Merge Stats into DatFile Since statistics are something that is supposed to be inherent to Dats, this makes the most sense being inside of DatFile --- SabreTools.Helper/Objects/Dat/DatFile.cs | 157 ++++++++++++++++++++- SabreTools.Helper/Objects/SimpleSort.cs | 2 +- SabreTools.Helper/Objects/Stats.cs | 150 -------------------- SabreTools.Helper/SabreTools.Helper.csproj | 1 - SabreTools/Partials/SabreTools_Inits.cs | 20 +-- 5 files changed, 158 insertions(+), 172 deletions(-) delete mode 100644 SabreTools.Helper/Objects/Stats.cs diff --git a/SabreTools.Helper/Objects/Dat/DatFile.cs b/SabreTools.Helper/Objects/Dat/DatFile.cs index c1b37ab2..8f5584ab 100644 --- a/SabreTools.Helper/Objects/Dat/DatFile.cs +++ b/SabreTools.Helper/Objects/Dat/DatFile.cs @@ -2370,6 +2370,161 @@ namespace SabreTools.Helper #endregion + #region Statistics + + /// + /// Recalculate the statistics for the Dat + /// + public void RecalculateStats() + { + // Wipe out any stats already there + RomCount = 0; + DiskCount = 0; + TotalSize = 0; + CRCCount = 0; + MD5Count = 0; + SHA1Count = 0; + NodumpCount = 0; + + // If we have a blank Dat in any way, return + if (this == null || Files == null || Files.Count == 0) + { + return; + } + + // Loop through and add + foreach (List roms in Files.Values) + { + foreach (Rom rom in roms) + { + RomCount += (rom.Type == ItemType.Rom ? 1 : 0); + DiskCount += (rom.Type == ItemType.Disk ? 1 : 0); + TotalSize += (rom.ItemStatus == ItemStatus.Nodump ? 0 : rom.Size); + CRCCount += (String.IsNullOrEmpty(rom.CRC) ? 0 : 1); + MD5Count += (String.IsNullOrEmpty(rom.MD5) ? 0 : 1); + SHA1Count += (String.IsNullOrEmpty(rom.SHA1) ? 0 : 1); + } + } + } + + /// + /// Output the stats for the Dat in a human-readable format + /// + /// Logger object for file and console writing + /// True if numbers should be recalculated for the DAT, false otherwise (default) + /// Number of games to use, -1 means recalculate games (default) + public void OutputStats(Logger logger, bool recalculate = false, long game = -1) + { + // If we're supposed to recalculate the statistics, do so + if (recalculate) + { + RecalculateStats(); + } + + SortedDictionary> newroms = DatFile.BucketByGame(Files, false, true, logger, false); + if (TotalSize < 0) + { + TotalSize = Int64.MaxValue + TotalSize; + } + logger.User(" Uncompressed size: " + Style.GetBytesReadable(TotalSize) + @" + Games found: " + (game == -1 ? newroms.Count : game) + @" + Roms found: " + RomCount + @" + Disks found: " + DiskCount + @" + Roms with CRC: " + CRCCount + @" + Roms with MD5: " + MD5Count + @" + Roms with SHA-1: " + SHA1Count + @" + Roms with Nodump status: " + NodumpCount + @" +"); + } + + /// + /// Output the stats for a list of input dats as files in a human-readable format + /// + /// List of input files and folders + /// True if single DAT stats are output, false otherwise + /// Logger object for file and console output + public static void OutputStats(List inputs, bool single, Logger logger) + { + // Make sure we have all files + List newinputs = new List(); + foreach (string input in inputs) + { + if (File.Exists(input)) + { + newinputs.Add(input); + } + if (Directory.Exists(input)) + { + foreach (string file in Directory.GetFiles(input, "*", SearchOption.AllDirectories)) + { + newinputs.Add(file); + } + } + } + + // Init all total variables + long totalSize = 0; + long totalGame = 0; + long totalRom = 0; + long totalDisk = 0; + long totalCRC = 0; + long totalMD5 = 0; + long totalSHA1 = 0; + long totalNodump = 0; + + /// Now process each of the input files + foreach (string filename in newinputs) + { + logger.Log("Beginning stat collection for '" + filename + "'"); + List games = new List(); + DatFile datdata = new DatFile(); + Parse(filename, 0, 0, ref datdata, logger); + SortedDictionary> newroms = BucketByGame(datdata.Files, false, true, logger, false); + + // Output single DAT stats (if asked) + if (single) + { + logger.User(@"\nFor file '" + filename + @"': +--------------------------------------------------"); + datdata.OutputStats(logger); + } + else + { + logger.User("Adding stats for file '" + filename + "'\n"); + } + + // Add single DAT stats to totals + totalSize += datdata.TotalSize; + totalGame += newroms.Count; + totalRom += datdata.RomCount; + totalDisk += datdata.DiskCount; + totalCRC += datdata.CRCCount; + totalMD5 += datdata.MD5Count; + totalSHA1 += datdata.SHA1Count; + totalNodump += datdata.NodumpCount; + } + + // Output total DAT stats + if (!single) { logger.User(""); } + DatFile totaldata = new DatFile + { + TotalSize = totalSize, + RomCount = totalRom, + DiskCount = totalDisk, + CRCCount = totalCRC, + MD5Count = totalMD5, + SHA1Count = totalSHA1, + NodumpCount = totalNodump, + }; + logger.User(@"For ALL DATs found +--------------------------------------------------"); + totaldata.OutputStats(logger, game: totalGame); + logger.User(@" +Please check the log folder if the stats scrolled offscreen"); + } + + #endregion + #region Bucketing /// @@ -3110,7 +3265,7 @@ namespace SabreTools.Helper // Output initial statistics, for kicks if (stats) { - Stats.OutputStats(datdata, logger, (datdata.RomCount + datdata.DiskCount == 0)); + datdata.OutputStats(logger, (datdata.RomCount + datdata.DiskCount == 0)); } // Bucket roms by game name and optionally dedupe diff --git a/SabreTools.Helper/Objects/SimpleSort.cs b/SabreTools.Helper/Objects/SimpleSort.cs index 8decb954..8728176b 100644 --- a/SabreTools.Helper/Objects/SimpleSort.cs +++ b/SabreTools.Helper/Objects/SimpleSort.cs @@ -264,7 +264,7 @@ namespace SabreTools.Helper _logger.ClearBeneath(Constants.HeaderHeight); Console.SetCursorPosition(0, Constants.HeaderHeight + 1); _logger.User("Stats of the matched ROMs:"); - Stats.OutputStats(_matched, _logger, true); + _matched.OutputStats(_logger, true); // Now output the fixdat based on the original input if asked if (_updateDat) diff --git a/SabreTools.Helper/Objects/Stats.cs b/SabreTools.Helper/Objects/Stats.cs deleted file mode 100644 index 409f385e..00000000 --- a/SabreTools.Helper/Objects/Stats.cs +++ /dev/null @@ -1,150 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace SabreTools.Helper -{ - /// - /// Get statistics on one or more DAT files - /// - public class Stats - { - // Private instance variables - private List _inputs; - private bool _single; - private Logger _logger; - - /// - /// Create a new UncompressedSize object - /// - /// List of files and folders to parse - /// True if single DAT stats are output, false otherwise - /// Logger object for file and console output - public Stats(List inputs, bool single, Logger logger) - { - _inputs = inputs; - _single = single; - _logger = logger; - } - - /// - /// Output all requested statistics - /// - /// True if output succeeded, false otherwise - public bool Process() - { - // Init all total variables - long totalSize = 0; - long totalGame = 0; - long totalRom = 0; - long totalDisk = 0; - long totalCRC = 0; - long totalMD5 = 0; - long totalSHA1 = 0; - long totalNodump = 0; - - /// Now process each of the input files - foreach (string filename in _inputs) - { - _logger.Log("Beginning stat collection for '" + filename + "'"); - List games = new List(); - DatFile datdata = new DatFile(); - DatFile.Parse(filename, 0, 0, ref datdata, _logger); - SortedDictionary> newroms = DatFile.BucketByGame(datdata.Files, false, true, _logger, false); - - // Output single DAT stats (if asked) - if (_single) - { - _logger.User(@"\nFor file '" + filename + @"': ---------------------------------------------------"); - OutputStats(datdata, _logger); - } - else - { - _logger.User("Adding stats for file '" + filename + "'\n"); - } - - // Add single DAT stats to totals - totalSize += datdata.TotalSize; - totalGame += newroms.Count; - totalRom += datdata.RomCount; - totalDisk += datdata.DiskCount; - totalCRC += datdata.CRCCount; - totalMD5 += datdata.MD5Count; - totalSHA1 += datdata.SHA1Count; - totalNodump += datdata.NodumpCount; - } - - // Output total DAT stats - if (!_single) { _logger.User(""); } - DatFile totaldata = new DatFile - { - TotalSize = totalSize, - RomCount = totalRom, - DiskCount = totalDisk, - CRCCount = totalCRC, - MD5Count = totalMD5, - SHA1Count = totalSHA1, - NodumpCount = totalNodump, - }; - _logger.User(@"For ALL DATs found ---------------------------------------------------"); - OutputStats(totaldata, _logger, game:totalGame); - _logger.User(@" -Please check the log folder if the stats scrolled offscreen"); - - return true; - } - - /// - /// Output the stats in a human-readable format - /// - /// DatData object to read stats from - /// Logger object for file and console writing - /// True if numbers should be recalculated for the DAT, false otherwise (default) - /// Number of games to use, -1 means recalculate games (default) - public static void OutputStats(DatFile datdata, Logger logger, bool recalculate = false, long game = -1) - { - if (recalculate) - { - // Wipe out any stats already there - datdata.RomCount = 0; - datdata.DiskCount = 0; - datdata.TotalSize = 0; - datdata.CRCCount = 0; - datdata.MD5Count = 0; - datdata.SHA1Count = 0; - datdata.NodumpCount = 0; - - // Loop through and add - foreach (List roms in datdata.Files.Values) - { - foreach (Rom rom in roms) - { - datdata.RomCount += (rom.Type == ItemType.Rom ? 1 : 0); - datdata.DiskCount += (rom.Type == ItemType.Disk ? 1 : 0); - datdata.TotalSize += (rom.ItemStatus == ItemStatus.Nodump ? 0 : rom.Size); - datdata.CRCCount += (String.IsNullOrEmpty(rom.CRC) ? 0 : 1); - datdata.MD5Count += (String.IsNullOrEmpty(rom.MD5) ? 0 : 1); - datdata.SHA1Count += (String.IsNullOrEmpty(rom.SHA1) ? 0 : 1); - datdata.NodumpCount += (rom.ItemStatus == ItemStatus.Nodump ? 1 : 0); - } - } - } - - SortedDictionary> newroms = DatFile.BucketByGame(datdata.Files, false, true, logger, false); - if (datdata.TotalSize < 0) - { - datdata.TotalSize = Int64.MaxValue + datdata.TotalSize; - } - logger.User(" Uncompressed size: " + Style.GetBytesReadable(datdata.TotalSize) + @" - Games found: " + (game == -1 ? newroms.Count : game) + @" - Roms found: " + datdata.RomCount + @" - Disks found: " + datdata.DiskCount + @" - Roms with CRC: " + datdata.CRCCount + @" - Roms with MD5: " + datdata.MD5Count + @" - Roms with SHA-1: " + datdata.SHA1Count + @" - Roms with Nodump status: " + datdata.NodumpCount + @" -"); - } - } -} diff --git a/SabreTools.Helper/SabreTools.Helper.csproj b/SabreTools.Helper/SabreTools.Helper.csproj index d33c11bc..4236b5f6 100644 --- a/SabreTools.Helper/SabreTools.Helper.csproj +++ b/SabreTools.Helper/SabreTools.Helper.csproj @@ -141,7 +141,6 @@ - diff --git a/SabreTools/Partials/SabreTools_Inits.cs b/SabreTools/Partials/SabreTools_Inits.cs index 82453c63..fff1a784 100644 --- a/SabreTools/Partials/SabreTools_Inits.cs +++ b/SabreTools/Partials/SabreTools_Inits.cs @@ -262,26 +262,8 @@ namespace SabreTools /// True to show individual DAT statistics, false otherwise private static void InitStats(List inputs, bool single) { - List newinputs = new List(); - - foreach (string input in inputs) - { - if (File.Exists(input)) - { - newinputs.Add(input); - } - if (Directory.Exists(input)) - { - foreach (string file in Directory.GetFiles(input, "*", SearchOption.AllDirectories)) - { - newinputs.Add(file); - } - } - } - Logger statlog = new Logger(true, "stats.txt"); - Stats stats = new Stats(newinputs, single, statlog); - stats.Process(); + DatFile.OutputStats(inputs, single, statlog); statlog.Close(true); }