Stage 2 changes for using in-mem database

This change makes it mostly functional, having a way to both parse and merge in one step and output from the database. The issue is that the output from Parse2 doesn't match Parse OR Parse + Merge. Duplicates don't seem to be added in the first case and there are a differing number of roms in the second.
This commit is contained in:
Matt Nadareski
2016-04-27 01:10:24 -07:00
parent bce1cc5839
commit e450eebfd7
3 changed files with 125 additions and 0 deletions

View File

@@ -129,6 +129,7 @@ namespace SabreTools
{ {
_logger.Log("Total number of lines in database: " + slc.ExecuteScalar()); _logger.Log("Total number of lines in database: " + slc.ExecuteScalar());
} }
Output.WriteToDat2(_name + "-db", _desc + "-db", _version, _date, _cat, _author, _forceunpack, _old, "", dbc, _logger);
dbc.Close(); dbc.Close();
// If we're in Alldiff mode, we can only use the first 2 inputs // If we're in Alldiff mode, we can only use the first 2 inputs

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Mono.Data.Sqlite;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Web; using System.Web;
@@ -127,6 +128,128 @@ namespace SabreTools.Helper
return true; return true;
} }
/// <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>
/// <param name="forceunpack">Force all sets to be unzipped</param>
/// <param name="old">Set output mode to old-style DAT</param>
/// <param name="outDir">Set the output directory</param>
/// <param name="dbc">Database connection representing the roms to be written</param>
/// <param name="logger">Logger object for console and/or file output</param>
/// <returns>Tru if the DAT was written correctly, false otherwise</returns>
public static bool WriteToDat2(string name, string description, string version, string date, string category, string author, bool forceunpack, bool old, string outDir, SqliteConnection dbc, 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" +
"\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" +
"\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" +
(forceunpack ? "\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 = "";
using (SqliteDataReader sldr = (new SqliteCommand("SELECT * FROM roms ORDER BY game, name", dbc).ExecuteReader()))
{
while (sldr.Read())
{
string state = "";
if (lastgame != "" && lastgame != sldr.GetString(1))
{
state += (old ? ")\n" : "\t</machine>\n");
}
if (lastgame != sldr.GetString(1))
{
state += (old ? "game (\n\tname \"" + sldr.GetString(1) + "\"\n" +
"\tdescription \"" + sldr.GetString(1) + "\"\n" :
"\t<machine name=\"" + HttpUtility.HtmlEncode(sldr.GetString(1)) + "\">\n" +
"\t\t<description>" + HttpUtility.HtmlEncode(sldr.GetString(1)) + "</description>\n");
}
if (old)
{
state += "\t" + sldr.GetString(3) + " ( name \"" + sldr.GetString(2) + "\"" +
(sldr.GetInt64(6) != 0 ? " size " + sldr.GetInt64(6) : "") +
(sldr.GetString(7) != "" ? " crc " + sldr.GetString(7).ToLowerInvariant() : "") +
(sldr.GetString(8) != "" ? " md5 " + sldr.GetString(8).ToLowerInvariant() : "") +
(sldr.GetString(9) != "" ? " sha1 " + sldr.GetString(9).ToLowerInvariant() : "") +
" )\n";
}
else
{
state += "\t\t<" + sldr.GetString(3) + " name=\"" + HttpUtility.HtmlEncode(sldr.GetString(2)) + "\"" +
(sldr.GetInt64(6) != -1 ? " size=\"" + sldr.GetInt64(6) + "\"" : "") +
(sldr.GetString(7) != "" ? " crc=\"" + sldr.GetString(7).ToLowerInvariant() + "\"" : "") +
(sldr.GetString(8) != "" ? " md5=\"" + sldr.GetString(8).ToLowerInvariant() + "\"" : "") +
(sldr.GetString(9) != "" ? " sha1=\"" + sldr.GetString(9).ToLowerInvariant() + "\"" : "") +
"/>\n";
}
lastgame = sldr.GetString(1);
sw.Write(state);
}
}
sw.Write((old ? ")" : "\t</machine>\n</datafile>"));
logger.Log("File written!" + Environment.NewLine);
sw.Close();
fs.Close();
}
catch (Exception ex)
{
logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary> /// <summary>
/// Output a list of roms as a text file with an arbitrary prefix and postfix /// Output a list of roms as a text file with an arbitrary prefix and postfix
/// </summary> /// </summary>

View File

@@ -355,6 +355,7 @@ namespace SabreTools.Helper
/// <param name="dbc">Database connection for adding found ROMs</param> /// <param name="dbc">Database connection for adding found ROMs</param>
/// <param name="logger">Logger object for console and/or file output</param> /// <param name="logger">Logger object for console and/or file output</param>
/// <returns>True if no errors occur, false otherwise</returns> /// <returns>True if no errors occur, false otherwise</returns>
/// <remarks>This doesn't have the same output as Parse + Merge OR even just Parse. Duplicates don't seem to be added either way, why?</remarks>
public static bool Parse2(string filename, int sysid, int srcid, bool merge, SqliteConnection dbc, Logger logger) public static bool Parse2(string filename, int sysid, int srcid, bool merge, SqliteConnection dbc, Logger logger)
{ {
XmlTextReader xtr = GetXmlTextReader(filename, logger); XmlTextReader xtr = GetXmlTextReader(filename, logger);