Improve ASF codebase

This commit is contained in:
Archi
2021-06-21 22:07:35 +02:00
parent b083587d46
commit c76decda35
5 changed files with 47 additions and 16 deletions

View File

@@ -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

View File

@@ -70,6 +70,14 @@ namespace ArchiSteamFarm.Compatibility {
return Task.FromResult(hashAlgorithm.ComputeHash(inputStream)); 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<IWebHostBuilder> configure) { public static IWebHostBuilder ConfigureWebHostDefaults(this IWebHostBuilder builder, Action<IWebHostBuilder> configure) {
if (configure == null) { if (configure == null) {
throw new ArgumentNullException(nameof(configure)); throw new ArgumentNullException(nameof(configure));

View File

@@ -358,13 +358,7 @@ namespace ArchiSteamFarm.Core {
MemoryStream ms = new(response.Content as byte[] ?? response.Content.ToArray()); MemoryStream ms = new(response.Content as byte[] ?? response.Content.ToArray());
try { 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)) { await using (ms.ConfigureAwait(false)) {
#endif
using ZipArchive zipArchive = new(ms); using ZipArchive zipArchive = new(ms);
if (!UpdateFromArchive(zipArchive, SharedInfo.HomeDirectory)) { if (!UpdateFromArchive(zipArchive, SharedInfo.HomeDirectory)) {

View File

@@ -2899,11 +2899,7 @@ namespace ArchiSteamFarm.Steam {
FileStream fileStream = File.Open(sentryFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); 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 #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)) { await using (fileStream.ConfigureAwait(false)) {
#endif
fileStream.Seek(callback.Offset, SeekOrigin.Begin); fileStream.Seek(callback.Offset, SeekOrigin.Begin);
#if NETFRAMEWORK #if NETFRAMEWORK

View File

@@ -146,13 +146,7 @@ namespace ArchiSteamFarm.Web {
MemoryStream ms = new((int) response.Length); 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 #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)) { await using (ms.ConfigureAwait(false)) {
#endif
try { try {
byte batch = 0; byte batch = 0;
long readThisBatch = 0; long readThisBatch = 0;