diff --git a/DATabase/MergeDiff.cs b/DATabase/MergeDiff.cs index e8aa3a3a..19bea00d 100644 --- a/DATabase/MergeDiff.cs +++ b/DATabase/MergeDiff.cs @@ -129,6 +129,7 @@ namespace SabreTools { _logger.Log("Total number of lines in database: " + slc.ExecuteScalar()); } + Output.WriteToDat2(_name + "-db", _desc + "-db", _version, _date, _cat, _author, _forceunpack, _old, "", dbc, _logger); dbc.Close(); // If we're in Alldiff mode, we can only use the first 2 inputs diff --git a/SabreHelper/Output.cs b/SabreHelper/Output.cs index d29464cd..fb68e0aa 100644 --- a/SabreHelper/Output.cs +++ b/SabreHelper/Output.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Mono.Data.Sqlite; using System.IO; using System.Text; using System.Web; @@ -127,6 +128,128 @@ namespace SabreTools.Helper return true; } + /// + /// Create and open an output file for writing + /// + /// Internal name of the DAT + /// Description and external name of the DAT + /// Version or iteration of the DAT + /// Usually the DAT creation date + /// Category of the DAT + /// DAT author + /// Force all sets to be unzipped + /// Set output mode to old-style DAT + /// Set the output directory + /// Database connection representing the roms to be written + /// Logger object for console and/or file output + /// Tru if the DAT was written correctly, false otherwise + public static bool WriteToDat2(string name, string description, string version, string date, string category, string author, bool forceunpack, bool old, string outDir, SqliteConnection dbc, Logger logger) + { + // If it's empty, use the current folder + if (outDir.Trim() == "") + { + outDir = Environment.CurrentDirectory; + } + + // Double check the outdir for the end delim + if (!outDir.EndsWith(Path.DirectorySeparatorChar.ToString())) + { + outDir += Path.DirectorySeparatorChar; + } + + // (currently uses current time, change to "last updated time") + logger.Log("Opening file for writing: " + outDir + description + (old ? ".dat" : ".xml")); + + try + { + FileStream fs = File.Create(outDir + description + (old ? ".dat" : ".xml")); + StreamWriter sw = new StreamWriter(fs, Encoding.UTF8); + + string header_old = "clrmamepro (\n" + + "\tname \"" + HttpUtility.HtmlEncode(name) + "\"\n" + + "\tdescription \"" + HttpUtility.HtmlEncode(description) + "\"\n" + + "\tcategory \"" + HttpUtility.HtmlEncode(category) + "\"\n" + + "\tversion \"" + HttpUtility.HtmlEncode(version) + "\"\n" + + "\tdate \"" + HttpUtility.HtmlEncode(date) + "\"\n" + + "\tauthor \"" + HttpUtility.HtmlEncode(author) + "\"\n" + + (forceunpack ? "\tforcezipping no\n" : "") + + ")\n"; + + string header = "\n" + + "\n\n" + + "\t\n" + + "\t\t
\n" + + "\t\t\t" + HttpUtility.HtmlEncode(name) + "\n" + + "\t\t\t" + HttpUtility.HtmlEncode(description) + "\n" + + "\t\t\t" + HttpUtility.HtmlEncode(category) + "\n" + + "\t\t\t" + HttpUtility.HtmlEncode(version) + "\n" + + "\t\t\t" + HttpUtility.HtmlEncode(date) + "\n" + + "\t\t\t" + HttpUtility.HtmlEncode(author) + "\n" + + (forceunpack ? "\t\t\t\n" : "") + + "\t\t
\n"; + + // Write the header out + sw.Write((old ? header_old : header)); + + // Write out each of the machines and roms + string lastgame = ""; + using (SqliteDataReader sldr = (new SqliteCommand("SELECT * FROM roms ORDER BY game, name", dbc).ExecuteReader())) + { + while (sldr.Read()) + { + string state = ""; + if (lastgame != "" && lastgame != sldr.GetString(1)) + { + state += (old ? ")\n" : "\t\n"); + } + + if (lastgame != sldr.GetString(1)) + { + state += (old ? "game (\n\tname \"" + sldr.GetString(1) + "\"\n" + + "\tdescription \"" + sldr.GetString(1) + "\"\n" : + "\t\n" + + "\t\t" + HttpUtility.HtmlEncode(sldr.GetString(1)) + "\n"); + } + + if (old) + { + state += "\t" + sldr.GetString(3) + " ( name \"" + sldr.GetString(2) + "\"" + + (sldr.GetInt64(6) != 0 ? " size " + sldr.GetInt64(6) : "") + + (sldr.GetString(7) != "" ? " crc " + sldr.GetString(7).ToLowerInvariant() : "") + + (sldr.GetString(8) != "" ? " md5 " + sldr.GetString(8).ToLowerInvariant() : "") + + (sldr.GetString(9) != "" ? " sha1 " + sldr.GetString(9).ToLowerInvariant() : "") + + " )\n"; + } + else + { + state += "\t\t<" + sldr.GetString(3) + " name=\"" + HttpUtility.HtmlEncode(sldr.GetString(2)) + "\"" + + (sldr.GetInt64(6) != -1 ? " size=\"" + sldr.GetInt64(6) + "\"" : "") + + (sldr.GetString(7) != "" ? " crc=\"" + sldr.GetString(7).ToLowerInvariant() + "\"" : "") + + (sldr.GetString(8) != "" ? " md5=\"" + sldr.GetString(8).ToLowerInvariant() + "\"" : "") + + (sldr.GetString(9) != "" ? " sha1=\"" + sldr.GetString(9).ToLowerInvariant() + "\"" : "") + + "/>\n"; + } + + lastgame = sldr.GetString(1); + + sw.Write(state); + } + } + + sw.Write((old ? ")" : "\t\n
")); + logger.Log("File written!" + Environment.NewLine); + sw.Close(); + fs.Close(); + } + catch (Exception ex) + { + logger.Error(ex.ToString()); + return false; + } + + return true; + } + /// /// Output a list of roms as a text file with an arbitrary prefix and postfix /// diff --git a/SabreHelper/RomManipulation.cs b/SabreHelper/RomManipulation.cs index 9c47f69e..586202e0 100644 --- a/SabreHelper/RomManipulation.cs +++ b/SabreHelper/RomManipulation.cs @@ -355,6 +355,7 @@ namespace SabreTools.Helper /// Database connection for adding found ROMs /// Logger object for console and/or file output /// True if no errors occur, false otherwise + /// This doesn't have the same output as Parse + Merge OR even just Parse. Duplicates don't seem to be added either way, why? public static bool Parse2(string filename, int sysid, int srcid, bool merge, SqliteConnection dbc, Logger logger) { XmlTextReader xtr = GetXmlTextReader(filename, logger);