2016-04-18 13:59:15 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2016-04-27 01:10:24 -07:00
|
|
|
|
using Mono.Data.Sqlite;
|
2016-04-18 13:59:15 -07:00
|
|
|
|
using System.IO;
|
2016-04-28 16:58:59 -07:00
|
|
|
|
using System.Linq;
|
2016-04-18 13:59:15 -07:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Web;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.Helper
|
|
|
|
|
|
{
|
|
|
|
|
|
public class Output
|
|
|
|
|
|
{
|
2016-04-27 01:10:24 -07:00
|
|
|
|
/// <summary>
|
2016-04-28 21:46:57 -07:00
|
|
|
|
/// Create and open an output file for writing direct from a dictionary
|
2016-04-27 01:10:24 -07:00
|
|
|
|
/// </summary>
|
2016-05-15 14:34:06 -07:00
|
|
|
|
/// <param name="datdata">All information for creating the datfile header</param>
|
2016-04-28 10:57:32 -07:00
|
|
|
|
/// <param name="outDir">Set the output directory</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for console and/or file output</param>
|
2016-05-09 09:35:07 -07:00
|
|
|
|
/// <param name="norename">True if games should only be compared on game and file name (default), false if system and source are counted</param>
|
2016-04-28 16:48:14 -07:00
|
|
|
|
/// <returns>True if the DAT was written correctly, false otherwise</returns>
|
2016-05-15 14:02:25 -07:00
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// The following features have been requested for file output:
|
|
|
|
|
|
/// - Have the ability to strip special (non-ASCII) characters from rom information
|
|
|
|
|
|
/// - Add a flag for ignoring roms with blank sizes
|
|
|
|
|
|
/// </remarks>
|
2016-05-16 21:52:49 -07:00
|
|
|
|
public static bool WriteDatfile(DatData datdata, string outDir, Logger logger, bool norename = true)
|
2016-04-28 10:57:32 -07:00
|
|
|
|
{
|
2016-04-28 16:58:59 -07:00
|
|
|
|
// Get all values in the dictionary and write out
|
2016-04-29 13:02:46 -07:00
|
|
|
|
SortedDictionary<string, List<RomData>> sortable = new SortedDictionary<string, List<RomData>>();
|
2016-04-29 11:49:23 -07:00
|
|
|
|
long count = 0;
|
2016-05-16 13:42:21 -07:00
|
|
|
|
foreach (List<RomData> roms in datdata.Roms.Values)
|
2016-04-28 16:58:59 -07:00
|
|
|
|
{
|
2016-05-03 23:59:32 -07:00
|
|
|
|
List<RomData> newroms = roms;
|
2016-05-16 13:42:21 -07:00
|
|
|
|
if (datdata.MergeRoms)
|
2016-05-03 23:59:32 -07:00
|
|
|
|
{
|
|
|
|
|
|
newroms = RomManipulation.Merge(newroms);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (RomData rom in newroms)
|
2016-04-28 16:58:59 -07:00
|
|
|
|
{
|
2016-04-29 11:49:23 -07:00
|
|
|
|
count++;
|
2016-05-09 16:22:47 -07:00
|
|
|
|
string key = (norename ? "" : rom.SystemID.ToString().PadLeft(10, '0') + "-" + rom.SourceID.ToString().PadLeft(10, '0') + "-") + rom.Game.ToLowerInvariant();
|
2016-04-29 13:02:46 -07:00
|
|
|
|
if (sortable.ContainsKey(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
sortable[key].Add(rom);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
List<RomData> temp = new List<RomData>();
|
|
|
|
|
|
temp.Add(rom);
|
|
|
|
|
|
sortable.Add(key, temp);
|
|
|
|
|
|
}
|
2016-04-28 16:58:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-17 00:20:49 -07:00
|
|
|
|
logger.User("A total of " + count + " file hashes will be written out to file");
|
2016-04-28 16:58:59 -07:00
|
|
|
|
|
|
|
|
|
|
// Now write out to file
|
2016-04-29 13:02:46 -07:00
|
|
|
|
// 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-18 14:19:02 -07:00
|
|
|
|
// Create the output directory if it doesn't already exist
|
|
|
|
|
|
Directory.CreateDirectory(outDir);
|
|
|
|
|
|
|
2016-04-29 13:02:46 -07:00
|
|
|
|
// (currently uses current time, change to "last updated time")
|
2016-05-15 20:15:18 -07:00
|
|
|
|
logger.User("Opening file for writing: " + outDir + datdata.Description + (datdata.OutputFormat == OutputFormat.Xml ? ".xml" : ".dat"));
|
2016-04-29 13:02:46 -07:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2016-05-15 20:15:18 -07:00
|
|
|
|
FileStream fs = File.Create(outDir + datdata.Description + (datdata.OutputFormat == OutputFormat.Xml ? ".xml" : ".dat"));
|
2016-04-29 13:02:46 -07:00
|
|
|
|
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
|
|
|
|
|
|
|
2016-05-15 20:42:15 -07:00
|
|
|
|
string header = "";
|
|
|
|
|
|
switch (datdata.OutputFormat)
|
|
|
|
|
|
{
|
|
|
|
|
|
case OutputFormat.ClrMamePro:
|
|
|
|
|
|
header = "clrmamepro (\n" +
|
|
|
|
|
|
"\tname \"" + HttpUtility.HtmlEncode(datdata.Name) + "\"\n" +
|
|
|
|
|
|
"\tdescription \"" + HttpUtility.HtmlEncode(datdata.Description) + "\"\n" +
|
|
|
|
|
|
"\tcategory \"" + HttpUtility.HtmlEncode(datdata.Category) + "\"\n" +
|
|
|
|
|
|
"\tversion \"" + HttpUtility.HtmlEncode(datdata.Version) + "\"\n" +
|
|
|
|
|
|
"\tdate \"" + HttpUtility.HtmlEncode(datdata.Date) + "\"\n" +
|
|
|
|
|
|
"\tauthor \"" + HttpUtility.HtmlEncode(datdata.Author) + "\"\n" +
|
|
|
|
|
|
"\tcomment \"" + HttpUtility.HtmlEncode(datdata.Comment) + "\"\n" +
|
|
|
|
|
|
(datdata.ForcePacking == ForcePacking.Unzip ? "\tforcezipping no\n" : "") +
|
|
|
|
|
|
")\n";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case OutputFormat.RomCenter:
|
|
|
|
|
|
header = "[CREDITS]\n" +
|
|
|
|
|
|
"author=" + HttpUtility.HtmlEncode(datdata.Author) + "\n" +
|
|
|
|
|
|
"version=" + HttpUtility.HtmlEncode(datdata.Version) + "\n" +
|
|
|
|
|
|
"comment=" + HttpUtility.HtmlEncode(datdata.Comment) + "\n" +
|
|
|
|
|
|
"[DAT]\n" +
|
|
|
|
|
|
"version=2.50\n" +
|
|
|
|
|
|
"split=" + (datdata.ForceMerging == ForceMerging.Split ? "1" : "0") + "\n" +
|
|
|
|
|
|
"merge=" + (datdata.ForceMerging == ForceMerging.Full ? "1" : "0") + "\n" +
|
|
|
|
|
|
"[EMULATOR]\n" +
|
|
|
|
|
|
"refname=" + HttpUtility.HtmlEncode(datdata.Name) + "\n" +
|
|
|
|
|
|
"version=" + HttpUtility.HtmlEncode(datdata.Description) + "\n" +
|
|
|
|
|
|
"[GAMES]\n";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case OutputFormat.Xml:
|
|
|
|
|
|
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" +
|
|
|
|
|
|
"<datafile>\n" +
|
|
|
|
|
|
"\t<header>\n" +
|
|
|
|
|
|
"\t\t<name>" + HttpUtility.HtmlEncode(datdata.Name) + "</name>\n" +
|
|
|
|
|
|
"\t\t<description>" + HttpUtility.HtmlEncode(datdata.Description) + "</description>\n" +
|
|
|
|
|
|
"\t\t<category>" + HttpUtility.HtmlEncode(datdata.Category) + "</category>\n" +
|
|
|
|
|
|
"\t\t<version>" + HttpUtility.HtmlEncode(datdata.Version) + "</version>\n" +
|
|
|
|
|
|
"\t\t<date>" + HttpUtility.HtmlEncode(datdata.Date) + "</date>\n" +
|
|
|
|
|
|
"\t\t<author>" + HttpUtility.HtmlEncode(datdata.Author) + "</author>\n" +
|
|
|
|
|
|
"\t\t<comment>" + HttpUtility.HtmlEncode(datdata.Comment) + "</comment>\n" +
|
2016-05-17 11:23:06 -07:00
|
|
|
|
(datdata.Type != null && datdata.Type != "" ? "\t\t<type>" + datdata.Type + "</type>\n" : "") +
|
2016-05-15 20:42:15 -07:00
|
|
|
|
(datdata.ForcePacking == ForcePacking.Unzip ? "\t\t<clrmamepro forcepacking=\"unzip\" />\n" : "") +
|
|
|
|
|
|
"\t</header>\n";
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2016-04-29 13:02:46 -07:00
|
|
|
|
|
|
|
|
|
|
// Write the header out
|
2016-05-15 20:42:15 -07:00
|
|
|
|
sw.Write(header);
|
2016-04-29 13:02:46 -07:00
|
|
|
|
|
|
|
|
|
|
// Write out each of the machines and roms
|
2016-05-09 09:53:56 -07:00
|
|
|
|
string lastgame = null;
|
2016-04-29 13:02:46 -07:00
|
|
|
|
foreach (List<RomData> roms in sortable.Values)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (RomData rom in roms)
|
|
|
|
|
|
{
|
|
|
|
|
|
string state = "";
|
2016-05-09 10:31:15 -07:00
|
|
|
|
if (lastgame != null && lastgame.ToLowerInvariant() != rom.Game.ToLowerInvariant())
|
2016-04-29 13:02:46 -07:00
|
|
|
|
{
|
2016-05-15 20:42:15 -07:00
|
|
|
|
switch (datdata.OutputFormat)
|
|
|
|
|
|
{
|
|
|
|
|
|
case OutputFormat.ClrMamePro:
|
|
|
|
|
|
state += ")\n";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case OutputFormat.Xml:
|
2016-05-16 15:17:11 -07:00
|
|
|
|
state += "\t</machine>\n";
|
2016-05-15 20:42:15 -07:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
2016-04-29 13:02:46 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-09 11:12:26 -07:00
|
|
|
|
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Game.ToLowerInvariant())
|
2016-04-29 13:02:46 -07:00
|
|
|
|
{
|
2016-05-15 20:42:15 -07:00
|
|
|
|
switch (datdata.OutputFormat)
|
|
|
|
|
|
{
|
|
|
|
|
|
case OutputFormat.ClrMamePro:
|
|
|
|
|
|
state += "game (\n\tname \"" + rom.Game + "\"\n" +
|
|
|
|
|
|
"\tdescription \"" + rom.Game + "\"\n";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case OutputFormat.Xml:
|
|
|
|
|
|
state += "\t<machine name=\"" + HttpUtility.HtmlEncode(rom.Game) + "\">\n" +
|
|
|
|
|
|
"\t\t<description>" + HttpUtility.HtmlEncode(rom.Game) + "</description>\n";
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2016-04-29 13:02:46 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-15 20:42:15 -07:00
|
|
|
|
// Now output the rom data
|
|
|
|
|
|
switch (datdata.OutputFormat)
|
2016-04-29 13:02:46 -07:00
|
|
|
|
{
|
2016-05-15 20:42:15 -07:00
|
|
|
|
case OutputFormat.ClrMamePro:
|
|
|
|
|
|
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";
|
|
|
|
|
|
break;
|
2016-05-16 21:52:49 -07:00
|
|
|
|
case OutputFormat.MissFile:
|
|
|
|
|
|
string pre = datdata.Prefix + (datdata.Quotes ? "\"" : "");
|
|
|
|
|
|
string post = (datdata.Quotes ? "\"" : "") + datdata.Postfix;
|
|
|
|
|
|
|
2016-05-16 22:13:59 -07:00
|
|
|
|
// If we're in Romba mode, the state is consistent
|
|
|
|
|
|
if (datdata.Romba)
|
2016-05-16 21:52:49 -07:00
|
|
|
|
{
|
2016-05-16 22:13:59 -07:00
|
|
|
|
// We can only write out if there's a SHA-1
|
|
|
|
|
|
if (rom.SHA1 != "")
|
|
|
|
|
|
{
|
|
|
|
|
|
string name = "/" + rom.SHA1.Substring(0, 2) + "/" + rom.SHA1.Substring(2, 2) + "/" + rom.SHA1.Substring(4, 2) + "/" +
|
2016-05-16 22:14:48 -07:00
|
|
|
|
rom.SHA1.Substring(6, 2) + "/" + rom.SHA1 + ".gz\n";
|
2016-05-16 22:13:59 -07:00
|
|
|
|
state += pre + name + post;
|
|
|
|
|
|
}
|
2016-05-16 21:52:49 -07:00
|
|
|
|
}
|
2016-05-16 22:13:59 -07:00
|
|
|
|
// Otherwise, use any flags
|
|
|
|
|
|
else
|
2016-05-16 21:52:49 -07:00
|
|
|
|
{
|
2016-05-16 22:13:59 -07:00
|
|
|
|
string name = (datdata.UseGame ? rom.Game : rom.Name);
|
|
|
|
|
|
if (datdata.RepExt != "")
|
|
|
|
|
|
{
|
|
|
|
|
|
string dir = Path.GetDirectoryName(name);
|
|
|
|
|
|
dir = (dir.EndsWith(Path.DirectorySeparatorChar.ToString()) ? dir : dir + Path.DirectorySeparatorChar);
|
|
|
|
|
|
dir = (dir.StartsWith(Path.DirectorySeparatorChar.ToString()) ? dir.Remove(0, 1) : dir);
|
|
|
|
|
|
name = dir + Path.GetFileNameWithoutExtension(name) + datdata.RepExt;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (datdata.AddExt != "")
|
|
|
|
|
|
{
|
|
|
|
|
|
name += datdata.AddExt;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!datdata.UseGame && datdata.GameName)
|
|
|
|
|
|
{
|
|
|
|
|
|
name = (rom.Game.EndsWith(Path.DirectorySeparatorChar.ToString()) ? rom.Game : rom.Game + Path.DirectorySeparatorChar) + name;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (datdata.UseGame && rom.Game != lastgame)
|
|
|
|
|
|
{
|
|
|
|
|
|
state += pre + name + post + "\n";
|
|
|
|
|
|
lastgame = rom.Game;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (!datdata.UseGame)
|
|
|
|
|
|
{
|
|
|
|
|
|
state += pre + name + post + "\n";
|
|
|
|
|
|
}
|
2016-05-16 21:52:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
2016-05-15 20:42:15 -07:00
|
|
|
|
case OutputFormat.RomCenter:
|
|
|
|
|
|
state += "¬¬¬" + HttpUtility.HtmlEncode(rom.Game) +
|
|
|
|
|
|
"¬" + HttpUtility.HtmlEncode(rom.Game) +
|
|
|
|
|
|
"¬" + HttpUtility.HtmlEncode(rom.Name) +
|
|
|
|
|
|
"¬" + rom.CRC.ToLowerInvariant() +
|
2016-05-16 15:38:33 -07:00
|
|
|
|
"¬" + (rom.Size != -1 ? rom.Size.ToString() : "") + "¬¬¬\n";
|
2016-05-15 20:42:15 -07:00
|
|
|
|
break;
|
|
|
|
|
|
case OutputFormat.Xml:
|
|
|
|
|
|
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";
|
|
|
|
|
|
break;
|
2016-04-29 13:02:46 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lastgame = rom.Game;
|
|
|
|
|
|
|
|
|
|
|
|
sw.Write(state);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-15 20:42:15 -07:00
|
|
|
|
string footer = "";
|
|
|
|
|
|
switch (datdata.OutputFormat)
|
|
|
|
|
|
{
|
|
|
|
|
|
case OutputFormat.ClrMamePro:
|
|
|
|
|
|
footer = ")";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case OutputFormat.Xml:
|
|
|
|
|
|
footer = "\t</machine>\n</datafile>";
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sw.Write(footer);
|
2016-05-18 15:34:49 -07:00
|
|
|
|
logger.Log("File written!" + Environment.NewLine);
|
2016-04-29 13:02:46 -07:00
|
|
|
|
sw.Close();
|
|
|
|
|
|
fs.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
2016-04-28 10:57:32 -07:00
|
|
|
|
}
|
2016-04-18 13:59:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|