2017-10-09 18:04:49 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2020-06-10 22:37:19 -07:00
|
|
|
|
using System.IO;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
using System.Text;
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2017-10-09 18:04:49 -07:00
|
|
|
|
using SabreTools.Library.Data;
|
2017-11-02 15:44:15 -07:00
|
|
|
|
using SabreTools.Library.DatItems;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
using SabreTools.Library.Tools;
|
|
|
|
|
|
using NaturalSort;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.Library.DatFiles
|
|
|
|
|
|
{
|
2019-01-11 13:43:15 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents parsing and writing of a hashfile such as an SFV, MD5, or SHA-1 file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal class Hashfile : DatFile
|
|
|
|
|
|
{
|
|
|
|
|
|
// Private instance variables specific to Hashfile DATs
|
2020-06-10 22:37:19 -07:00
|
|
|
|
private readonly Hash _hash;
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor designed for casting a base DatFile
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="datFile">Parent DatFile to copy from</param>
|
|
|
|
|
|
/// <param name="hash">Type of hash that is associated with this DAT</param>
|
|
|
|
|
|
public Hashfile(DatFile datFile, Hash hash)
|
|
|
|
|
|
: base(datFile, cloneHeader: false)
|
|
|
|
|
|
{
|
|
|
|
|
|
_hash = hash;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parse a hashfile or SFV and return all found games and roms within
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filename">Name of the file to be parsed</param>
|
|
|
|
|
|
/// <param name="sysid">System ID for the DAT</param>
|
|
|
|
|
|
/// <param name="srcid">Source ID for the DAT</param>
|
|
|
|
|
|
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
|
|
|
|
|
|
/// <param name="clean">True if game names are sanitized, false otherwise (default)</param>
|
|
|
|
|
|
/// <param name="remUnicode">True if we should remove non-ASCII characters from output, false otherwise (default)</param>
|
|
|
|
|
|
public override void ParseFile(
|
|
|
|
|
|
// Standard Dat parsing
|
|
|
|
|
|
string filename,
|
|
|
|
|
|
int sysid,
|
|
|
|
|
|
int srcid,
|
|
|
|
|
|
|
|
|
|
|
|
// Miscellaneous
|
|
|
|
|
|
bool keep,
|
|
|
|
|
|
bool clean,
|
|
|
|
|
|
bool remUnicode)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Open a file reader
|
|
|
|
|
|
Encoding enc = Utilities.GetEncoding(filename);
|
|
|
|
|
|
StreamReader sr = new StreamReader(Utilities.TryOpenRead(filename), enc);
|
|
|
|
|
|
|
|
|
|
|
|
while (!sr.EndOfStream)
|
|
|
|
|
|
{
|
|
|
|
|
|
string line = sr.ReadLine();
|
|
|
|
|
|
|
|
|
|
|
|
// Split the line and get the name and hash
|
|
|
|
|
|
string[] split = line.Split(' ');
|
2020-06-10 22:37:19 -07:00
|
|
|
|
string name = string.Empty;
|
|
|
|
|
|
string hash = string.Empty;
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
|
|
|
|
|
// If we have CRC, then it's an SFV file and the name is first are
|
|
|
|
|
|
if ((_hash & Hash.CRC) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
name = split[0].Replace("*", String.Empty);
|
|
|
|
|
|
hash = split[1];
|
|
|
|
|
|
}
|
|
|
|
|
|
// Otherwise, the name is second
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
name = split[1].Replace("*", String.Empty);
|
|
|
|
|
|
hash = split[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Rom rom = new Rom
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = name,
|
|
|
|
|
|
Size = -1,
|
|
|
|
|
|
CRC = ((_hash & Hash.CRC) != 0 ? Utilities.CleanHashData(hash, Constants.CRCLength) : null),
|
|
|
|
|
|
MD5 = ((_hash & Hash.MD5) != 0 ? Utilities.CleanHashData(hash, Constants.MD5Length) : null),
|
2020-06-05 22:26:44 -07:00
|
|
|
|
RIPEMD160 = ((_hash & Hash.RIPEMD160) != 0 ? Utilities.CleanHashData(hash, Constants.RIPEMD160Length) : null),
|
2019-01-11 13:43:15 -08:00
|
|
|
|
SHA1 = ((_hash & Hash.SHA1) != 0 ? Utilities.CleanHashData(hash, Constants.SHA1Length) : null),
|
|
|
|
|
|
SHA256 = ((_hash & Hash.SHA256) != 0 ? Utilities.CleanHashData(hash, Constants.SHA256Length) : null),
|
|
|
|
|
|
SHA384 = ((_hash & Hash.SHA384) != 0 ? Utilities.CleanHashData(hash, Constants.SHA384Length) : null),
|
|
|
|
|
|
SHA512 = ((_hash & Hash.SHA512) != 0 ? Utilities.CleanHashData(hash, Constants.SHA512Length) : null),
|
|
|
|
|
|
ItemStatus = ItemStatus.None,
|
|
|
|
|
|
|
|
|
|
|
|
MachineName = Path.GetFileNameWithoutExtension(filename),
|
|
|
|
|
|
|
|
|
|
|
|
SystemID = sysid,
|
|
|
|
|
|
SourceID = srcid,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Now process and add the rom
|
|
|
|
|
|
ParseAddHelper(rom, clean, remUnicode);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sr.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create and open an output file for writing direct from a dictionary
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="outfile">Name of the file to write to</param>
|
|
|
|
|
|
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
|
|
|
|
|
|
/// <returns>True if the DAT was written correctly, false otherwise</returns>
|
|
|
|
|
|
public override bool WriteToFile(string outfile, bool ignoreblanks = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2020-06-10 22:37:19 -07:00
|
|
|
|
Globals.Logger.User($"Opening file for writing: {outfile}");
|
2019-01-11 13:43:15 -08:00
|
|
|
|
FileStream fs = Utilities.TryCreate(outfile);
|
|
|
|
|
|
|
|
|
|
|
|
// If we get back null for some reason, just log and return
|
|
|
|
|
|
if (fs == null)
|
|
|
|
|
|
{
|
2020-06-10 22:37:19 -07:00
|
|
|
|
Globals.Logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
|
2019-01-11 13:43:15 -08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(false));
|
|
|
|
|
|
|
|
|
|
|
|
// Get a properly sorted set of keys
|
|
|
|
|
|
List<string> keys = Keys;
|
|
|
|
|
|
keys.Sort(new NaturalComparer());
|
|
|
|
|
|
|
|
|
|
|
|
foreach (string key in keys)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<DatItem> roms = this[key];
|
|
|
|
|
|
|
|
|
|
|
|
// Resolve the names in the block
|
|
|
|
|
|
roms = DatItem.ResolveNames(roms);
|
|
|
|
|
|
|
|
|
|
|
|
for (int index = 0; index < roms.Count; index++)
|
|
|
|
|
|
{
|
|
|
|
|
|
DatItem rom = roms[index];
|
|
|
|
|
|
|
|
|
|
|
|
// There are apparently times when a null rom can skip by, skip them
|
|
|
|
|
|
if (rom.Name == null || rom.MachineName == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Warning("Null rom found!");
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we have a "null" game (created by DATFromDir or something similar), log it to file
|
|
|
|
|
|
if (rom.ItemType == ItemType.Rom
|
|
|
|
|
|
&& ((Rom)rom).Size == -1
|
|
|
|
|
|
&& ((Rom)rom).CRC == "null")
|
|
|
|
|
|
{
|
2020-06-10 22:37:19 -07:00
|
|
|
|
Globals.Logger.Verbose($"Empty folder found: {rom.MachineName}");
|
2019-01-11 13:43:15 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now, output the rom data
|
|
|
|
|
|
WriteDatItem(sw, rom, ignoreblanks);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-10 22:37:19 -07:00
|
|
|
|
Globals.Logger.Verbose($"File written!{Environment.NewLine}");
|
2019-01-11 13:43:15 -08:00
|
|
|
|
sw.Dispose();
|
|
|
|
|
|
fs.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write out DatItem using the supplied StreamWriter
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sw">StreamWriter to output to</param>
|
2020-06-10 22:37:19 -07:00
|
|
|
|
/// <param name="datItem">DatItem object to be output</param>
|
2019-01-11 13:43:15 -08:00
|
|
|
|
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
|
|
|
|
|
|
/// <returns>True if the data was written, false on error</returns>
|
2020-06-10 22:37:19 -07:00
|
|
|
|
private bool WriteDatItem(StreamWriter sw, DatItem datItem, bool ignoreblanks = false)
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
|
|
|
|
|
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
|
2020-06-10 22:37:19 -07:00
|
|
|
|
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
|
2019-01-11 13:43:15 -08:00
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// Pre-process the item name
|
2020-06-10 22:37:19 -07:00
|
|
|
|
ProcessItemName(datItem, true);
|
2019-01-11 13:43:15 -08:00
|
|
|
|
|
2020-06-10 22:37:19 -07:00
|
|
|
|
// Build the state based on excluded fields
|
2019-01-11 13:43:15 -08:00
|
|
|
|
switch (_hash)
|
|
|
|
|
|
{
|
2020-06-05 22:26:44 -07:00
|
|
|
|
case Hash.CRC:
|
2020-06-12 11:02:23 -07:00
|
|
|
|
switch (datItem.ItemType)
|
2020-06-05 22:26:44 -07:00
|
|
|
|
{
|
2020-06-12 11:02:23 -07:00
|
|
|
|
case ItemType.Rom:
|
|
|
|
|
|
var rom = datItem as Rom;
|
|
|
|
|
|
if (GameName)
|
|
|
|
|
|
sw.Write($"{rom.GetField(Field.MachineName, ExcludeFields)}{Path.DirectorySeparatorChar}");
|
|
|
|
|
|
sw.Write($"{rom.GetField(Field.Name, ExcludeFields)}");
|
|
|
|
|
|
sw.Write($"{rom.GetField(Field.CRC, ExcludeFields)}");
|
|
|
|
|
|
sw.Write("\n");
|
|
|
|
|
|
break;
|
2020-06-05 22:26:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2019-01-11 13:43:15 -08:00
|
|
|
|
case Hash.MD5:
|
2020-06-05 22:26:44 -07:00
|
|
|
|
case Hash.RIPEMD160:
|
2019-01-11 13:43:15 -08:00
|
|
|
|
case Hash.SHA1:
|
|
|
|
|
|
case Hash.SHA256:
|
|
|
|
|
|
case Hash.SHA384:
|
|
|
|
|
|
case Hash.SHA512:
|
2020-06-10 22:37:19 -07:00
|
|
|
|
Field hashField = Utilities.GetFieldFromHash(_hash);
|
2020-06-12 11:02:23 -07:00
|
|
|
|
|
|
|
|
|
|
switch (datItem.ItemType)
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
2020-06-12 11:02:23 -07:00
|
|
|
|
case ItemType.Disk:
|
|
|
|
|
|
var disk = datItem as Disk;
|
|
|
|
|
|
sw.Write($"{disk.GetField(hashField, ExcludeFields)}");
|
|
|
|
|
|
if (GameName)
|
|
|
|
|
|
sw.Write($"{disk.GetField(Field.MachineName, ExcludeFields)}{Path.DirectorySeparatorChar}");
|
|
|
|
|
|
sw.Write($"{disk.GetField(Field.Name, ExcludeFields)}");
|
|
|
|
|
|
sw.Write("\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case ItemType.Rom:
|
|
|
|
|
|
var rom = datItem as Rom;
|
|
|
|
|
|
sw.Write($"{rom.GetField(hashField, ExcludeFields)}");
|
|
|
|
|
|
if (GameName)
|
|
|
|
|
|
sw.Write($"{rom.GetField(Field.MachineName, ExcludeFields)}{Path.DirectorySeparatorChar}");
|
|
|
|
|
|
sw.Write($"{rom.GetField(Field.Name, ExcludeFields)}");
|
|
|
|
|
|
sw.Write("\n");
|
|
|
|
|
|
break;
|
2019-01-11 13:43:15 -08:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sw.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-10-09 18:04:49 -07:00
|
|
|
|
}
|