From c76decda35f0af22cfee7f4c5b973a8a954f5124 Mon Sep 17 00:00:00 2001 From: Archi Date: Mon, 21 Jun 2021 22:07:35 +0200 Subject: [PATCH] Improve ASF codebase --- .../Compatibility/AsyncDisposableWrapper.cs | 39 +++++++++++++++++++ ArchiSteamFarm/Compatibility/StaticHelpers.cs | 8 ++++ ArchiSteamFarm/Core/ASF.cs | 6 --- ArchiSteamFarm/Steam/Bot.cs | 4 -- ArchiSteamFarm/Web/WebBrowser.cs | 6 --- 5 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 ArchiSteamFarm/Compatibility/AsyncDisposableWrapper.cs diff --git a/ArchiSteamFarm/Compatibility/AsyncDisposableWrapper.cs b/ArchiSteamFarm/Compatibility/AsyncDisposableWrapper.cs new file mode 100644 index 000000000..d4f1e0d6a --- /dev/null +++ b/ArchiSteamFarm/Compatibility/AsyncDisposableWrapper.cs @@ -0,0 +1,39 @@ +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// | +// Copyright 2015-2021 Ɓukasz "JustArchi" Domeradzki +// Contact: JustArchi@JustArchi.net +// | +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// | +// http://www.apache.org/licenses/LICENSE-2.0 +// | +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#if NETFRAMEWORK +using System; +using System.Threading.Tasks; + +namespace ArchiSteamFarm.Compatibility { + internal sealed class AsyncDisposableWrapper : IAsyncDisposable { + private readonly IDisposable Disposable; + + internal AsyncDisposableWrapper(IDisposable disposable) => Disposable = disposable ?? throw new ArgumentNullException(nameof(disposable)); + + public ValueTask DisposeAsync() { + Disposable.Dispose(); + + return default(ValueTask); + } + } +} +#endif diff --git a/ArchiSteamFarm/Compatibility/StaticHelpers.cs b/ArchiSteamFarm/Compatibility/StaticHelpers.cs index 7dfd433b5..a109baffb 100644 --- a/ArchiSteamFarm/Compatibility/StaticHelpers.cs +++ b/ArchiSteamFarm/Compatibility/StaticHelpers.cs @@ -70,6 +70,14 @@ namespace ArchiSteamFarm.Compatibility { return Task.FromResult(hashAlgorithm.ComputeHash(inputStream)); } + public static IAsyncDisposable ConfigureAwait(this IDisposable source, bool _) { + if (source == null) { + throw new ArgumentNullException(nameof(source)); + } + + return new AsyncDisposableWrapper(source); + } + public static IWebHostBuilder ConfigureWebHostDefaults(this IWebHostBuilder builder, Action configure) { if (configure == null) { throw new ArgumentNullException(nameof(configure)); diff --git a/ArchiSteamFarm/Core/ASF.cs b/ArchiSteamFarm/Core/ASF.cs index df984d7ca..f18df7d4f 100644 --- a/ArchiSteamFarm/Core/ASF.cs +++ b/ArchiSteamFarm/Core/ASF.cs @@ -358,13 +358,7 @@ namespace ArchiSteamFarm.Core { MemoryStream ms = new(response.Content as byte[] ?? response.Content.ToArray()); try { -#if NETFRAMEWORK -#pragma warning disable CA1508 // False positive, ms is not null here indeed, but using clause is needed for dispose - using (ms) { -#pragma warning restore CA1508 // False positive, ms is not null here indeed, but using clause is needed for dispose -#else await using (ms.ConfigureAwait(false)) { -#endif using ZipArchive zipArchive = new(ms); if (!UpdateFromArchive(zipArchive, SharedInfo.HomeDirectory)) { diff --git a/ArchiSteamFarm/Steam/Bot.cs b/ArchiSteamFarm/Steam/Bot.cs index b064cdd76..269ca4132 100644 --- a/ArchiSteamFarm/Steam/Bot.cs +++ b/ArchiSteamFarm/Steam/Bot.cs @@ -2899,11 +2899,7 @@ namespace ArchiSteamFarm.Steam { FileStream fileStream = File.Open(sentryFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); #pragma warning restore CA2000 // False positive, we're actually wrapping it in the using clause below exactly for that purpose -#if NETFRAMEWORK - using (fileStream) { -#else await using (fileStream.ConfigureAwait(false)) { -#endif fileStream.Seek(callback.Offset, SeekOrigin.Begin); #if NETFRAMEWORK diff --git a/ArchiSteamFarm/Web/WebBrowser.cs b/ArchiSteamFarm/Web/WebBrowser.cs index 216fc3c81..5e64605c2 100644 --- a/ArchiSteamFarm/Web/WebBrowser.cs +++ b/ArchiSteamFarm/Web/WebBrowser.cs @@ -146,13 +146,7 @@ namespace ArchiSteamFarm.Web { MemoryStream ms = new((int) response.Length); #pragma warning restore CA2000 // False positive, we're actually wrapping it in the using clause below exactly for that purpose -#if NETFRAMEWORK -#pragma warning disable CA1508 // False positive, ms is not null here indeed, but using clause is needed for dispose - using (ms) { -#pragma warning restore CA1508 // False positive, ms is not null here indeed, but using clause is needed for dispose -#else await using (ms.ConfigureAwait(false)) { -#endif try { byte batch = 0; long readThisBatch = 0;