From 2b0866b4f31abb6c0c9c07c5d5599b09d098f09a Mon Sep 17 00:00:00 2001 From: Justin Swanson Date: Mon, 27 Apr 2020 22:17:06 -0500 Subject: [PATCH] Adjusted TempFolder init concepts Wasn't being awaited in the App ctor. Swapped for a static factory that can be awaited to ensure initialization is complete --- Wabbajack.Common/Util/TempFolder.cs | 17 +++++++++++------ Wabbajack.Lib/Downloaders/YouTubeDownloader.cs | 2 +- Wabbajack.VirtualFileSystem/FileExtractor.cs | 10 +++++----- Wabbajack/App.xaml.cs | 1 - Wabbajack/Views/MainWindow.xaml.cs | 1 + 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Wabbajack.Common/Util/TempFolder.cs b/Wabbajack.Common/Util/TempFolder.cs index 0d66f842..9ce128d3 100644 --- a/Wabbajack.Common/Util/TempFolder.cs +++ b/Wabbajack.Common/Util/TempFolder.cs @@ -15,24 +15,28 @@ namespace Wabbajack.Common static TempFolder() { - _cleanTask = "tmp_files".RelativeTo(AbsolutePath.EntryPoint).DeleteDirectory(); + _cleanTask = Task.Run(() => "tmp_files".RelativeTo(AbsolutePath.EntryPoint).DeleteDirectory()); } - public static async Task EnsureInited() + public static void Init() { - Utils.Log("Cleaning temp files"); - await _cleanTask; + // Nothing to do, as work is done in static ctor } - public TempFolder(bool deleteAfter = true) + private TempFolder(bool deleteAfter = true) { - _cleanTask.Wait(); Dir = Path.Combine("tmp_files", Guid.NewGuid().ToString()).RelativeTo(AbsolutePath.EntryPoint); if (!Dir.Exists) Dir.CreateDirectory(); DeleteAfter = deleteAfter; } + public static async Task Create(bool deleteAfter = true) + { + await _cleanTask; + return new TempFolder(deleteAfter: deleteAfter); + } + public TempFolder(AbsolutePath dir, bool deleteAfter = true) { Dir = dir; @@ -42,6 +46,7 @@ namespace Wabbajack.Common } DeleteAfter = deleteAfter; } + public async ValueTask DisposeAsync() { Utils.Log($"Deleting {Dir}"); diff --git a/Wabbajack.Lib/Downloaders/YouTubeDownloader.cs b/Wabbajack.Lib/Downloaders/YouTubeDownloader.cs index c6f4436e..783bfbb7 100644 --- a/Wabbajack.Lib/Downloaders/YouTubeDownloader.cs +++ b/Wabbajack.Lib/Downloaders/YouTubeDownloader.cs @@ -102,7 +102,7 @@ namespace Wabbajack.Lib.Downloaders try { using var queue = new WorkQueue(); - await using var folder = new TempFolder(); + await using var folder = await TempFolder.Create(); folder.Dir.Combine("tracks").CreateDirectory(); var client = new YoutubeClient(Common.Http.ClientFactory.Client); var meta = await client.Videos.GetAsync(Key); diff --git a/Wabbajack.VirtualFileSystem/FileExtractor.cs b/Wabbajack.VirtualFileSystem/FileExtractor.cs index c9953a68..a1cdae92 100644 --- a/Wabbajack.VirtualFileSystem/FileExtractor.cs +++ b/Wabbajack.VirtualFileSystem/FileExtractor.cs @@ -25,7 +25,7 @@ namespace Wabbajack.VirtualFileSystem if (Consts.SupportedBSAs.Contains(source.Extension)) return await ExtractAllWithBSA(queue, source); else if (source.Extension == Consts.OMOD) - return ExtractAllWithOMOD(source); + return await ExtractAllWithOMOD(source); else if (source.Extension == Consts.EXE) return await ExtractAllExe(source); else @@ -47,7 +47,7 @@ namespace Wabbajack.VirtualFileSystem return await ExtractAllWith7Zip(source, null); } - var dest = new TempFolder(); + var dest = await TempFolder.Create(); Utils.Log($"Extracting {(string)source.FileName}"); var process = new ProcessHelper @@ -94,9 +94,9 @@ namespace Wabbajack.VirtualFileSystem } } - private static ExtractedFiles ExtractAllWithOMOD(AbsolutePath source) + private static async Task ExtractAllWithOMOD(AbsolutePath source) { - var dest = new TempFolder(); + var dest = await TempFolder.Create(); Utils.Log($"Extracting {(string)source.FileName}"); Framework.Settings.TempPath = (string)dest.Dir; @@ -128,7 +128,7 @@ namespace Wabbajack.VirtualFileSystem private static async Task ExtractAllWith7Zip(AbsolutePath source, IEnumerable onlyFiles) { TempFile tmpFile = null; - var dest = new TempFolder(); + var dest = await TempFolder.Create(); Utils.Log(new GenericInfo($"Extracting {(string)source.FileName}", $"The contents of {(string)source.FileName} are being extracted to {(string)source.FileName} using 7zip.exe")); var process = new ProcessHelper diff --git a/Wabbajack/App.xaml.cs b/Wabbajack/App.xaml.cs index 4c542895..b2986871 100644 --- a/Wabbajack/App.xaml.cs +++ b/Wabbajack/App.xaml.cs @@ -20,7 +20,6 @@ namespace Wabbajack { public App() { - TempFolder.EnsureInited(); CLIOld.ParseOptions(Environment.GetCommandLineArgs()); if (CLIArguments.Help) CLIOld.DisplayHelpText(); diff --git a/Wabbajack/Views/MainWindow.xaml.cs b/Wabbajack/Views/MainWindow.xaml.cs index e2a306e6..c6572535 100644 --- a/Wabbajack/Views/MainWindow.xaml.cs +++ b/Wabbajack/Views/MainWindow.xaml.cs @@ -23,6 +23,7 @@ namespace Wabbajack public MainWindow() { + TempFolder.Init(); Helpers.Init(); // Wire any unhandled crashing exceptions to log before exiting AppDomain.CurrentDomain.UnhandledException += (sender, e) =>