mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[ALL] Code cleanup and move
This commit is contained in:
@@ -44,11 +44,11 @@ namespace SabreTools.Helper.Tools
|
|||||||
/// Attempt to extract a file as an archive
|
/// Attempt to extract a file as an archive
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="input">Name of the file to be extracted</param>
|
/// <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="archiveScanLevel">ArchiveScanLevel representing the archive handling levels</param>
|
||||||
/// <param name="logger">Logger object for file and console output</param>
|
/// <param name="logger">Logger object for file and console output</param>
|
||||||
/// <returns>True if the extraction was a success, false otherwise</returns>
|
/// <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;
|
bool encounteredErrors = true;
|
||||||
|
|
||||||
@@ -69,13 +69,13 @@ namespace SabreTools.Helper.Tools
|
|||||||
logger.Verbose("Found archive of type: " + at);
|
logger.Verbose("Found archive of type: " + at);
|
||||||
|
|
||||||
// Create the temp directory
|
// Create the temp directory
|
||||||
Directory.CreateDirectory(tempDir);
|
Directory.CreateDirectory(outDir);
|
||||||
|
|
||||||
// Extract all files to the temp directory
|
// Extract all files to the temp directory
|
||||||
SevenZipArchive sza = SevenZipArchive.Open(File.OpenRead(input));
|
SevenZipArchive sza = SevenZipArchive.Open(File.OpenRead(input));
|
||||||
foreach (SevenZipArchiveEntry entry in sza.Entries)
|
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;
|
encounteredErrors = false;
|
||||||
sza.Dispose();
|
sza.Dispose();
|
||||||
@@ -87,10 +87,10 @@ namespace SabreTools.Helper.Tools
|
|||||||
logger.Verbose("Found archive of type: " + at);
|
logger.Verbose("Found archive of type: " + at);
|
||||||
|
|
||||||
// Create the temp directory
|
// Create the temp directory
|
||||||
Directory.CreateDirectory(tempDir);
|
Directory.CreateDirectory(outDir);
|
||||||
|
|
||||||
// Decompress the input stream
|
// 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);
|
GZipStream gzstream = new GZipStream(File.OpenRead(input), CompressionMode.Decompress);
|
||||||
gzstream.CopyTo(outstream);
|
gzstream.CopyTo(outstream);
|
||||||
|
|
||||||
@@ -107,13 +107,13 @@ namespace SabreTools.Helper.Tools
|
|||||||
logger.Verbose("Found archive of type: " + at);
|
logger.Verbose("Found archive of type: " + at);
|
||||||
|
|
||||||
// Create the temp directory
|
// Create the temp directory
|
||||||
Directory.CreateDirectory(tempDir);
|
Directory.CreateDirectory(outDir);
|
||||||
|
|
||||||
// Extract all files to the temp directory
|
// Extract all files to the temp directory
|
||||||
RarArchive ra = RarArchive.Open(input);
|
RarArchive ra = RarArchive.Open(input);
|
||||||
foreach (RarArchiveEntry entry in ra.Entries)
|
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;
|
encounteredErrors = false;
|
||||||
ra.Dispose();
|
ra.Dispose();
|
||||||
@@ -125,13 +125,13 @@ namespace SabreTools.Helper.Tools
|
|||||||
logger.Verbose("Found archive of type: " + at);
|
logger.Verbose("Found archive of type: " + at);
|
||||||
|
|
||||||
// Create the temp directory
|
// Create the temp directory
|
||||||
Directory.CreateDirectory(tempDir);
|
Directory.CreateDirectory(outDir);
|
||||||
|
|
||||||
// Extract all files to the temp directory
|
// Extract all files to the temp directory
|
||||||
TarArchive ta = TarArchive.Open(input);
|
TarArchive ta = TarArchive.Open(input);
|
||||||
foreach (TarArchiveEntry entry in ta.Entries)
|
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;
|
encounteredErrors = false;
|
||||||
ta.Dispose();
|
ta.Dispose();
|
||||||
@@ -143,7 +143,7 @@ namespace SabreTools.Helper.Tools
|
|||||||
logger.Verbose("Found archive of type: " + at);
|
logger.Verbose("Found archive of type: " + at);
|
||||||
|
|
||||||
// Create the temp directory
|
// Create the temp directory
|
||||||
Directory.CreateDirectory(tempDir);
|
Directory.CreateDirectory(outDir);
|
||||||
|
|
||||||
// Extract all files to the temp directory
|
// Extract all files to the temp directory
|
||||||
ZipFile zf = new ZipFile();
|
ZipFile zf = new ZipFile();
|
||||||
@@ -166,7 +166,7 @@ namespace SabreTools.Helper.Tools
|
|||||||
// Create the rest of the path, if needed
|
// Create the rest of the path, if needed
|
||||||
if (!String.IsNullOrEmpty(Path.GetDirectoryName(zf.Entries[i].FileName)))
|
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
|
// If the entry ends with a directory separator, continue to the next item, if any
|
||||||
@@ -177,7 +177,7 @@ namespace SabreTools.Helper.Tools
|
|||||||
continue;
|
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];
|
byte[] ibuffer = new byte[_bufferSize];
|
||||||
int ilen;
|
int ilen;
|
||||||
@@ -971,7 +971,7 @@ namespace SabreTools.Helper.Tools
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <param name="inputFile">Input filenames to be moved</param>
|
/// <param name="inputFile">Input filenames to be moved</param>
|
||||||
/// <param name="outDir">Output directory to build to</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 we're in romba mode, create the subfolder and move the file
|
||||||
if (romba)
|
if (romba)
|
||||||
{
|
{
|
||||||
MoveToRombaFolder(rom, outDir, outfile, logger);
|
FileTools.MoveToRombaFolder(rom, outDir, outfile, logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -1087,7 +1087,7 @@ namespace SabreTools.Helper.Tools
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <param name="inputFile">Input filenames to be moved</param>
|
/// <param name="inputFile">Input filenames to be moved</param>
|
||||||
/// <param name="outDir">Output directory to build to</param>
|
/// <param name="outDir">Output directory to build to</param>
|
||||||
@@ -1121,7 +1121,7 @@ namespace SabreTools.Helper.Tools
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <param name="inputFile">Input filenames to be moved</param>
|
/// <param name="inputFile">Input filenames to be moved</param>
|
||||||
/// <param name="outDir">Output directory to build to</param>
|
/// <param name="outDir">Output directory to build to</param>
|
||||||
@@ -1155,7 +1155,7 @@ namespace SabreTools.Helper.Tools
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <param name="inputFile">Input filenames to be moved</param>
|
/// <param name="inputFile">Input filenames to be moved</param>
|
||||||
/// <param name="outDir">Output directory to build to</param>
|
/// <param name="outDir">Output directory to build to</param>
|
||||||
@@ -1418,44 +1418,6 @@ namespace SabreTools.Helper.Tools
|
|||||||
return true;
|
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
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Mono.Data.Sqlite;
|
using Mono.Data.Sqlite;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
using SabreTools.Helper.Data;
|
using SabreTools.Helper.Data;
|
||||||
|
|
||||||
@@ -27,6 +28,9 @@ namespace SabreTools.Helper.Tools
|
|||||||
{
|
{
|
||||||
bool exists = false;
|
bool exists = false;
|
||||||
|
|
||||||
|
// Ensure the database exists
|
||||||
|
EnsureDatabase(Constants.HeadererDbSchema, Constants.HeadererFileName, Constants.HeadererConnectionString);
|
||||||
|
|
||||||
// Open the database connection
|
// Open the database connection
|
||||||
SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString);
|
SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString);
|
||||||
dbc.Open();
|
dbc.Open();
|
||||||
@@ -153,5 +157,48 @@ CREATE TABLE IF NOT EXISTS data (
|
|||||||
dbc.Dispose();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -231,34 +231,6 @@ namespace SabreTools.Helper.Tools
|
|||||||
|
|
||||||
#region File Manipulation
|
#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>
|
/// <summary>
|
||||||
/// Add an aribtrary number of bytes to the inputted file
|
/// Add an aribtrary number of bytes to the inputted file
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -307,6 +279,30 @@ namespace SabreTools.Helper.Tools
|
|||||||
fsw.Dispose();
|
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>
|
/// <summary>
|
||||||
/// Detect header skipper compliance and create an output file
|
/// Detect header skipper compliance and create an output file
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -364,6 +360,133 @@ namespace SabreTools.Helper.Tools
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve a list of just files from inputs
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="inputs">List of strings representing directories and files</param>
|
||||||
|
/// <param name="maxDegreeOfParallelism">Integer representing the maximum amount of parallelization to be used</param>
|
||||||
|
/// <param name="logger">Logger object for file and console output</param>
|
||||||
|
/// <param name="appendparent">True if the parent name should be appended after the special character "¬", false otherwise</param>
|
||||||
|
/// <returns>List of strings representing just files from the inputs</returns>
|
||||||
|
public static List<string> GetOnlyFilesFromInputs(List<string> inputs, int maxDegreeOfParallelism, Logger logger, bool appendparent = false)
|
||||||
|
{
|
||||||
|
List<string> outputs = new List<string>();
|
||||||
|
Parallel.ForEach(inputs,
|
||||||
|
new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism, },
|
||||||
|
input =>
|
||||||
|
{
|
||||||
|
if (Directory.Exists(input))
|
||||||
|
{
|
||||||
|
List<string> files = FileTools.RetrieveFiles(input, new List<string>());
|
||||||
|
foreach (string file in files)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
lock (outputs)
|
||||||
|
{
|
||||||
|
outputs.Add(Path.GetFullPath(file) + (appendparent ? "¬" + Path.GetFullPath(input) : ""));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (PathTooLongException)
|
||||||
|
{
|
||||||
|
logger.Warning("The path for " + file + " was too long");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (File.Exists(input))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
lock (outputs)
|
||||||
|
{
|
||||||
|
outputs.Add(Path.GetFullPath(input) + (appendparent ? "¬" + Path.GetFullPath(input) : ""));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (PathTooLongException)
|
||||||
|
{
|
||||||
|
logger.Warning("The path for " + input + " was too long");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.Error(ex.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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>
|
/// <summary>
|
||||||
/// Detect and replace header(s) to the given file
|
/// Detect and replace header(s) to the given file
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -379,133 +502,29 @@ namespace SabreTools.Helper.Tools
|
|||||||
Directory.CreateDirectory(outDir);
|
Directory.CreateDirectory(outDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool success = true;
|
|
||||||
|
|
||||||
// First, get the SHA-1 hash of the file
|
// First, get the SHA-1 hash of the file
|
||||||
Rom rom = GetFileInfo(file, logger);
|
Rom rom = GetFileInfo(file, logger);
|
||||||
|
|
||||||
// Then try to pull the corresponding headers from the database
|
// Retrieve a list of all related headers from the database
|
||||||
string header = "";
|
List<string> headers = DatabaseTools.RetrieveHeadersFromDatabase(rom.SHA1, logger);
|
||||||
|
|
||||||
// Open the database connection
|
// If we have nothing retrieved, we return false
|
||||||
SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString);
|
if (headers.Count == 0)
|
||||||
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;
|
return false;
|
||||||
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
|
// Now loop through and create the reheadered files, if possible
|
||||||
slc.Dispose();
|
for (int i = 0; i < headers.Count; i++)
|
||||||
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
|
logger.User("Creating reheadered file: " +
|
||||||
{
|
(outDir == "" ? Path.GetFullPath(file) + ".new" : Path.Combine(outDir, Path.GetFileName(file))) + i);
|
||||||
File.Delete(file);
|
AppendBytesToFile(file,
|
||||||
}
|
(outDir == "" ? Path.GetFullPath(file) + ".new" : Path.Combine(outDir, Path.GetFileName(file))) + i, headers[i], string.Empty);
|
||||||
catch { }
|
logger.User("Reheadered file created!");
|
||||||
}
|
}
|
||||||
foreach (string dir in Directory.EnumerateDirectories(dirname, "*", SearchOption.TopDirectoryOnly))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Directory.Delete(dir, true);
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
return true;
|
||||||
/// Retrieve a list of just files from inputs
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="inputs">List of strings representing directories and files</param>
|
|
||||||
/// <param name="maxDegreeOfParallelism">Integer representing the maximum amount of parallelization to be used</param>
|
|
||||||
/// <param name="logger">Logger object for file and console output</param>
|
|
||||||
/// <param name="appendparent">True if the parent name should be appended after the special character "¬", false otherwise</param>
|
|
||||||
/// <returns>List of strings representing just files from the inputs</returns>
|
|
||||||
public static List<string> GetOnlyFilesFromInputs(List<string> inputs, int maxDegreeOfParallelism, Logger logger, bool appendparent = false)
|
|
||||||
{
|
|
||||||
List<string> outputs = new List<string>();
|
|
||||||
Parallel.ForEach(inputs,
|
|
||||||
new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism, },
|
|
||||||
input =>
|
|
||||||
{
|
|
||||||
if (Directory.Exists(input))
|
|
||||||
{
|
|
||||||
List<string> files = FileTools.RetrieveFiles(input, new List<string>());
|
|
||||||
foreach (string file in files)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
lock (outputs)
|
|
||||||
{
|
|
||||||
outputs.Add(Path.GetFullPath(file) + (appendparent ? "¬" + Path.GetFullPath(input) : ""));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (PathTooLongException)
|
|
||||||
{
|
|
||||||
logger.Warning("The path for " + file + " was too long");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
logger.Error(ex.ToString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (File.Exists(input))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
lock (outputs)
|
|
||||||
{
|
|
||||||
outputs.Add(Path.GetFullPath(input) + (appendparent ? "¬" + Path.GetFullPath(input) : ""));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (PathTooLongException)
|
|
||||||
{
|
|
||||||
logger.Warning("The path for " + input + " was too long");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
logger.Error(ex.ToString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return outputs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ namespace SabreTools
|
|||||||
Console.Clear();
|
Console.Clear();
|
||||||
}
|
}
|
||||||
Build.Start("SabreTools");
|
Build.Start("SabreTools");
|
||||||
DatabaseTools.EnsureDatabase(Constants.HeadererDbSchema, Constants.HeadererFileName, Constants.HeadererConnectionString);
|
|
||||||
|
|
||||||
// Credits take precidence over all
|
// Credits take precidence over all
|
||||||
if ((new List<string>(args)).Contains("--credits"))
|
if ((new List<string>(args)).Contains("--credits"))
|
||||||
|
|||||||
Reference in New Issue
Block a user