Files
SabreTools/Headerer/Restore.cs

129 lines
5.1 KiB
C#
Raw Normal View History

2024-10-24 20:26:18 -04:00
using System;
2020-12-08 14:53:49 -08:00
using System.Collections.Generic;
2024-10-24 20:26:18 -04:00
using System.IO;
2024-03-04 23:56:05 -05:00
using Microsoft.Data.Sqlite;
using SabreTools.Hashing;
2024-10-24 00:46:28 -04:00
using SabreTools.IO.Extensions;
2020-07-31 23:17:12 -07:00
2024-10-24 20:26:18 -04:00
namespace Headerer
2020-07-31 23:17:12 -07:00
{
2024-10-24 20:26:18 -04:00
internal static class Restore
2020-07-31 23:17:12 -07:00
{
2020-12-08 14:53:49 -08:00
/// <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>
/// <returns>True if a header was found and appended, false otherwise</returns>
2024-10-24 20:26:18 -04:00
public static bool RestoreHeader(string file, string? outDir)
2020-12-08 14:53:49 -08:00
{
2021-02-09 21:22:56 -08:00
// Create the output directory if it doesn't exist
if (!string.IsNullOrWhiteSpace(outDir) && !Directory.Exists(outDir))
Directory.CreateDirectory(outDir);
2020-12-08 14:53:49 -08:00
// First, get the SHA-1 hash of the file
2024-10-23 22:10:55 -04:00
string sha1 = HashTool.GetFileHash(file, HashType.SHA1) ?? string.Empty;
2020-12-08 14:53:49 -08:00
// Retrieve a list of all related headers from the database
2024-10-23 22:10:55 -04:00
List<string> headers = RetrieveHeadersFromDatabase(sha1);
2020-12-08 14:53:49 -08:00
// 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++)
{
string outputFile = (string.IsNullOrWhiteSpace(outDir) ? $"{Path.GetFullPath(file)}.new" : Path.Combine(outDir, Path.GetFileName(file))) + i;
2024-10-24 20:26:18 -04:00
Console.WriteLine($"Creating reheadered file: {outputFile}");
2024-10-24 00:46:28 -04:00
AppendBytes(file, outputFile, ByteArrayExtensions.StringToByteArray(headers[i]), null);
2024-10-24 20:26:18 -04:00
Console.WriteLine("Reheadered file created!");
2020-12-08 14:53:49 -08:00
}
return true;
}
/// <summary>
/// Retrieve headers from the database
/// </summary>
/// <param name="SHA1">SHA-1 of the deheadered file</param>
/// <returns>List of strings representing the headers to add</returns>
2024-10-24 20:26:18 -04:00
private static List<string> RetrieveHeadersFromDatabase(string SHA1)
2020-12-08 14:53:49 -08:00
{
// Ensure the database exists
2024-10-24 20:26:18 -04:00
Database.EnsureDatabase();
2020-12-08 14:53:49 -08:00
// Open the database connection
2024-10-24 20:26:18 -04:00
var dbc = new SqliteConnection(Database.HeadererConnectionString);
2020-12-08 14:53:49 -08:00
dbc.Open();
// Create the output list of headers
List<string> headers = [];
2020-12-08 14:53:49 -08:00
string query = $"SELECT header, type FROM data WHERE sha1='{SHA1}'";
2024-10-24 20:26:18 -04:00
var slc = new SqliteCommand(query, dbc);
2020-12-08 14:53:49 -08:00
SqliteDataReader sldr = slc.ExecuteReader();
if (sldr.HasRows)
{
while (sldr.Read())
{
2024-10-24 20:26:18 -04:00
Console.WriteLine($"Found match with rom type '{sldr.GetString(1)}'"); // TODO: Gate behind debug flag
2020-12-08 14:53:49 -08:00
headers.Add(sldr.GetString(0));
}
}
else
{
2024-10-24 20:26:18 -04:00
Console.Error.WriteLine("No matching header could be found!");
2020-12-08 14:53:49 -08:00
}
// Dispose of database objects
slc.Dispose();
sldr.Dispose();
dbc.Dispose();
return headers;
}
2020-12-08 14:53:49 -08:00
/// <summary>
/// Add an aribtrary number of bytes to the inputted file
/// </summary>
/// <param name="input">File to be appended to</param>
/// <param name="output">Outputted file</param>
/// <param name="bytesToAddToHead">Bytes to be added to head of file</param>
/// <param name="bytesToAddToTail">Bytes to be added to tail of file</param>
private static void AppendBytes(string input, string output, byte[]? bytesToAddToHead, byte[]? bytesToAddToTail)
2020-12-08 14:53:49 -08:00
{
// If any of the inputs are invalid, skip
if (!File.Exists(input))
return;
using FileStream fsr = File.OpenRead(input);
using FileStream fsw = File.OpenWrite(output);
AppendBytes(fsr, fsw, bytesToAddToHead, bytesToAddToTail);
2020-12-08 14:53:49 -08:00
}
/// <summary>
/// Add an aribtrary number of bytes to the inputted stream
/// </summary>
/// <param name="input">Stream to be appended to</param>
/// <param name="output">Outputted stream</param>
/// <param name="bytesToAddToHead">Bytes to be added to head of stream</param>
/// <param name="bytesToAddToTail">Bytes to be added to tail of stream</param>
private static void AppendBytes(Stream input, Stream output, byte[]? bytesToAddToHead, byte[]? bytesToAddToTail)
2020-12-08 14:53:49 -08:00
{
// Write out prepended bytes
if (bytesToAddToHead != null && bytesToAddToHead.Length > 0)
output.Write(bytesToAddToHead, 0, bytesToAddToHead.Length);
// Now copy the existing file over
input.CopyTo(output);
// Write out appended bytes
if (bytesToAddToTail != null && bytesToAddToTail.Length > 0)
output.Write(bytesToAddToTail, 0, bytesToAddToTail.Length);
}
2020-07-31 23:17:12 -07:00
}
}