diff --git a/Wabbajack.Common/Json.cs b/Wabbajack.Common/Json.cs index 979a0fc8..69916c2d 100644 --- a/Wabbajack.Common/Json.cs +++ b/Wabbajack.Common/Json.cs @@ -86,8 +86,6 @@ namespace Wabbajack.Common throw new JsonException("Type deserialized into null"); return result; } - - private class HashJsonConverter : JsonConverter { diff --git a/Wabbajack.Lib/AInstaller.cs b/Wabbajack.Lib/AInstaller.cs index 497ffd33..fd3b6292 100644 --- a/Wabbajack.Lib/AInstaller.cs +++ b/Wabbajack.Lib/AInstaller.cs @@ -177,7 +177,7 @@ namespace Wabbajack.Lib }); Status("Unstaging files"); - onFinish(); + await onFinish(); // Now patch all the files from this archive await grouping.OfType() diff --git a/Wabbajack.Lib/MO2Compiler.cs b/Wabbajack.Lib/MO2Compiler.cs index 4ddc87ca..af0145eb 100644 --- a/Wabbajack.Lib/MO2Compiler.cs +++ b/Wabbajack.Lib/MO2Compiler.cs @@ -450,7 +450,7 @@ namespace Wabbajack.Lib private async Task BuildArchivePatches(Hash archiveSha, IEnumerable group, Dictionary absolutePaths) { - using var files = await VFS.StageWith(@group.Select(g => VFS.Index.FileForArchiveHashPath(g.ArchiveHashPath))); + await using var files = await VFS.StageWith(@group.Select(g => VFS.Index.FileForArchiveHashPath(g.ArchiveHashPath))); var byPath = files.GroupBy(f => string.Join("|", f.FilesInFullPath.Skip(1).Select(i => i.Name))) .ToDictionary(f => f.Key, f => f.First()); // Now Create the patches diff --git a/Wabbajack.Test/ACompilerTest.cs b/Wabbajack.Test/ACompilerTest.cs index c980b234..a48d50a4 100644 --- a/Wabbajack.Test/ACompilerTest.cs +++ b/Wabbajack.Test/ACompilerTest.cs @@ -28,7 +28,7 @@ namespace Wabbajack.Test public override void Dispose() { - utils.Dispose(); + utils.DisposeAsync().AsTask().Wait(); _unsub.Dispose(); base.Dispose(); } diff --git a/Wabbajack.Test/EndToEndTests.cs b/Wabbajack.Test/EndToEndTests.cs index 70caa696..9ad82fb8 100644 --- a/Wabbajack.Test/EndToEndTests.cs +++ b/Wabbajack.Test/EndToEndTests.cs @@ -39,6 +39,7 @@ namespace Wabbajack.Test { Queue.Dispose(); _unsub.Dispose(); + utils.DisposeAsync().AsTask().Wait(); base.Dispose(); } diff --git a/Wabbajack.Test/TestUtils.cs b/Wabbajack.Test/TestUtils.cs index 5efdb7a1..0081c560 100644 --- a/Wabbajack.Test/TestUtils.cs +++ b/Wabbajack.Test/TestUtils.cs @@ -14,7 +14,7 @@ using Path = Alphaleonis.Win32.Filesystem.Path; namespace Wabbajack.Test { - public class TestUtils : IDisposable + public class TestUtils : IAsyncDisposable { private static Random _rng = new Random(); public TestUtils() @@ -119,13 +119,14 @@ namespace Wabbajack.Test return arr; } - public void Dispose() + public async ValueTask DisposeAsync() { - var exts = new [] {".md", ".exe"}; - WorkingDirectory.Combine(ID).DeleteDirectory(); + var exts = new[] { ".md", ".exe" }; + await WorkingDirectory.Combine(ID).DeleteDirectory(); Profiles.Do(p => { - foreach (var ext in exts) { + foreach (var ext in exts) + { var path = Path.Combine(Directory.GetCurrentDirectory(), p + ext); if (File.Exists(path)) File.Delete(path); @@ -246,7 +247,5 @@ namespace Wabbajack.Test GenerateRandomFileData(fullPath, i); return fullPath; } - - } } diff --git a/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs b/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs index eca1b235..5059fbf8 100644 --- a/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs +++ b/Wabbajack.VirtualFileSystem.Test/VirtualFileSystemTests.cs @@ -9,7 +9,7 @@ using Xunit.Abstractions; namespace Wabbajack.VirtualFileSystem.Test { - public class VFSTests + public class VFSTests : IAsyncLifetime { private static readonly AbsolutePath VFS_TEST_DIR = "vfs_test_dir".ToPath().RelativeToEntryPoint(); private static readonly AbsolutePath TEST_ZIP = "test.zip".RelativeTo(VFS_TEST_DIR); @@ -18,18 +18,26 @@ namespace Wabbajack.VirtualFileSystem.Test private Context context; private readonly ITestOutputHelper _helper; - private WorkQueue Queue { get; } + private WorkQueue Queue { get; } = new WorkQueue(); public VFSTests(ITestOutputHelper helper) { _helper = helper; Utils.LogMessages.Subscribe(f => _helper.WriteLine(f.ShortDescription)); - VFS_TEST_DIR.DeleteDirectory(); - VFS_TEST_DIR.CreateDirectory(); - Queue = new WorkQueue(); context = new Context(Queue); } + public async Task InitializeAsync() + { + await VFS_TEST_DIR.DeleteDirectory(); + VFS_TEST_DIR.CreateDirectory(); + } + + public async Task DisposeAsync() + { + await VFS_TEST_DIR.DeleteDirectory(); + } + [Fact] public async Task FilesAreIndexed() { @@ -51,12 +59,11 @@ namespace Wabbajack.VirtualFileSystem.Test await context.IntegrateFromFile( "vfs_cache.bin".RelativeTo(VFS_TEST_DIR)); } - [Fact] public async Task ArchiveContentsAreIndexed() { await AddFile(ARCHIVE_TEST_TXT, "This is a test"); - ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); + await ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); await AddTestRoot(); var absPath = "test.zip".RelativeTo(VFS_TEST_DIR); @@ -78,7 +85,7 @@ namespace Wabbajack.VirtualFileSystem.Test public async Task DuplicateFileHashes() { await AddFile(ARCHIVE_TEST_TXT, "This is a test"); - ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); + await ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); await AddFile(TEST_TXT, "This is a test"); await AddTestRoot(); @@ -127,7 +134,7 @@ namespace Wabbajack.VirtualFileSystem.Test public async Task CanStageSimpleArchives() { await AddFile(ARCHIVE_TEST_TXT, "This is a test"); - ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); + await ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); await AddTestRoot(); var res = new FullPath(TEST_ZIP, new[] {(RelativePath)"test.txt"}); @@ -136,19 +143,19 @@ namespace Wabbajack.VirtualFileSystem.Test var cleanup = await context.Stage(new List {file}); Assert.Equal("This is a test", await file.StagedPath.ReadAllTextAsync()); - cleanup(); + await cleanup(); } [Fact] public async Task CanStageNestedArchives() { await AddFile(ARCHIVE_TEST_TXT, "This is a test"); - ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); + await ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); var inner_dir = @"archive\other\dir".RelativeTo(VFS_TEST_DIR); inner_dir.CreateDirectory(); TEST_ZIP.MoveTo( @"archive\other\dir\nested.zip".RelativeTo(VFS_TEST_DIR)); - ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); + await ZipUpFolder(ARCHIVE_TEST_TXT.Parent, TEST_ZIP); await AddTestRoot(); @@ -159,7 +166,7 @@ namespace Wabbajack.VirtualFileSystem.Test foreach (var file in files) Assert.Equal("This is a test", await file.StagedPath.ReadAllTextAsync()); - cleanup(); + await cleanup(); } private static async Task AddFile(AbsolutePath filename, string text) @@ -168,10 +175,10 @@ namespace Wabbajack.VirtualFileSystem.Test await filename.WriteAllTextAsync(text); } - private static void ZipUpFolder(AbsolutePath folder, AbsolutePath output) + private static async Task ZipUpFolder(AbsolutePath folder, AbsolutePath output) { ZipFile.CreateFromDirectory((string)folder, (string)output); - folder.DeleteDirectory(); + await folder.DeleteDirectory(); } } } diff --git a/Wabbajack.VirtualFileSystem/Context.cs b/Wabbajack.VirtualFileSystem/Context.cs index 2d42769b..5105d83d 100644 --- a/Wabbajack.VirtualFileSystem/Context.cs +++ b/Wabbajack.VirtualFileSystem/Context.cs @@ -195,7 +195,7 @@ namespace Wabbajack.VirtualFileSystem } } - public async Task Stage(IEnumerable files) + public async Task> Stage(IEnumerable files) { var grouped = files.SelectMany(f => f.FilesInFullPath) .Distinct() @@ -215,18 +215,18 @@ namespace Wabbajack.VirtualFileSystem file.StagedPath = file.RelativeName.RelativeTo(tmpPath); } - return () => + return async () => { - paths.Do(p => + foreach (var p in paths) { - p.DeleteDirectory(); - }); + await p.DeleteDirectory(); + } }; } - public async Task> StageWith(IEnumerable files) + public async Task> StageWith(IEnumerable files) { - return new DisposableList(await Stage(files), files); + return new AsyncDisposableList(await Stage(files), files); } @@ -275,7 +275,7 @@ namespace Wabbajack.VirtualFileSystem _knownFiles = new List(); } - + #endregion } @@ -294,6 +294,21 @@ namespace Wabbajack.VirtualFileSystem } } + public class AsyncDisposableList : List, IAsyncDisposable + { + private Func _unstage; + + public AsyncDisposableList(Func unstage, IEnumerable files) : base(files) + { + _unstage = unstage; + } + + public async ValueTask DisposeAsync() + { + await _unstage(); + } + } + public class IndexRoot { public static IndexRoot Empty = new IndexRoot();