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-03-30 01:31:07 -07:00
|
|
|
|
class Headerer
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-03-30 02:36:23 -07:00
|
|
|
|
private static string _dbName = "Headerer.sqlite";
|
|
|
|
|
|
private static string _connectionString = "Data Source=" + _dbName + ";Version = 3;";
|
2016-04-09 04:07:46 -07:00
|
|
|
|
private static 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-05-10 15:41:33 -07:00
|
|
|
|
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-03-28 18:23:49 -07:00
|
|
|
|
if (args.Length == 0 || args.Length > 2)
|
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-03-28 18:23:49 -07:00
|
|
|
|
string file = "";
|
2016-03-30 02:36:23 -07:00
|
|
|
|
bool deheader = true;
|
2016-03-28 18:23:49 -07:00
|
|
|
|
if (args.Length == 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
file = args[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-03-30 02:36:23 -07:00
|
|
|
|
|
|
|
|
|
|
if (args[0] == "-e")
|
|
|
|
|
|
{
|
|
|
|
|
|
deheader = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (args[0] == "-r")
|
|
|
|
|
|
{
|
|
|
|
|
|
deheader = false;
|
|
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
|
2016-03-30 02:36:23 -07:00
|
|
|
|
file = args[1];
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
2016-03-30 02:36:23 -07:00
|
|
|
|
|
|
|
|
|
|
if (deheader)
|
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
|
|
|
|
|
|
if (File.Exists(file))
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-03-30 02:36:23 -07:00
|
|
|
|
DetectRemoveHeader(file);
|
|
|
|
|
|
}
|
|
|
|
|
|
// If it's a directory, recursively check all
|
|
|
|
|
|
else if (Directory.Exists(file))
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (string sub in Directory.GetFiles(file))
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-03-30 02:36:23 -07:00
|
|
|
|
if (sub != ".." && sub != ".")
|
|
|
|
|
|
{
|
|
|
|
|
|
DetectRemoveHeader(sub);
|
|
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-30 02:36:23 -07:00
|
|
|
|
// Else, show that help text
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-04-06 14:19:01 -07:00
|
|
|
|
Build.Help();
|
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
|
|
|
|
|
|
if (File.Exists(file))
|
|
|
|
|
|
{
|
|
|
|
|
|
ReplaceHeader(file);
|
|
|
|
|
|
}
|
|
|
|
|
|
// If it's a directory, recursively check all
|
|
|
|
|
|
else if (Directory.Exists(file))
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (string sub in Directory.GetFiles(file))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (sub != ".." && sub != ".")
|
|
|
|
|
|
{
|
|
|
|
|
|
ReplaceHeader(sub);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Else, show that help text
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-04-06 14:19:01 -07:00
|
|
|
|
Build.Help();
|
2016-03-30 02:36:23 -07:00
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
2016-04-09 04:10:31 -07:00
|
|
|
|
logger.Close();
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Detect and remove header from the given file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="file">Name of the file to be parsed</param>
|
2016-03-28 01:29:55 -07:00
|
|
|
|
private static void DetectRemoveHeader(string file)
|
2016-03-28 01:01:56 -07:00
|
|
|
|
{
|
2016-06-14 01:40:47 -07:00
|
|
|
|
// First get the HeaderType, if any
|
|
|
|
|
|
int headerSize = -1;
|
2016-06-17 01:22:22 -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-05-10 15:41:33 -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-04-09 04:10:31 -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;
|
|
|
|
|
|
using (BinaryReader br = new BinaryReader(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-14 01:40:47 -07:00
|
|
|
|
// Write out the remaining bytes to new file
|
2016-05-10 15:41:33 -07:00
|
|
|
|
logger.User("Creating unheadered file: " + file + ".new");
|
2016-06-14 01:40:47 -07:00
|
|
|
|
Output.RemoveBytesFromFile(file, file + ".new", headerSize, 0);
|
2016-05-10 15:41:33 -07:00
|
|
|
|
logger.User("Unheadered file created!");
|
2016-03-30 02:36:23 -07:00
|
|
|
|
|
|
|
|
|
|
// Now add the information to the database if it's not already there
|
2016-06-16 18:57:34 -07:00
|
|
|
|
Rom rom = RomTools.GetSingleFileInfo(file + ".new");
|
2016-06-14 01:40:47 -07:00
|
|
|
|
AddHeaderToDatabase(hstr, rom.SHA1, type);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
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>
|
|
|
|
|
|
private static void AddHeaderToDatabase(string header, string SHA1, HeaderType type)
|
|
|
|
|
|
{
|
|
|
|
|
|
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-14 01:40:47 -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>
|
|
|
|
|
|
private static void ReplaceHeader(string file)
|
|
|
|
|
|
{
|
|
|
|
|
|
// First, get the SHA-1 hash of the file
|
2016-06-16 18:57:34 -07:00
|
|
|
|
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-06-14 01:40:47 -07:00
|
|
|
|
string query = @"SELECT header, type FROM data WHERE sha1='" + rom.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-04-09 04:10:31 -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-05-10 15:41:33 -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-05-10 15:41:33 -07:00
|
|
|
|
logger.User("Reheadered file created!");
|
2016-03-31 12:38:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-04-09 04:10:31 -07:00
|
|
|
|
logger.Warning("No matching header could be found!");
|
2016-03-31 12:38:58 -07:00
|
|
|
|
return;
|
2016-03-30 02:36:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-28 01:01:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|