Files
SabreTools/SabreTools.Skippers/SkipperMatch.cs

121 lines
4.7 KiB
C#
Raw Normal View History

2020-12-08 14:53:49 -08:00
using System.Collections.Generic;
using System.IO;
namespace SabreTools.Skippers
{
/// <summary>
/// Class for matching existing Skippers
/// <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 SkipperMatch
{
/// <summary>
/// Header detectors represented by a list of detector objects
2020-12-08 14:53:49 -08:00
/// </summary>
private static List<Detector>? Skippers = null;
2020-12-08 14:53:49 -08:00
/// <summary>
/// Initialize static fields
/// </summary>
2024-02-29 00:48:09 -05:00
public static void Init()
2020-12-08 14:53:49 -08:00
{
2020-12-10 21:29:17 -08:00
// If the list is populated, don't add to it
if (Skippers != null)
return;
2024-02-29 00:48:09 -05:00
// Generate header skippers internally
PopulateSkippers();
2020-12-08 14:53:49 -08:00
}
/// <summary>
2020-12-10 21:29:17 -08:00
/// Populate the entire list of header skippers from generated objects
/// </summary>
/// <remarks>
/// http://mamedev.emulab.it/clrmamepro/docs/xmlheaders.txt
/// http://www.emulab.it/forum/index.php?topic=127.0
/// </remarks>
2024-02-29 00:48:09 -05:00
private static void PopulateSkippers()
{
// Ensure the list exists
2024-02-28 19:19:50 -05:00
Skippers ??= [];
// Get skippers for each known header type
Skippers.Add(new Detectors.Atari7800());
Skippers.Add(new Detectors.AtariLynx());
Skippers.Add(new Detectors.CommodorePSID());
Skippers.Add(new Detectors.NECPCEngine());
Skippers.Add(new Detectors.Nintendo64());
Skippers.Add(new Detectors.NintendoEntertainmentSystem());
Skippers.Add(new Detectors.NintendoFamicomDiskSystem());
Skippers.Add(new Detectors.SuperNintendoEntertainmentSystem());
Skippers.Add(new Detectors.SuperFamicomSPC());
}
2020-12-08 14:53:49 -08:00
/// <summary>
/// Get the Rule associated with a given file
2020-12-08 14:53:49 -08:00
/// </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 Rule that matched the file</returns>
public static Rule GetMatchingRule(string input, string skipperName)
2020-12-08 14:53:49 -08:00
{
// If the file doesn't exist, return a blank skipper rule
if (!File.Exists(input))
return new Rule();
2020-12-08 14:53:49 -08:00
return GetMatchingRule(File.OpenRead(input), skipperName);
}
/// <summary>
/// Get the Rule associated with a given stream
2020-12-08 14:53:49 -08:00
/// </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 Rule that matched the file</returns>
2024-02-28 19:19:50 -05:00
public static Rule GetMatchingRule(Stream? input, string skipperName, bool keepOpen = false)
2020-12-08 14:53:49 -08:00
{
var skipperRule = new Rule();
2020-12-08 14:53:49 -08:00
2024-02-28 19:19:50 -05:00
// If we have an invalid input
if (input == null || !input.CanRead)
return skipperRule;
// If we have an invalid set of skippers or skipper name
if (Skippers == null || skipperName == null)
2020-12-08 14:53:49 -08:00
return skipperRule;
// Loop through all known Detectors
foreach (Detector? skipper in Skippers)
2020-12-08 14:53:49 -08:00
{
// This should not happen
if (skipper == null)
continue;
2020-12-08 14:53:49 -08:00
skipperRule = skipper.GetMatchingRule(input, skipperName);
if (skipperRule != null)
break;
}
// If we're not keeping the stream open, dispose of the binary reader
if (!keepOpen)
2024-02-28 19:19:50 -05:00
input?.Dispose();
2020-12-08 14:53:49 -08:00
// If the Rule is null, make it empty
skipperRule ??= new Rule();
2020-12-08 14:53:49 -08:00
return skipperRule;
}
}
}