From 140090cfc4a66c4bd02f3475e1bbbdee50846bb5 Mon Sep 17 00:00:00 2001 From: Halgari Date: Fri, 7 Oct 2022 15:02:16 -0600 Subject: [PATCH] Fix more warnings --- Wabbajack.Compiler.Test/ModListHarness.cs | 4 +-- Wabbajack.Compiler.Test/SanityTests.cs | 2 +- Wabbajack.Paths.IO/AbsolutePathExtensions.cs | 11 ++++---- Wabbajack.Paths.IO/TemporaryFileManager.cs | 28 ++++++++++++++++++-- Wabbajack.Paths.IO/TemporaryPath.cs | 3 ++- 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/Wabbajack.Compiler.Test/ModListHarness.cs b/Wabbajack.Compiler.Test/ModListHarness.cs index 262302c1..6ce4adc1 100644 --- a/Wabbajack.Compiler.Test/ModListHarness.cs +++ b/Wabbajack.Compiler.Test/ModListHarness.cs @@ -133,7 +133,7 @@ public class ModListHarness public async Task AddManualDownload(AbsolutePath path) { var toPath = path.FileName.RelativeTo(_downloadPath); - await path.CopyToAsync(toPath, true, CancellationToken.None); + await path.CopyToAsync(toPath, CancellationToken.None); await toPath.WithExtension(Ext.Meta) .WriteAllLinesAsync(new[] {"[General]", $"manualURL={path.FileName}"}, CancellationToken.None); @@ -170,7 +170,7 @@ public record Mod(RelativePath Name, AbsolutePath FullPath, ModListHarness Harne public async Task AddFile(AbsolutePath src) { var dest = FullPath.Combine(src.FileName); - await src.CopyToAsync(dest, true, CancellationToken.None); + await src.CopyToAsync(dest, CancellationToken.None); return dest; } diff --git a/Wabbajack.Compiler.Test/SanityTests.cs b/Wabbajack.Compiler.Test/SanityTests.cs index 5eda994b..84cbd1a0 100644 --- a/Wabbajack.Compiler.Test/SanityTests.cs +++ b/Wabbajack.Compiler.Test/SanityTests.cs @@ -146,7 +146,7 @@ public class CompilerSanityTests : IAsyncLifetime { var newPath = file.RelativeTo(_mod.FullPath).RelativeTo(_mod.FullPath.Combine("duplicates")); newPath.Parent.CreateDirectory(); - await file.CopyToAsync(newPath, true, CancellationToken.None); + await file.CopyToAsync(newPath, CancellationToken.None); } await CompileAndValidate(5); diff --git a/Wabbajack.Paths.IO/AbsolutePathExtensions.cs b/Wabbajack.Paths.IO/AbsolutePathExtensions.cs index 6ef6ac4b..81bd6848 100644 --- a/Wabbajack.Paths.IO/AbsolutePathExtensions.cs +++ b/Wabbajack.Paths.IO/AbsolutePathExtensions.cs @@ -28,7 +28,7 @@ public static class AbsolutePathExtensions { File.Delete(path); } - catch (UnauthorizedAccessException ex) + catch (UnauthorizedAccessException) { var fi = new FileInfo(path); if (fi.IsReadOnly) @@ -192,7 +192,7 @@ public static class AbsolutePathExtensions File.Move(srcStr, destStr, overwrite); return; } - catch (Exception ex) + catch (Exception) { if (retries > 10) throw; @@ -202,11 +202,12 @@ public static class AbsolutePathExtensions } } - public static async ValueTask CopyToAsync(this AbsolutePath src, AbsolutePath dest, bool overwrite, + public static async ValueTask CopyToAsync(this AbsolutePath src, AbsolutePath dest, CancellationToken token) { - // TODO: Make this async - File.Copy(src.ToString(), dest.ToString(), overwrite); + await using var inf = src.Open(FileMode.Open, FileAccess.Read, FileShare.Read); + await using var ouf = dest.Open(FileMode.Create, FileAccess.Write, FileShare.Read); + await inf.CopyToAsync(ouf, token); } public static void WriteAllText(this AbsolutePath file, string str) diff --git a/Wabbajack.Paths.IO/TemporaryFileManager.cs b/Wabbajack.Paths.IO/TemporaryFileManager.cs index 5ca27c82..f9801d76 100644 --- a/Wabbajack.Paths.IO/TemporaryFileManager.cs +++ b/Wabbajack.Paths.IO/TemporaryFileManager.cs @@ -1,10 +1,11 @@ using System; using System.IO; using System.Threading; +using System.Threading.Tasks; namespace Wabbajack.Paths.IO; -public class TemporaryFileManager : IDisposable +public class TemporaryFileManager : IDisposable, IAsyncDisposable { private readonly AbsolutePath _basePath; private readonly bool _deleteOnDispose; @@ -24,6 +25,7 @@ public class TemporaryFileManager : IDisposable { if (!_deleteOnDispose) return; for (var retries = 0; retries < 10; retries++) + { try { if (!_basePath.DirectoryExists()) @@ -31,10 +33,31 @@ public class TemporaryFileManager : IDisposable _basePath.DeleteDirectory(); return; } - catch (IOException ex) + catch (IOException) { Thread.Sleep(1000); } + } + } + + + public async ValueTask DisposeAsync() + { + if (!_deleteOnDispose) return; + for (var retries = 0; retries < 10; retries++) + { + try + { + if (!_basePath.DirectoryExists()) + return; + _basePath.DeleteDirectory(); + return; + } + catch (IOException) + { + await Task.Delay(1000); + } + } } public TemporaryPath CreateFile(Extension? ext = default, bool deleteOnDispose = true) @@ -51,4 +74,5 @@ public class TemporaryFileManager : IDisposable path.CreateDirectory(); return new TemporaryPath(path); } + } \ No newline at end of file diff --git a/Wabbajack.Paths.IO/TemporaryPath.cs b/Wabbajack.Paths.IO/TemporaryPath.cs index 6eae73dd..ee9ca70e 100644 --- a/Wabbajack.Paths.IO/TemporaryPath.cs +++ b/Wabbajack.Paths.IO/TemporaryPath.cs @@ -27,8 +27,9 @@ public struct TemporaryPath : IDisposable, IAsyncDisposable return tp.Path; } - public async ValueTask DisposeAsync() + public ValueTask DisposeAsync() { Path.Delete(); + return ValueTask.CompletedTask; } } \ No newline at end of file