2024-12-11 13:31:54 -05:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2025-09-26 13:06:18 -04:00
|
|
|
using SabreTools.Data.Models.LZ;
|
2024-12-11 13:31:54 -05: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.LZ.Constants;
|
2024-12-11 13:31:54 -05: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
|
2024-12-11 13:31:54 -05:00
|
|
|
{
|
2025-09-26 15:02:43 -04:00
|
|
|
public class LZQBasic : BaseBinaryReader<QBasicFile>
|
2024-12-11 13:31:54 -05:00
|
|
|
{
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override QBasicFile? Deserialize(Stream? data)
|
|
|
|
|
{
|
|
|
|
|
// If the data is invalid
|
2026-01-25 14:30:18 -05:00
|
|
|
if (data is null || !data.CanRead)
|
2024-12-11 13:31:54 -05:00
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Cache the current offset
|
|
|
|
|
int initialOffset = (int)data.Position;
|
|
|
|
|
|
|
|
|
|
// Create a new file to fill
|
|
|
|
|
var file = new QBasicFile();
|
|
|
|
|
|
|
|
|
|
#region File Header
|
|
|
|
|
|
|
|
|
|
// Try to parse the header
|
|
|
|
|
var header = ParseHeader(data);
|
2026-01-25 14:30:18 -05:00
|
|
|
if (header is null)
|
2024-12-11 13:31:54 -05:00
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
// Set the header
|
|
|
|
|
file.Header = header;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// Ignore the actual error
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parse a Stream into a header
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">Stream to parse</param>
|
|
|
|
|
/// <returns>Filled header on success, null on error</returns>
|
|
|
|
|
private static QBasicHeader? ParseHeader(Stream data)
|
|
|
|
|
{
|
|
|
|
|
var header = new QBasicHeader();
|
|
|
|
|
|
|
|
|
|
header.Magic = data.ReadBytes(8);
|
|
|
|
|
if (Encoding.ASCII.GetString(header.Magic) != Encoding.ASCII.GetString(QBasicSignatureBytes))
|
|
|
|
|
return null;
|
|
|
|
|
|
2024-12-17 22:38:57 -05:00
|
|
|
header.RealLength = data.ReadUInt32LittleEndian();
|
2024-12-11 13:31:54 -05:00
|
|
|
|
|
|
|
|
return header;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-24 09:31:28 -04:00
|
|
|
}
|