2019-10-07 22:13:38 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2020-03-05 00:02:16 +00:00
|
|
|
|
using System.IO.MemoryMappedFiles;
|
2019-10-07 22:13:38 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2020-04-17 03:52:19 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-04-03 03:57:59 +00:00
|
|
|
|
using ICSharpCode.SharpZipLib.Zip.Compression;
|
2019-10-07 22:13:38 +00:00
|
|
|
|
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
|
2020-03-05 00:02:16 +00:00
|
|
|
|
using Wabbajack.Common;
|
2019-10-07 22:13:38 +00:00
|
|
|
|
|
2020-06-27 14:26:38 +00:00
|
|
|
|
namespace Wabbajack.Compression.BSA
|
2019-10-07 22:13:38 +00:00
|
|
|
|
{
|
|
|
|
|
interface IFileBuilder
|
|
|
|
|
{
|
|
|
|
|
uint FileHash { get; }
|
|
|
|
|
uint DirHash { get; }
|
|
|
|
|
string FullName { get; }
|
|
|
|
|
|
|
|
|
|
int Index { get; }
|
|
|
|
|
|
2020-04-20 21:36:33 +00:00
|
|
|
|
Task WriteData(BinaryWriter wtr);
|
2019-10-07 22:13:38 +00:00
|
|
|
|
void WriteHeader(BinaryWriter wtr);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public class BA2Builder : IBSABuilder
|
|
|
|
|
{
|
|
|
|
|
private BA2StateObject _state;
|
|
|
|
|
private List<IFileBuilder> _entries = new List<IFileBuilder>();
|
2020-03-05 00:02:16 +00:00
|
|
|
|
private DiskSlabAllocator _slab;
|
2019-10-07 22:13:38 +00:00
|
|
|
|
|
2020-03-09 20:38:35 +00:00
|
|
|
|
public BA2Builder(BA2StateObject state, long size)
|
2019-10-07 22:13:38 +00:00
|
|
|
|
{
|
|
|
|
|
_state = state;
|
2020-03-09 20:38:35 +00:00
|
|
|
|
_slab = new DiskSlabAllocator(size);
|
2019-10-07 22:13:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 03:52:19 +00:00
|
|
|
|
public async ValueTask DisposeAsync()
|
2019-10-07 22:13:38 +00:00
|
|
|
|
{
|
2020-05-28 02:43:57 +00:00
|
|
|
|
await _slab.DisposeAsync();
|
2019-10-07 22:13:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 21:36:33 +00:00
|
|
|
|
public async Task AddFile(FileStateObject state, Stream src)
|
2019-10-07 22:13:38 +00:00
|
|
|
|
{
|
|
|
|
|
switch (_state.Type)
|
|
|
|
|
{
|
|
|
|
|
case EntryType.GNRL:
|
2020-04-20 21:36:33 +00:00
|
|
|
|
var result = await BA2FileEntryBuilder.Create((BA2FileEntryState)state, src, _slab);
|
2019-10-07 22:13:38 +00:00
|
|
|
|
lock(_entries) _entries.Add(result);
|
|
|
|
|
break;
|
2019-10-08 04:02:03 +00:00
|
|
|
|
case EntryType.DX10:
|
2020-04-20 21:36:33 +00:00
|
|
|
|
var resultdx10 = await BA2DX10FileEntryBuilder.Create((BA2DX10EntryState)state, src, _slab);
|
2019-10-08 04:02:03 +00:00
|
|
|
|
lock(_entries) _entries.Add(resultdx10);
|
|
|
|
|
break;
|
2019-10-07 22:13:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 21:36:33 +00:00
|
|
|
|
public async Task Build(AbsolutePath filename)
|
2019-10-07 22:13:38 +00:00
|
|
|
|
{
|
|
|
|
|
SortEntries();
|
2020-05-25 16:24:16 +00:00
|
|
|
|
await using var fs = await filename.Create();
|
2020-04-20 21:36:33 +00:00
|
|
|
|
await using var bw = new BinaryWriter(fs);
|
2020-04-03 03:57:59 +00:00
|
|
|
|
|
|
|
|
|
bw.Write(Encoding.ASCII.GetBytes(_state.HeaderMagic));
|
|
|
|
|
bw.Write(_state.Version);
|
|
|
|
|
bw.Write(Encoding.ASCII.GetBytes(Enum.GetName(typeof(EntryType), _state.Type)));
|
|
|
|
|
bw.Write((uint)_entries.Count);
|
|
|
|
|
var table_offset_loc = bw.BaseStream.Position;
|
|
|
|
|
bw.Write((ulong)0);
|
|
|
|
|
|
|
|
|
|
foreach (var entry in _entries)
|
2019-10-07 22:13:38 +00:00
|
|
|
|
{
|
2020-04-03 03:57:59 +00:00
|
|
|
|
entry.WriteHeader(bw);
|
|
|
|
|
}
|
2019-10-07 22:13:38 +00:00
|
|
|
|
|
2020-04-20 21:36:33 +00:00
|
|
|
|
await _entries.DoProgress("Writing BSA Files", async entry =>
|
2020-04-03 03:57:59 +00:00
|
|
|
|
{
|
2020-04-20 21:36:33 +00:00
|
|
|
|
await entry.WriteData(bw);
|
|
|
|
|
});
|
2019-10-07 22:13:38 +00:00
|
|
|
|
|
2020-04-03 03:57:59 +00:00
|
|
|
|
if (!_state.HasNameTable) return;
|
|
|
|
|
|
|
|
|
|
var pos = bw.BaseStream.Position;
|
|
|
|
|
bw.BaseStream.Seek(table_offset_loc, SeekOrigin.Begin);
|
|
|
|
|
bw.Write((ulong)pos);
|
|
|
|
|
bw.BaseStream.Seek(pos, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
|
|
foreach (var entry in _entries)
|
|
|
|
|
{
|
2020-05-11 14:32:29 +00:00
|
|
|
|
var bytes = Encoding.UTF8.GetBytes(entry.FullName);
|
2020-04-03 03:57:59 +00:00
|
|
|
|
bw.Write((ushort)bytes.Length);
|
2020-04-20 21:36:33 +00:00
|
|
|
|
await bw.BaseStream.WriteAsync(bytes, 0, bytes.Length);
|
2019-10-07 22:13:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SortEntries()
|
|
|
|
|
{
|
|
|
|
|
_entries = _entries.OrderBy(e => e.Index).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 04:02:03 +00:00
|
|
|
|
public class BA2DX10FileEntryBuilder : IFileBuilder
|
|
|
|
|
{
|
|
|
|
|
private BA2DX10EntryState _state;
|
|
|
|
|
private List<ChunkBuilder> _chunks;
|
|
|
|
|
|
2020-04-20 21:36:33 +00:00
|
|
|
|
public static async Task<BA2DX10FileEntryBuilder> Create(BA2DX10EntryState state, Stream src, DiskSlabAllocator slab)
|
2019-10-08 04:02:03 +00:00
|
|
|
|
{
|
2019-11-16 00:01:37 +00:00
|
|
|
|
var builder = new BA2DX10FileEntryBuilder {_state = state};
|
2019-10-10 05:04:28 +00:00
|
|
|
|
|
2020-04-03 03:57:59 +00:00
|
|
|
|
var headerSize = DDS.HeaderSizeForFormat((DXGI_FORMAT) state.PixelFormat) + 4;
|
|
|
|
|
new BinaryReader(src).ReadBytes((int)headerSize);
|
2019-10-10 05:04:28 +00:00
|
|
|
|
|
2019-11-12 04:35:07 +00:00
|
|
|
|
// This can't be parallel because it all runs off the same base IO stream.
|
|
|
|
|
builder._chunks = new List<ChunkBuilder>();
|
|
|
|
|
|
|
|
|
|
foreach (var chunk in state.Chunks)
|
2020-04-20 21:36:33 +00:00
|
|
|
|
builder._chunks.Add(await ChunkBuilder.Create(state, chunk, src, slab));
|
2019-11-12 04:35:07 +00:00
|
|
|
|
|
|
|
|
|
return builder;
|
2019-10-08 04:02:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint FileHash => _state.NameHash;
|
|
|
|
|
public uint DirHash => _state.DirHash;
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public string FullName => (string)_state.Path;
|
2019-10-08 04:02:03 +00:00
|
|
|
|
public int Index => _state.Index;
|
|
|
|
|
|
|
|
|
|
public void WriteHeader(BinaryWriter bw)
|
|
|
|
|
{
|
|
|
|
|
bw.Write(_state.NameHash);
|
|
|
|
|
bw.Write(Encoding.ASCII.GetBytes(_state.Extension));
|
|
|
|
|
bw.Write(_state.DirHash);
|
|
|
|
|
bw.Write(_state.Unk8);
|
|
|
|
|
bw.Write((byte)_chunks.Count);
|
|
|
|
|
bw.Write(_state.ChunkHdrLen);
|
|
|
|
|
bw.Write(_state.Height);
|
|
|
|
|
bw.Write(_state.Width);
|
|
|
|
|
bw.Write(_state.NumMips);
|
|
|
|
|
bw.Write(_state.PixelFormat);
|
|
|
|
|
bw.Write(_state.Unk16);
|
|
|
|
|
|
|
|
|
|
foreach (var chunk in _chunks)
|
|
|
|
|
chunk.WriteHeader(bw);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 21:36:33 +00:00
|
|
|
|
public async Task WriteData(BinaryWriter wtr)
|
2019-10-08 04:02:03 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var chunk in _chunks)
|
2020-04-20 21:36:33 +00:00
|
|
|
|
await chunk.WriteData(wtr);
|
2019-10-08 04:02:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ChunkBuilder
|
|
|
|
|
{
|
|
|
|
|
private ChunkState _chunk;
|
|
|
|
|
private uint _packSize;
|
|
|
|
|
private long _offsetOffset;
|
2020-03-05 00:02:16 +00:00
|
|
|
|
private Stream _dataSlab;
|
2019-10-08 04:02:03 +00:00
|
|
|
|
|
2020-04-20 21:36:33 +00:00
|
|
|
|
public static async Task<ChunkBuilder> Create(BA2DX10EntryState state, ChunkState chunk, Stream src, DiskSlabAllocator slab)
|
2019-10-08 04:02:03 +00:00
|
|
|
|
{
|
2019-11-12 04:35:07 +00:00
|
|
|
|
var builder = new ChunkBuilder {_chunk = chunk};
|
2019-10-10 05:04:28 +00:00
|
|
|
|
|
2020-03-05 00:02:16 +00:00
|
|
|
|
if (!chunk.Compressed)
|
|
|
|
|
{
|
|
|
|
|
builder._dataSlab = slab.Allocate(chunk.FullSz);
|
2020-04-20 21:36:33 +00:00
|
|
|
|
await src.CopyToWithStatusAsync((int)chunk.FullSz, builder._dataSlab, $"Writing {state.Path} {chunk.StartMip}:{chunk.EndMip}");
|
2019-10-10 05:04:28 +00:00
|
|
|
|
}
|
2020-03-05 00:02:16 +00:00
|
|
|
|
else
|
2019-10-08 04:02:03 +00:00
|
|
|
|
{
|
2020-04-03 03:57:59 +00:00
|
|
|
|
var deflater = new Deflater(Deflater.BEST_COMPRESSION);
|
2020-04-20 21:36:33 +00:00
|
|
|
|
await using var ms = new MemoryStream();
|
|
|
|
|
await using (var ds = new DeflaterOutputStream(ms, deflater))
|
2019-10-08 04:02:03 +00:00
|
|
|
|
{
|
2020-03-05 05:27:15 +00:00
|
|
|
|
ds.IsStreamOwner = false;
|
2020-04-20 21:36:33 +00:00
|
|
|
|
await src.CopyToWithStatusAsync((int)chunk.FullSz, ds, $"Compressing {state.Path} {chunk.StartMip}:{chunk.EndMip}");
|
2019-10-08 04:02:03 +00:00
|
|
|
|
}
|
2019-11-12 04:35:07 +00:00
|
|
|
|
|
2020-03-05 00:02:16 +00:00
|
|
|
|
builder._dataSlab = slab.Allocate(ms.Length);
|
|
|
|
|
ms.Position = 0;
|
2020-04-20 21:36:33 +00:00
|
|
|
|
await ms.CopyToWithStatusAsync(ms.Length, builder._dataSlab, $"Writing {state.Path} {chunk.StartMip}:{chunk.EndMip}");
|
2020-03-05 00:02:16 +00:00
|
|
|
|
builder._packSize = (uint)ms.Length;
|
2019-10-08 04:02:03 +00:00
|
|
|
|
}
|
2020-03-05 00:02:16 +00:00
|
|
|
|
builder._dataSlab.Position = 0;
|
2019-11-12 04:35:07 +00:00
|
|
|
|
|
|
|
|
|
return builder;
|
2019-10-08 04:02:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WriteHeader(BinaryWriter bw)
|
|
|
|
|
{
|
|
|
|
|
_offsetOffset = bw.BaseStream.Position;
|
|
|
|
|
bw.Write((ulong)0);
|
|
|
|
|
bw.Write(_packSize);
|
|
|
|
|
bw.Write(_chunk.FullSz);
|
|
|
|
|
bw.Write(_chunk.StartMip);
|
|
|
|
|
bw.Write(_chunk.EndMip);
|
|
|
|
|
bw.Write(_chunk.Align);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 21:36:33 +00:00
|
|
|
|
public async Task WriteData(BinaryWriter bw)
|
2019-10-08 04:02:03 +00:00
|
|
|
|
{
|
|
|
|
|
var pos = bw.BaseStream.Position;
|
|
|
|
|
bw.BaseStream.Position = _offsetOffset;
|
|
|
|
|
bw.Write((ulong)pos);
|
|
|
|
|
bw.BaseStream.Position = pos;
|
2020-04-20 21:36:33 +00:00
|
|
|
|
await _dataSlab.CopyToLimitAsync(bw.BaseStream, (int)_dataSlab.Length);
|
|
|
|
|
await _dataSlab.DisposeAsync();
|
2019-10-08 04:02:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-07 22:13:38 +00:00
|
|
|
|
public class BA2FileEntryBuilder : IFileBuilder
|
|
|
|
|
{
|
|
|
|
|
private int _rawSize;
|
|
|
|
|
private int _size;
|
|
|
|
|
private BA2FileEntryState _state;
|
|
|
|
|
private long _offsetOffset;
|
2020-03-05 00:02:16 +00:00
|
|
|
|
private Stream _dataSrc;
|
2019-10-07 22:13:38 +00:00
|
|
|
|
|
2020-04-20 21:36:33 +00:00
|
|
|
|
public static async Task<BA2FileEntryBuilder> Create(BA2FileEntryState state, Stream src, DiskSlabAllocator slab)
|
2019-10-07 22:13:38 +00:00
|
|
|
|
{
|
2020-03-05 00:02:16 +00:00
|
|
|
|
var builder = new BA2FileEntryBuilder
|
2019-10-07 22:13:38 +00:00
|
|
|
|
{
|
2020-03-05 00:02:16 +00:00
|
|
|
|
_state = state,
|
|
|
|
|
_rawSize = (int)src.Length,
|
|
|
|
|
_dataSrc = src
|
|
|
|
|
};
|
|
|
|
|
if (!state.Compressed)
|
|
|
|
|
return builder;
|
2019-10-07 22:13:38 +00:00
|
|
|
|
|
2020-04-20 21:36:33 +00:00
|
|
|
|
await using (var ms = new MemoryStream())
|
2019-10-07 22:13:38 +00:00
|
|
|
|
{
|
2020-04-20 21:36:33 +00:00
|
|
|
|
await using (var ds = new DeflaterOutputStream(ms))
|
2019-10-07 22:13:38 +00:00
|
|
|
|
{
|
2020-03-05 05:27:15 +00:00
|
|
|
|
ds.IsStreamOwner = false;
|
2020-04-20 21:36:33 +00:00
|
|
|
|
await builder._dataSrc.CopyToAsync(ds);
|
2019-10-07 22:13:38 +00:00
|
|
|
|
}
|
2020-03-05 00:02:16 +00:00
|
|
|
|
|
|
|
|
|
builder._dataSrc = slab.Allocate(ms.Length);
|
|
|
|
|
ms.Position = 0;
|
2020-04-20 21:36:33 +00:00
|
|
|
|
await ms.CopyToAsync(builder._dataSrc);
|
2020-03-05 00:02:16 +00:00
|
|
|
|
builder._dataSrc.Position = 0;
|
|
|
|
|
builder._size = (int)ms.Length;
|
2019-10-07 22:13:38 +00:00
|
|
|
|
}
|
2019-11-12 04:35:07 +00:00
|
|
|
|
return builder;
|
2019-10-07 22:13:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint FileHash => _state.NameHash;
|
|
|
|
|
public uint DirHash => _state.DirHash;
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public string FullName => (string)_state.Path;
|
2019-10-07 22:13:38 +00:00
|
|
|
|
public int Index => _state.Index;
|
|
|
|
|
|
|
|
|
|
public void WriteHeader(BinaryWriter wtr)
|
|
|
|
|
{
|
|
|
|
|
wtr.Write(_state.NameHash);
|
|
|
|
|
wtr.Write(Encoding.ASCII.GetBytes(_state.Extension));
|
|
|
|
|
wtr.Write(_state.DirHash);
|
|
|
|
|
wtr.Write(_state.Flags);
|
|
|
|
|
_offsetOffset = wtr.BaseStream.Position;
|
|
|
|
|
wtr.Write((ulong)0);
|
|
|
|
|
wtr.Write(_size);
|
|
|
|
|
wtr.Write(_rawSize);
|
|
|
|
|
wtr.Write(_state.Align);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 21:36:33 +00:00
|
|
|
|
public async Task WriteData(BinaryWriter wtr)
|
2019-10-07 22:13:38 +00:00
|
|
|
|
{
|
|
|
|
|
var pos = wtr.BaseStream.Position;
|
2020-03-05 00:02:16 +00:00
|
|
|
|
wtr.BaseStream.Position = _offsetOffset;
|
2019-10-07 22:13:38 +00:00
|
|
|
|
wtr.Write((ulong)pos);
|
|
|
|
|
wtr.BaseStream.Position = pos;
|
2020-03-05 00:02:16 +00:00
|
|
|
|
_dataSrc.Position = 0;
|
2020-04-20 21:36:33 +00:00
|
|
|
|
await _dataSrc.CopyToLimitAsync(wtr.BaseStream, (int)_dataSrc.Length);
|
|
|
|
|
await _dataSrc.DisposeAsync();
|
2019-10-07 22:13:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|