diff --git a/Wabbajack.VirtualFileSystem/Context.cs b/Wabbajack.VirtualFileSystem/Context.cs index 2d24b797..b30c48e7 100644 --- a/Wabbajack.VirtualFileSystem/Context.cs +++ b/Wabbajack.VirtualFileSystem/Context.cs @@ -345,16 +345,27 @@ namespace Wabbajack.VirtualFileSystem await _unstage(); } } + + public static class EmptyLookup + { + private static readonly ILookup _instance + = Enumerable.Empty().ToLookup(x => default(TKey)); + + public static ILookup Instance + { + get { return _instance; } + } + } public class IndexRoot { public static IndexRoot Empty = new IndexRoot(); - public IndexRoot(ImmutableList aFiles, - Dictionary byFullPath, - ImmutableDictionary> byHash, - ImmutableDictionary byRoot, - ImmutableDictionary> byName) + public IndexRoot(IReadOnlyList aFiles, + IDictionary byFullPath, + ILookup byHash, + IDictionary byRoot, + ILookup byName) { AllFiles = aFiles; ByFullPath = byFullPath; @@ -367,17 +378,17 @@ namespace Wabbajack.VirtualFileSystem { AllFiles = ImmutableList.Empty; ByFullPath = new Dictionary(); - ByHash = ImmutableDictionary>.Empty; - ByRootPath = ImmutableDictionary.Empty; - ByName = ImmutableDictionary>.Empty; + ByHash = EmptyLookup.Instance; + ByRootPath = new Dictionary(); + ByName = EmptyLookup.Instance; } - public ImmutableList AllFiles { get; } - public Dictionary ByFullPath { get; } - public ImmutableDictionary> ByHash { get; } - public ImmutableDictionary> ByName { get; set; } - public ImmutableDictionary ByRootPath { get; } + public IReadOnlyList AllFiles { get; } + public IDictionary ByFullPath { get; } + public ILookup ByHash { get; } + public ILookup ByName { get; set; } + public IDictionary ByRootPath { get; } public async Task Integrate(ICollection files) { @@ -385,19 +396,19 @@ namespace Wabbajack.VirtualFileSystem var allFiles = AllFiles.Concat(files) .OrderByDescending(f => f.LastModified) .GroupBy(f => f.FullPath).Select(g => g.Last()) - .ToImmutableList(); + .ToList(); var byFullPath = Task.Run(() => allFiles.SelectMany(f => f.ThisAndAllChildren) .ToDictionary(f => f.FullPath)); var byHash = Task.Run(() => allFiles.SelectMany(f => f.ThisAndAllChildren) .Where(f => f.Hash != Hash.Empty) - .ToGroupedImmutableDictionary(f => f.Hash)); + .ToLookup(f => f.Hash)); var byName = Task.Run(() => allFiles.SelectMany(f => f.ThisAndAllChildren) - .ToGroupedImmutableDictionary(f => f.Name)); + .ToLookup(f => f.Name)); - var byRootPath = Task.Run(() => allFiles.ToImmutableDictionary(f => f.AbsoluteName)); + var byRootPath = Task.Run(() => allFiles.ToDictionary(f => f.AbsoluteName)); var result = new IndexRoot(allFiles, await byFullPath, diff --git a/Wabbajack.VirtualFileSystem/VirtualFile.cs b/Wabbajack.VirtualFileSystem/VirtualFile.cs index c6a6f933..0dcc1459 100644 --- a/Wabbajack.VirtualFileSystem/VirtualFile.cs +++ b/Wabbajack.VirtualFileSystem/VirtualFile.cs @@ -235,7 +235,11 @@ namespace Wabbajack.VirtualFileSystem self.ExtendedHashes = await ExtendedHashes.FromStream(stream); // Can't extract, so return - if (!sig.HasValue || !FileExtractor2.ExtractableExtensions.Contains(relPath.FileName.Extension)) return self; + if (!sig.HasValue || !FileExtractor2.ExtractableExtensions.Contains(relPath.FileName.Extension)) + { + await WriteToCache(self); + return self; + } try { @@ -256,11 +260,16 @@ namespace Wabbajack.VirtualFileSystem throw; } + await WriteToCache(self); + return self; + } + + private static async Task WriteToCache(VirtualFile self) + { await using var ms = new MemoryStream(); self.ToIndexedVirtualFile().Write(ms); ms.Position = 0; await InsertIntoVFSCache(self.Hash, ms); - return self; } private static async Task InsertIntoVFSCache(Hash hash, MemoryStream data)