mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Add generic DAT output to DLL
This commit is contained in:
102
SabreHelper/Output.cs
Normal file
102
SabreHelper/Output.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
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<RomData> 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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
||||
"<!DOCTYPE datafile PUBLIC \"-//Logiqx//DTD ROM Management Datafile//EN\" \"http://www.logiqx.com/Dats/datafile.dtd\">\n\n" +
|
||||
"\t<datafile>\n" +
|
||||
"\t\t<header>\n" +
|
||||
"\t\t\t<name>" + HttpUtility.HtmlEncode(name) + "</name>\n" +
|
||||
"\t\t\t<description>" + HttpUtility.HtmlEncode(description) + "</description>\n" +
|
||||
"\t\t\t<category>" + HttpUtility.HtmlEncode(category) + "/category>\n" +
|
||||
"\t\t\t<version>" + HttpUtility.HtmlEncode(version) + "</version>\n" +
|
||||
"\t\t\t<date>" + HttpUtility.HtmlEncode(date) + "</date>\n" +
|
||||
"\t\t\t<author>" + HttpUtility.HtmlEncode(author) + "</author>\n" +
|
||||
(forceunzip ? "\t\t\t<clrmamepro forcepacking=\"unzip\" />\n" : "") +
|
||||
"\t\t</header>\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</machine>\n");
|
||||
}
|
||||
|
||||
if (lastgame != rom.Game)
|
||||
{
|
||||
state += (old ? "game (\n\tname \"" + rom.Game + "\"\n" +
|
||||
"\tdescription \"" + rom.Game + "\"\n" :
|
||||
"\t<machine name=\"" + HttpUtility.HtmlEncode(rom.Game) + "\">\n" +
|
||||
"\t\t<description>" + HttpUtility.HtmlEncode(rom.Game) + "</description>\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</machine>\n</datafile>"));
|
||||
logger.Log("File written!");
|
||||
sw.Close();
|
||||
fs.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex.ToString());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user