2024-04-04 01:17:41 -04:00
|
|
|
using System.IO;
|
|
|
|
|
|
2025-09-26 14:57:20 -04:00
|
|
|
namespace SabreTools.Serialization.Readers
|
2024-04-04 01:17:41 -04:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Base class for all binary deserializers
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TModel">Type of the model to deserialize</typeparam>
|
2025-09-16 22:25:40 -04:00
|
|
|
/// <remarks>
|
2025-09-26 15:02:43 -04:00
|
|
|
/// This class allows all inheriting types to only implement <see cref="IStreamReader<>"/>
|
2025-10-30 20:44:16 -04:00
|
|
|
/// and still implicitly implement <see cref="IByteReader<>"/> and <see cref="IFileReader<>"/>
|
2025-09-16 22:25:40 -04:00
|
|
|
/// </remarks>
|
2025-09-26 15:02:43 -04:00
|
|
|
public abstract class BaseBinaryReader<TModel> :
|
|
|
|
|
IByteReader<TModel>,
|
|
|
|
|
IFileReader<TModel>,
|
|
|
|
|
IStreamReader<TModel>
|
2024-04-04 01:17:41 -04:00
|
|
|
{
|
2026-01-25 20:07:59 -05:00
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public bool Debug { get; set; } = false;
|
|
|
|
|
|
2025-09-26 15:02:43 -04:00
|
|
|
#region IByteReader
|
2024-04-04 01:26:27 -04:00
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public virtual TModel? Deserialize(byte[]? data, int offset)
|
|
|
|
|
{
|
|
|
|
|
// If the data is invalid
|
2026-01-25 14:30:18 -05:00
|
|
|
if (data is null || data.Length == 0)
|
2024-04-04 01:26:27 -04:00
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
// If the offset is out of bounds
|
|
|
|
|
if (offset < 0 || offset >= data.Length)
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
// Create a memory stream and parse that
|
|
|
|
|
var dataStream = new MemoryStream(data, offset, data.Length - offset);
|
2025-09-16 22:21:22 -04:00
|
|
|
return Deserialize(dataStream);
|
2024-04-04 01:26:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2025-09-26 15:02:43 -04:00
|
|
|
#region IFileReader
|
2024-04-04 01:38:08 -04:00
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public virtual TModel? Deserialize(string? path)
|
|
|
|
|
{
|
2025-09-24 09:43:27 -04:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// If we don't have a file
|
|
|
|
|
if (string.IsNullOrEmpty(path) || !File.Exists(path))
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
// Open the file for deserialization
|
|
|
|
|
using var stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
|
|
|
return Deserialize(stream);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// TODO: Handle logging the exception
|
|
|
|
|
return default;
|
|
|
|
|
}
|
2024-04-04 01:38:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2025-09-26 15:02:43 -04:00
|
|
|
#region IStreamReader
|
2024-04-04 01:55:05 -04:00
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public abstract TModel? Deserialize(Stream? data);
|
|
|
|
|
|
|
|
|
|
#endregion
|
2024-04-04 01:17:41 -04:00
|
|
|
}
|
2025-07-24 09:31:28 -04:00
|
|
|
}
|