2023-09-08 21:16:52 -04:00
|
|
|
using System.IO;
|
2024-04-03 20:55:02 -04:00
|
|
|
using System.Text;
|
2025-09-26 13:06:18 -04:00
|
|
|
using SabreTools.Data.Models.BFPK;
|
2024-04-17 11:52:22 -04:00
|
|
|
using SabreTools.IO.Extensions;
|
2026-03-24 19:17:25 -04:00
|
|
|
using SabreTools.Numerics.Extensions;
|
2025-09-26 13:06:18 -04:00
|
|
|
using static SabreTools.Data.Models.BFPK.Constants;
|
2023-09-08 21:16:52 -04:00
|
|
|
|
2026-01-27 12:03:01 -05:00
|
|
|
#pragma warning disable IDE0017 // Simplify object initialization
|
2025-09-26 14:57:20 -04:00
|
|
|
namespace SabreTools.Serialization.Readers
|
2023-09-08 21:16:52 -04:00
|
|
|
{
|
2025-09-26 15:02:43 -04:00
|
|
|
public class BFPK : BaseBinaryReader<Archive>
|
2023-09-08 21:16:52 -04:00
|
|
|
{
|
2024-04-03 20:55:02 -04:00
|
|
|
/// <inheritdoc/>
|
2024-04-04 01:55:05 -04:00
|
|
|
public override Archive? Deserialize(Stream? data)
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
|
|
|
|
// If the data is invalid
|
2026-01-25 14:30:18 -05:00
|
|
|
if (data is null || !data.CanRead)
|
2024-04-03 20:55:02 -04:00
|
|
|
return null;
|
|
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
try
|
|
|
|
|
{
|
2025-08-19 09:09:33 -04:00
|
|
|
// Cache the current offset
|
|
|
|
|
long initialOffset = data.Position;
|
|
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Create a new archive to fill
|
|
|
|
|
var archive = new Archive();
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
#region Header
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Try to parse the header
|
2024-12-16 23:08:45 -05:00
|
|
|
var header = ParseHeader(data);
|
|
|
|
|
if (header.Magic != SignatureString)
|
2024-11-28 22:57:12 -05:00
|
|
|
return null;
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Set the archive header
|
|
|
|
|
archive.Header = header;
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
#endregion
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
#region Files
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// If we have any files
|
|
|
|
|
var files = new FileEntry[header.Files];
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Read all entries in turn
|
|
|
|
|
for (int i = 0; i < header.Files; i++)
|
|
|
|
|
{
|
2025-08-19 09:09:33 -04:00
|
|
|
files[i] = ParseFileEntry(data, initialOffset);
|
2024-11-28 22:57:12 -05:00
|
|
|
}
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Set the files
|
|
|
|
|
archive.Files = files;
|
2024-11-19 12:40:32 -05:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
#endregion
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
return archive;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// Ignore the actual error
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-12-16 23:08:45 -05:00
|
|
|
/// Parse a Stream into a FileEntry
|
2024-04-03 20:55:02 -04:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">Stream to parse</param>
|
2025-08-19 22:01:10 -04:00
|
|
|
/// <param name="initialOffset">Initial offset to use in address comparisons</param>
|
2024-12-16 23:08:45 -05:00
|
|
|
/// <returns>Filled FileEntry on success, null on error</returns>
|
2025-08-19 09:09:33 -04:00
|
|
|
public static FileEntry ParseFileEntry(Stream data, long initialOffset)
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2024-04-23 21:00:10 -04:00
|
|
|
var fileEntry = new FileEntry();
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-12-16 23:08:45 -05:00
|
|
|
fileEntry.NameSize = data.ReadInt32LittleEndian();
|
2024-04-03 20:55:02 -04:00
|
|
|
if (fileEntry.NameSize > 0)
|
|
|
|
|
{
|
2024-11-27 13:47:07 -05:00
|
|
|
byte[] name = data.ReadBytes(fileEntry.NameSize);
|
|
|
|
|
fileEntry.Name = Encoding.ASCII.GetString(name);
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
2024-12-16 23:08:45 -05:00
|
|
|
fileEntry.UncompressedSize = data.ReadInt32LittleEndian();
|
|
|
|
|
fileEntry.Offset = data.ReadInt32LittleEndian();
|
2024-04-03 20:55:02 -04:00
|
|
|
if (fileEntry.Offset > 0)
|
|
|
|
|
{
|
|
|
|
|
long currentOffset = data.Position;
|
2025-10-27 22:43:56 -04:00
|
|
|
data.SeekIfPossible(initialOffset + fileEntry.Offset, SeekOrigin.Begin);
|
2024-12-16 23:08:45 -05:00
|
|
|
fileEntry.CompressedSize = data.ReadInt32LittleEndian();
|
2025-10-27 22:43:56 -04:00
|
|
|
data.SeekIfPossible(currentOffset, SeekOrigin.Begin);
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fileEntry;
|
2024-04-03 17:27:08 -04:00
|
|
|
}
|
2024-12-16 23:08:45 -05:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parse a Stream into a Header
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">Stream to parse</param>
|
|
|
|
|
/// <returns>Filled Header on success, null on error</returns>
|
|
|
|
|
public static Header ParseHeader(Stream data)
|
|
|
|
|
{
|
|
|
|
|
var obj = new Header();
|
|
|
|
|
|
|
|
|
|
byte[] magic = data.ReadBytes(4);
|
|
|
|
|
obj.Magic = Encoding.ASCII.GetString(magic);
|
|
|
|
|
obj.Version = data.ReadInt32LittleEndian();
|
|
|
|
|
obj.Files = data.ReadInt32LittleEndian();
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
2023-09-08 21:16:52 -04:00
|
|
|
}
|
2025-07-24 09:31:28 -04:00
|
|
|
}
|