Remove all traces of the outdated DB merge/import

This commit is contained in:
Matt Nadareski
2016-04-28 12:43:56 -07:00
parent 13ba806a59
commit fa519036ad
4 changed files with 0 additions and 429 deletions

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using Mono.Data.Sqlite;
using System.IO;
using SabreTools.Helper;
@@ -100,20 +99,6 @@ namespace SabreTools
_author = "SabreTools";
}
/*
// Create the database of ROMs from the input DATs
SqliteConnection dbc = DBTools.InMemoryDb();
foreach (string input in _inputs)
{
_logger.Log("Adding DAT: " + input);
RomManipulation.ParseDb(input, 0, 0, _dedup, dbc, _logger);
}
// Output all DATs specified by user inputs
Output.WriteToDatFromDb(_name, _desc, _version, _date, _cat, _author, _forceunpack, _old, _diff, _ad, "", dbc, _logger);
dbc.Close();
*/
// Create a dictionary of all ROMs from the input DATs
Dictionary<string, List<RomData>> dict = new Dictionary<string, List<RomData>>();
foreach (string input in _inputs)

View File

@@ -177,35 +177,6 @@ CREATE TABLE IF NOT EXISTS gamesource (
}
}
/// <summary>
/// Create an in-memory database table for import and merging
/// </summary>
/// <returns>An active database connection</returns>
public static SqliteConnection InMemoryDb()
{
SqliteConnection dbc = new SqliteConnection("Data Source=:memory:;Version = 3;");
dbc.Open();
string query = @"
CREATE TABLE IF NOT EXISTS roms (
'id' INTEGER PRIMARY KEY NOT NULL,
'game' TEXT NOT NULL,
'name' TEXT NOT NULL,
'type' TEXT NOT NULL DEFAULT 'rom',
'sysid' INTEGER NOT NULL,
'srcid' INTEGER NOT NULL,
'size' INTEGER NOT NULL DEFAULT -1,
'crc' TEXT NOT NULL,
'md5' TEXT NOT NULL,
'sha1' TEXT NOT NULL,
'dupe' TEXT NOT NULL DEFAULT 'false'
)";
SqliteCommand slc = new SqliteCommand(query, dbc);
slc.ExecuteNonQuery();
return dbc;
}
/// <summary>
/// Add a new source to the database if it doesn't already exist
/// </summary>

View File

@@ -129,186 +129,6 @@ namespace SabreTools.Helper
return true;
}
/// <summary>
/// Create and open an output file for writing direct from a database
/// </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="diff">Only output files that don't have dupes</param>
/// <param name="ab">Enable output of files just in each DAT and also just duped. Best if combined with diff=true.</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 WriteToDatFromDb(string name, string description, string version, string date, string category, string author,
bool forceunpack, bool old, bool diff, bool ab, 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;
}
// Get all query strings to use
List<string> inputs = new List<string>();
inputs.Add("");
if (diff)
{
inputs.Add("<>");
}
if (ab)
{
using (SqliteDataReader sldr = (new SqliteCommand("SELECT DISTINCT dupe FROM roms", dbc).ExecuteReader()))
{
if (sldr.HasRows)
{
while (sldr.Read())
{
inputs.Add(sldr.GetString(0));
}
}
}
}
for (int i = 0; i < inputs.Count; i++)
{
// Set strings to be used internally
string input = inputs[i];
string where = "";
string tempname = name;
string tempdesc = description;
// Get the right value from the input
if (input == "<>")
{
where = "WHERE dupe<>'true'";
input = "diff";
}
else if (input == "true")
{
where = "WHERE dupe='true'";
input = "diff";
}
else if (input != "")
{
where = "WHERE dupe='" + input + "'";
input = Path.GetFileNameWithoutExtension(input);
}
// If we have special outputs, append the right things
if (i != 0)
{
tempname += " (" + input + ")";
tempdesc += " (" + input + ")";
}
// (currently uses current time, change to "last updated time")
logger.Log("Opening file for writing: " + outDir + tempdesc + (old ? ".dat" : ".xml"));
try
{
FileStream fs = File.Create(outDir + tempdesc + (old ? ".dat" : ".xml"));
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
string header_old = "clrmamepro (\n" +
"\tname \"" + HttpUtility.HtmlEncode(tempname) + "\"\n" +
"\tdescription \"" + HttpUtility.HtmlEncode(tempdesc) + "\"\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(tempname) + "</name>\n" +
"\t\t\t<description>" + HttpUtility.HtmlEncode(tempdesc) + "</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 = "";
string query = "SELECT * FROM roms " + where + " ORDER BY game, name";
using (SqliteDataReader sldr = (new SqliteCommand(query, 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>
/// Create and open an output file for writing direct from a database
/// </summary>

View File

@@ -345,211 +345,6 @@ namespace SabreTools.Helper
return roms;
}
/// <summary>
/// Parse a DAT and return all found games and roms within
/// </summary>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="sysid">System ID for the DAT</param>
/// <param name="srcid">Source ID for the DAT</param>
/// <param name="merge">True if files should be matched by hash alone, false otherwise</param>
/// <param name="dbc">Database connection for adding found ROMs</param>
/// <param name="logger">Logger object for console and/or file output</param>
/// <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 ParseDb(string filename, int sysid, int srcid, bool merge, SqliteConnection dbc, Logger logger)
{
XmlTextReader xtr = GetXmlTextReader(filename, logger);
xtr.WhitespaceHandling = WhitespaceHandling.None;
bool superdat = false, shouldbreak = false;
string parent = "";
// If the reader is null, return false
if (xtr == null)
{
return false;
}
xtr.MoveToContent();
while (xtr.NodeType != XmlNodeType.None)
{
// We only want elements
if (xtr.NodeType != XmlNodeType.Element)
{
xtr.Read();
continue;
}
switch (xtr.Name)
{
case "datafile":
case "softwarelist":
parent = xtr.Name;
xtr.Read();
break;
case "header":
xtr.ReadToDescendant("name");
string content = xtr.ReadElementContentAsString();
superdat = (content != null ? content.Contains(" - SuperDAT") : false);
while (xtr.Name != "header")
{
xtr.Read();
}
xtr.Read();
break;
case "machine":
case "game":
case "software":
string temptype = xtr.Name;
string tempname = "";
// We want to process the entire subtree of the game
XmlReader subreader = xtr.ReadSubtree();
if (subreader != null)
{
if (temptype == "software" && subreader.ReadToFollowing("description"))
{
tempname = subreader.ReadElementContentAsString();
}
else
{
// There are rare cases where a malformed XML will not have the required attributes. We can only skip them.
if (xtr.AttributeCount == 0)
{
logger.Error("No attributes were found");
xtr.ReadToNextSibling(xtr.Name);
continue;
}
tempname = xtr.GetAttribute("name");
}
if (superdat)
{
tempname = Regex.Match(tempname, @".*?\\(.*)").Groups[1].Value;
}
while (subreader.Read())
{
// We only want elements
if (subreader.NodeType != XmlNodeType.Element)
{
continue;
}
// Get the roms from the machine
switch (subreader.Name)
{
case "rom":
case "disk":
// Take care of hex-sized files
long size = -1;
if (xtr.GetAttribute("size") != null && xtr.GetAttribute("size").Contains("0x"))
{
size = Convert.ToInt64(xtr.GetAttribute("size"), 16);
}
else if (xtr.GetAttribute("size") != null)
{
Int64.TryParse(xtr.GetAttribute("size"), out size);
}
// If we're in merged mode, check before adding
if (merge)
{
// If the rom doesn't exist, add it to the database
string query = @"SELECT id FROM roms WHERE size=" + size +
(xtr.GetAttribute("crc") != null ? " AND (crc='" + xtr.GetAttribute("crc").ToLowerInvariant().Trim() + "' OR crc='')" : "") +
(xtr.GetAttribute("md5") != null ? " AND (md5='" + xtr.GetAttribute("md5").ToLowerInvariant().Trim() + "' OR md5='')" : "") +
(xtr.GetAttribute("sha1") != null ? " AND (sha1='" + xtr.GetAttribute("sha1").ToLowerInvariant().Trim() + "' OR sha1='')" : "");
using (SqliteCommand slc = new SqliteCommand(query, dbc))
{
using (SqliteDataReader sldr = slc.ExecuteReader())
{
// If there's no returns, then add the file
if (!sldr.HasRows)
{
query = @"INSERT INTO roms
(game, name, type, sysid, srcid, size, crc, md5, sha1, dupe)
VALUES ('" + tempname.Replace("'", "''") + "', '" +
xtr.GetAttribute("name").Replace("'", "''") + "', '" +
xtr.Name + "', " +
sysid + ", " +
srcid + ", " +
size +
(xtr.GetAttribute("crc") != null ? ", '" + xtr.GetAttribute("crc").ToLowerInvariant().Trim() + "'" : ", ''") +
(xtr.GetAttribute("md5") != null ? ", '" + xtr.GetAttribute("md5").ToLowerInvariant().Trim() + "'" : ", ''") +
(xtr.GetAttribute("sha1") != null ? ", '" + xtr.GetAttribute("sha1").ToLowerInvariant().Trim() + "'" : ", ''") +
", '" + filename + "'" +
")";
using (SqliteCommand sslc = new SqliteCommand(query, dbc))
{
sslc.ExecuteNonQueryAsync();
}
}
// Otherwise, set the dupe flag to true
else
{
query = @"UPDATE roms SET dupe='true' WHERE size=" + size +
(xtr.GetAttribute("crc") != null ? " AND crc='" + xtr.GetAttribute("crc").ToLowerInvariant().Trim() + "'" : "") +
(xtr.GetAttribute("md5") != null ? " AND md5='" + xtr.GetAttribute("md5").ToLowerInvariant().Trim() + "'" : "") +
(xtr.GetAttribute("sha1") != null ? " AND sha1='" + xtr.GetAttribute("sha1").ToLowerInvariant().Trim() + "'" : "");
using (SqliteCommand sslc = new SqliteCommand(query, dbc))
{
sslc.ExecuteNonQueryAsync();
}
}
}
}
}
// If we're not in merged mode, just add it
else
{
string query = @"INSERT INTO roms
(game, name, type, sysid, srcid, size, crc, md5, sha1, dupe)
VALUES ('" + tempname.Replace("'", "''") + "', '" +
xtr.GetAttribute("name").Replace("'", "''") + "', '" +
xtr.Name + "', " +
sysid + ", " +
srcid + ", " +
size +
(xtr.GetAttribute("crc") != null ? ", '" + xtr.GetAttribute("crc").ToLowerInvariant().Trim() + "'" : ", ''") +
(xtr.GetAttribute("md5") != null ? ", '" + xtr.GetAttribute("md5").ToLowerInvariant().Trim() + "'" : ", ''") +
(xtr.GetAttribute("sha1") != null ? ", '" + xtr.GetAttribute("sha1").ToLowerInvariant().Trim() + "'" : ", ''") +
", '" + filename + "'" +
")";
using (SqliteCommand sslc = new SqliteCommand(query, dbc))
{
sslc.ExecuteNonQueryAsync();
}
}
break;
}
}
}
// Read to next game
if (!xtr.ReadToNextSibling(temptype))
{
shouldbreak = true;
}
break;
default:
xtr.Read();
break;
}
// If we hit an endpoint, break out of the loop early
if (shouldbreak)
{
break;
}
}
return true;
}
/// <summary>
/// Parse a DAT and return all found games and roms within
/// </summary>