diff --git a/ArchiSteamFarm.sln.DotSettings b/ArchiSteamFarm.sln.DotSettings index 19d4523ca..991387ba3 100644 --- a/ArchiSteamFarm.sln.DotSettings +++ b/ArchiSteamFarm.sln.DotSettings @@ -53,7 +53,8 @@ SUGGESTION WARNING SUGGESTION - SUGGESTION + + True SUGGESTION WARNING SUGGESTION diff --git a/ArchiSteamFarm/ArchiWebHandler.cs b/ArchiSteamFarm/ArchiWebHandler.cs index 78e291012..12677c981 100644 --- a/ArchiSteamFarm/ArchiWebHandler.cs +++ b/ArchiSteamFarm/ArchiWebHandler.cs @@ -133,15 +133,15 @@ namespace ArchiSteamFarm { return await UrlPostRetryWithSession(SteamStoreURL, request, data).ConfigureAwait(false); } - internal async Task DeclineTradeOffer(ulong tradeID) { + internal async Task DeclineTradeOffer(ulong tradeID) { if (tradeID == 0) { Bot.ArchiLogger.LogNullError(nameof(tradeID)); - return; + return false; } string steamApiKey = await GetApiKey().ConfigureAwait(false); if (string.IsNullOrEmpty(steamApiKey)) { - return; + return false; } KeyValue response = null; @@ -165,7 +165,10 @@ namespace ArchiSteamFarm { if (response == null) { Bot.ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries)); + return false; } + + return true; } internal async Task> GenerateNewDiscoveryQueue() { @@ -587,12 +590,18 @@ namespace ArchiSteamFarm { } } - if (response != null) { - return response["server_time"].AsUnsignedInteger(); + if (response == null) { + Bot.ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries)); + return 0; } - Bot.ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries)); - return 0; + uint result = response["server_time"].AsUnsignedInteger(); + if (result == 0) { + Bot.ArchiLogger.LogNullError(nameof(result)); + return 0; + } + + return result; } internal async Task GetSteamAwardsPage() { @@ -639,12 +648,12 @@ namespace ArchiSteamFarm { text = text.Substring(0, index); - if (byte.TryParse(text, out byte result)) { - return result; + if (!byte.TryParse(text, out byte result)) { + Bot.ArchiLogger.LogNullError(nameof(result)); + return null; } - Bot.ArchiLogger.LogNullError(nameof(result)); - return null; + return result; } internal async Task GetTradeHoldDurationForUser(ulong steamID, string tradeToken = null) { @@ -1168,17 +1177,12 @@ namespace ArchiSteamFarm { } text = text.Substring(keyIndex); - if (text.Length != 32) { + if ((text.Length != 32) || !Utilities.IsValidHexadecimalString(text)) { Bot.ArchiLogger.LogNullError(nameof(text)); return (ESteamApiKeyState.Error, null); } - if (Utilities.IsValidHexadecimalString(text)) { - return (ESteamApiKeyState.Registered, text); - } - - Bot.ArchiLogger.LogNullError(nameof(text)); - return (ESteamApiKeyState.Error, null); + return (ESteamApiKeyState.Registered, text); } private static uint GetAppIDFromMarketHashName(string hashName) { @@ -1232,9 +1236,9 @@ namespace ArchiSteamFarm { return null; } - // Notice: checked doesn't have a value - null is lack of attribute, "" is attribute existing string state = htmlNode.GetAttributeValue("checked", null); + // Notice: checked doesn't have a value - null is lack of attribute, "" is attribute existing return state != null; } @@ -1244,8 +1248,7 @@ namespace ArchiSteamFarm { return false; } - bool result = uri.AbsolutePath.StartsWith("/login", StringComparison.Ordinal) || uri.Host.Equals("lostauth"); - return result; + return uri.AbsolutePath.StartsWith("/login", StringComparison.Ordinal) || uri.Host.Equals("lostauth"); } private static bool ParseItems(Dictionary descriptions, IReadOnlyCollection input, ICollection output) { @@ -1415,13 +1418,13 @@ namespace ArchiSteamFarm { return response.Content; } - if (await RefreshSession(host).ConfigureAwait(false)) { - return await UrlGetToHtmlDocumentRetryWithSession(host, request, --maxTries).ConfigureAwait(false); + if (!await RefreshSession(host).ConfigureAwait(false)) { + Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); + Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); + return null; } - Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); - Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); - return null; + return await UrlGetToHtmlDocumentRetryWithSession(host, request, --maxTries).ConfigureAwait(false); } private async Task UrlGetToObjectRetryWithSession(string host, string request, byte maxTries = WebBrowser.MaxTries) { @@ -1459,13 +1462,13 @@ namespace ArchiSteamFarm { return response.Content; } - if (await RefreshSession(host).ConfigureAwait(false)) { - return await UrlGetToObjectRetryWithSession(host, request, --maxTries).ConfigureAwait(false); + if (!await RefreshSession(host).ConfigureAwait(false)) { + Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); + Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); + return default; } - Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); - Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); - return default; + return await UrlGetToObjectRetryWithSession(host, request, --maxTries).ConfigureAwait(false); } private async Task UrlGetToXmlDocumentRetryWithSession(string host, string request, byte maxTries = WebBrowser.MaxTries) { @@ -1503,13 +1506,13 @@ namespace ArchiSteamFarm { return response.Content; } - if (await RefreshSession(host).ConfigureAwait(false)) { - return await UrlGetToXmlDocumentRetryWithSession(host, request, --maxTries).ConfigureAwait(false); + if (!await RefreshSession(host).ConfigureAwait(false)) { + Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); + Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); + return null; } - Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); - Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); - return null; + return await UrlGetToXmlDocumentRetryWithSession(host, request, --maxTries).ConfigureAwait(false); } private async Task UrlHeadRetryWithSession(string host, string request, byte maxTries = WebBrowser.MaxTries) { @@ -1547,13 +1550,13 @@ namespace ArchiSteamFarm { return true; } - if (await RefreshSession(host).ConfigureAwait(false)) { - return await UrlHeadRetryWithSession(host, request, --maxTries).ConfigureAwait(false); + if (!await RefreshSession(host).ConfigureAwait(false)) { + Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); + Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); + return false; } - Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); - Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); - return false; + return await UrlHeadRetryWithSession(host, request, --maxTries).ConfigureAwait(false); } private async Task UrlPostRetryWithSession(string host, string request, Dictionary data = null, string referer = null, ESession session = ESession.Lowercase, byte maxTries = WebBrowser.MaxTries) { @@ -1620,13 +1623,13 @@ namespace ArchiSteamFarm { return true; } - if (await RefreshSession(host).ConfigureAwait(false)) { - return await UrlPostRetryWithSession(host, request, data, referer, session, --maxTries).ConfigureAwait(false); + if (!await RefreshSession(host).ConfigureAwait(false)) { + Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); + Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); + return false; } - Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); - Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); - return false; + return await UrlPostRetryWithSession(host, request, data, referer, session, --maxTries).ConfigureAwait(false); } private async Task UrlPostToHtmlDocumentRetryWithSession(string host, string request, Dictionary data = null, string referer = null, ESession session = ESession.Lowercase, byte maxTries = WebBrowser.MaxTries) { @@ -1693,13 +1696,13 @@ namespace ArchiSteamFarm { return response.Content; } - if (await RefreshSession(host).ConfigureAwait(false)) { - return await UrlPostToHtmlDocumentRetryWithSession(host, request, data, referer, session, --maxTries).ConfigureAwait(false); + if (!await RefreshSession(host).ConfigureAwait(false)) { + Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); + Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); + return null; } - Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); - Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); - return null; + return await UrlPostToHtmlDocumentRetryWithSession(host, request, data, referer, session, --maxTries).ConfigureAwait(false); } private async Task UrlPostToObjectRetryWithSession(string host, string request, Dictionary data = null, string referer = null, ESession session = ESession.Lowercase, byte maxTries = WebBrowser.MaxTries) { @@ -1766,13 +1769,13 @@ namespace ArchiSteamFarm { return response.Content; } - if (await RefreshSession(host).ConfigureAwait(false)) { - return await UrlPostToObjectRetryWithSession(host, request, data, referer, session, --maxTries).ConfigureAwait(false); + if (!await RefreshSession(host).ConfigureAwait(false)) { + Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); + Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); + return default; } - Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); - Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); - return default; + return await UrlPostToObjectRetryWithSession(host, request, data, referer, session, --maxTries).ConfigureAwait(false); } private async Task UrlPostToObjectRetryWithSession(string host, string request, List> data = null, string referer = null, ESession session = ESession.Lowercase, byte maxTries = WebBrowser.MaxTries) { @@ -1842,13 +1845,13 @@ namespace ArchiSteamFarm { return response.Content; } - if (await RefreshSession(host).ConfigureAwait(false)) { - return await UrlPostToObjectRetryWithSession(host, request, data, referer, session, --maxTries).ConfigureAwait(false); + if (!await RefreshSession(host).ConfigureAwait(false)) { + Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); + Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); + return default; } - Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); - Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); - return default; + return await UrlPostToObjectRetryWithSession(host, request, data, referer, session, --maxTries).ConfigureAwait(false); } private enum ESession : byte { diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 66aed852e..61f7c653d 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -357,12 +357,12 @@ namespace ArchiSteamFarm { } internal static string FormatBotResponse(string response, string botName) { - if (!string.IsNullOrEmpty(response) && !string.IsNullOrEmpty(botName)) { - return Environment.NewLine + "<" + botName + "> " + response; + if (string.IsNullOrEmpty(response) || string.IsNullOrEmpty(botName)) { + ASF.ArchiLogger.LogNullError(nameof(response) + " || " + nameof(botName)); + return null; } - ASF.ArchiLogger.LogNullError(nameof(response) + " || " + nameof(botName)); - return null; + return Environment.NewLine + "<" + botName + "> " + response; } internal async Task<(uint PlayableAppID, DateTime IgnoredUntil)> GetAppDataForIdling(uint appID, float hoursPlayed, bool allowRecursiveDiscovery = true, bool optimisticDiscovery = true) { @@ -639,8 +639,7 @@ namespace ArchiSteamFarm { } } - byte? result = await ArchiWebHandler.GetTradeHoldDurationForTrade(tradeID).ConfigureAwait(false); - return result; + return await ArchiWebHandler.GetTradeHoldDurationForTrade(tradeID).ConfigureAwait(false); } internal async Task IdleGame(CardsFarmer.Game game) { @@ -729,8 +728,7 @@ namespace ArchiSteamFarm { return false; } - bool result = BotDatabase.IsBlacklistedFromIdling(appID); - return result; + return BotDatabase.IsBlacklistedFromIdling(appID); } internal bool IsBlacklistedFromTrades(ulong steamID) { @@ -739,8 +737,7 @@ namespace ArchiSteamFarm { return false; } - bool result = BotDatabase.IsBlacklistedFromTrades(steamID); - return result; + return BotDatabase.IsBlacklistedFromTrades(steamID); } internal bool IsMaster(ulong steamID) { @@ -762,8 +759,7 @@ namespace ArchiSteamFarm { return false; } - bool result = BotDatabase.IsPriorityIdling(appID); - return result; + return BotDatabase.IsPriorityIdling(appID); } internal async Task LootIfNeeded() { @@ -1283,32 +1279,32 @@ namespace ArchiSteamFarm { } private string FormatBotResponse(string response) { - if (!string.IsNullOrEmpty(response)) { - return Environment.NewLine + "<" + BotName + "> " + response; + if (string.IsNullOrEmpty(response)) { + ASF.ArchiLogger.LogNullError(nameof(response)); + return null; } - ASF.ArchiLogger.LogNullError(nameof(response)); - return null; + return Environment.NewLine + "<" + BotName + "> " + response; } private static string FormatStaticResponse(string response) { - if (!string.IsNullOrEmpty(response)) { - return Environment.NewLine + response; + if (string.IsNullOrEmpty(response)) { + ASF.ArchiLogger.LogNullError(nameof(response)); + return null; } - ASF.ArchiLogger.LogNullError(nameof(response)); - return null; + return Environment.NewLine + response; } private ulong GetFirstSteamMasterID() => BotConfig.SteamUserPermissions.Where(kv => (kv.Key != 0) && (kv.Value == BotConfig.EPermission.Master)).Select(kv => kv.Key).OrderByDescending(steamID => steamID != CachedSteamID).ThenBy(steamID => steamID).FirstOrDefault(); private BotConfig.EPermission GetSteamUserPermission(ulong steamID) { - if (steamID != 0) { - return BotConfig.SteamUserPermissions.TryGetValue(steamID, out BotConfig.EPermission permission) ? permission : BotConfig.EPermission.None; + if (steamID == 0) { + ArchiLogger.LogNullError(nameof(steamID)); + return BotConfig.EPermission.None; } - ArchiLogger.LogNullError(nameof(steamID)); - return BotConfig.EPermission.None; + return BotConfig.SteamUserPermissions.TryGetValue(steamID, out BotConfig.EPermission permission) ? permission : BotConfig.EPermission.None; } private void HandleCallbacks() { @@ -1460,8 +1456,7 @@ namespace ArchiSteamFarm { SetUserInput(ASF.EUserInputType.Password, steamPassword); } - const bool result = true; - return result; + return true; } private void InitModules() { @@ -1525,8 +1520,7 @@ namespace ArchiSteamFarm { } // This should have reference to lowest permission for command execution - bool result = Bots.Values.Any(bot => bot.IsFamilySharing(steamID)); - return result; + return Bots.Values.Any(bot => bot.IsFamilySharing(steamID)); } private bool IsFamilySharing(ulong steamID) { @@ -1543,12 +1537,12 @@ namespace ArchiSteamFarm { } private bool IsMasterClanID(ulong steamID) { - if (steamID != 0) { - return steamID == BotConfig.SteamMasterClanID; + if (steamID == 0) { + ArchiLogger.LogNullError(nameof(steamID)); + return false; } - ArchiLogger.LogNullError(nameof(steamID)); - return false; + return steamID == BotConfig.SteamMasterClanID; } private bool IsOperator(ulong steamID) { @@ -1565,12 +1559,12 @@ namespace ArchiSteamFarm { } private static bool IsOwner(ulong steamID) { - if (steamID != 0) { - return (steamID == Program.GlobalConfig.SteamOwnerID) || (Debugging.IsDebugBuild && (steamID == SharedInfo.ArchiSteamID)); + if (steamID == 0) { + ASF.ArchiLogger.LogNullError(nameof(steamID)); + return false; } - ASF.ArchiLogger.LogNullError(nameof(steamID)); - return false; + return (steamID == Program.GlobalConfig.SteamOwnerID) || (Debugging.IsDebugBuild && (steamID == SharedInfo.ArchiSteamID)); } private static bool IsRefundable(EPaymentMethod method) { @@ -1595,12 +1589,12 @@ namespace ArchiSteamFarm { } private static bool IsValidCdKey(string key) { - if (!string.IsNullOrEmpty(key)) { - return Regex.IsMatch(key, @"^[0-9A-Z]{4,7}-[0-9A-Z]{4,7}-[0-9A-Z]{4,7}(?:(?:-[0-9A-Z]{4,7})?(?:-[0-9A-Z]{4,7}))?$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); + if (string.IsNullOrEmpty(key)) { + ASF.ArchiLogger.LogNullError(nameof(key)); + return false; } - ASF.ArchiLogger.LogNullError(nameof(key)); - return false; + return Regex.IsMatch(key, @"^[0-9A-Z]{4,7}-[0-9A-Z]{4,7}-[0-9A-Z]{4,7}(?:(?:-[0-9A-Z]{4,7})?(?:-[0-9A-Z]{4,7}))?$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); } private void JoinMasterChat() { @@ -3055,7 +3049,6 @@ namespace ArchiSteamFarm { } Utilities.InBackground(CardsFarmer.StartFarming); - return FormatBotResponse(Strings.Done); } @@ -3091,12 +3084,12 @@ namespace ArchiSteamFarm { } private string ResponseHelp(ulong steamID) { - if (steamID != 0) { - return IsFamilySharing(steamID) ? FormatBotResponse("https://github.com/" + SharedInfo.GithubRepo + "/wiki/Commands") : null; + if (steamID == 0) { + ArchiLogger.LogNullError(nameof(steamID)); + return null; } - ArchiLogger.LogNullError(nameof(steamID)); - return null; + return IsFamilySharing(steamID) ? FormatBotResponse("https://github.com/" + SharedInfo.GithubRepo + "/wiki/Commands") : null; } private string ResponseIdleBlacklist(ulong steamID) { @@ -4771,12 +4764,12 @@ namespace ArchiSteamFarm { } private string ResponseUnknown(ulong steamID) { - if (steamID != 0) { - return IsOperator(steamID) ? FormatBotResponse(Strings.UnknownCommand) : null; + if (steamID == 0) { + ArchiLogger.LogNullError(nameof(steamID)); + return null; } - ArchiLogger.LogNullError(nameof(steamID)); - return null; + return IsOperator(steamID) ? FormatBotResponse(Strings.UnknownCommand) : null; } private async Task ResponseUnpackBoosters(ulong steamID) { @@ -4862,12 +4855,12 @@ namespace ArchiSteamFarm { } private string ResponseVersion(ulong steamID) { - if (steamID != 0) { - return IsOperator(steamID) ? FormatBotResponse(string.Format(Strings.BotVersion, SharedInfo.ASF, SharedInfo.Version)) : null; + if (steamID == 0) { + ArchiLogger.LogNullError(nameof(steamID)); + return null; } - ArchiLogger.LogNullError(nameof(steamID)); - return null; + return IsOperator(steamID) ? FormatBotResponse(string.Format(Strings.BotVersion, SharedInfo.ASF, SharedInfo.Version)) : null; } private async Task SendMessageToChannel(ulong steamID, string message) { diff --git a/ArchiSteamFarm/BotDatabase.cs b/ArchiSteamFarm/BotDatabase.cs index c45170b66..9909bb1bf 100644 --- a/ArchiSteamFarm/BotDatabase.cs +++ b/ArchiSteamFarm/BotDatabase.cs @@ -68,7 +68,6 @@ namespace ArchiSteamFarm { } FilePath = filePath; - Save().Wait(); } // This constructor is used only by deserializer @@ -165,8 +164,7 @@ namespace ArchiSteamFarm { return false; } - bool result = IdlingBlacklistedAppIDs.Contains(appID); - return result; + return IdlingBlacklistedAppIDs.Contains(appID); } internal bool IsBlacklistedFromTrades(ulong steamID) { @@ -175,8 +173,7 @@ namespace ArchiSteamFarm { return false; } - bool result = BlacklistedFromTradesSteamIDs.Contains(steamID); - return result; + return BlacklistedFromTradesSteamIDs.Contains(steamID); } internal bool IsPriorityIdling(uint appID) { @@ -185,8 +182,7 @@ namespace ArchiSteamFarm { return false; } - bool result = IdlingPriorityAppIDs.Contains(appID); - return result; + return IdlingPriorityAppIDs.Contains(appID); } internal static BotDatabase Load(string filePath) { diff --git a/ArchiSteamFarm/CardsFarmer.cs b/ArchiSteamFarm/CardsFarmer.cs index 7f372b945..6f0cb666d 100755 --- a/ArchiSteamFarm/CardsFarmer.cs +++ b/ArchiSteamFarm/CardsFarmer.cs @@ -811,12 +811,12 @@ namespace ArchiSteamFarm { return 0; } - if (ushort.TryParse(match.Value, out ushort cardsRemaining) && (cardsRemaining != 0)) { - return cardsRemaining; + if (!ushort.TryParse(match.Value, out ushort cardsRemaining) || (cardsRemaining == 0)) { + Bot.ArchiLogger.LogNullError(nameof(cardsRemaining)); + return null; } - Bot.ArchiLogger.LogNullError(nameof(cardsRemaining)); - return null; + return cardsRemaining; } private async Task IsAnythingToFarm() { diff --git a/ArchiSteamFarm/GlobalConfig.cs b/ArchiSteamFarm/GlobalConfig.cs index 107f9ab09..dfe33f6ae 100644 --- a/ArchiSteamFarm/GlobalConfig.cs +++ b/ArchiSteamFarm/GlobalConfig.cs @@ -170,8 +170,7 @@ namespace ArchiSteamFarm { } } - GlobalConfig result = globalConfig; - return result; + return globalConfig; } internal static async Task Write(string filePath, GlobalConfig globalConfig) { diff --git a/ArchiSteamFarm/GlobalDatabase.cs b/ArchiSteamFarm/GlobalDatabase.cs index 926aa4fc3..4bed70d53 100644 --- a/ArchiSteamFarm/GlobalDatabase.cs +++ b/ArchiSteamFarm/GlobalDatabase.cs @@ -54,7 +54,6 @@ namespace ArchiSteamFarm { } FilePath = filePath; - Save().Wait(); } // This constructor is used only by deserializer @@ -75,8 +74,7 @@ namespace ArchiSteamFarm { return null; } - HashSet result = new HashSet(PackagesData.Where(package => (package.Value.AppIDs != null) && package.Value.AppIDs.Contains(appID)).Select(package => package.Key)); - return result; + return new HashSet(PackagesData.Where(package => (package.Value.AppIDs != null) && package.Value.AppIDs.Contains(appID)).Select(package => package.Key)); } internal static GlobalDatabase Load(string filePath) { diff --git a/ArchiSteamFarm/Json/Steam.cs b/ArchiSteamFarm/Json/Steam.cs index 1ed5c063a..31f8a36f5 100644 --- a/ArchiSteamFarm/Json/Steam.cs +++ b/ArchiSteamFarm/Json/Steam.cs @@ -209,12 +209,12 @@ namespace ArchiSteamFarm.Json { } id = id.Substring(index); - if (ulong.TryParse(id, out _TradeOfferID) && (_TradeOfferID != 0)) { - return _TradeOfferID; + if (!ulong.TryParse(id, out _TradeOfferID) || (_TradeOfferID == 0)) { + ASF.ArchiLogger.LogNullError(nameof(_TradeOfferID)); + return 0; } - ASF.ArchiLogger.LogNullError(nameof(_TradeOfferID)); - return 0; + return _TradeOfferID; } } @@ -292,12 +292,12 @@ namespace ArchiSteamFarm.Json { return 0; } - if (uint.TryParse(miniProfile, out _OtherSteamID3) && (_OtherSteamID3 != 0)) { - return _OtherSteamID3; + if (!uint.TryParse(miniProfile, out _OtherSteamID3) || (_OtherSteamID3 == 0)) { + ASF.ArchiLogger.LogNullError(nameof(_OtherSteamID3)); + return 0; } - ASF.ArchiLogger.LogNullError(nameof(_OtherSteamID3)); - return 0; + return _OtherSteamID3; } } @@ -554,8 +554,7 @@ namespace ArchiSteamFarm.Json { return false; } - bool result = ItemsToGive.All(item => (item.AppID == Asset.SteamAppID) && (item.ContextID == Asset.SteamCommunityContextID) && acceptedTypes.Contains(item.Type)); - return result; + return ItemsToGive.All(item => (item.AppID == Asset.SteamAppID) && (item.ContextID == Asset.SteamCommunityContextID) && acceptedTypes.Contains(item.Type)); } [SuppressMessage("ReSharper", "UnusedMember.Global")] diff --git a/ArchiSteamFarm/MobileAuthenticator.cs b/ArchiSteamFarm/MobileAuthenticator.cs index e92923b95..a2b1d8410 100644 --- a/ArchiSteamFarm/MobileAuthenticator.cs +++ b/ArchiSteamFarm/MobileAuthenticator.cs @@ -81,12 +81,12 @@ namespace ArchiSteamFarm { internal async Task GenerateToken() { uint time = await GetSteamTime().ConfigureAwait(false); - if (time != 0) { - return GenerateTokenForTime(time); + if (time == 0) { + Bot.ArchiLogger.LogNullError(nameof(time)); + return null; } - Bot.ArchiLogger.LogNullError(nameof(time)); - return null; + return GenerateTokenForTime(time); } internal async Task GetConfirmationDetails(Confirmation confirmation) { diff --git a/ArchiSteamFarm/SteamSaleEvent.cs b/ArchiSteamFarm/SteamSaleEvent.cs index c995c3301..84cc82f47 100644 --- a/ArchiSteamFarm/SteamSaleEvent.cs +++ b/ArchiSteamFarm/SteamSaleEvent.cs @@ -99,8 +99,7 @@ namespace ArchiSteamFarm { Bot.ArchiLogger.LogGenericTrace(text); // It'd make more sense to check against "Come back tomorrow", but it might not cover out-of-the-event queue - bool result = text.StartsWith("You can get ", StringComparison.Ordinal); - return result; + return text.StartsWith("You can get ", StringComparison.Ordinal); } private async Task VoteForSteamAwards() { diff --git a/ArchiSteamFarm/Utilities.cs b/ArchiSteamFarm/Utilities.cs index a7214cfcd..6dde439db 100644 --- a/ArchiSteamFarm/Utilities.cs +++ b/ArchiSteamFarm/Utilities.cs @@ -40,8 +40,7 @@ namespace ArchiSteamFarm { return null; } - string result = string.Join(delimiter, GetArgs(args, argsToSkip)); - return result; + return string.Join(delimiter, GetArgs(args, argsToSkip)); } internal static string GetCookieValue(this CookieContainer cookieContainer, string url, string name) { @@ -60,7 +59,7 @@ namespace ArchiSteamFarm { } CookieCollection cookies = cookieContainer.GetCookies(uri); - return cookies.Count != 0 ? (from Cookie cookie in cookies where cookie.Name.Equals(name) select cookie.Value).FirstOrDefault() : null; + return cookies.Count > 0 ? (from Cookie cookie in cookies where cookie.Name.Equals(name) select cookie.Value).FirstOrDefault() : null; } internal static string GetUnifiedName(this Type type) { diff --git a/ArchiSteamFarm/WebBrowser.cs b/ArchiSteamFarm/WebBrowser.cs index 22c0ba5df..802cdf1c4 100644 --- a/ArchiSteamFarm/WebBrowser.cs +++ b/ArchiSteamFarm/WebBrowser.cs @@ -102,13 +102,13 @@ namespace ArchiSteamFarm { response = await UrlGetToBinaryWithProgress(request, referer).ConfigureAwait(false); } - if (response != null) { - return response; + if (response == null) { + ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, MaxTries)); + ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); + return null; } - ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, MaxTries)); - ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); - return null; + return response; } internal async Task UrlGetToHtmlDocumentRetry(string request, string referer = null) { @@ -186,13 +186,13 @@ namespace ArchiSteamFarm { response = await UrlHead(request, referer).ConfigureAwait(false); } - if (response != null) { - return response; + if (response == null) { + ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, MaxTries)); + ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); + return null; } - ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, MaxTries)); - ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); - return null; + return response; } internal async Task UrlPost(string request, IReadOnlyCollection> data = null, string referer = null) { @@ -218,13 +218,13 @@ namespace ArchiSteamFarm { response = await UrlPost(request, data, referer).ConfigureAwait(false); } - if (response != null) { - return response; + if (response == null) { + ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, MaxTries)); + ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); + return null; } - ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, MaxTries)); - ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); - return null; + return response; } internal async Task UrlPostToHtmlDocumentRetry(string request, IReadOnlyCollection> data = null, string referer = null) { @@ -330,8 +330,7 @@ namespace ArchiSteamFarm { return null; } - HttpResponseMessage response = await UrlRequest(new Uri(request), HttpMethod.Get, null, referer, httpCompletionOptions).ConfigureAwait(false); - return response; + return await UrlRequest(new Uri(request), HttpMethod.Get, null, referer, httpCompletionOptions).ConfigureAwait(false); } private async Task UrlGetToString(string request, string referer = null) { @@ -357,13 +356,13 @@ namespace ArchiSteamFarm { response = await UrlGetToString(request, referer).ConfigureAwait(false); } - if (response != null) { - return response; + if (response == null) { + ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, MaxTries)); + ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); + return null; } - ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, MaxTries)); - ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); - return null; + return response; } private async Task UrlHead(string request, string referer = null) { @@ -383,8 +382,7 @@ namespace ArchiSteamFarm { return null; } - HttpResponseMessage response = await UrlRequest(new Uri(request), HttpMethod.Head, null, referer).ConfigureAwait(false); - return response; + return await UrlRequest(new Uri(request), HttpMethod.Head, null, referer).ConfigureAwait(false); } private async Task UrlPostToHttp(string request, IReadOnlyCollection> data = null, string referer = null) { @@ -393,8 +391,7 @@ namespace ArchiSteamFarm { return null; } - HttpResponseMessage response = await UrlRequest(new Uri(request), HttpMethod.Post, data, referer).ConfigureAwait(false); - return response; + return await UrlRequest(new Uri(request), HttpMethod.Post, data, referer).ConfigureAwait(false); } private async Task UrlPostToString(string request, IReadOnlyCollection> data = null, string referer = null) { @@ -420,13 +417,13 @@ namespace ArchiSteamFarm { response = await UrlPostToString(request, data, referer).ConfigureAwait(false); } - if (response != null) { - return response; + if (response == null) { + ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, MaxTries)); + ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); + return null; } - ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, MaxTries)); - ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); - return null; + return response; } private async Task UrlRequest(Uri requestUri, HttpMethod httpMethod, IReadOnlyCollection> data = null, string referer = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead, byte maxRedirections = MaxTries) { @@ -493,13 +490,12 @@ namespace ArchiSteamFarm { } using (response) { - if (!Debugging.IsDebugBuild) { - return null; + if (Debugging.IsDebugBuild) { + ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, requestUri)); + ArchiLogger.LogGenericDebug(string.Format(Strings.StatusCode, response.StatusCode)); + ArchiLogger.LogGenericDebug(string.Format(Strings.Content, await response.Content.ReadAsStringAsync().ConfigureAwait(false))); } - ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, requestUri)); - ArchiLogger.LogGenericDebug(string.Format(Strings.StatusCode, response.StatusCode)); - ArchiLogger.LogGenericDebug(string.Format(Strings.Content, await response.Content.ReadAsStringAsync().ConfigureAwait(false))); return null; } }