Handle blank folders in DATFromDir and output alike

This commit is contained in:
Matt Nadareski
2016-05-19 10:28:53 -07:00
parent b10e5e9502
commit 41063921f2
4 changed files with 222 additions and 86 deletions

View File

@@ -249,10 +249,67 @@ namespace SabreTools
_basePath = Path.GetFullPath(_basePath);
}
bool items = false;
foreach (string subitem in Directory.EnumerateFiles(item, "*", SearchOption.AllDirectories))
{
items = true;
ProcessFile(subitem);
}
// If there were no subitems, add a "blank" game to to the set
if (!items)
{
RomData rom = new RomData
{
Name = "null",
Game = item.Remove(0, basePathBackup.Length),
Size = -1,
CRC = "null",
MD5 = "null",
SHA1 = "null",
};
string key = rom.Size + "-" + rom.CRC;
if (_dict.ContainsKey(key))
{
_dict[key].Add(rom);
}
else
{
List<RomData> temp = new List<RomData>();
temp.Add(rom);
_dict.Add(key, temp);
}
}
// Now scour subdirectories for empties and add those as well
foreach (string subdir in Directory.EnumerateDirectories(item, "*", SearchOption.AllDirectories))
{
if (Directory.EnumerateFiles(subdir, "*", SearchOption.AllDirectories).Count() == 0)
{
RomData rom = new RomData
{
Name = "null",
Game = subdir.Remove(0, basePathBackup.Length),
Size = -1,
CRC = "null",
MD5 = "null",
SHA1 = "null",
};
string key = rom.Size + "-" + rom.CRC;
if (_dict.ContainsKey(key))
{
_dict[key].Add(rom);
}
else
{
List<RomData> temp = new List<RomData>();
temp.Add(rom);
_dict.Add(key, temp);
}
}
}
}
_basePath = basePathBackup;
}

View File

@@ -28,7 +28,7 @@ namespace SabreTools.Helper
{
XElement elem = new XElement("datafile");
bool block = false;
bool block = false, romfound = false;
for (int k = 0; k < filecontents.Length; k++)
{
string line = filecontents[k];
@@ -61,6 +61,7 @@ namespace SabreTools.Helper
// If the line is a rom or disk and we're in a block
else if ((line.Trim().StartsWith("rom (") || line.Trim().StartsWith("disk (")) && block)
{
romfound = true;
string[] gc = line.Trim().Split(' ');
XElement temp = new XElement(gc[0]);
@@ -151,6 +152,7 @@ namespace SabreTools.Helper
// If we find an end bracket that's not associated with anything else, the block is done
else if (Regex.IsMatch(line, _endPatternCMP) && block)
{
romfound = false;
block = false;
elem = elem.Parent;
}

View File

@@ -233,7 +233,14 @@ namespace SabreTools.Helper
}
}
// Now output the rom data
// If we have a "null" game (created by DATFromDir or something similar), skip trying to add a rom
if (rom.Name == "null" && rom.Size == -1 && rom.CRC == "null" && rom.MD5 == "null" && rom.SHA1 == "null")
{
logger.Log("Empty folder found: " + rom.Game);
}
// Otherwise, output the rom data
else
{
switch (datdata.OutputFormat)
{
case OutputFormat.ClrMamePro:
@@ -321,6 +328,7 @@ namespace SabreTools.Helper
"/>\n";
break;
}
}
splitpath = newsplit;
lastgame = rom.Game;

View File

@@ -102,7 +102,7 @@ namespace SabreTools.Helper
{
// Prepare all internal variables
XmlReader subreader, headreader, flagreader;
bool superdat = false, shouldbreak = false, nodump = false;
bool superdat = false, shouldbreak = false, nodump = false, empty = true;
string key = "", crc = "", md5 = "", sha1 = "";
long size = -1;
List<string> parent = new List<string>();
@@ -113,8 +113,40 @@ namespace SabreTools.Helper
xtr.MoveToContent();
while (xtr.NodeType != XmlNodeType.None)
{
// If we're ending a folder or game, take care of possibly empty games and removing from the parent
if (xtr.NodeType == XmlNodeType.EndElement && (xtr.Name == "directory" || xtr.Name == "dir"))
{
// If we didn't find any items in the folder, make sure to add the blank rom
if (empty)
{
RomData rom = new RomData
{
Name = "null",
Game = String.Join("\\", parent),
Size = -1,
CRC = "null",
MD5 = "null",
SHA1 = "null",
};
key = rom.Size + "-" + rom.CRC;
if (datdata.Roms.ContainsKey(key))
{
datdata.Roms[key].Add(rom);
}
else
{
List<RomData> temp = new List<RomData>();
temp.Add(rom);
datdata.Roms.Add(key, temp);
}
}
// Regardless, end the current folder
empty = true;
// If we have an end folder element, remove one item from the parent, if possible
if (xtr.NodeType == XmlNodeType.EndElement && (xtr.Name == "directory" || xtr.Name == "dir") && parent.Count > 0)
if (parent.Count > 0)
{
parent.RemoveAt(parent.Count - 1);
if (keep)
@@ -122,6 +154,8 @@ namespace SabreTools.Helper
datdata.Type = (String.IsNullOrEmpty(datdata.Type) ? "SuperDAT" : datdata.Type);
}
}
}
// We only want elements
if (xtr.NodeType != XmlNodeType.Element)
@@ -395,6 +429,8 @@ namespace SabreTools.Helper
{
case "rom":
case "disk":
empty = false;
// If the rom is nodump, flag it
nodump = false;
if (xtr.GetAttribute("flags") == "nodump" || xtr.GetAttribute("status") == "nodump")
@@ -498,6 +534,37 @@ namespace SabreTools.Helper
}
}
// If we didn't find any items in the folder, make sure to add the blank rom
if (empty)
{
tempname = (parent.Count > 0 ? String.Join("\\", parent) + Path.DirectorySeparatorChar : "") + tempname;
RomData rom = new RomData
{
Name = "null",
Game = tempname,
Size = -1,
CRC = "null",
MD5 = "null",
SHA1 = "null",
};
key = rom.Size + "-" + rom.CRC;
if (datdata.Roms.ContainsKey(key))
{
datdata.Roms[key].Add(rom);
}
else
{
List<RomData> temp = new List<RomData>();
temp.Add(rom);
datdata.Roms.Add(key, temp);
}
}
// Regardless, end the current folder
empty = true;
// Read to next game
if (!xtr.ReadToNextSibling(temptype))
{
@@ -521,6 +588,8 @@ namespace SabreTools.Helper
xtr.Read();
break;
case "file":
empty = false;
// If the rom is nodump, flag it
nodump = false;
flagreader = xtr.ReadSubtree();