wabbajack/Wabbajack.Test/EndToEndTests.cs

190 lines
6.6 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 Microsoft.VisualStudio.TestTools.UnitTesting;
using Wabbajack.Common;
using Wabbajack.Lib;
using Wabbajack.Lib.Downloaders;
using Wabbajack.Lib.NexusApi;
2020-01-07 13:50:11 +00:00
using Wabbajack.Util;
2019-10-28 03:15:48 +00:00
namespace Wabbajack.Test
{
[TestClass]
public class EndToEndTests
{
private const string DOWNLOAD_FOLDER = "downloads";
private TestUtils utils = new TestUtils();
public TestContext TestContext { get; set; }
2019-11-17 04:16:42 +00:00
public WorkQueue Queue { get; set; }
2019-10-28 03:15:48 +00:00
[TestInitialize]
public void TestInitialize()
{
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
Utils.LogMessages.Subscribe(f => TestContext.WriteLine($"{DateTime.Now} - {f}"));
2019-10-28 03:15:48 +00:00
if (!Directory.Exists(DOWNLOAD_FOLDER))
Directory.CreateDirectory(DOWNLOAD_FOLDER);
2019-11-17 04:16:42 +00:00
}
2019-10-28 03:15:48 +00:00
2019-11-17 04:16:42 +00:00
[TestCleanup]
public void Cleanup()
{
Queue.Dispose();
2019-10-28 03:15:48 +00:00
}
[TestMethod]
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",
"Mod.Organizer.2.2.1.7z",
utils.MO2Folder);
File.WriteAllLines(Path.Combine(utils.DownloadsFolder, "Mod.Organizer.2.2.1.7z.meta"),
new List<string>
{
"[General]",
"directURL=https://github.com/ModOrganizer2/modorganizer/releases/download/v2.2.1/Mod.Organizer.2.2.1.7z"
});
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.
File.Delete(modfiles[3].Download);
2019-10-28 03:15:48 +00:00
utils.Configure();
2020-02-05 05:17:12 +00:00
File.WriteAllLines(Path.Combine(modfiles[3].ModFolder, "meta.ini"), new []
{
"[General]",
$"matchAll= {Path.GetFileName(modfiles[2].Download)}"
});
2019-10-28 03:15:48 +00:00
2019-12-13 13:02:58 +00:00
var modlist = await CompileAndInstall(profile);
2019-10-28 03:15:48 +00:00
utils.VerifyAllFiles();
2019-11-04 04:44:44 +00:00
var loot_folder = Path.Combine(utils.InstallFolder, "LOOT Config Files");
if (Directory.Exists(loot_folder))
Utils.DeleteDirectory(loot_folder);
var compiler = new MO2Compiler(
mo2Folder: utils.InstallFolder,
mo2Profile: profile,
outputFile: profile + Consts.ModListExtension);
compiler.MO2DownloadsFolder = Path.Combine(utils.DownloadsFolder);
2019-12-07 02:54:27 +00:00
Assert.IsTrue(await compiler.Begin());
2019-10-28 03:15:48 +00:00
}
private async Task DownloadAndInstall(string url, string filename, string mod_name = null)
2019-10-28 03:15:48 +00:00
{
var src = Path.Combine(DOWNLOAD_FOLDER, filename);
if (!File.Exists(src))
{
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
}
if (!Directory.Exists(utils.DownloadsFolder))
{
Directory.CreateDirectory(utils.DownloadsFolder);
}
2020-02-05 05:17:12 +00:00
await Utils.CopyFileAsync(src, Path.Combine(utils.DownloadsFolder, filename));
2019-10-28 03:15:48 +00:00
await FileExtractor.ExtractAll(Queue, src,
2019-11-17 04:16:42 +00:00
mod_name == null ? utils.MO2Folder : Path.Combine(utils.ModsFolder, mod_name));
2019-10-28 03:15:48 +00:00
}
2020-02-05 05:17:12 +00:00
private async Task<(string Download, string ModFolder)> DownloadAndInstall(Game game, int modid, string mod_name)
{
utils.AddMod(mod_name);
2019-12-07 03:50:50 +00:00
var client = await NexusApiClient.Get();
2019-12-07 02:45:13 +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));
var src = Path.Combine(DOWNLOAD_FOLDER, file.file_name);
var ini = string.Join("\n",
new List<string>
{
"[General]",
$"gameName={game.MetaData().MO2ArchiveName}",
$"modID={modid}",
$"fileID={file.file_id}"
});
2019-11-11 06:15:52 +00:00
if (!File.Exists(src))
{
2019-12-13 21:48:40 +00:00
var state = (AbstractDownloadState)await DownloadDispatcher.ResolveArchive(ini.LoadIniString());
await state.Download(src);
}
if (!Directory.Exists(utils.DownloadsFolder))
{
Directory.CreateDirectory(utils.DownloadsFolder);
}
var dest = Path.Combine(utils.DownloadsFolder, file.file_name);
2020-02-05 05:17:12 +00:00
await Utils.CopyFileAsync(src, dest);
2020-02-05 05:17:12 +00:00
var modFolder = Path.Combine(utils.ModsFolder, mod_name);
await FileExtractor.ExtractAll(Queue, src, modFolder);
2020-01-20 01:45:05 +00:00
File.WriteAllText(dest + Consts.MetaFileExtension, 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,
parameters: SystemParametersConstructor.Create());
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,
outputFile: profile + Consts.ModListExtension);
2019-12-07 02:54:27 +00:00
Assert.IsTrue(await compiler.Begin());
2019-10-28 03:15:48 +00:00
return compiler;
}
}
}