Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Revert "Revert asynchronous file IO in .NET 8"
Browse files Browse the repository at this point in the history
This reverts commit 98a846c.
  • Loading branch information
SadPencil committed Feb 20, 2024
1 parent 98a846c commit a64179d
Showing 1 changed file with 86 additions and 13 deletions.
99 changes: 86 additions & 13 deletions ClientUpdater/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,12 +582,6 @@ private static void ReadLegacyUpdaterConfig(List<UpdateMirror> updateMirrors)
}
}

private static FileStream NewFileStream(string filename)
{
var fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.Asynchronous);
return fileStream;
}

/// <summary>
/// Performs a version file check on update server.
/// </summary>
Expand Down Expand Up @@ -621,12 +615,34 @@ private static async Task DoVersionCheckAsync()
{
Logger.Log("Updater: Trying to connect to update mirror " + updateMirrors[currentUpdateMirrorIndex].URL);

using FileStream fileStream = NewFileStream(downloadFile.FullName);
Stream stream = await SharedHttpClient.GetStreamAsync(updateMirrors[currentUpdateMirrorIndex].URL + VERSION_FILE).ConfigureAwait(false);
#if NETFRAMEWORK
var fileStream = new FileStream(downloadFile.FullName, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.Asynchronous);
#else
var fileStream = new FileStream(downloadFile.FullName, new FileStreamOptions
{
Access = FileAccess.Write,
Mode = FileMode.Create,
Options = FileOptions.Asynchronous,
Share = FileShare.None
});
#endif

await using (stream.ConfigureAwait(false))
#if NETFRAMEWORK
using (fileStream)
#else
await using (fileStream.ConfigureAwait(false))
#endif
{
await stream.CopyToAsync(fileStream).ConfigureAwait(false);
Stream stream = await SharedHttpClient.GetStreamAsync(updateMirrors[currentUpdateMirrorIndex].URL + VERSION_FILE).ConfigureAwait(false);

#if NETFRAMEWORK
using (stream)
#else
await using (stream.ConfigureAwait(false))
#endif
{
await stream.CopyToAsync(fileStream).ConfigureAwait(false);
}
}

break;
Expand Down Expand Up @@ -775,12 +791,31 @@ private static async ValueTask ExecuteAfterUpdateScriptAsync()
try
{
string downloadFile = SafePath.CombineFilePath(GamePath, "updateexec");
#if NETFRAMEWORK
var fileStream = new FileStream(downloadFile, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.Asynchronous);
#else
var fileStream = new FileStream(downloadFile, new FileStreamOptions
{
Access = FileAccess.Write,
Mode = FileMode.Create,
Options = FileOptions.Asynchronous,
Share = FileShare.None
});
#endif

using FileStream fileStream = NewFileStream(downloadFile);
#if NETFRAMEWORK
using (fileStream)
#else
await using (fileStream.ConfigureAwait(false))
#endif
{
Stream stream = await SharedHttpClient.GetStreamAsync(updateMirrors[currentUpdateMirrorIndex].URL + "updateexec").ConfigureAwait(false);

#if NETFRAMEWORK
using (stream)
#else
await using (stream.ConfigureAwait(false))
#endif
{
await stream.CopyToAsync(fileStream).ConfigureAwait(false);
}
Expand All @@ -805,12 +840,31 @@ private static async ValueTask<bool> ExecutePreUpdateScriptAsync()
try
{
string downloadFile = SafePath.CombineFilePath(GamePath, "preupdateexec");
#if NETFRAMEWORK
var fileStream = new FileStream(downloadFile, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.Asynchronous);
#else
var fileStream = new FileStream(downloadFile, new FileStreamOptions
{
Access = FileAccess.Write,
Mode = FileMode.Create,
Options = FileOptions.Asynchronous,
Share = FileShare.None
});
#endif

using FileStream fileStream = NewFileStream(downloadFile);
#if NETFRAMEWORK
using (fileStream)
#else
await using (fileStream.ConfigureAwait(false))
#endif
{
Stream stream = await SharedHttpClient.GetStreamAsync(updateMirrors[currentUpdateMirrorIndex].URL + "preupdateexec").ConfigureAwait(false);

#if NETFRAMEWORK
using (stream)
#else
await using (stream.ConfigureAwait(false))
#endif
{
await stream.CopyToAsync(fileStream).ConfigureAwait(false);
}
Expand Down Expand Up @@ -1420,12 +1474,31 @@ private static async ValueTask<string> DownloadFileAsync(UpdaterFileInfo fileInf
else
{
Logger.Log("Updater: Downloading file " + filename + extraExtension);
#if NETFRAMEWORK
var fileStream = new FileStream(downloadFile.FullName, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.Asynchronous);
#else
var fileStream = new FileStream(downloadFile.FullName, new FileStreamOptions
{
Access = FileAccess.Write,
Mode = FileMode.Create,
Options = FileOptions.Asynchronous,
Share = FileShare.None
});
#endif

using FileStream fileStream = NewFileStream(downloadFile.FullName);
#if NETFRAMEWORK
using (fileStream)
#else
await using (fileStream.ConfigureAwait(false))
#endif
{
Stream stream = await SharedHttpClient.GetStreamAsync(new Uri(uriString)).ConfigureAwait(false);

#if NETFRAMEWORK
using (stream)
#else
await using (stream.ConfigureAwait(false))
#endif
{
await stream.CopyToAsync(fileStream).ConfigureAwait(false);
}
Expand Down

0 comments on commit a64179d

Please sign in to comment.