From 0696bbab723a6dfb2e242e36724233693b8e94f1 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 4 Apr 2024 14:06:59 -0400 Subject: [PATCH] Add MoPaQ wrapper --- .../Wrappers/MicrosoftCabinet.cs | 4 +- SabreTools.Serialization/Wrappers/MoPaQ.cs | 79 +++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 SabreTools.Serialization/Wrappers/MoPaQ.cs diff --git a/SabreTools.Serialization/Wrappers/MicrosoftCabinet.cs b/SabreTools.Serialization/Wrappers/MicrosoftCabinet.cs index aae4aa05..a021e4a4 100644 --- a/SabreTools.Serialization/Wrappers/MicrosoftCabinet.cs +++ b/SabreTools.Serialization/Wrappers/MicrosoftCabinet.cs @@ -26,7 +26,9 @@ namespace SabreTools.Serialization.Wrappers : base(model, data) { // All logic is handled by the base class - }/// + } + + /// /// Create a Microsoft Cabinet from a byte array and offset /// /// Byte array representing the cabinet diff --git a/SabreTools.Serialization/Wrappers/MoPaQ.cs b/SabreTools.Serialization/Wrappers/MoPaQ.cs new file mode 100644 index 00000000..43cf9990 --- /dev/null +++ b/SabreTools.Serialization/Wrappers/MoPaQ.cs @@ -0,0 +1,79 @@ +using System.IO; + +namespace SabreTools.Serialization.Wrappers +{ + // TODO: Figure out extension properties + public partial class MoPaQ : WrapperBase + { + #region Descriptive Properties + + /// + public override string DescriptionString => "MoPaQ Archive"; + + #endregion + + #region Constructors + + /// + public MoPaQ(Models.MoPaQ.Archive? model, byte[]? data, int offset) + : base(model, data, offset) + { + // All logic is handled by the base class + } + + /// + public MoPaQ(Models.MoPaQ.Archive? model, Stream? data) + : base(model, data) + { + // All logic is handled by the base class + } + + /// + /// Create a Microsoft Cabinet from a byte array and offset + /// + /// Byte array representing the cabinet + /// Offset within the array to parse + /// A cabinet wrapper on success, null on failure + public static MoPaQ? Create(byte[]? data, int offset) + { + // 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 + var dataStream = new MemoryStream(data, offset, data.Length - offset); + return Create(dataStream); + } + + /// + /// Create a Microsoft Cabinet from a Stream + /// + /// Stream representing the cabinet + /// A cabinet wrapper on success, null on failure + public static MoPaQ? Create(Stream? data) + { + // If the data is invalid + if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead) + return null; + + var cabinet = Deserializers.MoPaQ.DeserializeStream(data); + if (cabinet == null) + return null; + + try + { + return new MoPaQ(cabinet, data); + } + catch + { + return null; + } + } + + #endregion + } +}