wabbajack/Wabbajack.Compression.BSA/TES5Archive/FileNameBlock.cs

37 lines
1.5 KiB
C#
Raw Normal View History

using System;
using System.IO;
2021-09-27 12:42:46 +00:00
using System.Threading;
using Wabbajack.Common;
2021-09-27 12:42:46 +00:00
namespace Wabbajack.Compression.BSA.TES5Archive
{
internal class FileNameBlock
{
public readonly Lazy<ReadOnlyMemorySlice<byte>[]> Names;
2021-09-27 12:42:46 +00:00
public FileNameBlock(Reader bsa, long position)
{
Names = new Lazy<ReadOnlyMemorySlice<byte>[]>(
2021-09-27 12:42:46 +00:00
mode: LazyThreadSafetyMode.ExecutionAndPublication,
valueFactory: () =>
{
using var stream = bsa.GetStream();
stream.BaseStream.Position = position;
ReadOnlyMemorySlice<byte> data = stream.ReadBytes(checked((int)bsa._totalFileNameLength));
ReadOnlyMemorySlice<byte>[] names = new ReadOnlyMemorySlice<byte>[bsa._fileCount];
2021-09-27 12:42:46 +00:00
for (var i = 0; i < bsa._fileCount; i++)
{
var index = data.Span.IndexOf(default(byte));
2021-09-27 12:42:46 +00:00
if (index == -1) throw new InvalidDataException("Did not end all of its strings in null bytes");
names[i] = data.Slice(0, index + 1);
var str = names[i].ReadStringTerm(bsa.HeaderType);
data = data.Slice(index + 1);
}
2021-09-27 12:42:46 +00:00
2020-08-11 16:08:37 +00:00
// Data doesn't seem to need to be fully consumed.
// Official BSAs have overflow of zeros
return names;
});
}
}
2021-09-27 12:42:46 +00:00
}