2019-10-16 21:36:14 +00:00
|
|
|
|
using System;
|
2019-07-23 04:17:52 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2019-08-26 23:02:49 +00:00
|
|
|
|
using System.Windows;
|
2019-07-23 04:17:52 +00:00
|
|
|
|
using Wabbajack.Common;
|
2019-10-16 03:10:34 +00:00
|
|
|
|
using Wabbajack.Lib.Downloaders;
|
|
|
|
|
using Wabbajack.Lib.NexusApi;
|
|
|
|
|
using Wabbajack.Lib.Validation;
|
2019-09-18 21:33:23 +00:00
|
|
|
|
using Directory = Alphaleonis.Win32.Filesystem.Directory;
|
|
|
|
|
using File = Alphaleonis.Win32.Filesystem.File;
|
|
|
|
|
using Path = Alphaleonis.Win32.Filesystem.Path;
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
2019-10-16 03:10:34 +00:00
|
|
|
|
namespace Wabbajack.Lib
|
2019-07-23 04:17:52 +00:00
|
|
|
|
{
|
2019-11-18 00:17:06 +00:00
|
|
|
|
public class MO2Installer : AInstaller
|
2019-07-23 04:17:52 +00:00
|
|
|
|
{
|
2019-11-18 05:21:24 +00:00
|
|
|
|
public bool WarnOnOverwrite { get; set; } = true;
|
|
|
|
|
|
2019-11-21 15:51:57 +00:00
|
|
|
|
public MO2Installer(string archive, ModList modList, string outputFolder)
|
2019-07-23 04:17:52 +00:00
|
|
|
|
{
|
2019-11-16 13:22:40 +00:00
|
|
|
|
ModManager = ModManager.MO2;
|
2019-10-01 22:39:25 +00:00
|
|
|
|
ModListArchive = archive;
|
2019-11-21 15:51:57 +00:00
|
|
|
|
OutputFolder = outputFolder;
|
2019-11-16 13:22:40 +00:00
|
|
|
|
DownloadFolder = Path.Combine(OutputFolder, "downloads");
|
2019-11-21 15:51:57 +00:00
|
|
|
|
ModList = modList;
|
2019-07-23 04:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-24 04:20:24 +00:00
|
|
|
|
public string GameFolder { get; set; }
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
2019-11-17 23:48:32 +00:00
|
|
|
|
protected override bool _Begin()
|
2019-07-23 04:17:52 +00:00
|
|
|
|
{
|
2019-11-24 23:03:36 +00:00
|
|
|
|
ConfigureProcessor(17, RecommendQueueSize());
|
2019-11-02 23:20:41 +00:00
|
|
|
|
var game = GameRegistry.Games[ModList.GameType];
|
|
|
|
|
|
|
|
|
|
if (GameFolder == null)
|
|
|
|
|
GameFolder = game.GameLocation;
|
|
|
|
|
|
|
|
|
|
if (GameFolder == null)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(
|
|
|
|
|
$"In order to do a proper install Wabbajack needs to know where your {game.MO2Name} folder resides. We tried looking the" +
|
|
|
|
|
"game location up in the windows registry but were unable to find it, please make sure you launch the game once before running this installer. ",
|
|
|
|
|
"Could not find game location", MessageBoxButton.OK);
|
|
|
|
|
Utils.Log("Exiting because we couldn't find the game folder.");
|
2019-11-17 23:48:32 +00:00
|
|
|
|
return false;
|
2019-11-02 23:20:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 23:03:36 +00:00
|
|
|
|
UpdateTracker.NextStep("Validating Game ESMs");
|
2019-11-02 21:08:37 +00:00
|
|
|
|
ValidateGameESMs();
|
2019-11-24 23:03:36 +00:00
|
|
|
|
|
|
|
|
|
UpdateTracker.NextStep("Validating Modlist");
|
2019-09-29 22:21:18 +00:00
|
|
|
|
ValidateModlist.RunValidation(ModList);
|
|
|
|
|
|
2019-11-16 13:22:40 +00:00
|
|
|
|
Directory.CreateDirectory(OutputFolder);
|
2019-07-23 04:17:52 +00:00
|
|
|
|
Directory.CreateDirectory(DownloadFolder);
|
|
|
|
|
|
2019-11-18 05:21:24 +00:00
|
|
|
|
if (Directory.Exists(Path.Combine(OutputFolder, "mods")) && WarnOnOverwrite)
|
2019-09-04 21:19:37 +00:00
|
|
|
|
{
|
2019-09-14 04:35:42 +00:00
|
|
|
|
if (MessageBox.Show(
|
2019-09-24 15:26:44 +00:00
|
|
|
|
"There already appears to be a Mod Organizer 2 install in this folder, are you sure you wish to continue" +
|
2019-09-14 04:35:42 +00:00
|
|
|
|
" with installation? If you do, you may render both your existing install and the new modlist inoperable.",
|
|
|
|
|
"Existing MO2 installation in install folder",
|
|
|
|
|
MessageBoxButton.YesNo,
|
|
|
|
|
MessageBoxImage.Exclamation) == MessageBoxResult.No)
|
2019-09-18 21:33:23 +00:00
|
|
|
|
{
|
2019-09-04 21:19:37 +00:00
|
|
|
|
Utils.Log("Existing installation at the request of the user, existing mods folder found.");
|
2019-11-17 23:48:32 +00:00
|
|
|
|
return false;
|
2019-09-18 21:33:23 +00:00
|
|
|
|
}
|
2019-09-04 21:19:37 +00:00
|
|
|
|
}
|
2019-09-14 04:35:42 +00:00
|
|
|
|
|
2019-11-24 23:03:36 +00:00
|
|
|
|
UpdateTracker.NextStep("Optimizing Modlist");
|
2019-11-18 05:21:24 +00:00
|
|
|
|
OptimizeModlist();
|
2019-08-24 23:20:54 +00:00
|
|
|
|
|
2019-11-24 23:03:36 +00:00
|
|
|
|
UpdateTracker.NextStep("Hashing Archives");
|
2019-07-23 04:17:52 +00:00
|
|
|
|
HashArchives();
|
2019-11-24 23:03:36 +00:00
|
|
|
|
|
|
|
|
|
UpdateTracker.NextStep("Downloading Missing Archives");
|
2019-07-23 04:17:52 +00:00
|
|
|
|
DownloadArchives();
|
2019-11-24 23:03:36 +00:00
|
|
|
|
|
|
|
|
|
UpdateTracker.NextStep("Hashing Remaining Archives");
|
2019-07-23 04:17:52 +00:00
|
|
|
|
HashArchives();
|
|
|
|
|
|
|
|
|
|
var missing = ModList.Archives.Where(a => !HashedArchives.ContainsKey(a.Hash)).ToList();
|
|
|
|
|
if (missing.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (var a in missing)
|
2019-09-26 22:32:15 +00:00
|
|
|
|
Info($"Unable to download {a.Name}");
|
2019-08-07 23:06:38 +00:00
|
|
|
|
if (IgnoreMissingFiles)
|
|
|
|
|
Info("Missing some archives, but continuing anyways at the request of the user");
|
|
|
|
|
else
|
|
|
|
|
Error("Cannot continue, was unable to download one or more archives");
|
2019-07-23 04:17:52 +00:00
|
|
|
|
}
|
2019-08-20 04:57:08 +00:00
|
|
|
|
|
2019-11-24 23:03:36 +00:00
|
|
|
|
UpdateTracker.NextStep("Priming VFS");
|
2019-11-16 00:01:37 +00:00
|
|
|
|
PrimeVFS();
|
2019-08-20 04:57:08 +00:00
|
|
|
|
|
2019-11-24 23:03:36 +00:00
|
|
|
|
UpdateTracker.NextStep("Building Folder Structure");
|
2019-07-23 04:17:52 +00:00
|
|
|
|
BuildFolderStructure();
|
2019-11-24 23:03:36 +00:00
|
|
|
|
|
|
|
|
|
UpdateTracker.NextStep("Installing Archives");
|
2019-07-23 04:17:52 +00:00
|
|
|
|
InstallArchives();
|
2019-11-24 23:03:36 +00:00
|
|
|
|
|
|
|
|
|
UpdateTracker.NextStep("Installing Included files");
|
2019-07-23 04:27:26 +00:00
|
|
|
|
InstallIncludedFiles();
|
2019-11-24 23:03:36 +00:00
|
|
|
|
|
|
|
|
|
UpdateTracker.NextStep("Installing Archive Metas");
|
2019-11-16 13:22:40 +00:00
|
|
|
|
InstallIncludedDownloadMetas();
|
2019-11-24 23:03:36 +00:00
|
|
|
|
|
|
|
|
|
UpdateTracker.NextStep("Building BSAs");
|
2019-07-28 23:04:23 +00:00
|
|
|
|
BuildBSAs();
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
2019-11-24 23:03:36 +00:00
|
|
|
|
UpdateTracker.NextStep("Generating Merges");
|
2019-11-02 15:38:03 +00:00
|
|
|
|
zEditIntegration.GenerateMerges(this);
|
|
|
|
|
|
2019-11-24 23:03:36 +00:00
|
|
|
|
UpdateTracker.NextStep("Installation complete! You may exit the program.");
|
2019-11-17 23:48:32 +00:00
|
|
|
|
return true;
|
2019-08-26 23:02:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-18 05:21:24 +00:00
|
|
|
|
|
2019-11-16 13:22:40 +00:00
|
|
|
|
private void InstallIncludedDownloadMetas()
|
2019-11-04 04:36:25 +00:00
|
|
|
|
{
|
|
|
|
|
ModList.Directives
|
|
|
|
|
.OfType<ArchiveMeta>()
|
2019-11-17 04:16:42 +00:00
|
|
|
|
.PMap(Queue, directive =>
|
2019-11-04 04:36:25 +00:00
|
|
|
|
{
|
|
|
|
|
Status($"Writing included .meta file {directive.To}");
|
2019-11-21 15:51:57 +00:00
|
|
|
|
var outPath = Path.Combine(DownloadFolder, directive.To);
|
|
|
|
|
if (File.Exists(outPath)) File.Delete(outPath);
|
|
|
|
|
File.WriteAllBytes(outPath, LoadBytesFromPath(directive.SourceDataID));
|
2019-11-04 04:36:25 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-02 21:08:37 +00:00
|
|
|
|
private void ValidateGameESMs()
|
|
|
|
|
{
|
|
|
|
|
foreach (var esm in ModList.Directives.OfType<CleanedESM>().ToList())
|
|
|
|
|
{
|
|
|
|
|
var filename = Path.GetFileName(esm.To);
|
2019-11-21 15:51:57 +00:00
|
|
|
|
var gameFile = Path.Combine(GameFolder, "Data", filename);
|
2019-11-02 21:08:37 +00:00
|
|
|
|
Utils.Log($"Validating {filename}");
|
2019-11-21 15:51:57 +00:00
|
|
|
|
var hash = gameFile.FileHash();
|
2019-11-02 21:08:37 +00:00
|
|
|
|
if (hash != esm.SourceESMHash)
|
|
|
|
|
{
|
|
|
|
|
Utils.Error("Game ESM hash doesn't match, is the ESM already cleaned? Please verify your local game files.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 23:02:49 +00:00
|
|
|
|
private void AskToEndorse()
|
|
|
|
|
{
|
2019-08-27 00:19:23 +00:00
|
|
|
|
var mods = ModList.Archives
|
2019-10-12 22:15:20 +00:00
|
|
|
|
.Select(m => m.State)
|
|
|
|
|
.OfType<NexusDownloader.State>()
|
2019-08-26 23:02:49 +00:00
|
|
|
|
.GroupBy(f => (f.GameName, f.ModID))
|
|
|
|
|
.Select(mod => mod.First())
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
var result = MessageBox.Show(
|
2019-08-27 00:19:23 +00:00
|
|
|
|
$"Installation has completed, but you have installed {mods.Length} from the Nexus, would you like to" +
|
2019-08-26 23:02:49 +00:00
|
|
|
|
" endorse these mods to show support to the authors? It will only take a few moments.", "Endorse Mods?",
|
|
|
|
|
MessageBoxButton.YesNo, MessageBoxImage.Question);
|
|
|
|
|
|
|
|
|
|
if (result != MessageBoxResult.Yes) return;
|
|
|
|
|
|
|
|
|
|
// Shuffle mods so that if we hit a API limit we don't always miss the same mods
|
|
|
|
|
var r = new Random();
|
|
|
|
|
for (var i = 0; i < mods.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var a = r.Next(mods.Length);
|
|
|
|
|
var b = r.Next(mods.Length);
|
|
|
|
|
var tmp = mods[a];
|
|
|
|
|
mods[a] = mods[b];
|
|
|
|
|
mods[b] = tmp;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-17 04:16:42 +00:00
|
|
|
|
mods.PMap(Queue, mod =>
|
2019-08-27 00:19:23 +00:00
|
|
|
|
{
|
2019-09-29 00:18:42 +00:00
|
|
|
|
var er = new NexusApiClient().EndorseMod(mod);
|
2019-08-27 00:19:23 +00:00
|
|
|
|
Utils.Log($"Endorsed {mod.GameName} - {mod.ModID} - Result: {er.message}");
|
|
|
|
|
});
|
2019-08-26 23:02:49 +00:00
|
|
|
|
Info("Done! You may now exit the application!");
|
2019-07-23 04:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 23:04:23 +00:00
|
|
|
|
private void BuildBSAs()
|
|
|
|
|
{
|
|
|
|
|
var bsas = ModList.Directives.OfType<CreateBSA>().ToList();
|
2019-07-30 03:32:52 +00:00
|
|
|
|
Info($"Building {bsas.Count} bsa files");
|
2019-07-28 23:04:23 +00:00
|
|
|
|
|
|
|
|
|
bsas.Do(bsa =>
|
|
|
|
|
{
|
|
|
|
|
Status($"Building {bsa.To}");
|
2019-11-21 15:51:57 +00:00
|
|
|
|
var sourceDir = Path.Combine(OutputFolder, Consts.BSACreationDir, bsa.TempID);
|
2019-07-28 23:04:23 +00:00
|
|
|
|
|
2019-10-11 23:31:36 +00:00
|
|
|
|
using (var a = bsa.State.MakeBuilder())
|
|
|
|
|
{
|
2019-11-17 04:16:42 +00:00
|
|
|
|
bsa.FileStates.PMap(Queue, state =>
|
2019-07-28 23:04:23 +00:00
|
|
|
|
{
|
2019-10-11 23:31:36 +00:00
|
|
|
|
Status($"Adding {state.Path} to BSA");
|
2019-11-21 15:51:57 +00:00
|
|
|
|
using (var fs = File.OpenRead(Path.Combine(sourceDir, state.Path)))
|
2019-09-14 04:35:42 +00:00
|
|
|
|
{
|
2019-10-11 23:31:36 +00:00
|
|
|
|
a.AddFile(state, fs);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Info($"Writing {bsa.To}");
|
2019-11-16 13:22:40 +00:00
|
|
|
|
a.Build(Path.Combine(OutputFolder, bsa.To));
|
2019-10-11 23:31:36 +00:00
|
|
|
|
}
|
2019-07-28 23:04:23 +00:00
|
|
|
|
});
|
|
|
|
|
|
2019-09-18 21:33:23 +00:00
|
|
|
|
|
2019-11-21 15:51:57 +00:00
|
|
|
|
var bsaDir = Path.Combine(OutputFolder, Consts.BSACreationDir);
|
|
|
|
|
if (Directory.Exists(bsaDir))
|
2019-08-20 22:37:55 +00:00
|
|
|
|
{
|
|
|
|
|
Info($"Removing temp folder {Consts.BSACreationDir}");
|
2019-11-23 17:37:24 +00:00
|
|
|
|
Utils.DeleteDirectory(bsaDir);
|
2019-08-20 22:37:55 +00:00
|
|
|
|
}
|
2019-07-28 23:04:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-23 04:27:26 +00:00
|
|
|
|
private void InstallIncludedFiles()
|
|
|
|
|
{
|
|
|
|
|
Info("Writing inline files");
|
|
|
|
|
ModList.Directives
|
2019-09-14 04:35:42 +00:00
|
|
|
|
.OfType<InlineFile>()
|
2019-11-17 04:16:42 +00:00
|
|
|
|
.PMap(Queue, directive =>
|
2019-09-14 04:35:42 +00:00
|
|
|
|
{
|
2019-09-26 22:32:15 +00:00
|
|
|
|
Status($"Writing included file {directive.To}");
|
2019-11-21 15:51:57 +00:00
|
|
|
|
var outPath = Path.Combine(OutputFolder, directive.To);
|
|
|
|
|
if (File.Exists(outPath)) File.Delete(outPath);
|
2019-09-14 04:35:42 +00:00
|
|
|
|
if (directive is RemappedInlineFile)
|
2019-10-07 17:33:34 +00:00
|
|
|
|
WriteRemappedFile((RemappedInlineFile)directive);
|
2019-09-14 04:35:42 +00:00
|
|
|
|
else if (directive is CleanedESM)
|
2019-10-07 17:33:34 +00:00
|
|
|
|
GenerateCleanedESM((CleanedESM)directive);
|
2019-09-14 04:35:42 +00:00
|
|
|
|
else
|
2019-11-21 15:51:57 +00:00
|
|
|
|
File.WriteAllBytes(outPath, LoadBytesFromPath(directive.SourceDataID));
|
2019-09-14 04:35:42 +00:00
|
|
|
|
});
|
2019-07-23 04:27:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 03:46:32 +00:00
|
|
|
|
private void GenerateCleanedESM(CleanedESM directive)
|
|
|
|
|
{
|
|
|
|
|
var filename = Path.GetFileName(directive.To);
|
2019-11-21 15:51:57 +00:00
|
|
|
|
var gameFile = Path.Combine(GameFolder, "Data", filename);
|
2019-08-25 03:46:32 +00:00
|
|
|
|
Info($"Generating cleaned ESM for {filename}");
|
2019-11-21 15:51:57 +00:00
|
|
|
|
if (!File.Exists(gameFile)) throw new InvalidDataException($"Missing {filename} at {gameFile}");
|
2019-08-25 03:46:32 +00:00
|
|
|
|
Status($"Hashing game version of {filename}");
|
2019-11-21 15:51:57 +00:00
|
|
|
|
var sha = gameFile.FileHash();
|
2019-08-25 03:46:32 +00:00
|
|
|
|
if (sha != directive.SourceESMHash)
|
2019-09-14 04:35:42 +00:00
|
|
|
|
throw new InvalidDataException(
|
|
|
|
|
$"Cannot patch {filename} from the game folder hashes don't match have you already cleaned the file?");
|
2019-08-25 03:46:32 +00:00
|
|
|
|
|
2019-11-21 15:51:57 +00:00
|
|
|
|
var patchData = LoadBytesFromPath(directive.SourceDataID);
|
|
|
|
|
var toFile = Path.Combine(OutputFolder, directive.To);
|
2019-08-25 03:46:32 +00:00
|
|
|
|
Status($"Patching {filename}");
|
2019-11-21 15:51:57 +00:00
|
|
|
|
using (var output = File.OpenWrite(toFile))
|
|
|
|
|
using (var input = File.OpenRead(gameFile))
|
2019-09-14 04:35:42 +00:00
|
|
|
|
{
|
2019-11-21 15:51:57 +00:00
|
|
|
|
BSDiff.Apply(input, () => new MemoryStream(patchData), output);
|
2019-08-25 03:46:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-24 23:20:54 +00:00
|
|
|
|
private void WriteRemappedFile(RemappedInlineFile directive)
|
|
|
|
|
{
|
2019-10-01 22:39:25 +00:00
|
|
|
|
var data = Encoding.UTF8.GetString(LoadBytesFromPath(directive.SourceDataID));
|
2019-08-24 23:20:54 +00:00
|
|
|
|
|
|
|
|
|
data = data.Replace(Consts.GAME_PATH_MAGIC_BACK, GameFolder);
|
|
|
|
|
data = data.Replace(Consts.GAME_PATH_MAGIC_DOUBLE_BACK, GameFolder.Replace("\\", "\\\\"));
|
|
|
|
|
data = data.Replace(Consts.GAME_PATH_MAGIC_FORWARD, GameFolder.Replace("\\", "/"));
|
|
|
|
|
|
2019-11-16 13:22:40 +00:00
|
|
|
|
data = data.Replace(Consts.MO2_PATH_MAGIC_BACK, OutputFolder);
|
|
|
|
|
data = data.Replace(Consts.MO2_PATH_MAGIC_DOUBLE_BACK, OutputFolder.Replace("\\", "\\\\"));
|
|
|
|
|
data = data.Replace(Consts.MO2_PATH_MAGIC_FORWARD, OutputFolder.Replace("\\", "/"));
|
2019-08-24 23:20:54 +00:00
|
|
|
|
|
2019-09-03 22:12:39 +00:00
|
|
|
|
data = data.Replace(Consts.DOWNLOAD_PATH_MAGIC_BACK, DownloadFolder);
|
|
|
|
|
data = data.Replace(Consts.DOWNLOAD_PATH_MAGIC_DOUBLE_BACK, DownloadFolder.Replace("\\", "\\\\"));
|
|
|
|
|
data = data.Replace(Consts.DOWNLOAD_PATH_MAGIC_FORWARD, DownloadFolder.Replace("\\", "/"));
|
|
|
|
|
|
2019-11-16 13:22:40 +00:00
|
|
|
|
File.WriteAllText(Path.Combine(OutputFolder, directive.To), data);
|
2019-07-23 04:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-24 15:26:44 +00:00
|
|
|
|
}
|