2016-06-13 23:54:13 -07:00
|
|
|
|
using Mono.Data.Sqlite;
|
|
|
|
|
|
using SabreTools.Helper;
|
|
|
|
|
|
using System;
|
2016-03-28 01:01:56 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
2016-03-29 13:48:10 -07:00
|
|
|
|
namespace SabreTools
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Entry class for the Deheader application
|
|
|
|
|
|
/// </summary>
|
2016-06-17 11:47:30 -07:00
|
|
|
|
public class Headerer
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-06-17 11:47:30 -07:00
|
|
|
|
// Private instance variables
|
|
|
|
|
|
private string _input;
|
|
|
|
|
|
private bool _flag;
|
|
|
|
|
|
private Logger _logger;
|
|
|
|
|
|
|
|
|
|
|
|
// Private required variables
|
2016-03-30 02:36:23 -07:00
|
|
|
|
private static string _dbName = "Headerer.sqlite";
|
|
|
|
|
|
private static string _connectionString = "Data Source=" + _dbName + ";Version = 3;";
|
2016-06-17 11:47:30 -07:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a new Headerer object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input">Input file or folder name</param>
|
|
|
|
|
|
/// <param name="flag">True if we're extracting headers (default), false if we're replacing them</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for file and console output</param>
|
|
|
|
|
|
public Headerer(string input, bool flag, Logger logger)
|
|
|
|
|
|
{
|
|
|
|
|
|
_input = input;
|
|
|
|
|
|
_flag = flag;
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
}
|
2016-04-09 00:34:37 -07:00
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Start deheader operation with supplied parameters
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="args">String array representing command line parameters</param>
|
2016-06-14 01:40:47 -07:00
|
|
|
|
public static void Main(string[] args)
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-06-09 23:35:12 -07:00
|
|
|
|
// If output is being redirected, don't allow clear screens
|
|
|
|
|
|
if (!Console.IsOutputRedirected)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-09 04:07:46 -07:00
|
|
|
|
// Perform initial setup and verification
|
2016-06-17 11:47:30 -07:00
|
|
|
|
Logger logger = new Logger(true, "headerer.log");
|
2016-04-09 04:07:46 -07:00
|
|
|
|
logger.Start();
|
2016-06-13 20:59:38 -07:00
|
|
|
|
DBTools.EnsureDatabase(_dbName, _connectionString);
|
2016-04-09 04:07:46 -07:00
|
|
|
|
|
2016-04-18 16:32:17 -07:00
|
|
|
|
// Credits take precidence over all
|
|
|
|
|
|
if ((new List<string>(args)).Contains("--credits"))
|
|
|
|
|
|
{
|
|
|
|
|
|
Build.Credits();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-17 11:47:30 -07:00
|
|
|
|
// If we have no arguments, show the help
|
|
|
|
|
|
if (args.Length == 0)
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-04-06 14:19:01 -07:00
|
|
|
|
Build.Help();
|
2016-04-09 04:10:31 -07:00
|
|
|
|
logger.Close();
|
2016-03-28 01:01:56 -07:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-18 20:04:38 -07:00
|
|
|
|
// Output the title
|
|
|
|
|
|
Build.Start("Headerer");
|
|
|
|
|
|
|
2016-03-28 01:29:55 -07:00
|
|
|
|
// Get the filename (or foldername)
|
2016-06-17 11:47:30 -07:00
|
|
|
|
string input = "";
|
|
|
|
|
|
bool help = false,
|
|
|
|
|
|
flag = true,
|
|
|
|
|
|
headerer = true;
|
|
|
|
|
|
foreach (string arg in args)
|
2016-03-28 18:23:49 -07:00
|
|
|
|
{
|
2016-06-17 11:47:30 -07:00
|
|
|
|
string temparg = arg.Replace("\"", "").Replace("file://", "");
|
|
|
|
|
|
switch (temparg)
|
2016-03-30 02:36:23 -07:00
|
|
|
|
{
|
2016-06-17 11:47:30 -07:00
|
|
|
|
case "-?":
|
|
|
|
|
|
case "-h":
|
|
|
|
|
|
case "--help":
|
|
|
|
|
|
help = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "-e":
|
|
|
|
|
|
case "--extract":
|
|
|
|
|
|
flag = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "-r":
|
|
|
|
|
|
case "--replace":
|
|
|
|
|
|
flag = false;
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
2016-08-29 13:51:45 -07:00
|
|
|
|
if (System.IO.File.Exists(temparg) || Directory.Exists(temparg))
|
2016-06-17 11:47:30 -07:00
|
|
|
|
{
|
|
|
|
|
|
input = temparg;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.Error("Invalid input detected: " + arg);
|
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
Build.Help();
|
2016-08-26 21:14:01 -07:00
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
logger.Error("Invalid input detected: " + arg);
|
2016-06-17 11:47:30 -07:00
|
|
|
|
logger.Close();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2016-03-30 02:36:23 -07:00
|
|
|
|
}
|
2016-06-17 11:47:30 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If help is set, show the help screen
|
|
|
|
|
|
if (help)
|
|
|
|
|
|
{
|
|
|
|
|
|
Build.Help();
|
|
|
|
|
|
logger.Close();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
|
2016-06-17 11:47:30 -07:00
|
|
|
|
// If a switch that requires a filename is set and no file is, show the help screen
|
|
|
|
|
|
if (String.IsNullOrEmpty(input) && (headerer))
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.Error("This feature requires at exactly one input");
|
|
|
|
|
|
Build.Help();
|
|
|
|
|
|
logger.Close();
|
|
|
|
|
|
return;
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
2016-03-30 02:36:23 -07:00
|
|
|
|
|
2016-06-17 11:47:30 -07:00
|
|
|
|
// If we're in headerer mode
|
|
|
|
|
|
if (headerer)
|
|
|
|
|
|
{
|
|
|
|
|
|
InitHeaderer(input, flag, logger);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If nothing is set, show the help
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Build.Help();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.Close();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Wrap extracting and replacing headers
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input">Input file or folder name</param>
|
|
|
|
|
|
/// <param name="flag">True if we're extracting headers (default), false if we're replacing them</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for file and console output</param>
|
|
|
|
|
|
private static void InitHeaderer(string input, bool flag, Logger logger)
|
|
|
|
|
|
{
|
|
|
|
|
|
Headerer headerer = new Headerer(input, flag, logger);
|
|
|
|
|
|
headerer.Process();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Extract and remove or replace headers
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>True if it succeeded, false otherwise</returns>
|
|
|
|
|
|
public bool Process()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_flag)
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-03-30 02:36:23 -07:00
|
|
|
|
// If it's a single file, just check it
|
2016-08-29 13:51:45 -07:00
|
|
|
|
if (System.IO.File.Exists(_input))
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-06-17 11:47:30 -07:00
|
|
|
|
DetectSkipperAndTransform(_input);
|
2016-03-30 02:36:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
// If it's a directory, recursively check all
|
2016-06-17 11:47:30 -07:00
|
|
|
|
else if (Directory.Exists(_input))
|
2016-03-30 02:36:23 -07:00
|
|
|
|
{
|
2016-06-17 11:47:30 -07:00
|
|
|
|
foreach (string sub in Directory.EnumerateFiles(_input, "*", SearchOption.AllDirectories))
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-03-30 02:36:23 -07:00
|
|
|
|
if (sub != ".." && sub != ".")
|
|
|
|
|
|
{
|
2016-06-17 11:02:38 -07:00
|
|
|
|
DetectSkipperAndTransform(sub);
|
2016-03-30 02:36:23 -07:00
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-03-30 02:36:23 -07:00
|
|
|
|
// If it's a single file, just check it
|
2016-08-29 13:51:45 -07:00
|
|
|
|
if (System.IO.File.Exists(_input))
|
2016-03-30 02:36:23 -07:00
|
|
|
|
{
|
2016-06-17 11:47:30 -07:00
|
|
|
|
ReplaceHeader(_input);
|
2016-03-30 02:36:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
// If it's a directory, recursively check all
|
2016-06-17 11:47:30 -07:00
|
|
|
|
else if (Directory.Exists(_input))
|
2016-03-30 02:36:23 -07:00
|
|
|
|
{
|
2016-06-17 11:47:30 -07:00
|
|
|
|
foreach (string sub in Directory.GetFiles(_input))
|
2016-03-30 02:36:23 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (sub != ".." && sub != ".")
|
|
|
|
|
|
{
|
|
|
|
|
|
ReplaceHeader(sub);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
2016-06-17 11:47:30 -07:00
|
|
|
|
|
|
|
|
|
|
return true;
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
2016-06-17 11:02:38 -07:00
|
|
|
|
/// Detect header skipper compliance and create an output file
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="file">Name of the file to be parsed</param>
|
2016-06-17 11:02:38 -07:00
|
|
|
|
/// <returns>True if the output file was created, false otherwise</returns>
|
2016-06-17 11:47:30 -07:00
|
|
|
|
public bool DetectSkipperAndTransform(string file)
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-06-17 11:47:30 -07:00
|
|
|
|
_logger.User("\nGetting skipper information for '" + file + "'");
|
2016-06-17 11:02:38 -07:00
|
|
|
|
|
|
|
|
|
|
// Then, if the file was headered, store it to the database
|
2016-06-14 01:40:47 -07:00
|
|
|
|
int headerSize = -1;
|
2016-06-17 11:47:30 -07:00
|
|
|
|
HeaderType type = Skippers.GetFileHeaderType(file, out headerSize, _logger);
|
2016-03-28 01:01:56 -07:00
|
|
|
|
|
2016-06-14 01:40:47 -07:00
|
|
|
|
// If we have a valid HeaderType, remove the correct byte count
|
2016-06-17 11:47:30 -07:00
|
|
|
|
_logger.User("File has header: " + (type != HeaderType.None));
|
2016-04-09 00:34:37 -07:00
|
|
|
|
if (type != HeaderType.None)
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-06-17 11:47:30 -07:00
|
|
|
|
_logger.Log("Deteched header type: " + type);
|
2016-03-28 01:29:55 -07:00
|
|
|
|
|
2016-06-14 01:40:47 -07:00
|
|
|
|
// Now take care of the header and new output file
|
|
|
|
|
|
string hstr = string.Empty;
|
2016-08-29 13:51:45 -07:00
|
|
|
|
using (BinaryReader br = new BinaryReader(System.IO.File.OpenRead(file)))
|
2016-03-28 18:23:49 -07:00
|
|
|
|
{
|
2016-06-14 01:40:47 -07:00
|
|
|
|
// Extract the header as a string for the database
|
|
|
|
|
|
byte[] hbin = br.ReadBytes(headerSize);
|
|
|
|
|
|
for (int i = 0; i < headerSize; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
hstr += BitConverter.ToString(new byte[] { hbin[i] });
|
|
|
|
|
|
}
|
2016-03-28 18:23:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-17 11:02:38 -07:00
|
|
|
|
// Then find an apply the exact rule to the file
|
2016-06-17 11:47:30 -07:00
|
|
|
|
SkipperRule rule = Skippers.MatchesSkipper(file, "", _logger);
|
2016-06-17 11:02:38 -07:00
|
|
|
|
|
|
|
|
|
|
// If we have an empty rule, return false
|
|
|
|
|
|
if (rule.Tests == null || rule.Tests.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Otherwise, apply the rule ot the file
|
2016-06-21 10:41:28 -07:00
|
|
|
|
string newfile = file + ".new";
|
|
|
|
|
|
Skippers.TransformFile(file, newfile, rule, _logger);
|
2016-06-17 11:02:38 -07:00
|
|
|
|
|
|
|
|
|
|
// If the output file doesn't exist, return false
|
2016-08-29 13:51:45 -07:00
|
|
|
|
if (!System.IO.File.Exists(newfile))
|
2016-06-17 11:02:38 -07:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2016-03-30 02:36:23 -07:00
|
|
|
|
|
|
|
|
|
|
// Now add the information to the database if it's not already there
|
2016-08-29 13:57:46 -07:00
|
|
|
|
Helper.Rom rom = RomTools.GetSingleFileInfo(newfile);
|
2016-08-29 13:05:32 -07:00
|
|
|
|
AddHeaderToDatabase(hstr, rom.HashData.SHA1, type);
|
2016-06-14 01:40:47 -07:00
|
|
|
|
}
|
2016-06-17 11:02:38 -07:00
|
|
|
|
|
|
|
|
|
|
return true;
|
2016-06-14 01:40:47 -07:00
|
|
|
|
}
|
2016-03-30 02:36:23 -07:00
|
|
|
|
|
2016-06-14 01:40:47 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Add a header to the database
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="header">String representing the header bytes</param>
|
|
|
|
|
|
/// <param name="SHA1">SHA-1 of the deheadered file</param>
|
|
|
|
|
|
/// <param name="type">HeaderType representing the detected header</param>
|
2016-06-17 11:47:30 -07:00
|
|
|
|
public void AddHeaderToDatabase(string header, string SHA1, HeaderType type)
|
2016-06-14 01:40:47 -07:00
|
|
|
|
{
|
|
|
|
|
|
bool exists = false;
|
|
|
|
|
|
|
|
|
|
|
|
string query = @"SELECT * FROM data WHERE sha1='" + SHA1 + "' AND header='" + header + "'";
|
|
|
|
|
|
using (SqliteConnection dbc = new SqliteConnection(_connectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
dbc.Open();
|
|
|
|
|
|
using (SqliteCommand slc = new SqliteCommand(query, dbc))
|
2016-03-30 02:36:23 -07:00
|
|
|
|
{
|
2016-06-14 01:40:47 -07:00
|
|
|
|
using (SqliteDataReader sldr = slc.ExecuteReader())
|
2016-03-30 02:36:23 -07:00
|
|
|
|
{
|
2016-06-14 01:40:47 -07:00
|
|
|
|
exists = sldr.HasRows;
|
2016-03-30 02:36:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-06-14 01:40:47 -07:00
|
|
|
|
}
|
2016-03-30 02:36:23 -07:00
|
|
|
|
|
2016-06-14 01:40:47 -07:00
|
|
|
|
if (!exists)
|
|
|
|
|
|
{
|
|
|
|
|
|
query = @"INSERT INTO data (sha1, header, type) VALUES ('" +
|
|
|
|
|
|
SHA1 + "', " +
|
|
|
|
|
|
"'" + header + "', " +
|
|
|
|
|
|
"'" + type.ToString() + "')";
|
|
|
|
|
|
using (SqliteConnection dbc = new SqliteConnection(_connectionString))
|
2016-03-30 02:36:23 -07:00
|
|
|
|
{
|
2016-06-14 01:40:47 -07:00
|
|
|
|
dbc.Open();
|
|
|
|
|
|
using (SqliteCommand slc = new SqliteCommand(query, dbc))
|
2016-03-30 02:36:23 -07:00
|
|
|
|
{
|
2016-06-17 11:47:30 -07:00
|
|
|
|
_logger.Log("Result of inserting header: " + slc.ExecuteNonQuery());
|
2016-03-30 02:36:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-30 02:36:23 -07:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-03-31 12:38:58 -07:00
|
|
|
|
/// Detect and replace header(s) to the given file
|
2016-03-30 02:36:23 -07:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="file">Name of the file to be parsed</param>
|
2016-06-17 11:47:30 -07:00
|
|
|
|
/// <returns>True if a header was found and appended, false otherwise</returns>
|
|
|
|
|
|
public bool ReplaceHeader(string file)
|
2016-03-30 02:36:23 -07:00
|
|
|
|
{
|
|
|
|
|
|
// First, get the SHA-1 hash of the file
|
2016-08-29 13:57:46 -07:00
|
|
|
|
Helper.Rom rom = RomTools.GetSingleFileInfo(file);
|
2016-03-30 02:36:23 -07:00
|
|
|
|
|
2016-03-31 12:38:58 -07:00
|
|
|
|
// Then try to pull the corresponding headers from the database
|
2016-03-30 02:36:23 -07:00
|
|
|
|
string header = "";
|
|
|
|
|
|
|
2016-08-29 13:05:32 -07:00
|
|
|
|
string query = @"SELECT header, type FROM data WHERE sha1='" + rom.HashData.SHA1 + "'";
|
2016-04-20 17:02:15 -07:00
|
|
|
|
using (SqliteConnection dbc = new SqliteConnection(_connectionString))
|
2016-03-30 02:36:23 -07:00
|
|
|
|
{
|
|
|
|
|
|
dbc.Open();
|
2016-04-20 17:02:15 -07:00
|
|
|
|
using (SqliteCommand slc = new SqliteCommand(query, dbc))
|
2016-03-30 02:36:23 -07:00
|
|
|
|
{
|
2016-04-20 17:02:15 -07:00
|
|
|
|
using (SqliteDataReader sldr = slc.ExecuteReader())
|
2016-03-30 02:36:23 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (sldr.HasRows)
|
|
|
|
|
|
{
|
2016-03-31 12:38:58 -07:00
|
|
|
|
int sub = 0;
|
|
|
|
|
|
while (sldr.Read())
|
|
|
|
|
|
{
|
2016-06-17 11:47:30 -07:00
|
|
|
|
_logger.Log("Found match with rom type " + sldr.GetString(1));
|
2016-03-31 12:38:58 -07:00
|
|
|
|
header = sldr.GetString(0);
|
|
|
|
|
|
|
2016-06-17 11:47:30 -07:00
|
|
|
|
_logger.User("Creating reheadered file: " + file + ".new" + sub);
|
2016-06-14 01:40:47 -07:00
|
|
|
|
Output.AppendBytesToFile(file, file + ".new" + sub, header, string.Empty);
|
2016-06-17 11:47:30 -07:00
|
|
|
|
_logger.User("Reheadered file created!");
|
2016-03-31 12:38:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-06-17 11:47:30 -07:00
|
|
|
|
_logger.Warning("No matching header could be found!");
|
|
|
|
|
|
return false;
|
2016-03-30 02:36:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-06-17 11:47:30 -07:00
|
|
|
|
|
|
|
|
|
|
return true;
|
2016-03-30 02:36:23 -07:00
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|