[ALL] Get rid of rest of non-library usings

This commit is contained in:
Matt Nadareski
2016-09-22 15:59:03 -07:00
parent 74fbe60686
commit 863e936d07
5 changed files with 196 additions and 316 deletions

View File

@@ -314,110 +314,97 @@ namespace SabreTools
// Populate the List from the database // Populate the List from the database
string query = "SELECT UNIQUE value FROM data WHERE key=\"dat\""; 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<string, string> toscan = new Dictionary<string, string>();
// 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(); query = "SELECT id FROM data WHERE key=\"size\" AND value=\"" + rom.Size + "\" AND ("
string hash = sldr.GetString(0); + "(key=\"crc\" AND (value=\"" + rom.CRC + "\" OR value=\"null\"))"
if (!databaseDats.Contains(hash)) + "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) // If it doesn't exist, add the hash and the dat hash for a new id
Dictionary<string, string> toscan = new Dictionary<string, string>(); else
// 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])
{ {
query = "SELECT id FROM data WHERE key=\"size\" AND value=\"" + rom.Size + "\" AND (" string squery = "INSERT INTO data (key, value) VALUES (\"size\", \"" + rom.Size + "\")";
+ "(key=\"crc\" AND (value=\"" + rom.CRC + "\" OR value=\"null\"))" SqliteCommand sslc = new SqliteCommand(squery, dbc);
+ "AND (key=\"md5\" AND value=\"" + rom.MD5 + "\" OR value=\"null\"))" sslc.ExecuteNonQuery();
+ "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 (id, key, value) VALUES (\"" + id + "\", \"dat\", \"" + key + "\")"; long id = -1;
using (SqliteCommand sslc = new SqliteCommand(squery, dbc))
{
sslc.ExecuteNonQuery();
}
}
// If it doesn't exist, add the hash and the dat hash for a new id squery = "SELECT last_insertConstants.Rowid()";
else sslc = new SqliteCommand(squery, dbc);
{ id = (long)sslc.ExecuteScalar();
string squery = "INSERT INTO data (key, value) VALUES (\"size\", \"" + rom.Size + "\")";
using (SqliteCommand sslc = new SqliteCommand(squery, dbc))
{
sslc.ExecuteNonQuery();
}
long id = -1; squery = "INSERT INTO data (id, key, value) VALUES (\"" + id + "\", \"crc\", \"" + rom.CRC + "\"),"
+ " (\"" + id + "\", \"md5\", \"" + rom.MD5 + "\"),"
squery = "SELECT last_insertConstants.Rowid()"; + " (\"" + id + "\", \"sha1\", \"" + rom.SHA1 + "\"),"
using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) + " (\"" + id + "\", \"dat\", \"" + key + "\"),"
{ + " (\"" + id + "\", \"exists\", \"false\")";
id = (long)sslc.ExecuteScalar(); 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\")";
using (SqliteCommand sslc = new SqliteCommand(squery, dbc))
{
sslc.ExecuteNonQuery();
}
}
}
}
} }
} }
} }
@@ -428,10 +415,9 @@ namespace SabreTools
foreach (string dathash in databaseDats) foreach (string dathash in databaseDats)
{ {
query = "DELETE FROM data WHERE key=\"dat\" AND value=\"" + dathash + "\""; query = "DELETE FROM data WHERE key=\"dat\" AND value=\"" + dathash + "\"";
using (SqliteCommand slc = new SqliteCommand(query, dbc)) slc = new SqliteCommand(query, dbc);
{ slc.ExecuteNonQuery();
slc.ExecuteNonQuery(); slc.Dispose();
}
} }
} }
} }

View File

@@ -982,32 +982,37 @@ namespace SabreTools.Helper
return; return;
} }
// Now get the hash of the stream // Create the hashers
uint tempCrc; uint tempCrc;
using (OptimizedCRC crc = new OptimizedCRC()) OptimizedCRC crc = new OptimizedCRC();
using (MD5 md5 = System.Security.Cryptography.MD5.Create()) MD5 md5 = System.Security.Cryptography.MD5.Create();
using (SHA1 sha1 = System.Security.Cryptography.SHA1.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); crc.Update(buffer, 0, read);
md5.TransformBlock(buffer, 0, read, buffer, 0);
byte[] buffer = new byte[1024]; sha1.TransformBlock(buffer, 0, read, buffer, 0);
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, 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) if (_compressionMethod == CompressionMethod.Deflated)
{ {
stream.Close(); stream.Close();

View File

@@ -149,18 +149,14 @@ namespace SabreTools
{ {
bool exists = false; 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 + "'"; string query = @"SELECT * FROM data WHERE sha1='" + SHA1 + "' AND header='" + header + "'";
using (SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString)) SqliteCommand slc = new SqliteCommand(query, dbc);
{ SqliteDataReader sldr = slc.ExecuteReader();
dbc.Open(); exists = sldr.HasRows;
using (SqliteCommand slc = new SqliteCommand(query, dbc))
{
using (SqliteDataReader sldr = slc.ExecuteReader())
{
exists = sldr.HasRows;
}
}
}
if (!exists) if (!exists)
{ {
@@ -168,15 +164,14 @@ namespace SabreTools
SHA1 + "', " + SHA1 + "', " +
"'" + header + "', " + "'" + header + "', " +
"'" + type.ToString() + "')"; "'" + type.ToString() + "')";
using (SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString)) slc = new SqliteCommand(query, dbc);
{ _logger.Log("Result of inserting header: " + slc.ExecuteNonQuery());
dbc.Open();
using (SqliteCommand slc = new SqliteCommand(query, dbc))
{
_logger.Log("Result of inserting header: " + slc.ExecuteNonQuery());
}
}
} }
// Dispose of database objects
slc.Dispose();
sldr.Dispose();
dbc.Dispose();
} }
/// <summary> /// <summary>
@@ -186,45 +181,49 @@ namespace SabreTools
/// <returns>True if a header was found and appended, false otherwise</returns> /// <returns>True if a header was found and appended, false otherwise</returns>
private bool RestoreHeader(string file) private bool RestoreHeader(string file)
{ {
bool success = true;
// First, get the SHA-1 hash of the file // First, get the SHA-1 hash of the file
Rom rom = FileTools.GetSingleFileInfo(file); Rom rom = FileTools.GetSingleFileInfo(file);
// Then try to pull the corresponding headers from the database // Then try to pull the corresponding headers from the database
string header = ""; string header = "";
string query = @"SELECT header, type FROM data WHERE sha1='" + rom.SHA1 + "'"; // Open the database connection
using (SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString)) SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString);
{ dbc.Open();
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);
_logger.User("Creating reheadered file: " + string query = @"SELECT header, type FROM data WHERE sha1='" + rom.SHA1 + "'";
(_outDir == "" ? Path.GetFullPath(file) + ".new" : Path.Combine(_outDir, Path.GetFileName(file))) + sub); SqliteCommand slc = new SqliteCommand(query, dbc);
FileTools.AppendBytesToFile(file, SqliteDataReader sldr = slc.ExecuteReader();
(_outDir == "" ? Path.GetFullPath(file) + ".new" : Path.Combine(_outDir, Path.GetFileName(file))) + sub, header, string.Empty);
_logger.User("Reheadered file created!"); if (sldr.HasRows)
} {
} int sub = 0;
else while (sldr.Read())
{ {
_logger.Warning("No matching header could be found!"); _logger.Log("Found match with rom type " + sldr.GetString(1));
return false; 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;
} }
} }
} }

View File

@@ -26,7 +26,7 @@ namespace SabreTools.Helper
SqliteConnection.CreateFile(db); SqliteConnection.CreateFile(db);
} }
// Connect to the file // Open the database connection
SqliteConnection dbc = new SqliteConnection(connectionString); SqliteConnection dbc = new SqliteConnection(connectionString);
dbc.Open(); dbc.Open();
@@ -44,6 +44,7 @@ CREATE TABLE IF NOT EXISTS data (
)"; )";
SqliteCommand slc = new SqliteCommand(query, dbc); SqliteCommand slc = new SqliteCommand(query, dbc);
slc.ExecuteNonQuery(); slc.ExecuteNonQuery();
slc.Dispose();
} }
else if (type == "headerer") else if (type == "headerer")
{ {
@@ -56,6 +57,7 @@ CREATE TABLE IF NOT EXISTS data (
)"; )";
SqliteCommand slc = new SqliteCommand(query, dbc); SqliteCommand slc = new SqliteCommand(query, dbc);
slc.ExecuteNonQuery(); slc.ExecuteNonQuery();
slc.Dispose();
} }
} }
catch (Exception ex) catch (Exception ex)
@@ -64,124 +66,7 @@ CREATE TABLE IF NOT EXISTS data (
} }
finally finally
{ {
// Close and return the database connection dbc.Dispose();
dbc.Close();
}
}
/// <summary>
/// Add a new source to the database if it doesn't already exist
/// </summary>
/// <param name="name">Source name</param>
/// <param name="url">Source URL(s)</param>
/// <param name="connectionString">Connection string for SQLite</param>
/// <returns>True if the source existed or could be added, false otherwise</returns>
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;
}
/// <summary>
/// Remove an existing source from the database
/// </summary>
/// <param name="id">Source ID to be removed from the database</param>
/// <param name="connectionString">Connection string for SQLite</param>
/// <returns>True if the source was removed, false otherwise</returns>
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;
}
}
}
/// <summary>
/// Add a new system to the database if it doesn't already exist
/// </summary>
/// <param name="manufacturer">Manufacturer name</param>
/// <param name="system">System name</param>
/// <param name="connectionString">Connection string for SQLite</param>
/// <returns>True if the system existed or could be added, false otherwise</returns>
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;
}
/// <summary>
/// Remove an existing system from the database
/// </summary>
/// <param name="id">System ID to be removed from the database</param>
/// <param name="connectionString">Connection string for SQLite</param>
/// <returns>True if the system was removed, false otherwise</returns>
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;
}
} }
} }
} }

View File

@@ -726,45 +726,50 @@ namespace SabreTools.Helper
try try
{ {
using (OptimizedCRC crc = new OptimizedCRC()) // Initialize the hashers
using (MD5 md5 = MD5.Create()) OptimizedCRC crc = new OptimizedCRC();
using (SHA1 sha1 = SHA1.Create()) 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 input.Seek(offset, SeekOrigin.Begin);
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();
byte[] buffer = new byte[1024];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
crc.Update(buffer, 0, read);
if (!noMD5) if (!noMD5)
{ {
md5.TransformFinalBlock(buffer, 0, 0); md5.TransformBlock(buffer, 0, read, buffer, 0);
rom.MD5 = BitConverter.ToString(md5.Hash).Replace("-", "").ToLowerInvariant();
} }
if (!noSHA1) if (!noSHA1)
{ {
sha1.TransformFinalBlock(buffer, 0, 0); sha1.TransformBlock(buffer, 0, read, buffer, 0);
rom.SHA1 = BitConverter.ToString(sha1.Hash).Replace("-", "").ToLowerInvariant();
} }
} }
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) catch (IOException)
{ {