2021-02-18 11:13:11 -08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
2024-03-13 01:22:59 -04:00
|
|
|
|
using SabreTools.DatFiles;
|
2024-03-13 00:02:19 -04:00
|
|
|
|
using SabreTools.DatItems;
|
2024-03-04 23:56:05 -05:00
|
|
|
|
using SabreTools.Hashing;
|
2024-10-24 00:36:44 -04:00
|
|
|
|
using SabreTools.IO.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
|
|
|
|
|
|
{
|
2024-10-30 13:41:45 -04:00
|
|
|
|
protected bool _writeToConsole = false;
|
2021-02-18 11:13:11 -08:00
|
|
|
|
|
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>
|
2024-10-30 13:41:45 -04:00
|
|
|
|
public Textfile(List<DatStatistics> statsList)
|
2021-02-18 11:13:11 -08:00
|
|
|
|
: base(statsList)
|
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)
|
|
|
|
|
|
{
|
2025-01-08 16:59:44 -05:00
|
|
|
|
_logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
|
2021-02-18 11:13:11 -08:00
|
|
|
|
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);
|
2024-07-17 15:46:42 -04:00
|
|
|
|
|
2021-02-18 11:13:11 -08:00
|
|
|
|
// 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)
|
|
|
|
|
|
{
|
2025-01-08 16:59:44 -05:00
|
|
|
|
_logger.Error(ex);
|
2021-02-18 11:13:11 -08:00
|
|
|
|
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>
|
2025-02-14 20:45:47 -05:00
|
|
|
|
private static 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-03-13 01:22:59 -04:00
|
|
|
|
Uncompressed size: " + GetBytesReadable(stat!.TotalSize) + @"
|
2021-02-18 11:13:11 -08:00
|
|
|
|
Games found: " + stat.MachineCount + @"
|
2024-03-13 01:22:59 -04:00
|
|
|
|
Roms found: " + stat.GetItemCount(ItemType.Rom) + @"
|
|
|
|
|
|
Disks found: " + stat.GetItemCount(ItemType.Disk) + @"
|
|
|
|
|
|
Roms with CRC: " + stat.GetHashCount(HashType.CRC32) + @"
|
|
|
|
|
|
Roms with MD5: " + stat.GetHashCount(HashType.MD5) + @"
|
|
|
|
|
|
Roms with SHA-1: " + stat.GetHashCount(HashType.SHA1) + @"
|
|
|
|
|
|
Roms with SHA-256: " + stat.GetHashCount(HashType.SHA256) + @"
|
|
|
|
|
|
Roms with SHA-384: " + stat.GetHashCount(HashType.SHA384) + @"
|
|
|
|
|
|
Roms with SHA-512: " + stat.GetHashCount(HashType.SHA512) + "\n";
|
2021-02-18 11:13:11 -08:00
|
|
|
|
|
|
|
|
|
|
if (baddumpCol)
|
2024-03-13 01:22:59 -04:00
|
|
|
|
line += " Roms with BadDump status: " + stat.GetStatusCount(ItemStatus.BadDump) + "\n";
|
2021-02-18 11:13:11 -08:00
|
|
|
|
|
|
|
|
|
|
if (nodumpCol)
|
2024-03-13 01:22:59 -04:00
|
|
|
|
line += " Roms with Nodump status: " + stat.GetStatusCount(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>
|
2025-02-14 20:45:47 -05:00
|
|
|
|
private static 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
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-30 13:41:45 -04:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Console report format
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal sealed class ConsoleOutput : Textfile
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a new report from the filename
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="statsList">List of statistics objects to set</param>
|
|
|
|
|
|
public ConsoleOutput(List<DatStatistics> statsList) : base(statsList)
|
|
|
|
|
|
{
|
|
|
|
|
|
_writeToConsole = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
}
|