2017-11-07 13:56:15 -08:00
|
|
|
|
using System;
|
2021-02-18 11:13:11 -08:00
|
|
|
|
using System.Collections.Generic;
|
2020-06-10 22:37:19 -07:00
|
|
|
|
using System.IO;
|
2019-03-29 00:15:40 -07:00
|
|
|
|
using System.Net;
|
2021-02-18 11:13:11 -08:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
|
|
|
|
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>
|
|
|
|
|
|
/// HTML report format
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// TODO: Make output standard width, without making the entire thing a table
|
|
|
|
|
|
internal class Html : BaseReport
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <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>
|
|
|
|
|
|
public Html(List<DatStatistics> statsList)
|
|
|
|
|
|
: 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}");
|
2021-02-18 11:13:11 -08:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// Try to create the output file
|
2024-02-28 19:19:50 -05:00
|
|
|
|
FileStream fs = 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-19 16:39:58 -04:00
|
|
|
|
XmlTextWriter xtw = new(fs, Encoding.UTF8)
|
2021-02-18 11:13:11 -08:00
|
|
|
|
{
|
|
|
|
|
|
Formatting = Formatting.Indented,
|
|
|
|
|
|
IndentChar = '\t',
|
|
|
|
|
|
Indentation = 1
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Write out the header
|
|
|
|
|
|
WriteHeader(xtw, baddumpCol, nodumpCol);
|
|
|
|
|
|
|
|
|
|
|
|
// Now process each of the statistics
|
|
|
|
|
|
for (int i = 0; i < Statistics.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Get the current statistic
|
|
|
|
|
|
DatStatistics stat = Statistics[i];
|
|
|
|
|
|
|
|
|
|
|
|
// If we have a directory statistic
|
|
|
|
|
|
if (stat.IsDirectory)
|
|
|
|
|
|
{
|
|
|
|
|
|
WriteMidSeparator(xtw, baddumpCol, nodumpCol);
|
|
|
|
|
|
WriteIndividual(xtw, stat, baddumpCol, nodumpCol);
|
2024-02-28 23:57:08 -05: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(xtw, baddumpCol, nodumpCol);
|
|
|
|
|
|
WriteMidHeader(xtw, baddumpCol, nodumpCol);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we have a normal statistic
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
WriteIndividual(xtw, stat, baddumpCol, nodumpCol);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WriteFooter(xtw);
|
2024-02-28 23:57:08 -05:00
|
|
|
|
#if NET452_OR_GREATER
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.Dispose();
|
2024-02-28 23:57:08 -05:00
|
|
|
|
#endif
|
2021-02-18 11:13:11 -08:00
|
|
|
|
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 out the header 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="xtw">XmlTextWriter to write to</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 WriteHeader(XmlTextWriter xtw, bool baddumpCol, bool nodumpCol)
|
2019-02-08 20:53:13 -08:00
|
|
|
|
{
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteDocType("html", null, null, null);
|
|
|
|
|
|
xtw.WriteStartElement("html");
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("header");
|
|
|
|
|
|
xtw.WriteElementString("title", "DAT Statistics Report");
|
|
|
|
|
|
xtw.WriteElementString("style", @"
|
|
|
|
|
|
body {
|
|
|
|
|
|
background-color: lightgray;
|
|
|
|
|
|
}
|
|
|
|
|
|
.dir {
|
|
|
|
|
|
color: #0088FF;
|
|
|
|
|
|
}");
|
|
|
|
|
|
xtw.WriteEndElement(); // header
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("body");
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteElementString("h2", $"DAT Statistics Report ({DateTime.Now.ToShortDateString()})");
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("table");
|
|
|
|
|
|
xtw.WriteAttributeString("border", "1");
|
|
|
|
|
|
xtw.WriteAttributeString("cellpadding", "5");
|
|
|
|
|
|
xtw.WriteAttributeString("cellspacing", "0");
|
|
|
|
|
|
xtw.Flush();
|
|
|
|
|
|
|
|
|
|
|
|
// Now write the mid header for those who need it
|
|
|
|
|
|
WriteMidHeader(xtw, baddumpCol, nodumpCol);
|
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 mid-header 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="xtw">XmlTextWriter to write to</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 WriteMidHeader(XmlTextWriter xtw, bool baddumpCol, bool nodumpCol)
|
2019-02-08 20:53:13 -08:00
|
|
|
|
{
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteStartElement("tr");
|
|
|
|
|
|
xtw.WriteAttributeString("bgcolor", "gray");
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteElementString("th", "File Name");
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("th");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
|
|
|
|
|
xtw.WriteString("Total Size");
|
|
|
|
|
|
xtw.WriteEndElement(); // th
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("th");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
|
|
|
|
|
xtw.WriteString("Games");
|
|
|
|
|
|
xtw.WriteEndElement(); // th
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("th");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
|
|
|
|
|
xtw.WriteString("Roms");
|
|
|
|
|
|
xtw.WriteEndElement(); // th
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("th");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
|
|
|
|
|
xtw.WriteString("Disks");
|
|
|
|
|
|
xtw.WriteEndElement(); // th
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("th");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
|
|
|
|
|
xtw.WriteString("# with CRC");
|
|
|
|
|
|
xtw.WriteEndElement(); // th
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("th");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
|
|
|
|
|
xtw.WriteString("# with MD5");
|
|
|
|
|
|
xtw.WriteEndElement(); // th
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("th");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
|
|
|
|
|
xtw.WriteString("# with SHA-1");
|
|
|
|
|
|
xtw.WriteEndElement(); // th
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("th");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
|
|
|
|
|
xtw.WriteString("# with SHA-256");
|
|
|
|
|
|
xtw.WriteEndElement(); // th
|
|
|
|
|
|
|
|
|
|
|
|
if (baddumpCol)
|
|
|
|
|
|
{
|
|
|
|
|
|
xtw.WriteStartElement("th");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
|
|
|
|
|
xtw.WriteString("Baddumps");
|
|
|
|
|
|
xtw.WriteEndElement(); // th
|
2019-02-08 20:53:13 -08:00
|
|
|
|
}
|
2021-02-18 11:13:11 -08:00
|
|
|
|
|
|
|
|
|
|
if (nodumpCol)
|
|
|
|
|
|
{
|
|
|
|
|
|
xtw.WriteStartElement("th");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
|
|
|
|
|
xtw.WriteString("Nodumps");
|
|
|
|
|
|
xtw.WriteEndElement(); // th
|
2019-02-08 20:53:13 -08:00
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteEndElement(); // tr
|
|
|
|
|
|
xtw.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 a single set of statistics
|
2019-02-08 20:53:13 -08:00
|
|
|
|
/// </summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// <param name="xtw">XmlTextWriter 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(XmlTextWriter xtw, DatStatistics stat, bool baddumpCol, bool nodumpCol)
|
2019-02-08 20:53:13 -08:00
|
|
|
|
{
|
2024-02-28 19:19:50 -05:00
|
|
|
|
bool isDirectory = stat.DisplayName!.StartsWith("DIR: ");
|
2021-02-18 11:13:11 -08:00
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("tr");
|
|
|
|
|
|
if (isDirectory)
|
|
|
|
|
|
xtw.WriteAttributeString("class", "dir");
|
2024-02-28 23:57:08 -05:00
|
|
|
|
|
|
|
|
|
|
#if NET20 || NET35
|
|
|
|
|
|
xtw.WriteElementString("td", isDirectory ? stat.DisplayName.Remove(0, 5) : stat.DisplayName);
|
|
|
|
|
|
#else
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteElementString("td", isDirectory ? WebUtility.HtmlEncode(stat.DisplayName.Remove(0, 5)) : WebUtility.HtmlEncode(stat.DisplayName));
|
2024-02-28 23:57:08 -05:00
|
|
|
|
#endif
|
2021-02-18 11:13:11 -08:00
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("td");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
2024-02-28 19:19:50 -05:00
|
|
|
|
xtw.WriteString(GetBytesReadable(stat.Statistics!.TotalSize));
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteEndElement(); // td
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("td");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
|
|
|
|
|
xtw.WriteString(stat.MachineCount.ToString());
|
|
|
|
|
|
xtw.WriteEndElement(); // td
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("td");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
2024-03-04 22:52:03 -05:00
|
|
|
|
xtw.WriteString(stat.Statistics.GetItemCount(Core.ItemType.Rom).ToString());
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteEndElement(); // td
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("td");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
2024-03-04 22:52:03 -05:00
|
|
|
|
xtw.WriteString(stat.Statistics.GetItemCount(Core.ItemType.Disk).ToString());
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteEndElement(); // td
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("td");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
2024-03-04 23:05:58 -05:00
|
|
|
|
xtw.WriteString(stat.Statistics.GetHashCount(Core.Hash.CRC).ToString());
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteEndElement(); // td
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("td");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
2024-03-04 23:05:58 -05:00
|
|
|
|
xtw.WriteString(stat.Statistics.GetHashCount(Core.Hash.MD5).ToString());
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteEndElement(); // td
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("td");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
2024-03-04 23:05:58 -05:00
|
|
|
|
xtw.WriteString(stat.Statistics.GetHashCount(Core.Hash.SHA1).ToString());
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteEndElement(); // td
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("td");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
2024-03-04 23:05:58 -05:00
|
|
|
|
xtw.WriteString(stat.Statistics.GetHashCount(Core.Hash.SHA256).ToString());
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteEndElement(); // td
|
|
|
|
|
|
|
|
|
|
|
|
if (baddumpCol)
|
|
|
|
|
|
{
|
|
|
|
|
|
xtw.WriteStartElement("td");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
2024-03-04 23:17:13 -05:00
|
|
|
|
xtw.WriteString(stat.Statistics.GetStatusCount(Core.ItemStatus.BadDump).ToString());
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteEndElement(); // td
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (nodumpCol)
|
|
|
|
|
|
{
|
|
|
|
|
|
xtw.WriteStartElement("td");
|
|
|
|
|
|
xtw.WriteAttributeString("align", "right");
|
2024-03-04 23:17:13 -05:00
|
|
|
|
xtw.WriteString(stat.Statistics.GetStatusCount(Core.ItemStatus.Nodump).ToString());
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteEndElement(); // td
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteEndElement(); // tr
|
|
|
|
|
|
xtw.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>
|
|
|
|
|
|
/// Write out the separator to the stream, if any exists
|
|
|
|
|
|
/// </summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// <param name="xtw">XmlTextWriter to write to</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 WriteMidSeparator(XmlTextWriter xtw, bool baddumpCol, bool nodumpCol)
|
2019-02-08 20:53:13 -08:00
|
|
|
|
{
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteStartElement("tr");
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("td");
|
|
|
|
|
|
xtw.WriteAttributeString("colspan", baddumpCol && nodumpCol ? "12" : (baddumpCol ^ nodumpCol ? "11" : "10"));
|
|
|
|
|
|
xtw.WriteEndElement(); // td
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteEndElement(); // tr
|
|
|
|
|
|
xtw.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>
|
|
|
|
|
|
/// Write out the footer-separator to the stream, if any exists
|
|
|
|
|
|
/// </summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// <param name="xtw">XmlTextWriter to write to</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 WriteFooterSeparator(XmlTextWriter xtw, bool baddumpCol, bool nodumpCol)
|
2019-02-08 20:53:13 -08:00
|
|
|
|
{
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteStartElement("tr");
|
|
|
|
|
|
xtw.WriteAttributeString("border", "0");
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteStartElement("td");
|
|
|
|
|
|
xtw.WriteAttributeString("colspan", baddumpCol && nodumpCol ? "12" : (baddumpCol ^ nodumpCol ? "11" : "10"));
|
|
|
|
|
|
xtw.WriteEndElement(); // td
|
|
|
|
|
|
|
|
|
|
|
|
xtw.WriteEndElement(); // tr
|
|
|
|
|
|
xtw.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>
|
|
|
|
|
|
/// Write out the footer to the stream, if any exists
|
|
|
|
|
|
/// </summary>
|
2021-02-18 11:13:11 -08:00
|
|
|
|
/// <param name="xtw">XmlTextWriter to write to</param>
|
|
|
|
|
|
private void WriteFooter(XmlTextWriter xtw)
|
2019-02-08 20:53:13 -08:00
|
|
|
|
{
|
2021-02-18 11:13:11 -08:00
|
|
|
|
xtw.WriteEndElement(); // table
|
|
|
|
|
|
xtw.WriteEndElement(); // body
|
|
|
|
|
|
xtw.WriteEndElement(); // html
|
|
|
|
|
|
xtw.Flush();
|
2019-02-08 20:53:13 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-11-07 13:56:15 -08:00
|
|
|
|
}
|