[ALL] Code cleanup and move

This commit is contained in:
Matt Nadareski
2016-10-31 14:26:23 -07:00
parent c3064996b2
commit b7527ca174
4 changed files with 229 additions and 202 deletions

View File

@@ -44,11 +44,11 @@ namespace SabreTools.Helper.Tools
/// Attempt to extract a file as an archive
/// </summary>
/// <param name="input">Name of the file to be extracted</param>
/// <param name="tempDir">Temporary directory for archive extraction</param>
/// <param name="outDir">Output directory for archive extraction</param>
/// <param name="archiveScanLevel">ArchiveScanLevel representing the archive handling levels</param>
/// <param name="logger">Logger object for file and console output</param>
/// <returns>True if the extraction was a success, false otherwise</returns>
public static bool ExtractArchive(string input, string tempDir, ArchiveScanLevel archiveScanLevel, Logger logger)
public static bool ExtractArchive(string input, string outDir, ArchiveScanLevel archiveScanLevel, Logger logger)
{
bool encounteredErrors = true;
@@ -69,13 +69,13 @@ namespace SabreTools.Helper.Tools
logger.Verbose("Found archive of type: " + at);
// Create the temp directory
Directory.CreateDirectory(tempDir);
Directory.CreateDirectory(outDir);
// Extract all files to the temp directory
SevenZipArchive sza = SevenZipArchive.Open(File.OpenRead(input));
foreach (SevenZipArchiveEntry entry in sza.Entries)
{
entry.WriteToDirectory(tempDir, new ExtractionOptions{ PreserveFileTime = true, ExtractFullPath = true, Overwrite = true });
entry.WriteToDirectory(outDir, new ExtractionOptions{ PreserveFileTime = true, ExtractFullPath = true, Overwrite = true });
}
encounteredErrors = false;
sza.Dispose();
@@ -87,10 +87,10 @@ namespace SabreTools.Helper.Tools
logger.Verbose("Found archive of type: " + at);
// Create the temp directory
Directory.CreateDirectory(tempDir);
Directory.CreateDirectory(outDir);
// Decompress the input stream
FileStream outstream = File.Create(Path.Combine(tempDir, Path.GetFileNameWithoutExtension(input)));
FileStream outstream = File.Create(Path.Combine(outDir, Path.GetFileNameWithoutExtension(input)));
GZipStream gzstream = new GZipStream(File.OpenRead(input), CompressionMode.Decompress);
gzstream.CopyTo(outstream);
@@ -107,13 +107,13 @@ namespace SabreTools.Helper.Tools
logger.Verbose("Found archive of type: " + at);
// Create the temp directory
Directory.CreateDirectory(tempDir);
Directory.CreateDirectory(outDir);
// Extract all files to the temp directory
RarArchive ra = RarArchive.Open(input);
foreach (RarArchiveEntry entry in ra.Entries)
{
entry.WriteToDirectory(tempDir, new ExtractionOptions { PreserveFileTime = true, ExtractFullPath = true, Overwrite = true });
entry.WriteToDirectory(outDir, new ExtractionOptions { PreserveFileTime = true, ExtractFullPath = true, Overwrite = true });
}
encounteredErrors = false;
ra.Dispose();
@@ -125,13 +125,13 @@ namespace SabreTools.Helper.Tools
logger.Verbose("Found archive of type: " + at);
// Create the temp directory
Directory.CreateDirectory(tempDir);
Directory.CreateDirectory(outDir);
// Extract all files to the temp directory
TarArchive ta = TarArchive.Open(input);
foreach (TarArchiveEntry entry in ta.Entries)
{
entry.WriteToDirectory(tempDir, new ExtractionOptions { PreserveFileTime = true, ExtractFullPath = true, Overwrite = true });
entry.WriteToDirectory(outDir, new ExtractionOptions { PreserveFileTime = true, ExtractFullPath = true, Overwrite = true });
}
encounteredErrors = false;
ta.Dispose();
@@ -143,7 +143,7 @@ namespace SabreTools.Helper.Tools
logger.Verbose("Found archive of type: " + at);
// Create the temp directory
Directory.CreateDirectory(tempDir);
Directory.CreateDirectory(outDir);
// Extract all files to the temp directory
ZipFile zf = new ZipFile();
@@ -166,7 +166,7 @@ namespace SabreTools.Helper.Tools
// Create the rest of the path, if needed
if (!String.IsNullOrEmpty(Path.GetDirectoryName(zf.Entries[i].FileName)))
{
Directory.CreateDirectory(Path.Combine(tempDir, Path.GetDirectoryName(zf.Entries[i].FileName)));
Directory.CreateDirectory(Path.Combine(outDir, Path.GetDirectoryName(zf.Entries[i].FileName)));
}
// If the entry ends with a directory separator, continue to the next item, if any
@@ -177,7 +177,7 @@ namespace SabreTools.Helper.Tools
continue;
}
FileStream writeStream = File.OpenWrite(Path.Combine(tempDir, zf.Entries[i].FileName));
FileStream writeStream = File.OpenWrite(Path.Combine(outDir, zf.Entries[i].FileName));
byte[] ibuffer = new byte[_bufferSize];
int ilen;
@@ -971,7 +971,7 @@ namespace SabreTools.Helper.Tools
}
/// <summary>
/// Write a set of input files to a torrent7z archive (assuming the same output archive name)
/// (UNIMPLEMENTED) Write a set of input files to a torrent7z archive (assuming the same output archive name)
/// </summary>
/// <param name="inputFile">Input filenames to be moved</param>
/// <param name="outDir">Output directory to build to</param>
@@ -1060,7 +1060,7 @@ namespace SabreTools.Helper.Tools
// If we're in romba mode, create the subfolder and move the file
if (romba)
{
MoveToRombaFolder(rom, outDir, outfile, logger);
FileTools.MoveToRombaFolder(rom, outDir, outfile, logger);
}
return true;
@@ -1087,7 +1087,7 @@ namespace SabreTools.Helper.Tools
}
/// <summary>
/// Write a set of input files to a torrentlrzip archive (assuming the same output archive name)
/// (UNIMPLEMENTED) Write a set of input files to a torrentlrzip archive (assuming the same output archive name)
/// </summary>
/// <param name="inputFile">Input filenames to be moved</param>
/// <param name="outDir">Output directory to build to</param>
@@ -1121,7 +1121,7 @@ namespace SabreTools.Helper.Tools
}
/// <summary>
/// Write a set of input files to a torrentrar archive (assuming the same output archive name)
/// (UNIMPLEMENTED) Write a set of input files to a torrentrar archive (assuming the same output archive name)
/// </summary>
/// <param name="inputFile">Input filenames to be moved</param>
/// <param name="outDir">Output directory to build to</param>
@@ -1155,7 +1155,7 @@ namespace SabreTools.Helper.Tools
}
/// <summary>
/// Write a set of input files to a torrentxz archive (assuming the same output archive name)
/// (UNIMPLEMENTED) Write a set of input files to a torrentxz archive (assuming the same output archive name)
/// </summary>
/// <param name="inputFile">Input filenames to be moved</param>
/// <param name="outDir">Output directory to build to</param>
@@ -1418,44 +1418,6 @@ namespace SabreTools.Helper.Tools
return true;
}
/// <summary>
/// Get the romba path for a file based on the rom's SHA-1
/// </summary>
/// <param name="rom">Rom to get the sha1 from</param>
/// <param name="baseOutDir">Base output folder</param>
/// <returns>Formatted path string to use</returns>
public static string GetRombaPath(Rom rom, string baseOutDir)
{
string subfolder = Path.Combine(rom.SHA1.Substring(0, 2), rom.SHA1.Substring(2, 2), rom.SHA1.Substring(4, 2), rom.SHA1.Substring(6, 2));
return Path.Combine(baseOutDir, subfolder);
}
/// <summary>
/// Move a file to a named, Romba-style subdirectory
/// </summary>
/// <param name="rom">Rom to get the sha1 from</param>
/// <param name="baseOutDir">Base output folder</param>
/// <param name="filename">Name of the file to be moved</param>
/// <param name="logger">Logger object for file and console output</param>
public static void MoveToRombaFolder(Rom rom, string baseOutDir, string filename, Logger logger)
{
string outDir = GetRombaPath(rom, baseOutDir);
if (!Directory.Exists(outDir))
{
Directory.CreateDirectory(outDir);
}
try
{
File.Move(filename, Path.Combine(outDir, Path.GetFileName(filename)));
}
catch (Exception ex)
{
logger.Warning(ex.ToString());
File.Delete(filename);
}
}
#endregion
}
}

View File

@@ -1,5 +1,6 @@
using Mono.Data.Sqlite;
using System;
using System.Collections.Generic;
using SabreTools.Helper.Data;
@@ -27,6 +28,9 @@ namespace SabreTools.Helper.Tools
{
bool exists = false;
// Ensure the database exists
EnsureDatabase(Constants.HeadererDbSchema, Constants.HeadererFileName, Constants.HeadererConnectionString);
// Open the database connection
SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString);
dbc.Open();
@@ -153,5 +157,48 @@ CREATE TABLE IF NOT EXISTS data (
dbc.Dispose();
}
}
/// <summary>
/// Retrieve headers from the database
/// </summary>
/// <param name="SHA1">SHA-1 of the deheadered file</param>
/// <param name="logger">Logger object for console and file output</param>
/// <returns>List of strings representing the headers to add</returns>
public static List<string> RetrieveHeadersFromDatabase(string SHA1, Logger logger)
{
// Ensure the database exists
EnsureDatabase(Constants.HeadererDbSchema, Constants.HeadererFileName, Constants.HeadererConnectionString);
// Open the database connection
SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString);
dbc.Open();
// Create the output list of headers
List<string> headers = new List<string>();
string query = @"SELECT header, type FROM data WHERE sha1='" + SHA1 + "'";
SqliteCommand slc = new SqliteCommand(query, dbc);
SqliteDataReader sldr = slc.ExecuteReader();
if (sldr.HasRows)
{
while (sldr.Read())
{
logger.Verbose("Found match with rom type " + sldr.GetString(1));
headers.Add(sldr.GetString(0));
}
}
else
{
logger.Warning("No matching header could be found!");
}
// Dispose of database objects
slc.Dispose();
sldr.Dispose();
dbc.Dispose();
return headers;
}
}
}

View File

@@ -231,34 +231,6 @@ namespace SabreTools.Helper.Tools
#region File Manipulation
/// <summary>
/// Get the XmlTextReader associated with a file, if possible
/// </summary>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="logger">Logger object for console and file output</param>
/// <returns>The XmlTextReader representing the (possibly converted) file, null otherwise</returns>
public static XmlReader GetXmlTextReader(string filename, Logger logger)
{
logger.Verbose("Attempting to read file: \"" + filename + "\"");
// Check if file exists
if (!File.Exists(filename))
{
logger.Warning("File '" + filename + "' could not read from!");
return null;
}
XmlReader xtr = XmlReader.Create(filename, new XmlReaderSettings {
CheckCharacters = false,
DtdProcessing = DtdProcessing.Ignore,
IgnoreComments = true,
IgnoreWhitespace = true,
ValidationFlags = XmlSchemaValidationFlags.None,
ValidationType = ValidationType.None,
});
return xtr;
}
/// <summary>
/// Add an aribtrary number of bytes to the inputted file
/// </summary>
@@ -307,6 +279,30 @@ namespace SabreTools.Helper.Tools
fsw.Dispose();
}
/// <summary>
/// Cleans out the temporary directory
/// </summary>
/// <param name="dirname">Name of the directory to clean out</param>
public static void CleanDirectory(string dirname)
{
foreach (string file in Directory.EnumerateFiles(dirname, "*", SearchOption.TopDirectoryOnly))
{
try
{
File.Delete(file);
}
catch { }
}
foreach (string dir in Directory.EnumerateDirectories(dirname, "*", SearchOption.TopDirectoryOnly))
{
try
{
Directory.Delete(dir, true);
}
catch { }
}
}
/// <summary>
/// Detect header skipper compliance and create an output file
/// </summary>
@@ -364,90 +360,6 @@ namespace SabreTools.Helper.Tools
return true;
}
/// <summary>
/// Detect and replace header(s) to the given file
/// </summary>
/// <param name="file">Name of the file to be parsed</param>
/// <param name="outDir">Output directory to write the file to, empty means the same directory as the input file</param>
/// <param name="logger">Logger object for console and file output</param>
/// <returns>True if a header was found and appended, false otherwise</returns>
public static bool RestoreHeader(string file, string outDir, Logger logger)
{
// Create the output directory if it doesn't exist
if (outDir != "" && !Directory.Exists(outDir))
{
Directory.CreateDirectory(outDir);
}
bool success = true;
// First, get the SHA-1 hash of the file
Rom rom = GetFileInfo(file, logger);
// Then try to pull the corresponding headers from the database
string header = "";
// Open the database connection
SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString);
dbc.Open();
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.Verbose("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);
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;
}
// Dispose of database objects
slc.Dispose();
sldr.Dispose();
dbc.Dispose();
return success;
}
/// <summary>
/// Cleans out the temporary directory
/// </summary>
/// <param name="dirname">Name of the directory to clean out</param>
public static void CleanDirectory(string dirname)
{
foreach (string file in Directory.EnumerateFiles(dirname, "*", SearchOption.TopDirectoryOnly))
{
try
{
File.Delete(file);
}
catch { }
}
foreach (string dir in Directory.EnumerateDirectories(dirname, "*", SearchOption.TopDirectoryOnly))
{
try
{
Directory.Delete(dir, true);
}
catch { }
}
}
/// <summary>
/// Retrieve a list of just files from inputs
/// </summary>
@@ -508,6 +420,113 @@ namespace SabreTools.Helper.Tools
return outputs;
}
/// <summary>
/// Get the romba path for a file based on the rom's SHA-1
/// </summary>
/// <param name="rom">Rom to get the sha1 from</param>
/// <param name="baseOutDir">Base output folder</param>
/// <returns>Formatted path string to use</returns>
public static string GetRombaPath(Rom rom, string baseOutDir)
{
string subfolder = Path.Combine(rom.SHA1.Substring(0, 2), rom.SHA1.Substring(2, 2), rom.SHA1.Substring(4, 2), rom.SHA1.Substring(6, 2));
return Path.Combine(baseOutDir, subfolder);
}
/// <summary>
/// Get the XmlTextReader associated with a file, if possible
/// </summary>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="logger">Logger object for console and file output</param>
/// <returns>The XmlTextReader representing the (possibly converted) file, null otherwise</returns>
public static XmlReader GetXmlTextReader(string filename, Logger logger)
{
logger.Verbose("Attempting to read file: \"" + filename + "\"");
// Check if file exists
if (!File.Exists(filename))
{
logger.Warning("File '" + filename + "' could not read from!");
return null;
}
XmlReader xtr = XmlReader.Create(filename, new XmlReaderSettings
{
CheckCharacters = false,
DtdProcessing = DtdProcessing.Ignore,
IgnoreComments = true,
IgnoreWhitespace = true,
ValidationFlags = XmlSchemaValidationFlags.None,
ValidationType = ValidationType.None,
});
return xtr;
}
/// <summary>
/// Move a file to a named, Romba-style subdirectory
/// </summary>
/// <param name="rom">Rom to get the sha1 from</param>
/// <param name="baseOutDir">Base output folder</param>
/// <param name="filename">Name of the file to be moved</param>
/// <param name="logger">Logger object for file and console output</param>
public static void MoveToRombaFolder(Rom rom, string baseOutDir, string filename, Logger logger)
{
string outDir = GetRombaPath(rom, baseOutDir);
if (!Directory.Exists(outDir))
{
Directory.CreateDirectory(outDir);
}
try
{
File.Move(filename, Path.Combine(outDir, Path.GetFileName(filename)));
}
catch (Exception ex)
{
logger.Warning(ex.ToString());
File.Delete(filename);
}
}
/// <summary>
/// Detect and replace header(s) to the given file
/// </summary>
/// <param name="file">Name of the file to be parsed</param>
/// <param name="outDir">Output directory to write the file to, empty means the same directory as the input file</param>
/// <param name="logger">Logger object for console and file output</param>
/// <returns>True if a header was found and appended, false otherwise</returns>
public static bool RestoreHeader(string file, string outDir, Logger logger)
{
// Create the output directory if it doesn't exist
if (outDir != "" && !Directory.Exists(outDir))
{
Directory.CreateDirectory(outDir);
}
// First, get the SHA-1 hash of the file
Rom rom = GetFileInfo(file, logger);
// Retrieve a list of all related headers from the database
List<string> headers = DatabaseTools.RetrieveHeadersFromDatabase(rom.SHA1, logger);
// If we have nothing retrieved, we return false
if (headers.Count == 0)
{
return false;
}
// Now loop through and create the reheadered files, if possible
for (int i = 0; i < headers.Count; i++)
{
logger.User("Creating reheadered file: " +
(outDir == "" ? Path.GetFullPath(file) + ".new" : Path.Combine(outDir, Path.GetFileName(file))) + i);
AppendBytesToFile(file,
(outDir == "" ? Path.GetFullPath(file) + ".new" : Path.Combine(outDir, Path.GetFileName(file))) + i, headers[i], string.Empty);
logger.User("Reheadered file created!");
}
return true;
}
#endregion
#region Stream Information

View File

@@ -36,7 +36,6 @@ namespace SabreTools
Console.Clear();
}
Build.Start("SabreTools");
DatabaseTools.EnsureDatabase(Constants.HeadererDbSchema, Constants.HeadererFileName, Constants.HeadererConnectionString);
// Credits take precidence over all
if ((new List<string>(args)).Contains("--credits"))