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

@@ -248,11 +248,68 @@ namespace SabreTools
_basePath = (File.Exists(item) ? item : item + Path.DirectorySeparatorChar); _basePath = (File.Exists(item) ? item : item + Path.DirectorySeparatorChar);
_basePath = Path.GetFullPath(_basePath); _basePath = Path.GetFullPath(_basePath);
} }
bool items = false;
foreach (string subitem in Directory.EnumerateFiles(item, "*", SearchOption.AllDirectories)) foreach (string subitem in Directory.EnumerateFiles(item, "*", SearchOption.AllDirectories))
{ {
items = true;
ProcessFile(subitem); 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; _basePath = basePathBackup;
} }

View File

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

View File

@@ -233,93 +233,101 @@ 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
switch (datdata.OutputFormat) if (rom.Name == "null" && rom.Size == -1 && rom.CRC == "null" && rom.MD5 == "null" && rom.SHA1 == "null")
{ {
case OutputFormat.ClrMamePro: logger.Log("Empty folder found: " + rom.Game);
state += "\t" + rom.Type + " ( name \"" + rom.Name + "\"" + }
(rom.Size != 0 ? " size " + rom.Size : "") + // Otherwise, output the rom data
(rom.CRC != "" ? " crc " + rom.CRC.ToLowerInvariant() : "") + else
(rom.MD5 != "" ? " md5 " + rom.MD5.ToLowerInvariant() : "") + {
(rom.SHA1 != "" ? " sha1 " + rom.SHA1.ToLowerInvariant() : "") + switch (datdata.OutputFormat)
(rom.Nodump ? " flags nodump" : "") + {
" )\n"; case OutputFormat.ClrMamePro:
break; state += "\t" + rom.Type + " ( name \"" + rom.Name + "\"" +
case OutputFormat.MissFile: (rom.Size != 0 ? " size " + rom.Size : "") +
string pre = datdata.Prefix + (datdata.Quotes ? "\"" : ""); (rom.CRC != "" ? " crc " + rom.CRC.ToLowerInvariant() : "") +
string post = (datdata.Quotes ? "\"" : "") + datdata.Postfix; (rom.MD5 != "" ? " md5 " + rom.MD5.ToLowerInvariant() : "") +
(rom.SHA1 != "" ? " sha1 " + rom.SHA1.ToLowerInvariant() : "") +
(rom.Nodump ? " flags nodump" : "") +
" )\n";
break;
case OutputFormat.MissFile:
string pre = datdata.Prefix + (datdata.Quotes ? "\"" : "");
string post = (datdata.Quotes ? "\"" : "") + datdata.Postfix;
// If we're in Romba mode, the state is consistent // If we're in Romba mode, the state is consistent
if (datdata.Romba) if (datdata.Romba)
{
// We can only write out if there's a SHA-1
if (rom.SHA1 != "")
{ {
string name = "/" + rom.SHA1.Substring(0, 2) + "/" + rom.SHA1.Substring(2, 2) + "/" + rom.SHA1.Substring(4, 2) + "/" + // We can only write out if there's a SHA-1
rom.SHA1.Substring(6, 2) + "/" + rom.SHA1 + ".gz\n"; if (rom.SHA1 != "")
state += pre + name + post; {
string name = "/" + rom.SHA1.Substring(0, 2) + "/" + rom.SHA1.Substring(2, 2) + "/" + rom.SHA1.Substring(4, 2) + "/" +
rom.SHA1.Substring(6, 2) + "/" + rom.SHA1 + ".gz\n";
state += pre + name + post;
}
} }
} // Otherwise, use any flags
// Otherwise, use any flags else
else
{
string name = (datdata.UseGame ? rom.Game : rom.Name);
if (datdata.RepExt != "")
{ {
string dir = Path.GetDirectoryName(name); string name = (datdata.UseGame ? rom.Game : rom.Name);
dir = (dir.EndsWith(Path.DirectorySeparatorChar.ToString()) ? dir : dir + Path.DirectorySeparatorChar); if (datdata.RepExt != "")
dir = (dir.StartsWith(Path.DirectorySeparatorChar.ToString()) ? dir.Remove(0, 1) : dir); {
name = dir + Path.GetFileNameWithoutExtension(name) + datdata.RepExt; string dir = Path.GetDirectoryName(name);
} dir = (dir.EndsWith(Path.DirectorySeparatorChar.ToString()) ? dir : dir + Path.DirectorySeparatorChar);
if (datdata.AddExt != "") dir = (dir.StartsWith(Path.DirectorySeparatorChar.ToString()) ? dir.Remove(0, 1) : dir);
{ name = dir + Path.GetFileNameWithoutExtension(name) + datdata.RepExt;
name += datdata.AddExt; }
} if (datdata.AddExt != "")
if (!datdata.UseGame && datdata.GameName) {
{ name += datdata.AddExt;
name = (rom.Game.EndsWith(Path.DirectorySeparatorChar.ToString()) ? rom.Game : rom.Game + Path.DirectorySeparatorChar) + name; }
} if (!datdata.UseGame && datdata.GameName)
{
name = (rom.Game.EndsWith(Path.DirectorySeparatorChar.ToString()) ? rom.Game : rom.Game + Path.DirectorySeparatorChar) + name;
}
if (datdata.UseGame && rom.Game != lastgame) if (datdata.UseGame && rom.Game != lastgame)
{ {
state += pre + name + post + "\n"; state += pre + name + post + "\n";
lastgame = rom.Game; lastgame = rom.Game;
}
else if (!datdata.UseGame)
{
state += pre + name + post + "\n";
}
} }
else if (!datdata.UseGame) break;
case OutputFormat.RomCenter:
state += "¬¬¬" + HttpUtility.HtmlEncode(rom.Game) +
"¬" + HttpUtility.HtmlEncode(rom.Game) +
"¬" + HttpUtility.HtmlEncode(rom.Name) +
"¬" + rom.CRC.ToLowerInvariant() +
"¬" + (rom.Size != -1 ? rom.Size.ToString() : "") + "¬¬¬\n";
break;
case OutputFormat.SabreDat:
for (int i = 0; i < depth; i++)
{ {
state += pre + name + post + "\n"; state += "\t";
} }
} state += "<file type=\"" + rom.Type + "\" name=\"" + HttpUtility.HtmlEncode(rom.Name) + "\"" +
break; (rom.Size != -1 ? " size=\"" + rom.Size + "\"" : "") +
case OutputFormat.RomCenter: (rom.CRC != "" ? " crc=\"" + rom.CRC.ToLowerInvariant() + "\"" : "") +
state += "¬¬¬" + HttpUtility.HtmlEncode(rom.Game) + (rom.MD5 != "" ? " md5=\"" + rom.MD5.ToLowerInvariant() + "\"" : "") +
"¬" + HttpUtility.HtmlEncode(rom.Game) + (rom.SHA1 != "" ? " sha1=\"" + rom.SHA1.ToLowerInvariant() + "\"" : "") +
"¬" + HttpUtility.HtmlEncode(rom.Name) + (rom.Nodump ? " status=\"nodump\"" : "") +
"¬" + rom.CRC.ToLowerInvariant() + "/>\n";
"¬" + (rom.Size != -1 ? rom.Size.ToString() : "") + "¬¬¬\n"; break;
break; case OutputFormat.Xml:
case OutputFormat.SabreDat: state += "\t\t<" + rom.Type + " name=\"" + HttpUtility.HtmlEncode(rom.Name) + "\"" +
for (int i = 0; i < depth; i++) (rom.Size != -1 ? " size=\"" + rom.Size + "\"" : "") +
{ (rom.CRC != "" ? " crc=\"" + rom.CRC.ToLowerInvariant() + "\"" : "") +
state += "\t"; (rom.MD5 != "" ? " md5=\"" + rom.MD5.ToLowerInvariant() + "\"" : "") +
} (rom.SHA1 != "" ? " sha1=\"" + rom.SHA1.ToLowerInvariant() + "\"" : "") +
state += "<file type=\"" + rom.Type + "\" name=\"" + HttpUtility.HtmlEncode(rom.Name) + "\"" + (rom.Nodump ? " status=\"nodump\"" : "") +
(rom.Size != -1 ? " size=\"" + rom.Size + "\"" : "") + "/>\n";
(rom.CRC != "" ? " crc=\"" + rom.CRC.ToLowerInvariant() + "\"" : "") + break;
(rom.MD5 != "" ? " md5=\"" + rom.MD5.ToLowerInvariant() + "\"" : "") + }
(rom.SHA1 != "" ? " sha1=\"" + rom.SHA1.ToLowerInvariant() + "\"" : "") +
(rom.Nodump ? " status=\"nodump\"" : "") +
"/>\n";
break;
case OutputFormat.Xml:
state += "\t\t<" + rom.Type + " name=\"" + HttpUtility.HtmlEncode(rom.Name) + "\"" +
(rom.Size != -1 ? " size=\"" + rom.Size + "\"" : "") +
(rom.CRC != "" ? " crc=\"" + rom.CRC.ToLowerInvariant() + "\"" : "") +
(rom.MD5 != "" ? " md5=\"" + rom.MD5.ToLowerInvariant() + "\"" : "") +
(rom.SHA1 != "" ? " sha1=\"" + rom.SHA1.ToLowerInvariant() + "\"" : "") +
(rom.Nodump ? " status=\"nodump\"" : "") +
"/>\n";
break;
} }
splitpath = newsplit; splitpath = newsplit;

View File

@@ -102,7 +102,7 @@ namespace SabreTools.Helper
{ {
// Prepare all internal variables // Prepare all internal variables
XmlReader subreader, headreader, flagreader; 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 = ""; string key = "", crc = "", md5 = "", sha1 = "";
long size = -1; long size = -1;
List<string> parent = new List<string>(); List<string> parent = new List<string>();
@@ -113,16 +113,50 @@ namespace SabreTools.Helper
xtr.MoveToContent(); xtr.MoveToContent();
while (xtr.NodeType != XmlNodeType.None) while (xtr.NodeType != XmlNodeType.None)
{ {
// If we have an end folder element, remove one item from the parent, if possible // 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") && parent.Count > 0) if (xtr.NodeType == XmlNodeType.EndElement && (xtr.Name == "directory" || xtr.Name == "dir"))
{ {
parent.RemoveAt(parent.Count - 1); // If we didn't find any items in the folder, make sure to add the blank rom
if (keep) if (empty)
{ {
datdata.Type = (String.IsNullOrEmpty(datdata.Type) ? "SuperDAT" : datdata.Type); 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 (parent.Count > 0)
{
parent.RemoveAt(parent.Count - 1);
if (keep)
{
datdata.Type = (String.IsNullOrEmpty(datdata.Type) ? "SuperDAT" : datdata.Type);
}
} }
} }
// We only want elements // We only want elements
if (xtr.NodeType != XmlNodeType.Element) if (xtr.NodeType != XmlNodeType.Element)
{ {
@@ -395,6 +429,8 @@ namespace SabreTools.Helper
{ {
case "rom": case "rom":
case "disk": case "disk":
empty = false;
// If the rom is nodump, flag it // If the rom is nodump, flag it
nodump = false; nodump = false;
if (xtr.GetAttribute("flags") == "nodump" || xtr.GetAttribute("status") == "nodump") 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 // Read to next game
if (!xtr.ReadToNextSibling(temptype)) if (!xtr.ReadToNextSibling(temptype))
{ {
@@ -521,6 +588,8 @@ namespace SabreTools.Helper
xtr.Read(); xtr.Read();
break; break;
case "file": case "file":
empty = false;
// If the rom is nodump, flag it // If the rom is nodump, flag it
nodump = false; nodump = false;
flagreader = xtr.ReadSubtree(); flagreader = xtr.ReadSubtree();