using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Web; namespace SabreTools.Helper { public class Output { public static bool WriteToDat(string name, string description, string version, string date, string category, string author, bool forceunzip, bool old, string outDir, List roms, Logger logger) { // Create and open an output file for writing (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" + "\tversion \"" + HttpUtility.HtmlEncode(version) + "\"\n" + "\tcomment \"\"\n" + "\tauthor \"" + HttpUtility.HtmlEncode(author) + "\"\n" + (forceunzip ? "\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) + "/category>\n" + "\t\t\t" + HttpUtility.HtmlEncode(version) + "\n" + "\t\t\t" + HttpUtility.HtmlEncode(date) + "\n" + "\t\t\t" + HttpUtility.HtmlEncode(author) + "\n" + (forceunzip ? "\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 (RomData rom in roms) { string state = ""; if (lastgame != "" && lastgame != rom.Game) { state += (old ? ")\n" : "\t\n"); } if (lastgame != rom.Game) { state += (old ? "game (\n\tname \"" + rom.Game + "\"\n" + "\tdescription \"" + rom.Game + "\"\n" : "\t\n" + "\t\t" + HttpUtility.HtmlEncode(rom.Game) + "\n"); } if (old) { state += "\t" + rom.Type + " ( name \"" + rom.Name + "\"" + (rom.Size != 0 ? " size " + rom.Size : "") + (rom.CRC != "" ? " crc " + rom.CRC.ToLowerInvariant() : "") + (rom.MD5 != "" ? " md5 " + rom.MD5.ToLowerInvariant() : "") + (rom.SHA1 != "" ? " sha1 " + rom.SHA1.ToLowerInvariant() : "") + " )\n"; } else { 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() + "\"" : "") + " />\n"; } lastgame = rom.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; } } }