From 884746a5a19e35c56cbacf7a29f844d8709a1bd3 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 28 Nov 2024 20:50:43 -0500 Subject: [PATCH] Add more extension properties --- SabreTools.Serialization/Wrappers/PAK.cs | 20 ++++++++++----- SabreTools.Serialization/Wrappers/PFF.cs | 31 +++++++++++++++++------- SabreTools.Serialization/Wrappers/PIC.cs | 14 ++++++++--- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/SabreTools.Serialization/Wrappers/PAK.cs b/SabreTools.Serialization/Wrappers/PAK.cs index 7f8c0280..9f30a0fc 100644 --- a/SabreTools.Serialization/Wrappers/PAK.cs +++ b/SabreTools.Serialization/Wrappers/PAK.cs @@ -1,4 +1,5 @@ using System.IO; +using SabreTools.Models.PAK; namespace SabreTools.Serialization.Wrappers { @@ -11,6 +12,13 @@ namespace SabreTools.Serialization.Wrappers #endregion + #region Extension Properties + + /// + public DirectoryItem?[] DirectoryItems => Model.DirectoryItems ?? []; + + #endregion + #region Constructors /// @@ -85,12 +93,12 @@ namespace SabreTools.Serialization.Wrappers public bool ExtractAll(string outputDirectory) { // If we have no directory items - if (Model.DirectoryItems == null || Model.DirectoryItems.Length == 0) + if (DirectoryItems == null || DirectoryItems.Length == 0) return false; // Loop through and extract all files to the output bool allExtracted = true; - for (int i = 0; i < Model.DirectoryItems.Length; i++) + for (int i = 0; i < DirectoryItems.Length; i++) { allExtracted &= ExtractFile(i, outputDirectory); } @@ -107,15 +115,15 @@ namespace SabreTools.Serialization.Wrappers public bool ExtractFile(int index, string outputDirectory) { // If we have no directory items - if (Model.DirectoryItems == null || Model.DirectoryItems.Length == 0) + if (DirectoryItems == null || DirectoryItems.Length == 0) return false; // If the directory item index is invalid - if (index < 0 || index >= Model.DirectoryItems.Length) + if (index < 0 || index >= DirectoryItems.Length) return false; // Get the directory item - var directoryItem = Model.DirectoryItems[index]; + var directoryItem = DirectoryItems[index]; if (directoryItem == null) return false; @@ -143,7 +151,7 @@ namespace SabreTools.Serialization.Wrappers try { // Open the output file for writing - using Stream fs = File.OpenWrite(filename); + using Stream fs = System.IO.File.OpenWrite(filename); fs.Write(data, 0, data.Length); } catch diff --git a/SabreTools.Serialization/Wrappers/PFF.cs b/SabreTools.Serialization/Wrappers/PFF.cs index cf7af398..91b7482e 100644 --- a/SabreTools.Serialization/Wrappers/PFF.cs +++ b/SabreTools.Serialization/Wrappers/PFF.cs @@ -1,8 +1,9 @@ using System.IO; +using SabreTools.Models.PFF; namespace SabreTools.Serialization.Wrappers { - public class PFF : WrapperBase + public class PFF : WrapperBase { #region Descriptive Properties @@ -11,17 +12,29 @@ namespace SabreTools.Serialization.Wrappers #endregion + #region Extension Properties + + /// + /// Number of files in the archive + /// + public long FileCount => Model.Header?.NumberOfFiles ?? 0; + + /// + public Segment?[] Segments => Model.Segments ?? []; + + #endregion + #region Constructors /// - public PFF(Models.PFF.Archive? model, byte[]? data, int offset) + public PFF(Archive? model, byte[]? data, int offset) : base(model, data, offset) { // All logic is handled by the base class } /// - public PFF(Models.PFF.Archive? model, Stream? data) + public PFF(Archive? model, Stream? data) : base(model, data) { // All logic is handled by the base class @@ -85,12 +98,12 @@ namespace SabreTools.Serialization.Wrappers public bool ExtractAll(string outputDirectory) { // If we have no segments - if (Model.Segments == null || Model.Segments.Length == 0) + if (Segments == null || Segments.Length == 0) return false; // Loop through and extract all files to the output bool allExtracted = true; - for (int i = 0; i < Model.Segments.Length; i++) + for (int i = 0; i < Segments.Length; i++) { allExtracted &= ExtractSegment(i, outputDirectory); } @@ -107,19 +120,19 @@ namespace SabreTools.Serialization.Wrappers public bool ExtractSegment(int index, string outputDirectory) { // If we have no files - if (Model.Header?.NumberOfFiles == null || Model.Header.NumberOfFiles == 0) + if (FileCount == 0) return false; // If we have no segments - if (Model.Segments == null || Model.Segments.Length == 0) + if (Segments == null || Segments.Length == 0) return false; // If we have an invalid index - if (index < 0 || index >= Model.Segments.Length) + if (index < 0 || index >= Segments.Length) return false; // Get the segment information - var file = Model.Segments[index]; + var file = Segments[index]; if (file == null) return false; diff --git a/SabreTools.Serialization/Wrappers/PIC.cs b/SabreTools.Serialization/Wrappers/PIC.cs index e207dc6a..a371cddb 100644 --- a/SabreTools.Serialization/Wrappers/PIC.cs +++ b/SabreTools.Serialization/Wrappers/PIC.cs @@ -1,8 +1,9 @@ using System.IO; +using SabreTools.Models.PIC; namespace SabreTools.Serialization.Wrappers { - public class PIC : WrapperBase + public class PIC : WrapperBase { #region Descriptive Properties @@ -11,17 +12,24 @@ namespace SabreTools.Serialization.Wrappers #endregion + #region Extension Properties + + /// + public DiscInformationUnit?[] Units => Model.Units ?? []; + + #endregion + #region Constructors /// - public PIC(Models.PIC.DiscInformation? model, byte[]? data, int offset) + public PIC(DiscInformation? model, byte[]? data, int offset) : base(model, data, offset) { // All logic is handled by the base class } /// - public PIC(Models.PIC.DiscInformation? model, Stream? data) + public PIC(DiscInformation? model, Stream? data) : base(model, data) { // All logic is handled by the base class