Files
SabreTools/Headerer/Extract.cs

72 lines
2.8 KiB
C#
Raw Normal View History

2024-10-24 20:26:18 -04:00
using System;
using System.IO;
2024-03-04 23:56:05 -05:00
using SabreTools.Hashing;
2024-10-24 00:46:28 -04:00
using SabreTools.IO.Extensions;
2020-12-08 14:53:49 -08:00
using SabreTools.Skippers;
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 Extract
2020-07-31 23:17:12 -07:00
{
2020-12-08 14:53:49 -08:00
/// <summary>
/// Detect header skipper compliance and create an output 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>
/// <param name="nostore">True if headers should not be stored in the database, false otherwise</param>
/// <param name="debug">Enable additional log statements for debugging</param>
2020-12-08 14:53:49 -08:00
/// <returns>True if the output file was created, false otherwise</returns>
public static bool DetectTransformStore(string file, string? outDir, bool nostore, bool debug = false)
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
2024-10-24 20:26:18 -04:00
Console.WriteLine($"\nGetting skipper information for '{file}'");
2020-12-08 14:53:49 -08:00
// Get the skipper rule that matches the file, if any
2020-12-10 21:29:17 -08:00
SkipperMatch.Init();
Rule rule = SkipperMatch.GetMatchingRule(file, string.Empty);
2020-12-08 14:53:49 -08:00
// If we have an empty rule, return false
if (rule.Tests == null || rule.Tests.Length == 0 || rule.Operation != HeaderSkipOperation.None)
2020-12-08 14:53:49 -08:00
return false;
2024-10-24 20:26:18 -04:00
Console.WriteLine("File has a valid copier header");
2020-12-08 14:53:49 -08:00
// Get the header bytes from the file first
string hstr;
try
{
// Extract the header as a string for the database
using var fs = File.OpenRead(file);
int startOffset = int.Parse(rule.StartOffset ?? "0");
byte[] hbin = new byte[startOffset];
fs.Read(hbin, 0, startOffset);
2024-10-24 00:46:28 -04:00
hstr = ByteArrayExtensions.ByteArrayToString(hbin)!;
2020-12-08 14:53:49 -08:00
}
catch
{
return false;
}
// Apply the rule to the file
string newfile = string.IsNullOrWhiteSpace(outDir) ? Path.GetFullPath(file) + ".new" : Path.Combine(outDir, Path.GetFileName(file));
2020-12-08 14:53:49 -08:00
rule.TransformFile(file, newfile);
// If the output file doesn't exist, return false
if (!File.Exists(newfile))
return false;
// Now add the information to the database if it's not already there
if (!nostore)
{
2024-10-23 22:10:55 -04:00
string sha1 = HashTool.GetFileHash(newfile, HashType.SHA1) ?? string.Empty;
2024-10-24 21:14:55 -04:00
Database.AddHeader(hstr, sha1, rule.SourceFile!, debug);
2020-12-08 14:53:49 -08:00
}
return true;
}
2020-07-31 23:17:12 -07:00
}
}