From 22e00bf42ab7f00fbf4c8519ee3776252f3fc146 Mon Sep 17 00:00:00 2001 From: yell0wsuit <5692900+yell0wsuit@users.noreply.github.com> Date: Wed, 17 Apr 2024 16:38:16 +0700 Subject: [PATCH] Fixed CA1835 --- .../Modules/Updater/Utils/DoUpdateWithMultipleThreads.cs | 8 +++++--- .../Modules/Updater/Utils/DoUpdateWithSingleThread.cs | 7 ++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Ryujinx/Modules/Updater/Utils/DoUpdateWithMultipleThreads.cs b/src/Ryujinx/Modules/Updater/Utils/DoUpdateWithMultipleThreads.cs index d914cbe28..58d0a0923 100644 --- a/src/Ryujinx/Modules/Updater/Utils/DoUpdateWithMultipleThreads.cs +++ b/src/Ryujinx/Modules/Updater/Utils/DoUpdateWithMultipleThreads.cs @@ -95,18 +95,20 @@ namespace Ryujinx.Modules private static async Task DownloadFileChunk(string url, long start, long end, int index, TaskDialog taskDialog, int[] progressPercentage) { - byte[] buffer = new byte[8192]; + Memory buffer = new byte[8192]; + using var request = new HttpRequestMessage(HttpMethod.Get, url); request.Headers.Range = new RangeHeaderValue(start, end); HttpResponseMessage response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); + using var stream = await response.Content.ReadAsStreamAsync(); using var memoryStream = new MemoryStream(); int bytesRead; long totalRead = 0; - while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0) + while ((bytesRead = await stream.ReadAsync(buffer, CancellationToken.None)) > 0) { - memoryStream.Write(buffer, 0, bytesRead); + memoryStream.Write(buffer.Slice(0, bytesRead).ToArray(), 0, bytesRead); totalRead += bytesRead; int progress = (int)((totalRead * 100) / (end - start + 1)); progressPercentage[index] = progress; diff --git a/src/Ryujinx/Modules/Updater/Utils/DoUpdateWithSingleThread.cs b/src/Ryujinx/Modules/Updater/Utils/DoUpdateWithSingleThread.cs index 9fda86177..24121c5dc 100644 --- a/src/Ryujinx/Modules/Updater/Utils/DoUpdateWithSingleThread.cs +++ b/src/Ryujinx/Modules/Updater/Utils/DoUpdateWithSingleThread.cs @@ -26,6 +26,7 @@ namespace Ryujinx.Modules HttpResponseMessage response = await httpClient.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead); if (!response.IsSuccessStatusCode) { + Logger.Error?.Print(LogClass.Application, $"Failed to download file: {response.ReasonPhrase}"); throw new HttpRequestException($"Failed to download file: {response.ReasonPhrase}"); } @@ -36,12 +37,12 @@ namespace Ryujinx.Modules using Stream remoteFileStream = await response.Content.ReadAsStreamAsync(); using Stream updateFileStream = File.Open(updateFile, FileMode.Create); - byte[] buffer = new byte[32 * 1024]; + Memory buffer = new byte[32 * 1024]; int readSize; - while ((readSize = await remoteFileStream.ReadAsync(buffer, 0, buffer.Length)) > 0) + while ((readSize = await remoteFileStream.ReadAsync(buffer, CancellationToken.None)) > 0) { - updateFileStream.Write(buffer, 0, readSize); + updateFileStream.Write(buffer.Slice(0, readSize).ToArray(), 0, readSize); byteWritten += readSize; int progress = GetPercentage(byteWritten, totalBytes);