From a6169988c1f4ff8a463432953e215e061dc446a1 Mon Sep 17 00:00:00 2001 From: Justin Swanson Date: Tue, 11 Aug 2020 10:24:02 -0500 Subject: [PATCH] IFolder --- Compression.BSA/BSA/Reader/BSAReader.cs | 13 ++++--------- Compression.BSA/BSA/Reader/FolderRecord.cs | 16 ++++++---------- Compression.BSA/Interfaces/IFolder.cs | 13 +++++++++++++ 3 files changed, 23 insertions(+), 19 deletions(-) create mode 100644 Compression.BSA/Interfaces/IFolder.cs diff --git a/Compression.BSA/BSA/Reader/BSAReader.cs b/Compression.BSA/BSA/Reader/BSAReader.cs index f96e0ffd..199ab80d 100644 --- a/Compression.BSA/BSA/Reader/BSAReader.cs +++ b/Compression.BSA/BSA/Reader/BSAReader.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text; using System.Threading.Tasks; using Wabbajack.Common; @@ -28,15 +29,9 @@ namespace Compression.BSA public FileFlags FileFlags { get; private set; } - public IEnumerable Files - { - get - { - foreach (var folder in _folders.Value) - foreach (var file in folder._files.Value) - yield return file; - } - } + public IEnumerable Files => _folders.Value.SelectMany(f => f.Files); + + public IEnumerable Folders => _folders.Value; public ArchiveStateObject State => new BSAStateObject(this); diff --git a/Compression.BSA/BSA/Reader/FolderRecord.cs b/Compression.BSA/BSA/Reader/FolderRecord.cs index 1cc64010..00be479c 100644 --- a/Compression.BSA/BSA/Reader/FolderRecord.cs +++ b/Compression.BSA/BSA/Reader/FolderRecord.cs @@ -9,34 +9,30 @@ using File = Alphaleonis.Win32.Filesystem.File; namespace Compression.BSA { - public class FolderRecord + public class FolderRecord : IFolder { internal readonly BSAReader BSA; private readonly ReadOnlyMemorySlice _data; internal Lazy _files; - private ReadOnlyMemorySlice? _nameData; private int _prevFileCount; internal FileNameBlock FileNameBlock; - private readonly Lazy _name; + internal int Index { get; } + public string Name { get; private set; } - public int Index { get; } - public string Name => _name.Value; + public IEnumerable Files => _files.Value; internal FolderRecord(BSAReader bsa, ReadOnlyMemorySlice data, int index) { BSA = bsa; _data = data; Index = index; - _name = new Lazy( - () => _nameData.HasValue ? _nameData.Value.ReadStringTerm(BSA.HeaderType) : string.Empty, - isThreadSafe: true); } private bool IsLongform => BSA.HeaderType == VersionType.SSE; public ulong Hash => BinaryPrimitives.ReadUInt64LittleEndian(_data); - public uint FileCount => BinaryPrimitives.ReadUInt32LittleEndian(_data.Slice(0x8)); + public int FileCount => checked((int)BinaryPrimitives.ReadUInt32LittleEndian(_data.Slice(0x8))); public uint Unknown => IsLongform ? BinaryPrimitives.ReadUInt32LittleEndian(_data.Slice(0xC)) : @@ -65,7 +61,7 @@ namespace Compression.BSA { var len = rdr.ReadByte(); data = rdr.ReadBytes(len + totalFileLen); - _nameData = data.Slice(0, len); + Name = data.Slice(0, len).ReadStringTerm(BSA.HeaderType); data = data.Slice(len); } else diff --git a/Compression.BSA/Interfaces/IFolder.cs b/Compression.BSA/Interfaces/IFolder.cs new file mode 100644 index 00000000..a3da1107 --- /dev/null +++ b/Compression.BSA/Interfaces/IFolder.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Compression.BSA +{ + public interface IFolder + { + string Name { get; } + IEnumerable Files { get; } + int FileCount { get; } + } +}