2023-09-08 22:51:45 -04:00
|
|
|
using System.IO;
|
2024-12-16 23:08:45 -05:00
|
|
|
using System.Text;
|
2025-09-26 13:06:18 -04:00
|
|
|
using SabreTools.Data.Models.PAK;
|
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.PAK.Constants;
|
2023-09-08 22:51:45 -04:00
|
|
|
|
2025-09-26 14:57:20 -04:00
|
|
|
namespace SabreTools.Serialization.Readers
|
2023-09-08 22:51:45 -04:00
|
|
|
{
|
2025-09-26 15:02:43 -04:00
|
|
|
public class PAK : BaseBinaryReader<Data.Models.PAK.File>
|
2023-09-08 22:51:45 -04:00
|
|
|
{
|
2024-04-03 20:55:02 -04:00
|
|
|
/// <inheritdoc/>
|
2025-09-26 13:06:18 -04:00
|
|
|
public override Data.Models.PAK.File? 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 Half-Life Package to fill
|
2025-09-26 13:06:18 -04:00
|
|
|
var file = new Data.Models.PAK.File();
|
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.Signature != 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 package header
|
|
|
|
|
file.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 Directory Items
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Get the directory items offset
|
2025-08-19 09:09:33 -04:00
|
|
|
long directoryItemsOffset = initialOffset + header.DirectoryOffset;
|
|
|
|
|
if (directoryItemsOffset < initialOffset || directoryItemsOffset >= data.Length)
|
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
|
|
|
// Seek to the directory items
|
2025-10-27 22:43:56 -04:00
|
|
|
data.SeekIfPossible(directoryItemsOffset, SeekOrigin.Begin);
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Create the directory item array
|
|
|
|
|
file.DirectoryItems = new DirectoryItem[header.DirectoryLength / 64];
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Try to parse the directory items
|
|
|
|
|
for (int i = 0; i < file.DirectoryItems.Length; i++)
|
|
|
|
|
{
|
2024-12-16 23:08:45 -05:00
|
|
|
file.DirectoryItems[i] = ParseDirectoryItem(data);
|
2024-11-28 22:57:12 -05:00
|
|
|
}
|
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
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// Ignore the actual error
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
2024-12-16 23:08:45 -05:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parse a Stream into a DirectoryItem
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">Stream to parse</param>
|
|
|
|
|
/// <returns>Filled DirectoryItem on success, null on error</returns>
|
|
|
|
|
public static DirectoryItem ParseDirectoryItem(Stream data)
|
|
|
|
|
{
|
|
|
|
|
var obj = new DirectoryItem();
|
|
|
|
|
|
|
|
|
|
byte[] itemName = data.ReadBytes(56);
|
|
|
|
|
obj.ItemName = Encoding.ASCII.GetString(itemName).TrimEnd('\0');
|
|
|
|
|
obj.ItemOffset = data.ReadUInt32LittleEndian();
|
|
|
|
|
obj.ItemLength = data.ReadUInt32LittleEndian();
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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[] signature = data.ReadBytes(4);
|
|
|
|
|
obj.Signature = Encoding.ASCII.GetString(signature);
|
|
|
|
|
obj.DirectoryOffset = data.ReadUInt32LittleEndian();
|
|
|
|
|
obj.DirectoryLength = data.ReadUInt32LittleEndian();
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
2023-09-08 22:51:45 -04:00
|
|
|
}
|
2025-07-24 09:31:28 -04:00
|
|
|
}
|