Skip to content

Commit

Permalink
Port code from SabreTools
Browse files Browse the repository at this point in the history
  • Loading branch information
mnadareski committed Feb 29, 2024
1 parent 7e65202 commit c4e3ec2
Show file tree
Hide file tree
Showing 24 changed files with 1,626 additions and 21 deletions.
24 changes: 24 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "shell",
"args": [
"build",
// Ask dotnet build to generate full paths for file names.
"/property:GenerateFullPaths=true",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
99 changes: 99 additions & 0 deletions Detector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace SabreTools.Skippers
{
[XmlRoot("detector")]
public abstract class Detector
{
#region Fields

/// <summary>
/// Detector name
/// </summary>
[XmlElement("name")]
public string? Name { get; protected set; }

/// <summary>
/// Author names
/// </summary>
[XmlElement("author")]
public string? Author { get; protected set; }

/// <summary>
/// File version
/// </summary>
[XmlElement("version")]
public string? Version { get; protected set; }

/// <summary>
/// Set of all rules in the skipper
/// </summary>
[XmlElement("rule")]
public Rule[]? Rules { get; protected set; }

/// <summary>
/// Filename the skipper lives in
/// </summary>
[XmlIgnore]
public string? SourceFile { get; protected set; }

#endregion

#region Matching

/// <summary>
/// Get the Rule associated with a given stream
/// </summary>
/// <param name="input">Stream to be checked</param>
/// <param name="skipperName">Name of the skipper to be used, blank to find a matching skipper</param>
/// <returns>The Rule that matched the stream, null otherwise</returns>
public Rule? GetMatchingRule(Stream input, string skipperName)
{
// If we have no name supplied, try to blindly match
if (string.IsNullOrEmpty(skipperName))
return GetMatchingRule(input);

// If the name matches the internal name of the skipper
else if (string.Equals(skipperName, Name, StringComparison.OrdinalIgnoreCase))
return GetMatchingRule(input);

// If the name matches the source file name of the skipper
else if (string.Equals(skipperName, SourceFile, StringComparison.OrdinalIgnoreCase))
return GetMatchingRule(input);

// Otherwise, nothing matches by default
return null;
}

/// <summary>
/// Get the matching Rule from all Rules, if possible
/// </summary>
/// <param name="input">Stream to be checked</param>
/// <returns>The Rule that matched the stream, null otherwise</returns>
private Rule? GetMatchingRule(Stream input)
{
// If we have no rules
if (Rules == null)
return null;

// Loop through the rules until one is found that works
foreach (Rule rule in Rules)
{
// Always reset the stream back to the original place
input.Seek(0, SeekOrigin.Begin);

// If all tests in the rule pass
if (rule.PassesAllTests(input))
return rule;
}

// If nothing passed
return null;
}

#endregion
}
}
33 changes: 33 additions & 0 deletions Detectors/Atari7800.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using SabreTools.Skippers.Tests;

namespace SabreTools.Skippers.Detectors
{
/// <summary>
/// Detector for Atari 7800 headers
/// </summary>
/// <remarks>Originally from a7800.xml</remarks>
internal class Atari7800 : Detector
{
public Atari7800()
{
// Create tests
var rule1Test1 = new DataTest("1", "415441524937383030", true);
var rule2Test1 = new DataTest("64", "41435455414C20434152542044415441205354415254532048455245", true);

// Create rules
var rule1 = new Rule("80", "EOF", HeaderSkipOperation.None, [rule1Test1], "a7800");
var rule2 = new Rule("80", "EOF", HeaderSkipOperation.None, [rule2Test1], "a7800");

// Create file
Name = "Atari 7800";
Author = "Roman Scherzer";
Version = "1.0";
SourceFile = "a7800";
Rules =
[
rule1,
rule2,
];
}
}
}
33 changes: 33 additions & 0 deletions Detectors/AtariLynx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using SabreTools.Skippers.Tests;

namespace SabreTools.Skippers.Detectors
{
/// <summary>
/// Detector for Atari Lynx headers
/// </summary>
/// <remarks>Originally from lynx.xml</remarks>
internal class AtariLynx : Detector
{
public AtariLynx()
{
// Create tests
var rule1Test1 = new DataTest("0", "4C594E58", true);
var rule2Test1 = new DataTest("6", "425339", true);

// Create rules
var rule1 = new Rule("40", "EOF", HeaderSkipOperation.None, [rule1Test1], "lynx");
var rule2 = new Rule("40", "EOF", HeaderSkipOperation.None, [rule2Test1], "lynx");

// Create file
Name = "Atari Lynx";
Author = "Roman Scherzer";
Version = "1.0";
SourceFile = "lynx";
Rules =
[
rule1,
rule2,
];
}
}
}
42 changes: 42 additions & 0 deletions Detectors/CommodorePSID.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using SabreTools.Skippers.Tests;

namespace SabreTools.Skippers.Detectors
{
/// <summary>
/// Detector for Commodore PSID headers
/// </summary>
/// <remarks>Originally from psid.xml</remarks>
internal class CommodorePSID : Detector
{
public CommodorePSID()
{
// Create tests
var rule1Test1 = new DataTest("0", "5053494400010076", true);
var rule2Test1 = new DataTest("0", "505349440003007c", true);
var rule3Test1 = new DataTest("0", "505349440002007c", true);
var rule4Test1 = new DataTest("0", "505349440001007c", true);
var rule5Test1 = new DataTest("0", "525349440002007c", true);

// Create rules
var rule1 = new Rule("76", "EOF", HeaderSkipOperation.None, [rule1Test1], "psid");
var rule2 = new Rule("76", "EOF", HeaderSkipOperation.None, [rule2Test1], "psid");
var rule3 = new Rule("7c", "EOF", HeaderSkipOperation.None, [rule3Test1], "psid");
var rule4 = new Rule("7c", "EOF", HeaderSkipOperation.None, [rule4Test1], "psid");
var rule5 = new Rule("7c", "EOF", HeaderSkipOperation.None, [rule5Test1], "psid");

// Create file
Name = "psid";
Author = "Yori Yoshizuki";
Version = "1.2";
SourceFile = "psid";
Rules =
[
rule1,
rule2,
rule3,
rule4,
rule5,
];
}
}
}
30 changes: 30 additions & 0 deletions Detectors/NECPCEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using SabreTools.Skippers.Tests;

namespace SabreTools.Skippers.Detectors
{
/// <summary>
/// Detector for NEC PC-Engine / TurboGrafx 16 headers
/// </summary>
/// <remarks>Originally from pce.xml</remarks>
internal class NECPCEngine : Detector
{
public NECPCEngine()
{
// Create tests
var rule1Test1 = new DataTest("0", "4000000000000000AABB02", true);

// Create rules
var rule1 = new Rule("200", null, HeaderSkipOperation.None, [rule1Test1], "pce");

// Create file
Name = "NEC TurboGrafx-16/PC-Engine";
Author = "Matt Nadareski (darksabre76)";
Version = "1.0";
SourceFile = "pce";
Rules =
[
rule1,
];
}
}
}
36 changes: 36 additions & 0 deletions Detectors/Nintendo64.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using SabreTools.Skippers.Tests;

namespace SabreTools.Skippers.Detectors
{
/// <summary>
/// Detector for Nintendo 64 headers
/// </summary>
/// <remarks>Originally from n64.xml</remarks>
internal class Nintendo64 : Detector
{
public Nintendo64()
{
// Create tests
var v64Test = new DataTest("0", "80371240", true);
var z64Test = new DataTest("0", "37804012", true);
var n64Test = new DataTest("0", "40123780", true);

// Create rules
var v64Rule = new Rule("0", "EOF", HeaderSkipOperation.None, [v64Test], "n64");
var z64Rule = new Rule("0", "EOF", HeaderSkipOperation.Byteswap, [z64Test], "n64");
var n64Rule = new Rule("0", "EOF", HeaderSkipOperation.Wordswap, [n64Test], "n64");

// Create file
Name = "Nintendo 64 - ABCD";
Author = "CUE";
Version = "1.1";
SourceFile = "n64";
Rules =
[
v64Rule,
z64Rule,
n64Rule,
];
}
}
}
30 changes: 30 additions & 0 deletions Detectors/NintendoEntertainmentSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using SabreTools.Skippers.Tests;

namespace SabreTools.Skippers.Detectors
{
/// <summary>
/// Detector for Nintendo Entertainment System headers
/// </summary>
/// <remarks>Originally from nes.xml</remarks>
internal class NintendoEntertainmentSystem : Detector
{
public NintendoEntertainmentSystem()
{
// Create tests
var inesTest = new DataTest("0", "4E45531A", true);

// Create rules
var inesRule = new Rule("10", "EOF", HeaderSkipOperation.None, [inesTest], "nes");

// Create file
Name = "Nintendo Famicon/NES";
Author = "Roman Scherzer";
Version = "1.1";
SourceFile = "nes";
Rules =
[
inesRule,
];
}
}
}
39 changes: 39 additions & 0 deletions Detectors/NintendoFamicomDiskSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using SabreTools.Skippers.Tests;

namespace SabreTools.Skippers.Detectors
{
/// <summary>
/// Detector for Nintendo Famicom Disk System headers
/// </summary>
/// <remarks>Originally from fds.xml</remarks>
internal class NintendoFamicomDiskSystem : Detector
{
public NintendoFamicomDiskSystem()
{
// Create tests
var rule1Test1 = new DataTest("0", "4644531A010000000000000000000000", true);
var rule2Test1 = new DataTest("0", "4644531A020000000000000000000000", true);
var rule3Test1 = new DataTest("0", "4644531A030000000000000000000000", true);
var rule4Test1 = new DataTest("0", "4644531A040000000000000000000000", true);

// Create rules
var rule1 = new Rule("10", null, HeaderSkipOperation.None, [rule1Test1], "fds");
var rule2 = new Rule("10", null, HeaderSkipOperation.None, [rule2Test1], "fds");
var rule3 = new Rule("10", null, HeaderSkipOperation.None, [rule3Test1], "fds");
var rule4 = new Rule("10", null, HeaderSkipOperation.None, [rule4Test1], "fds");

// Create file
Name = "fds";
Author = "Yori Yoshizuki";
Version = "1.0";
SourceFile = "fds";
Rules =
[
rule1,
rule2,
rule3,
rule4,
];
}
}
}
Loading

0 comments on commit c4e3ec2

Please sign in to comment.