From 850a883e14ec5a1bbe081705c55cdfa427c43306 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 20 Aug 2025 08:08:53 -0400 Subject: [PATCH] Methods to properties --- .../Wrappers/DataSource.cs | 170 +++++++++--------- SabreTools.Serialization/Wrappers/VPK.cs | 2 +- .../Wrappers/WrapperBaseT.cs | 4 +- 3 files changed, 93 insertions(+), 83 deletions(-) diff --git a/SabreTools.Serialization/Wrappers/DataSource.cs b/SabreTools.Serialization/Wrappers/DataSource.cs index 7bbe73ea..e95021de 100644 --- a/SabreTools.Serialization/Wrappers/DataSource.cs +++ b/SabreTools.Serialization/Wrappers/DataSource.cs @@ -9,6 +9,51 @@ namespace SabreTools.Serialization.Wrappers /// public class DataSource { + #region Properties + + /// + /// Filename from the source, if possible + /// + /// String representing the filename on success, null otherwise + /// This only works if the source was a + public string? Filename + { + get + { + // Only streams can have a filename + if (_dataSourceType != DataSourceType.Stream) + return null; + + // Only file streams can have a filename + if (_streamData == null || _streamData is not FileStream fs) + return null; + + // Return the name + return fs.Name; + } + } + + /// + /// Usable length of the underlying data + /// + /// The usable length on success, -1 on error + public long Length + { + get + { + return _dataSourceType switch + { + DataSourceType.ByteArray => _byteArrayData!.Length - _initialPosition, + DataSourceType.Stream => _streamData!.Length - _initialPosition, + + // Everything else is invalid + _ => -1, + }; + } + } + + #endregion + #region Instance Variables /// @@ -70,86 +115,6 @@ namespace SabreTools.Serialization.Wrappers #region Data - /// - /// Get the filename from the source, if possible - /// - /// String representing the filename on success, null otherwise - /// This only works if the source was a - public string? GetFilename() - { - // Only streams can have a filename - if (_dataSourceType != DataSourceType.Stream) - return null; - - // Only file streams can have a filename - if (_streamData == null || _streamData is not FileStream fs) - return null; - - // Return the name - return fs.Name; - } - - /// - /// Get the usable length of the underlying data - /// - /// The usable length on success, -1 on error - public long GetLength() - { - return _dataSourceType switch - { - DataSourceType.ByteArray => _byteArrayData!.Length - _initialPosition, - DataSourceType.Stream => _streamData!.Length - _initialPosition, - - // Everything else is invalid - _ => -1, - }; - } - - /// - /// Validate the backing data source - /// - /// True if the data source is valid, false otherwise - public bool IsValid() - { - return _dataSourceType switch - { - // Byte array data requires both a valid array and offset - DataSourceType.ByteArray => _byteArrayData != null && _initialPosition >= 0, - - // Stream data requires both a valid stream - DataSourceType.Stream => _streamData != null && _initialPosition >= 0 && _streamData.CanRead && _streamData.CanSeek, - - // Everything else is invalid - _ => false, - }; - } - - /// - /// Check if a data segment is valid in the data source - /// - /// Position in the source - /// Length of the data to check - /// True if the positional data is valid, false otherwise - public bool SegmentValid(int position, int length) - { - // Validate the data souece - if (!IsValid()) - return false; - - // If we have an invalid position - if (position < 0 || position >= GetLength()) - return false; - - return _dataSourceType switch - { - DataSourceType.ByteArray => _initialPosition + position + length <= _byteArrayData!.Length, - DataSourceType.Stream => _initialPosition + position + length <= _streamData!.Length, - - // Everything else is invalid - _ => false, - }; - } - /// /// Read data from the source /// @@ -198,6 +163,51 @@ namespace SabreTools.Serialization.Wrappers } } + /// + /// Validate the backing data source + /// + /// True if the data source is valid, false otherwise + private bool IsValid() + { + return _dataSourceType switch + { + // Byte array data requires both a valid array and offset + DataSourceType.ByteArray => _byteArrayData != null && _initialPosition >= 0, + + // Stream data requires both a valid stream + DataSourceType.Stream => _streamData != null && _initialPosition >= 0 && _streamData.CanRead && _streamData.CanSeek, + + // Everything else is invalid + _ => false, + }; + } + + /// + /// Check if a data segment is valid in the data source + /// + /// Position in the source + /// Length of the data to check + /// True if the positional data is valid, false otherwise + private bool SegmentValid(int position, int length) + { + // Validate the data souece + if (!IsValid()) + return false; + + // If we have an invalid position + if (position < 0 || position >= Length) + return false; + + return _dataSourceType switch + { + DataSourceType.ByteArray => _initialPosition + position + length <= _byteArrayData!.Length, + DataSourceType.Stream => _initialPosition + position + length <= _streamData!.Length, + + // Everything else is invalid + _ => false, + }; + } + #endregion } } \ No newline at end of file diff --git a/SabreTools.Serialization/Wrappers/VPK.cs b/SabreTools.Serialization/Wrappers/VPK.cs index 3e1e5cc0..ba38aa14 100644 --- a/SabreTools.Serialization/Wrappers/VPK.cs +++ b/SabreTools.Serialization/Wrappers/VPK.cs @@ -29,7 +29,7 @@ namespace SabreTools.Serialization.Wrappers return _archiveFilenames; // If we don't have a source filename - string? sourceFilename = _dataSource.GetFilename(); + string? sourceFilename = _dataSource.Filename; if (string.IsNullOrEmpty(sourceFilename)) return null; diff --git a/SabreTools.Serialization/Wrappers/WrapperBaseT.cs b/SabreTools.Serialization/Wrappers/WrapperBaseT.cs index afe5c567..ff7424a2 100644 --- a/SabreTools.Serialization/Wrappers/WrapperBaseT.cs +++ b/SabreTools.Serialization/Wrappers/WrapperBaseT.cs @@ -19,9 +19,9 @@ namespace SabreTools.Serialization.Wrappers public T Model { get; } /// - /// Length of the underlying data + /// Usable length of the underlying data /// - public long Length => _dataSource.GetLength(); + public long Length => _dataSource.Length; #endregion