Files

494 lines
17 KiB
C#
Raw Permalink Normal View History

2022-12-24 20:15:58 -08:00
using System;
2022-12-24 22:02:30 -08:00
using System.Collections.Generic;
2022-12-24 20:15:58 -08:00
using System.IO;
2023-01-13 14:04:21 -08:00
using System.Text;
2023-03-07 16:59:14 -05:00
using static BinaryObjectScanner.Models.BSP.Constants;
2022-12-24 20:15:58 -08:00
2023-03-07 16:59:14 -05:00
namespace BinaryObjectScanner.Wrappers
2022-12-24 20:15:58 -08:00
{
public class BSP : WrapperBase
{
2023-01-18 11:18:53 -08:00
#region Descriptive Properties
/// <inheritdoc/>
public override string Description => "Half-Life Level (BSP)";
#endregion
2022-12-24 20:15:58 -08:00
#region Pass-Through Properties
#region Header
/// <inheritdoc cref="Models.BSP.Header.Version"/>
public uint Version => _file.Header.Version;
#endregion
#region Lumps
/// <inheritdoc cref="Models.BSP.File.Lumps"/>
2023-03-08 17:49:14 -05:00
public Models.BSP.Lump[] Lumps => _file.Lumps;
2022-12-24 20:15:58 -08:00
#endregion
#region Texture Header
/// <inheritdoc cref="Models.BSP.TextureHeader.TextureCount"/>
public uint TextureCount => _file.TextureHeader.TextureCount;
/// <inheritdoc cref="Models.BSP.TextureHeader.Offsets"/>
public uint[] Offsets => _file.TextureHeader.Offsets;
#endregion
2022-12-24 22:02:30 -08:00
#region Textures
/// <inheritdoc cref="Models.BSP.File.Textures"/>
2023-03-08 17:49:14 -05:00
public Models.BSP.Texture[] Textures => _file.Textures;
2022-12-24 22:02:30 -08:00
#endregion
2022-12-24 20:15:58 -08:00
#endregion
#region Instance Variables
/// <summary>
/// Internal representation of the BSP
/// </summary>
2023-03-08 17:49:14 -05:00
private Models.BSP.File _file;
2022-12-24 20:15:58 -08:00
#endregion
#region Constructors
/// <summary>
/// Private constructor
/// </summary>
private BSP() { }
/// <summary>
2022-12-25 21:27:06 -08:00
/// Create a BSP from a byte array and offset
2022-12-24 20:15:58 -08:00
/// </summary>
/// <param name="data">Byte array representing the BSP</param>
/// <param name="offset">Offset within the array to parse</param>
2022-12-25 21:27:06 -08:00
/// <returns>A BSP wrapper on success, null on failure</returns>
2022-12-24 20:15:58 -08:00
public static BSP 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>
2022-12-25 21:27:06 -08:00
/// Create a BSP from a Stream
2022-12-24 20:15:58 -08:00
/// </summary>
2022-12-25 21:27:06 -08:00
/// <param name="data">Stream representing the BSP</param>
/// <returns>An BSP wrapper on success, null on failure</returns>
2022-12-24 20:15:58 -08:00
public static BSP Create(Stream data)
{
// If the data is invalid
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
var file = Builders.BSP.ParseFile(data);
if (file == null)
return null;
var wrapper = new BSP
{
_file = file,
_dataSource = DataSource.Stream,
_streamData = data,
};
return wrapper;
}
#endregion
#region Printing
/// <inheritdoc/>
2023-01-13 14:04:21 -08:00
public override StringBuilder PrettyPrint()
2022-12-24 20:15:58 -08:00
{
2023-01-13 14:04:21 -08:00
StringBuilder builder = new StringBuilder();
builder.AppendLine("BSP Information:");
builder.AppendLine("-------------------------");
builder.AppendLine();
PrintHeader(builder);
PrintLumps(builder);
PrintTextureHeader(builder);
PrintTextures(builder);
return builder;
2022-12-24 20:15:58 -08:00
}
/// <summary>
/// Print header information
/// </summary>
2023-01-13 14:04:21 -08:00
/// <param name="builder">StringBuilder to append information to</param>
private void PrintHeader(StringBuilder builder)
2022-12-24 20:15:58 -08:00
{
2023-01-13 14:04:21 -08:00
builder.AppendLine(" Header Information:");
builder.AppendLine(" -------------------------");
builder.AppendLine($" Version: {Version} (0x{Version:X})");
builder.AppendLine();
2022-12-24 20:15:58 -08:00
}
/// <summary>
/// Print lumps information
/// </summary>
2023-01-13 14:04:21 -08:00
/// <param name="builder">StringBuilder to append information to</param>
private void PrintLumps(StringBuilder builder)
2022-12-24 20:15:58 -08:00
{
2023-01-13 14:04:21 -08:00
builder.AppendLine(" Lumps Information:");
builder.AppendLine(" -------------------------");
2022-12-24 20:15:58 -08:00
if (Lumps == null || Lumps.Length == 0)
{
2023-01-13 14:04:21 -08:00
builder.AppendLine(" No lumps");
2022-12-24 20:15:58 -08:00
}
else
{
for (int i = 0; i < Lumps.Length; i++)
{
var lump = Lumps[i];
string specialLumpName = string.Empty;
switch (i)
{
2022-12-28 10:04:10 -08:00
case HL_BSP_LUMP_ENTITIES:
2022-12-24 20:15:58 -08:00
specialLumpName = " (entities)";
break;
2022-12-28 10:04:10 -08:00
case HL_BSP_LUMP_TEXTUREDATA:
2022-12-24 20:15:58 -08:00
specialLumpName = " (texture data)";
break;
}
2023-01-13 14:04:21 -08:00
builder.AppendLine($" Lump {i}{specialLumpName}");
builder.AppendLine($" Offset: {lump.Offset} (0x{lump.Offset:X})");
builder.AppendLine($" Length: {lump.Length} (0x{lump.Length:X})");
2022-12-24 20:15:58 -08:00
}
}
2023-01-13 14:04:21 -08:00
builder.AppendLine();
2022-12-24 20:15:58 -08:00
}
/// <summary>
/// Print texture header information
/// </summary>
2023-01-13 14:04:21 -08:00
/// <param name="builder">StringBuilder to append information to</param>
private void PrintTextureHeader(StringBuilder builder)
2022-12-24 20:15:58 -08:00
{
2023-01-13 14:04:21 -08:00
builder.AppendLine(" Texture Header Information:");
builder.AppendLine(" -------------------------");
builder.AppendLine($" Texture count: {TextureCount}");
builder.AppendLine($" Offsets:");
2023-01-10 09:50:49 -08:00
for (int i = 0; i < Offsets.Length; i++)
{
2023-01-13 14:04:21 -08:00
builder.AppendLine($" Offset {i}: {Offsets[i]} (0x{Offsets[i]:X})");
2023-01-10 09:50:49 -08:00
}
2023-01-13 14:04:21 -08:00
builder.AppendLine();
2022-12-24 20:15:58 -08:00
}
2022-12-24 22:02:30 -08:00
/// <summary>
/// Print textures information
/// </summary>
2023-01-13 14:04:21 -08:00
/// <param name="builder">StringBuilder to append information to</param>
private void PrintTextures(StringBuilder builder)
2022-12-24 22:02:30 -08:00
{
2023-01-13 14:04:21 -08:00
builder.AppendLine(" Textures Information:");
builder.AppendLine(" -------------------------");
2022-12-24 22:02:30 -08:00
if (Textures == null || Textures.Length == 0)
{
2023-01-13 14:04:21 -08:00
builder.AppendLine(" No textures");
2022-12-24 22:02:30 -08:00
}
else
{
for (int i = 0; i < Textures.Length; i++)
{
var texture = Textures[i];
2023-01-13 14:04:21 -08:00
builder.AppendLine($" Texture {i}");
builder.AppendLine($" Name: {texture.Name}");
builder.AppendLine($" Width: {texture.Width} (0x{texture.Width:X})");
builder.AppendLine($" Height: {texture.Height} (0x{texture.Height:X})");
builder.AppendLine($" Offsets:");
2023-01-10 09:50:49 -08:00
for (int j = 0; j < texture.Offsets.Length; j++)
{
2023-01-13 14:04:21 -08:00
builder.AppendLine($" Offset {j}: {Offsets[i]} (0x{texture.Offsets[j]:X})");
2023-01-10 09:50:49 -08:00
}
2022-12-24 22:02:30 -08:00
// Skip texture data
2023-01-13 14:04:21 -08:00
builder.AppendLine($" Palette size: {texture.PaletteSize} (0x{texture.PaletteSize:X})");
2022-12-24 22:02:30 -08:00
// Skip palette data
}
}
2023-01-13 14:04:21 -08:00
builder.AppendLine();
2022-12-24 22:02:30 -08:00
}
#if NET6_0_OR_GREATER
/// <inheritdoc/>
public override string ExportJSON() => System.Text.Json.JsonSerializer.Serialize(_file, _jsonSerializerOptions);
#endif
2022-12-24 20:15:58 -08:00
#endregion
#region Extraction
/// <summary>
2022-12-24 22:02:30 -08:00
/// Extract all lumps from the BSP to an output directory
2022-12-24 20:15:58 -08:00
/// </summary>
/// <param name="outputDirectory">Output directory to write to</param>
2022-12-24 22:02:30 -08:00
/// <returns>True if all lumps extracted, false otherwise</returns>
public bool ExtractAllLumps(string outputDirectory)
2022-12-24 20:15:58 -08:00
{
2022-12-24 22:02:30 -08:00
// 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;
2022-12-24 20:15:58 -08:00
}
/// <summary>
2022-12-24 22:02:30 -08:00
/// Extract a lump from the BSP to an output directory by index
2022-12-24 20:15:58 -08:00
/// </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)
{
2022-12-24 22:02:30 -08:00
// 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)
{
2022-12-28 10:04:10 -08:00
case HL_BSP_LUMP_ENTITIES:
2022-12-24 22:02:30 -08:00
filename = "entities.ent";
break;
2022-12-28 10:04:10 -08:00
case HL_BSP_LUMP_TEXTUREDATA:
2022-12-24 22:02:30 -08:00
filename = "texture_data.bin";
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;
2022-12-24 20:15:58 -08:00
}
/// <summary>
2022-12-24 22:02:30 -08:00
/// Extract all textures from the BSP to an output directory
/// </summary>
/// <param name="outputDirectory">Output directory to write to</param>
/// <returns>True if all textures extracted, false otherwise</returns>
public bool ExtractAllTextures(string outputDirectory)
{
// If we have no textures
if (Offsets == null || Offsets.Length == 0)
return false;
// Loop through and extract all lumps to the output
bool allExtracted = true;
for (int i = 0; i < Offsets.Length; i++)
{
allExtracted &= ExtractTexture(i, outputDirectory);
}
return allExtracted;
}
/// <summary>
/// Extract a texture from the BSP to an output directory by index
2022-12-24 20:15:58 -08:00
/// </summary>
/// <param name="index">Lump index to extract</param>
/// <param name="outputDirectory">Output directory to write to</param>
/// <returns>True if the texture extracted, false otherwise</returns>
public bool ExtractTexture(int index, string outputDirectory)
{
2022-12-24 22:02:30 -08:00
// If we have no textures
if (Textures == null || Textures.Length == 0)
return false;
// If the texture index is invalid
if (index < 0 || index >= Textures.Length)
return false;
// Get the texture
var texture = Textures[index];
if (texture == null)
return false;
// Read the data
byte[] data = CreateTextureData(texture);
if (data == null)
return false;
// Create the filename
string filename = $"{texture.Name}.bmp";
// 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;
}
/// <summary>
/// Create a bitmap from the texture and palette data
/// </summary>
/// <param name="texture">Texture object to format</param>
/// <returns>Byte array representing the texture as a bitmap</returns>
2023-03-08 17:49:14 -05:00
private static byte[] CreateTextureData(Models.BSP.Texture texture)
2022-12-24 22:02:30 -08:00
{
// If there's no texture data
if (texture.TextureData == null || texture.TextureData.Length == 0)
return null;
// Create the bitmap file header
2023-03-08 17:49:14 -05:00
var fileHeader = new Models.BMP.BITMAPFILEHEADER()
2022-12-24 22:02:30 -08:00
{
Type = ('M' << 8) | 'B',
Size = 14 + 40 + (texture.PaletteSize * 4) + (texture.Width * texture.Height),
OffBits = 14 + 40 + (texture.PaletteSize * 4),
};
// Create the bitmap info header
2023-03-08 17:49:14 -05:00
var infoHeader = new Models.BMP.BITMAPINFOHEADER
2022-12-24 22:02:30 -08:00
{
Size = 40,
Width = (int)texture.Width,
Height = (int)texture.Height,
Planes = 1,
BitCount = 8,
SizeImage = 0,
ClrUsed = texture.PaletteSize,
ClrImportant = texture.PaletteSize,
};
// Reformat the palette data
byte[] paletteData = new byte[texture.PaletteSize * 4];
for (uint i = 0; i < texture.PaletteSize; i++)
{
paletteData[i * 4 + 0] = texture.PaletteData[i * 3 + 2];
paletteData[i * 4 + 1] = texture.PaletteData[i * 3 + 1];
paletteData[i * 4 + 2] = texture.PaletteData[i * 3 + 0];
paletteData[i * 4 + 3] = 0;
}
// Reformat the pixel data
byte[] pixelData = new byte[texture.Width * texture.Height];
for (uint i = 0; i < texture.Width; i++)
{
for (uint j = 0; j < texture.Height; j++)
{
pixelData[i + ((texture.Height - 1 - j) * texture.Width)] = texture.TextureData[i + j * texture.Width];
}
}
// Build the file data
List<byte> buffer = new List<byte>();
// Bitmap file header
buffer.AddRange(BitConverter.GetBytes(fileHeader.Type));
buffer.AddRange(BitConverter.GetBytes(fileHeader.Size));
buffer.AddRange(BitConverter.GetBytes(fileHeader.Reserved1));
buffer.AddRange(BitConverter.GetBytes(fileHeader.Reserved2));
buffer.AddRange(BitConverter.GetBytes(fileHeader.OffBits));
// Bitmap info header
buffer.AddRange(BitConverter.GetBytes(infoHeader.Size));
buffer.AddRange(BitConverter.GetBytes(infoHeader.Width));
buffer.AddRange(BitConverter.GetBytes(infoHeader.Height));
buffer.AddRange(BitConverter.GetBytes(infoHeader.Planes));
buffer.AddRange(BitConverter.GetBytes(infoHeader.BitCount));
buffer.AddRange(BitConverter.GetBytes(infoHeader.Compression));
buffer.AddRange(BitConverter.GetBytes(infoHeader.SizeImage));
buffer.AddRange(BitConverter.GetBytes(infoHeader.XPelsPerMeter));
buffer.AddRange(BitConverter.GetBytes(infoHeader.YPelsPerMeter));
buffer.AddRange(BitConverter.GetBytes(infoHeader.ClrUsed));
buffer.AddRange(BitConverter.GetBytes(infoHeader.ClrImportant));
// Palette data
buffer.AddRange(paletteData);
// Pixel data
buffer.AddRange(pixelData);
return buffer.ToArray();
2022-12-24 20:15:58 -08:00
}
#endregion
}
}