From cfd2b7b7aaa77ad9e13977b5fdb14d368b3ae33e Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Mon, 20 Jun 2022 22:36:14 -0600 Subject: [PATCH 1/4] Fixed rare bug that would cause a hang if an error occured in the download loop --- Source/AaxDecrypter/AaxDecrypter.csproj | 2 +- Source/AaxDecrypter/NetworkFileStream.cs | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Source/AaxDecrypter/AaxDecrypter.csproj b/Source/AaxDecrypter/AaxDecrypter.csproj index 9ef266c7..90fdc76d 100644 --- a/Source/AaxDecrypter/AaxDecrypter.csproj +++ b/Source/AaxDecrypter/AaxDecrypter.csproj @@ -5,7 +5,7 @@ - + diff --git a/Source/AaxDecrypter/NetworkFileStream.cs b/Source/AaxDecrypter/NetworkFileStream.cs index e1159d67..183fff7e 100644 --- a/Source/AaxDecrypter/NetworkFileStream.cs +++ b/Source/AaxDecrypter/NetworkFileStream.cs @@ -145,7 +145,14 @@ namespace AaxDecrypter private void Update() { RequestHeaders = HttpRequest.Headers; - Updated?.Invoke(this, EventArgs.Empty); + try + { + Updated?.Invoke(this, EventArgs.Empty); + } + catch (Exception ex) + { + Serilog.Log.Error(ex, "An error was encountered while saving the download progress to JSON"); + } } /// @@ -245,9 +252,6 @@ namespace AaxDecrypter WritePosition = downloadPosition; Update(); - downloadedPiece.Set(); - downloadEnded.Set(); - if (!IsCancelled && WritePosition < ContentLength) throw new WebException($"Downloaded size (0x{WritePosition:X10}) is less than {nameof(ContentLength)} (0x{ContentLength:X10})."); @@ -259,6 +263,11 @@ namespace AaxDecrypter Serilog.Log.Error(ex, "An error was encountered while downloading {Uri}", Uri); IsCancelled = true; } + finally + { + downloadedPiece.Set(); + downloadEnded.Set(); + } } #endregion @@ -401,10 +410,11 @@ namespace AaxDecrypter { if (!hasBegunDownloading) BeginDownloading(); - + var toRead = Math.Min(count, Length - Position); WaitToPosition(Position + toRead); - return _readFile.Read(buffer, offset, count); + + return IsCancelled ? 0 : _readFile.Read(buffer, offset, count); } public override long Seek(long offset, SeekOrigin origin) From 1ae767087f0c90187f2113eac7b8f0b30ea42f31 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Mon, 20 Jun 2022 23:13:34 -0600 Subject: [PATCH 2/4] Check downloadEnded inside WaitToPosition --- Source/AaxDecrypter/NetworkFileStream.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Source/AaxDecrypter/NetworkFileStream.cs b/Source/AaxDecrypter/NetworkFileStream.cs index 183fff7e..eaab46f1 100644 --- a/Source/AaxDecrypter/NetworkFileStream.cs +++ b/Source/AaxDecrypter/NetworkFileStream.cs @@ -261,7 +261,6 @@ namespace AaxDecrypter catch (Exception ex) { Serilog.Log.Error(ex, "An error was encountered while downloading {Uri}", Uri); - IsCancelled = true; } finally { @@ -414,7 +413,7 @@ namespace AaxDecrypter var toRead = Math.Min(count, Length - Position); WaitToPosition(Position + toRead); - return IsCancelled ? 0 : _readFile.Read(buffer, offset, count); + return _readFile.Read(buffer, offset, count); } public override long Seek(long offset, SeekOrigin origin) @@ -436,14 +435,18 @@ namespace AaxDecrypter /// The minimum required flished data length in . private void WaitToPosition(long requiredPosition) { - while (requiredPosition > WritePosition && !IsCancelled && hasBegunDownloading && !downloadedPiece.WaitOne(1000)) ; + while (requiredPosition > WritePosition + && hasBegunDownloading + && !IsCancelled + && !downloadEnded.WaitOne(0) + && !downloadedPiece.WaitOne(100)) ; } public override void Close() { IsCancelled = true; - while (downloadEnded is not null && !downloadEnded.WaitOne(1000)) ; + while (downloadEnded is not null && !downloadEnded.WaitOne(100)) ; _readFile.Close(); _writeFile.Close(); From 503379079b4ef254af86c5b13ee4c23e5e22255c Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Tue, 21 Jun 2022 00:23:02 -0600 Subject: [PATCH 3/4] Fix WaitToPosition logic --- Source/AaxDecrypter/NetworkFileStream.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Source/AaxDecrypter/NetworkFileStream.cs b/Source/AaxDecrypter/NetworkFileStream.cs index eaab46f1..8f2079f1 100644 --- a/Source/AaxDecrypter/NetworkFileStream.cs +++ b/Source/AaxDecrypter/NetworkFileStream.cs @@ -412,7 +412,6 @@ namespace AaxDecrypter var toRead = Math.Min(count, Length - Position); WaitToPosition(Position + toRead); - return _readFile.Read(buffer, offset, count); } @@ -435,11 +434,13 @@ namespace AaxDecrypter /// The minimum required flished data length in . private void WaitToPosition(long requiredPosition) { - while (requiredPosition > WritePosition + while (WritePosition < requiredPosition && hasBegunDownloading && !IsCancelled - && !downloadEnded.WaitOne(0) - && !downloadedPiece.WaitOne(100)) ; + && !downloadEnded.WaitOne(0)) + { + downloadedPiece.WaitOne(100); + } } public override void Close() From 71387e94d8327addfe770c5e5c0f2d9e8ecdcecc Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Tue, 21 Jun 2022 08:08:09 -0600 Subject: [PATCH 4/4] Fix bug if folder ended in trailing slash --- Source/FileManager/FileNamingTemplate.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/FileManager/FileNamingTemplate.cs b/Source/FileManager/FileNamingTemplate.cs index ae9b92c7..1f472b6d 100644 --- a/Source/FileManager/FileNamingTemplate.cs +++ b/Source/FileManager/FileNamingTemplate.cs @@ -18,7 +18,8 @@ namespace FileManager /// Generate a valid path for this file or directory public LongPath GetFilePath(bool returnFirstExisting = false) { - string fileName = Template; + + string fileName = Template.EndsWith(Path.DirectorySeparatorChar) ? Template[..^1] : Template; List pathParts = new(); var paramReplacements = ParameterReplacements.ToDictionary(r => $"<{formatKey(r.Key)}>", r => formatValue(r.Value));