diff --git a/DATabase/MergeDiff.cs b/DATabase/MergeDiff.cs index 8e9b764f..d6de9999 100644 --- a/DATabase/MergeDiff.cs +++ b/DATabase/MergeDiff.cs @@ -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> dict = new Dictionary>(); foreach (string input in _inputs) diff --git a/SabreHelper/DBTools.cs b/SabreHelper/DBTools.cs index 4d130486..a01ce247 100644 --- a/SabreHelper/DBTools.cs +++ b/SabreHelper/DBTools.cs @@ -177,35 +177,6 @@ CREATE TABLE IF NOT EXISTS gamesource ( } } - /// - /// Create an in-memory database table for import and merging - /// - /// An active database connection - 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; - } - /// /// Add a new source to the database if it doesn't already exist /// diff --git a/SabreHelper/Output.cs b/SabreHelper/Output.cs index 41123b4c..a5909810 100644 --- a/SabreHelper/Output.cs +++ b/SabreHelper/Output.cs @@ -129,186 +129,6 @@ namespace SabreTools.Helper return true; } - /// - /// Create and open an output file for writing direct from a database - /// - /// Internal name of the DAT - /// Description and external name of the DAT - /// Version or iteration of the DAT - /// Usually the DAT creation date - /// Category of the DAT - /// DAT author - /// Force all sets to be unzipped - /// Set output mode to old-style DAT - /// Only output files that don't have dupes - /// Enable output of files just in each DAT and also just duped. Best if combined with diff=true. - /// Set the output directory - /// Database connection representing the roms to be written - /// Logger object for console and/or file output - /// Tru if the DAT was written correctly, false otherwise - 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 inputs = new List(); - 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 = "\n" + - "\n\n" + - "\t\n" + - "\t\t
\n" + - "\t\t\t" + HttpUtility.HtmlEncode(tempname) + "\n" + - "\t\t\t" + HttpUtility.HtmlEncode(tempdesc) + "\n" + - "\t\t\t" + HttpUtility.HtmlEncode(category) + "\n" + - "\t\t\t" + HttpUtility.HtmlEncode(version) + "\n" + - "\t\t\t" + HttpUtility.HtmlEncode(date) + "\n" + - "\t\t\t" + HttpUtility.HtmlEncode(author) + "\n" + - (forceunpack ? "\t\t\t\n" : "") + - "\t\t
\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\n"); - } - - if (lastgame != sldr.GetString(1)) - { - state += (old ? "game (\n\tname \"" + sldr.GetString(1) + "\"\n" + - "\tdescription \"" + sldr.GetString(1) + "\"\n" : - "\t\n" + - "\t\t" + HttpUtility.HtmlEncode(sldr.GetString(1)) + "\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\n
")); - logger.Log("File written!" + Environment.NewLine); - sw.Close(); - fs.Close(); - } - catch (Exception ex) - { - logger.Error(ex.ToString()); - return false; - } - } - - return true; - } - /// /// Create and open an output file for writing direct from a database /// diff --git a/SabreHelper/RomManipulation.cs b/SabreHelper/RomManipulation.cs index acc9fe58..d1c1c17c 100644 --- a/SabreHelper/RomManipulation.cs +++ b/SabreHelper/RomManipulation.cs @@ -345,211 +345,6 @@ namespace SabreTools.Helper return roms; } - /// - /// Parse a DAT and return all found games and roms within - /// - /// Name of the file to be parsed - /// System ID for the DAT - /// Source ID for the DAT - /// True if files should be matched by hash alone, false otherwise - /// Database connection for adding found ROMs - /// Logger object for console and/or file output - /// True if no errors occur, false otherwise - /// This doesn't have the same output as Parse + Merge OR even just Parse. Duplicates don't seem to be added either way, why? - 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; - } - /// /// Parse a DAT and return all found games and roms within ///