wabbajack/Compression.BSA.Test/BSATests.cs

147 lines
5.5 KiB
C#
Raw Normal View History

2019-11-11 06:15:52 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
2019-11-11 06:15:52 +00:00
using Newtonsoft.Json;
using Wabbajack.Common;
using Wabbajack.Lib.Downloaders;
using Wabbajack.Lib.NexusApi;
using Wabbajack.VirtualFileSystem;
2020-03-26 13:01:42 +00:00
using Xunit;
using Xunit.Abstractions;
2019-11-11 06:15:52 +00:00
namespace Compression.BSA.Test
{
public class BSATests
{
private static AbsolutePath _stagingFolder = ((RelativePath)"NexusDownloads").RelativeToEntryPoint();
private static AbsolutePath _bsaFolder = ((RelativePath)"BSAs").RelativeToEntryPoint();
private static AbsolutePath _testDir = ((RelativePath)"BSA Test Dir").RelativeToEntryPoint();
private static AbsolutePath _tempDir = ((RelativePath)"BSA Temp Dir").RelativeToEntryPoint();
2019-11-11 06:15:52 +00:00
2020-03-26 13:01:42 +00:00
public ITestOutputHelper TestContext { get; }
2019-11-11 06:15:52 +00:00
2019-11-17 04:16:42 +00:00
private static WorkQueue Queue { get; set; }
2020-03-26 13:01:42 +00:00
public BSATests(ITestOutputHelper helper)
2019-11-11 06:15:52 +00:00
{
2020-03-26 13:01:42 +00:00
TestContext = helper;
2019-11-17 04:16:42 +00:00
Queue = new WorkQueue();
2020-03-26 13:01:42 +00:00
Utils.LogMessages.Subscribe(f => TestContext.WriteLine(f.ShortDescription));
_stagingFolder.DeleteDirectory();
_bsaFolder.DeleteDirectory();
2020-03-26 13:01:42 +00:00
_bsaFolder.CreateDirectory();
2019-11-11 06:15:52 +00:00
}
2020-03-26 13:01:42 +00:00
private static async Task<AbsolutePath> DownloadMod(Game game, int mod)
2019-11-11 06:15:52 +00:00
{
using var client = await NexusApiClient.Get();
2020-03-26 13:01:42 +00:00
var results = await client.GetModFiles(game, mod);
var file = results.files.FirstOrDefault(f => f.is_primary) ??
results.files.OrderByDescending(f => f.uploaded_timestamp).First();
var src = _stagingFolder.Combine(file.file_name);
if (src.Exists) return src;
var state = new NexusDownloader.State
{
2020-03-26 13:01:42 +00:00
ModID = mod.ToString(),
GameName = game.MetaData().NexusName,
FileID = file.file_id.ToString()
};
await state.Download(src);
return src;
2019-11-11 06:15:52 +00:00
}
2020-03-26 13:01:42 +00:00
[Theory]
[InlineData(Game.SkyrimSpecialEdition, 12604)] // SkyUI
[InlineData(Game.Skyrim, 3863)] // SkyUI
[InlineData(Game.Skyrim, 51473)] // INeed
[InlineData(Game.Fallout4, 22223)] // 10mm SMG
[InlineData(Game.Fallout4, 4472)] // True Storms
[InlineData(Game.Morrowind, 44537)]
public async Task BSACompressionRecompression(Game game, int modid)
2019-11-11 06:15:52 +00:00
{
2020-03-26 13:01:42 +00:00
var filename = await DownloadMod(game, modid);
var folder = _bsaFolder.Combine(game.ToString(), modid.ToString());
2020-03-28 04:33:26 +00:00
await folder.DeleteDirectory();
2020-03-26 13:01:42 +00:00
folder.CreateDirectory();
await FileExtractor.ExtractAll(Queue, filename, folder);
2019-11-11 06:15:52 +00:00
2020-03-26 13:01:42 +00:00
foreach (var bsa in folder.EnumerateFiles().Where(f => Consts.SupportedBSAs.Contains(f.Extension)))
2019-11-11 06:15:52 +00:00
{
2020-03-26 13:01:42 +00:00
TestContext.WriteLine($"From {bsa}");
TestContext.WriteLine("Cleaning Output Dir");
2020-03-28 04:33:26 +00:00
await _tempDir.DeleteDirectory();
2020-03-26 13:01:42 +00:00
_tempDir.CreateDirectory();
TestContext.WriteLine($"Reading {bsa}");
var tempFile = ((RelativePath)"tmp.bsa").RelativeToEntryPoint();
var size = bsa.Size;
using var a = BSADispatch.OpenRead(bsa);
await a.Files.PMap(Queue, file =>
2019-11-11 06:15:52 +00:00
{
var absName = _tempDir.Combine(file.Path);
2019-11-11 06:15:52 +00:00
ViaJson(file.State);
absName.Parent.CreateDirectory();
using (var fs = absName.Create())
2019-11-11 06:15:52 +00:00
{
2019-11-16 00:01:37 +00:00
file.CopyDataTo(fs);
2019-11-11 06:15:52 +00:00
}
2020-03-26 13:01:42 +00:00
Assert.Equal(file.Size, absName.Size);
2019-11-11 06:15:52 +00:00
});
2020-03-26 13:01:42 +00:00
TestContext.WriteLine($"Building {bsa}");
2019-11-11 06:15:52 +00:00
2020-03-09 20:38:35 +00:00
using (var w = ViaJson(a.State).MakeBuilder(size))
2019-11-11 06:15:52 +00:00
{
var streams = await a.Files.PMap(Queue, file =>
2019-11-11 06:15:52 +00:00
{
var absPath = _tempDir.Combine(file.Path);
var str = absPath.OpenRead();
w.AddFile(ViaJson(file.State), str);
return str;
2019-11-11 06:15:52 +00:00
});
2019-11-21 15:41:46 +00:00
w.Build(tempFile);
streams.Do(s => s.Dispose());
2019-11-11 06:15:52 +00:00
}
2020-03-26 13:01:42 +00:00
TestContext.WriteLine($"Verifying {bsa}");
using var b = BSADispatch.OpenRead(tempFile);
TestContext.WriteLine($"Performing A/B tests on {bsa}");
Assert.Equal(a.State.ToJSON(), b.State.ToJSON());
2019-11-11 06:15:52 +00:00
2020-03-26 13:01:42 +00:00
// Check same number of files
Assert.Equal(a.Files.Count(), b.Files.Count());
await a.Files.Zip(b.Files, (ai, bi) => (ai, bi))
.PMap(Queue, pair =>
{
Assert.Equal(pair.ai.State.ToJSON(), pair.bi.State.ToJSON());
//Console.WriteLine($" - {pair.ai.Path}");
Assert.Equal(pair.ai.Path, pair.bi.Path);
//Equal(pair.ai.Compressed, pair.bi.Compressed);
Assert.Equal(pair.ai.Size, pair.bi.Size);
Assert.Equal(GetData(pair.ai), GetData(pair.bi));
});
2019-11-11 06:15:52 +00:00
}
}
2019-11-16 00:01:37 +00:00
private static byte[] GetData(IFile pairAi)
2019-11-11 06:15:52 +00:00
{
2020-03-26 13:01:42 +00:00
using var ms = new MemoryStream();
pairAi.CopyDataTo(ms);
return ms.ToArray();
2019-11-11 06:15:52 +00:00
}
2020-03-26 13:01:42 +00:00
private static T ViaJson<T>(T i)
2019-11-11 06:15:52 +00:00
{
2020-03-26 13:01:42 +00:00
return i.ToJSON().FromJSONString<T>();
2019-11-11 06:15:52 +00:00
}
}
}