2020-08-07 08:29:12 +00:00
|
|
|
using System;
|
2019-11-16 13:22:40 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO.Compression;
|
|
|
|
using System.Linq;
|
2020-01-07 13:50:11 +00:00
|
|
|
using System.Threading;
|
2019-12-04 01:26:26 +00:00
|
|
|
using System.Threading.Tasks;
|
2019-11-20 00:15:46 +00:00
|
|
|
using Alphaleonis.Win32.Filesystem;
|
2019-11-16 13:22:40 +00:00
|
|
|
using Wabbajack.Common;
|
|
|
|
using Wabbajack.Lib.Downloaders;
|
|
|
|
using Wabbajack.VirtualFileSystem;
|
2020-09-05 14:01:32 +00:00
|
|
|
using Wabbajack.VirtualFileSystem.SevenZipExtractor;
|
2019-11-16 13:22:40 +00:00
|
|
|
using Directory = Alphaleonis.Win32.Filesystem.Directory;
|
2019-11-24 13:04:57 +00:00
|
|
|
using File = Alphaleonis.Win32.Filesystem.File;
|
|
|
|
using FileInfo = Alphaleonis.Win32.Filesystem.FileInfo;
|
2019-11-16 13:22:40 +00:00
|
|
|
using Path = Alphaleonis.Win32.Filesystem.Path;
|
|
|
|
|
|
|
|
namespace Wabbajack.Lib
|
|
|
|
{
|
2019-11-17 23:48:32 +00:00
|
|
|
public abstract class AInstaller : ABatchProcessor
|
2019-11-16 13:22:40 +00:00
|
|
|
{
|
|
|
|
public bool IgnoreMissingFiles { get; internal set; } = false;
|
|
|
|
|
2020-03-25 12:47:25 +00:00
|
|
|
public AbsolutePath OutputFolder { get; private set; }
|
|
|
|
public AbsolutePath DownloadFolder { get; private set; }
|
2019-11-16 13:22:40 +00:00
|
|
|
|
2019-12-01 20:22:33 +00:00
|
|
|
public abstract ModManager ModManager { get; }
|
2019-11-16 13:22:40 +00:00
|
|
|
|
2020-03-26 21:15:44 +00:00
|
|
|
public AbsolutePath ModListArchive { get; private set; }
|
2019-12-01 20:22:33 +00:00
|
|
|
public ModList ModList { get; private set; }
|
2020-04-09 17:17:24 +00:00
|
|
|
public Dictionary<Hash, AbsolutePath> HashedArchives { get; } = new Dictionary<Hash, AbsolutePath>();
|
2020-01-07 13:50:11 +00:00
|
|
|
|
2020-05-02 23:05:05 +00:00
|
|
|
public GameMetaData Game { get; }
|
|
|
|
|
2020-04-10 01:29:53 +00:00
|
|
|
public SystemParameters? SystemParameters { get; set; }
|
2020-08-05 22:01:45 +00:00
|
|
|
|
|
|
|
public bool UseCompression { get; set; }
|
2019-11-16 13:22:40 +00:00
|
|
|
|
2020-09-05 14:01:32 +00:00
|
|
|
public TempFolder? ExtractedModlistFolder { get; set; } = null;
|
|
|
|
|
2020-05-13 04:12:55 +00:00
|
|
|
|
2020-05-02 23:05:05 +00:00
|
|
|
public AInstaller(AbsolutePath archive, ModList modList, AbsolutePath outputFolder, AbsolutePath downloadFolder, SystemParameters? parameters, int steps, Game game)
|
2020-04-04 18:26:14 +00:00
|
|
|
: base(steps)
|
2019-12-01 20:22:33 +00:00
|
|
|
{
|
|
|
|
ModList = modList;
|
|
|
|
ModListArchive = archive;
|
|
|
|
OutputFolder = outputFolder;
|
|
|
|
DownloadFolder = downloadFolder;
|
2020-01-07 13:50:11 +00:00
|
|
|
SystemParameters = parameters;
|
2020-05-02 23:05:05 +00:00
|
|
|
Game = game.MetaData();
|
2020-09-05 14:01:32 +00:00
|
|
|
|
2019-12-01 20:22:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 04:12:55 +00:00
|
|
|
public async Task ExtractModlist()
|
|
|
|
{
|
2020-09-05 14:01:32 +00:00
|
|
|
ExtractedModlistFolder = await TempFolder.Create();
|
|
|
|
await FileExtractor2.GatheringExtract(new NativeFileStreamFactory(ModListArchive), _ => true,
|
|
|
|
async (path, sfn) =>
|
|
|
|
{
|
|
|
|
await using var s = await sfn.GetStream();
|
|
|
|
var fp = ExtractedModlistFolder.Dir.Combine(path);
|
|
|
|
fp.Parent.CreateDirectory();
|
|
|
|
await fp.WriteAllAsync(s);
|
|
|
|
return 0;
|
|
|
|
});
|
2020-05-13 04:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-11-16 13:22:40 +00:00
|
|
|
public void Info(string msg)
|
|
|
|
{
|
|
|
|
Utils.Log(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Status(string msg)
|
|
|
|
{
|
2020-02-08 04:35:08 +00:00
|
|
|
Queue.Report(msg, Percent.Zero);
|
2019-11-16 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Error(string msg)
|
|
|
|
{
|
|
|
|
Utils.Log(msg);
|
|
|
|
throw new Exception(msg);
|
|
|
|
}
|
|
|
|
|
2020-03-25 22:30:43 +00:00
|
|
|
public async Task<byte[]> LoadBytesFromPath(RelativePath path)
|
2019-11-16 13:22:40 +00:00
|
|
|
{
|
2020-09-05 19:36:44 +00:00
|
|
|
var fullPath = ExtractedModlistFolder!.Dir.Combine(path);
|
|
|
|
if (!fullPath.IsFile)
|
|
|
|
throw new Exception($"Cannot load inlined data {path} file does not exist");
|
|
|
|
|
|
|
|
return await fullPath.ReadAllBytesAsync();
|
2019-11-16 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:15:44 +00:00
|
|
|
public static ModList LoadFromFile(AbsolutePath path)
|
2019-11-16 13:22:40 +00:00
|
|
|
{
|
2020-03-26 21:15:44 +00:00
|
|
|
using var fs = new FileStream((string)path, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
|
|
using var ar = new ZipArchive(fs, ZipArchiveMode.Read);
|
|
|
|
var entry = ar.GetEntry("modlist");
|
|
|
|
if (entry == null)
|
2019-11-16 13:22:40 +00:00
|
|
|
{
|
2020-03-26 21:15:44 +00:00
|
|
|
entry = ar.GetEntry("modlist.json");
|
2019-11-16 13:22:40 +00:00
|
|
|
using (var e = entry.Open())
|
2020-04-06 20:48:54 +00:00
|
|
|
return e.FromJson<ModList>();
|
2019-11-16 13:22:40 +00:00
|
|
|
}
|
2020-03-26 21:15:44 +00:00
|
|
|
using (var e = entry.Open())
|
2020-04-06 20:48:54 +00:00
|
|
|
return e.FromJson<ModList>();
|
2019-11-16 13:22:40 +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>
|
2020-03-25 12:47:25 +00:00
|
|
|
protected async Task PrimeVFS()
|
2019-11-16 13:22:40 +00:00
|
|
|
{
|
2020-03-25 12:47:25 +00:00
|
|
|
VFS.AddKnown(ModList.Directives.OfType<FromArchive>().Select(d => d.ArchiveHashPath), HashedArchives);
|
2019-12-07 02:54:27 +00:00
|
|
|
await VFS.BackfillMissing();
|
2019-11-16 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void BuildFolderStructure()
|
|
|
|
{
|
|
|
|
Info("Building Folder Structure");
|
|
|
|
ModList.Directives
|
2020-03-25 12:47:25 +00:00
|
|
|
.Select(d => OutputFolder.Combine(d.To.Parent))
|
2020-01-07 13:50:11 +00:00
|
|
|
.Distinct()
|
2020-03-28 02:54:14 +00:00
|
|
|
.Do(f => f.CreateDirectory());
|
2019-11-16 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 01:26:26 +00:00
|
|
|
public async Task InstallArchives()
|
2019-11-16 13:22:40 +00:00
|
|
|
{
|
|
|
|
var grouped = ModList.Directives
|
|
|
|
.OfType<FromArchive>()
|
2020-09-05 14:01:32 +00:00
|
|
|
.Select(a => new {VF = VFS.Index.FileForArchiveHashPath(a.ArchiveHashPath), Directive = a})
|
|
|
|
.GroupBy(a => a.VF)
|
|
|
|
.ToDictionary(a => a.Key);
|
|
|
|
|
|
|
|
if (grouped.Count == 0) return;
|
|
|
|
|
|
|
|
await VFS.Extract(Queue, grouped.Keys.ToHashSet(), async (vf, sf) =>
|
|
|
|
{
|
|
|
|
await using var s = await sf.GetStream();
|
|
|
|
foreach (var directive in grouped[vf])
|
|
|
|
{
|
2020-09-06 03:28:31 +00:00
|
|
|
var file = directive.Directive;
|
2020-09-05 14:01:32 +00:00
|
|
|
s.Position = 0;
|
2020-09-05 19:36:44 +00:00
|
|
|
|
2020-09-06 03:28:31 +00:00
|
|
|
switch (file)
|
2020-09-05 19:36:44 +00:00
|
|
|
{
|
|
|
|
case PatchedFromArchive pfa:
|
|
|
|
{
|
|
|
|
var patchData = await LoadBytesFromPath(pfa.PatchID);
|
2020-09-06 03:28:31 +00:00
|
|
|
var toFile = file.To.RelativeTo(OutputFolder);
|
2020-09-08 02:22:23 +00:00
|
|
|
{
|
|
|
|
await using var os = await toFile.Create();
|
|
|
|
Utils.ApplyPatch(s, () => new MemoryStream(patchData), os);
|
|
|
|
}
|
|
|
|
|
2020-09-06 03:28:31 +00:00
|
|
|
if (await VirusScanner.ShouldScan(toFile) &&
|
|
|
|
await ClientAPI.GetVirusScanResult(toFile) == VirusScanner.Result.Malware)
|
|
|
|
{
|
|
|
|
await toFile.DeleteAsync();
|
|
|
|
Utils.ErrorThrow(new Exception($"Virus scan of patched executable reported possible malware: {toFile.ToString()} ({(long)await toFile.FileHashCachedAsync()})"));
|
|
|
|
}
|
2020-09-05 19:36:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FromArchive _:
|
|
|
|
await directive.Directive.To.RelativeTo(OutputFolder).WriteAllAsync(s, false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception($"No handler for {directive}");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2020-05-14 11:28:29 +00:00
|
|
|
|
2020-08-07 11:24:30 +00:00
|
|
|
if (file is PatchedFromArchive)
|
|
|
|
{
|
|
|
|
await file.To.RelativeTo(OutputFolder).FileHashAsync();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file.To.RelativeTo(OutputFolder).FileHashWriteCache(file.Hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UseCompression)
|
2020-08-05 22:01:45 +00:00
|
|
|
{
|
|
|
|
Utils.Status($"Compacting {file.To}");
|
|
|
|
await file.To.RelativeTo(OutputFolder).Compact(FileCompaction.Algorithm.XPRESS16K);
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 03:28:31 +00:00
|
|
|
});
|
2019-11-16 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 01:26:26 +00:00
|
|
|
public async Task DownloadArchives()
|
2019-11-16 13:22:40 +00:00
|
|
|
{
|
|
|
|
var missing = ModList.Archives.Where(a => !HashedArchives.ContainsKey(a.Hash)).ToList();
|
|
|
|
Info($"Missing {missing.Count} archives");
|
|
|
|
|
|
|
|
Info("Getting Nexus API Key, if a browser appears, please accept");
|
|
|
|
|
|
|
|
var dispatchers = missing.Select(m => m.State.GetDownloader()).Distinct();
|
|
|
|
|
2019-12-07 02:45:13 +00:00
|
|
|
await Task.WhenAll(dispatchers.Select(d => d.Prepare()));
|
2020-02-06 05:30:31 +00:00
|
|
|
|
2019-12-04 01:26:26 +00:00
|
|
|
await DownloadMissingArchives(missing);
|
2019-11-16 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 12:37:24 +00:00
|
|
|
public async Task DownloadMissingArchives(List<Archive> missing, bool download = true)
|
2019-11-16 13:22:40 +00:00
|
|
|
{
|
|
|
|
if (download)
|
|
|
|
{
|
|
|
|
foreach (var a in missing.Where(a => a.State.GetType() == typeof(ManualDownloader.State)))
|
|
|
|
{
|
2020-03-25 22:49:32 +00:00
|
|
|
var outputPath = DownloadFolder.Combine(a.Name);
|
2019-12-06 05:29:17 +00:00
|
|
|
await a.State.Download(a, outputPath);
|
2019-11-16 13:22:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 20:23:03 +00:00
|
|
|
DesiredThreads.OnNext(DownloadThreads);
|
2019-12-04 01:26:26 +00:00
|
|
|
await missing.Where(a => a.State.GetType() != typeof(ManualDownloader.State))
|
2019-12-06 05:29:17 +00:00
|
|
|
.PMap(Queue, async archive =>
|
2019-11-16 13:22:40 +00:00
|
|
|
{
|
|
|
|
Info($"Downloading {archive.Name}");
|
2020-03-25 22:49:32 +00:00
|
|
|
var outputPath = DownloadFolder.Combine(archive.Name);
|
2019-11-16 13:22:40 +00:00
|
|
|
|
|
|
|
if (download)
|
2020-02-12 05:31:42 +00:00
|
|
|
{
|
2020-03-25 22:49:32 +00:00
|
|
|
if (outputPath.Exists)
|
2020-02-12 05:31:42 +00:00
|
|
|
{
|
2020-03-25 22:49:32 +00:00
|
|
|
var origName = Path.GetFileNameWithoutExtension(archive.Name);
|
2020-02-12 05:31:42 +00:00
|
|
|
var ext = Path.GetExtension(archive.Name);
|
2020-03-25 22:49:32 +00:00
|
|
|
var uniqueKey = archive.State.PrimaryKeyString.StringSha256Hex();
|
|
|
|
outputPath = DownloadFolder.Combine(origName + "_" + uniqueKey + "_" + ext);
|
2020-05-26 11:31:11 +00:00
|
|
|
await outputPath.DeleteAsync();
|
2020-02-12 05:31:42 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-16 13:22:40 +00:00
|
|
|
|
2020-02-12 12:37:24 +00:00
|
|
|
return await DownloadArchive(archive, download, outputPath);
|
2019-11-16 13:22:40 +00:00
|
|
|
});
|
2020-09-12 20:23:03 +00:00
|
|
|
|
|
|
|
DesiredThreads.OnNext(DiskThreads);
|
|
|
|
|
2019-11-16 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 22:49:32 +00:00
|
|
|
public async Task<bool> DownloadArchive(Archive archive, bool download, AbsolutePath? destination = null)
|
2019-11-16 13:22:40 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-08-20 22:02:50 +00:00
|
|
|
destination ??= DownloadFolder.Combine(archive.Name);
|
|
|
|
|
|
|
|
var result = await DownloadDispatcher.DownloadWithPossibleUpgrade(archive, destination.Value);
|
|
|
|
if (result == DownloadDispatcher.DownloadResult.Update)
|
|
|
|
{
|
|
|
|
await destination.Value.MoveToAsync(destination.Value.Parent.Combine(archive.Hash.ToHex()));
|
|
|
|
}
|
2019-11-16 13:22:40 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Utils.Log($"Download error for file {archive.Name}");
|
|
|
|
Utils.Log(ex.ToString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-04 01:26:26 +00:00
|
|
|
public async Task HashArchives()
|
2019-11-16 13:22:40 +00:00
|
|
|
{
|
2020-05-16 15:08:40 +00:00
|
|
|
Utils.Log("Looking for files to hash");
|
|
|
|
var toHash = DownloadFolder.EnumerateFiles()
|
2020-05-02 23:05:05 +00:00
|
|
|
.Concat(Game.GameLocation().EnumerateFiles())
|
2020-05-16 15:08:40 +00:00
|
|
|
.ToList();
|
|
|
|
|
2020-05-16 21:27:23 +00:00
|
|
|
Utils.Log($"Found {toHash.Count} files to hash");
|
2020-05-16 15:08:40 +00:00
|
|
|
|
|
|
|
var hashResults = await
|
|
|
|
toHash
|
|
|
|
.PMap(Queue, async e => (await e.FileHashCachedAsync(), e));
|
|
|
|
|
2020-04-12 18:18:08 +00:00
|
|
|
HashedArchives.SetTo(hashResults
|
2020-03-25 22:49:32 +00:00
|
|
|
.OrderByDescending(e => e.Item2.LastModified)
|
2019-11-16 13:22:40 +00:00
|
|
|
.GroupBy(e => e.Item1)
|
|
|
|
.Select(e => e.First())
|
2020-04-09 17:17:24 +00:00
|
|
|
.Select(e => new KeyValuePair<Hash, AbsolutePath>(e.Item1, e.Item2)));
|
2019-11-16 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 21:36:26 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Disabled
|
|
|
|
/// </summary>
|
2019-11-20 23:39:03 +00:00
|
|
|
public void ValidateFreeSpace()
|
|
|
|
{
|
2019-11-24 21:36:26 +00:00
|
|
|
return;
|
|
|
|
// Disabled, caused more problems than it was worth.
|
|
|
|
/*
|
|
|
|
DiskSpaceInfo DriveInfo(string path)
|
2019-11-20 23:39:03 +00:00
|
|
|
{
|
|
|
|
return Volume.GetDiskFreeSpace(Volume.GetUniqueVolumeNameForPath(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
var paths = new[] {(OutputFolder, ModList.InstallSize),
|
|
|
|
(DownloadFolder, ModList.DownloadSize),
|
|
|
|
(Directory.GetCurrentDirectory(), ModList.ScratchSpaceSize)};
|
|
|
|
paths.GroupBy(f => DriveInfo(f.Item1).DriveName)
|
|
|
|
.Do(g =>
|
|
|
|
{
|
|
|
|
var required = g.Sum(i => i.Item2);
|
2019-11-24 00:04:34 +00:00
|
|
|
var contains = g.Sum(folder =>
|
|
|
|
Directory.EnumerateFiles(folder.Item1, "*", DirectoryEnumerationOptions.Recursive)
|
|
|
|
.Sum(file => new FileInfo(file).Length));
|
2019-11-20 23:39:03 +00:00
|
|
|
var available = DriveInfo(g.Key).FreeBytesAvailable;
|
2019-11-24 00:04:34 +00:00
|
|
|
if (required - contains > available)
|
2019-11-20 23:39:03 +00:00
|
|
|
throw new NotEnoughDiskSpaceException(
|
2020-01-13 21:11:07 +00:00
|
|
|
$"This ModList requires {required.ToFileSizeString()} on {g.Key} but only {available.ToFileSizeString()} is available.");
|
2019-11-20 23:39:03 +00:00
|
|
|
});
|
2019-11-24 21:36:26 +00:00
|
|
|
*/
|
2019-11-20 23:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-18 05:21:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The user may already have some files in the OutputFolder. If so we can go through these and
|
|
|
|
/// figure out which need to be updated, deleted, or left alone
|
|
|
|
/// </summary>
|
2019-12-04 01:26:26 +00:00
|
|
|
public async Task OptimizeModlist()
|
2019-11-18 05:21:24 +00:00
|
|
|
{
|
2020-01-13 21:11:07 +00:00
|
|
|
Utils.Log("Optimizing ModList directives");
|
2020-01-06 04:35:12 +00:00
|
|
|
|
2020-01-13 21:11:07 +00:00
|
|
|
// Clone the ModList so our changes don't modify the original data
|
2020-01-06 04:35:12 +00:00
|
|
|
ModList = ModList.Clone();
|
|
|
|
|
2019-11-18 05:21:24 +00:00
|
|
|
var indexed = ModList.Directives.ToDictionary(d => d.To);
|
|
|
|
|
2020-07-01 03:46:26 +00:00
|
|
|
|
|
|
|
var profileFolder = OutputFolder.Combine("profiles");
|
|
|
|
var savePath = (RelativePath)"saves";
|
|
|
|
|
2019-11-24 23:03:36 +00:00
|
|
|
UpdateTracker.NextStep("Looking for files to delete");
|
2020-03-25 23:15:19 +00:00
|
|
|
await OutputFolder.EnumerateFiles()
|
2020-05-26 11:31:11 +00:00
|
|
|
.PMap(Queue, UpdateTracker, async f =>
|
2019-11-20 00:15:46 +00:00
|
|
|
{
|
2020-03-28 02:54:14 +00:00
|
|
|
var relativeTo = f.RelativeTo(OutputFolder);
|
|
|
|
if (indexed.ContainsKey(relativeTo) || f.InFolder(DownloadFolder))
|
2019-11-20 00:15:46 +00:00
|
|
|
return;
|
|
|
|
|
2020-07-01 03:46:26 +00:00
|
|
|
if (f.InFolder(profileFolder) && f.Parent.FileName == savePath) return;
|
|
|
|
|
2020-03-28 02:54:14 +00:00
|
|
|
Utils.Log($"Deleting {relativeTo} it's not part of this ModList");
|
2020-05-26 11:31:11 +00:00
|
|
|
await f.DeleteAsync();
|
2019-11-20 00:15:46 +00:00
|
|
|
});
|
|
|
|
|
2020-01-05 05:07:00 +00:00
|
|
|
Utils.Log("Cleaning empty folders");
|
2020-01-07 04:46:36 +00:00
|
|
|
var expectedFolders = indexed.Keys
|
2020-03-25 23:15:19 +00:00
|
|
|
.Select(f => f.RelativeTo(OutputFolder))
|
2020-01-07 04:46:36 +00:00
|
|
|
// We ignore the last part of the path, so we need a dummy file name
|
2020-03-25 23:15:19 +00:00
|
|
|
.Append(DownloadFolder.Combine("_"))
|
2020-03-28 02:54:14 +00:00
|
|
|
.Where(f => f.InFolder(OutputFolder))
|
2020-01-07 04:46:36 +00:00
|
|
|
.SelectMany(path =>
|
2020-02-08 19:25:49 +00:00
|
|
|
{
|
|
|
|
// Get all the folders and all the folder parents
|
|
|
|
// so for foo\bar\baz\qux.txt this emits ["foo", "foo\\bar", "foo\\bar\\baz"]
|
2020-03-25 23:15:19 +00:00
|
|
|
var split = ((string)path.RelativeTo(OutputFolder)).Split('\\');
|
2020-02-08 19:25:49 +00:00
|
|
|
return Enumerable.Range(1, split.Length - 1).Select(t => string.Join("\\", split.Take(t)));
|
|
|
|
})
|
|
|
|
.Distinct()
|
2020-03-25 23:15:19 +00:00
|
|
|
.Select(p => OutputFolder.Combine(p))
|
2020-02-08 19:25:49 +00:00
|
|
|
.ToHashSet();
|
2020-01-05 05:07:00 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-03-28 04:33:26 +00:00
|
|
|
var toDelete = OutputFolder.EnumerateDirectories(true)
|
2020-01-05 05:07:00 +00:00
|
|
|
.Where(p => !expectedFolders.Contains(p))
|
2020-03-28 04:33:26 +00:00
|
|
|
.OrderByDescending(p => ((string)p).Length)
|
|
|
|
.ToList();
|
|
|
|
foreach (var dir in toDelete)
|
|
|
|
{
|
2020-07-01 03:46:26 +00:00
|
|
|
await dir.DeleteDirectory(dontDeleteIfNotEmpty:true);
|
2020-03-28 04:33:26 +00:00
|
|
|
}
|
2020-01-05 05:07:00 +00:00
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
// ignored because it's not worth throwing a fit over
|
|
|
|
Utils.Log("Error when trying to clean empty folders. This doesn't really matter.");
|
|
|
|
}
|
|
|
|
|
2020-09-11 01:22:07 +00:00
|
|
|
var existingfiles = OutputFolder.EnumerateFiles().ToHashSet();
|
|
|
|
|
2020-02-08 19:25:49 +00:00
|
|
|
UpdateTracker.NextStep("Looking for unmodified files");
|
2020-03-25 23:15:19 +00:00
|
|
|
(await indexed.Values.PMap(Queue, UpdateTracker, async d =>
|
2020-02-08 19:25:49 +00:00
|
|
|
{
|
|
|
|
// Bit backwards, but we want to return null for
|
|
|
|
// all files we *want* installed. We return the files
|
|
|
|
// to remove from the install list.
|
2020-03-25 23:15:19 +00:00
|
|
|
var path = OutputFolder.Combine(d.To);
|
2020-09-11 01:22:07 +00:00
|
|
|
if (!existingfiles.Contains(path)) return null;
|
2020-02-08 19:25:49 +00:00
|
|
|
|
2020-03-25 23:15:19 +00:00
|
|
|
if (path.Size != d.Size) return null;
|
2020-09-11 01:22:07 +00:00
|
|
|
Status($"Optimizing {d.To}");
|
2020-02-08 19:25:49 +00:00
|
|
|
|
2020-05-12 21:31:53 +00:00
|
|
|
return await path.FileHashCachedAsync() == d.Hash ? d : null;
|
2020-02-08 19:25:49 +00:00
|
|
|
}))
|
2020-04-09 17:17:24 +00:00
|
|
|
.Do(d =>
|
|
|
|
{
|
|
|
|
if (d != null)
|
|
|
|
{
|
|
|
|
indexed.Remove(d.To);
|
|
|
|
}
|
|
|
|
});
|
2020-02-08 19:25:49 +00:00
|
|
|
|
2020-01-13 21:11:07 +00:00
|
|
|
UpdateTracker.NextStep("Updating ModList");
|
2019-11-18 05:21:24 +00:00
|
|
|
Utils.Log($"Optimized {ModList.Directives.Count} directives to {indexed.Count} required");
|
|
|
|
var requiredArchives = indexed.Values.OfType<FromArchive>()
|
2020-03-25 23:15:19 +00:00
|
|
|
.GroupBy(d => d.ArchiveHashPath.BaseHash)
|
|
|
|
.Select(d => d.Key)
|
2019-11-18 05:21:24 +00:00
|
|
|
.ToHashSet();
|
|
|
|
|
|
|
|
ModList.Archives = ModList.Archives.Where(a => requiredArchives.Contains(a.Hash)).ToList();
|
|
|
|
ModList.Directives = indexed.Values.ToList();
|
|
|
|
|
|
|
|
}
|
2019-11-16 13:22:40 +00:00
|
|
|
}
|
2019-11-20 23:39:03 +00:00
|
|
|
|
|
|
|
public class NotEnoughDiskSpaceException : Exception
|
|
|
|
{
|
|
|
|
public NotEnoughDiskSpaceException(string s) : base(s)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2019-11-16 13:22:40 +00:00
|
|
|
}
|