2021-02-18 11:13:11 -08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
|
|
using SabreTools.Logging;
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2020-12-11 10:10:56 -08:00
|
|
|
|
namespace SabreTools.Reports.Formats
|
2017-11-07 13:56:15 -08:00
|
|
|
|
{
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Textfile report format
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal class Textfile : BaseReport
|
|
|
|
|
|
{
|
2021-02-18 11:13:11 -08:00
|
|
|
|
private readonly bool _writeToConsole;
|
|
|
|
|
|
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// <summary>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// Create a new report from the filename
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// </summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// <param name="statsList">List of statistics objects to set</param>
|
|
|
|
|
|
/// <param name="writeToConsole">True to write to consoke output, false otherwise</param>
|
|
|
|
|
|
public Textfile(List<DatStatistics> statsList, bool writeToConsole)
|
|
|
|
|
|
: base(statsList)
|
2019-02-08 20:53:13 -08:00
|
|
|
|
{
|
2021-02-18 11:13:11 -08:00
|
|
|
|
_writeToConsole = writeToConsole;
|
2019-02-08 20:53:13 -08:00
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// <inheritdoc/>
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public override bool WriteToFile(string? outfile, bool baddumpCol, bool nodumpCol, bool throwOnError = false)
|
2019-02-08 20:53:13 -08:00
|
|
|
|
{
|
2023-04-19 16:39:58 -04:00
|
|
|
|
InternalStopwatch watch = new($"Writing statistics to '{outfile}");
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2021-02-18 11:13:11 -08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// Try to create the output file
|
2024-02-28 19:19:50 -05:00
|
|
|
|
Stream fs = _writeToConsole ? Console.OpenStandardOutput() : File.Create(outfile ?? string.Empty);
|
2021-02-18 11:13:11 -08:00
|
|
|
|
if (fs == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2023-04-19 16:39:58 -04:00
|
|
|
|
StreamWriter sw = new(fs);
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2021-02-18 11:13:11 -08:00
|
|
|
|
// Now process each of the statistics
|
|
|
|
|
|
for (int i = 0; i < Statistics.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Get the current statistic
|
|
|
|
|
|
DatStatistics stat = Statistics[i];
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2021-02-18 11:13:11 -08:00
|
|
|
|
// If we have a directory statistic
|
|
|
|
|
|
if (stat.IsDirectory)
|
|
|
|
|
|
{
|
|
|
|
|
|
WriteIndividual(sw, stat, baddumpCol, nodumpCol);
|
|
|
|
|
|
|
|
|
|
|
|
// If we have anything but the last value, write the separator
|
|
|
|
|
|
if (i < Statistics.Count - 1)
|
|
|
|
|
|
WriteFooterSeparator(sw);
|
|
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2021-02-18 11:13:11 -08:00
|
|
|
|
// If we have a normal statistic
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
WriteIndividual(sw, stat, baddumpCol, nodumpCol);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2021-02-18 11:13:11 -08:00
|
|
|
|
sw.Dispose();
|
|
|
|
|
|
fs.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex) when (!throwOnError)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.Error(ex);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
watch.Stop();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
2019-02-08 20:53:13 -08:00
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// <summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// Write a single set of statistics
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// </summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// <param name="sw">StreamWriter to write to</param>
|
|
|
|
|
|
/// <param name="stat">DatStatistics object to write out</param>
|
|
|
|
|
|
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
|
|
|
|
|
|
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
|
|
|
|
|
|
private void WriteIndividual(StreamWriter sw, DatStatistics stat, bool baddumpCol, bool nodumpCol)
|
2019-02-08 20:53:13 -08:00
|
|
|
|
{
|
2021-02-18 11:13:11 -08:00
|
|
|
|
string line = @"'" + stat.DisplayName + @"':
|
|
|
|
|
|
--------------------------------------------------
|
2024-02-28 19:19:50 -05:00
|
|
|
|
Uncompressed size: " + GetBytesReadable(stat.Statistics!.TotalSize) + @"
|
2021-02-18 11:13:11 -08:00
|
|
|
|
Games found: " + stat.MachineCount + @"
|
2024-03-04 22:52:03 -05:00
|
|
|
|
Roms found: " + stat.Statistics.GetItemCount(Core.ItemType.Rom) + @"
|
|
|
|
|
|
Disks found: " + stat.Statistics.GetItemCount(Core.ItemType.Disk) + @"
|
2024-03-04 23:05:58 -05:00
|
|
|
|
Roms with CRC: " + stat.Statistics.GetHashCount(Core.Hash.CRC) + @"
|
|
|
|
|
|
Roms with MD5: " + stat.Statistics.GetHashCount(Core.Hash.MD5) + @"
|
|
|
|
|
|
Roms with SHA-1: " + stat.Statistics.GetHashCount(Core.Hash.SHA1) + @"
|
|
|
|
|
|
Roms with SHA-256: " + stat.Statistics.GetHashCount(Core.Hash.SHA256) + @"
|
|
|
|
|
|
Roms with SHA-384: " + stat.Statistics.GetHashCount(Core.Hash.SHA384) + @"
|
|
|
|
|
|
Roms with SHA-512: " + stat.Statistics.GetHashCount(Core.Hash.SHA512) + "\n";
|
2021-02-18 11:13:11 -08:00
|
|
|
|
|
|
|
|
|
|
if (baddumpCol)
|
2024-03-04 23:17:13 -05:00
|
|
|
|
line += " Roms with BadDump status: " + stat.Statistics.GetStatusCount(Core.ItemStatus.BadDump) + "\n";
|
2021-02-18 11:13:11 -08:00
|
|
|
|
|
|
|
|
|
|
if (nodumpCol)
|
2024-03-04 23:17:13 -05:00
|
|
|
|
line += " Roms with Nodump status: " + stat.Statistics.GetStatusCount(Core.ItemStatus.Nodump) + "\n";
|
2021-02-18 11:13:11 -08:00
|
|
|
|
|
|
|
|
|
|
// For spacing between DATs
|
|
|
|
|
|
line += "\n\n";
|
|
|
|
|
|
|
|
|
|
|
|
sw.Write(line);
|
|
|
|
|
|
sw.Flush();
|
2019-02-08 20:53:13 -08:00
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// <summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// Write out the footer-separator to the stream, if any exists
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// </summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// <param name="sw">StreamWriter to write to</param>
|
|
|
|
|
|
private void WriteFooterSeparator(StreamWriter sw)
|
2019-02-08 20:53:13 -08:00
|
|
|
|
{
|
2021-02-18 11:13:11 -08:00
|
|
|
|
sw.Write("\n");
|
|
|
|
|
|
sw.Flush();
|
2019-02-08 20:53:13 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
}
|