More usage of enums within BSA code

This commit is contained in:
Justin Swanson 2020-08-11 07:14:12 -05:00
parent 2e3b25ee69
commit b6d5464682
2 changed files with 28 additions and 47 deletions

View File

@ -15,15 +15,12 @@ namespace Compression.BSA
{
public class BSABuilder : IBSABuilder
{
internal uint _archiveFlags;
internal uint _fileFlags;
internal byte[] _fileId;
private List<FileEntry> _files = new List<FileEntry>();
internal List<FolderRecordBuilder> _folders = new List<FolderRecordBuilder>();
internal uint _offset;
internal uint _totalFileNameLength;
internal uint _version;
internal DiskSlabAllocator _slab;
public static async Task<BSABuilder> Create(long size)
@ -39,32 +36,20 @@ namespace Compression.BSA
public static async Task<BSABuilder> Create(BSAStateObject bsaStateObject, long size)
{
var self = await Create(size);
self._version = bsaStateObject.Version;
self._fileFlags = bsaStateObject.FileFlags;
self._archiveFlags = bsaStateObject.ArchiveFlags;
var self = await Create(size).ConfigureAwait(false);
self.HeaderType = (VersionType)bsaStateObject.Version;
self.FileFlags = (FileFlags)bsaStateObject.FileFlags;
self.ArchiveFlags = (ArchiveFlags)bsaStateObject.ArchiveFlags;
return self;
}
public IEnumerable<FileEntry> Files => _files;
public ArchiveFlags ArchiveFlags
{
get => (ArchiveFlags) _archiveFlags;
set => _archiveFlags = (uint) value;
}
public ArchiveFlags ArchiveFlags { get; set; }
public FileFlags FileFlags
{
get => (FileFlags) _archiveFlags;
set => _archiveFlags = (uint) value;
}
public FileFlags FileFlags { get; set; }
public VersionType HeaderType
{
get => (VersionType) _version;
set => _version = (uint) value;
}
public VersionType HeaderType { get; set; }
public IEnumerable<RelativePath> FolderNames
{
@ -74,13 +59,13 @@ namespace Compression.BSA
}
}
public bool HasFolderNames => (_archiveFlags & 0x1) > 0;
public bool HasFolderNames => ArchiveFlags.HasFlag(ArchiveFlags.HasFileNames);
public bool HasFileNames => (_archiveFlags & 0x2) > 0;
public bool HasFileNames => ArchiveFlags.HasFlag(ArchiveFlags.HasFileNames);
public bool CompressedByDefault => (_archiveFlags & 0x4) > 0;
public bool CompressedByDefault => ArchiveFlags.HasFlag(ArchiveFlags.Compressed);
public bool HasNameBlobs => (_archiveFlags & 0x100) > 0;
public bool HasNameBlobs => ArchiveFlags.HasFlag(ArchiveFlags.HasFileNameBlobs);
public async ValueTask DisposeAsync()
{
@ -105,9 +90,9 @@ namespace Compression.BSA
await using var wtr = new BinaryWriter(fs);
wtr.Write(_fileId);
wtr.Write(_version);
wtr.Write((uint)HeaderType);
wtr.Write(_offset);
wtr.Write(_archiveFlags);
wtr.Write((uint)ArchiveFlags);
var folders = FolderNames.ToList();
wtr.Write((uint) folders.Count);
wtr.Write((uint) _files.Count);
@ -115,7 +100,7 @@ namespace Compression.BSA
var s = _files.Select(f => f._pathBytes.Count()).Sum();
_totalFileNameLength = (uint) _files.Select(f => f._nameBytes.Count()).Sum();
wtr.Write(_totalFileNameLength); // totalFileNameLength
wtr.Write(_fileFlags);
wtr.Write((uint)FileFlags);
foreach (var folder in _folders) folder.WriteFolderRecord(wtr);

View File

@ -52,9 +52,7 @@ namespace Compression.BSA
public class BSAReader : IBSAReader
{
internal uint _archiveFlags;
internal uint _fileCount;
internal uint _fileFlags;
internal AbsolutePath _fileName;
internal uint _folderCount;
internal uint _folderRecordOffset;
@ -62,7 +60,6 @@ namespace Compression.BSA
internal string _magic;
internal uint _totalFileNameLength;
internal uint _totalFolderNameLength;
internal uint _version;
public void Dump(Action<string> print)
{
@ -107,26 +104,25 @@ namespace Compression.BSA
public ArchiveStateObject State => new BSAStateObject(this);
public VersionType HeaderType => (VersionType) _version;
public VersionType HeaderType { get; set; }
public ArchiveFlags ArchiveFlags => (ArchiveFlags) _archiveFlags;
public ArchiveFlags ArchiveFlags { get; set; }
public FileFlags FileFlags => (FileFlags)_fileFlags;
public FileFlags FileFlags { get; set; }
public bool HasFolderNames => ArchiveFlags.HasFlag(ArchiveFlags.HasFolderNames);
public bool HasFolderNames => (_archiveFlags & 0x1) > 0;
public bool HasFileNames => ArchiveFlags.HasFlag(ArchiveFlags.HasFileNames);
public bool HasFileNames => (_archiveFlags & 0x2) > 0;
public bool CompressedByDefault => ArchiveFlags.HasFlag(ArchiveFlags.Compressed);
public bool CompressedByDefault => (_archiveFlags & 0x4) > 0;
public bool Bit9Set => (_archiveFlags & 0x100) > 0;
public bool Bit9Set => ArchiveFlags.HasFlag(ArchiveFlags.HasFileNameBlobs);
public bool HasNameBlobs
{
get
{
if (HeaderType == VersionType.FO3 || HeaderType == VersionType.SSE) return (_archiveFlags & 0x100) > 0;
if (HeaderType == VersionType.FO3 || HeaderType == VersionType.SSE) return Bit9Set;
return false;
}
}
@ -139,14 +135,14 @@ namespace Compression.BSA
throw new InvalidDataException("Archive is not a BSA");
_magic = fourcc;
_version = rdr.ReadUInt32();
HeaderType = (VersionType)rdr.ReadUInt32();
_folderRecordOffset = rdr.ReadUInt32();
_archiveFlags = rdr.ReadUInt32();
ArchiveFlags = (ArchiveFlags)rdr.ReadUInt32();
_folderCount = rdr.ReadUInt32();
_fileCount = rdr.ReadUInt32();
_totalFolderNameLength = rdr.ReadUInt32();
_totalFileNameLength = rdr.ReadUInt32();
_fileFlags = rdr.ReadUInt32();
FileFlags = (FileFlags)rdr.ReadUInt32();
LoadFolderRecords(rdr);
}
@ -173,9 +169,9 @@ namespace Compression.BSA
public BSAStateObject(BSAReader bsaReader)
{
Magic = bsaReader._magic;
Version = bsaReader._version;
ArchiveFlags = bsaReader._archiveFlags;
FileFlags = bsaReader._fileFlags;
Version = (uint)bsaReader.HeaderType;
ArchiveFlags = (uint)bsaReader.ArchiveFlags;
FileFlags = (uint)bsaReader.FileFlags;
}