using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Web; namespace SabreTools.Helper { public class Output { /// /// 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 /// List of RomData objects representing the games to be written out /// Logger object for console and/or file output /// Tru if the DAT was written correctly, false otherwise 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) { // 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" + "\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) + "\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; } /// /// Output a list of roms as a text file with an arbitrary prefix and postfix /// /// Name of the output file /// List of RomData objects representing the roms to be output /// Logger object for console and/or file output /// True if only games are written to text file (default), false for files only /// Arbitrary string to prefix each line /// Arbitrary string to postfix each line /// True if the file was written, false otherwise public static bool WriteToText(string textfile, List roms, Logger logger, bool useGame = true, string prefix = "", string postfix = "") { logger.Log("Opening file for writing: " + textfile); try { FileStream fs = File.Create(textfile); StreamWriter sw = new StreamWriter(fs, Encoding.UTF8); string lastgame = ""; foreach (RomData rom in roms) { if (useGame && rom.Game != lastgame) { sw.WriteLine(prefix + rom.Game + postfix); lastgame = rom.Game; } else if (!useGame) { sw.WriteLine(prefix + rom.Name + postfix); } } logger.Log("File written!" + Environment.NewLine); sw.Close(); fs.Close(); } catch (Exception ex) { logger.Error(ex.ToString()); return false; } return true; } /// /// Convert a List of RomData objects to a List of tab-deliminated strings /// /// List of RomData objects representing the roms to be parsed /// List of Strings representing the roms public static List RomDataToString(List roms) { List outlist = new List(); foreach (RomData rom in roms) { outlist.Add(rom.Manufacturer + "\t" + rom.System + "\t" + rom.SystemID + "\t" + rom.Source + "\t" + rom.URL + "\t" + rom.SourceID + "\t" + rom.Game + "\t" + rom.Name + "\t" + rom.Type + "\t" + rom.Size + "\t" + rom.CRC + "\t" + rom.MD5 + "\t" + rom.SHA1); } return outlist; } /// /// Convert a List of tab-deliminated strings objects to a List of RomData objects /// /// List of Strings representing the roms to be parsed /// List of RomData objects representing the roms public static List StringToRomData(List roms) { List outlist = new List(); foreach (String rom in roms) { string[] temp = rom.Split('\t'); try { outlist.Add(new RomData { Manufacturer = temp[0], System = temp[1], SystemID = Int32.Parse(temp[2]), Source = temp[3], URL = temp[4], SourceID = Int32.Parse(temp[5]), Game = temp[6], Name = temp[7], Type = temp[8], Size = Int64.Parse(temp[9]), CRC = temp[10], MD5 = temp[11], SHA1 = temp[12], }); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } return outlist; } } }