diff --git a/Bytes/BDPlus.Deserializer.cs b/Bytes/BDPlus.Deserializer.cs new file mode 100644 index 00000000..e3b90190 --- /dev/null +++ b/Bytes/BDPlus.Deserializer.cs @@ -0,0 +1,28 @@ +using System.IO; +using SabreTools.Models.BDPlus; + +namespace SabreTools.Serialization.Bytes +{ + public partial class BDPlus : IByteSerializer + { + /// +#if NET48 + public SVM Deserialize(byte[] data, int offset) +#else + public SVM? Deserialize(byte[]? data, int offset) +#endif + { + // 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 parse that + MemoryStream dataStream = new MemoryStream(data, offset, data.Length - offset); + return new Streams.BDPlus().Deserialize(dataStream); + } + } +} \ No newline at end of file diff --git a/IByteSerializer.cs b/IByteSerializer.cs index b4afc3b7..349f3cf1 100644 --- a/IByteSerializer.cs +++ b/IByteSerializer.cs @@ -17,5 +17,7 @@ namespace SabreTools.Serialization #else T? Deserialize(byte[]? data, int offset); #endif + + // TODO: Add serialization method } } \ No newline at end of file diff --git a/Streams/BDPlus.Deserializer.cs b/Streams/BDPlus.Deserializer.cs new file mode 100644 index 00000000..dd3ab023 --- /dev/null +++ b/Streams/BDPlus.Deserializer.cs @@ -0,0 +1,66 @@ +using System.IO; +using System.Text; +using SabreTools.IO; +using SabreTools.Models.BDPlus; +using static SabreTools.Models.BDPlus.Constants; + +namespace SabreTools.Serialization.Streams +{ + public partial class BDPlus : IStreamSerializer + { + /// +#if NET48 + public SVM Deserialize(Stream data) +#else + public SVM? Deserialize(Stream? data) +#endif + { + // If the data is invalid + if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead) + return null; + + // If the offset is out of bounds + if (data.Position < 0 || data.Position >= data.Length) + return null; + + // Cache the current offset + int initialOffset = (int)data.Position; + + // Try to parse the SVM + return ParseSVMData(data); + } + + /// + /// Parse a Stream into an SVM + /// + /// Stream to parse + /// Filled SVM on success, null on error + private static SVM ParseSVMData(Stream data) + { + // TODO: Use marshalling here instead of building + var svm = new SVM(); + + byte[] signature = data.ReadBytes(8); + svm.Signature = Encoding.ASCII.GetString(signature); + if (svm.Signature != SignatureString) + return null; + + svm.Unknown1 = data.ReadBytes(5); + svm.Year = data.ReadUInt16BigEndian(); + svm.Month = data.ReadByteValue(); + if (svm.Month < 1 || svm.Month > 12) + return null; + + svm.Day = data.ReadByteValue(); + if (svm.Day < 1 || svm.Day > 31) + return null; + + svm.Unknown2 = data.ReadBytes(4); + svm.Length = data.ReadUInt32(); + // if (svm.Length > 0) + // svm.Data = data.ReadBytes((int)svm.Length); + + return svm; + } + } +} \ No newline at end of file diff --git a/Streams/BDPlus.Serializer.cs b/Streams/BDPlus.Serializer.cs new file mode 100644 index 00000000..724ab761 --- /dev/null +++ b/Streams/BDPlus.Serializer.cs @@ -0,0 +1,16 @@ +using System; +using System.IO; +using SabreTools.Models.BDPlus; + +namespace SabreTools.Serialization.Streams +{ + public partial class BDPlus : IStreamSerializer + { + /// +#if NET48 + public Stream Serialize(SVM obj) => throw new NotImplementedException(); +#else + public Stream? Serialize(SVM? obj) => throw new NotImplementedException(); +#endif + } +} \ No newline at end of file