2016-03-29 12:11:58 -07:00
|
|
|
using System.Collections.Generic;
|
2020-06-10 22:37:19 -07:00
|
|
|
using System.IO;
|
2020-07-30 21:07:25 -07:00
|
|
|
|
2020-12-07 12:33:24 -08:00
|
|
|
using SabreTools.Data;
|
2020-08-27 20:56:50 -07:00
|
|
|
using SabreTools.Library.DatFiles;
|
2020-07-30 21:07:25 -07:00
|
|
|
using SabreTools.Library.FileTypes;
|
2020-10-07 15:42:30 -07:00
|
|
|
using SabreTools.Library.Logging;
|
2020-12-07 13:02:42 -08:00
|
|
|
using SabreTools.Library.Skippers;
|
2020-07-30 21:07:25 -07:00
|
|
|
using SabreTools.Library.Tools;
|
2016-10-24 13:51:39 -07:00
|
|
|
|
2020-12-07 13:02:42 -08:00
|
|
|
namespace SabreTools.Library.IO
|
2016-03-18 01:17:39 -07:00
|
|
|
{
|
2020-07-30 21:07:25 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Class for wrapping general file transformations
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Skippers, in general, are distributed as XML files by some projects
|
|
|
|
|
/// in order to denote a way of transforming a file so that it will match
|
|
|
|
|
/// the hashes included in their DATs. Each skipper file can contain multiple
|
|
|
|
|
/// skipper rules, each of which denote a type of header/transformation. In
|
|
|
|
|
/// turn, each of those rules can contain multiple tests that denote that
|
|
|
|
|
/// a file should be processed using that rule. Transformations can include
|
|
|
|
|
/// simply skipping over a portion of the file all the way to byteswapping
|
|
|
|
|
/// the entire file. For the purposes of this library, Skippers also denote
|
|
|
|
|
/// a way of changing files directly in order to produce a file whose external
|
|
|
|
|
/// hash would match those same DATs.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static class Transform
|
2019-02-08 21:01:54 -08:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2020-07-30 21:07:25 -07:00
|
|
|
/// Header skippers represented by a list of skipper objects
|
2019-02-08 21:01:54 -08:00
|
|
|
/// </summary>
|
2020-07-30 21:07:25 -07:00
|
|
|
private static List<SkipperFile> List;
|
2019-02-08 21:01:54 -08:00
|
|
|
|
2020-12-07 13:02:42 -08:00
|
|
|
/// <summary>
|
|
|
|
|
/// Local paths
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static string LocalPath = Path.Combine(Globals.ExeDir, "Skippers") + Path.DirectorySeparatorChar;
|
|
|
|
|
|
2020-10-07 15:42:30 -07:00
|
|
|
#region Logging
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Logging object
|
|
|
|
|
/// </summary>
|
2020-10-07 16:11:05 -07:00
|
|
|
private static readonly Logger logger = new Logger();
|
2020-10-07 15:42:30 -07:00
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initialize static fields
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void Init()
|
|
|
|
|
{
|
|
|
|
|
PopulateSkippers();
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 21:01:54 -08:00
|
|
|
/// <summary>
|
|
|
|
|
/// Populate the entire list of header Skippers
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// http://mamedev.emulab.it/clrmamepro/docs/xmlheaders.txt
|
|
|
|
|
/// http://www.emulab.it/forum/index.php?topic=127.0
|
|
|
|
|
/// </remarks>
|
|
|
|
|
private static void PopulateSkippers()
|
|
|
|
|
{
|
2020-07-15 09:41:59 -07:00
|
|
|
if (List == null)
|
2020-07-30 21:07:25 -07:00
|
|
|
List = new List<SkipperFile>();
|
2019-02-08 21:01:54 -08:00
|
|
|
|
|
|
|
|
foreach (string skipperFile in Directory.EnumerateFiles(LocalPath, "*", SearchOption.AllDirectories))
|
|
|
|
|
{
|
2020-07-30 21:07:25 -07:00
|
|
|
List.Add(new SkipperFile(Path.GetFullPath(skipperFile)));
|
2019-02-08 21:01:54 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-15 09:41:59 -07: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>
|
|
|
|
|
/// <returns>True if the output file was created, false otherwise</returns>
|
|
|
|
|
public static bool DetectTransformStore(string file, string outDir, bool nostore)
|
|
|
|
|
{
|
|
|
|
|
// Create the output directory if it doesn't exist
|
|
|
|
|
DirectoryExtensions.Ensure(outDir, create: true);
|
|
|
|
|
|
2020-10-07 15:42:30 -07:00
|
|
|
logger.User($"\nGetting skipper information for '{file}'");
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
|
|
|
// Get the skipper rule that matches the file, if any
|
|
|
|
|
SkipperRule rule = GetMatchingRule(file, string.Empty);
|
|
|
|
|
|
|
|
|
|
// If we have an empty rule, return false
|
|
|
|
|
if (rule.Tests == null || rule.Tests.Count == 0 || rule.Operation != HeaderSkipOperation.None)
|
|
|
|
|
return false;
|
|
|
|
|
|
2020-10-07 15:42:30 -07:00
|
|
|
logger.User("File has a valid copier header");
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
|
|
|
// Get the header bytes from the file first
|
|
|
|
|
string hstr;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Extract the header as a string for the database
|
|
|
|
|
#if NET_FRAMEWORK
|
|
|
|
|
using (var fs = FileExtensions.TryOpenRead(file))
|
|
|
|
|
{
|
|
|
|
|
#else
|
|
|
|
|
using var fs = FileExtensions.TryOpenRead(file);
|
|
|
|
|
#endif
|
|
|
|
|
byte[] hbin = new byte[(int)rule.StartOffset];
|
|
|
|
|
fs.Read(hbin, 0, (int)rule.StartOffset);
|
|
|
|
|
hstr = Utilities.ByteArrayToString(hbin);
|
|
|
|
|
#if NET_FRAMEWORK
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply the rule to the file
|
|
|
|
|
string newfile = (string.IsNullOrWhiteSpace(outDir) ? Path.GetFullPath(file) + ".new" : Path.Combine(outDir, Path.GetFileName(file)));
|
|
|
|
|
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)
|
|
|
|
|
{
|
2020-10-05 17:43:44 -07:00
|
|
|
BaseFile baseFile = FileExtensions.GetInfo(newfile, hashes: Hash.SHA1, asFiles: TreatAsFile.NonArchive);
|
2020-07-15 09:41:59 -07:00
|
|
|
DatabaseTools.AddHeaderToDatabase(hstr, Utilities.ByteArrayToString(baseFile.SHA1), rule.SourceFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
public static bool RestoreHeader(string file, string outDir)
|
|
|
|
|
{
|
|
|
|
|
// Create the output directory if it doesn't exist
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(outDir) && !Directory.Exists(outDir))
|
|
|
|
|
Directory.CreateDirectory(outDir);
|
|
|
|
|
|
|
|
|
|
// First, get the SHA-1 hash of the file
|
2020-10-05 17:43:44 -07:00
|
|
|
BaseFile baseFile = FileExtensions.GetInfo(file, hashes: Hash.SHA1, asFiles: TreatAsFile.NonArchive);
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
|
|
|
// Retrieve a list of all related headers from the database
|
|
|
|
|
List<string> headers = DatabaseTools.RetrieveHeadersFromDatabase(Utilities.ByteArrayToString(baseFile.SHA1));
|
|
|
|
|
|
|
|
|
|
// 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;
|
2020-10-07 15:42:30 -07:00
|
|
|
logger.User($"Creating reheadered file: {outputFile}");
|
2020-07-15 09:41:59 -07:00
|
|
|
FileExtensions.AppendBytes(file, outputFile, Utilities.StringToByteArray(headers[i]), null);
|
2020-10-07 15:42:30 -07:00
|
|
|
logger.User("Reheadered file created!");
|
2020-07-15 09:41:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 21:01:54 -08:00
|
|
|
/// <summary>
|
|
|
|
|
/// Get the SkipperRule associated with a given file
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">Name of the file to be checked</param>
|
|
|
|
|
/// <param name="skipperName">Name of the skipper to be used, blank to find a matching skipper</param>
|
|
|
|
|
/// <param name="logger">Logger object for file and console output</param>
|
|
|
|
|
/// <returns>The SkipperRule that matched the file</returns>
|
|
|
|
|
public static SkipperRule GetMatchingRule(string input, string skipperName)
|
|
|
|
|
{
|
|
|
|
|
// If the file doesn't exist, return a blank skipper rule
|
|
|
|
|
if (!File.Exists(input))
|
|
|
|
|
{
|
2020-10-07 15:42:30 -07:00
|
|
|
logger.Error($"The file '{input}' does not exist so it cannot be tested");
|
2019-02-08 21:01:54 -08:00
|
|
|
return new SkipperRule();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
return GetMatchingRule(FileExtensions.TryOpenRead(input), skipperName);
|
2019-02-08 21:01:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get the SkipperRule associated with a given stream
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">Name of the file to be checked</param>
|
|
|
|
|
/// <param name="skipperName">Name of the skipper to be used, blank to find a matching skipper</param>
|
|
|
|
|
/// <param name="keepOpen">True if the underlying stream should be kept open, false otherwise</param>
|
|
|
|
|
/// <returns>The SkipperRule that matched the file</returns>
|
|
|
|
|
public static SkipperRule GetMatchingRule(Stream input, string skipperName, bool keepOpen = false)
|
|
|
|
|
{
|
|
|
|
|
SkipperRule skipperRule = new SkipperRule();
|
|
|
|
|
|
|
|
|
|
// If we have a null skipper name, we return since we're not matching skippers
|
|
|
|
|
if (skipperName == null)
|
|
|
|
|
return skipperRule;
|
|
|
|
|
|
|
|
|
|
// Loop through and find a Skipper that has the right name
|
2020-10-07 15:42:30 -07:00
|
|
|
logger.Verbose("Beginning search for matching header skip rules");
|
2020-07-30 21:07:25 -07:00
|
|
|
List<SkipperFile> tempList = new List<SkipperFile>();
|
2019-02-08 21:01:54 -08:00
|
|
|
tempList.AddRange(List);
|
|
|
|
|
|
2020-07-30 22:32:16 -07:00
|
|
|
// Loop through all known SkipperFiles
|
2020-07-30 21:07:25 -07:00
|
|
|
foreach (SkipperFile skipper in tempList)
|
2019-02-08 21:01:54 -08:00
|
|
|
{
|
2020-07-30 22:32:16 -07:00
|
|
|
skipperRule = skipper.GetMatchingRule(input, skipperName);
|
|
|
|
|
if (skipperRule != null)
|
|
|
|
|
break;
|
2019-02-08 21:01:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we're not keeping the stream open, dispose of the binary reader
|
|
|
|
|
if (!keepOpen)
|
|
|
|
|
input.Dispose();
|
|
|
|
|
|
2020-07-30 22:32:16 -07:00
|
|
|
// If the SkipperRule is null, make it empty
|
|
|
|
|
if (skipperRule == null)
|
|
|
|
|
skipperRule = new SkipperRule();
|
|
|
|
|
|
2019-02-08 21:01:54 -08:00
|
|
|
// If we have a blank rule, inform the user
|
|
|
|
|
if (skipperRule.Tests == null)
|
2020-10-07 15:42:30 -07:00
|
|
|
logger.Verbose("No matching rule found!");
|
2020-07-30 22:32:16 -07:00
|
|
|
else
|
2020-10-07 15:42:30 -07:00
|
|
|
logger.User("Matching rule found!");
|
2019-02-08 21:01:54 -08:00
|
|
|
|
|
|
|
|
return skipperRule;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-30 21:07:25 -07:00
|
|
|
}
|