wabbajack/Wabbajack.Test/EndToEndTests.cs

179 lines
6.5 KiB
C#
Raw Normal View History

2019-10-28 03:15:48 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2019-10-28 03:15:48 +00:00
using Alphaleonis.Win32.Filesystem;
using Wabbajack.Common;
using Wabbajack.Lib;
using Wabbajack.Lib.Downloaders;
using Wabbajack.Lib.NexusApi;
using Wabbajack.VirtualFileSystem;
2020-03-28 18:22:53 +00:00
using Xunit;
using Xunit.Abstractions;
2019-10-28 03:15:48 +00:00
namespace Wabbajack.Test
{
2020-03-29 20:42:45 +00:00
public class EndToEndTests : XunitContextBase, IDisposable
2019-10-28 03:15:48 +00:00
{
2020-03-28 18:22:53 +00:00
private AbsolutePath _downloadFolder = "downloads".RelativeTo(AbsolutePath.EntryPoint);
2019-10-28 03:15:48 +00:00
private TestUtils utils = new TestUtils();
2020-03-29 20:42:45 +00:00
private IDisposable _unsub;
2019-10-28 03:15:48 +00:00
2019-11-17 04:16:42 +00:00
public WorkQueue Queue { get; set; }
2020-03-29 20:42:45 +00:00
public EndToEndTests(ITestOutputHelper helper) : base(helper)
2019-10-28 03:15:48 +00:00
{
2019-11-17 04:16:42 +00:00
Queue = new WorkQueue();
2019-10-28 03:15:48 +00:00
Consts.TestMode = true;
utils = new TestUtils();
2019-11-16 22:02:36 +00:00
utils.Game = Game.SkyrimSpecialEdition;
2019-10-28 03:15:48 +00:00
2020-03-29 20:42:45 +00:00
_unsub = Utils.LogMessages.Subscribe(f => XunitContext.WriteLine($"{DateTime.Now} - {f}"));
2020-03-28 18:22:53 +00:00
_downloadFolder.CreateDirectory();
2019-11-17 04:16:42 +00:00
}
2019-10-28 03:15:48 +00:00
2020-03-29 20:42:45 +00:00
public override void Dispose()
2019-11-17 04:16:42 +00:00
{
Queue.Dispose();
2020-03-29 20:42:45 +00:00
_unsub.Dispose();
base.Dispose();
2019-10-28 03:15:48 +00:00
}
2020-03-28 18:22:53 +00:00
[Fact]
public async Task CreateModlist()
2019-10-28 03:15:48 +00:00
{
var profile = utils.AddProfile("Default");
var mod = utils.AddMod();
await DownloadAndInstall(
2019-10-28 03:15:48 +00:00
"https://github.com/ModOrganizer2/modorganizer/releases/download/v2.2.1/Mod.Organizer.2.2.1.7z",
2020-03-28 18:22:53 +00:00
"Mod.Organizer.2.2.1.7z");
await utils.DownloadsFolder.Combine("Mod.Organizer.2.2.1.7z.meta").WriteAllLinesAsync(
2019-10-28 03:15:48 +00:00
"[General]",
"directURL=https://github.com/ModOrganizer2/modorganizer/releases/download/v2.2.1/Mod.Organizer.2.2.1.7z"
2020-03-28 18:22:53 +00:00
);
2019-10-28 03:15:48 +00:00
2020-02-05 05:17:12 +00:00
var modfiles = await Task.WhenAll(
DownloadAndInstall(Game.SkyrimSpecialEdition, 12604, "SkyUI"),
DownloadAndInstall(Game.Fallout4, 11925, "Anti-Tank Rifle"),
DownloadAndInstall(Game.SkyrimSpecialEdition, 4783, "Frost Armor UNP"),
DownloadAndInstall(Game.SkyrimSpecialEdition, 32359, "Frost Armor HDT"));
// We're going to fully patch this mod from another source.
2020-03-28 18:22:53 +00:00
modfiles[3].Download.Delete();
2020-03-28 18:22:53 +00:00
await utils.Configure();
2020-02-05 05:17:12 +00:00
2020-03-28 18:22:53 +00:00
await modfiles[3].ModFolder.Combine("meta.ini").WriteAllLinesAsync(
2020-02-05 05:17:12 +00:00
"[General]",
2020-03-28 18:22:53 +00:00
$"matchAll= {modfiles[2].Download.FileName}"
);
2020-03-28 18:22:53 +00:00
await utils.MO2Folder.Combine("startup.bat").WriteAllLinesAsync(
"ModOrganizer2.exe SKSE"
2020-03-28 18:22:53 +00:00
);
2020-02-05 05:17:12 +00:00
2019-10-28 03:15:48 +00:00
2020-03-28 18:22:53 +00:00
await CompileAndInstall(profile);
utils.VerifyAllFiles();
2019-11-04 04:44:44 +00:00
2020-03-28 18:22:53 +00:00
await utils.InstallFolder.Combine(Consts.LOOTFolderFilesDir).DeleteDirectory();
var compiler = new MO2Compiler(
mo2Folder: utils.InstallFolder,
mo2Profile: profile,
2020-03-28 18:22:53 +00:00
outputFile: profile.RelativeTo(AbsolutePath.EntryPoint).WithExtension(Consts.ModListExtension));
compiler.MO2DownloadsFolder = utils.DownloadsFolder;
Assert.True(await compiler.Begin());
2019-10-28 03:15:48 +00:00
}
2020-03-28 18:22:53 +00:00
private async Task DownloadAndInstall(string url, string filename, string modName = null)
2019-10-28 03:15:48 +00:00
{
2020-03-28 18:22:53 +00:00
var src = _downloadFolder.Combine(filename);
if (!src.Exists)
2019-10-28 03:15:48 +00:00
{
var state = DownloadDispatcher.ResolveArchive(url);
2019-12-13 13:02:58 +00:00
await state.Download(new Archive { Name = "Unknown"}, src);
2019-10-28 03:15:48 +00:00
}
2020-03-28 18:22:53 +00:00
utils.DownloadsFolder.CreateDirectory();
2019-10-28 03:15:48 +00:00
2020-03-28 18:22:53 +00:00
await src.CopyToAsync(utils.DownloadsFolder.Combine(filename));
2019-10-28 03:15:48 +00:00
await FileExtractor.ExtractAll(Queue, src,
2020-03-28 18:22:53 +00:00
modName == null ? utils.MO2Folder : utils.ModsFolder.Combine(modName));
2019-10-28 03:15:48 +00:00
}
2020-03-28 18:22:53 +00:00
private async Task<(AbsolutePath Download, AbsolutePath ModFolder)> DownloadAndInstall(Game game, int modId, string modName)
{
2020-03-28 18:22:53 +00:00
utils.AddMod(modName);
2019-12-07 03:50:50 +00:00
var client = await NexusApiClient.Get();
2020-03-28 18:22:53 +00:00
var resp = await client.GetModFiles(game, modId);
2020-02-05 05:17:12 +00:00
var file = resp.files.FirstOrDefault(f => f.is_primary) ?? resp.files.FirstOrDefault(f => !string.IsNullOrEmpty(f.category_name));
2020-03-28 18:22:53 +00:00
var src = _downloadFolder.Combine(file.file_name);
var ini = string.Join("\n",
new List<string>
{
"[General]",
$"gameName={game.MetaData().MO2ArchiveName}",
2020-03-28 18:22:53 +00:00
$"modID={modId}",
$"fileID={file.file_id}"
});
2020-03-28 18:22:53 +00:00
if (!src.Exists)
{
2019-12-13 21:48:40 +00:00
var state = (AbstractDownloadState)await DownloadDispatcher.ResolveArchive(ini.LoadIniString());
await state.Download(src);
}
2020-03-28 18:22:53 +00:00
utils.DownloadsFolder.CreateDirectory();
2020-03-28 18:22:53 +00:00
var dest = utils.DownloadsFolder.Combine(file.file_name);
await src.CopyToAsync(dest);
2020-03-28 18:22:53 +00:00
var modFolder = utils.ModsFolder.Combine(modName);
2020-02-05 05:17:12 +00:00
await FileExtractor.ExtractAll(Queue, src, modFolder);
2020-03-28 18:22:53 +00:00
await dest.WithExtension(Consts.MetaFileExtension).WriteAllTextAsync(ini);
2020-02-05 05:17:12 +00:00
return (dest, modFolder);
}
2019-10-28 03:15:48 +00:00
2019-12-07 02:54:27 +00:00
private async Task<ModList> CompileAndInstall(string profile)
2019-10-28 03:15:48 +00:00
{
2019-12-07 02:54:27 +00:00
var compiler = await ConfigureAndRunCompiler(profile);
2019-12-13 13:02:58 +00:00
await Install(compiler);
2019-10-28 03:15:48 +00:00
return compiler.ModList;
}
2019-12-13 13:02:58 +00:00
private async Task Install(MO2Compiler compiler)
2019-10-28 03:15:48 +00:00
{
2019-11-21 15:05:10 +00:00
var modlist = AInstaller.LoadFromFile(compiler.ModListOutputFile);
var installer = new MO2Installer(
archive: compiler.ModListOutputFile,
modList: modlist,
outputFolder: utils.InstallFolder,
2020-01-07 13:50:11 +00:00
downloadFolder: utils.DownloadsFolder,
2020-03-28 18:22:53 +00:00
parameters: ACompilerTest.CreateDummySystemParameters());
2019-10-28 03:15:48 +00:00
installer.GameFolder = utils.GameFolder;
2019-12-13 13:02:58 +00:00
await installer.Begin();
2019-10-28 03:15:48 +00:00
}
2019-12-07 02:54:27 +00:00
private async Task<MO2Compiler> ConfigureAndRunCompiler(string profile)
2019-10-28 03:15:48 +00:00
{
var compiler = new MO2Compiler(
mo2Folder: utils.MO2Folder,
mo2Profile: profile,
2020-03-28 18:22:53 +00:00
outputFile: profile.RelativeTo(AbsolutePath.EntryPoint).WithExtension(Consts.ModListExtension));
Assert.True(await compiler.Begin());
2019-10-28 03:15:48 +00:00
return compiler;
}
}
}