From 87df6b3ebde1fbcd0916d9fefcdec23c98b58929 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 25 Oct 2023 14:51:07 -0400 Subject: [PATCH] Add IRD wrapper --- Wrappers/IRD.cs | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Wrappers/IRD.cs diff --git a/Wrappers/IRD.cs b/Wrappers/IRD.cs new file mode 100644 index 00000000..e4676718 --- /dev/null +++ b/Wrappers/IRD.cs @@ -0,0 +1,94 @@ +using System.IO; + +namespace SabreTools.Serialization.Wrappers +{ + public class IRD : WrapperBase + { + #region Descriptive Properties + + /// + public override string DescriptionString => "PS3 IRD file"; + + #endregion + + #region Constructors + + /// +#if NET48 + public IRD(Models.IRD.IRD model, byte[] data, int offset) +#else + public IRD(Models.IRD.IRD? model, byte[]? data, int offset) +#endif + : base(model, data, offset) + { + // All logic is handled by the base class + } + + /// +#if NET48 + public IRD(Models.IRD.IRD model, Stream data) +#else + public IRD(Models.IRD.IRD? model, Stream? data) +#endif + : base(model, data) + { + // All logic is handled by the base class + } + + /// + /// Create an IRD from a byte array and offset + /// + /// Byte array representing the archive + /// Offset within the array to parse + /// An IRD wrapper on success, null on failure +#if NET48 + public static IRD Create(byte[] data, int offset) +#else + public static IRD? Create(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 use that + MemoryStream dataStream = new MemoryStream(data, offset, data.Length - offset); + return Create(dataStream); + } + + /// + /// Create an IRD from a Stream + /// + /// Stream representing the archive + /// An IRD wrapper on success, null on failure +#if NET48 + public static IRD Create(Stream data) +#else + public static IRD? Create(Stream? data) +#endif + { + // If the data is invalid + if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead) + return null; + + var ird = new Streams.IRD().Deserialize(data); + if (ird == null) + return null; + + try + { + return new IRD(ird, data); + } + catch + { + return null; + } + } + + #endregion + } +} \ No newline at end of file