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..8f2079f1 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})."); @@ -257,7 +261,11 @@ namespace AaxDecrypter catch (Exception ex) { Serilog.Log.Error(ex, "An error was encountered while downloading {Uri}", Uri); - IsCancelled = true; + } + finally + { + downloadedPiece.Set(); + downloadEnded.Set(); } } @@ -401,7 +409,7 @@ namespace AaxDecrypter { if (!hasBegunDownloading) BeginDownloading(); - + var toRead = Math.Min(count, Length - Position); WaitToPosition(Position + toRead); return _readFile.Read(buffer, offset, count); @@ -426,14 +434,20 @@ namespace AaxDecrypter /// The minimum required flished data length in . private void WaitToPosition(long requiredPosition) { - while (requiredPosition > WritePosition && !IsCancelled && hasBegunDownloading && !downloadedPiece.WaitOne(1000)) ; + while (WritePosition < requiredPosition + && 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(); 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));