Wrappers should never throw

This commit is contained in:
Matt Nadareski
2024-12-02 11:34:08 -05:00
parent 1ed0fe7dd3
commit 366e6ae171
13 changed files with 126 additions and 246 deletions

View File

@@ -22,27 +22,17 @@ namespace BinaryObjectScanner.FileType
/// <inheritdoc/>
public string? Detect(Stream stream, string file, bool includeDebug)
{
// If the MKB file itself fails
try
{
// Create the wrapper
var mkb = SabreTools.Serialization.Wrappers.AACSMediaKeyBlock.Create(stream);
if (mkb == null)
return null;
// Create the wrapper
var mkb = SabreTools.Serialization.Wrappers.AACSMediaKeyBlock.Create(stream);
if (mkb == null)
return null;
// Derive the version, if possible
var typeAndVersion = Array.Find(mkb.Records ?? [], r => r?.RecordType == SabreTools.Models.AACS.RecordType.TypeAndVersion);
if (typeAndVersion == null)
return "AACS (Unknown Version)";
else
return $"AACS {(typeAndVersion as SabreTools.Models.AACS.TypeAndVersionRecord)?.VersionNumber}";
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex);
}
return null;
// Derive the version, if possible
var typeAndVersion = Array.Find(mkb.Records ?? [], r => r?.RecordType == SabreTools.Models.AACS.RecordType.TypeAndVersion);
if (typeAndVersion == null)
return "AACS (Unknown Version)";
else
return $"AACS {(typeAndVersion as SabreTools.Models.AACS.TypeAndVersionRecord)?.VersionNumber}";
}
}
}

View File

@@ -1,4 +1,3 @@
using System;
using System.IO;
using BinaryObjectScanner.Interfaces;
@@ -22,23 +21,13 @@ namespace BinaryObjectScanner.FileType
/// <inheritdoc/>
public string? Detect(Stream stream, string file, bool includeDebug)
{
// If the BD+ file itself fails
try
{
// Create the wrapper
var svm = SabreTools.Serialization.Wrappers.BDPlusSVM.Create(stream);
if (svm == null)
return null;
// Create the wrapper
var svm = SabreTools.Serialization.Wrappers.BDPlusSVM.Create(stream);
if (svm == null)
return null;
// Return the formatted value
return $"BD+ {svm.Date}";
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex);
}
return null;
// Return the formatted value
return $"BD+ {svm.Date}";
}
}
}

View File

@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.IO;
using BinaryObjectScanner.Interfaces;
#if NET462_OR_GREATER || NETCOREAPP
using SharpCompress.Compressors;
@@ -26,24 +25,16 @@ namespace BinaryObjectScanner.FileType
/// <inheritdoc/>
public bool Extract(Stream? stream, string file, string outDir, bool includeDebug)
{
try
{
// Create the wrapper
var bfpk = SabreTools.Serialization.Wrappers.BFPK.Create(stream);
if (bfpk == null)
return false;
// Extract all files
Directory.CreateDirectory(outDir);
ExtractAll(bfpk, outDir);
return true;
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex);
// Create the wrapper
var bfpk = SabreTools.Serialization.Wrappers.BFPK.Create(stream);
if (bfpk == null)
return false;
}
// Extract all files
Directory.CreateDirectory(outDir);
ExtractAll(bfpk, outDir);
return true;
}
/// <summary>
@@ -119,12 +110,12 @@ namespace BinaryObjectScanner.FileType
fs.Write(data, 0, compressedSize);
}
#if NET462_OR_GREATER || NETCOREAPP
else
{
MemoryStream ms = new MemoryStream(data);
ZlibStream zs = new ZlibStream(ms, CompressionMode.Decompress);
zs.CopyTo(fs);
}
else
{
MemoryStream ms = new MemoryStream(data);
ZlibStream zs = new ZlibStream(ms, CompressionMode.Decompress);
zs.CopyTo(fs);
}
#endif
return true;

View File

@@ -1,4 +1,3 @@
using System;
using System.IO;
using BinaryObjectScanner.Interfaces;
@@ -22,26 +21,18 @@ namespace BinaryObjectScanner.FileType
/// <inheritdoc/>
public bool Extract(Stream? stream, string file, string outDir, bool includeDebug)
{
try
{
// Create the wrapper
var bsp = SabreTools.Serialization.Wrappers.BSP.Create(stream);
if (bsp == null)
return false;
// TODO: Introduce helper methods for all specialty lump types
// Loop through and extract all files
Directory.CreateDirectory(outDir);
bsp.ExtractAllLumps(outDir);
return true;
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex);
// Create the wrapper
var bsp = SabreTools.Serialization.Wrappers.BSP.Create(stream);
if (bsp == null)
return false;
}
// TODO: Introduce helper methods for all specialty lump types
// Loop through and extract all files
Directory.CreateDirectory(outDir);
bsp.ExtractAllLumps(outDir);
return true;
}
}
}

View File

@@ -1,4 +1,3 @@
using System;
using System.IO;
using BinaryObjectScanner.Interfaces;
@@ -22,24 +21,16 @@ namespace BinaryObjectScanner.FileType
/// <inheritdoc/>
public bool Extract(Stream? stream, string file, string outDir, bool includeDebug)
{
try
{
// Create the wrapper
var gcf = SabreTools.Serialization.Wrappers.GCF.Create(stream);
if (gcf == null)
return false;
// Loop through and extract all files
Directory.CreateDirectory(outDir);
gcf.ExtractAll(outDir);
return true;
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex);
// Create the wrapper
var gcf = SabreTools.Serialization.Wrappers.GCF.Create(stream);
if (gcf == null)
return false;
}
// Loop through and extract all files
Directory.CreateDirectory(outDir);
gcf.ExtractAll(outDir);
return true;
}
}
}

View File

@@ -1,4 +1,3 @@
using System;
using System.IO;
using BinaryObjectScanner.Interfaces;
@@ -22,24 +21,16 @@ namespace BinaryObjectScanner.FileType
/// <inheritdoc/>
public bool Extract(Stream? stream, string file, string outDir, bool includeDebug)
{
try
{
// Create the wrapper
var pak = SabreTools.Serialization.Wrappers.PAK.Create(stream);
if (pak == null)
return false;
// Loop through and extract all files
Directory.CreateDirectory(outDir);
pak.ExtractAll(outDir);
return true;
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex);
// Create the wrapper
var pak = SabreTools.Serialization.Wrappers.PAK.Create(stream);
if (pak == null)
return false;
}
// Loop through and extract all files
Directory.CreateDirectory(outDir);
pak.ExtractAll(outDir);
return true;
}
}
}

View File

@@ -1,4 +1,3 @@
using System;
using System.IO;
using BinaryObjectScanner.Interfaces;
@@ -22,24 +21,16 @@ namespace BinaryObjectScanner.FileType
/// <inheritdoc/>
public bool Extract(Stream? stream, string file, string outDir, bool includeDebug)
{
try
{
// Create the wrapper
var pff = SabreTools.Serialization.Wrappers.PFF.Create(stream);
if (pff == null)
return false;
// Extract all files
Directory.CreateDirectory(outDir);
pff.ExtractAll(outDir);
return true;
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex.Message);
// Create the wrapper
var pff = SabreTools.Serialization.Wrappers.PFF.Create(stream);
if (pff == null)
return false;
}
// Extract all files
Directory.CreateDirectory(outDir);
pff.ExtractAll(outDir);
return true;
}
}
}

View File

@@ -1,4 +1,3 @@
using System;
using System.IO;
using BinaryObjectScanner.Interfaces;
@@ -22,24 +21,16 @@ namespace BinaryObjectScanner.FileType
/// <inheritdoc/>
public bool Extract(Stream? stream, string file, string outDir, bool includeDebug)
{
try
{
// Create the wrapper
var quantum = SabreTools.Serialization.Wrappers.Quantum.Create(stream);
if (quantum == null)
return false;
// Extract all files
Directory.CreateDirectory(outDir);
ExtractAll(quantum, outDir);
return true;
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex.Message);
// Create the wrapper
var quantum = SabreTools.Serialization.Wrappers.Quantum.Create(stream);
if (quantum == null)
return false;
}
// Extract all files
Directory.CreateDirectory(outDir);
ExtractAll(quantum, outDir);
return true;
}
/// <summary>

View File

@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
using BinaryObjectScanner.Interfaces;
@@ -24,24 +23,16 @@ namespace BinaryObjectScanner.FileType
/// <inheritdoc/>
public bool Extract(Stream? stream, string file, string outDir, bool includeDebug)
{
try
{
// Create the wrapper
var sga = SabreTools.Serialization.Wrappers.SGA.Create(stream);
if (sga == null)
return false;
// Loop through and extract all files
Directory.CreateDirectory(outDir);
ExtractAll(sga, outDir);
return true;
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex);
// Create the wrapper
var sga = SabreTools.Serialization.Wrappers.SGA.Create(stream);
if (sga == null)
return false;
}
// Loop through and extract all files
Directory.CreateDirectory(outDir);
ExtractAll(sga, outDir);
return true;
}
/// <summary>

View File

@@ -1,4 +1,3 @@
using System;
using System.IO;
using BinaryObjectScanner.Interfaces;
@@ -22,26 +21,18 @@ namespace BinaryObjectScanner.FileType
/// <inheritdoc/>
public bool Extract(Stream? stream, string file, string outDir, bool includeDebug)
{
try
{
// Create the wrapper
var vbsp = SabreTools.Serialization.Wrappers.VBSP.Create(stream);
if (vbsp == null)
return false;
// TODO: Introduce helper methods for all specialty lump types
// Loop through and extract all files
Directory.CreateDirectory(outDir);
vbsp.ExtractAllLumps(outDir);
return true;
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex.ToString());
// Create the wrapper
var vbsp = SabreTools.Serialization.Wrappers.VBSP.Create(stream);
if (vbsp == null)
return false;
}
// TODO: Introduce helper methods for all specialty lump types
// Loop through and extract all files
Directory.CreateDirectory(outDir);
vbsp.ExtractAllLumps(outDir);
return true;
}
}
}

View File

@@ -1,4 +1,3 @@
using System;
using System.IO;
using BinaryObjectScanner.Interfaces;
@@ -22,24 +21,16 @@ namespace BinaryObjectScanner.FileType
/// <inheritdoc/>
public bool Extract(Stream? stream, string file, string outDir, bool includeDebug)
{
try
{
// Create the wrapper
var vpk = SabreTools.Serialization.Wrappers.VPK.Create(stream);
if (vpk == null)
return false;
// Loop through and extract all files
Directory.CreateDirectory(outDir);
vpk.ExtractAll(outDir);
return true;
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex);
// Create the wrapper
var vpk = SabreTools.Serialization.Wrappers.VPK.Create(stream);
if (vpk == null)
return false;
}
// Loop through and extract all files
Directory.CreateDirectory(outDir);
vpk.ExtractAll(outDir);
return true;
}
}
}

View File

@@ -1,4 +1,3 @@
using System;
using System.IO;
using BinaryObjectScanner.Interfaces;
@@ -22,24 +21,16 @@ namespace BinaryObjectScanner.FileType
/// <inheritdoc/>
public bool Extract(Stream? stream, string file, string outDir, bool includeDebug)
{
try
{
// Create the wrapper
var wad = SabreTools.Serialization.Wrappers.WAD3.Create(stream);
if (wad == null)
return false;
// Loop through and extract all files
Directory.CreateDirectory(outDir);
wad.ExtractAllLumps(outDir);
return true;
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex);
// Create the wrapper
var wad = SabreTools.Serialization.Wrappers.WAD3.Create(stream);
if (wad == null)
return false;
}
// Loop through and extract all files
Directory.CreateDirectory(outDir);
wad.ExtractAllLumps(outDir);
return true;
}
}
}

View File

@@ -1,4 +1,3 @@
using System;
using System.IO;
using BinaryObjectScanner.Interfaces;
@@ -22,24 +21,16 @@ namespace BinaryObjectScanner.FileType
/// <inheritdoc/>
public bool Extract(Stream? stream, string file, string outDir, bool includeDebug)
{
try
{
// Create the wrapper
var xzp = SabreTools.Serialization.Wrappers.XZP.Create(stream);
if (xzp == null)
return false;
// Loop through and extract all files
Directory.CreateDirectory(outDir);
xzp.ExtractAll(outDir);
return true;
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex);
// Create the wrapper
var xzp = SabreTools.Serialization.Wrappers.XZP.Create(stream);
if (xzp == null)
return false;
}
// Loop through and extract all files
Directory.CreateDirectory(outDir);
xzp.ExtractAll(outDir);
return true;
}
}
}