wabbajack/Wabbajack.Installer/AInstaller.cs

559 lines
22 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Wabbajack.Common;
using Wabbajack.Downloaders;
2021-10-13 03:59:54 +00:00
using Wabbajack.Downloaders.GameFile;
2021-09-27 12:42:46 +00:00
using Wabbajack.DTOs;
using Wabbajack.DTOs.Directives;
using Wabbajack.DTOs.DownloadStates;
using Wabbajack.DTOs.JsonConverters;
using Wabbajack.Hashing.PHash;
using Wabbajack.Hashing.xxHash64;
using Wabbajack.Installer.Utilities;
using Wabbajack.Networking.WabbajackClientApi;
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
using Wabbajack.RateLimiter;
using Wabbajack.VFS;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Installer;
public record StatusUpdate(string StatusCategory, string StatusText, Percent StepsProgress, Percent StepProgress)
2021-10-23 16:51:17 +00:00
{
}
2021-12-27 23:15:30 +00:00
public interface IInstaller
{
Task<bool> Begin(CancellationToken token);
}
2021-10-23 16:51:17 +00:00
public abstract class AInstaller<T>
where T : AInstaller<T>
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
private const int _limitMS = 100;
2022-05-17 22:32:53 +00:00
public static RelativePath BSACreationDir = "TEMP_BSA_FILES".ToRelativePath();
2021-10-23 16:51:17 +00:00
private static readonly Regex NoDeleteRegex = new(@"(?i)[\\\/]\[NoDelete\]", RegexOptions.Compiled);
protected readonly InstallerConfiguration _configuration;
protected readonly DownloadDispatcher _downloadDispatcher;
private readonly FileExtractor.FileExtractor _extractor;
2022-05-17 22:32:53 +00:00
protected readonly FileHashCache FileHashCache;
2021-10-23 16:51:17 +00:00
protected readonly IGameLocator _gameLocator;
private readonly DTOSerializer _jsonSerializer;
protected readonly ILogger<T> _logger;
protected readonly TemporaryFileManager _manager;
protected readonly ParallelOptions _parallelOptions;
private readonly Context _vfs;
protected readonly Client _wjClient;
private int _currentStep;
private long _currentStepProgress;
2021-11-02 13:40:59 +00:00
protected long MaxStepProgress { get; set; }
private string _statusCategory;
2021-10-23 16:51:17 +00:00
private string _statusText;
private readonly Stopwatch _updateStopWatch = new();
2021-11-02 13:40:59 +00:00
public Action<StatusUpdate>? OnStatusUpdate;
private readonly IResource<IInstaller> _limiter;
private Func<long, string> _statusFormatter = x => x.ToString();
2021-10-23 16:51:17 +00:00
public AInstaller(ILogger<T> logger, InstallerConfiguration config, IGameLocator gameLocator,
FileExtractor.FileExtractor extractor,
DTOSerializer jsonSerializer, Context vfs, FileHashCache fileHashCache,
DownloadDispatcher downloadDispatcher,
ParallelOptions parallelOptions,
IResource<IInstaller> limiter,
Client wjClient)
2021-09-27 12:42:46 +00:00
{
_limiter = limiter;
2021-10-23 16:51:17 +00:00
_manager = new TemporaryFileManager(config.Install.Combine("__temp__"));
ExtractedModlistFolder = _manager.CreateFolder();
_configuration = config;
_logger = logger;
_extractor = extractor;
_jsonSerializer = jsonSerializer;
_vfs = vfs;
2022-05-17 22:32:53 +00:00
FileHashCache = fileHashCache;
2021-10-23 16:51:17 +00:00
_downloadDispatcher = downloadDispatcher;
_parallelOptions = parallelOptions;
_gameLocator = gameLocator;
_wjClient = wjClient;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
protected long MaxSteps { get; set; }
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public Dictionary<Hash, AbsolutePath> HashedArchives { get; set; } = new();
public bool UseCompression { get; set; }
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public TemporaryPath ExtractedModlistFolder { get; set; }
2021-10-21 03:18:15 +00:00
2021-10-23 16:51:17 +00:00
public ModList ModList => _configuration.ModList;
2021-09-27 12:42:46 +00:00
public void NextStep(string statusCategory, string statusText, long maxStepProgress, Func<long, string>? formatter = null)
2021-10-23 16:51:17 +00:00
{
_updateStopWatch.Restart();
2021-11-02 13:40:59 +00:00
MaxStepProgress = maxStepProgress;
2021-10-23 16:51:17 +00:00
_currentStep += 1;
_statusText = statusText;
_statusCategory = statusCategory;
_statusFormatter = formatter ?? (x => x.ToString());
2021-10-23 16:51:17 +00:00
_logger.LogInformation("Next Step: {Step}", statusText);
OnStatusUpdate?.Invoke(new StatusUpdate(statusCategory, statusText,
2021-11-02 13:40:59 +00:00
Percent.FactoryPutInRange(_currentStep, MaxSteps), Percent.Zero));
2021-10-23 16:51:17 +00:00
}
2021-09-27 12:42:46 +00:00
2021-11-02 13:40:59 +00:00
public void UpdateProgress(long stepProgress)
2021-10-23 16:51:17 +00:00
{
Interlocked.Add(ref _currentStepProgress, stepProgress);
2021-09-27 12:42:46 +00:00
OnStatusUpdate?.Invoke(new StatusUpdate(_statusCategory, $"[{_currentStep}/{MaxSteps}] {_statusText} ({_statusFormatter(_currentStepProgress)}/{_statusFormatter(MaxStepProgress)})",
2022-05-17 22:32:53 +00:00
Percent.FactoryPutInRange(_currentStep, MaxSteps),
Percent.FactoryPutInRange(_currentStepProgress, MaxStepProgress)));
2021-10-23 16:51:17 +00:00
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public abstract Task<bool> Begin(CancellationToken token);
2021-09-27 12:42:46 +00:00
2021-11-03 05:03:41 +00:00
protected async Task ExtractModlist(CancellationToken token)
2021-10-23 16:51:17 +00:00
{
ExtractedModlistFolder = _manager.CreateFolder();
2021-11-03 05:03:41 +00:00
await using var stream = _configuration.ModlistArchive.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
using var archive = new ZipArchive(stream, ZipArchiveMode.Read);
2022-05-17 22:32:53 +00:00
NextStep(Consts.StepPreparing, "Extracting Modlist", archive.Entries.Count);
2021-11-03 05:03:41 +00:00
foreach (var entry in archive.Entries)
{
var path = entry.FullName.ToRelativePath().RelativeTo(ExtractedModlistFolder);
path.Parent.CreateDirectory();
await using var of = path.Open(FileMode.Create, FileAccess.Write, FileShare.None);
await entry.Open().CopyToAsync(of, token);
UpdateProgress(1);
}
2021-10-23 16:51:17 +00:00
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public async Task<byte[]> LoadBytesFromPath(RelativePath path)
{
var fullPath = ExtractedModlistFolder.Path.Combine(path);
if (!fullPath.FileExists())
throw new Exception($"Cannot load inlined data {path} file does not exist");
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
return await fullPath.ReadAllBytesAsync();
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public async Task<Stream> InlinedFileStream(RelativePath inlinedFile)
{
var fullPath = ExtractedModlistFolder.Path.Combine(inlinedFile);
return fullPath.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static async Task<ModList> LoadFromFile(DTOSerializer serializer, AbsolutePath path)
{
await using var fs = path.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
using var ar = new ZipArchive(fs, ZipArchiveMode.Read);
var entry = ar.GetEntry("modlist");
if (entry == null)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
entry = ar.GetEntry("modlist.json");
2021-09-27 12:42:46 +00:00
if (entry == null)
2021-10-23 16:51:17 +00:00
throw new Exception("Invalid Wabbajack Installer");
await using var e = entry.Open();
return (await serializer.DeserializeAsync<ModList>(e))!;
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
await using (var e = entry.Open())
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
return (await serializer.DeserializeAsync<ModList>(e))!;
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
}
2021-09-27 12:42:46 +00:00
2021-12-30 23:55:41 +00:00
public static async Task<Stream> ModListImageStream(AbsolutePath path)
{
await using var fs = path.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
using var ar = new ZipArchive(fs, ZipArchiveMode.Read);
var entry = ar.GetEntry("modlist-image.png");
if (entry == null)
throw new InvalidDataException("No modlist image found");
return new MemoryStream(await entry.Open().ReadAllAsync());
}
2021-10-23 16:51:17 +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>
protected async Task PrimeVFS()
{
2022-05-17 22:32:53 +00:00
NextStep(Consts.StepPreparing, "Priming VFS", 0);
2021-10-23 16:51:17 +00:00
_vfs.AddKnown(_configuration.ModList.Directives.OfType<FromArchive>().Select(d => d.ArchiveHashPath),
HashedArchives);
await _vfs.BackfillMissing();
}
2021-09-27 12:42:46 +00:00
2021-11-02 13:40:59 +00:00
public async Task BuildFolderStructure()
2022-05-17 22:32:53 +00:00
{
NextStep(Consts.StepPreparing, "Building Folder Structure", 0);
2021-10-23 16:51:17 +00:00
_logger.LogInformation("Building Folder Structure");
ModList.Directives
.Where(d => d.To.Depth > 1)
.Select(d => _configuration.Install.Combine(d.To.Parent))
.Distinct()
.Do(f => f.CreateDirectory());
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public async Task InstallArchives(CancellationToken token)
{
NextStep(Consts.StepInstalling, "Installing files", ModList.Directives.Sum(d => d.Size), x => x.ToFileSizeString());
2021-10-23 16:51:17 +00:00
var grouped = ModList.Directives
.OfType<FromArchive>()
.Select(a => new {VF = _vfs.Index.FileForArchiveHashPath(a.ArchiveHashPath), Directive = a})
.GroupBy(a => a.VF)
.ToDictionary(a => a.Key);
if (grouped.Count == 0) return;
2021-09-27 12:42:46 +00:00
2022-05-17 22:32:53 +00:00
2021-10-23 16:51:17 +00:00
await _vfs.Extract(grouped.Keys.ToHashSet(), async (vf, sf) =>
{
var directives = grouped[vf];
2022-05-17 22:32:53 +00:00
using var job = await _limiter.Begin($"Installing files from {vf.Name}", directives.Sum(f => f.VF.Size),
token);
foreach (var directive in directives)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
var file = directive.Directive;
2021-11-02 13:40:59 +00:00
UpdateProgress(file.Size);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
switch (file)
{
case PatchedFromArchive pfa:
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
await using var s = await sf.GetStream();
s.Position = 0;
await using var patchDataStream = await InlinedFileStream(pfa.PatchID);
var toFile = file.To.RelativeTo(_configuration.Install);
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
await using var os = toFile.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.None);
await BinaryPatching.ApplyPatch(s, patchDataStream, os);
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
}
break;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
case TransformedTexture tt:
{
await using var s = await sf.GetStream();
await using var of = directive.Directive.To.RelativeTo(_configuration.Install)
.Open(FileMode.Create, FileAccess.Write);
await ImageLoader.Recompress(s, tt.ImageState.Width, tt.ImageState.Height, tt.ImageState.Format,
of, token);
}
break;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
case FromArchive _:
if (grouped[vf].Count() == 1)
{
await sf.Move(directive.Directive.To.RelativeTo(_configuration.Install), token);
}
else
2021-09-27 12:42:46 +00:00
{
await using var s = await sf.GetStream();
2021-10-23 16:51:17 +00:00
await directive.Directive.To.RelativeTo(_configuration.Install)
.WriteAllAsync(s, token, false);
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
break;
default:
throw new Exception($"No handler for {directive}");
2021-09-27 12:42:46 +00:00
}
2022-05-17 22:32:53 +00:00
await job.Report((int) directive.VF.Size, token);
2021-10-23 16:51:17 +00:00
}
}, token);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public async Task DownloadArchives(CancellationToken token)
{
var missing = ModList.Archives.Where(a => !HashedArchives.ContainsKey(a.Hash)).ToList();
_logger.LogInformation("Missing {count} archives", missing.Count);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var dispatchers = missing.Select(m => _downloadDispatcher.Downloader(m))
.Distinct()
.ToList();
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
await Task.WhenAll(dispatchers.Select(d => d.Prepare()));
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
_logger.LogInformation("Downloading validation data");
var validationData = await _wjClient.LoadDownloadAllowList();
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
_logger.LogInformation("Validating Archives");
foreach (var archive in missing.Where(archive =>
2022-05-17 22:32:53 +00:00
!_downloadDispatcher.Downloader(archive).IsAllowed(validationData, archive.State)))
2021-10-23 16:51:17 +00:00
{
_logger.LogCritical("File {primaryKeyString} failed validation", archive.State.PrimaryKeyString);
return;
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
_logger.LogInformation("Downloading missing archives");
await DownloadMissingArchives(missing, token);
}
public async Task DownloadMissingArchives(List<Archive> missing, CancellationToken token, bool download = true)
{
_logger.LogInformation("Downloading {Count} archives", missing.Count.ToString());
NextStep(Consts.StepDownloading, "Downloading files", missing.Count);
2021-10-23 16:51:17 +00:00
if (download)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
var result = SendDownloadMetrics(missing);
foreach (var a in missing.Where(a => a.State is Manual))
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
var outputPath = _configuration.Downloads.Combine(a.Name);
2022-05-16 23:18:10 +00:00
await DownloadArchive(a, true, token, outputPath);
UpdateProgress(1);
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
}
2021-10-23 16:51:17 +00:00
await missing
.OrderBy(a => a.Size)
.Where(a => a.State is not Manual)
.PDoAll(async archive =>
{
_logger.LogInformation("Downloading {Archive}", archive.Name);
2021-10-23 16:51:17 +00:00
var outputPath = _configuration.Downloads.Combine(archive.Name);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
if (download)
if (outputPath.FileExists())
{
var origName = Path.GetFileNameWithoutExtension(archive.Name);
var ext = Path.GetExtension(archive.Name);
var uniqueKey = archive.State.PrimaryKeyString.StringSha256Hex();
outputPath = _configuration.Downloads.Combine(origName + "_" + uniqueKey + "_" + ext);
outputPath.Delete();
}
2021-09-27 12:42:46 +00:00
2022-05-16 23:18:10 +00:00
var hash = await DownloadArchive(archive, download, token, outputPath);
2021-11-02 13:40:59 +00:00
UpdateProgress(1);
2021-10-23 16:51:17 +00:00
});
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
private async Task SendDownloadMetrics(List<Archive> missing)
{
var grouped = missing.GroupBy(m => m.State.GetType());
foreach (var group in grouped)
await _wjClient.SendMetric($"downloading_{group.Key.FullName!.Split(".").Last().Split("+").First()}",
group.Sum(g => g.Size).ToString());
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public async Task<bool> DownloadArchive(Archive archive, bool download, CancellationToken token,
AbsolutePath? destination = null)
{
try
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
destination ??= _configuration.Downloads.Combine(archive.Name);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var (result, hash) =
await _downloadDispatcher.DownloadWithPossibleUpgrade(archive, destination.Value, token);
2021-09-27 12:42:46 +00:00
2022-05-16 23:18:10 +00:00
if (hash != archive.Hash)
{
2022-05-17 22:32:53 +00:00
_logger.LogError("Downloaded hash {Downloaded} does not match expected hash: {Expected}", hash,
archive.Hash);
2022-05-16 23:18:10 +00:00
if (destination!.Value.FileExists())
{
destination!.Value.Delete();
}
return false;
}
2022-05-17 22:32:53 +00:00
2021-10-23 16:51:17 +00:00
if (hash != default)
2022-05-17 22:32:53 +00:00
FileHashCache.FileHashWriteCache(destination.Value, hash);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
if (result == DownloadResult.Update)
await destination.Value.MoveToAsync(destination.Value.Parent.Combine(archive.Hash.ToHex()), true,
token);
}
catch (Exception ex)
{
_logger.LogError(ex, "Download error for file {name}", archive.Name);
2021-09-27 12:42:46 +00:00
return false;
}
2021-10-23 16:51:17 +00:00
return false;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public async Task HashArchives(CancellationToken token)
{
NextStep(Consts.StepHashing, "Hashing Archives", 0);
2021-10-23 16:51:17 +00:00
_logger.LogInformation("Looking for files to hash");
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var allFiles = _configuration.Downloads.EnumerateFiles()
.Concat(_gameLocator.GameLocation(_configuration.Game).EnumerateFiles())
.ToList();
2022-05-17 22:32:53 +00:00
2021-10-23 16:51:17 +00:00
var hashDict = allFiles.GroupBy(f => f.Size()).ToDictionary(g => g.Key);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var toHash = ModList.Archives.Where(a => hashDict.ContainsKey(a.Size))
.SelectMany(a => hashDict[a.Size]).ToList();
2021-09-27 12:42:46 +00:00
2021-11-02 13:40:59 +00:00
MaxStepProgress = toHash.Count;
2021-10-23 16:51:17 +00:00
_logger.LogInformation("Found {count} total files, {hashedCount} matching filesize", allFiles.Count,
toHash.Count);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var hashResults = await
toHash
2021-11-02 13:40:59 +00:00
.PMapAll(async e =>
{
UpdateProgress(1);
2022-05-17 22:32:53 +00:00
return (await FileHashCache.FileHashCachedAsync(e, token), e);
2021-11-02 13:40:59 +00:00
})
2021-10-23 16:51:17 +00:00
.ToList();
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
HashedArchives = hashResults
.OrderByDescending(e => e.Item2.LastModified())
.GroupBy(e => e.Item1)
.Select(e => e.First())
.Where(x => x.Item1 != default)
.ToDictionary(kv => kv.Item1, kv => kv.e);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
/// <summary>
/// The user may already have some files in the _configuration.Install. If so we can go through these and
/// figure out which need to be updated, deleted, or left alone
/// </summary>
protected async Task OptimizeModlist(CancellationToken token)
{
_logger.LogInformation("Optimizing ModList directives");
2022-05-17 22:32:53 +00:00
var indexed = ModList.Directives.ToDictionary(d => d.To);
2021-09-27 12:42:46 +00:00
2022-05-17 22:32:53 +00:00
var bsasToBuild = await ModList.Directives
.OfType<CreateBSA>()
.PMapAll(async b =>
{
var file = _configuration.Install.Combine(b.To);
if (!file.FileExists())
return (true, b);
return (b.Hash != await FileHashCache.FileHashCachedAsync(file, token), b);
})
.ToArray();
2021-09-27 12:42:46 +00:00
2022-05-17 22:32:53 +00:00
var bsasToNotBuild = bsasToBuild
.Where(b => b.Item1 == false).Select(t => t.b.TempID).ToHashSet();
var bsaPathsToNotBuild = bsasToBuild
.Where(b => b.Item1 == false).Select(t => t.b.To.RelativeTo(_configuration.Install))
.ToHashSet();
indexed = indexed.Values
.Where(d =>
{
return d switch
{
CreateBSA bsa => !bsasToNotBuild.Contains(bsa.TempID),
FromArchive a when a.To.StartsWith($"{BSACreationDir}") => !bsasToNotBuild.Any(b =>
a.To.RelativeTo(_configuration.Install).InFolder(_configuration.Install.Combine(BSACreationDir, b))),
_ => true
};
}).ToDictionary(d => d.To);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var profileFolder = _configuration.Install.Combine("profiles");
var savePath = (RelativePath) "saves";
2021-09-27 12:42:46 +00:00
2022-05-17 22:32:53 +00:00
NextStep(Consts.StepPreparing, "Looking for files to delete", 0);
await _configuration.Install.EnumerateFiles()
2021-10-23 16:51:17 +00:00
.PDoAll(async f =>
{
var relativeTo = f.RelativeTo(_configuration.Install);
if (indexed.ContainsKey(relativeTo) || f.InFolder(_configuration.Downloads))
return;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
if (f.InFolder(profileFolder) && f.Parent.FileName == savePath) return;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
if (NoDeleteRegex.IsMatch(f.ToString()))
return;
2021-09-27 12:42:46 +00:00
2022-05-17 22:32:53 +00:00
if (bsaPathsToNotBuild.Contains(f))
return;
_logger.LogInformation("Deleting {RelativePath} it's not part of this ModList", relativeTo);
2021-10-23 16:51:17 +00:00
f.Delete();
});
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
_logger.LogInformation("Cleaning empty folders");
2022-05-17 22:32:53 +00:00
var expectedFolders = indexed.Keys
.Select(f => f.RelativeTo(_configuration.Install))
// We ignore the last part of the path, so we need a dummy file name
.Append(_configuration.Downloads.Combine("_"))
.Where(f => f.InFolder(_configuration.Install))
.SelectMany(path =>
{
// Get all the folders and all the folder parents
// so for foo\bar\baz\qux.txt this emits ["foo", "foo\\bar", "foo\\bar\\baz"]
var split = ((string) path.RelativeTo(_configuration.Install)).Split('\\');
return Enumerable.Range(1, split.Length - 1).Select(t => string.Join("\\", split.Take(t)));
})
2021-10-23 16:51:17 +00:00
.Distinct()
.Select(p => _configuration.Install.Combine(p))
.ToHashSet();
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
try
{
2022-05-17 22:32:53 +00:00
var toDelete = _configuration.Install.EnumerateDirectories(true)
2021-10-23 16:51:17 +00:00
.Where(p => !expectedFolders.Contains(p))
.OrderByDescending(p => p.ToString().Length)
.ToList();
2022-05-17 22:32:53 +00:00
foreach (var dir in toDelete)
{
dir.DeleteDirectory(dontDeleteIfNotEmpty: true);
}
2021-10-23 16:51:17 +00:00
}
catch (Exception)
{
// ignored because it's not worth throwing a fit over
2022-05-17 22:32:53 +00:00
_logger.LogInformation("Error when trying to clean empty folders. This doesn't really matter.");
2021-10-23 16:51:17 +00:00
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var existingfiles = _configuration.Install.EnumerateFiles().ToHashSet();
2021-09-27 12:42:46 +00:00
2022-05-17 22:32:53 +00:00
NextStep(Consts.StepPreparing, "Looking for unmodified files", 0);
2021-10-23 16:51:17 +00:00
await indexed.Values.PMapAll<Directive, Directive?>(async d =>
{
// Bit backwards, but we want to return null for
// all files we *want* installed. We return the files
// to remove from the install list.
var path = _configuration.Install.Combine(d.To);
if (!existingfiles.Contains(path)) return null;
2022-05-17 22:32:53 +00:00
return await FileHashCache.FileHashCachedAsync(path, token) == d.Hash ? d : null;
2021-10-23 16:51:17 +00:00
})
.Do(d =>
{
2022-05-17 22:32:53 +00:00
if (d != null)
{
indexed.Remove(d.To);
}
2021-10-23 16:51:17 +00:00
});
2022-05-17 22:32:53 +00:00
NextStep(Consts.StepPreparing, "Updating ModList", 0);
_logger.LogInformation("Optimized {From} directives to {To} required", ModList.Directives.Length, indexed.Count);
2021-10-23 16:51:17 +00:00
var requiredArchives = indexed.Values.OfType<FromArchive>()
.GroupBy(d => d.ArchiveHashPath.Hash)
.Select(d => d.Key)
.ToHashSet();
ModList.Archives = ModList.Archives.Where(a => requiredArchives.Contains(a.Hash)).ToArray();
ModList.Directives = indexed.Values.ToArray();
2021-09-27 12:42:46 +00:00
}
}