Files
BinaryObjectScanner/BinaryObjectScanner.Wrappers/BDPlusSVM.cs

159 lines
4.2 KiB
C#
Raw Normal View History

2023-01-12 14:45:04 -08:00
using System.IO;
2023-01-13 14:04:21 -08:00
using System.Text;
2023-09-13 01:29:50 -04:00
using SabreTools.Models.BDPlus;
2023-01-12 14:45:04 -08:00
2023-03-07 16:59:14 -05:00
namespace BinaryObjectScanner.Wrappers
2023-01-12 14:45:04 -08:00
{
2023-09-13 01:29:50 -04:00
public class BDPlusSVM : WrapperBase<SVM>
2023-01-12 14:45:04 -08:00
{
2023-01-18 11:18:53 -08:00
#region Descriptive Properties
/// <inheritdoc/>
2023-09-11 23:25:09 -04:00
public override string DescriptionString => "BD+ SVM";
2023-01-18 11:18:53 -08:00
#endregion
2023-01-12 14:45:04 -08:00
#region Pass-Through Properties
/// <inheritdoc cref="Models.BDPlus.SVM.Signature"/>
2023-09-12 17:12:23 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public string Signature => _model.Signature;
2023-09-12 17:12:23 -04:00
#else
public string? Signature => _model.Signature;
#endif
2023-01-12 14:45:04 -08:00
/// <inheritdoc cref="Models.BDPlus.SVM.Unknown1"/>
2023-09-12 17:12:23 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public byte[] Unknown1 => _model.Unknown1;
2023-09-12 17:12:23 -04:00
#else
public byte[]? Unknown1 => _model.Unknown1;
#endif
2023-01-12 14:45:04 -08:00
/// <inheritdoc cref="Models.BDPlus.SVM.Year"/>
2023-09-11 23:25:09 -04:00
public ushort Year => _model.Year;
2023-01-12 14:45:04 -08:00
/// <inheritdoc cref="Models.BDPlus.SVM.Month"/>
2023-09-11 23:25:09 -04:00
public byte Month => _model.Month;
2023-01-12 14:45:04 -08:00
/// <inheritdoc cref="Models.BDPlus.SVM.Day"/>
2023-09-11 23:25:09 -04:00
public byte Day => _model.Day;
2023-01-12 14:45:04 -08:00
/// <inheritdoc cref="Models.BDPlus.SVM.Unknown2"/>
2023-09-12 17:12:23 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public byte[] Unknown2 => _model.Unknown2;
2023-09-12 17:12:23 -04:00
#else
public byte[]? Unknown2 => _model.Unknown2;
#endif
2023-01-12 14:45:04 -08:00
/// <inheritdoc cref="Models.BDPlus.SVM.Length"/>
2023-09-11 23:25:09 -04:00
public uint Length => _model.Length;
2023-01-12 14:45:04 -08:00
/// <inheritdoc cref="Models.BDPlus.SVM.Data"/>
2023-09-12 17:12:23 -04:00
#if NET48
2023-09-11 23:25:09 -04:00
public byte[] Data => _model.Data;
2023-09-12 17:12:23 -04:00
#else
public byte[]? Data => _model.Data;
#endif
2023-01-12 14:45:04 -08:00
#endregion
#region Constructors
2023-09-11 23:25:09 -04:00
/// <inheritdoc/>
#if NET48
2023-09-13 01:29:50 -04:00
public BDPlusSVM(SVM model, byte[] data, int offset)
2023-09-11 23:25:09 -04:00
#else
2023-09-13 01:29:50 -04:00
public BDPlusSVM(SVM? model, byte[]? data, int offset)
2023-09-11 23:25:09 -04:00
#endif
: base(model, data, offset)
{
// All logic is handled by the base class
}
/// <inheritdoc/>
#if NET48
2023-09-13 01:29:50 -04:00
public BDPlusSVM(SVM model, Stream data)
2023-09-11 23:25:09 -04:00
#else
2023-09-13 01:29:50 -04:00
public BDPlusSVM(SVM? model, Stream? data)
2023-09-11 23:25:09 -04:00
#endif
: base(model, data)
{
// All logic is handled by the base class
}
2023-01-12 14:45:04 -08:00
/// <summary>
/// Create a BD+ SVM 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 BD+ SVM wrapper on success, null on failure</returns>
2023-09-12 17:12:23 -04:00
#if NET48
2023-01-12 14:45:04 -08:00
public static BDPlusSVM Create(byte[] data, int offset)
2023-09-12 17:12:23 -04:00
#else
public static BDPlusSVM? Create(byte[]? data, int offset)
#endif
2023-01-12 14:45:04 -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);
return Create(dataStream);
}
/// <summary>
/// Create a BD+ SVM from a Stream
/// </summary>
/// <param name="data">Stream representing the archive</param>
/// <returns>A BD+ SVM wrapper on success, null on failure</returns>
2023-09-12 17:12:23 -04:00
#if NET48
2023-01-12 14:45:04 -08:00
public static BDPlusSVM Create(Stream data)
2023-09-12 17:12:23 -04:00
#else
public static BDPlusSVM? Create(Stream? data)
#endif
2023-01-12 14:45:04 -08:00
{
// If the data is invalid
if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead)
return null;
2023-09-10 23:51:38 -04:00
var svm = new SabreTools.Serialization.Streams.BDPlus().Deserialize(data);
2023-01-12 14:45:04 -08:00
if (svm == null)
return null;
2023-09-11 23:25:09 -04:00
try
2023-01-12 14:45:04 -08:00
{
2023-09-11 23:25:09 -04:00
return new BDPlusSVM(svm, data);
}
catch
{
return null;
}
2023-01-12 14:45:04 -08:00
}
#endregion
#region Printing
/// <inheritdoc/>
2023-01-13 14:04:21 -08:00
public override StringBuilder PrettyPrint()
2023-01-12 14:45:04 -08:00
{
2023-01-13 14:04:21 -08:00
StringBuilder builder = new StringBuilder();
2023-09-13 01:29:50 -04:00
Printing.BDPlusSVM.Print(builder, _model);
2023-01-13 14:04:21 -08:00
return builder;
2023-01-12 14:45:04 -08:00
}
#if NET6_0_OR_GREATER
/// <inheritdoc/>
2023-09-11 23:25:09 -04:00
public override string ExportJSON() => System.Text.Json.JsonSerializer.Serialize(_model, _jsonSerializerOptions);
#endif
2023-01-12 14:45:04 -08:00
#endregion
}
}