[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

@@ -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();

View File

@@ -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();
}
/// <summary>
@@ -186,45 +181,49 @@ namespace SabreTools
/// <returns>True if a header was found and appended, false otherwise</returns>
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;
}
}
}

View File

@@ -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();
}
}
/// <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;
}
dbc.Dispose();
}
}
}

View File

@@ -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)
{