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-18 15:12:30 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create and open an output file for writing
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name">Internal name of the DAT</param>
|
|
|
|
|
|
/// <param name="description">Description and external name of the DAT</param>
|
|
|
|
|
|
/// <param name="version">Version or iteration of the DAT</param>
|
|
|
|
|
|
/// <param name="date">Usually the DAT creation date</param>
|
|
|
|
|
|
/// <param name="category">Category of the DAT</param>
|
|
|
|
|
|
/// <param name="author">DAT author</param>
|
2016-04-20 21:17:23 -07:00
|
|
|
|
/// <param name="forceunpack">Force all sets to be unzipped</param>
|
2016-04-18 15:12:30 -07:00
|
|
|
|
/// <param name="old">Set output mode to old-style DAT</param>
|
|
|
|
|
|
/// <param name="outDir">Set the output directory</param>
|
|
|
|
|
|
/// <param name="roms">List of RomData objects representing the games to be written out</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for console and/or file output</param>
|
2016-04-18 15:41:58 -07:00
|
|
|
|
/// <returns>Tru if the DAT was written correctly, false otherwise</returns>
|
2016-04-27 16:57:03 -07:00
|
|
|
|
public static bool WriteToDat(string name, string description, string version, string date, string category, string author,
|
|
|
|
|
|
bool forceunpack, bool old, string outDir, List<RomData> roms, Logger logger)
|
2016-04-18 13:59:15 -07:00
|
|
|
|
{
|
2016-04-19 12:10:21 -07:00
|
|
|
|
// If it's empty, use the current folder
|
|
|
|
|
|
if (outDir.Trim() == "")
|
|
|
|
|
|
{
|
|
|
|
|
|
outDir = Environment.CurrentDirectory;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-18 15:33:36 -07:00
|
|
|
|
// Double check the outdir for the end delim
|
|
|
|
|
|
if (!outDir.EndsWith(Path.DirectorySeparatorChar.ToString()))
|
|
|
|
|
|
{
|
|
|
|
|
|
outDir += Path.DirectorySeparatorChar;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-18 15:12:30 -07:00
|
|
|
|
// (currently uses current time, change to "last updated time")
|
2016-05-10 15:41:33 -07:00
|
|
|
|
logger.User("Opening file for writing: " + outDir + description + (old ? ".dat" : ".xml"));
|
2016-04-18 13:59:15 -07:00
|
|
|
|
|
|
|
|
|
|
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" +
|
2016-04-21 20:14:29 -07:00
|
|
|
|
"\tcategory \"" + HttpUtility.HtmlEncode(category) + "\"\n" +
|
2016-04-18 13:59:15 -07:00
|
|
|
|
"\tversion \"" + HttpUtility.HtmlEncode(version) + "\"\n" +
|
2016-04-21 20:14:29 -07:00
|
|
|
|
"\tdate \"" + HttpUtility.HtmlEncode(date) + "\"\n" +
|
2016-04-18 13:59:15 -07:00
|
|
|
|
"\tauthor \"" + HttpUtility.HtmlEncode(author) + "\"\n" +
|
2016-04-20 21:17:23 -07:00
|
|
|
|
(forceunpack ? "\tforcezipping no\n" : "") +
|
2016-04-18 13:59:15 -07:00
|
|
|
|
")\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" +
|
2016-04-28 20:25:28 -07:00
|
|
|
|
"<datafile>\n" +
|
|
|
|
|
|
"\t<header>\n" +
|
|
|
|
|
|
"\t\t<name>" + HttpUtility.HtmlEncode(name) + "</name>\n" +
|
|
|
|
|
|
"\t\t<description>" + HttpUtility.HtmlEncode(description) + "</description>\n" +
|
|
|
|
|
|
"\t\t<category>" + HttpUtility.HtmlEncode(category) + "</category>\n" +
|
|
|
|
|
|
"\t\t<version>" + HttpUtility.HtmlEncode(version) + "</version>\n" +
|
|
|
|
|
|
"\t\t<date>" + HttpUtility.HtmlEncode(date) + "</date>\n" +
|
|
|
|
|
|
"\t\t<author>" + HttpUtility.HtmlEncode(author) + "</author>\n" +
|
|
|
|
|
|
(forceunpack ? "\t\t<clrmamepro forcepacking=\"unzip\" />\n" : "") +
|
|
|
|
|
|
"\t</header>\n";
|
2016-04-18 13:59:15 -07:00
|
|
|
|
|
|
|
|
|
|
// Write the header out
|
|
|
|
|
|
sw.Write((old ? header_old : header));
|
|
|
|
|
|
|
|
|
|
|
|
// Write out each of the machines and roms
|
2016-05-08 20:57:14 -07:00
|
|
|
|
string lastgame = null;
|
2016-04-18 13:59:15 -07:00
|
|
|
|
foreach (RomData rom in roms)
|
|
|
|
|
|
{
|
|
|
|
|
|
string state = "";
|
2016-05-08 20:57:14 -07:00
|
|
|
|
if (lastgame != null && lastgame != rom.Game)
|
2016-04-18 13:59:15 -07:00
|
|
|
|
{
|
|
|
|
|
|
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 + "\"" +
|
2016-05-06 13:12:00 -07:00
|
|
|
|
(rom.Size != -1 &&
|
|
|
|
|
|
rom.Size != 0 &&
|
|
|
|
|
|
rom.CRC != RomManipulation.CRCZero &&
|
|
|
|
|
|
rom.MD5 != RomManipulation.MD5Zero &&
|
|
|
|
|
|
rom.SHA1 != RomManipulation.SHA1Zero ? " size " + rom.Size : "") +
|
2016-04-18 13:59:15 -07:00
|
|
|
|
(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) + "\"" +
|
2016-05-06 13:12:00 -07:00
|
|
|
|
(rom.Size != -1 &&
|
|
|
|
|
|
rom.Size != 0 &&
|
|
|
|
|
|
rom.CRC != RomManipulation.CRCZero &&
|
|
|
|
|
|
rom.MD5 != RomManipulation.MD5Zero &&
|
|
|
|
|
|
rom.SHA1 != RomManipulation.SHA1Zero ? " size=\"" + rom.Size + "\"" : "") +
|
2016-04-18 13:59:15 -07:00
|
|
|
|
(rom.CRC != "" ? " crc=\"" + rom.CRC.ToLowerInvariant() + "\"" : "") +
|
|
|
|
|
|
(rom.MD5 != "" ? " md5=\"" + rom.MD5.ToLowerInvariant() + "\"" : "") +
|
|
|
|
|
|
(rom.SHA1 != "" ? " sha1=\"" + rom.SHA1.ToLowerInvariant() + "\"" : "") +
|
2016-04-19 15:05:46 -07:00
|
|
|
|
"/>\n";
|
2016-04-18 13:59:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lastgame = rom.Game;
|
|
|
|
|
|
|
|
|
|
|
|
sw.Write(state);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sw.Write((old ? ")" : "\t</machine>\n</datafile>"));
|
2016-05-10 15:41:33 -07:00
|
|
|
|
logger.User("File written!" + Environment.NewLine);
|
2016-04-18 13:59:15 -07:00
|
|
|
|
sw.Close();
|
|
|
|
|
|
fs.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2016-04-18 23:04:03 -07:00
|
|
|
|
|
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>
|
|
|
|
|
|
/// <param name="name">Internal name of the DAT</param>
|
|
|
|
|
|
/// <param name="description">Description and external name of the DAT</param>
|
|
|
|
|
|
/// <param name="version">Version or iteration of the DAT</param>
|
|
|
|
|
|
/// <param name="date">Usually the DAT creation date</param>
|
|
|
|
|
|
/// <param name="category">Category of the DAT</param>
|
|
|
|
|
|
/// <param name="author">DAT author</param>
|
|
|
|
|
|
/// <param name="forceunpack">Force all sets to be unzipped</param>
|
|
|
|
|
|
/// <param name="old">Set output mode to old-style DAT</param>
|
2016-04-28 11:06:27 -07:00
|
|
|
|
/// <param name="merge">Enable output in merged mode (one game per hash)</param>
|
2016-04-28 10:57:32 -07:00
|
|
|
|
/// <param name="outDir">Set the output directory</param>
|
|
|
|
|
|
/// <param name="dict">Dictionary containing all the roms to be written</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 add a comment field
|
|
|
|
|
|
/// - Have the ability to strip special (non-ASCII) characters from rom information
|
|
|
|
|
|
/// - Add a flag for ignoring roms with blank sizes
|
|
|
|
|
|
/// </remarks>
|
2016-04-28 10:57:32 -07:00
|
|
|
|
public static bool WriteToDatFromDict(string name, string description, string version, string date, string category, string author,
|
2016-05-09 09:35:07 -07:00
|
|
|
|
bool forceunpack, bool old, bool merge, string outDir, Dictionary<string, List<RomData>> dict, 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;
|
|
|
|
|
|
foreach (List<RomData> roms in dict.Values)
|
2016-04-28 16:58:59 -07:00
|
|
|
|
{
|
2016-05-03 23:59:32 -07:00
|
|
|
|
List<RomData> newroms = roms;
|
|
|
|
|
|
if (merge)
|
|
|
|
|
|
{
|
|
|
|
|
|
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-04 12:41:18 -07:00
|
|
|
|
logger.Log("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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// (currently uses current time, change to "last updated time")
|
2016-05-10 15:41:33 -07:00
|
|
|
|
logger.User("Opening file for writing: " + outDir + description + (old ? ".dat" : ".xml"));
|
2016-04-29 13:02:46 -07:00
|
|
|
|
|
|
|
|
|
|
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" +
|
|
|
|
|
|
"\tcategory \"" + HttpUtility.HtmlEncode(category) + "\"\n" +
|
|
|
|
|
|
"\tversion \"" + HttpUtility.HtmlEncode(version) + "\"\n" +
|
|
|
|
|
|
"\tdate \"" + HttpUtility.HtmlEncode(date) + "\"\n" +
|
|
|
|
|
|
"\tauthor \"" + HttpUtility.HtmlEncode(author) + "\"\n" +
|
|
|
|
|
|
(forceunpack ? "\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" +
|
|
|
|
|
|
"<datafile>\n" +
|
|
|
|
|
|
"\t<header>\n" +
|
|
|
|
|
|
"\t\t<name>" + HttpUtility.HtmlEncode(name) + "</name>\n" +
|
|
|
|
|
|
"\t\t<description>" + HttpUtility.HtmlEncode(description) + "</description>\n" +
|
|
|
|
|
|
"\t\t<category>" + HttpUtility.HtmlEncode(category) + "</category>\n" +
|
|
|
|
|
|
"\t\t<version>" + HttpUtility.HtmlEncode(version) + "</version>\n" +
|
|
|
|
|
|
"\t\t<date>" + HttpUtility.HtmlEncode(date) + "</date>\n" +
|
|
|
|
|
|
"\t\t<author>" + HttpUtility.HtmlEncode(author) + "</author>\n" +
|
|
|
|
|
|
(forceunpack ? "\t\t<clrmamepro forcepacking=\"unzip\" />\n" : "") +
|
|
|
|
|
|
"\t</header>\n";
|
|
|
|
|
|
|
|
|
|
|
|
// Write the header out
|
|
|
|
|
|
sw.Write((old ? header_old : header));
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
{
|
|
|
|
|
|
state += (old ? ")\n" : "\t</machine>\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-09 11:12:26 -07:00
|
|
|
|
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Game.ToLowerInvariant())
|
2016-04-29 13:02:46 -07:00
|
|
|
|
{
|
|
|
|
|
|
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>"));
|
2016-05-10 15:41:33 -07:00
|
|
|
|
logger.User("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 23:04:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Output a list of roms as a text file with an arbitrary prefix and postfix
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="textfile">Name of the output file</param>
|
2016-04-20 12:00:16 -07:00
|
|
|
|
/// <param name="outdir">Output directory for the miss file</param>
|
2016-04-18 23:04:03 -07:00
|
|
|
|
/// <param name="roms">List of RomData objects representing the roms to be output</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for console and/or file output</param>
|
|
|
|
|
|
/// <param name="useGame">True if only games are written to text file (default), false for files only</param>
|
|
|
|
|
|
/// <param name="prefix">Arbitrary string to prefix each line</param>
|
|
|
|
|
|
/// <param name="postfix">Arbitrary string to postfix each line</param>
|
2016-04-20 12:15:57 -07:00
|
|
|
|
/// <param name="quotes">True if quotes should be put around the item, false otherwise (default)</param>
|
2016-04-20 13:41:01 -07:00
|
|
|
|
/// <param name="addext">Arbitrary extension added to the end of each item</param>
|
|
|
|
|
|
/// <param name="repext">Arbitrary extension to replace all extensions in the item</param>
|
2016-04-20 17:45:39 -07:00
|
|
|
|
/// <param name="gamename">True if the game name is appended (only when !usegame), false otherwise</param>
|
2016-04-18 23:04:03 -07:00
|
|
|
|
/// <returns>True if the file was written, false otherwise</returns>
|
2016-05-15 14:02:25 -07:00
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// The following features have been requested for this method:
|
|
|
|
|
|
/// - Have switch for automatically outputting to Romba format:
|
|
|
|
|
|
/// e.g. /aa/bb/cc/dd/aabbccddef770b06131a878b46d4302ac28dd126.gz
|
|
|
|
|
|
/// Anything without a SHA-1 has to be skipped
|
|
|
|
|
|
/// </remarks>
|
2016-04-20 13:20:50 -07:00
|
|
|
|
public static bool WriteToText(string textfile, string outdir, List<RomData> roms, Logger logger, bool useGame = true, string prefix = "",
|
2016-04-20 17:45:39 -07:00
|
|
|
|
string postfix = "", string addext = "", string repext = "", bool quotes = false, bool gamename = false)
|
2016-04-18 23:04:03 -07:00
|
|
|
|
{
|
2016-04-20 12:00:16 -07:00
|
|
|
|
// Normalize the output directory
|
|
|
|
|
|
if (outdir == "")
|
|
|
|
|
|
{
|
|
|
|
|
|
outdir = Environment.CurrentDirectory;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!outdir.EndsWith(Path.DirectorySeparatorChar.ToString()))
|
|
|
|
|
|
{
|
|
|
|
|
|
outdir += Path.DirectorySeparatorChar;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Make the output directory if it doesn't exist
|
|
|
|
|
|
if (!Directory.Exists(outdir))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(outdir);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-20 13:41:01 -07:00
|
|
|
|
// Normalize the extensions
|
2016-04-20 13:56:05 -07:00
|
|
|
|
addext = (addext == "" || addext.StartsWith(".") ? addext : "." + addext);
|
|
|
|
|
|
repext = (repext == "" || repext.StartsWith(".") ? repext : "." + repext);
|
2016-04-20 13:41:01 -07:00
|
|
|
|
|
2016-05-10 15:41:33 -07:00
|
|
|
|
logger.User("Opening file for writing: " + outdir + textfile);
|
2016-04-18 23:04:03 -07:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2016-04-20 12:00:16 -07:00
|
|
|
|
FileStream fs = File.Create(outdir + textfile);
|
2016-04-18 23:04:03 -07:00
|
|
|
|
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
|
|
|
|
|
|
|
|
|
|
|
|
string lastgame = "";
|
|
|
|
|
|
foreach (RomData rom in roms)
|
|
|
|
|
|
{
|
2016-04-20 13:20:50 -07:00
|
|
|
|
string pre = prefix + (quotes ? "\"" : "");
|
|
|
|
|
|
string post = (quotes ? "\"" : "") + postfix;
|
2016-04-20 17:45:39 -07:00
|
|
|
|
string name = (useGame ? rom.Game : rom.Name);
|
2016-04-20 13:20:50 -07:00
|
|
|
|
if (repext != "")
|
|
|
|
|
|
{
|
|
|
|
|
|
string dir = Path.GetDirectoryName(name);
|
|
|
|
|
|
dir = (dir.EndsWith(Path.DirectorySeparatorChar.ToString()) ? dir : dir + Path.DirectorySeparatorChar);
|
2016-04-20 13:41:01 -07:00
|
|
|
|
dir = (dir.StartsWith(Path.DirectorySeparatorChar.ToString()) ? dir.Remove(0, 1) : dir);
|
2016-04-20 13:20:50 -07:00
|
|
|
|
name = dir + Path.GetFileNameWithoutExtension(name) + repext;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (addext != "")
|
|
|
|
|
|
{
|
|
|
|
|
|
name += addext;
|
|
|
|
|
|
}
|
2016-04-20 17:45:39 -07:00
|
|
|
|
if (!useGame && gamename)
|
|
|
|
|
|
{
|
|
|
|
|
|
name = (rom.Game.EndsWith(Path.DirectorySeparatorChar.ToString()) ? rom.Game : rom.Game + Path.DirectorySeparatorChar) + name;
|
|
|
|
|
|
}
|
2016-04-20 13:20:50 -07:00
|
|
|
|
|
2016-04-18 23:04:03 -07:00
|
|
|
|
if (useGame && rom.Game != lastgame)
|
|
|
|
|
|
{
|
2016-04-20 13:41:01 -07:00
|
|
|
|
sw.WriteLine(pre + name + post);
|
2016-04-18 23:04:03 -07:00
|
|
|
|
lastgame = rom.Game;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (!useGame)
|
|
|
|
|
|
{
|
2016-04-20 13:41:01 -07:00
|
|
|
|
sw.WriteLine(pre + name + post);
|
2016-04-18 23:04:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-10 15:41:33 -07:00
|
|
|
|
logger.User("File written!" + Environment.NewLine);
|
2016-04-18 23:04:03 -07:00
|
|
|
|
sw.Close();
|
|
|
|
|
|
fs.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2016-04-19 16:39:17 -07:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Convert a List of RomData objects to a List of tab-deliminated strings
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="roms">List of RomData objects representing the roms to be parsed</param>
|
|
|
|
|
|
/// <returns>List of Strings representing the roms</returns>
|
|
|
|
|
|
public static List<String> RomDataToString(List<RomData> roms)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<String> outlist = new List<String>();
|
|
|
|
|
|
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);
|
2016-04-26 13:17:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
return outlist;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Convert a List of RomData objects' hash information to a List of tab-deliminated strings
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="roms">List of RomData objects representing the roms to be parsed</param>
|
|
|
|
|
|
/// <returns>List of Strings representing the rom hashes</returns>
|
|
|
|
|
|
public static List<String> HashDataToString(List<RomData> roms)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<String> outlist = new List<String>();
|
|
|
|
|
|
foreach (RomData rom in roms)
|
|
|
|
|
|
{
|
|
|
|
|
|
outlist.Add(rom.Size + "\t" +
|
|
|
|
|
|
rom.CRC + "\t" +
|
|
|
|
|
|
rom.MD5 + "\t" +
|
|
|
|
|
|
rom.SHA1);
|
2016-04-19 16:39:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
return outlist;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Convert a List of tab-deliminated strings objects to a List of RomData objects
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="roms">List of Strings representing the roms to be parsed</param>
|
|
|
|
|
|
/// <returns>List of RomData objects representing the roms</returns>
|
|
|
|
|
|
public static List<RomData> StringToRomData(List<String> roms)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<RomData> outlist = new List<RomData>();
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
2016-04-18 13:59:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|