From 970fcbd93bc49ae7f8894848d4891808707037ee Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sat, 27 Apr 2024 22:45:49 -0400 Subject: [PATCH] Add PKZIP shell wrapper --- SabreTools.Serialization/Wrappers/PKZIP.cs | 79 +++++++++++++++++++ .../Wrappers/WrapperFactory.cs | 2 +- .../Wrappers/WrapperType.cs | 1 - 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 SabreTools.Serialization/Wrappers/PKZIP.cs diff --git a/SabreTools.Serialization/Wrappers/PKZIP.cs b/SabreTools.Serialization/Wrappers/PKZIP.cs new file mode 100644 index 00000000..56443b8f --- /dev/null +++ b/SabreTools.Serialization/Wrappers/PKZIP.cs @@ -0,0 +1,79 @@ +using System.IO; +using SabreTools.Models.PKZIP; + +namespace SabreTools.Serialization.Wrappers +{ + public class PKZIP : WrapperBase + { + #region Descriptive Properties + + /// + public override string DescriptionString => "PKZIP Archive (or Derived Format)"; + + #endregion + + #region Constructors + + /// + public PKZIP(Archive? model, byte[]? data, int offset) + : base(model, data, offset) + { + // All logic is handled by the base class + } + + /// + public PKZIP(Archive? model, Stream? data) + : base(model, data) + { + // All logic is handled by the base class + } + + /// + /// Create a PKZIP archive (or derived format) from a byte array and offset + /// + /// Byte array representing the archive + /// Offset within the array to parse + /// A PKZIP wrapper on success, null on failure + public static PKZIP? 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 PKZIP archive (or derived format) from a Stream + /// + /// Stream representing the archive + /// A PKZIP wrapper on success, null on failure + public static PKZIP? Create(Stream? data) + { + // If the data is invalid + if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead) + return null; + + var archive = Deserializers.PKZIP.DeserializeStream(data); + if (archive == null) + return null; + + try + { + return new PKZIP(archive, data); + } + catch + { + return null; + } + } + + #endregion + } +} \ No newline at end of file diff --git a/SabreTools.Serialization/Wrappers/WrapperFactory.cs b/SabreTools.Serialization/Wrappers/WrapperFactory.cs index e9014c96..79c6557e 100644 --- a/SabreTools.Serialization/Wrappers/WrapperFactory.cs +++ b/SabreTools.Serialization/Wrappers/WrapperFactory.cs @@ -38,7 +38,7 @@ namespace SabreTools.Serialization.Wrappers case WrapperType.PAK: return PAK.Create(data); case WrapperType.PFF: return PFF.Create(data); case WrapperType.PIC: return PIC.Create(data); - case WrapperType.PKZIP: return null; // TODO: Implement wrapper + case WrapperType.PKZIP: return PKZIP.Create(data); case WrapperType.PlayJAudioFile: return PlayJAudioFile.Create(data); case WrapperType.PlayJPlaylist: return PlayJPlaylist.Create(data); case WrapperType.Quantum: return Quantum.Create(data); diff --git a/SabreTools.Serialization/Wrappers/WrapperType.cs b/SabreTools.Serialization/Wrappers/WrapperType.cs index c852e17b..a0070ca3 100644 --- a/SabreTools.Serialization/Wrappers/WrapperType.cs +++ b/SabreTools.Serialization/Wrappers/WrapperType.cs @@ -136,7 +136,6 @@ namespace SabreTools.Serialization.Wrappers /// /// PKWARE ZIP archive and derivatives /// - /// Currently has no IWrapper implementation PKZIP, ///