2019-08-04 22:08:03 +00:00
|
|
|
|
using CG.Web.MegaApiClient;
|
|
|
|
|
using Compression.BSA;
|
2019-08-24 23:20:54 +00:00
|
|
|
|
using Ookii.Dialogs.Wpf;
|
2019-07-23 04:17:52 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
2019-07-31 03:59:19 +00:00
|
|
|
|
using System.Reflection;
|
2019-09-12 23:44:35 +00:00
|
|
|
|
using System.Runtime.Serialization;
|
|
|
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
2019-07-23 04:17:52 +00:00
|
|
|
|
using System.Text;
|
2019-07-24 04:31:08 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2019-07-23 04:17:52 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2019-08-26 23:02:49 +00:00
|
|
|
|
using System.Windows;
|
2019-09-12 23:44:35 +00:00
|
|
|
|
using K4os.Compression.LZ4.Encoders;
|
|
|
|
|
using K4os.Compression.LZ4.Streams;
|
2019-08-20 04:57:08 +00:00
|
|
|
|
using VFS;
|
2019-07-23 04:17:52 +00:00
|
|
|
|
using Wabbajack.Common;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack
|
|
|
|
|
{
|
|
|
|
|
public class Installer
|
|
|
|
|
{
|
2019-09-03 22:12:39 +00:00
|
|
|
|
private string _downloadsFolder;
|
|
|
|
|
|
2019-08-20 04:57:08 +00:00
|
|
|
|
public VirtualFileSystem VFS
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return VirtualFileSystem.VFS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-23 04:17:52 +00:00
|
|
|
|
public Installer(ModList mod_list, string output_folder, Action<string> log_fn)
|
|
|
|
|
{
|
|
|
|
|
Outputfolder = output_folder;
|
|
|
|
|
ModList = mod_list;
|
|
|
|
|
Log_Fn = log_fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Outputfolder { get; }
|
|
|
|
|
public string DownloadFolder
|
|
|
|
|
{
|
2019-09-03 22:12:39 +00:00
|
|
|
|
get => _downloadsFolder ?? Path.Combine(Outputfolder, "downloads");
|
|
|
|
|
set => _downloadsFolder = value;
|
2019-07-23 04:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
public ModList ModList { get; }
|
|
|
|
|
public Action<string> Log_Fn { get; }
|
|
|
|
|
public Dictionary<string, string> HashedArchives { get; private set; }
|
2019-07-26 20:59:14 +00:00
|
|
|
|
|
2019-08-30 04:24:31 +00:00
|
|
|
|
public string NexusAPIKey { get; set; }
|
2019-08-07 23:06:38 +00:00
|
|
|
|
public bool IgnoreMissingFiles { get; internal set; }
|
2019-08-24 23:20:54 +00:00
|
|
|
|
public string GameFolder { get; private set; }
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
|
|
|
|
public void Info(string msg, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Length > 0)
|
|
|
|
|
msg = String.Format(msg, args);
|
|
|
|
|
Log_Fn(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Status(string msg, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Length > 0)
|
|
|
|
|
msg = String.Format(msg, args);
|
|
|
|
|
WorkQueue.Report(msg, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Status(int progress, string msg, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Length > 0)
|
|
|
|
|
msg = String.Format(msg, args);
|
|
|
|
|
WorkQueue.Report(msg, progress);
|
|
|
|
|
}
|
|
|
|
|
private void Error(string msg, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Length > 0)
|
|
|
|
|
msg = String.Format(msg, args);
|
|
|
|
|
Log_Fn(msg);
|
|
|
|
|
throw new Exception(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Install()
|
|
|
|
|
{
|
2019-08-27 00:19:23 +00:00
|
|
|
|
|
2019-07-23 04:17:52 +00:00
|
|
|
|
Directory.CreateDirectory(Outputfolder);
|
|
|
|
|
Directory.CreateDirectory(DownloadFolder);
|
|
|
|
|
|
2019-09-04 21:19:37 +00:00
|
|
|
|
if (Directory.Exists(Path.Combine(Outputfolder, "mods")))
|
|
|
|
|
{
|
|
|
|
|
if (MessageBox.Show("There already appears to be a Mod Organize 2 install in this folder, are you sure you wish to continue" +
|
|
|
|
|
" 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)
|
|
|
|
|
Utils.Log("Existing installation at the request of the user, existing mods folder found.");
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 03:09:21 +00:00
|
|
|
|
if (ModList.Directives.OfType<RemappedInlineFile>().FirstOrDefault() != null ||
|
|
|
|
|
ModList.Directives.OfType<CleanedESM>().FirstOrDefault() != null)
|
2019-08-24 23:20:54 +00:00
|
|
|
|
{
|
2019-08-27 03:09:21 +00:00
|
|
|
|
MessageBox.Show(
|
|
|
|
|
"In order to do a proper install Wabbajack needs to know where your game folder resides. This is most likely " +
|
|
|
|
|
"somewhere in one of your Steam folders. Please select this folder on the next screen." +
|
|
|
|
|
"Note: This is not the install location where Mod Organizer 2 will be installed. ",
|
|
|
|
|
"Select your Game Folder", MessageBoxButton.OK);
|
|
|
|
|
if (!LocateGameFolder())
|
|
|
|
|
{
|
|
|
|
|
Info("Stopping installation because game folder was not selected");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-08-24 23:20:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-23 04:17:52 +00:00
|
|
|
|
HashArchives();
|
|
|
|
|
DownloadArchives();
|
|
|
|
|
HashArchives();
|
|
|
|
|
|
|
|
|
|
var missing = ModList.Archives.Where(a => !HashedArchives.ContainsKey(a.Hash)).ToList();
|
|
|
|
|
if (missing.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (var a in missing)
|
|
|
|
|
Info("Unable to download {0}", 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
|
|
|
|
|
|
|
|
|
PrimeVFS();
|
|
|
|
|
|
2019-07-23 04:17:52 +00:00
|
|
|
|
BuildFolderStructure();
|
|
|
|
|
InstallArchives();
|
2019-07-23 04:27:26 +00:00
|
|
|
|
InstallIncludedFiles();
|
2019-07-28 23:04:23 +00:00
|
|
|
|
BuildBSAs();
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
|
|
|
|
Info("Installation complete! You may exit the program.");
|
2019-09-05 22:18:54 +00:00
|
|
|
|
// Removed until we decide if we want this functionality
|
|
|
|
|
// Nexus devs weren't sure this was a good idea, I (halgari) agree.
|
|
|
|
|
//AskToEndorse();
|
2019-08-26 23:02:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AskToEndorse()
|
|
|
|
|
{
|
2019-08-27 00:19:23 +00:00
|
|
|
|
var mods = ModList.Archives
|
2019-08-26 23:02:49 +00:00
|
|
|
|
.OfType<NexusMod>()
|
|
|
|
|
.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-08-27 00:19:23 +00:00
|
|
|
|
mods.PMap(mod =>
|
|
|
|
|
{
|
|
|
|
|
var er = NexusAPI.EndorseMod(mod, NexusAPIKey);
|
|
|
|
|
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-08-24 23:20:54 +00:00
|
|
|
|
private bool LocateGameFolder()
|
|
|
|
|
{
|
|
|
|
|
var vf = new VistaFolderBrowserDialog();
|
|
|
|
|
vf.Description = "Please Locate Your Game Installation Path";
|
|
|
|
|
vf.UseDescriptionForTitle = true;
|
|
|
|
|
if (vf.ShowDialog() == true)
|
|
|
|
|
{
|
|
|
|
|
GameFolder = vf.SelectedPath;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-20 04:57:08 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// We don't want to make the installer index all the archives, that's just a waste of time, so instead
|
|
|
|
|
/// we'll pass just enough information to VFS to let it know about the files we have.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void PrimeVFS()
|
|
|
|
|
{
|
|
|
|
|
HashedArchives.Do(a => VFS.AddKnown(new VirtualFile()
|
|
|
|
|
{
|
2019-08-20 22:37:55 +00:00
|
|
|
|
Paths = new string[] { a.Value },
|
|
|
|
|
Hash = a.Key
|
2019-08-20 04:57:08 +00:00
|
|
|
|
}));
|
|
|
|
|
VFS.RefreshIndexes();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ModList.Directives
|
|
|
|
|
.OfType<FromArchive>()
|
|
|
|
|
.Do(f =>
|
|
|
|
|
{
|
2019-08-20 22:37:55 +00:00
|
|
|
|
var updated_path = new string[f.ArchiveHashPath.Length];
|
2019-08-20 04:57:08 +00:00
|
|
|
|
f.ArchiveHashPath.CopyTo(updated_path, 0);
|
|
|
|
|
updated_path[0] = VFS.HashIndex[updated_path[0]].Where(e => e.IsConcrete).First().FullPath;
|
|
|
|
|
VFS.AddKnown(new VirtualFile() { Paths = updated_path });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
VFS.BackfillMissing();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
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}");
|
|
|
|
|
var source_dir = Path.Combine(Outputfolder, Consts.BSACreationDir, bsa.TempID);
|
2019-07-30 03:32:52 +00:00
|
|
|
|
var source_files = Directory.EnumerateFiles(source_dir, "*", SearchOption.AllDirectories)
|
|
|
|
|
.Select(e => e.Substring(source_dir.Length + 1))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2019-08-07 23:06:38 +00:00
|
|
|
|
if(source_files.Count > 0)
|
2019-07-30 03:32:52 +00:00
|
|
|
|
using (var a = new BSABuilder())
|
2019-07-28 23:04:23 +00:00
|
|
|
|
{
|
|
|
|
|
|
2019-07-30 03:32:52 +00:00
|
|
|
|
//a.Create(Path.Combine(Outputfolder, bsa.To), (bsa_archive_type_t)bsa.Type, entries);
|
|
|
|
|
a.HeaderType = (VersionType)bsa.Type;
|
|
|
|
|
a.FileFlags = (FileFlags)bsa.FileFlags;
|
|
|
|
|
a.ArchiveFlags = (ArchiveFlags)bsa.ArchiveFlags;
|
2019-07-28 23:04:23 +00:00
|
|
|
|
|
2019-07-30 03:32:52 +00:00
|
|
|
|
source_files.PMap(f =>
|
2019-07-28 23:04:23 +00:00
|
|
|
|
{
|
2019-07-30 03:32:52 +00:00
|
|
|
|
Status($"Adding {f} to BSA");
|
|
|
|
|
using (var fs = File.OpenRead(Path.Combine(source_dir, f)))
|
|
|
|
|
a.AddFile(f, fs);
|
|
|
|
|
});
|
2019-07-28 23:04:23 +00:00
|
|
|
|
|
2019-07-30 03:32:52 +00:00
|
|
|
|
Info($"Writing {bsa.To}");
|
|
|
|
|
a.Build(Path.Combine(Outputfolder, bsa.To));
|
2019-07-28 23:04:23 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-08-20 22:37:55 +00:00
|
|
|
|
if (Directory.Exists(Consts.BSACreationDir))
|
|
|
|
|
{
|
|
|
|
|
Info($"Removing temp folder {Consts.BSACreationDir}");
|
|
|
|
|
Directory.Delete(Path.Combine(Outputfolder, Consts.BSACreationDir), true);
|
|
|
|
|
}
|
2019-08-11 22:57:32 +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
|
|
|
|
|
.OfType<InlineFile>()
|
|
|
|
|
.PMap(directive =>
|
|
|
|
|
{
|
|
|
|
|
Status("Writing included file {0}", directive.To);
|
|
|
|
|
var out_path = Path.Combine(Outputfolder, directive.To);
|
|
|
|
|
if (File.Exists(out_path)) File.Delete(out_path);
|
2019-08-24 23:20:54 +00:00
|
|
|
|
if (directive is RemappedInlineFile)
|
|
|
|
|
{
|
|
|
|
|
WriteRemappedFile((RemappedInlineFile)directive);
|
|
|
|
|
}
|
2019-08-25 03:46:32 +00:00
|
|
|
|
else if (directive is CleanedESM)
|
|
|
|
|
{
|
|
|
|
|
GenerateCleanedESM((CleanedESM)directive);
|
|
|
|
|
}
|
2019-08-24 23:20:54 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
File.WriteAllBytes(out_path, directive.SourceData.FromBase64());
|
|
|
|
|
}
|
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);
|
|
|
|
|
var game_file = Path.Combine(GameFolder, "Data", filename);
|
|
|
|
|
Info($"Generating cleaned ESM for {filename}");
|
|
|
|
|
if (!File.Exists(game_file))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidDataException($"Missing {filename} at {game_file}");
|
|
|
|
|
}
|
|
|
|
|
Status($"Hashing game version of {filename}");
|
|
|
|
|
var sha = Utils.FileSHA256(game_file);
|
|
|
|
|
if (sha != directive.SourceESMHash)
|
|
|
|
|
throw new InvalidDataException($"Cannot patch {filename} from the game folder hashes don't match have you already cleaned the file?");
|
|
|
|
|
|
|
|
|
|
var patch_data = directive.SourceData.FromBase64();
|
|
|
|
|
var to_file = Path.Combine(Outputfolder, directive.To);
|
|
|
|
|
Status($"Patching {filename}");
|
|
|
|
|
using (var output = File.OpenWrite(to_file)) {
|
|
|
|
|
BSDiff.Apply(File.OpenRead(game_file), () => new MemoryStream(patch_data), output);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-24 23:20:54 +00:00
|
|
|
|
private void WriteRemappedFile(RemappedInlineFile directive)
|
|
|
|
|
{
|
|
|
|
|
var data = Encoding.UTF8.GetString(directive.SourceData.FromBase64());
|
|
|
|
|
|
|
|
|
|
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("\\", "/"));
|
|
|
|
|
|
|
|
|
|
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-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-08-24 23:20:54 +00:00
|
|
|
|
File.WriteAllText(Path.Combine(Outputfolder, directive.To), data);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-23 04:17:52 +00:00
|
|
|
|
private void BuildFolderStructure()
|
|
|
|
|
{
|
|
|
|
|
Info("Building Folder Structure");
|
|
|
|
|
ModList.Directives
|
|
|
|
|
.Select(d => Path.Combine(Outputfolder, Path.GetDirectoryName(d.To)))
|
|
|
|
|
.ToHashSet()
|
|
|
|
|
.Do(f => {
|
|
|
|
|
if (Directory.Exists(f)) return;
|
|
|
|
|
Directory.CreateDirectory(f);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InstallArchives()
|
|
|
|
|
{
|
|
|
|
|
Info("Installing Archives");
|
2019-08-07 23:06:38 +00:00
|
|
|
|
Info("Grouping Install Files");
|
2019-07-23 04:17:52 +00:00
|
|
|
|
var grouped = ModList.Directives
|
|
|
|
|
.OfType<FromArchive>()
|
2019-08-09 04:07:23 +00:00
|
|
|
|
.GroupBy(e => e.ArchiveHashPath[0])
|
2019-07-23 04:17:52 +00:00
|
|
|
|
.ToDictionary(k => k.Key);
|
|
|
|
|
var archives = ModList.Archives
|
2019-08-07 23:06:38 +00:00
|
|
|
|
.Select(a => new { Archive = a, AbsolutePath = HashedArchives.GetOrDefault(a.Hash) })
|
|
|
|
|
.Where(a => a.AbsolutePath != null)
|
2019-07-23 04:17:52 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
2019-08-07 23:06:38 +00:00
|
|
|
|
Info("Installing Archives");
|
2019-07-23 04:17:52 +00:00
|
|
|
|
archives.PMap(a => InstallArchive(a.Archive, a.AbsolutePath, grouped[a.Archive.Hash]));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InstallArchive(Archive archive, string absolutePath, IGrouping<string, FromArchive> grouping)
|
|
|
|
|
{
|
2019-08-22 22:05:16 +00:00
|
|
|
|
Status($"Extracting {archive.Name}");
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
2019-08-26 04:32:05 +00:00
|
|
|
|
var vfiles = grouping.Select(g =>
|
2019-07-23 04:17:52 +00:00
|
|
|
|
{
|
2019-08-26 04:32:05 +00:00
|
|
|
|
var file = VFS.FileForArchiveHashPath(g.ArchiveHashPath);
|
|
|
|
|
g.FromFile = file;
|
|
|
|
|
return g;
|
2019-08-20 04:57:08 +00:00
|
|
|
|
}).ToList();
|
|
|
|
|
|
2019-08-26 04:32:05 +00:00
|
|
|
|
var on_finish = VFS.Stage(vfiles.Select(f => f.FromFile).Distinct());
|
2019-08-20 04:57:08 +00:00
|
|
|
|
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
2019-08-26 04:32:05 +00:00
|
|
|
|
Status("Copying files for {0}", archive.Name);
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
2019-08-26 04:32:05 +00:00
|
|
|
|
vfiles.DoIndexed((idx, file) =>
|
2019-07-23 04:17:52 +00:00
|
|
|
|
{
|
2019-08-26 23:02:49 +00:00
|
|
|
|
Utils.Status($"Installing files", idx * 100 / vfiles.Count);
|
2019-08-26 04:32:05 +00:00
|
|
|
|
File.Copy(file.FromFile.StagedPath, Path.Combine(Outputfolder, file.To));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Status("Unstaging files");
|
|
|
|
|
on_finish();
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
|
|
|
|
// Now patch all the files from this archive
|
|
|
|
|
foreach (var to_patch in grouping.OfType<PatchedFromArchive>())
|
|
|
|
|
{
|
|
|
|
|
using (var patch_stream = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
Status("Patching {0}", Path.GetFileName(to_patch.To));
|
|
|
|
|
// Read in the patch data
|
|
|
|
|
|
|
|
|
|
var patch_data = to_patch.Patch.FromBase64();
|
|
|
|
|
|
|
|
|
|
var to_file = Path.Combine(Outputfolder, to_patch.To);
|
|
|
|
|
MemoryStream old_data = new MemoryStream(File.ReadAllBytes(to_file));
|
|
|
|
|
|
2019-08-03 13:02:12 +00:00
|
|
|
|
// Remove the file we're about to patch
|
|
|
|
|
File.Delete(to_file);
|
|
|
|
|
|
2019-07-23 04:17:52 +00:00
|
|
|
|
// Patch it
|
|
|
|
|
using (var out_stream = File.OpenWrite(to_file))
|
|
|
|
|
{
|
|
|
|
|
BSDiff.Apply(old_data, () => new MemoryStream(patch_data), out_stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DownloadArchives()
|
|
|
|
|
{
|
|
|
|
|
var missing = ModList.Archives.Where(a => !HashedArchives.ContainsKey(a.Hash)).ToList();
|
|
|
|
|
Info("Missing {0} archives", missing.Count);
|
|
|
|
|
|
|
|
|
|
Info("Getting Nexus API Key, if a browser appears, please accept");
|
|
|
|
|
NexusAPIKey = NexusAPI.GetNexusAPIKey();
|
|
|
|
|
|
2019-08-04 22:08:03 +00:00
|
|
|
|
var user_status = NexusAPI.GetUserStatus(NexusAPIKey);
|
|
|
|
|
|
|
|
|
|
if (!user_status.is_premium) {
|
|
|
|
|
Info($"Automated installs with Wabbajack requires a premium nexus account. {user_status.name} is not a premium account");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-23 04:17:52 +00:00
|
|
|
|
DownloadMissingArchives(missing);
|
2019-08-04 22:08:03 +00:00
|
|
|
|
return;
|
2019-07-23 04:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 04:24:31 +00:00
|
|
|
|
private void DownloadMissingArchives(List<Archive> missing, bool download=true)
|
2019-07-23 04:17:52 +00:00
|
|
|
|
{
|
|
|
|
|
missing.PMap(archive =>
|
|
|
|
|
{
|
2019-08-04 22:08:03 +00:00
|
|
|
|
Info($"Downloading {archive.Name}");
|
|
|
|
|
var output_path = Path.Combine(DownloadFolder, archive.Name);
|
|
|
|
|
|
2019-08-30 04:24:31 +00:00
|
|
|
|
if (download)
|
|
|
|
|
if (output_path.FileExists())
|
|
|
|
|
File.Delete(output_path);
|
2019-08-04 22:08:03 +00:00
|
|
|
|
|
2019-08-30 04:24:31 +00:00
|
|
|
|
return DownloadArchive(archive, download);
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-08-11 22:57:32 +00:00
|
|
|
|
|
2019-08-30 04:24:31 +00:00
|
|
|
|
public bool DownloadArchive(Archive archive, bool download)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
switch (archive)
|
|
|
|
|
{
|
2019-07-26 20:59:14 +00:00
|
|
|
|
case NexusMod a:
|
2019-08-02 22:31:13 +00:00
|
|
|
|
string url;
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-08-30 04:24:31 +00:00
|
|
|
|
url = NexusAPI.GetNexusDownloadLink(a as NexusMod, NexusAPIKey, !download);
|
|
|
|
|
if (!download) return true;
|
2019-08-02 22:31:13 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Info($"{a.Name} - Error Getting Nexus Download URL - {ex.Message}");
|
2019-08-30 04:24:31 +00:00
|
|
|
|
return false;
|
2019-08-02 22:31:13 +00:00
|
|
|
|
}
|
2019-08-30 04:24:31 +00:00
|
|
|
|
Info($"Downloading Nexus Archive - {archive.Name} - {a.GameName} - {a.ModID} - {a.FileID}");
|
2019-07-26 20:59:14 +00:00
|
|
|
|
DownloadURLDirect(archive, url);
|
2019-08-30 04:24:31 +00:00
|
|
|
|
return true;
|
2019-08-04 22:08:03 +00:00
|
|
|
|
case MEGAArchive a:
|
2019-08-30 04:24:31 +00:00
|
|
|
|
return DownloadMegaArchive(a, download);
|
2019-07-26 20:59:14 +00:00
|
|
|
|
case GoogleDriveMod a:
|
2019-08-30 04:24:31 +00:00
|
|
|
|
return DownloadGoogleDriveArchive(a, download);
|
2019-07-26 20:59:14 +00:00
|
|
|
|
case MODDBArchive a:
|
2019-08-30 04:24:31 +00:00
|
|
|
|
return DownloadModDBArchive(archive, (archive as MODDBArchive).URL, download);
|
2019-08-02 22:31:13 +00:00
|
|
|
|
case MediaFireArchive a:
|
2019-08-30 04:24:31 +00:00
|
|
|
|
return false;
|
|
|
|
|
//return DownloadMediaFireArchive(archive, a.URL, download);
|
2019-07-26 20:59:14 +00:00
|
|
|
|
case DirectURLArchive a:
|
2019-08-30 04:24:31 +00:00
|
|
|
|
return DownloadURLDirect(archive, a.URL, headers: a.Headers, download: download);
|
|
|
|
|
}
|
2019-07-26 20:59:14 +00:00
|
|
|
|
}
|
2019-08-30 04:24:31 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Utils.Log($"Download error for file {archive.Name}");
|
|
|
|
|
Utils.Log(ex.ToString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2019-07-23 04:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-02 22:31:13 +00:00
|
|
|
|
private void DownloadMediaFireArchive(Archive a, string url)
|
|
|
|
|
{
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
|
var result = client.GetStringSync(url);
|
|
|
|
|
var regex = new Regex("(?<= href =\\\").*\\.mediafire\\.com.*(?=\\\")");
|
|
|
|
|
var confirm = regex.Match(result);
|
|
|
|
|
DownloadURLDirect(a, confirm.ToString(), client);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 04:24:31 +00:00
|
|
|
|
private bool DownloadMegaArchive(MEGAArchive m, bool download)
|
2019-08-04 22:08:03 +00:00
|
|
|
|
{
|
|
|
|
|
var client = new MegaApiClient();
|
|
|
|
|
Status("Logging into MEGA (as anonymous)");
|
|
|
|
|
client.LoginAnonymous();
|
|
|
|
|
var file_link = new Uri(m.URL);
|
|
|
|
|
var node = client.GetNodeFromLink(file_link);
|
2019-08-30 04:24:31 +00:00
|
|
|
|
if (!download) return true;
|
2019-08-04 22:08:03 +00:00
|
|
|
|
Status("Downloading MEGA file: {0}", m.Name);
|
|
|
|
|
|
|
|
|
|
var output_path = Path.Combine(DownloadFolder, m.Name);
|
|
|
|
|
client.DownloadFile(file_link, output_path);
|
2019-08-30 04:24:31 +00:00
|
|
|
|
return true;
|
2019-08-04 22:08:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 04:24:31 +00:00
|
|
|
|
private bool DownloadGoogleDriveArchive(GoogleDriveMod a, bool download)
|
2019-07-26 20:59:14 +00:00
|
|
|
|
{
|
|
|
|
|
var initial_url = $"https://drive.google.com/uc?id={a.Id}&export=download";
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
|
var result = client.GetStringSync(initial_url);
|
|
|
|
|
var regex = new Regex("(?<=/uc\\?export=download&confirm=).*(?=;id=)");
|
|
|
|
|
var confirm = regex.Match(result);
|
2019-08-30 04:24:31 +00:00
|
|
|
|
return DownloadURLDirect(a, $"https://drive.google.com/uc?export=download&confirm={confirm}&id={a.Id}", client, download: download);
|
2019-07-26 20:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 04:24:31 +00:00
|
|
|
|
private bool DownloadModDBArchive(Archive archive, string url, bool download)
|
2019-07-24 04:31:08 +00:00
|
|
|
|
{
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
|
var result = client.GetStringSync(url);
|
|
|
|
|
var regex = new Regex("https:\\/\\/www\\.moddb\\.com\\/downloads\\/mirror\\/.*(?=\\\")");
|
|
|
|
|
var match = regex.Match(result);
|
2019-08-30 04:24:31 +00:00
|
|
|
|
return DownloadURLDirect(archive, match.Value, download: download);
|
2019-07-24 04:31:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 04:24:31 +00:00
|
|
|
|
private bool DownloadURLDirect(Archive archive, string url, HttpClient client = null, bool download = true, List<string> headers = null)
|
2019-07-23 04:17:52 +00:00
|
|
|
|
{
|
2019-08-02 22:31:13 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (client == null)
|
|
|
|
|
{
|
|
|
|
|
client = new HttpClient();
|
|
|
|
|
client.DefaultRequestHeaders.Add("User-Agent", Consts.UserAgent);
|
|
|
|
|
}
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
2019-08-02 22:31:13 +00:00
|
|
|
|
if (headers != null) {
|
|
|
|
|
foreach (var header in headers)
|
|
|
|
|
{
|
|
|
|
|
var idx = header.IndexOf(':');
|
|
|
|
|
var k = header.Substring(0, idx);
|
|
|
|
|
var v = header.Substring(idx + 1);
|
|
|
|
|
client.DefaultRequestHeaders.Add(k, v);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
2019-08-02 22:31:13 +00:00
|
|
|
|
long total_read = 0;
|
|
|
|
|
int buffer_size = 1024 * 32;
|
2019-07-24 04:31:08 +00:00
|
|
|
|
|
2019-08-02 22:31:13 +00:00
|
|
|
|
var response = client.GetSync(url);
|
|
|
|
|
var stream = response.Content.ReadAsStreamAsync();
|
2019-08-04 22:08:03 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
stream.Wait();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
if (stream.IsFaulted)
|
|
|
|
|
{
|
|
|
|
|
Info($"While downloading {url} - {Utils.ExceptionToString(stream.Exception)}");
|
2019-08-30 04:24:31 +00:00
|
|
|
|
return false;
|
2019-08-04 22:08:03 +00:00
|
|
|
|
}
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
2019-08-30 04:24:31 +00:00
|
|
|
|
if (!download)
|
|
|
|
|
return true;
|
|
|
|
|
|
2019-08-02 22:31:13 +00:00
|
|
|
|
string header_var = "1";
|
|
|
|
|
if (response.Content.Headers.Contains("Content-Length"))
|
|
|
|
|
header_var = response.Content.Headers.GetValues("Content-Length").FirstOrDefault();
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
2019-08-02 22:31:13 +00:00
|
|
|
|
long content_size = header_var != null ? long.Parse(header_var) : 1;
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
2019-08-02 22:31:13 +00:00
|
|
|
|
var output_path = Path.Combine(DownloadFolder, archive.Name);
|
2019-08-04 22:08:03 +00:00
|
|
|
|
;
|
2019-08-02 22:31:13 +00:00
|
|
|
|
|
|
|
|
|
using (var webs = stream.Result)
|
|
|
|
|
using (var fs = File.OpenWrite(output_path))
|
2019-07-23 04:17:52 +00:00
|
|
|
|
{
|
2019-08-02 22:31:13 +00:00
|
|
|
|
var buffer = new byte[buffer_size];
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
var read = webs.Read(buffer, 0, buffer_size);
|
|
|
|
|
if (read == 0) break;
|
|
|
|
|
Status((int)(total_read * 100 / content_size), "Downloading {0}", archive.Name);
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
2019-08-02 22:31:13 +00:00
|
|
|
|
fs.Write(buffer, 0, read);
|
|
|
|
|
total_read += read;
|
2019-07-23 04:17:52 +00:00
|
|
|
|
|
2019-08-02 22:31:13 +00:00
|
|
|
|
}
|
2019-07-23 04:17:52 +00:00
|
|
|
|
}
|
2019-08-02 22:31:13 +00:00
|
|
|
|
Status("Hashing {0}", archive.Name);
|
|
|
|
|
HashArchive(output_path);
|
2019-08-30 04:24:31 +00:00
|
|
|
|
return true;
|
2019-08-02 22:31:13 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Info($"{archive.Name} - Error downloading from: {url}");
|
2019-08-30 04:24:31 +00:00
|
|
|
|
return false;
|
2019-07-23 04:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HashArchives()
|
|
|
|
|
{
|
|
|
|
|
HashedArchives = Directory.EnumerateFiles(DownloadFolder)
|
2019-09-02 22:02:04 +00:00
|
|
|
|
.Where(e => !e.EndsWith(".sha"))
|
2019-07-23 04:17:52 +00:00
|
|
|
|
.PMap(e => (HashArchive(e), e))
|
2019-09-02 19:17:07 +00:00
|
|
|
|
.OrderByDescending(e => File.GetLastWriteTime(e.Item2))
|
|
|
|
|
.GroupBy(e => e.Item1)
|
|
|
|
|
.Select(e => e.First())
|
2019-07-23 04:17:52 +00:00
|
|
|
|
.ToDictionary(e => e.Item1, e => e.Item2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string HashArchive(string e)
|
|
|
|
|
{
|
|
|
|
|
var cache = e + ".sha";
|
|
|
|
|
if (cache.FileExists() && new FileInfo(cache).LastWriteTime >= new FileInfo(e).LastWriteTime)
|
|
|
|
|
return File.ReadAllText(cache);
|
|
|
|
|
|
|
|
|
|
Status("Hashing {0}", Path.GetFileName(e));
|
|
|
|
|
File.WriteAllText(cache, Utils.FileSHA256(e));
|
|
|
|
|
return HashArchive(e);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 23:44:35 +00:00
|
|
|
|
public static ModList CheckForModPack()
|
2019-07-31 03:59:19 +00:00
|
|
|
|
{
|
2019-08-30 04:24:31 +00:00
|
|
|
|
Utils.Log("Looking for attached modlist");
|
2019-07-31 03:59:19 +00:00
|
|
|
|
using (var s = File.OpenRead(Assembly.GetExecutingAssembly().Location))
|
|
|
|
|
{
|
|
|
|
|
var magic_bytes = Encoding.ASCII.GetBytes(Consts.ModPackMagic);
|
|
|
|
|
s.Position = s.Length - magic_bytes.Length;
|
|
|
|
|
using (var br = new BinaryReader(s))
|
|
|
|
|
{
|
|
|
|
|
var bytes = br.ReadBytes(magic_bytes.Length);
|
|
|
|
|
var magic = Encoding.ASCII.GetString(bytes);
|
|
|
|
|
if (magic != Consts.ModPackMagic)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.Position = s.Length - magic_bytes.Length - 8;
|
|
|
|
|
var start_pos = br.ReadInt64();
|
|
|
|
|
s.Position = start_pos;
|
2019-08-30 04:24:31 +00:00
|
|
|
|
Utils.Log("Modlist found, loading...");
|
2019-09-12 23:44:35 +00:00
|
|
|
|
using (var dc = LZ4Stream.Decode(br.BaseStream, leaveOpen: true))
|
|
|
|
|
{
|
|
|
|
|
IFormatter formatter = new BinaryFormatter();
|
|
|
|
|
var list = formatter.Deserialize(dc);
|
|
|
|
|
Utils.Log("Modlist loaded.");
|
|
|
|
|
return (ModList)list;
|
|
|
|
|
}
|
2019-07-31 03:59:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-23 04:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|