Files

260 lines
8.3 KiB
C#
Raw Permalink Normal View History

2023-01-13 14:04:21 -08:00
using System.IO;
using System.Text;
2022-12-14 22:58:18 -08:00
using SharpCompress.Compressors;
using SharpCompress.Compressors.Deflate;
2023-03-07 16:59:14 -05:00
namespace BinaryObjectScanner.Wrappers
2022-12-14 22:58:18 -08:00
{
public class BFPK : WrapperBase
{
2023-01-18 11:18:53 -08:00
#region Descriptive Properties
/// <inheritdoc/>
public override string Description => "BFPK Archive";
#endregion
2022-12-14 22:58:18 -08:00
#region Pass-Through Properties
#region Header
/// <inheritdoc cref="Models.BFPK.Header.Magic"/>
2022-12-28 09:57:22 -08:00
public string Magic => _archive.Header.Magic;
2022-12-14 22:58:18 -08:00
/// <inheritdoc cref="Models.BFPK.Header.Version"/>
public int Version => _archive.Header.Version;
/// <inheritdoc cref="Models.BFPK.Header.Files"/>
public int Files => _archive.Header.Files;
#endregion
#region Files
/// <inheritdoc cref="Models.BFPK.Archive.Files"/>
2023-03-08 17:49:14 -05:00
public Models.BFPK.FileEntry[] FileTable => _archive.Files;
2022-12-14 22:58:18 -08:00
#endregion
#endregion
#region Instance Variables
/// <summary>
/// Internal representation of the archive
/// </summary>
2023-03-08 17:49:14 -05:00
private Models.BFPK.Archive _archive;
2022-12-14 22:58:18 -08:00
#endregion
#region Constructors
/// <summary>
/// Private constructor
/// </summary>
private BFPK() { }
/// <summary>
/// Create a BFPK archive from a byte array and offset
/// </summary>
/// <param name="data">Byte array representing the archive</param>
/// <param name="offset">Offset within the array to parse</param>
/// <returns>A BFPK archive wrapper on success, null on failure</returns>
public static BFPK Create(byte[] data, int offset)
{
2022-12-15 14:20:27 -08:00
// 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);
2022-12-15 12:41:08 -08:00
return Create(dataStream);
2022-12-14 22:58:18 -08:00
}
/// <summary>
/// Create a BFPK archive from a Stream
/// </summary>
/// <param name="data">Stream representing the archive</param>
/// <returns>A BFPK archive wrapper on success, null on failure</returns>
public static BFPK Create(Stream data)
{
2022-12-15 14:20:27 -08:00
// If the data is invalid
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
2022-12-22 16:02:10 -08:00
var archive = Builders.BFPK.ParseArchive(data);
2022-12-14 22:58:18 -08:00
if (archive == null)
return null;
var wrapper = new BFPK
{
_archive = archive,
_dataSource = DataSource.Stream,
_streamData = data,
};
return wrapper;
}
#endregion
#region Data
/// <summary>
2023-01-03 09:19:35 -08:00
/// Extract all files from the BFPK to an output directory
2022-12-14 22:58:18 -08:00
/// </summary>
2023-01-03 09:19:35 -08:00
/// <param name="outputDirectory">Output directory to write to</param>
/// <returns>True if all files extracted, false otherwise</returns>
public bool ExtractAll(string outputDirectory)
{
// If we have no files
if (FileTable == null || FileTable.Length == 0)
return false;
// Loop through and extract all files to the output
bool allExtracted = true;
for (int i = 0; i < FileTable.Length; i++)
{
allExtracted &= ExtractFile(i, outputDirectory);
}
return allExtracted;
}
/// <summary>
/// Extract a file from the BFPK to an output directory by index
/// </summary>
/// <param name="index">File index to extract</param>
/// <param name="outputDirectory">Output directory to write to</param>
/// <returns>True if the file extracted, false otherwise</returns>
2022-12-14 22:58:18 -08:00
public bool ExtractFile(int index, string outputDirectory)
{
// If we have no files
if (Files == 0 || FileTable == null || FileTable.Length == 0)
return false;
// If we have an invalid index
if (index < 0 || index >= FileTable.Length)
return false;
// Get the file information
var file = FileTable[index];
// Get the read index and length
int offset = file.Offset + 4;
int compressedSize = file.CompressedSize;
// Some files can lack the length prefix
if (compressedSize > GetEndOfFile())
{
offset -= 4;
compressedSize = file.UncompressedSize;
}
try
{
// Ensure the output directory exists
Directory.CreateDirectory(outputDirectory);
// Create the output path
string filePath = Path.Combine(outputDirectory, file.Name);
using (FileStream fs = File.OpenWrite(filePath))
{
// Read the data block
byte[] data = ReadFromDataSource(offset, compressedSize);
// If we have uncompressed data
if (compressedSize == file.UncompressedSize)
{
fs.Write(data, 0, compressedSize);
}
else
{
MemoryStream ms = new MemoryStream(data);
ZlibStream zs = new ZlibStream(ms, CompressionMode.Decompress);
zs.CopyTo(fs);
}
}
return true;
}
catch
{
return false;
}
}
#endregion
#region Printing
/// <inheritdoc/>
2023-01-13 14:04:21 -08:00
public override StringBuilder PrettyPrint()
2022-12-14 22:58:18 -08:00
{
2023-01-13 14:04:21 -08:00
StringBuilder builder = new StringBuilder();
2022-12-14 22:58:18 -08:00
2023-01-13 14:04:21 -08:00
builder.AppendLine("BFPK Information:");
builder.AppendLine("-------------------------");
builder.AppendLine();
PrintHeader(builder);
PrintFileTable(builder);
return builder;
2022-12-14 22:58:18 -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-14 22:58:18 -08:00
{
2023-01-13 14:04:21 -08:00
builder.AppendLine(" Header Information:");
builder.AppendLine(" -------------------------");
builder.AppendLine($" Magic: {Magic}");
builder.AppendLine($" Version: {Version} (0x{Version:X})");
builder.AppendLine($" Files: {Files} (0x{Files:X})");
builder.AppendLine();
2022-12-14 22:58:18 -08:00
}
/// <summary>
2022-12-14 23:06:09 -08:00
/// Print file table information
2022-12-14 22:58:18 -08:00
/// </summary>
2023-01-13 14:04:21 -08:00
/// <param name="builder">StringBuilder to append information to</param>
private void PrintFileTable(StringBuilder builder)
2022-12-14 22:58:18 -08:00
{
2023-01-13 14:04:21 -08:00
builder.AppendLine(" File Table Information:");
builder.AppendLine(" -------------------------");
2022-12-14 23:06:09 -08:00
if (Files == 0 || FileTable == null || FileTable.Length == 0)
2022-12-14 22:58:18 -08:00
{
2023-01-13 14:04:21 -08:00
builder.AppendLine(" No file table items");
2022-12-14 22:58:18 -08:00
}
else
{
2022-12-14 23:06:09 -08:00
for (int i = 0; i < FileTable.Length; i++)
2022-12-14 22:58:18 -08:00
{
2022-12-14 23:06:09 -08:00
var entry = FileTable[i];
2023-01-13 14:04:21 -08:00
builder.AppendLine($" File Table Entry {i}");
builder.AppendLine($" Name size: {entry.NameSize} (0x{entry.NameSize:X})");
builder.AppendLine($" Name: {entry.Name}");
builder.AppendLine($" Uncompressed size: {entry.UncompressedSize} (0x{entry.UncompressedSize:X})");
builder.AppendLine($" Offset: {entry.Offset} (0x{entry.Offset:X})");
builder.AppendLine($" Compressed Size: {entry.CompressedSize} (0x{entry.CompressedSize:X})");
2022-12-14 22:58:18 -08:00
}
}
2023-01-13 14:04:21 -08:00
builder.AppendLine();
2022-12-14 22:58:18 -08:00
}
#if NET6_0_OR_GREATER
/// <inheritdoc/>
public override string ExportJSON() => System.Text.Json.JsonSerializer.Serialize(_archive, _jsonSerializerOptions);
#endif
2022-12-14 22:58:18 -08:00
#endregion
}
}