Reduce unnecessary code separation

This commit is contained in:
Matt Nadareski
2025-09-20 15:29:13 -04:00
parent 9eb74a411c
commit 799489d079
3 changed files with 38 additions and 106 deletions

View File

@@ -1,58 +0,0 @@
using System;
using System.Collections.Generic;
using BinaryObjectScanner.Data;
using BinaryObjectScanner.Interfaces;
using SabreTools.Serialization.Wrappers;
using Xunit;
namespace BinaryObjectScanner.Test.Data
{
public class FactoryTests
{
#region CreateDetectable
private static readonly List<WrapperType> _detectableTypes =
[
// WrapperType.AACSMediaKeyBlock, // TODO: Create wrapper to reenable test
// WrapperType.BDPlusSVM, // TODO: Create wrapper to reenable test
// WrapperType.CIA,
// WrapperType.Executable, // TODO: This needs to be split internally
WrapperType.LDSCRYPT,
// WrapperType.N3DS,
// WrapperType.Nitro,
// WrapperType.PlayJAudioFile, // TODO: Create wrapper to reenable test
WrapperType.RealArcadeInstaller,
WrapperType.RealArcadeMezzanine,
WrapperType.SFFS,
WrapperType.Textfile,
];
[Theory]
[MemberData(nameof(GenerateIDetectableTestData))]
public void CreateDetectableTests(WrapperType type, bool expectNull)
{
IDetectable? actual = Factory.CreateDetectable(type);
if (expectNull)
Assert.Null(actual);
else
Assert.NotNull(actual);
}
public static List<object?[]> GenerateIDetectableTestData()
{
var testData = new List<object?[]>() { new object?[] { null, true } };
foreach (WrapperType type in Enum.GetValues(typeof(WrapperType)))
{
if (_detectableTypes.Contains(type))
testData.Add([type, false]);
else
testData.Add([type, true]);
}
return testData;
}
#endregion
}
}

View File

@@ -1,47 +0,0 @@
using SabreTools.Serialization.Interfaces;
using SabreTools.Serialization.Wrappers;
namespace BinaryObjectScanner.Data
{
internal static class Factory
{
/// <summary>
/// Create an instance of a detectable based on file type
/// </summary>
public static Interfaces.IDetectable? CreateDetectable(WrapperType fileType)
=> CreateDetectable(fileType, null);
/// <summary>
/// Create an instance of a detectable based on file type
/// </summary>
public static Interfaces.IDetectable? CreateDetectable(WrapperType fileType, IWrapper? wrapper)
{
// Use the wrapper before the type
switch (wrapper)
{
case AACSMediaKeyBlock obj: return new FileType.AACSMediaKeyBlock(obj);
case BDPlusSVM obj: return new FileType.BDPlusSVM(obj);
// case CIA obj => new FileType.CIA(obj),
case LinearExecutable obj: return new FileType.LinearExecutable(obj);
case MSDOS obj: return new FileType.MSDOS(obj);
// case N3DS obj: return new FileType.N3DS(obj);
case NewExecutable obj: return new FileType.NewExecutable(obj);
case PlayJAudioFile obj: return new FileType.PLJ(obj);
case PortableExecutable obj: return new FileType.PortableExecutable(obj);
}
// Fall back on the file type for types not implemented in Serialization
return fileType switch
{
// WrapperType.CIA => new FileType.CIA(),
WrapperType.LDSCRYPT => new FileType.LDSCRYPT(),
// WrapperType.N3DS => new FileType.N3DS(),
WrapperType.RealArcadeInstaller => new FileType.RealArcadeInstaller(),
WrapperType.RealArcadeMezzanine => new FileType.RealArcadeMezzanine(),
WrapperType.SFFS => new FileType.SFFS(),
WrapperType.Textfile => new FileType.Textfile(),
_ => null,
};
}
}
}

View File

@@ -303,7 +303,7 @@ namespace BinaryObjectScanner
#region Non-Archive File Types
// Create a detectable for the given file type
var detectable = Factory.CreateDetectable(fileType, wrapper);
var detectable = CreateDetectable(fileType, wrapper);
// If we're scanning file contents
if (detectable != null && _scanContents)
@@ -433,5 +433,42 @@ namespace BinaryObjectScanner
}
#endregion
#region Helpers
/// <summary>
/// Create an instance of a detectable based on file type
/// </summary>
private static IDetectable? CreateDetectable(WrapperType fileType, IWrapper? wrapper)
{
// Use the wrapper before the type
switch (wrapper)
{
case AACSMediaKeyBlock obj: return new FileType.AACSMediaKeyBlock(obj);
case BDPlusSVM obj: return new FileType.BDPlusSVM(obj);
// case CIA obj => new FileType.CIA(obj),
case LinearExecutable obj: return new FileType.LinearExecutable(obj);
case MSDOS obj: return new FileType.MSDOS(obj);
// case N3DS obj: return new FileType.N3DS(obj);
case NewExecutable obj: return new FileType.NewExecutable(obj);
case PlayJAudioFile obj: return new FileType.PLJ(obj);
case PortableExecutable obj: return new FileType.PortableExecutable(obj);
}
// Fall back on the file type for types not implemented in Serialization
return fileType switch
{
// WrapperType.CIA => new FileType.CIA(),
WrapperType.LDSCRYPT => new FileType.LDSCRYPT(),
// WrapperType.N3DS => new FileType.N3DS(),
WrapperType.RealArcadeInstaller => new FileType.RealArcadeInstaller(),
WrapperType.RealArcadeMezzanine => new FileType.RealArcadeMezzanine(),
WrapperType.SFFS => new FileType.SFFS(),
WrapperType.Textfile => new FileType.Textfile(),
_ => null,
};
}
#endregion
}
}