Reduce boilerplate for writing to file

This commit is contained in:
Matt Nadareski
2020-08-28 15:06:07 -07:00
parent d11209951b
commit f85fbd68ce
18 changed files with 395 additions and 816 deletions

View File

@@ -1,7 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net; using System.Linq;
using System.Text; using System.Text;
using SabreTools.Library.Data; using SabreTools.Library.Data;
@@ -30,13 +30,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile( protected override void ParseFile(string filename, int indexId, bool keep)
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// Open a file reader // Open a file reader
Encoding enc = FileExtensions.GetEncoding(filename); Encoding enc = FileExtensions.GetEncoding(filename);
@@ -147,45 +141,24 @@ namespace SabreTools.Library.DatFiles
// Write out the header // Write out the header
WriteHeader(svw); WriteHeader(svw);
// Write out each of the machines and roms
string lastgame = null;
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem item = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them // Check for a "null" item
if (item.Name == null || item.Machine.Name == null) datItem = ProcessNullifiedItem(datItem);
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a new game, output the beginning of the new item // Write out the item if we're not ignoring
if (lastgame == null || lastgame.ToLowerInvariant() != item.Machine.Name.ToLowerInvariant()) if (!ShouldIgnore(datItem, ignoreblanks))
WriteDatItem(svw, item, ignoreblanks); WriteDatItem(svw, datItem);
// If we have a "null" game (created by DATFromDir or something similar), log it to file
if (item.ItemType == ItemType.Rom
&& (item as Rom).Size == -1
&& (item as Rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {item.Machine.Name}");
item.Name = (item.Name == "null" ? "-" : item.Name);
(item as Rom).Size = Constants.SizeZero;
}
// Set the new data to compare against
lastgame = item.Machine.Name;
} }
} }
@@ -250,14 +223,9 @@ namespace SabreTools.Library.DatFiles
/// </summary> /// </summary>
/// <param name="svw">SeparatedValueWriter to output to</param> /// <param name="svw">SeparatedValueWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <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> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(SeparatedValueWriter svw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(SeparatedValueWriter svw, DatItem datItem)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
try try
{ {
// No game should start with a path separator // No game should start with a path separator

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using SabreTools.Library.Data; using SabreTools.Library.Data;
@@ -30,13 +31,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile( protected override void ParseFile(string filename, int indexId, bool keep)
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// Open a file reader // Open a file reader
Encoding enc = FileExtensions.GetEncoding(filename); Encoding enc = FileExtensions.GetEncoding(filename);
@@ -459,56 +454,32 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem rom = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them
if (rom.Name == null || rom.Machine.Name == null)
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a different game and we're not at the start of the list, output the end of last item // If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame != null && lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteEndGame(cmpw, rom); WriteEndGame(cmpw, datItem);
// If we have a new game, output the beginning of the new item // If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteStartGame(cmpw, rom); WriteStartGame(cmpw, datItem);
// If we have a "null" game (created by DATFromDir or something similar), log it to file // Check for a "null" item
if (rom.ItemType == ItemType.Rom datItem = ProcessNullifiedItem(datItem);
&& ((Rom)rom).Size == -1
&& ((Rom)rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
// If we're in a mode that doesn't allow for actual empty folders, add the blank info // Write out the item if we're not ignoring
rom.Name = (rom.Name == "null" ? "-" : rom.Name); if (!ShouldIgnore(datItem, ignoreblanks))
((Rom)rom).Size = Constants.SizeZero; WriteDatItem(cmpw, datItem);
((Rom)rom).CRC = ((Rom)rom).CRC == "null" ? Constants.CRCZero : null;
((Rom)rom).MD5 = ((Rom)rom).MD5 == "null" ? Constants.MD5Zero : null;
#if NET_FRAMEWORK
((Rom)rom).RIPEMD160 = ((Rom)rom).RIPEMD160 == "null" ? Constants.RIPEMD160Zero : null;
#endif
((Rom)rom).SHA1 = ((Rom)rom).SHA1 == "null" ? Constants.SHA1Zero : null;
((Rom)rom).SHA256 = ((Rom)rom).SHA256 == "null" ? Constants.SHA256Zero : null;
((Rom)rom).SHA384 = ((Rom)rom).SHA384 == "null" ? Constants.SHA384Zero : null;
((Rom)rom).SHA512 = ((Rom)rom).SHA512 == "null" ? Constants.SHA512Zero : null;
}
// Now, output the rom data
WriteDatItem(cmpw, rom, ignoreblanks);
// Set the new data to compare against // Set the new data to compare against
lastgame = rom.Machine.Name; lastgame = datItem.Machine.Name;
} }
} }
@@ -634,14 +605,9 @@ namespace SabreTools.Library.DatFiles
/// <param name="datFile">DatFile to write out from</param> /// <param name="datFile">DatFile to write out from</param>
/// <param name="cmpw">ClrMameProWriter to output to</param> /// <param name="cmpw">ClrMameProWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <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> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(ClrMameProWriter cmpw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(ClrMameProWriter cmpw, DatItem datItem)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
try try
{ {
// Pre-process the item name // Pre-process the item name

View File

@@ -3605,6 +3605,83 @@ namespace SabreTools.Library.DatFiles
/// <returns>True if the DAT was written correctly, false otherwise</returns> /// <returns>True if the DAT was written correctly, false otherwise</returns>
public abstract bool WriteToFile(string outfile, bool ignoreblanks = false); public abstract bool WriteToFile(string outfile, bool ignoreblanks = false);
/// <summary>
/// Create a prefix or postfix from inputs
/// </summary>
/// <param name="item">DatItem to create a prefix/postfix for</param>
/// <param name="prefix">True for prefix, false for postfix</param>
/// <returns>Sanitized string representing the postfix or prefix</returns>
protected string CreatePrefixPostfix(DatItem item, bool prefix)
{
// Initialize strings
string fix = string.Empty,
game = item.Machine.Name,
name = item.Name,
crc = string.Empty,
md5 = string.Empty,
ripemd160 = string.Empty,
sha1 = string.Empty,
sha256 = string.Empty,
sha384 = string.Empty,
sha512 = string.Empty,
size = string.Empty;
// If we have a prefix
if (prefix)
fix = Header.Prefix + (Header.Quotes ? "\"" : string.Empty);
// If we have a postfix
else
fix = (Header.Quotes ? "\"" : string.Empty) + Header.Postfix;
// Ensure we have the proper values for replacement
if (item.ItemType == ItemType.Disk)
{
md5 = (item as Disk).MD5 ?? string.Empty;
sha1 = (item as Disk).SHA1 ?? string.Empty;
}
else if (item.ItemType == ItemType.Media)
{
md5 = (item as Media).MD5 ?? string.Empty;
sha1 = (item as Media).SHA1 ?? string.Empty;
sha256 = (item as Media).SHA256 ?? string.Empty;
}
else if (item.ItemType == ItemType.Rom)
{
crc = (item as Rom).CRC ?? string.Empty;
md5 = (item as Rom).MD5 ?? string.Empty;
#if NET_FRAMEWORK
ripemd160 = (item as Rom).RIPEMD160 ?? string.Empty;
#endif
sha1 = (item as Rom).SHA1 ?? string.Empty;
sha256 = (item as Rom).SHA256 ?? string.Empty;
sha384 = (item as Rom).SHA384 ?? string.Empty;
sha512 = (item as Rom).SHA512 ?? string.Empty;
size = (item as Rom).Size.ToString();
}
// Now do bulk replacement where possible
fix = fix
.Replace("%game%", game)
.Replace("%machine%", game)
.Replace("%name%", name)
.Replace("%manufacturer%", item.Machine.Manufacturer ?? string.Empty)
.Replace("%publisher%", item.Machine.Publisher ?? string.Empty)
.Replace("%category%", item.Machine.Category ?? string.Empty)
.Replace("%crc%", crc)
.Replace("%md5%", md5)
.Replace("%ripemd160%", ripemd160)
.Replace("%sha1%", sha1)
.Replace("%sha256%", sha256)
.Replace("%sha384%", sha384)
.Replace("%sha512%", sha512)
.Replace("%size%", size);
// TODO: Add GameName logic here too?
return fix;
}
/// <summary> /// <summary>
/// Process an item and correctly set the item name /// Process an item and correctly set the item name
/// </summary> /// </summary>
@@ -3696,80 +3773,67 @@ namespace SabreTools.Library.DatFiles
} }
/// <summary> /// <summary>
/// Create a prefix or postfix from inputs /// Process any DatItems that are "null", usually created from directory population
/// </summary> /// </summary>
/// <param name="item">DatItem to create a prefix/postfix for</param> /// <param name="datItem">DatItem to check for "null" status</param>
/// <param name="prefix">True for prefix, false for postfix</param> /// <returns>Cleaned DatItem</returns>
/// <returns>Sanitized string representing the postfix or prefix</returns> protected DatItem ProcessNullifiedItem(DatItem datItem)
protected string CreatePrefixPostfix(DatItem item, bool prefix)
{ {
// Initialize strings // If we don't have a Rom, we can ignore it
string fix = string.Empty, if (datItem.ItemType != ItemType.Rom)
game = item.Machine.Name, return datItem;
name = item.Name,
crc = string.Empty,
md5 = string.Empty,
ripemd160 = string.Empty,
sha1 = string.Empty,
sha256 = string.Empty,
sha384 = string.Empty,
sha512 = string.Empty,
size = string.Empty;
// If we have a prefix // Cast for easier parsing
if (prefix) Rom rom = datItem as Rom;
fix = Header.Prefix + (Header.Quotes ? "\"" : string.Empty);
// If we have a postfix // If the Rom has "null" characteristics, ensure all fields
else if (rom.Size == -1 && rom.CRC == "null")
fix = (Header.Quotes ? "\"" : string.Empty) + Header.Postfix; {
Globals.Logger.Verbose($"Empty folder found: {datItem.Machine.Name}");
// Ensure we have the proper values for replacement datItem.Name = (datItem.Name == "null" ? "-" : datItem.Name);
if (item.ItemType == ItemType.Disk) rom.Size = Constants.SizeZero;
{ rom.CRC = rom.CRC == "null" ? Constants.CRCZero : null;
md5 = (item as Disk).MD5 ?? string.Empty; rom.MD5 = rom.MD5 == "null" ? Constants.MD5Zero : null;
sha1 = (item as Disk).SHA1 ?? string.Empty;
}
else if (item.ItemType == ItemType.Media)
{
md5 = (item as Media).MD5 ?? string.Empty;
sha1 = (item as Media).SHA1 ?? string.Empty;
sha256 = (item as Media).SHA256 ?? string.Empty;
}
else if (item.ItemType == ItemType.Rom)
{
crc = (item as Rom).CRC ?? string.Empty;
md5 = (item as Rom).MD5 ?? string.Empty;
#if NET_FRAMEWORK #if NET_FRAMEWORK
ripemd160 = (item as Rom).RIPEMD160 ?? string.Empty; rom.RIPEMD160 = rom.RIPEMD160 == "null" ? Constants.RIPEMD160Zero : null;
#endif #endif
sha1 = (item as Rom).SHA1 ?? string.Empty; rom.SHA1 = rom.SHA1 == "null" ? Constants.SHA1Zero : null;
sha256 = (item as Rom).SHA256 ?? string.Empty; rom.SHA256 = rom.SHA256 == "null" ? Constants.SHA256Zero : null;
sha384 = (item as Rom).SHA384 ?? string.Empty; rom.SHA384 = rom.SHA384 == "null" ? Constants.SHA384Zero : null;
sha512 = (item as Rom).SHA512 ?? string.Empty; rom.SHA512 = rom.SHA512 == "null" ? Constants.SHA512Zero : null;
size = (item as Rom).Size.ToString();
} }
// Now do bulk replacement where possible return rom;
fix = fix }
.Replace("%game%", game)
.Replace("%machine%", game)
.Replace("%name%", name)
.Replace("%manufacturer%", item.Machine.Manufacturer ?? string.Empty)
.Replace("%publisher%", item.Machine.Publisher ?? string.Empty)
.Replace("%category%", item.Machine.Category ?? string.Empty)
.Replace("%crc%", crc)
.Replace("%md5%", md5)
.Replace("%ripemd160%", ripemd160)
.Replace("%sha1%", sha1)
.Replace("%sha256%", sha256)
.Replace("%sha384%", sha384)
.Replace("%sha512%", sha512)
.Replace("%size%", size);
// TODO: Add GameName logic here too? /// <summary>
/// Get if an item should be ignored on write
/// </summary>
/// <param name="datItem">DatItem to check</param>
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise</param>
/// <returns>True if the item should be skipped on write, false otherwise</returns>
protected bool ShouldIgnore(DatItem datItem, bool ignoreBlanks)
{
// If the item is supposed to be removed, we ignore
if (datItem.Remove)
return true;
return fix; // If we have the Blank dat item, we ignore
if (datItem.ItemType == ItemType.Blank)
return true;
// If we're ignoring blanks and we have a Rom
if (ignoreBlanks && datItem.ItemType == ItemType.Rom)
{
Rom rom = datItem as Rom;
// If we have a 0-size or blank rom, then we ignore
if (rom.Size == 0 || rom.Size == -1)
return true;
}
return false;
} }
#endregion #endregion

View File

@@ -30,13 +30,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile( protected override void ParseFile(string filename, int indexId, bool keep)
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// Open a file reader // Open a file reader
Encoding enc = FileExtensions.GetEncoding(filename); Encoding enc = FileExtensions.GetEncoding(filename);
@@ -138,12 +132,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="cmpr">ClrMameProReader to use to parse the header</param> /// <param name="cmpr">ClrMameProReader to use to parse the header</param>
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
private void ReadGame( private void ReadGame(ClrMameProReader cmpr, string filename, int indexId)
ClrMameProReader cmpr,
// Standard Dat parsing
string filename,
int indexId)
{ {
// Prepare all internal variables // Prepare all internal variables
bool containsItems = false; bool containsItems = false;
@@ -295,57 +284,34 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem rom = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them List<string> newsplit = datItem.Machine.Name.Split('\\').ToList();
if (rom.Name == null || rom.Machine.Name == null)
{
Globals.Logger.Warning("Null rom found!");
continue;
}
List<string> newsplit = rom.Machine.Name.Split('\\').ToList();
// If we have a different game and we're not at the start of the list, output the end of last item // If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame != null && lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteEndGame(cmpw); WriteEndGame(cmpw);
// If we have a new game, output the beginning of the new item // If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteStartGame(cmpw, rom); WriteStartGame(cmpw, datItem);
// If we have a "null" game (created by DATFromDir or something similar), log it to file // Check for a "null" item
if (rom.ItemType == ItemType.Rom datItem = ProcessNullifiedItem(datItem);
&& (rom as Rom).Size == -1
&& (rom as Rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
rom.Name = (rom.Name == "null" ? "-" : rom.Name); // Write out the item if we're not ignoring
(rom as Rom).Size = Constants.SizeZero; if (!ShouldIgnore(datItem, ignoreblanks))
(rom as Rom).CRC = (rom as Rom).CRC == "null" ? Constants.CRCZero : null; WriteDatItem(cmpw, datItem);
(rom as Rom).MD5 = (rom as Rom).MD5 == "null" ? Constants.MD5Zero : null;
#if NET_FRAMEWORK
(rom as Rom).RIPEMD160 = (rom as Rom).RIPEMD160 == "null" ? Constants.RIPEMD160Zero : null;
#endif
(rom as Rom).SHA1 = (rom as Rom).SHA1 == "null" ? Constants.SHA1Zero : null;
(rom as Rom).SHA256 = (rom as Rom).SHA256 == "null" ? Constants.SHA256Zero : null;
(rom as Rom).SHA384 = (rom as Rom).SHA384 == "null" ? Constants.SHA384Zero : null;
(rom as Rom).SHA512 = (rom as Rom).SHA512 == "null" ? Constants.SHA512Zero : null;
}
// Now, output the rom data
WriteDatItem(cmpw, rom, ignoreblanks);
// Set the new data to compare against // Set the new data to compare against
lastgame = rom.Machine.Name; lastgame = datItem.Machine.Name;
} }
} }
@@ -454,14 +420,9 @@ namespace SabreTools.Library.DatFiles
/// </summary> /// </summary>
/// <param name="cmpw">ClrMameProWriter to output to</param> /// <param name="cmpw">ClrMameProWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <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> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(ClrMameProWriter cmpw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(ClrMameProWriter cmpw, DatItem datItem)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
try try
{ {
// Pre-process the item name // Pre-process the item name

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using SabreTools.Library.Data; using SabreTools.Library.Data;
@@ -29,13 +30,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile( protected override void ParseFile(string filename, int indexId, bool keep)
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// Open a file reader // Open a file reader
Encoding enc = FileExtensions.GetEncoding(filename); Encoding enc = FileExtensions.GetEncoding(filename);
@@ -117,34 +112,21 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem item = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them // Check for a "null" item
if (item.Name == null || item.Machine.Name == null) datItem = ProcessNullifiedItem(datItem);
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a "null" game (created by DATFromDir or something similar), log it to file // Write out the item if we're not ignoring
if (item.ItemType == ItemType.Rom if (!ShouldIgnore(datItem, ignoreblanks))
&& ((Rom)item).Size == -1 WriteDatItem(svw, datItem);
&& ((Rom)item).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {item.Machine.Name}");
item.Name = (item.Name == "null" ? "-" : item.Name);
((Rom)item).Size = Constants.SizeZero;
}
WriteDatItem(svw, item, ignoreblanks);
} }
} }
@@ -167,12 +149,8 @@ namespace SabreTools.Library.DatFiles
/// <param name="svw">SeparatedValueWriter to output to</param> /// <param name="svw">SeparatedValueWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <returns>True if the data was written, false on error</returns> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(SeparatedValueWriter svw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(SeparatedValueWriter svw, DatItem datItem)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
try try
{ {
// No game should start with a path separator // No game should start with a path separator

View File

@@ -34,13 +34,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile( protected override void ParseFile(string filename, int indexId, bool keep)
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// Open a file reader // Open a file reader
Encoding enc = FileExtensions.GetEncoding(filename); Encoding enc = FileExtensions.GetEncoding(filename);
@@ -132,32 +126,21 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items[key];
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem rom = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them // Check for a "null" item
if (rom.Name == null || rom.Machine.Name == null) datItem = ProcessNullifiedItem(datItem);
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a "null" game (created by DATFromDir or something similar), log it to file // Write out the item if we're not ignoring
if (rom.ItemType == ItemType.Rom if (!ShouldIgnore(datItem, ignoreblanks))
&& ((Rom)rom).Size == -1 WriteDatItem(svw, datItem);
&& ((Rom)rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
}
// Now, output the rom data
WriteDatItem(svw, rom, ignoreblanks);
} }
} }
@@ -179,14 +162,9 @@ namespace SabreTools.Library.DatFiles
/// </summary> /// </summary>
/// <param name="svw">SeparatedValueWriter to output to</param> /// <param name="svw">SeparatedValueWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <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> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(SeparatedValueWriter svw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(SeparatedValueWriter svw, DatItem datItem)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
try try
{ {
// Build the state // Build the state

View File

@@ -336,6 +336,31 @@ namespace SabreTools.Library.DatFiles
return false; return false;
} }
/// <summary>
/// Get a list of filtered items for a given key
/// </summary>
/// <param name="key">Key in the dictionary to retrieve</param>
public List<DatItem> FilteredItems(string key)
{
lock (key)
{
// Get the list, if possible
List<DatItem> fi = items[key];
if (fi == null)
return new List<DatItem>();
// Filter the list
fi = fi.Where(i => i != null)
.Where(i => !i.Remove)
.Where(i => i.Name != null)
.Where(i => i.Machine?.Name != null)
.ToList();
// Return the list
return fi;
}
}
/// <summary> /// <summary>
/// Remove a key from the file dictionary if it exists /// Remove a key from the file dictionary if it exists
/// </summary> /// </summary>

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using SabreTools.Library.Data; using SabreTools.Library.Data;
@@ -32,13 +33,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile( protected override void ParseFile(string filename, int indexId, bool keep)
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// Prepare all internal variables // Prepare all internal variables
StreamReader sr = new StreamReader(FileExtensions.TryOpenRead(filename), new UTF8Encoding(false)); StreamReader sr = new StreamReader(FileExtensions.TryOpenRead(filename), new UTF8Encoding(false));
@@ -112,12 +107,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="itr">JsonTextReader to use to parse the machine</param> /// <param name="itr">JsonTextReader to use to parse the machine</param>
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
private void ReadMachines( private void ReadMachines(JsonTextReader jtr, string filename, int indexId)
JsonTextReader jtr,
// Standard Dat parsing
string filename,
int indexId)
{ {
// If the reader is invalid, skip // If the reader is invalid, skip
if (jtr == null) if (jtr == null)
@@ -141,12 +131,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="machineObj">JObject representing a single machine</param> /// <param name="machineObj">JObject representing a single machine</param>
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
private void ReadMachine( private void ReadMachine(JObject machineObj, string filename, int indexId)
JObject machineObj,
// Standard Dat parsing
string filename,
int indexId)
{ {
// If object is invalid, skip it // If object is invalid, skip it
if (machineObj == null) if (machineObj == null)
@@ -299,55 +284,32 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem rom = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them
if (rom.Name == null || rom.Machine.Name == null)
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a different game and we're not at the start of the list, output the end of last item // If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame != null && lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteEndGame(jtw); WriteEndGame(jtw);
// If we have a new game, output the beginning of the new item // If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteStartGame(jtw, rom); WriteStartGame(jtw, datItem);
// If we have a "null" game (created by DATFromDir or something similar), log it to file // Check for a "null" item
if (rom.ItemType == ItemType.Rom datItem = ProcessNullifiedItem(datItem);
&& (rom as Rom).Size == -1
&& (rom as Rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
rom.Name = (rom.Name == "null" ? "-" : rom.Name); // Write out the item if we're not ignoring
(rom as Rom).Size = Constants.SizeZero; if (!ShouldIgnore(datItem, ignoreblanks))
(rom as Rom).CRC = (rom as Rom).CRC == "null" ? Constants.CRCZero : null; WriteDatItem(jtw, datItem);
(rom as Rom).MD5 = (rom as Rom).MD5 == "null" ? Constants.MD5Zero : null;
#if NET_FRAMEWORK
(rom as Rom).RIPEMD160 = (rom as Rom).RIPEMD160 == "null" ? Constants.RIPEMD160Zero : null;
#endif
(rom as Rom).SHA1 = (rom as Rom).SHA1 == "null" ? Constants.SHA1Zero : null;
(rom as Rom).SHA256 = (rom as Rom).SHA256 == "null" ? Constants.SHA256Zero : null;
(rom as Rom).SHA384 = (rom as Rom).SHA384 == "null" ? Constants.SHA384Zero : null;
(rom as Rom).SHA512 = (rom as Rom).SHA512 == "null" ? Constants.SHA512Zero : null;
}
// Now, output the rom data
WriteDatItem(jtw, rom, ignoreblanks);
// Set the new data to compare against // Set the new data to compare against
lastgame = rom.Machine.Name; lastgame = datItem.Machine.Name;
} }
} }
@@ -463,18 +425,9 @@ namespace SabreTools.Library.DatFiles
/// </summary> /// </summary>
/// <param name="jtw">JsonTextWriter to output to</param> /// <param name="jtw">JsonTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <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> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(JsonTextWriter jtw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(JsonTextWriter jtw, DatItem datItem)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
// If we have the blank item type somehow, skip
if (datItem.ItemType == ItemType.Blank)
return true;
try try
{ {
// Pre-process the item name // Pre-process the item name

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@@ -40,13 +41,7 @@ namespace SabreTools.Library.DatFiles
/// 6331.sound-u8 32 BAD CRC(1d298cb0) SHA1(bb0bb62365402543e3154b9a77be9c75010e6abc) BAD_DUMP /// 6331.sound-u8 32 BAD CRC(1d298cb0) SHA1(bb0bb62365402543e3154b9a77be9c75010e6abc) BAD_DUMP
/// 16v8h-blue.u24 279 NO GOOD DUMP KNOWN /// 16v8h-blue.u24 279 NO GOOD DUMP KNOWN
/// </remarks> /// </remarks>
protected override void ParseFile( protected override void ParseFile(string filename, int indexId, bool keep)
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// Open a file reader // Open a file reader
Encoding enc = FileExtensions.GetEncoding(filename); Encoding enc = FileExtensions.GetEncoding(filename);
@@ -292,52 +287,32 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem rom = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them
if (rom.Name == null || rom.Machine.Name == null)
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a different game and we're not at the start of the list, output the end of last item // If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame != null && lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteEndGame(sw); WriteEndGame(sw);
// If we have a new game, output the beginning of the new item // If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteStartGame(sw, rom); WriteStartGame(sw, datItem);
// If we have a "null" game (created by DATFromDir or something similar), log it to file // Check for a "null" item
if (rom.ItemType == ItemType.Rom datItem = ProcessNullifiedItem(datItem);
&& (rom as Rom).Size == -1
&& (rom as Rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
rom.Name = (rom.Name == "null" ? "-" : rom.Name); // Write out the item if we're not ignoring
(rom as Rom).Size = Constants.SizeZero; if (!ShouldIgnore(datItem, ignoreblanks))
(rom as Rom).CRC = (rom as Rom).CRC == "null" ? Constants.CRCZero : null; WriteDatItem(sw, datItem);
(rom as Rom).MD5 = (rom as Rom).MD5 == "null" ? Constants.MD5Zero : null;
(rom as Rom).SHA1 = (rom as Rom).SHA1 == "null" ? Constants.SHA1Zero : null;
(rom as Rom).SHA256 = (rom as Rom).SHA256 == "null" ? Constants.SHA256Zero : null;
(rom as Rom).SHA384 = (rom as Rom).SHA384 == "null" ? Constants.SHA384Zero : null;
(rom as Rom).SHA512 = (rom as Rom).SHA512 == "null" ? Constants.SHA512Zero : null;
}
// Now, output the rom data
WriteDatItem(sw, rom, ignoreblanks);
// Set the new data to compare against // Set the new data to compare against
lastgame = rom.Machine.Name; lastgame = datItem.Machine.Name;
} }
} }
@@ -410,14 +385,9 @@ namespace SabreTools.Library.DatFiles
/// </summary> /// </summary>
/// <param name="sw">StreamWriter to output to</param> /// <param name="sw">StreamWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <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> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(StreamWriter sw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(StreamWriter sw, DatItem datItem)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
try try
{ {
// Pre-process the item name // Pre-process the item name

View File

@@ -34,13 +34,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
/// <remarks> /// <remarks>
/// </remarks> /// </remarks>
protected override void ParseFile( protected override void ParseFile(string filename, int indexId, bool keep)
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// Prepare all internal variables // Prepare all internal variables
XmlReader xtr = filename.GetXmlTextReader(); XmlReader xtr = filename.GetXmlTextReader();
@@ -112,12 +106,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="reader">XmlReader representing a machine block</param> /// <param name="reader">XmlReader representing a machine block</param>
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
private void ReadMachine( private void ReadMachine(XmlReader reader, string filename, int indexId)
XmlReader reader,
// Standard Dat parsing
string filename,
int indexId)
{ {
// If we have an empty machine, skip it // If we have an empty machine, skip it
if (reader == null) if (reader == null)
@@ -499,7 +488,7 @@ namespace SabreTools.Library.DatFiles
slot.Name = reader.GetAttribute("name"); slot.Name = reader.GetAttribute("name");
// Now read the internal tags // Now read the internal tags
ReadSlot(reader.ReadSubtree(), slot, machine); ReadSlot(reader.ReadSubtree(), slot);
// Ensure the list exists // Ensure the list exists
if (machine.Slots == null) if (machine.Slots == null)
@@ -579,8 +568,7 @@ namespace SabreTools.Library.DatFiles
/// </summary> /// </summary>
/// <param name="reader">XmlReader representing a machine block</param> /// <param name="reader">XmlReader representing a machine block</param>
/// <param name="slot">ListXmlSlot to populate</param> /// <param name="slot">ListXmlSlot to populate</param>
/// <param name="machine">Machine information to pass to contained items</param> private void ReadSlot(XmlReader reader, ListXmlSlot slot)
private void ReadSlot(XmlReader reader, ListXmlSlot slot, Machine machine)
{ {
// If we have an empty machine, skip it // If we have an empty machine, skip it
if (reader == null) if (reader == null)
@@ -981,46 +969,32 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem rom = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them
if (rom.Name == null || rom.Machine.Name == null)
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a different game and we're not at the start of the list, output the end of last item // If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame != null && lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteEndGame(xtw, rom); WriteEndGame(xtw, datItem);
// If we have a new game, output the beginning of the new item // If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteStartGame(xtw, rom); WriteStartGame(xtw, datItem);
// If we have a "null" game (created by DATFromDir or something similar), log it to file // Check for a "null" item
if (rom.ItemType == ItemType.Rom datItem = ProcessNullifiedItem(datItem);
&& ((Rom)rom).Size == -1
&& ((Rom)rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
lastgame = rom.Machine.Name; // Write out the item if we're not ignoring
continue; if (!ShouldIgnore(datItem, ignoreblanks))
} WriteDatItem(xtw, datItem);
// Now, output the rom data
WriteDatItem(xtw, rom, ignoreblanks);
// Set the new data to compare against // Set the new data to compare against
lastgame = rom.Machine.Name; lastgame = datItem.Machine.Name;
} }
} }
@@ -1514,14 +1488,9 @@ namespace SabreTools.Library.DatFiles
/// </summary> /// </summary>
/// <param name="xtw">XmlTextWriter to output to</param> /// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <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> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
try try
{ {
// Pre-process the item name // Pre-process the item name

View File

@@ -39,13 +39,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile( protected override void ParseFile(string filename, int indexId, bool keep)
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// Prepare all internal variables // Prepare all internal variables
XmlReader xtr = filename.GetXmlTextReader(); XmlReader xtr = filename.GetXmlTextReader();
@@ -722,55 +716,32 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem rom = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them
if (rom.Name == null || rom.Machine.Name == null)
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a different game and we're not at the start of the list, output the end of last item // If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame != null && lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteEndGame(xtw); WriteEndGame(xtw);
// If we have a new game, output the beginning of the new item // If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteStartGame(xtw, rom); WriteStartGame(xtw, datItem);
// If we have a "null" game (created by DATFromDir or something similar), log it to file // Check for a "null" item
if (rom.ItemType == ItemType.Rom datItem = ProcessNullifiedItem(datItem);
&& (rom as Rom).Size == -1
&& (rom as Rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
rom.Name = (rom.Name == "null" ? "-" : rom.Name); // Write out the item if we're not ignoring
(rom as Rom).Size = Constants.SizeZero; if (!ShouldIgnore(datItem, ignoreblanks))
(rom as Rom).CRC = (rom as Rom).CRC == "null" ? Constants.CRCZero : null; WriteDatItem(xtw, datItem);
(rom as Rom).MD5 = (rom as Rom).MD5 == "null" ? Constants.MD5Zero : null;
#if NET_FRAMEWORK
(rom as Rom).RIPEMD160 = (rom as Rom).RIPEMD160 == "null" ? Constants.RIPEMD160Zero : null;
#endif
(rom as Rom).SHA1 = (rom as Rom).SHA1 == "null" ? Constants.SHA1Zero : null;
(rom as Rom).SHA256 = (rom as Rom).SHA256 == "null" ? Constants.SHA256Zero : null;
(rom as Rom).SHA384 = (rom as Rom).SHA384 == "null" ? Constants.SHA384Zero : null;
(rom as Rom).SHA512 = (rom as Rom).SHA512 == "null" ? Constants.SHA512Zero : null;
}
// Now, output the rom data
WriteDatItem(xtw, rom, ignoreblanks);
// Set the new data to compare against // Set the new data to compare against
lastgame = rom.Machine.Name; lastgame = datItem.Machine.Name;
} }
} }
@@ -981,14 +952,9 @@ namespace SabreTools.Library.DatFiles
/// </summary> /// </summary>
/// <param name="xtw">XmlTextWriter to output to</param> /// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <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> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
try try
{ {
// Pre-process the item name // Pre-process the item name

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using SabreTools.Library.Data; using SabreTools.Library.Data;
@@ -29,13 +30,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile( protected override void ParseFile(string filename, int indexId, bool keep)
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// There is no consistent way to parse a missfile... // There is no consistent way to parse a missfile...
throw new NotImplementedException(); throw new NotImplementedException();
@@ -69,37 +64,24 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem rom = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them // Check for a "null" item
if (rom.Name == null || rom.Machine.Name == null) datItem = ProcessNullifiedItem(datItem);
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a "null" game (created by DATFromDir or something similar), log it to file // Write out the item if we're not ignoring
if (rom.ItemType == ItemType.Rom if (!ShouldIgnore(datItem, ignoreblanks))
&& (rom as Rom).Size == -1 WriteDatItem(sw, datItem, lastgame);
&& (rom as Rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
lastgame = rom.Machine.Name;
continue;
}
// Now, output the rom data
WriteDatItem(sw, rom, lastgame, ignoreblanks);
// Set the new data to compare against // Set the new data to compare against
lastgame = rom.Machine.Name; lastgame = datItem.Machine.Name;
} }
} }
@@ -122,14 +104,9 @@ namespace SabreTools.Library.DatFiles
/// <param name="sw">StreamWriter to output to</param> /// <param name="sw">StreamWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <param name="lastgame">The name of the last game to be output</param> /// <param name="lastgame">The name of the last game to be output</param>
/// <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> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(StreamWriter sw, DatItem datItem, string lastgame, bool ignoreblanks = false) private bool WriteDatItem(StreamWriter sw, DatItem datItem, string lastgame)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
try try
{ {
// Process the item name // Process the item name

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using System.Xml; using System.Xml;
@@ -31,15 +32,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
/// <remarks> protected override void ParseFile(string filename, int indexId, bool keep)
/// </remarks>
protected override void ParseFile(
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
XmlReader xtr = filename.GetXmlTextReader(); XmlReader xtr = filename.GetXmlTextReader();
@@ -427,12 +420,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="reader">XmlReader to use to parse the header</param> /// <param name="reader">XmlReader to use to parse the header</param>
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
private void ReadGames( private void ReadGames(XmlReader reader, string filename, int indexId)
XmlReader reader,
// Standard Dat parsing
string filename,
int indexId)
{ {
// If there's no subtree to the configuration, skip it // If there's no subtree to the configuration, skip it
if (reader == null) if (reader == null)
@@ -474,12 +462,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="reader">XmlReader to use to parse the header</param> /// <param name="reader">XmlReader to use to parse the header</param>
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
private void ReadGame( private void ReadGame(XmlReader reader, string filename, int indexId)
XmlReader reader,
// Standard Dat parsing
string filename,
int indexId)
{ {
// Prepare all internal variables // Prepare all internal variables
string releaseNumber = string.Empty, duplicateid; string releaseNumber = string.Empty, duplicateid;
@@ -707,47 +690,24 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem rom = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them // Check for a "null" item
if (rom.Name == null || rom.Machine.Name == null) datItem = ProcessNullifiedItem(datItem);
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a "null" game (created by DATFromDir or something similar), log it to file // Write out the item if we're not ignoring
if (rom.ItemType == ItemType.Rom if (!ShouldIgnore(datItem, ignoreblanks))
&& (rom as Rom).Size == -1 WriteDatItem(xtw, datItem);
&& (rom as Rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
rom.Name = (rom.Name == "null" ? "-" : rom.Name);
(rom as Rom).Size = Constants.SizeZero;
(rom as Rom).CRC = (rom as Rom).CRC == "null" ? Constants.CRCZero : null;
(rom as Rom).MD5 = (rom as Rom).MD5 == "null" ? Constants.MD5Zero : null;
#if NET_FRAMEWORK
(rom as Rom).RIPEMD160 = (rom as Rom).RIPEMD160 == "null" ? Constants.RIPEMD160Zero : null;
#endif
(rom as Rom).SHA1 = (rom as Rom).SHA1 == "null" ? Constants.SHA1Zero : null;
(rom as Rom).SHA256 = (rom as Rom).SHA256 == "null" ? Constants.SHA256Zero : null;
(rom as Rom).SHA384 = (rom as Rom).SHA384 == "null" ? Constants.SHA384Zero : null;
(rom as Rom).SHA512 = (rom as Rom).SHA512 == "null" ? Constants.SHA512Zero : null;
}
// Now, output the rom data
WriteDatItem(xtw, rom, ignoreblanks);
// Set the new data to compare against // Set the new data to compare against
lastgame = rom.Machine.Name; lastgame = datItem.Machine.Name;
} }
} }
@@ -896,14 +856,9 @@ namespace SabreTools.Library.DatFiles
/// </summary> /// </summary>
/// <param name="xtw">XmlTextWriter to output to</param> /// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <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> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
try try
{ {
// Pre-process the item name // Pre-process the item name

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using System.Xml; using System.Xml;
@@ -31,13 +32,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile( protected override void ParseFile(string filename, int indexId, bool keep)
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// Prepare all internal variables // Prepare all internal variables
XmlReader xtr = filename.GetXmlTextReader(); XmlReader xtr = filename.GetXmlTextReader();
@@ -99,12 +94,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="reader">XmlReader representing a machine block</param> /// <param name="reader">XmlReader representing a machine block</param>
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
private void ReadSoftware( private void ReadSoftware(XmlReader reader, string filename, int indexId)
XmlReader reader,
// Standard Dat parsing
string filename,
int indexId)
{ {
// If we have an empty machine, skip it // If we have an empty machine, skip it
if (reader == null) if (reader == null)
@@ -546,46 +536,32 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem rom = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them
if (rom.Name == null || rom.Machine.Name == null)
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a different game and we're not at the start of the list, output the end of last item // If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame != null && lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteEndGame(xtw); WriteEndGame(xtw);
// If we have a new game, output the beginning of the new item // If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteStartGame(xtw, rom); WriteStartGame(xtw, datItem);
// If we have a "null" game (created by DATFromDir or something similar), log it to file // Check for a "null" item
if (rom.ItemType == ItemType.Rom datItem = ProcessNullifiedItem(datItem);
&& (rom as Rom).Size == -1
&& (rom as Rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
lastgame = rom.Machine.Name; // Write out the item if we're not ignoring
continue; if (!ShouldIgnore(datItem, ignoreblanks))
} WriteDatItem(xtw, datItem);
// Now, output the rom data
WriteDatItem(xtw, rom, ignoreblanks);
// Set the new data to compare against // Set the new data to compare against
lastgame = rom.Machine.Name; lastgame = datItem.Machine.Name;
} }
} }
@@ -706,14 +682,9 @@ namespace SabreTools.Library.DatFiles
/// </summary> /// </summary>
/// <param name="xtw">XmlTextWriter to output to</param> /// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <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> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
try try
{ {
// Pre-process the item name // Pre-process the item name

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using SabreTools.Library.Data; using SabreTools.Library.Data;
@@ -29,13 +30,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile( protected override void ParseFile(string filename, int indexId, bool keep)
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// Prepare all intenral variables // Prepare all intenral variables
IniReader ir = filename.GetIniReader(false); IniReader ir = filename.GetIniReader(false);
@@ -401,47 +396,24 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem rom = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them // Check for a "null" item
if (rom.Name == null || rom.Machine.Name == null) datItem = ProcessNullifiedItem(datItem);
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a "null" game (created by DATFromDir or something similar), log it to file // Write out the item if we're not ignoring
if (rom.ItemType == ItemType.Rom if (!ShouldIgnore(datItem, ignoreblanks))
&& (rom as Rom).Size == -1 WriteDatItem(iw, datItem);
&& (rom as Rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
rom.Name = (rom.Name == "null" ? "-" : rom.Name);
(rom as Rom).Size = Constants.SizeZero;
(rom as Rom).CRC = (rom as Rom).CRC == "null" ? Constants.CRCZero : null;
(rom as Rom).MD5 = (rom as Rom).MD5 == "null" ? Constants.MD5Zero : null;
#if NET_FRAMEWORK
(rom as Rom).RIPEMD160 = (rom as Rom).RIPEMD160 == "null" ? Constants.RIPEMD160Zero : null;
#endif
(rom as Rom).SHA1 = (rom as Rom).SHA1 == "null" ? Constants.SHA1Zero : null;
(rom as Rom).SHA256 = (rom as Rom).SHA256 == "null" ? Constants.SHA256Zero : null;
(rom as Rom).SHA384 = (rom as Rom).SHA384 == "null" ? Constants.SHA384Zero : null;
(rom as Rom).SHA512 = (rom as Rom).SHA512 == "null" ? Constants.SHA512Zero : null;
}
// Now, output the rom data
WriteDatItem(iw, rom, ignoreblanks);
// Set the new data to compare against // Set the new data to compare against
lastgame = rom.Machine.Name; lastgame = datItem.Machine.Name;
} }
} }
@@ -500,14 +472,9 @@ namespace SabreTools.Library.DatFiles
/// </summary> /// </summary>
/// <param name="iw">IniWriter to output to</param> /// <param name="iw">IniWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <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> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(IniWriter iw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(IniWriter iw, DatItem datItem)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
/* /*
The rominfo order is as follows: The rominfo order is as follows:
1 - parent name 1 - parent name

View File

@@ -32,13 +32,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile( protected override void ParseFile(string filename, int indexId, bool keep)
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// Prepare all internal variables // Prepare all internal variables
bool empty = true; bool empty = true;
@@ -224,7 +218,8 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
private bool ReadDirectory(XmlReader reader, private bool ReadDirectory(
XmlReader reader,
List<string> parent, List<string> parent,
// Standard Dat parsing // Standard Dat parsing
@@ -610,50 +605,35 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem rom = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them List<string> newsplit = datItem.Machine.Name.Split('\\').ToList();
if (rom.Name == null || rom.Machine.Name == null)
{
Globals.Logger.Warning("Null rom found!");
continue;
}
List<string> newsplit = rom.Machine.Name.Split('\\').ToList();
// If we have a different game and we're not at the start of the list, output the end of last item // If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame != null && lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
depth = WriteEndGame(xtw, splitpath, newsplit, depth, out last); depth = WriteEndGame(xtw, splitpath, newsplit, depth, out last);
// If we have a new game, output the beginning of the new item // If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
depth = WriteStartGame(xtw, rom, newsplit, depth, last); depth = WriteStartGame(xtw, datItem, newsplit, depth, last);
// If we have a "null" game (created by DATFromDir or something similar), log it to file // Check for a "null" item
if (rom.ItemType == ItemType.Rom datItem = ProcessNullifiedItem(datItem);
&& (rom as Rom).Size == -1
&& (rom as Rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
splitpath = newsplit; // Write out the item if we're not ignoring
lastgame = rom.Machine.Name; if (!ShouldIgnore(datItem, ignoreblanks))
continue; WriteDatItem(xtw, datItem, depth);
}
// Now, output the rom data
WriteDatItem(xtw, rom, depth, ignoreblanks);
// Set the new data to compare against // Set the new data to compare against
splitpath = newsplit; splitpath = newsplit;
lastgame = rom.Machine.Name; lastgame = datItem.Machine.Name;
} }
} }
@@ -848,14 +828,9 @@ namespace SabreTools.Library.DatFiles
/// <param name="xtw">XmlTextWriter to output to</param> /// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <param name="depth">Current depth to output file at (SabreDAT only)</param> /// <param name="depth">Current depth to output file at (SabreDAT only)</param>
/// <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> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem, int depth, bool ignoreblanks = false) private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem, int depth)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
try try
{ {
// Pre-process the item name // Pre-process the item name

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using SabreTools.Library.Data; using SabreTools.Library.Data;
@@ -35,13 +36,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile( protected override void ParseFile(string filename, int indexId, bool keep)
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// Open a file reader // Open a file reader
Encoding enc = FileExtensions.GetEncoding(filename); Encoding enc = FileExtensions.GetEncoding(filename);
@@ -134,32 +129,21 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem rom = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them // Check for a "null" item
if (rom.Name == null || rom.Machine.Name == null) datItem = ProcessNullifiedItem(datItem);
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a "null" game (created by DATFromDir or something similar), log it to file // Write out the item if we're not ignoring
if (rom.ItemType == ItemType.Rom if (!ShouldIgnore(datItem, ignoreblanks))
&& (rom as Rom).Size == -1 WriteDatItem(svw, datItem);
&& (rom as Rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
}
// Now, output the rom data
WriteDatItem(svw, rom, ignoreblanks);
} }
} }
@@ -224,14 +208,9 @@ namespace SabreTools.Library.DatFiles
/// </summary> /// </summary>
/// <param name="svw">SeparatedValueWriter to output to</param> /// <param name="svw">SeparatedValueWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <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> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(SeparatedValueWriter svw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(SeparatedValueWriter svw, DatItem datItem)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
try try
{ {
// Separated values should only output Rom and Disk // Separated values should only output Rom and Disk

View File

@@ -32,13 +32,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile( protected override void ParseFile(string filename, int indexId, bool keep)
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// Prepare all internal variables // Prepare all internal variables
XmlReader xtr = filename.GetXmlTextReader(); XmlReader xtr = filename.GetXmlTextReader();
@@ -109,15 +103,7 @@ namespace SabreTools.Library.DatFiles
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
private void ReadSoftware( private void ReadSoftware(XmlReader reader, string filename, int indexId, bool keep)
XmlReader reader,
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{ {
// If we have an empty software, skip it // If we have an empty software, skip it
if (reader == null) if (reader == null)
@@ -339,8 +325,7 @@ namespace SabreTools.Library.DatFiles
List<DatItem> disks = ReadDiskArea( List<DatItem> disks = ReadDiskArea(
reader.ReadSubtree(), reader.ReadSubtree(),
areaname, areaname,
areasize, areasize);
keep);
// If we got valid disks, add them to the list // If we got valid disks, add them to the list
if (disks != null) if (disks != null)
@@ -486,17 +471,8 @@ namespace SabreTools.Library.DatFiles
/// <param name="reader">XmlReader representing a diskarea block</param> /// <param name="reader">XmlReader representing a diskarea block</param>
/// <param name="areaname">Name of the containing area</param> /// <param name="areaname">Name of the containing area</param>
/// <param name="areasize">Size of the containing area</param> /// <param name="areasize">Size of the containing area</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> private List<DatItem> ReadDiskArea(XmlReader reader, string areaname, long? areasize)
private List<DatItem> ReadDiskArea(
XmlReader reader,
string areaname,
long? areasize,
// Miscellaneous
bool keep)
{ {
string key = string.Empty;
string temptype = reader.Name;
List<DatItem> items = new List<DatItem>(); List<DatItem> items = new List<DatItem>();
while (!reader.EOF) while (!reader.EOF)
@@ -620,46 +596,32 @@ namespace SabreTools.Library.DatFiles
// Use a sorted list of games to output // Use a sorted list of games to output
foreach (string key in Items.SortedKeys) foreach (string key in Items.SortedKeys)
{ {
List<DatItem> roms = Items[key]; List<DatItem> datItems = Items.FilteredItems(key);
// Resolve the names in the block // Resolve the names in the block
roms = DatItem.ResolveNames(roms); datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < roms.Count; index++) for (int index = 0; index < datItems.Count; index++)
{ {
DatItem rom = roms[index]; DatItem datItem = datItems[index];
// There are apparently times when a null rom can skip by, skip them
if (rom.Name == null || rom.Machine.Name == null)
{
Globals.Logger.Warning("Null rom found!");
continue;
}
// If we have a different game and we're not at the start of the list, output the end of last item // If we have a different game and we're not at the start of the list, output the end of last item
if (lastgame != null && lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame != null && lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteEndGame(xtw); WriteEndGame(xtw);
// If we have a new game, output the beginning of the new item // If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant()) if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
WriteStartGame(xtw, rom); WriteStartGame(xtw, datItem);
// If we have a "null" game (created by DATFromDir or something similar), log it to file // Check for a "null" item
if (rom.ItemType == ItemType.Rom datItem = ProcessNullifiedItem(datItem);
&& (rom as Rom).Size == -1
&& (rom as Rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
lastgame = rom.Machine.Name; // Write out the item if we're not ignoring
continue; if (!ShouldIgnore(datItem, ignoreblanks))
} WriteDatItem(xtw, datItem);
// Now, output the rom data
WriteDatItem(xtw, rom, ignoreblanks);
// Set the new data to compare against // Set the new data to compare against
lastgame = rom.Machine.Name; lastgame = datItem.Machine.Name;
} }
} }
@@ -820,14 +782,9 @@ namespace SabreTools.Library.DatFiles
/// </summary> /// </summary>
/// <param name="xtw">XmlTextWriter to output to</param> /// <param name="xtw">XmlTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param> /// <param name="datItem">DatItem object to be output</param>
/// <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> /// <returns>True if the data was written, false on error</returns>
private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem, bool ignoreblanks = false) private bool WriteDatItem(XmlTextWriter xtw, DatItem datItem)
{ {
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
try try
{ {
// Pre-process the item name // Pre-process the item name