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)
{
// 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;
}
}
}