From fc20b6cfc4f6cbff0ab173c6d2fc0652d274d094 Mon Sep 17 00:00:00 2001 From: Archi Date: Thu, 15 Dec 2022 19:23:46 +0100 Subject: [PATCH] Final Rider inspections --- .../PeriodicGCPlugin.cs | 1 - ArchiSteamFarm/Core/AprilFools.cs | 2 +- .../IPC/Controllers/Api/TypeController.cs | 2 +- ArchiSteamFarm/IPC/Startup.cs | 2 +- ArchiSteamFarm/IPC/WebUtilities.cs | 8 +++++-- ArchiSteamFarm/Steam/Bot.cs | 8 +++---- ArchiSteamFarm/Steam/Cards/CardsFarmer.cs | 4 ++-- ArchiSteamFarm/Steam/Data/TradeOffer.cs | 2 +- ArchiSteamFarm/Steam/Exchange/Trading.cs | 2 +- .../SteamKit2/InMemoryServerListProvider.cs | 2 +- ArchiSteamFarm/Web/WebBrowser.cs | 22 ++++++++++++++----- Directory.Build.props | 2 +- 12 files changed, 35 insertions(+), 22 deletions(-) diff --git a/ArchiSteamFarm.CustomPlugins.PeriodicGC/PeriodicGCPlugin.cs b/ArchiSteamFarm.CustomPlugins.PeriodicGC/PeriodicGCPlugin.cs index 18eca49ee..d1759478d 100644 --- a/ArchiSteamFarm.CustomPlugins.PeriodicGC/PeriodicGCPlugin.cs +++ b/ArchiSteamFarm.CustomPlugins.PeriodicGC/PeriodicGCPlugin.cs @@ -21,7 +21,6 @@ using System; using System.Composition; -using System.Diagnostics.CodeAnalysis; using System.Runtime; using System.Threading; using System.Threading.Tasks; diff --git a/ArchiSteamFarm/Core/AprilFools.cs b/ArchiSteamFarm/Core/AprilFools.cs index 79cf76f6c..24de6e3ff 100644 --- a/ArchiSteamFarm/Core/AprilFools.cs +++ b/ArchiSteamFarm/Core/AprilFools.cs @@ -36,7 +36,7 @@ internal static class AprilFools { internal static void Init(object? state = null) { DateTime now = DateTime.Now; - if ((now.Month == 4) && (now.Day == 1)) { + if (now is { Month: 4, Day: 1 }) { try { CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture(SharedInfo.LolcatCultureName); } catch (Exception e) { diff --git a/ArchiSteamFarm/IPC/Controllers/Api/TypeController.cs b/ArchiSteamFarm/IPC/Controllers/Api/TypeController.cs index 9287bc879..e50b84a46 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/TypeController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/TypeController.cs @@ -75,7 +75,7 @@ public sealed class TypeController : ArchiController { } } - foreach (PropertyInfo property in targetType.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(static property => property.CanRead && (property.GetMethod?.IsPrivate == false))) { + foreach (PropertyInfo property in targetType.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(static property => property is { CanRead: true, GetMethod.IsPrivate: false })) { JsonPropertyAttribute? jsonProperty = property.GetCustomAttribute(); if (jsonProperty != null) { diff --git a/ArchiSteamFarm/IPC/Startup.cs b/ArchiSteamFarm/IPC/Startup.cs index 0ea7e416e..0d6d3a254 100644 --- a/ArchiSteamFarm/IPC/Startup.cs +++ b/ArchiSteamFarm/IPC/Startup.cs @@ -101,7 +101,7 @@ internal sealed class Startup { app.UseStaticFiles( new StaticFileOptions { OnPrepareResponse = static context => { - if (context.File.Exists && !context.File.IsDirectory && !string.IsNullOrEmpty(context.File.Name)) { + if (context.File is { Exists: true, IsDirectory: false } && !string.IsNullOrEmpty(context.File.Name)) { string extension = Path.GetExtension(context.File.Name); CacheControlHeaderValue cacheControl = new(); diff --git a/ArchiSteamFarm/IPC/WebUtilities.cs b/ArchiSteamFarm/IPC/WebUtilities.cs index c2bb75a0f..e456d67f0 100644 --- a/ArchiSteamFarm/IPC/WebUtilities.cs +++ b/ArchiSteamFarm/IPC/WebUtilities.cs @@ -93,11 +93,15 @@ internal static class WebUtilities { StreamWriter streamWriter = new(response.Body, Encoding.UTF8); await using (streamWriter.ConfigureAwait(false)) { - using JsonTextWriter jsonWriter = new(streamWriter) { +#pragma warning disable CA2000 // False positive, we're actually wrapping it in the using clause below exactly for that purpose + JsonTextWriter jsonWriter = new(streamWriter) { CloseOutput = false }; +#pragma warning restore CA2000 // False positive, we're actually wrapping it in the using clause below exactly for that purpose - serializer.Serialize(jsonWriter, value); + await using (jsonWriter.ConfigureAwait(false)) { + serializer.Serialize(jsonWriter, value); + } } } } diff --git a/ArchiSteamFarm/Steam/Bot.cs b/ArchiSteamFarm/Steam/Bot.cs index 03a8c6631..d72f1cc2e 100644 --- a/ArchiSteamFarm/Steam/Bot.cs +++ b/ArchiSteamFarm/Steam/Bot.cs @@ -1159,7 +1159,7 @@ public sealed class Bot : IAsyncDisposable, IDisposable { return (appID, DateTime.MinValue, true); } - return ((productInfoResultSet.Complete && !productInfoResultSet.Failed) || optimisticDiscovery ? appID : 0, DateTime.MinValue, true); + return (productInfoResultSet is { Complete: true, Failed: false } || optimisticDiscovery ? appID : 0, DateTime.MinValue, true); } internal Task?> GetMarketableAppIDs() => ArchiWebHandler.GetAppList(); @@ -1422,7 +1422,7 @@ public sealed class Bot : IAsyncDisposable, IDisposable { internal async Task OnFarmingFinished(bool farmedSomething) { await OnFarmingStopped().ConfigureAwait(false); - if (BotConfig.SendOnFarmingFinished && (BotConfig.LootableTypes.Count > 0) && (farmedSomething || !FirstTradeSent)) { + if (BotConfig is { SendOnFarmingFinished: true, LootableTypes.Count: > 0 } && (farmedSomething || !FirstTradeSent)) { FirstTradeSent = true; await Actions.SendInventory(filterFunction: item => BotConfig.LootableTypes.Contains(item.Type)).ConfigureAwait(false); @@ -2068,7 +2068,7 @@ public sealed class Bot : IAsyncDisposable, IDisposable { SendItemsTimer = null; } - if ((BotConfig.SendTradePeriod > 0) && (BotConfig.LootableTypes.Count > 0) && BotConfig.SteamUserPermissions.Values.Any(static permission => permission >= BotConfig.EAccess.Master)) { + if (BotConfig is { SendTradePeriod: > 0, LootableTypes.Count: > 0 } && BotConfig.SteamUserPermissions.Values.Any(static permission => permission >= BotConfig.EAccess.Master)) { SendItemsTimer = new Timer( OnSendItemsTimer, null, @@ -2563,7 +2563,7 @@ public sealed class Bot : IAsyncDisposable, IDisposable { } // Under normal circumstances, timestamp must always be greater than 0, but Steam already proved that it's capable of going against the logic - if (!notification.local_echo && (notification.rtime32_server_timestamp > 0)) { + if (notification is { local_echo: false, rtime32_server_timestamp: > 0 }) { if (ShouldAckChatMessage(notification.steamid_friend)) { Utilities.InBackground(() => ArchiHandler.AckMessage(notification.steamid_friend, notification.rtime32_server_timestamp)); } diff --git a/ArchiSteamFarm/Steam/Cards/CardsFarmer.cs b/ArchiSteamFarm/Steam/Cards/CardsFarmer.cs index cf77f538b..9e315fe06 100644 --- a/ArchiSteamFarm/Steam/Cards/CardsFarmer.cs +++ b/ArchiSteamFarm/Steam/Cards/CardsFarmer.cs @@ -251,7 +251,7 @@ public sealed class CardsFarmer : IAsyncDisposable, IDisposable { // If we're not farming, and we got new items, it's likely to be a booster pack or likewise // In this case, perform a loot if user wants to do so - if (Bot.BotConfig.SendOnFarmingFinished && (Bot.BotConfig.LootableTypes.Count > 0)) { + if (Bot.BotConfig is { SendOnFarmingFinished: true, LootableTypes.Count: > 0 }) { await Bot.Actions.SendInventory(filterFunction: item => Bot.BotConfig.LootableTypes.Contains(item.Type)).ConfigureAwait(false); } } @@ -352,7 +352,7 @@ public sealed class CardsFarmer : IAsyncDisposable, IDisposable { if (minFarmingDelayAfterBlock > 0) { Bot.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.BotExtraIdlingCooldown, TimeSpan.FromSeconds(minFarmingDelayAfterBlock).ToHumanReadable())); - for (byte i = 0; (i < minFarmingDelayAfterBlock) && Bot.IsConnectedAndLoggedOn && Bot.IsPlayingPossible && Bot.PlayingWasBlocked; i++) { + for (byte i = 0; (i < minFarmingDelayAfterBlock) && Bot is { IsConnectedAndLoggedOn: true, IsPlayingPossible: true, PlayingWasBlocked: true }; i++) { await Task.Delay(1000).ConfigureAwait(false); } diff --git a/ArchiSteamFarm/Steam/Data/TradeOffer.cs b/ArchiSteamFarm/Steam/Data/TradeOffer.cs index 4a79f753f..15c989c26 100644 --- a/ArchiSteamFarm/Steam/Data/TradeOffer.cs +++ b/ArchiSteamFarm/Steam/Data/TradeOffer.cs @@ -73,6 +73,6 @@ public sealed class TradeOffer { throw new ArgumentNullException(nameof(acceptedTypes)); } - return ItemsToGive.All(item => (item.AppID == Asset.SteamAppID) && (item.ContextID == Asset.SteamCommunityContextID) && acceptedTypes.Contains(item.Type)); + return ItemsToGive.All(item => item is { AppID: Asset.SteamAppID, ContextID: Asset.SteamCommunityContextID } && acceptedTypes.Contains(item.Type)); } } diff --git a/ArchiSteamFarm/Steam/Exchange/Trading.cs b/ArchiSteamFarm/Steam/Exchange/Trading.cs index 4e2738b43..b3cc63b10 100644 --- a/ArchiSteamFarm/Steam/Exchange/Trading.cs +++ b/ArchiSteamFarm/Steam/Exchange/Trading.cs @@ -382,7 +382,7 @@ public sealed class Trading : IDisposable { lootableTypesReceived = await ParseActiveTrades().ConfigureAwait(false); } - if (lootableTypesReceived && Bot.BotConfig.SendOnFarmingFinished && (Bot.BotConfig.LootableTypes.Count > 0)) { + if (lootableTypesReceived && Bot.BotConfig is { SendOnFarmingFinished: true, LootableTypes.Count: > 0 }) { await Bot.Actions.SendInventory(filterFunction: item => Bot.BotConfig.LootableTypes.Contains(item.Type)).ConfigureAwait(false); } } finally { diff --git a/ArchiSteamFarm/Steam/SteamKit2/InMemoryServerListProvider.cs b/ArchiSteamFarm/Steam/SteamKit2/InMemoryServerListProvider.cs index ee36d2576..c18699331 100644 --- a/ArchiSteamFarm/Steam/SteamKit2/InMemoryServerListProvider.cs +++ b/ArchiSteamFarm/Steam/SteamKit2/InMemoryServerListProvider.cs @@ -33,7 +33,7 @@ internal sealed class InMemoryServerListProvider : IServerListProvider { [JsonProperty(Required = Required.DisallowNull)] private readonly ConcurrentHashSet ServerRecords = new(); - public Task> FetchServerListAsync() => Task.FromResult(ServerRecords.Where(static server => !string.IsNullOrEmpty(server.Host) && (server.Port > 0) && (server.ProtocolTypes > 0)).Select(static server => ServerRecord.CreateServer(server.Host, server.Port, server.ProtocolTypes))); + public Task> FetchServerListAsync() => Task.FromResult(ServerRecords.Where(static server => !string.IsNullOrEmpty(server.Host) && server is { Port: > 0, ProtocolTypes: > 0 }).Select(static server => ServerRecord.CreateServer(server.Host, server.Port, server.ProtocolTypes))); public Task UpdateServerListAsync(IEnumerable endpoints) { ArgumentNullException.ThrowIfNull(endpoints); diff --git a/ArchiSteamFarm/Web/WebBrowser.cs b/ArchiSteamFarm/Web/WebBrowser.cs index b9bf860eb..cc652643a 100644 --- a/ArchiSteamFarm/Web/WebBrowser.cs +++ b/ArchiSteamFarm/Web/WebBrowser.cs @@ -345,11 +345,16 @@ public sealed class WebBrowser : IDisposable { try { using StreamReader streamReader = new(response.Content); - using JsonTextReader jsonReader = new(streamReader); - JsonSerializer serializer = new(); +#pragma warning disable CA2000 // False positive, we're actually wrapping it in the using clause below exactly for that purpose + JsonTextReader jsonReader = new(streamReader); +#pragma warning restore CA2000 // False positive, we're actually wrapping it in the using clause below exactly for that purpose - obj = serializer.Deserialize(jsonReader); + await using (jsonReader.ConfigureAwait(false)) { + JsonSerializer serializer = new(); + + obj = serializer.Deserialize(jsonReader); + } } catch (Exception e) { if ((requestOptions.HasFlag(ERequestOptions.AllowInvalidBodyOnSuccess) && response.StatusCode.IsSuccessCode()) || (requestOptions.HasFlag(ERequestOptions.AllowInvalidBodyOnErrors) && !response.StatusCode.IsSuccessCode())) { return new ObjectResponse(response); @@ -649,11 +654,16 @@ public sealed class WebBrowser : IDisposable { try { using StreamReader streamReader = new(response.Content); - using JsonReader jsonReader = new JsonTextReader(streamReader); - JsonSerializer serializer = new(); +#pragma warning disable CA2000 // False positive, we're actually wrapping it in the using clause below exactly for that purpose + JsonReader jsonReader = new JsonTextReader(streamReader); +#pragma warning restore CA2000 // False positive, we're actually wrapping it in the using clause below exactly for that purpose - obj = serializer.Deserialize(jsonReader); + await using (jsonReader.ConfigureAwait(false)) { + JsonSerializer serializer = new(); + + obj = serializer.Deserialize(jsonReader); + } } catch (Exception e) { if ((requestOptions.HasFlag(ERequestOptions.AllowInvalidBodyOnSuccess) && response.StatusCode.IsSuccessCode()) || (requestOptions.HasFlag(ERequestOptions.AllowInvalidBodyOnErrors) && !response.StatusCode.IsSuccessCode())) { return new ObjectResponse(response); diff --git a/Directory.Build.props b/Directory.Build.props index 6f5bac001..dfe020671 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 5.4.0.4 + 5.4.1.0