Use content matching helper, part 4

This commit is contained in:
Matt Nadareski
2021-03-21 22:19:38 -07:00
parent d01826ffa4
commit cf9bd99f3d
33 changed files with 370 additions and 159 deletions

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BurnOutSharp.Matching;
namespace BurnOutSharp.ProtectionType
{
@@ -10,17 +11,20 @@ namespace BurnOutSharp.ProtectionType
/// <inheritdoc/>
public string CheckContents(string file, byte[] fileContent, bool includePosition = false)
{
// CD-Cops, ver.
byte?[] check = new byte?[] { 0x43, 0x44, 0x2D, 0x43, 0x6F, 0x70, 0x73, 0x2C, 0x20, 0x20, 0x76, 0x65, 0x72, 0x2E, 0x20 };
if (fileContent.FirstPosition(check, out int position))
return $"CD-Cops {GetVersion(fileContent, position)}" + (includePosition ? $" (Index {position})" : string.Empty);
var matchers = new List<Matcher>
{
// CD-Cops, ver.
new Matcher(new byte?[]
{
0x43, 0x44, 0x2D, 0x43, 0x6F, 0x70, 0x73, 0x2C,
0x20, 0x20, 0x76, 0x65, 0x72, 0x2E, 0x20
}, GetVersion, "CD-Cops"),
// .grand + (char)0x00
check = new byte?[] { 0x2E, 0x67, 0x72, 0x61, 0x6E, 0x64, 0x00};
if (fileContent.FirstPosition(check, out position))
return "CD-Cops" + (includePosition ? $" (Index {position})" : string.Empty);
// .grand + (char)0x00
new Matcher(new byte?[] { 0x2E, 0x67, 0x72, 0x61, 0x6E, 0x64, 0x00 }, "CD-Cops"),
};
return null;
return Utilities.GetContentMatches(file, fileContent, matchers, includePosition);
}
/// <inheritdoc/>
@@ -53,7 +57,7 @@ namespace BurnOutSharp.ProtectionType
return null;
}
private static string GetVersion(byte[] fileContent, int position)
public static string GetVersion(string file, byte[] fileContent, int position)
{
char[] version = new ArraySegment<byte>(fileContent, position + 15, 4).Select(b => (char)b).ToArray();
if (version[0] == 0x00)