mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[Headerer] Make Headerer great again
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using Mono.Data.Sqlite;
|
using Mono.Data.Sqlite;
|
||||||
using SabreTools.Helper;
|
using SabreTools.Helper;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace SabreTools
|
namespace SabreTools
|
||||||
@@ -11,7 +12,7 @@ namespace SabreTools
|
|||||||
public class Headerer
|
public class Headerer
|
||||||
{
|
{
|
||||||
// Private instance variables
|
// Private instance variables
|
||||||
private string _input;
|
private List<string> _inputs;
|
||||||
private bool _restore;
|
private bool _restore;
|
||||||
private Logger _logger;
|
private Logger _logger;
|
||||||
|
|
||||||
@@ -23,12 +24,12 @@ namespace SabreTools
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a new Headerer object
|
/// Create a new Headerer object
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="input">Input file or folder name</param>
|
/// <param name="inputs">Input file or folder names</param>
|
||||||
/// <param name="restore">False if we're extracting headers (default), true if we're restoring them</param>
|
/// <param name="restore">False if we're extracting headers (default), true if we're restoring them</param>
|
||||||
/// <param name="logger">Logger object for file and console output</param>
|
/// <param name="logger">Logger object for file and console output</param>
|
||||||
public Headerer(string input, bool restore, Logger logger)
|
public Headerer(List<string> inputs, bool restore, Logger logger)
|
||||||
{
|
{
|
||||||
_input = input;
|
_inputs = inputs;
|
||||||
_restore = restore;
|
_restore = restore;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
@@ -39,46 +40,44 @@ namespace SabreTools
|
|||||||
/// <returns>True if it succeeded, false otherwise</returns>
|
/// <returns>True if it succeeded, false otherwise</returns>
|
||||||
public bool Process()
|
public bool Process()
|
||||||
{
|
{
|
||||||
if (_restore)
|
bool success = true;
|
||||||
|
|
||||||
|
foreach (string input in _inputs)
|
||||||
{
|
{
|
||||||
// If it's a single file, just check it
|
if (File.Exists(input))
|
||||||
if (File.Exists(_input))
|
|
||||||
{
|
{
|
||||||
RestoreHeader(_input);
|
success &= ProcessHelper(input);
|
||||||
}
|
}
|
||||||
// If it's a directory, recursively check all
|
else if (Directory.Exists(input))
|
||||||
else if (Directory.Exists(_input))
|
|
||||||
{
|
{
|
||||||
foreach (string sub in Directory.EnumerateFiles(_input, "*", SearchOption.AllDirectories))
|
foreach (string sub in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories))
|
||||||
{
|
{
|
||||||
if (sub != ".." && sub != ".")
|
if (sub != ".." && sub != ".")
|
||||||
{
|
{
|
||||||
RestoreHeader(sub);
|
success &= RestoreHeader(sub);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// If it's a single file, just check it
|
|
||||||
if (File.Exists(_input))
|
|
||||||
{
|
|
||||||
DetectSkipperAndTransform(_input);
|
|
||||||
}
|
|
||||||
// If it's a directory, recursively check all
|
|
||||||
else if (Directory.Exists(_input))
|
|
||||||
{
|
|
||||||
foreach (string sub in Directory.EnumerateFiles(_input, "*", SearchOption.AllDirectories))
|
|
||||||
{
|
|
||||||
if (sub != ".." && sub != ".")
|
|
||||||
{
|
|
||||||
DetectSkipperAndTransform(sub);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Intermediary to route the input file to the correct method(s)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input">Input file name</param>
|
||||||
|
/// <returns>True on success, false otherwise</returns>
|
||||||
|
private bool ProcessHelper(string input)
|
||||||
|
{
|
||||||
|
if (_restore)
|
||||||
|
{
|
||||||
|
return RestoreHeader(input);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return DetectSkipperAndTransform(input);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -86,7 +85,7 @@ namespace SabreTools
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="file">Name of the file to be parsed</param>
|
/// <param name="file">Name of the file to be parsed</param>
|
||||||
/// <returns>True if the output file was created, false otherwise</returns>
|
/// <returns>True if the output file was created, false otherwise</returns>
|
||||||
public bool DetectSkipperAndTransform(string file)
|
private bool DetectSkipperAndTransform(string file)
|
||||||
{
|
{
|
||||||
_logger.User("\nGetting skipper information for '" + file + "'");
|
_logger.User("\nGetting skipper information for '" + file + "'");
|
||||||
|
|
||||||
@@ -145,7 +144,7 @@ namespace SabreTools
|
|||||||
/// <param name="header">String representing the header bytes</param>
|
/// <param name="header">String representing the header bytes</param>
|
||||||
/// <param name="SHA1">SHA-1 of the deheadered file</param>
|
/// <param name="SHA1">SHA-1 of the deheadered file</param>
|
||||||
/// <param name="type">HeaderType representing the detected header</param>
|
/// <param name="type">HeaderType representing the detected header</param>
|
||||||
public void AddHeaderToDatabase(string header, string SHA1, HeaderType type)
|
private void AddHeaderToDatabase(string header, string SHA1, HeaderType type)
|
||||||
{
|
{
|
||||||
bool exists = false;
|
bool exists = false;
|
||||||
|
|
||||||
@@ -184,7 +183,7 @@ namespace SabreTools
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="file">Name of the file to be parsed</param>
|
/// <param name="file">Name of the file to be parsed</param>
|
||||||
/// <returns>True if a header was found and appended, false otherwise</returns>
|
/// <returns>True if a header was found and appended, false otherwise</returns>
|
||||||
public bool RestoreHeader(string file)
|
private bool RestoreHeader(string file)
|
||||||
{
|
{
|
||||||
// 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);
|
||||||
|
|||||||
@@ -322,11 +322,8 @@ namespace SabreTools
|
|||||||
/// <param name="logger">Logger object for file and console output</param>
|
/// <param name="logger">Logger object for file and console output</param>
|
||||||
private static void InitHeaderer(List<string> inputs, bool restore, Logger logger)
|
private static void InitHeaderer(List<string> inputs, bool restore, Logger logger)
|
||||||
{
|
{
|
||||||
foreach (string input in inputs)
|
Headerer headerer = new Headerer(inputs, restore, logger);
|
||||||
{
|
headerer.Process();
|
||||||
Headerer headerer = new Headerer(input, restore, logger);
|
|
||||||
headerer.Process();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user