Add VBSP wrapper, extraction, and use it

This commit is contained in:
Matt Nadareski
2022-12-26 10:58:16 -08:00
parent 50fe127a8d
commit 2875f7ff7a
5 changed files with 352 additions and 22 deletions

View File

@@ -20,30 +20,10 @@ namespace BurnOutSharp.Builders
public const int HL_VBSP_LUMP_ENTITIES = 0;
/// <summary>
/// Idnex for the pakfile lump
/// Index for the pakfile lump
/// </summary>
public const int HL_VBSP_LUMP_PAKFILE = 40;
/// <summary>
/// Zip local file header signature as an integer
/// </summary>
public const int HL_VBSP_ZIP_LOCAL_FILE_HEADER_SIGNATURE = 0x04034b50;
/// <summary>
/// Zip file header signature as an integer
/// </summary>
public const int HL_VBSP_ZIP_FILE_HEADER_SIGNATURE = 0x02014b50;
/// <summary>
/// Zip end of central directory record signature as an integer
/// </summary>
public const int HL_VBSP_ZIP_END_OF_CENTRAL_DIRECTORY_RECORD_SIGNATURE = 0x06054b50;
/// <summary>
/// Length of a ZIP checksum in bytes
/// </summary>
public const int HL_VBSP_ZIP_CHECKSUM_LENGTH = 0x00008000;
#endregion
#region Byte Data

View File

@@ -0,0 +1,251 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace BurnOutSharp.Wrappers
{
public class VBSP : WrapperBase
{
#region Pass-Through Properties
/// <inheritdoc cref="Models.VBSP.Header.Signature"/>
public string Signature => _file.Header.Signature;
/// <inheritdoc cref="Models.VBSP.Header.Version"/>
public int Version => _file.Header.Version;
/// <inheritdoc cref="Models.VBSP.File.Lumps"/>
public Models.VBSP.Lump[] Lumps => _file.Header.Lumps;
/// <inheritdoc cref="Models.VBSP.Header.MapRevision"/>
public int MapRevision => _file.Header.MapRevision;
#endregion
#region Extension Properties
// TODO: Figure out what extension oroperties are needed
#endregion
#region Instance Variables
/// <summary>
/// Internal representation of the VBSP
/// </summary>
private Models.VBSP.File _file;
#endregion
#region Constructors
/// <summary>
/// Private constructor
/// </summary>
private VBSP() { }
/// <summary>
/// Create a VBSP from a byte array and offset
/// </summary>
/// <param name="data">Byte array representing the VBSP</param>
/// <param name="offset">Offset within the array to parse</param>
/// <returns>A VBSP wrapper on success, null on failure</returns>
public static VBSP Create(byte[] data, int offset)
{
// If the data is invalid
if (data == null)
return null;
// If the offset is out of bounds
if (offset < 0 || offset >= data.Length)
return null;
// Create a memory stream and use that
MemoryStream dataStream = new MemoryStream(data, offset, data.Length - offset);
return Create(dataStream);
}
/// <summary>
/// Create a VBSP from a Stream
/// </summary>
/// <param name="data">Stream representing the VBSP</param>
/// <returns>An VBSP wrapper on success, null on failure</returns>
public static VBSP Create(Stream data)
{
// If the data is invalid
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
var file = Builders.VBSP.ParseFile(data);
if (file == null)
return null;
var wrapper = new VBSP
{
_file = file,
_dataSource = DataSource.Stream,
_streamData = data,
};
return wrapper;
}
#endregion
#region Printing
/// <inheritdoc/>
public override void Print()
{
Console.WriteLine("VBSP Information:");
Console.WriteLine("-------------------------");
Console.WriteLine();
PrintHeader();
PrintLumps();
}
/// <summary>
/// Print header information
/// </summary>
/// <remarks>Slightly out of order due to the lumps</remarks>
private void PrintHeader()
{
Console.WriteLine(" Header Information:");
Console.WriteLine(" -------------------------");
Console.WriteLine($" Signature: {Signature}");
Console.WriteLine($" Version: {Version}");
Console.WriteLine($" Map revision: {MapRevision}");
Console.WriteLine();
}
/// <summary>
/// Print lumps information
/// </summary>
/// <remarks>Technically part of the header</remarks>
private void PrintLumps()
{
Console.WriteLine(" Lumps Information:");
Console.WriteLine(" -------------------------");
if (Lumps == null || Lumps.Length == 0)
{
Console.WriteLine(" No lumps");
}
else
{
for (int i = 0; i < Lumps.Length; i++)
{
var lump = Lumps[i];
string specialLumpName = string.Empty;
switch (i)
{
case Builders.VBSP.HL_VBSP_LUMP_ENTITIES:
specialLumpName = " (entities)";
break;
case Builders.VBSP.HL_VBSP_LUMP_PAKFILE:
specialLumpName = " (pakfile)";
break;
}
Console.WriteLine($" Lump {i}{specialLumpName}");
Console.WriteLine($" Offset: {lump.Offset}");
Console.WriteLine($" Length: {lump.Length}");
Console.WriteLine($" Version: {lump.Version}");
Console.WriteLine($" 4CC: {string.Join(", ", lump.FourCC)}");
}
}
Console.WriteLine();
}
#endregion
#region Extraction
/// <summary>
/// Extract all lumps from the VBSP to an output directory
/// </summary>
/// <param name="outputDirectory">Output directory to write to</param>
/// <returns>True if all lumps extracted, false otherwise</returns>
public bool ExtractAllLumps(string outputDirectory)
{
// If we have no lumps
if (Lumps == null || Lumps.Length == 0)
return false;
// Loop through and extract all lumps to the output
bool allExtracted = true;
for (int i = 0; i < Lumps.Length; i++)
{
allExtracted &= ExtractLump(i, outputDirectory);
}
return allExtracted;
}
/// <summary>
/// Extract a lump from the VBSP to an output directory by index
/// </summary>
/// <param name="index">Lump index to extract</param>
/// <param name="outputDirectory">Output directory to write to</param>
/// <returns>True if the lump extracted, false otherwise</returns>
public bool ExtractLump(int index, string outputDirectory)
{
// If we have no lumps
if (Lumps == null || Lumps.Length == 0)
return false;
// If the lumps index is invalid
if (index < 0 || index >= Lumps.Length)
return false;
// Get the lump
var lump = Lumps[index];
if (lump == null)
return false;
// Read the data
byte[] data = ReadFromDataSource((int)lump.Offset, (int)lump.Length);
if (data == null)
return false;
// Create the filename
string filename = $"lump_{index}.bin";
switch (index)
{
case Builders.VBSP.HL_VBSP_LUMP_ENTITIES:
filename = "entities.ent";
break;
case Builders.VBSP.HL_VBSP_LUMP_PAKFILE:
filename = "pakfile.zip";
break;
}
// If we have an invalid output directory
if (string.IsNullOrWhiteSpace(outputDirectory))
return false;
// Create the full output path
filename = Path.Combine(outputDirectory, filename);
// Ensure the output directory is created
Directory.CreateDirectory(Path.GetDirectoryName(filename));
// Try to write the data
try
{
// Open the output file for writing
using (Stream fs = File.OpenWrite(filename))
{
fs.Write(data, 0, data.Length);
}
}
catch
{
return false;
}
return true;
}
#endregion
}
}

View File

@@ -0,0 +1,69 @@
using System;
using System.Collections.Concurrent;
using System.IO;
using BurnOutSharp.Interfaces;
using static BurnOutSharp.Utilities.Dictionary;
namespace BurnOutSharp.FileType
{
/// <summary>
/// Half-Life 2 Level
/// </summary>
public class VBSP : IScannable
{
/// <inheritdoc/>
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, string file)
{
if (!File.Exists(file))
return null;
using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read))
{
return Scan(scanner, fs, file);
}
}
/// <inheritdoc/>
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, Stream stream, string file)
{
// If the VBSP file itself fails
try
{
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
// Create the wrapper
Wrappers.VBSP vbsp = Wrappers.VBSP.Create(stream);
if (vbsp == null)
return null;
// Loop through and extract all files
vbsp.ExtractAllLumps(tempPath);
// Collect and format all found protections
var protections = scanner.GetProtections(tempPath);
// If temp directory cleanup fails
try
{
Directory.Delete(tempPath, true);
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
// Remove temporary path references
StripFromKeys(protections, tempPath);
return protections;
}
catch (Exception ex)
{
if (scanner.IncludeDebug) Console.WriteLine(ex);
}
return null;
}
}
}

View File

@@ -637,7 +637,7 @@ namespace BurnOutSharp.Tools
case SupportedFileType.SGA: return new FileType.Valve();
case SupportedFileType.TapeArchive: return new FileType.TapeArchive();
case SupportedFileType.Textfile: return new FileType.Textfile();
case SupportedFileType.VBSP: return new FileType.Valve();
case SupportedFileType.VBSP: return new FileType.VBSP();
case SupportedFileType.VPK: return new FileType.VPK();
case SupportedFileType.WAD: return new FileType.Valve();
case SupportedFileType.XZ: return new FileType.XZ();

View File

@@ -419,6 +419,25 @@ namespace Test
pak.Print();
}
// VBSP
else if (IsVBSP(magic))
{
// Build the archive information
Console.WriteLine("Creating VBSP deserializer");
Console.WriteLine();
var vbsp = VBSP.Create(stream);
if (vbsp == null)
{
Console.WriteLine("Something went wrong parsing VBSP");
Console.WriteLine();
return;
}
// Print the VBSP info to screen
vbsp.Print();
}
// VPK
else if (IsVPK(magic))
{
@@ -571,6 +590,17 @@ namespace Test
return magic[0] == 'P' && magic[1] == 'E' && magic[2] == '\0' && magic[3] == '\0';
}
/// <summary>
/// Determine if the magic bytes indicate a VBSP
/// </summary>
private static bool IsVBSP(byte[] magic)
{
if (magic == null || magic.Length < 4)
return false;
return magic[0] == 'V' && magic[1] == 'B' && magic[2] == 'S' && magic[3] == 'P';
}
/// <summary>
/// Determine if the magic bytes indicate a VPK
/// </summary>