EndToEnd Tests pass

This commit is contained in:
Timothy Baldridge
2020-09-05 21:19:05 -06:00
parent 1ebb0b6492
commit 3bdab577e1
9 changed files with 91 additions and 17 deletions

View File

@ -3,6 +3,9 @@ using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Compression.BSA;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using SharpCompress.Archives.SevenZip;
using SharpCompress.Readers;
using Wabbajack.Common;
using Wabbajack.Common.FileSignatures;
using Wabbajack.VirtualFileSystem.SevenZipExtractor;
@ -16,7 +19,8 @@ namespace Wabbajack.VirtualFileSystem
Definitions.FileType.BA2,
Definitions.FileType.ZIP,
//Definitions.FileType.EXE,
Definitions.FileType.RAR,
Definitions.FileType.RAR_OLD,
Definitions.FileType.RAR_NEW,
Definitions.FileType._7Z);
@ -29,6 +33,9 @@ namespace Wabbajack.VirtualFileSystem
switch (sig)
{
case Definitions.FileType.RAR_OLD:
case Definitions.FileType.RAR_NEW:
case Definitions.FileType._7Z:
case Definitions.FileType.ZIP:
return await GatheringExtractWith7Zip<T>(archive, (Definitions.FileType)sig, shouldExtract, mapfn);
@ -62,6 +69,43 @@ namespace Wabbajack.VirtualFileSystem
private static async Task<Dictionary<RelativePath,T>> GatheringExtractWith7Zip<T>(Stream stream, Definitions.FileType sig, Predicate<RelativePath> shouldExtract, Func<RelativePath,IStreamFactory,ValueTask<T>> mapfn)
{
return await new GatheringExtractor<T>(stream, sig, shouldExtract, mapfn).Extract();
/*
IReader reader;
if (sig == Definitions.FileType._7Z)
reader = SevenZipArchive.Open(stream).ExtractAllEntries();
else
{
reader = ReaderFactory.Open(stream);
}
var results = new Dictionary<RelativePath, T>();
while (reader.MoveToNextEntry())
{
var path = (RelativePath)reader.Entry.Key;
if (!reader.Entry.IsDirectory && shouldExtract(path))
{
var ms = new MemoryStream();
reader.WriteEntryTo(ms);
ms.Position = 0;
var result = await mapfn(path, new MemoryStreamFactory(ms));
results.Add(path, result);
}
}
return results;
*/
}
public static async Task ExtractAll(AbsolutePath src, AbsolutePath dest)
{
await GatheringExtract(new NativeFileStreamFactory(src), _ => true, async (path, factory) =>
{
var abs = path.RelativeTo(dest);
abs.Parent.CreateDirectory();
await using var stream = await factory.GetStream();
await abs.WriteAllAsync(stream);
return 0;
});
}
}
}

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Compression.BSA;
using Wabbajack.Common;
using Wabbajack.Common.FileSignatures;
using Wabbajack.VirtualFileSystem.SevenZipExtractor;
@ -17,7 +18,7 @@ namespace Wabbajack.VirtualFileSystem
private Predicate<RelativePath> _shouldExtract;
private Func<RelativePath, IStreamFactory, ValueTask<T>> _mapFn;
private Dictionary<RelativePath, T> _results;
private Dictionary<uint, RelativePath> _indexes;
private Dictionary<uint, (RelativePath, ulong)> _indexes;
private Stream _stream;
private Definitions.FileType _sig;
@ -41,10 +42,11 @@ namespace Wabbajack.VirtualFileSystem
{
_archive = ArchiveFile.Open(_stream, _sig).Result;
_indexes = _archive.Entries
.Where(f => !f.IsFolder)
.Select((entry, idx) => ((RelativePath)entry.FileName, (uint)idx))
.Select((entry, idx) => (entry, (uint)idx))
.Where(f => !f.entry.IsFolder)
.Select(t => ((RelativePath)t.entry.FileName, t.Item2, t.entry.Size))
.Where(t => _shouldExtract(t.Item1))
.ToDictionary(t => t.Item2, t => t.Item1);
.ToDictionary(t => t.Item2, t => (t.Item1, t.Size));
_archive._archive.Extract(null, 0xFFFFFFFF, 0, this);
@ -101,27 +103,52 @@ namespace Wabbajack.VirtualFileSystem
{
private GatheringExtractor<T> _extractor;
private uint _index;
private bool _written;
private ulong _totalSize;
private MemoryStream _tmpStream;
public GatheringExtractorStream(GatheringExtractor<T> extractor, uint index)
{
_extractor = extractor;
_index = index;
_written = false;
_totalSize = extractor._indexes[index].Item2;
_tmpStream = new MemoryStream();
}
public int Write(IntPtr data, uint size, IntPtr processedSize)
{
unsafe
{
var result = _extractor._mapFn(_extractor._indexes[_index], new UnmanagedStreamFactory((byte*)data, size)).AsTask().Result;
var ums = new UnmanagedMemoryStream((byte*)data, size);
ums.CopyTo(_tmpStream);
if ((ulong)_tmpStream.Length >= _totalSize)
{
_tmpStream.Position = 0;
var result = _extractor._mapFn(_extractor._indexes[_index].Item1, new MemoryStreamFactory(_tmpStream)).AsTask().Result;
_extractor._results[_extractor._indexes[_index]] = result;
_extractor._results[_extractor._indexes[_index].Item1] = result;
}
if (processedSize != IntPtr.Zero)
{
Marshal.WriteInt32(processedSize, (int) size);
}
}
return 0;
if (_written) throw new Exception("TODO");
unsafe
{
_written = true;
return 0;
}
}