diff --git a/DATabase/MergeDiff.cs b/DATabase/MergeDiff.cs index f13dc914..a7e4112a 100644 --- a/DATabase/MergeDiff.cs +++ b/DATabase/MergeDiff.cs @@ -112,6 +112,9 @@ namespace SabreTools Output.WriteToDatFromDb(_name, _desc, _version, _date, _cat, _author, _forceunpack, _old, _diff, _ad, "", dbc, _logger); dbc.Close(); + // Create a dictionary of all ROMs from the input DATs + + return true; } } diff --git a/SabreHelper/Output.cs b/SabreHelper/Output.cs index f4a821d3..9f96b39b 100644 --- a/SabreHelper/Output.cs +++ b/SabreHelper/Output.cs @@ -309,6 +309,131 @@ namespace SabreTools.Helper return true; } + /// + /// Create and open an output file for writing direct from a database + /// + /// 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 + /// Only output files that don't have dupes + /// Enable output of files just in each DAT and also just duped. Best if combined with diff=true. + /// Set the output directory + /// Dictionary containing all 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 WriteToDatFromDict(string name, string description, string version, string date, string category, string author, + bool forceunpack, bool old, bool diff, bool ab, string outDir, Dictionary> dict, 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 = ""; + foreach (string key in dict.Keys) + { + foreach (RomData value in dict[key]) + { + string state = ""; + if (lastgame != "" && lastgame != value.Game) + { + state += (old ? ")\n" : "\t\n"); + } + + if (lastgame != value.Game) + { + state += (old ? "game (\n\tname \"" + value.Game + "\"\n" + + "\tdescription \"" + value.Game + "\"\n" : + "\t\n" + + "\t\t" + HttpUtility.HtmlEncode(value.Game) + "\n"); + } + + if (old) + { + state += "\t" + value.Type + " ( name \"" + value.Name + "\"" + + (value.Size != 0 ? " size " + value.Size : "") + + (value.CRC != "" ? " crc " + value.CRC.ToLowerInvariant() : "") + + (value.MD5 != "" ? " md5 " + value.MD5.ToLowerInvariant() : "") + + (value.SHA1 != "" ? " sha1 " + value.SHA1.ToLowerInvariant() : "") + + " )\n"; + } + else + { + state += "\t\t<" + value.Type + " name=\"" + HttpUtility.HtmlEncode(value.Name) + "\"" + + (value.Size != -1 ? " size=\"" + value.Size + "\"" : "") + + (value.CRC != "" ? " crc=\"" + value.CRC.ToLowerInvariant() + "\"" : "") + + (value.MD5 != "" ? " md5=\"" + value.MD5.ToLowerInvariant() + "\"" : "") + + (value.SHA1 != "" ? " sha1=\"" + value.SHA1.ToLowerInvariant() + "\"" : "") + + "/>\n"; + } + + lastgame = value.Game; + + 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 ///