diff --git a/RombaSharp/Partials/RombaSharp_Helpers.cs b/RombaSharp/Partials/RombaSharp_Helpers.cs index a364c30d..090ee1be 100644 --- a/RombaSharp/Partials/RombaSharp_Helpers.cs +++ b/RombaSharp/Partials/RombaSharp_Helpers.cs @@ -314,110 +314,97 @@ namespace SabreTools // Populate the List from the database string query = "SELECT UNIQUE value FROM data WHERE key=\"dat\""; - using (SqliteConnection dbc = new SqliteConnection(_connectionString)) + SqliteConnection dbc = new SqliteConnection(_connectionString); + SqliteCommand slc = new SqliteCommand(query, dbc); + SqliteDataReader sldr = slc.ExecuteReader(); + if (sldr.HasRows) { - using (SqliteCommand slc = new SqliteCommand(query, dbc)) + sldr.Read(); + string hash = sldr.GetString(0); + if (!databaseDats.Contains(hash)) { - using (SqliteDataReader sldr = slc.ExecuteReader()) + databaseDats.Add(hash); + } + } + + slc.Dispose(); + sldr.Dispose(); + + // Now create a Dictionary of dats to parse from what's not in the database (SHA-1, Path) + Dictionary toscan = new Dictionary(); + + // Loop through the datroot and add only needed files + foreach (string file in Directory.EnumerateFiles(_dats, "*", SearchOption.AllDirectories)) + { + Rom dat = FileTools.GetSingleFileInfo(file); + + // If the Dat isn't in the database and isn't already accounted for in the DatRoot, add it + if (!databaseDats.Contains(dat.SHA1) && !toscan.ContainsKey(dat.SHA1)) + { + toscan.Add(dat.SHA1, Path.GetFullPath(file)); + } + + // If the Dat is in the database already, remove it to find stragglers + else if (databaseDats.Contains(dat.SHA1)) + { + databaseDats.Remove(dat.SHA1); + } + } + + // Loop through the Dictionary and add all data + foreach (string key in toscan.Keys) + { + // Parse the Dat if possible + DatFile tempdat = new DatFile(); + DatFile.Parse(toscan[key], 0, 0, ref tempdat, _logger); + + // If the Dat wasn't empty, add the information + if (tempdat.Files.Count != 0) + { + // Loop through the parsed entries + foreach (string romkey in tempdat.Files.Keys) { - if (sldr.HasRows) + foreach (Rom rom in tempdat.Files[romkey]) { - sldr.Read(); - string hash = sldr.GetString(0); - if (!databaseDats.Contains(hash)) + query = "SELECT id FROM data WHERE key=\"size\" AND value=\"" + rom.Size + "\" AND (" + + "(key=\"crc\" AND (value=\"" + rom.CRC + "\" OR value=\"null\"))" + + "AND (key=\"md5\" AND value=\"" + rom.MD5 + "\" OR value=\"null\"))" + + "AND (key=\"sha1\" AND value=\"" + rom.SHA1 + "\" OR value=\"null\")))"; + slc = new SqliteCommand(query, dbc); + sldr = slc.ExecuteReader(); + + // If the hash exists in the database, add the dat hash for that id + if (sldr.HasRows) { - databaseDats.Add(hash); + sldr.Read(); + string id = sldr.GetString(0); + + string squery = "INSERT INTO data (id, key, value) VALUES (\"" + id + "\", \"dat\", \"" + key + "\")"; + SqliteCommand sslc = new SqliteCommand(squery, dbc); + sslc.ExecuteNonQuery(); + sslc.Dispose(); } - } - } - } - // Now create a Dictionary of dats to parse from what's not in the database (SHA-1, Path) - Dictionary toscan = new Dictionary(); - - // Loop through the datroot and add only needed files - foreach (string file in Directory.EnumerateFiles(_dats, "*", SearchOption.AllDirectories)) - { - Rom dat = FileTools.GetSingleFileInfo(file); - - // If the Dat isn't in the database and isn't already accounted for in the DatRoot, add it - if (!databaseDats.Contains(dat.SHA1) && !toscan.ContainsKey(dat.SHA1)) - { - toscan.Add(dat.SHA1, Path.GetFullPath(file)); - } - - // If the Dat is in the database already, remove it to find stragglers - else if (databaseDats.Contains(dat.SHA1)) - { - databaseDats.Remove(dat.SHA1); - } - } - - // Loop through the Dictionary and add all data - foreach (string key in toscan.Keys) - { - // Parse the Dat if possible - DatFile tempdat = new DatFile(); - DatFile.Parse(toscan[key], 0, 0, ref tempdat, _logger); - - // If the Dat wasn't empty, add the information - if (tempdat.Files.Count != 0) - { - // Loop through the parsed entries - foreach (string romkey in tempdat.Files.Keys) - { - foreach (Rom rom in tempdat.Files[romkey]) + // If it doesn't exist, add the hash and the dat hash for a new id + else { - query = "SELECT id FROM data WHERE key=\"size\" AND value=\"" + rom.Size + "\" AND (" - + "(key=\"crc\" AND (value=\"" + rom.CRC + "\" OR value=\"null\"))" - + "AND (key=\"md5\" AND value=\"" + rom.MD5 + "\" OR value=\"null\"))" - + "AND (key=\"sha1\" AND value=\"" + rom.SHA1 + "\" OR value=\"null\")))"; - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If the hash exists in the database, add the dat hash for that id - if (sldr.HasRows) - { - sldr.Read(); - string id = sldr.GetString(0); + string squery = "INSERT INTO data (key, value) VALUES (\"size\", \"" + rom.Size + "\")"; + SqliteCommand sslc = new SqliteCommand(squery, dbc); + sslc.ExecuteNonQuery(); - string squery = "INSERT INTO data (id, key, value) VALUES (\"" + id + "\", \"dat\", \"" + key + "\")"; - using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) - { - sslc.ExecuteNonQuery(); - } - } + long id = -1; - // If it doesn't exist, add the hash and the dat hash for a new id - else - { - string squery = "INSERT INTO data (key, value) VALUES (\"size\", \"" + rom.Size + "\")"; - using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) - { - sslc.ExecuteNonQuery(); - } + squery = "SELECT last_insertConstants.Rowid()"; + sslc = new SqliteCommand(squery, dbc); + id = (long)sslc.ExecuteScalar(); - long id = -1; - - squery = "SELECT last_insertConstants.Rowid()"; - using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) - { - id = (long)sslc.ExecuteScalar(); - } - - squery = "INSERT INTO data (id, key, value) VALUES (\"" + id + "\", \"crc\", \"" + rom.CRC + "\")," - + " (\"" + id + "\", \"md5\", \"" + rom.MD5 + "\")," - + " (\"" + id + "\", \"sha1\", \"" + rom.SHA1 + "\")," - + " (\"" + id + "\", \"dat\", \"" + key + "\")," - + " (\"" + id + "\", \"exists\", \"false\")"; - using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) - { - sslc.ExecuteNonQuery(); - } - } - } - } + squery = "INSERT INTO data (id, key, value) VALUES (\"" + id + "\", \"crc\", \"" + rom.CRC + "\")," + + " (\"" + id + "\", \"md5\", \"" + rom.MD5 + "\")," + + " (\"" + id + "\", \"sha1\", \"" + rom.SHA1 + "\")," + + " (\"" + id + "\", \"dat\", \"" + key + "\")," + + " (\"" + id + "\", \"exists\", \"false\")"; + sslc = new SqliteCommand(squery, dbc); + sslc.ExecuteNonQuery(); } } } @@ -428,10 +415,9 @@ namespace SabreTools foreach (string dathash in databaseDats) { query = "DELETE FROM data WHERE key=\"dat\" AND value=\"" + dathash + "\""; - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - slc.ExecuteNonQuery(); - } + slc = new SqliteCommand(query, dbc); + slc.ExecuteNonQuery(); + slc.Dispose(); } } } diff --git a/SabreTools.Helper/Objects/Archive/ZipFileEntry.cs b/SabreTools.Helper/Objects/Archive/ZipFileEntry.cs index bd942845..f98b2f3e 100644 --- a/SabreTools.Helper/Objects/Archive/ZipFileEntry.cs +++ b/SabreTools.Helper/Objects/Archive/ZipFileEntry.cs @@ -982,32 +982,37 @@ namespace SabreTools.Helper return; } - // Now get the hash of the stream + // Create the hashers uint tempCrc; - using (OptimizedCRC crc = new OptimizedCRC()) - using (MD5 md5 = System.Security.Cryptography.MD5.Create()) - using (SHA1 sha1 = System.Security.Cryptography.SHA1.Create()) + OptimizedCRC crc = new OptimizedCRC(); + MD5 md5 = System.Security.Cryptography.MD5.Create(); + SHA1 sha1 = System.Security.Cryptography.SHA1.Create(); + + // Now get the hash of the stream + BinaryReader fs = new BinaryReader(stream); + + byte[] buffer = new byte[1024]; + int read; + while ((read = fs.Read(buffer, 0, buffer.Length)) > 0) { - BinaryReader fs = new BinaryReader(stream); - - byte[] buffer = new byte[1024]; - int read; - while ((read = fs.Read(buffer, 0, buffer.Length)) > 0) - { - crc.Update(buffer, 0, read); - md5.TransformBlock(buffer, 0, read, buffer, 0); - sha1.TransformBlock(buffer, 0, read, buffer, 0); - } - - crc.Update(buffer, 0, 0); - md5.TransformFinalBlock(buffer, 0, 0); - sha1.TransformFinalBlock(buffer, 0, 0); - - tempCrc = crc.UnsignedValue; - _md5 = md5.Hash; - _sha1 = sha1.Hash; + crc.Update(buffer, 0, read); + md5.TransformBlock(buffer, 0, read, buffer, 0); + sha1.TransformBlock(buffer, 0, read, buffer, 0); } + crc.Update(buffer, 0, 0); + md5.TransformFinalBlock(buffer, 0, 0); + sha1.TransformFinalBlock(buffer, 0, 0); + + tempCrc = crc.UnsignedValue; + _md5 = md5.Hash; + _sha1 = sha1.Hash; + + // Dispose of the hashers + crc.Dispose(); + md5.Dispose(); + sha1.Dispose(); + if (_compressionMethod == CompressionMethod.Deflated) { stream.Close(); diff --git a/SabreTools.Helper/Objects/Headerer.cs b/SabreTools.Helper/Objects/Headerer.cs index 1c194cb6..5dc38641 100644 --- a/SabreTools.Helper/Objects/Headerer.cs +++ b/SabreTools.Helper/Objects/Headerer.cs @@ -149,18 +149,14 @@ namespace SabreTools { bool exists = false; + // Open the database connection + SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString); + dbc.Open(); + string query = @"SELECT * FROM data WHERE sha1='" + SHA1 + "' AND header='" + header + "'"; - using (SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - exists = sldr.HasRows; - } - } - } + SqliteCommand slc = new SqliteCommand(query, dbc); + SqliteDataReader sldr = slc.ExecuteReader(); + exists = sldr.HasRows; if (!exists) { @@ -168,15 +164,14 @@ namespace SabreTools SHA1 + "', " + "'" + header + "', " + "'" + type.ToString() + "')"; - using (SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - _logger.Log("Result of inserting header: " + slc.ExecuteNonQuery()); - } - } + slc = new SqliteCommand(query, dbc); + _logger.Log("Result of inserting header: " + slc.ExecuteNonQuery()); } + + // Dispose of database objects + slc.Dispose(); + sldr.Dispose(); + dbc.Dispose(); } /// @@ -186,45 +181,49 @@ namespace SabreTools /// True if a header was found and appended, false otherwise private bool RestoreHeader(string file) { + bool success = true; + // First, get the SHA-1 hash of the file Rom rom = FileTools.GetSingleFileInfo(file); // Then try to pull the corresponding headers from the database string header = ""; - string query = @"SELECT header, type FROM data WHERE sha1='" + rom.SHA1 + "'"; - using (SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - if (sldr.HasRows) - { - int sub = 0; - while (sldr.Read()) - { - _logger.Log("Found match with rom type " + sldr.GetString(1)); - header = sldr.GetString(0); + // Open the database connection + SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString); + dbc.Open(); - _logger.User("Creating reheadered file: " + - (_outDir == "" ? Path.GetFullPath(file) + ".new" : Path.Combine(_outDir, Path.GetFileName(file))) + sub); - FileTools.AppendBytesToFile(file, - (_outDir == "" ? Path.GetFullPath(file) + ".new" : Path.Combine(_outDir, Path.GetFileName(file))) + sub, header, string.Empty); - _logger.User("Reheadered file created!"); - } - } - else - { - _logger.Warning("No matching header could be found!"); - return false; - } - } + string query = @"SELECT header, type FROM data WHERE sha1='" + rom.SHA1 + "'"; + SqliteCommand slc = new SqliteCommand(query, dbc); + SqliteDataReader sldr = slc.ExecuteReader(); + + if (sldr.HasRows) + { + int sub = 0; + while (sldr.Read()) + { + _logger.Log("Found match with rom type " + sldr.GetString(1)); + header = sldr.GetString(0); + + _logger.User("Creating reheadered file: " + + (_outDir == "" ? Path.GetFullPath(file) + ".new" : Path.Combine(_outDir, Path.GetFileName(file))) + sub); + FileTools.AppendBytesToFile(file, + (_outDir == "" ? Path.GetFullPath(file) + ".new" : Path.Combine(_outDir, Path.GetFileName(file))) + sub, header, string.Empty); + _logger.User("Reheadered file created!"); } } + else + { + _logger.Warning("No matching header could be found!"); + success = false; + } - return true; + // Dispose of database objects + slc.Dispose(); + sldr.Dispose(); + dbc.Dispose(); + + return success; } } } diff --git a/SabreTools.Helper/Tools/DBTools.cs b/SabreTools.Helper/Tools/DBTools.cs index e72991fa..9cde13d1 100644 --- a/SabreTools.Helper/Tools/DBTools.cs +++ b/SabreTools.Helper/Tools/DBTools.cs @@ -26,7 +26,7 @@ namespace SabreTools.Helper SqliteConnection.CreateFile(db); } - // Connect to the file + // Open the database connection SqliteConnection dbc = new SqliteConnection(connectionString); dbc.Open(); @@ -44,6 +44,7 @@ CREATE TABLE IF NOT EXISTS data ( )"; SqliteCommand slc = new SqliteCommand(query, dbc); slc.ExecuteNonQuery(); + slc.Dispose(); } else if (type == "headerer") { @@ -56,6 +57,7 @@ CREATE TABLE IF NOT EXISTS data ( )"; SqliteCommand slc = new SqliteCommand(query, dbc); slc.ExecuteNonQuery(); + slc.Dispose(); } } catch (Exception ex) @@ -64,124 +66,7 @@ CREATE TABLE IF NOT EXISTS data ( } finally { - // Close and return the database connection - dbc.Close(); - } - } - - /// - /// Add a new source to the database if it doesn't already exist - /// - /// Source name - /// Source URL(s) - /// Connection string for SQLite - /// True if the source existed or could be added, false otherwise - public static bool AddSource(string name, string url, string connectionString) - { - string query = "SELECT id, name, url FROM source WHERE name='" + name + "'"; - using (SqliteConnection dbc = new SqliteConnection(connectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If nothing is found, add the source - if (!sldr.HasRows) - { - string squery = "INSERT INTO source (name, url) VALUES ('" + name + "', '" + url + "')"; - using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) - { - return sslc.ExecuteNonQuery() >= 1; - } - } - // Otherwise, update the source URL if it's different - else - { - sldr.Read(); - if (url != sldr.GetString(2)) - { - string squery = "UPDATE source SET url='" + url + "' WHERE id=" + sldr.GetInt32(0); - using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) - { - return sslc.ExecuteNonQuery() >= 1; - } - } - } - } - } - } - - return true; - } - - /// - /// Remove an existing source from the database - /// - /// Source ID to be removed from the database - /// Connection string for SQLite - /// True if the source was removed, false otherwise - public static bool RemoveSource(int id, string connectionString) - { - string query = "DELETE FROM source WHERE id=" + id; - using (SqliteConnection dbc = new SqliteConnection(connectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - return slc.ExecuteNonQuery() >= 1; - } - } - } - - /// - /// Add a new system to the database if it doesn't already exist - /// - /// Manufacturer name - /// System name - /// Connection string for SQLite - /// True if the system existed or could be added, false otherwise - public static bool AddSystem(string manufacturer, string system, string connectionString) - { - string query = "SELECT id, manufacturer, name FROM system WHERE manufacturer='" + manufacturer + "' AND system='" + system + "'"; - using (SqliteConnection dbc = new SqliteConnection(connectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If nothing is found, add the system - if (!sldr.HasRows) - { - string squery = "INSERT INTO name (manufacturer, system) VALUES ('" + manufacturer + "', '" + system + "')"; - using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) - { - return sslc.ExecuteNonQuery() >= 1; - } - } - } - } - } - return true; - } - - /// - /// Remove an existing system from the database - /// - /// System ID to be removed from the database - /// Connection string for SQLite - /// True if the system was removed, false otherwise - public static bool RemoveSystem(int id, string connectionString) - { - string query = "DELETE FROM system WHERE id=" + id; - using (SqliteConnection dbc = new SqliteConnection(connectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - return slc.ExecuteNonQuery() >= 1; - } + dbc.Dispose(); } } } diff --git a/SabreTools.Helper/Tools/FileTools.cs b/SabreTools.Helper/Tools/FileTools.cs index 70846b08..63ba7642 100644 --- a/SabreTools.Helper/Tools/FileTools.cs +++ b/SabreTools.Helper/Tools/FileTools.cs @@ -726,45 +726,50 @@ namespace SabreTools.Helper try { - using (OptimizedCRC crc = new OptimizedCRC()) - using (MD5 md5 = MD5.Create()) - using (SHA1 sha1 = SHA1.Create()) + // Initialize the hashers + OptimizedCRC crc = new OptimizedCRC(); + MD5 md5 = MD5.Create(); + SHA1 sha1 = SHA1.Create(); + + // Seek to the starting position, if one is set + if (offset > 0) { - // Seek to the starting position, if one is set - if (offset > 0) - { - input.Seek(offset, SeekOrigin.Begin); - } - - byte[] buffer = new byte[1024]; - int read; - while ((read = input.Read(buffer, 0, buffer.Length)) > 0) - { - crc.Update(buffer, 0, read); - if (!noMD5) - { - md5.TransformBlock(buffer, 0, read, buffer, 0); - } - if (!noSHA1) - { - sha1.TransformBlock(buffer, 0, read, buffer, 0); - } - } - - crc.Update(buffer, 0, 0); - rom.CRC = crc.Value.ToString("X8").ToLowerInvariant(); + input.Seek(offset, SeekOrigin.Begin); + } + byte[] buffer = new byte[1024]; + int read; + while ((read = input.Read(buffer, 0, buffer.Length)) > 0) + { + crc.Update(buffer, 0, read); if (!noMD5) { - md5.TransformFinalBlock(buffer, 0, 0); - rom.MD5 = BitConverter.ToString(md5.Hash).Replace("-", "").ToLowerInvariant(); + md5.TransformBlock(buffer, 0, read, buffer, 0); } if (!noSHA1) { - sha1.TransformFinalBlock(buffer, 0, 0); - rom.SHA1 = BitConverter.ToString(sha1.Hash).Replace("-", "").ToLowerInvariant(); + sha1.TransformBlock(buffer, 0, read, buffer, 0); } } + + crc.Update(buffer, 0, 0); + rom.CRC = crc.Value.ToString("X8").ToLowerInvariant(); + + if (!noMD5) + { + md5.TransformFinalBlock(buffer, 0, 0); + rom.MD5 = BitConverter.ToString(md5.Hash).Replace("-", "").ToLowerInvariant(); + } + if (!noSHA1) + { + sha1.TransformFinalBlock(buffer, 0, 0); + rom.SHA1 = BitConverter.ToString(sha1.Hash).Replace("-", "").ToLowerInvariant(); + } + + // Dispose of the hashers + crc.Dispose(); + md5.Dispose(); + sha1.Dispose(); } catch (IOException) {