Add DAT name prefixing

This commit is contained in:
Matt Nadareski
2016-04-20 15:30:56 -07:00
parent 24d81761f8
commit f82152e0a6
4 changed files with 92 additions and 8 deletions

View File

@@ -54,6 +54,7 @@ namespace SabreTools
convertMiss = false, convertMiss = false,
convertRV = false, convertRV = false,
convertXml = false, convertXml = false,
datname = false,
disableForce = false, disableForce = false,
extsplit = false, extsplit = false,
generate = false, generate = false,
@@ -113,6 +114,10 @@ namespace SabreTools
case "--disable-force": case "--disable-force":
disableForce = true; disableForce = true;
break; break;
case "-dp":
case "--dat-prefix":
datname = true;
break;
case "-es": case "-es":
case "--ext-split": case "--ext-split":
extsplit = true; extsplit = true;
@@ -294,7 +299,7 @@ namespace SabreTools
{ {
foreach (string input in inputs) foreach (string input in inputs)
{ {
InitConvertMiss(input, usegame, prefix, postfix, quotes, repext, addext); InitConvertMiss(input, usegame, prefix, postfix, quotes, repext, addext, datname);
} }
} }
@@ -880,7 +885,7 @@ or 'b' to go back to the previous menu:
private static void ConvertMissMenu() private static void ConvertMissMenu()
{ {
string selection = "", input = "", prefix = "", postfix = "", addext = "", repext = ""; string selection = "", input = "", prefix = "", postfix = "", addext = "", repext = "";
bool usegame = true, quotes = false; bool usegame = true, quotes = false, datname = false;
while (selection.ToLowerInvariant() != "b") while (selection.ToLowerInvariant() != "b")
{ {
Console.Clear(); Console.Clear();
@@ -896,7 +901,8 @@ Make a selection:
5) " + (quotes ? "Don't add quotes around each item" : "Add quotes around each item") + @" 5) " + (quotes ? "Don't add quotes around each item" : "Add quotes around each item") + @"
6) Replace all extensions with another" + (repext != "" ? ":\t" + repext : "") + @" 6) Replace all extensions with another" + (repext != "" ? ":\t" + repext : "") + @"
7) Add extensions to each item" + (addext != "" ? ":\n\t" + addext : "") + @" 7) Add extensions to each item" + (addext != "" ? ":\n\t" + addext : "") + @"
8) Begin conversion 8) " + (datname ? "Don't add dat name before every item" : "Add dat name before every item") + @"
9) Begin conversion
B) Go back to the previous menu B) Go back to the previous menu
"); ");
Console.Write("Enter selection: "); Console.Write("Enter selection: ");
@@ -935,8 +941,11 @@ Make a selection:
postfix = Console.ReadLine(); postfix = Console.ReadLine();
break; break;
case "8": case "8":
datname = !datname;
break;
case "9":
Console.Clear(); Console.Clear();
InitConvertMiss(input, usegame, prefix, postfix, quotes, repext, addext); InitConvertMiss(input, usegame, prefix, postfix, quotes, repext, addext, datname);
Console.Write("\nPress any key to continue..."); Console.Write("\nPress any key to continue...");
Console.ReadKey(); Console.ReadKey();
break; break;
@@ -954,7 +963,8 @@ Make a selection:
/// <param name="quotes">Add quotes to each item</param> /// <param name="quotes">Add quotes to each item</param>
/// <param name="repext">Replace all extensions with another</param> /// <param name="repext">Replace all extensions with another</param>
/// <param name="addext">Add an extension to all items</param> /// <param name="addext">Add an extension to all items</param>
private static void InitConvertMiss(string input, bool usegame, string prefix, string postfix, bool quotes, string repext, string addext) /// <param name="datname">Add the dat name as a directory prefix</param>
private static void InitConvertMiss(string input, bool usegame, string prefix, string postfix, bool quotes, string repext, string addext, bool datname)
{ {
// Strip any quotations from the name // Strip any quotations from the name
input = input.Replace("\"", ""); input = input.Replace("\"", "");
@@ -967,9 +977,16 @@ Make a selection:
// Get the output name // Get the output name
string name = Path.GetFileNameWithoutExtension(input) + "-miss.txt"; string name = Path.GetFileNameWithoutExtension(input) + "-miss.txt";
// Get the DAT name string
string datstring = "";
if (datname)
{
datstring = RomManipulation.GetDatName(input, logger);
}
// Read in the roms from the DAT and then write them to the file // Read in the roms from the DAT and then write them to the file
logger.Log("Converting " + input); logger.Log("Converting " + input);
Output.WriteToText(name, Path.GetDirectoryName(input), RomManipulation.Parse(input, 0, 0, logger), logger, usegame, prefix, postfix, addext, repext, quotes); Output.WriteToText(name, Path.GetDirectoryName(input), RomManipulation.Parse(input, 0, 0, logger), logger, usegame, prefix, postfix, addext, repext, quotes, datstring);
logger.Log(input + " converted to: " + name); logger.Log(input + " converted to: " + name);
return; return;
} }

View File

@@ -89,6 +89,7 @@ Options:
url= URL (source only) url= URL (source only)
-cm, --convert-miss -cm, --convert-miss
-r, --roms Output roms to miss instead of sets -r, --roms Output roms to miss instead of sets
-dp, --dat-prefix Add dat name as a prefix to each item
-pre=, --prefix= Set prefix to be printed in front of all lines -pre=, --prefix= Set prefix to be printed in front of all lines
-post=, --postfix= Set postfix to be printed behind all lines -post=, --postfix= Set postfix to be printed behind all lines
-q, --quotes Put double-quotes around each item -q, --quotes Put double-quotes around each item

View File

@@ -139,9 +139,10 @@ namespace SabreTools.Helper
/// <param name="quotes">True if quotes should be put around the item, false otherwise (default)</param> /// <param name="quotes">True if quotes should be put around the item, false otherwise (default)</param>
/// <param name="addext">Arbitrary extension added to the end of each item</param> /// <param name="addext">Arbitrary extension added to the end of each item</param>
/// <param name="repext">Arbitrary extension to replace all extensions in the item</param> /// <param name="repext">Arbitrary extension to replace all extensions in the item</param>
/// <param name="datname">The internal name of the DAT to prefix</param>
/// <returns>True if the file was written, false otherwise</returns> /// <returns>True if the file was written, false otherwise</returns>
public static bool WriteToText(string textfile, string outdir, List<RomData> roms, Logger logger, bool useGame = true, string prefix = "", public static bool WriteToText(string textfile, string outdir, List<RomData> roms, Logger logger, bool useGame = true, string prefix = "",
string postfix = "", string addext = "", string repext = "", bool quotes = false) string postfix = "", string addext = "", string repext = "", bool quotes = false, string datname = "")
{ {
// Normalize the output directory // Normalize the output directory
if (outdir == "") if (outdir == "")
@@ -153,6 +154,12 @@ namespace SabreTools.Helper
outdir += Path.DirectorySeparatorChar; outdir += Path.DirectorySeparatorChar;
} }
// Get the DAT name, if applicable
if (datname != "" && !datname.EndsWith(Path.DirectorySeparatorChar.ToString()))
{
datname = datname + Path.DirectorySeparatorChar;
}
// Make the output directory if it doesn't exist // Make the output directory if it doesn't exist
if (!Directory.Exists(outdir)) if (!Directory.Exists(outdir))
{ {
@@ -175,7 +182,7 @@ namespace SabreTools.Helper
{ {
string pre = prefix + (quotes ? "\"" : ""); string pre = prefix + (quotes ? "\"" : "");
string post = (quotes ? "\"" : "") + postfix; string post = (quotes ? "\"" : "") + postfix;
string name = (useGame ? rom.Game : rom.Name); string name = datname + (useGame ? rom.Game : rom.Name);
if (repext != "") if (repext != "")
{ {
string dir = Path.GetDirectoryName(name); string dir = Path.GetDirectoryName(name);

View File

@@ -28,6 +28,65 @@ namespace SabreTools.Helper
return true; return true;
} }
/// <summary>
/// Get the name of the DAT for external use
/// </summary>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="logger">Logger object for console and file output</param>
/// <returns>The internal name of the DAT on success, empty string otherwise</returns>
public static string GetDatName(string filename, Logger logger)
{
string name = "";
XmlDocument doc = new XmlDocument();
try
{
doc.LoadXml(File.ReadAllText(filename));
}
catch (XmlException)
{
try
{
doc.LoadXml(Converters.RomVaultToXML(File.ReadAllLines(filename)).ToString());
}
catch (Exception ex)
{
logger.Error(ex.ToString());
return name;
}
}
catch (Exception ex)
{
logger.Error(ex.ToString());
return name;
}
// Experimental looping using only XML parsing
XmlNode node = doc.FirstChild;
if (node != null && node.Name == "xml")
{
// Skip over everything that's not an element
while (node.NodeType != XmlNodeType.Element)
{
node = node.NextSibling;
}
}
// Once we find the main body, enter it
if (node != null && (node.Name == "datafile" || node.Name == "softwarelist"))
{
node = node.FirstChild;
}
// Get the name from the header
if (node != null && node.Name == "header")
{
name = node.SelectSingleNode("name").InnerText;
}
return name;
}
/// <summary> /// <summary>
/// Parse a DAT and return all found games and roms within /// Parse a DAT and return all found games and roms within
/// </summary> /// </summary>