diff --git a/ArchiSteamFarm/ArchiHandler.cs b/ArchiSteamFarm/ArchiHandler.cs index 65b9db445..c0dca2511 100644 --- a/ArchiSteamFarm/ArchiHandler.cs +++ b/ArchiSteamFarm/ArchiHandler.cs @@ -149,6 +149,7 @@ namespace ArchiSteamFarm { KeyValue receiptInfo = new KeyValue(); using (MemoryStream ms = new MemoryStream(msg.purchase_receipt_info)) { if (!receiptInfo.TryReadAsBinary(ms)) { + Logging.LogNullError(nameof(ms)); return; } @@ -202,7 +203,12 @@ namespace ArchiSteamFarm { } internal void PlayGames(ICollection gameIDs) { - if ((gameIDs == null) || !Client.IsConnected) { + if (gameIDs == null) { + Logging.LogNullError(nameof(gameIDs), Bot.BotName); + return; + } + + if (!Client.IsConnected) { return; } @@ -217,7 +223,12 @@ namespace ArchiSteamFarm { } internal async Task RedeemKey(string key) { - if (string.IsNullOrEmpty(key) || !Client.IsConnected) { + if (string.IsNullOrEmpty(key)) { + Logging.LogNullError(nameof(key), Bot.BotName); + return null; + } + + if (!Client.IsConnected) { return null; } @@ -281,6 +292,7 @@ namespace ArchiSteamFarm { public override void HandleMsg(IPacketMsg packetMsg) { if (packetMsg == null) { + Logging.LogNullError(nameof(packetMsg), Bot.BotName); return; } @@ -305,6 +317,7 @@ namespace ArchiSteamFarm { private void HandleFSOfflineMessageNotification(IPacketMsg packetMsg) { if (packetMsg == null) { + Logging.LogNullError(nameof(packetMsg), Bot.BotName); return; } @@ -314,6 +327,7 @@ namespace ArchiSteamFarm { private void HandleItemAnnouncements(IPacketMsg packetMsg) { if (packetMsg == null) { + Logging.LogNullError(nameof(packetMsg), Bot.BotName); return; } @@ -323,6 +337,7 @@ namespace ArchiSteamFarm { private void HandlePlayingSessionState(IPacketMsg packetMsg) { if (packetMsg == null) { + Logging.LogNullError(nameof(packetMsg), Bot.BotName); return; } @@ -332,6 +347,7 @@ namespace ArchiSteamFarm { private void HandlePurchaseResponse(IPacketMsg packetMsg) { if (packetMsg == null) { + Logging.LogNullError(nameof(packetMsg), Bot.BotName); return; } @@ -341,6 +357,7 @@ namespace ArchiSteamFarm { private void HandleUserNotifications(IPacketMsg packetMsg) { if (packetMsg == null) { + Logging.LogNullError(nameof(packetMsg), Bot.BotName); return; } diff --git a/ArchiSteamFarm/ArchiWebHandler.cs b/ArchiSteamFarm/ArchiWebHandler.cs index 841f5118e..3a81823e2 100644 --- a/ArchiSteamFarm/ArchiWebHandler.cs +++ b/ArchiSteamFarm/ArchiWebHandler.cs @@ -27,6 +27,7 @@ using HtmlAgilityPack; using SteamKit2; using System; using System.Collections.Generic; +using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; @@ -55,20 +56,28 @@ namespace ArchiSteamFarm { private static uint GetAppIDFromMarketHashName(string hashName) { if (string.IsNullOrEmpty(hashName)) { + Logging.LogNullError(nameof(hashName)); return 0; } int index = hashName.IndexOf('-'); if (index < 1) { + Logging.LogNullError(nameof(index)); return 0; } uint appID; - return !uint.TryParse(hashName.Substring(0, index), out appID) ? 0 : appID; + if (uint.TryParse(hashName.Substring(0, index), out appID)) { + return appID; + } + + Logging.LogNullError(nameof(appID)); + return 0; } private static Steam.Item.EType GetItemType(string name) { if (string.IsNullOrEmpty(name)) { + Logging.LogNullError(nameof(name)); return Steam.Item.EType.Unknown; } @@ -110,6 +119,7 @@ namespace ArchiSteamFarm { internal bool Init(SteamClient steamClient, string webAPIUserNonce, string parentalPin) { if ((steamClient == null) || string.IsNullOrEmpty(webAPIUserNonce)) { + Logging.LogNullError(nameof(steamClient) + " || " + nameof(webAPIUserNonce), Bot.BotName); return false; } @@ -158,6 +168,7 @@ namespace ArchiSteamFarm { } if (authResult == null) { + Logging.LogNullError(nameof(authResult), Bot.BotName); return false; } @@ -181,6 +192,7 @@ namespace ArchiSteamFarm { internal async Task AcceptGift(ulong gid) { if (gid == 0) { + Logging.LogNullError(nameof(gid), Bot.BotName); return false; } @@ -190,7 +202,7 @@ namespace ArchiSteamFarm { string sessionID = WebBrowser.CookieContainer.GetCookieValue(SteamCommunityURL, "sessionid"); if (string.IsNullOrEmpty(sessionID)) { - Logging.LogNullError("sessionID"); + Logging.LogNullError(nameof(sessionID), Bot.BotName); return false; } @@ -199,21 +211,12 @@ namespace ArchiSteamFarm { { "sessionid", sessionID } }; - bool result = false; - for (byte i = 0; (i < WebBrowser.MaxRetries) && !result; i++) { - result = await WebBrowser.UrlPost(request, data).ConfigureAwait(false); - } - - if (result) { - return true; - } - - Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName); - return false; + return await WebBrowser.UrlPostRetry(request, data).ConfigureAwait(false); } internal async Task JoinGroup(ulong groupID) { if (groupID == 0) { + Logging.LogNullError(nameof(groupID), Bot.BotName); return false; } @@ -223,7 +226,7 @@ namespace ArchiSteamFarm { string sessionID = WebBrowser.CookieContainer.GetCookieValue(SteamCommunityURL, "sessionid"); if (string.IsNullOrEmpty(sessionID)) { - Logging.LogNullError("sessionID"); + Logging.LogNullError(nameof(sessionID), Bot.BotName); return false; } @@ -233,17 +236,7 @@ namespace ArchiSteamFarm { { "action", "join" } }; - bool result = false; - for (byte i = 0; (i < WebBrowser.MaxRetries) && !result; i++) { - result = await WebBrowser.UrlPost(request, data).ConfigureAwait(false); - } - - if (result) { - return true; - } - - Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName); - return false; + return await WebBrowser.UrlPostRetry(request, data).ConfigureAwait(false); } internal async Task> GetOwnedGames() { @@ -253,13 +246,8 @@ namespace ArchiSteamFarm { string request = SteamCommunityURL + "/my/games/?xml=1"; - XmlDocument response = null; - for (byte i = 0; (i < WebBrowser.MaxRetries) && (response == null); i++) { - response = await WebBrowser.UrlGetToXML(request).ConfigureAwait(false); - } - + XmlDocument response = await WebBrowser.UrlGetToXMLRetry(request).ConfigureAwait(false); if (response == null) { - Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName); return null; } @@ -272,16 +260,19 @@ namespace ArchiSteamFarm { foreach (XmlNode xmlNode in xmlNodeList) { XmlNode appNode = xmlNode.SelectSingleNode("appID"); if (appNode == null) { + Logging.LogNullError(nameof(appNode), Bot.BotName); continue; } uint appID; if (!uint.TryParse(appNode.InnerText, out appID)) { + Logging.LogNullError(nameof(appID), Bot.BotName); continue; } XmlNode nameNode = xmlNode.SelectSingleNode("name"); if (nameNode == null) { + Logging.LogNullError(nameof(nameNode), Bot.BotName); continue; } @@ -293,6 +284,7 @@ namespace ArchiSteamFarm { internal Dictionary GetOwnedGames(ulong steamID) { if ((steamID == 0) || string.IsNullOrEmpty(Bot.BotConfig.SteamApiKey)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(Bot.BotConfig.SteamApiKey), Bot.BotName); return null; } @@ -308,7 +300,7 @@ namespace ArchiSteamFarm { secure: !Program.GlobalConfig.ForceHttp ); } catch (Exception e) { - Logging.LogGenericException(e); + Logging.LogGenericException(e, Bot.BotName); } } } @@ -322,6 +314,7 @@ namespace ArchiSteamFarm { foreach (KeyValue game in response["games"].Children) { uint appID = (uint) game["appid"].AsUnsignedLong(); if (appID == 0) { + Logging.LogNullError(nameof(appID)); continue; } @@ -333,6 +326,7 @@ namespace ArchiSteamFarm { internal HashSet GetTradeOffers() { if (string.IsNullOrEmpty(Bot.BotConfig.SteamApiKey)) { + Logging.LogNullError(nameof(Bot.BotConfig.SteamApiKey), Bot.BotName); return null; } @@ -363,6 +357,7 @@ namespace ArchiSteamFarm { foreach (KeyValue description in response["descriptions"].Children) { ulong classID = description["classid"].AsUnsignedLong(); if (classID == 0) { + Logging.LogNullError(nameof(classID), Bot.BotName); continue; } @@ -447,6 +442,7 @@ namespace ArchiSteamFarm { internal async Task AcceptTradeOffer(ulong tradeID) { if (tradeID == 0) { + Logging.LogNullError(nameof(tradeID), Bot.BotName); return false; } @@ -456,7 +452,7 @@ namespace ArchiSteamFarm { string sessionID = WebBrowser.CookieContainer.GetCookieValue(SteamCommunityURL, "sessionid"); if (string.IsNullOrEmpty(sessionID)) { - Logging.LogNullError("sessionID"); + Logging.LogNullError(nameof(sessionID), Bot.BotName); return false; } @@ -468,17 +464,7 @@ namespace ArchiSteamFarm { { "tradeofferid", tradeID.ToString() } }; - bool result = false; - for (byte i = 0; (i < WebBrowser.MaxRetries) && !result; i++) { - result = await WebBrowser.UrlPost(request, data, referer).ConfigureAwait(false); - } - - if (result) { - return true; - } - - Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName); - return false; + return await WebBrowser.UrlPostRetry(request, data, referer).ConfigureAwait(false); } internal async Task> GetMyTradableInventory() { @@ -492,40 +478,39 @@ namespace ArchiSteamFarm { while (true) { string request = SteamCommunityURL + "/my/inventory/json/" + Steam.Item.SteamAppID + "/" + Steam.Item.SteamContextID + "?trading=1&start=" + nextPage; - JObject jObject = null; - for (byte i = 0; (i < WebBrowser.MaxRetries) && (jObject == null); i++) { - jObject = await WebBrowser.UrlGetToJObject(request).ConfigureAwait(false); - } - + JObject jObject = await WebBrowser.UrlGetToJObjectRetry(request).ConfigureAwait(false); if (jObject == null) { - Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName); return null; } IEnumerable descriptions = jObject.SelectTokens("$.rgDescriptions.*"); if (descriptions == null) { - return null; + return null; // OK, empty inventory } Dictionary, Tuple> descriptionMap = new Dictionary, Tuple>(); foreach (JToken description in descriptions) { string classIDString = description["classid"].ToString(); if (string.IsNullOrEmpty(classIDString)) { + Logging.LogNullError(nameof(classIDString), Bot.BotName); continue; } ulong classID; if (!ulong.TryParse(classIDString, out classID) || (classID == 0)) { + Logging.LogNullError(nameof(classID), Bot.BotName); continue; } string instanceIDString = description["instanceid"].ToString(); if (string.IsNullOrEmpty(instanceIDString)) { + Logging.LogNullError(nameof(instanceIDString), Bot.BotName); continue; } ulong instanceID; if (!ulong.TryParse(instanceIDString, out instanceID)) { + Logging.LogNullError(nameof(instanceID), Bot.BotName); continue; } @@ -552,6 +537,7 @@ namespace ArchiSteamFarm { IEnumerable items = jObject.SelectTokens("$.rgInventory.*"); if (items == null) { + Logging.LogNullError(nameof(items), Bot.BotName); return null; } @@ -567,6 +553,7 @@ namespace ArchiSteamFarm { } if (steamItem == null) { + Logging.LogNullError(nameof(steamItem), Bot.BotName); continue; } @@ -583,12 +570,15 @@ namespace ArchiSteamFarm { bool more; if (!bool.TryParse(jObject["more"].ToString(), out more) || !more) { - break; + break; // OK, last page } - if (!ushort.TryParse(jObject["more_start"].ToString(), out nextPage)) { - break; + if (ushort.TryParse(jObject["more_start"].ToString(), out nextPage)) { + continue; } + + Logging.LogNullError(nameof(nextPage), Bot.BotName); + break; } return result; @@ -596,6 +586,7 @@ namespace ArchiSteamFarm { internal async Task SendTradeOffer(HashSet inventory, ulong partnerID, string token = null) { if ((inventory == null) || (inventory.Count == 0) || (partnerID == 0)) { + Logging.LogNullError(nameof(inventory) + " || " + nameof(inventory.Count) + " || " + nameof(partnerID), Bot.BotName); return false; } @@ -605,7 +596,7 @@ namespace ArchiSteamFarm { string sessionID = WebBrowser.CookieContainer.GetCookieValue(SteamCommunityURL, "sessionid"); if (string.IsNullOrEmpty(sessionID)) { - Logging.LogNullError("sessionID"); + Logging.LogNullError(nameof(sessionID), Bot.BotName); return false; } @@ -636,27 +627,17 @@ namespace ArchiSteamFarm { string referer = SteamCommunityURL + "/tradeoffer/new"; string request = referer + "/send"; - foreach (Steam.TradeOfferRequest trade in trades) { - Dictionary data = new Dictionary(6) { - { "sessionid", sessionID }, - { "serverid", "1" }, - { "partner", partnerID.ToString() }, - { "tradeoffermessage", "Sent by ASF" }, - { "json_tradeoffer", JsonConvert.SerializeObject(trade) }, - { "trade_offer_create_params", string.IsNullOrEmpty(token) ? "" : $"{{\"trade_offer_access_token\":\"{token}\"}}" } - }; - - bool result = false; - for (byte i = 0; (i < WebBrowser.MaxRetries) && !result; i++) { - result = await WebBrowser.UrlPost(request, data, referer).ConfigureAwait(false); + foreach (Dictionary data in trades.Select(trade => new Dictionary(6) { + { "sessionid", sessionID }, + { "serverid", "1" }, + { "partner", partnerID.ToString() }, + { "tradeoffermessage", "Sent by ASF" }, + { "json_tradeoffer", JsonConvert.SerializeObject(trade) }, + { "trade_offer_create_params", string.IsNullOrEmpty(token) ? "" : $"{{\"trade_offer_access_token\":\"{token}\"}}" } + })) { + if (!await WebBrowser.UrlPostRetry(request, data, referer).ConfigureAwait(false)) { + return false; } - - if (result) { - continue; - } - - Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName); - return false; } return true; @@ -664,6 +645,7 @@ namespace ArchiSteamFarm { internal async Task GetBadgePage(byte page) { if (page == 0) { + Logging.LogNullError(nameof(page), Bot.BotName); return null; } @@ -673,21 +655,12 @@ namespace ArchiSteamFarm { string request = SteamCommunityURL + "/my/badges?p=" + page; - HtmlDocument htmlDocument = null; - for (byte i = 0; (i < WebBrowser.MaxRetries) && (htmlDocument == null); i++) { - htmlDocument = await WebBrowser.UrlGetToHtmlDocument(request).ConfigureAwait(false); - } - - if (htmlDocument != null) { - return htmlDocument; - } - - Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName); - return null; + return await WebBrowser.UrlGetToHtmlDocumentRetry(request).ConfigureAwait(false); } internal async Task GetGameCardsPage(ulong appID) { if (appID == 0) { + Logging.LogNullError(nameof(appID), Bot.BotName); return null; } @@ -697,17 +670,7 @@ namespace ArchiSteamFarm { string request = SteamCommunityURL + "/my/gamecards/" + appID; - HtmlDocument htmlDocument = null; - for (byte i = 0; (i < WebBrowser.MaxRetries) && (htmlDocument == null); i++) { - htmlDocument = await WebBrowser.UrlGetToHtmlDocument(request).ConfigureAwait(false); - } - - if (htmlDocument != null) { - return htmlDocument; - } - - Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName); - return null; + return await WebBrowser.UrlGetToHtmlDocumentRetry(request).ConfigureAwait(false); } internal async Task MarkInventory() { @@ -717,33 +680,18 @@ namespace ArchiSteamFarm { string request = SteamCommunityURL + "/my/inventory"; - bool result = false; - for (byte i = 0; (i < WebBrowser.MaxRetries) && !result; i++) { - result = await WebBrowser.UrlHead(request).ConfigureAwait(false); - } - - if (result) { - return true; - } - - Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName); - return false; + return await WebBrowser.UrlHeadRetry(request).ConfigureAwait(false); } private async Task IsLoggedIn() { string request = SteamCommunityURL + "/my/profile"; - Uri uri = null; - for (byte i = 0; (i < WebBrowser.MaxRetries) && (uri == null); i++) { - uri = await WebBrowser.UrlHeadToUri(request).ConfigureAwait(false); + Uri uri = await WebBrowser.UrlHeadToUriRetry(request).ConfigureAwait(false); + if (uri == null) { + return null; } - if (uri != null) { - return !uri.AbsolutePath.StartsWith("/login", StringComparison.Ordinal); - } - - Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName); - return null; + return !uri.AbsolutePath.StartsWith("/login", StringComparison.Ordinal); } private async Task RefreshSessionIfNeeded() { @@ -774,7 +722,12 @@ namespace ArchiSteamFarm { } private async Task UnlockParentalAccount(string parentalPin) { - if (string.IsNullOrEmpty(parentalPin) || parentalPin.Equals("0")) { + if (string.IsNullOrEmpty(parentalPin)) { + Logging.LogNullError(nameof(parentalPin), Bot.BotName); + return false; + } + + if (parentalPin.Equals("0")) { return true; } @@ -785,13 +738,9 @@ namespace ArchiSteamFarm { { "pin", parentalPin } }; - bool result = false; - for (byte i = 0; (i < WebBrowser.MaxRetries) && !result; i++) { - result = await WebBrowser.UrlPost(request, data, SteamCommunityURL).ConfigureAwait(false); - } - + bool result = await WebBrowser.UrlPostRetry(request, data, SteamCommunityURL).ConfigureAwait(false); if (!result) { - Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName); + Logging.LogGenericInfo("Failed!", Bot.BotName); return false; } diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index ce9cae48f..05184aa19 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -93,14 +93,22 @@ namespace ArchiSteamFarm { } private static bool IsOwner(ulong steamID) { - if (steamID == 0) { - return false; + if (steamID != 0) { + return steamID == Program.GlobalConfig.SteamOwnerID; } - return steamID == Program.GlobalConfig.SteamOwnerID; + Logging.LogNullError(nameof(steamID)); + return false; } - private static bool IsValidCdKey(string key) => !string.IsNullOrEmpty(key) && Regex.IsMatch(key, @"[0-9A-Z]{4,5}-[0-9A-Z]{4,5}-[0-9A-Z]{4,5}-?(?:(?:[0-9A-Z]{4,5}-?)?(?:[0-9A-Z]{4,5}))?"); + private static bool IsValidCdKey(string key) { + if (!string.IsNullOrEmpty(key)) { + return Regex.IsMatch(key, @"[0-9A-Z]{4,5}-[0-9A-Z]{4,5}-[0-9A-Z]{4,5}-?(?:(?:[0-9A-Z]{4,5}-?)?(?:[0-9A-Z]{4,5}))?"); + } + + Logging.LogNullError(nameof(key)); + return false; + } private static async Task LimitLoginRequestsAsync() { await LoginSemaphore.WaitAsync().ConfigureAwait(false); @@ -126,6 +134,7 @@ namespace ArchiSteamFarm { } if (!BotConfig.Enabled) { + Logging.LogGenericInfo("Not starting this instance because it's disabled in config file", botName); return; } @@ -327,6 +336,7 @@ namespace ArchiSteamFarm { internal async Task Response(ulong steamID, string message) { if ((steamID == 0) || string.IsNullOrEmpty(message)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(message), BotName); return null; } @@ -449,11 +459,12 @@ namespace ArchiSteamFarm { } private bool IsMaster(ulong steamID) { - if (steamID == 0) { - return false; + if (steamID != 0) { + return (steamID == BotConfig.SteamMasterID) || IsOwner(steamID); } - return (steamID == BotConfig.SteamMasterID) || IsOwner(steamID); + Logging.LogNullError(nameof(steamID), BotName); + return false; } private void ImportAuthenticator(string maFilePath) { @@ -462,6 +473,7 @@ namespace ArchiSteamFarm { } Logging.LogGenericInfo("Converting SDA .maFile into ASF format...", BotName); + try { BotDatabase.SteamGuardAccount = JsonConvert.DeserializeObject(File.ReadAllText(maFilePath)); File.Delete(maFilePath); @@ -522,7 +534,12 @@ namespace ArchiSteamFarm { } private async Task ResponsePause(ulong steamID) { - if ((steamID == 0) || !IsMaster(steamID)) { + if (steamID == 0) { + Logging.LogNullError(nameof(steamID)); + return null; + } + + if (!IsMaster(steamID)) { return null; } @@ -537,6 +554,7 @@ namespace ArchiSteamFarm { private static async Task ResponsePause(ulong steamID, string botName) { if ((steamID == 0) || string.IsNullOrEmpty(botName)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(botName)); return null; } @@ -553,7 +571,12 @@ namespace ArchiSteamFarm { } private string ResponseStatus(ulong steamID) { - if ((steamID == 0) || !IsMaster(steamID)) { + if (steamID == 0) { + Logging.LogNullError(nameof(steamID)); + return null; + } + + if (!IsMaster(steamID)) { return null; } @@ -578,6 +601,7 @@ namespace ArchiSteamFarm { private static string ResponseStatus(ulong steamID, string botName) { if ((steamID == 0) || string.IsNullOrEmpty(botName)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(botName)); return null; } @@ -595,6 +619,7 @@ namespace ArchiSteamFarm { private static string ResponseStatusAll(ulong steamID) { if (steamID == 0) { + Logging.LogNullError(nameof(steamID)); return null; } @@ -617,7 +642,12 @@ namespace ArchiSteamFarm { } private async Task ResponseSendTrade(ulong steamID) { - if ((steamID == 0) || !IsMaster(steamID)) { + if (steamID == 0) { + Logging.LogNullError(nameof(steamID)); + return null; + } + + if (!IsMaster(steamID)) { return null; } @@ -650,6 +680,7 @@ namespace ArchiSteamFarm { private static async Task ResponseSendTrade(ulong steamID, string botName) { if ((steamID == 0) || string.IsNullOrEmpty(botName)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(botName)); return null; } @@ -666,7 +697,12 @@ namespace ArchiSteamFarm { } private string Response2FA(ulong steamID) { - if ((steamID == 0) || !IsMaster(steamID)) { + if (steamID == 0) { + Logging.LogNullError(nameof(steamID)); + return null; + } + + if (!IsMaster(steamID)) { return null; } @@ -680,6 +716,7 @@ namespace ArchiSteamFarm { private static string Response2FA(ulong steamID, string botName) { if ((steamID == 0) || string.IsNullOrEmpty(botName)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(botName)); return null; } @@ -696,7 +733,12 @@ namespace ArchiSteamFarm { } private string Response2FAOff(ulong steamID) { - if ((steamID == 0) || !IsMaster(steamID)) { + if (steamID == 0) { + Logging.LogNullError(nameof(steamID)); + return null; + } + + if (!IsMaster(steamID)) { return null; } @@ -709,6 +751,7 @@ namespace ArchiSteamFarm { private static string Response2FAOff(ulong steamID, string botName) { if ((steamID == 0) || string.IsNullOrEmpty(botName)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(botName)); return null; } @@ -725,7 +768,12 @@ namespace ArchiSteamFarm { } private async Task Response2FAConfirm(ulong steamID, bool confirm) { - if ((steamID == 0) || !IsMaster(steamID)) { + if (steamID == 0) { + Logging.LogNullError(nameof(steamID)); + return null; + } + + if (!IsMaster(steamID)) { return null; } @@ -739,6 +787,7 @@ namespace ArchiSteamFarm { private static async Task Response2FAConfirm(ulong steamID, string botName, bool confirm) { if ((steamID == 0) || string.IsNullOrEmpty(botName)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(botName)); return null; } @@ -756,6 +805,7 @@ namespace ArchiSteamFarm { private static string ResponseExit(ulong steamID) { if (steamID == 0) { + Logging.LogNullError(nameof(steamID)); return null; } @@ -773,7 +823,12 @@ namespace ArchiSteamFarm { } private string ResponseFarm(ulong steamID) { - if ((steamID == 0) || !IsMaster(steamID)) { + if (steamID == 0) { + Logging.LogNullError(nameof(steamID)); + return null; + } + + if (!IsMaster(steamID)) { return null; } @@ -787,6 +842,7 @@ namespace ArchiSteamFarm { private static string ResponseFarm(ulong steamID, string botName) { if ((steamID == 0) || string.IsNullOrEmpty(botName)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(botName)); return null; } @@ -803,7 +859,12 @@ namespace ArchiSteamFarm { } private string ResponseHelp(ulong steamID) { - if ((steamID == 0) || !IsMaster(steamID)) { + if (steamID == 0) { + Logging.LogNullError(nameof(steamID)); + return null; + } + + if (!IsMaster(steamID)) { return null; } @@ -811,7 +872,12 @@ namespace ArchiSteamFarm { } private async Task ResponseRedeem(ulong steamID, string message, bool validate) { - if ((steamID == 0) || string.IsNullOrEmpty(message) || !IsMaster(steamID)) { + if ((steamID == 0) || string.IsNullOrEmpty(message)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(message)); + return null; + } + + if (!IsMaster(steamID)) { return null; } @@ -904,6 +970,7 @@ namespace ArchiSteamFarm { private static async Task ResponseRedeem(ulong steamID, string botName, string message, bool validate) { if ((steamID == 0) || string.IsNullOrEmpty(botName) || string.IsNullOrEmpty(message)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(botName) + " || " + nameof(message)); return null; } @@ -920,6 +987,7 @@ namespace ArchiSteamFarm { private static string ResponseRejoinChat(ulong steamID) { if (steamID == 0) { + Logging.LogNullError(nameof(steamID)); return null; } @@ -936,6 +1004,7 @@ namespace ArchiSteamFarm { private static string ResponseRestart(ulong steamID) { if (steamID == 0) { + Logging.LogNullError(nameof(steamID)); return null; } @@ -953,7 +1022,12 @@ namespace ArchiSteamFarm { } private async Task ResponseAddLicense(ulong steamID, ICollection gameIDs) { - if ((steamID == 0) || (gameIDs == null) || (gameIDs.Count == 0) || !SteamClient.IsConnected || !IsMaster(steamID)) { + if ((steamID == 0) || (gameIDs == null) || (gameIDs.Count == 0)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(gameIDs) + " || " + nameof(gameIDs.Count)); + return null; + } + + if (!SteamClient.IsConnected || !IsMaster(steamID)) { return null; } @@ -972,6 +1046,7 @@ namespace ArchiSteamFarm { private static async Task ResponseAddLicense(ulong steamID, string botName, string games) { if ((steamID == 0) || string.IsNullOrEmpty(botName) || string.IsNullOrEmpty(games)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(botName) + " || " + nameof(games)); return null; } @@ -1004,7 +1079,12 @@ namespace ArchiSteamFarm { } private async Task ResponseOwns(ulong steamID, string query) { - if ((steamID == 0) || string.IsNullOrEmpty(query) || !IsMaster(steamID)) { + if ((steamID == 0) || string.IsNullOrEmpty(query)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(query)); + return null; + } + + if (!IsMaster(steamID)) { return null; } @@ -1051,6 +1131,7 @@ namespace ArchiSteamFarm { private static async Task ResponseOwns(ulong steamID, string botName, string query) { if ((steamID == 0) || string.IsNullOrEmpty(botName) || string.IsNullOrEmpty(query)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(botName) + " || " + nameof(query)); return null; } @@ -1067,7 +1148,12 @@ namespace ArchiSteamFarm { } private async Task ResponsePlay(ulong steamID, HashSet gameIDs) { - if ((steamID == 0) || (gameIDs == null) || (gameIDs.Count == 0) || !IsMaster(steamID)) { + if ((steamID == 0) || (gameIDs == null) || (gameIDs.Count == 0)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(gameIDs) + " || " + nameof(gameIDs.Count)); + return null; + } + + if (!IsMaster(steamID)) { return null; } @@ -1090,6 +1176,7 @@ namespace ArchiSteamFarm { private static async Task ResponsePlay(ulong steamID, string botName, string games) { if ((steamID == 0) || string.IsNullOrEmpty(botName) || string.IsNullOrEmpty(games)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(botName) + " || " + nameof(games)); return null; } @@ -1122,7 +1209,12 @@ namespace ArchiSteamFarm { } private async Task ResponseStart(ulong steamID) { - if ((steamID == 0) || !IsMaster(steamID)) { + if (steamID == 0) { + Logging.LogNullError(nameof(steamID)); + return null; + } + + if (!IsMaster(steamID)) { return null; } @@ -1137,6 +1229,7 @@ namespace ArchiSteamFarm { private static async Task ResponseStart(ulong steamID, string botName) { if ((steamID == 0) || string.IsNullOrEmpty(botName)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(botName)); return null; } @@ -1153,7 +1246,12 @@ namespace ArchiSteamFarm { } private string ResponseStop(ulong steamID) { - if ((steamID == 0) || !IsMaster(steamID)) { + if (steamID == 0) { + Logging.LogNullError(nameof(steamID)); + return null; + } + + if (!IsMaster(steamID)) { return null; } @@ -1167,6 +1265,7 @@ namespace ArchiSteamFarm { private static string ResponseStop(ulong steamID, string botName) { if ((steamID == 0) || string.IsNullOrEmpty(botName)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(botName)); return null; } @@ -1183,15 +1282,17 @@ namespace ArchiSteamFarm { } private string ResponseUnknown(ulong steamID) { - if ((steamID == 0) || !IsMaster(steamID)) { - return null; + if (steamID != 0) { + return !IsMaster(steamID) ? null : "ERROR: Unknown command!"; } - return "ERROR: Unknown command!"; + Logging.LogNullError(nameof(steamID), BotName); + return null; } private static async Task ResponseUpdate(ulong steamID) { if (steamID == 0) { + Logging.LogNullError(nameof(steamID)); return null; } @@ -1216,6 +1317,7 @@ namespace ArchiSteamFarm { private async Task HandleMessage(ulong chatID, ulong steamID, string message) { if ((chatID == 0) || (steamID == 0) || string.IsNullOrEmpty(message)) { + Logging.LogNullError(nameof(chatID) + " || " + nameof(steamID) + " || " + nameof(message), BotName); return; } @@ -1224,6 +1326,7 @@ namespace ArchiSteamFarm { private void SendMessage(ulong steamID, string message) { if ((steamID == 0) || string.IsNullOrEmpty(message)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(message), BotName); return; } @@ -1235,7 +1338,12 @@ namespace ArchiSteamFarm { } private void SendMessageToChannel(ulong steamID, string message) { - if ((steamID == 0) || string.IsNullOrEmpty(message) || !SteamClient.IsConnected) { + if ((steamID == 0) || string.IsNullOrEmpty(message)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(message), BotName); + return; + } + + if (!SteamClient.IsConnected) { return; } @@ -1246,7 +1354,12 @@ namespace ArchiSteamFarm { } private void SendMessageToUser(ulong steamID, string message) { - if ((steamID == 0) || string.IsNullOrEmpty(message) || !SteamClient.IsConnected) { + if ((steamID == 0) || string.IsNullOrEmpty(message)) { + Logging.LogNullError(nameof(steamID) + " || " + nameof(message), BotName); + return; + } + + if (!SteamClient.IsConnected) { return; } @@ -1385,6 +1498,7 @@ namespace ArchiSteamFarm { private void OnConnected(SteamClient.ConnectedCallback callback) { if (callback == null) { + Logging.LogNullError(nameof(callback), BotName); return; } @@ -1455,6 +1569,7 @@ namespace ArchiSteamFarm { private async void OnDisconnected(SteamClient.DisconnectedCallback callback) { if (callback == null) { + Logging.LogNullError(nameof(callback), BotName); return; } @@ -1493,11 +1608,19 @@ namespace ArchiSteamFarm { SteamClient.Connect(); } - [SuppressMessage("ReSharper", "MemberCanBeMadeStatic.Local")] - private void OnFreeLicense(SteamApps.FreeLicenseCallback callback) { } + private void OnFreeLicense(SteamApps.FreeLicenseCallback callback) { + if (callback == null) { + Logging.LogNullError(nameof(callback), BotName); + } + } private async void OnGuestPassList(SteamApps.GuestPassListCallback callback) { - if ((callback == null) || (callback.Result != EResult.OK) || (callback.CountGuestPassesToRedeem == 0) || (callback.GuestPasses.Count == 0) || !BotConfig.AcceptGifts) { + if ((callback == null) || (callback.GuestPasses == null)) { + Logging.LogNullError(nameof(callback) + " || " + nameof(callback.GuestPasses), BotName); + return; + } + + if ((callback.CountGuestPassesToRedeem == 0) || (callback.GuestPasses.Count == 0) || !BotConfig.AcceptGifts) { return; } @@ -1518,7 +1641,12 @@ namespace ArchiSteamFarm { } private void OnChatInvite(SteamFriends.ChatInviteCallback callback) { - if ((callback == null) || !IsMaster(callback.PatronID)) { + if ((callback == null) || (callback.ChatRoomID == null) || (callback.PatronID == null)) { + Logging.LogNullError(nameof(callback) + " || " + nameof(callback.ChatRoomID) + " || " + nameof(callback.PatronID), BotName); + return; + } + + if (!IsMaster(callback.PatronID)) { return; } @@ -1526,7 +1654,12 @@ namespace ArchiSteamFarm { } private async void OnChatMsg(SteamFriends.ChatMsgCallback callback) { - if ((callback == null) || (callback.ChatMsgType != EChatEntryType.ChatMsg)) { + if ((callback == null) || (callback.ChatRoomID == null) || (callback.ChatterID == null) || string.IsNullOrEmpty(callback.Message)) { + Logging.LogNullError(nameof(callback) + " || " + nameof(callback.ChatRoomID) + " || " + nameof(callback.ChatterID) + " || " + nameof(callback.Message), BotName); + return; + } + + if (callback.ChatMsgType != EChatEntryType.ChatMsg) { return; } @@ -1545,7 +1678,8 @@ namespace ArchiSteamFarm { } private void OnFriendsList(SteamFriends.FriendsListCallback callback) { - if (callback == null) { + if ((callback == null) || (callback.FriendList == null)) { + Logging.LogNullError(nameof(callback) + " || " + nameof(callback.FriendList), BotName); return; } @@ -1566,7 +1700,12 @@ namespace ArchiSteamFarm { } private async void OnFriendMsg(SteamFriends.FriendMsgCallback callback) { - if ((callback == null) || (callback.EntryType != EChatEntryType.ChatMsg)) { + if ((callback == null) || (callback.Sender == null) || string.IsNullOrEmpty(callback.Message)) { + Logging.LogNullError(nameof(callback) + " || " + nameof(callback.Sender) + " || " + nameof(callback.Message), BotName); + return; + } + + if (callback.EntryType != EChatEntryType.ChatMsg) { return; } @@ -1574,7 +1713,12 @@ namespace ArchiSteamFarm { } private async void OnFriendMsgHistory(SteamFriends.FriendMsgHistoryCallback callback) { - if ((callback == null) || (callback.Result != EResult.OK) || (callback.Messages.Count == 0) || !IsMaster(callback.SteamID)) { + if ((callback == null) || (callback.Messages == null) || (callback.SteamID == null)) { + Logging.LogNullError(nameof(callback) + " || " + nameof(callback.Messages) + " || " + nameof(callback.SteamID), BotName); + return; + } + + if ((callback.Messages.Count == 0) || !IsMaster(callback.SteamID)) { return; } @@ -1597,6 +1741,7 @@ namespace ArchiSteamFarm { private void OnAccountInfo(SteamUser.AccountInfoCallback callback) { if (callback == null) { + Logging.LogNullError(nameof(callback), BotName); return; } @@ -1607,6 +1752,7 @@ namespace ArchiSteamFarm { private void OnLoggedOff(SteamUser.LoggedOffCallback callback) { if (callback == null) { + Logging.LogNullError(nameof(callback), BotName); return; } @@ -1615,6 +1761,7 @@ namespace ArchiSteamFarm { private async void OnLoggedOn(SteamUser.LoggedOnCallback callback) { if (callback == null) { + Logging.LogNullError(nameof(callback), BotName); return; } @@ -1714,7 +1861,8 @@ namespace ArchiSteamFarm { } private void OnLoginKey(SteamUser.LoginKeyCallback callback) { - if (callback == null) { + if ((callback == null) || string.IsNullOrEmpty(callback.LoginKey)) { + Logging.LogNullError(nameof(callback) + " || " + nameof(callback.LoginKey), BotName); return; } @@ -1724,21 +1872,27 @@ namespace ArchiSteamFarm { private void OnMachineAuth(SteamUser.UpdateMachineAuthCallback callback) { if (callback == null) { + Logging.LogNullError(nameof(callback), BotName); return; } int fileSize; byte[] sentryHash; - using (FileStream fileStream = File.Open(SentryFile, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { - fileStream.Seek(callback.Offset, SeekOrigin.Begin); - fileStream.Write(callback.Data, 0, callback.BytesToWrite); - fileSize = (int) fileStream.Length; + try { + using (FileStream fileStream = File.Open(SentryFile, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { + fileStream.Seek(callback.Offset, SeekOrigin.Begin); + fileStream.Write(callback.Data, 0, callback.BytesToWrite); + fileSize = (int) fileStream.Length; - fileStream.Seek(0, SeekOrigin.Begin); - using (SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider()) { - sentryHash = sha.ComputeHash(fileStream); + fileStream.Seek(0, SeekOrigin.Begin); + using (SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider()) { + sentryHash = sha.ComputeHash(fileStream); + } } + } catch (Exception e) { + Logging.LogGenericException(e, BotName); + return; } // Inform the steam servers that we're accepting this sentry file @@ -1755,11 +1909,19 @@ namespace ArchiSteamFarm { }); } - [SuppressMessage("ReSharper", "MemberCanBeMadeStatic.Local")] - private void OnWebAPIUserNonce(SteamUser.WebAPIUserNonceCallback callback) { } + private void OnWebAPIUserNonce(SteamUser.WebAPIUserNonceCallback callback) { + if (callback == null) { + Logging.LogNullError(nameof(callback), BotName); + } + } private async void OnNotifications(ArchiHandler.NotificationsCallback callback) { - if ((callback == null) || (callback.Notifications == null)) { + if (callback == null) { + Logging.LogNullError(nameof(callback), BotName); + return; + } + + if ((callback.Notifications == null) || (callback.Notifications.Count == 0)) { return; } @@ -1786,11 +1948,12 @@ namespace ArchiSteamFarm { } private void OnOfflineMessage(ArchiHandler.OfflineMessageCallback callback) { - if ((callback == null) || (callback.OfflineMessagesCount == 0)) { + if (callback == null) { + Logging.LogNullError(nameof(callback), BotName); return; } - if (!BotConfig.HandleOfflineMessages) { + if ((callback.OfflineMessagesCount == 0) || !BotConfig.HandleOfflineMessages) { return; } @@ -1819,6 +1982,7 @@ namespace ArchiSteamFarm { private void OnPurchaseResponse(ArchiHandler.PurchaseResponseCallback callback) { if (callback == null) { + Logging.LogNullError(nameof(callback), BotName); return; } diff --git a/ArchiSteamFarm/BotConfig.cs b/ArchiSteamFarm/BotConfig.cs index 73fa48bca..bc1c2a102 100644 --- a/ArchiSteamFarm/BotConfig.cs +++ b/ArchiSteamFarm/BotConfig.cs @@ -105,11 +105,17 @@ namespace ArchiSteamFarm { internal static BotConfig Load(string filePath) { - if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath)) { + if (string.IsNullOrEmpty(filePath)) { + Logging.LogNullError(nameof(filePath)); + return null; + } + + if (!File.Exists(filePath)) { return null; } BotConfig botConfig; + try { botConfig = JsonConvert.DeserializeObject(File.ReadAllText(filePath)); } catch (Exception e) { diff --git a/ArchiSteamFarm/BotDatabase.cs b/ArchiSteamFarm/BotDatabase.cs index c26c1bcef..e86986208 100644 --- a/ArchiSteamFarm/BotDatabase.cs +++ b/ArchiSteamFarm/BotDatabase.cs @@ -68,6 +68,7 @@ namespace ArchiSteamFarm { internal static BotDatabase Load(string filePath) { if (string.IsNullOrEmpty(filePath)) { + Logging.LogNullError(nameof(filePath)); return null; } @@ -76,6 +77,7 @@ namespace ArchiSteamFarm { } BotDatabase botDatabase; + try { botDatabase = JsonConvert.DeserializeObject(File.ReadAllText(filePath)); } catch (Exception e) { @@ -84,6 +86,7 @@ namespace ArchiSteamFarm { } if (botDatabase == null) { + Logging.LogNullError(nameof(botDatabase)); return null; } diff --git a/ArchiSteamFarm/CardsFarmer.cs b/ArchiSteamFarm/CardsFarmer.cs index 2b9310e63..163feb9cd 100755 --- a/ArchiSteamFarm/CardsFarmer.cs +++ b/ArchiSteamFarm/CardsFarmer.cs @@ -193,6 +193,7 @@ namespace ArchiSteamFarm { private static HashSet GetGamesToFarmSolo(ConcurrentDictionary gamesToFarm) { if (gamesToFarm == null) { + Logging.LogNullError(nameof(gamesToFarm)); return null; } @@ -249,11 +250,13 @@ namespace ArchiSteamFarm { private void CheckPage(HtmlDocument htmlDocument) { if (htmlDocument == null) { + Logging.LogNullError(nameof(htmlDocument), Bot.BotName); return; } HtmlNodeCollection htmlNodes = htmlDocument.DocumentNode.SelectNodes("//div[@class='badge_title_stats']"); if (htmlNodes == null) { + Logging.LogNullError(nameof(htmlNodes), Bot.BotName); return; } @@ -265,32 +268,27 @@ namespace ArchiSteamFarm { string steamLink = farmingNode.GetAttributeValue("href", null); if (string.IsNullOrEmpty(steamLink)) { - Logging.LogNullError("steamLink", Bot.BotName); + Logging.LogNullError(nameof(steamLink), Bot.BotName); continue; } int index = steamLink.LastIndexOf('/'); if (index < 0) { - Logging.LogNullError("index", Bot.BotName); + Logging.LogNullError(nameof(index), Bot.BotName); continue; } index++; if (steamLink.Length <= index) { - Logging.LogNullError("length", Bot.BotName); + Logging.LogNullError(nameof(steamLink.Length), Bot.BotName); continue; } steamLink = steamLink.Substring(index); uint appID; - if (!uint.TryParse(steamLink, out appID)) { - Logging.LogNullError("appID", Bot.BotName); - continue; - } - - if (appID == 0) { - Logging.LogNullError("appID", Bot.BotName); + if (!uint.TryParse(steamLink, out appID) || (appID == 0)) { + Logging.LogNullError(nameof(appID), Bot.BotName); continue; } @@ -300,13 +298,13 @@ namespace ArchiSteamFarm { HtmlNode timeNode = htmlNode.SelectSingleNode(".//div[@class='badge_title_stats_playtime']"); if (timeNode == null) { - Logging.LogNullError("timeNode", Bot.BotName); + Logging.LogNullError(nameof(timeNode), Bot.BotName); continue; } string hoursString = timeNode.InnerText; if (string.IsNullOrEmpty(hoursString)) { - Logging.LogNullError("hoursString", Bot.BotName); + Logging.LogNullError(nameof(hoursString), Bot.BotName); continue; } @@ -314,7 +312,10 @@ namespace ArchiSteamFarm { Match match = Regex.Match(hoursString, @"[0-9\.,]+"); if (match.Success) { - float.TryParse(match.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out hours); + if (!float.TryParse(match.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out hours)) { + Logging.LogNullError(nameof(hours), Bot.BotName); + continue; + } } GamesToFarm[appID] = hours; @@ -323,6 +324,7 @@ namespace ArchiSteamFarm { private async Task CheckPage(byte page) { if (page == 0) { + Logging.LogNullError(nameof(page), Bot.BotName); return; } @@ -344,6 +346,7 @@ namespace ArchiSteamFarm { private async Task ShouldFarm(uint appID) { if (appID == 0) { + Logging.LogNullError(nameof(appID), Bot.BotName); return false; } @@ -353,11 +356,12 @@ namespace ArchiSteamFarm { } HtmlNode htmlNode = htmlDocument.DocumentNode.SelectSingleNode("//span[@class='progress_info_bold']"); - if (htmlNode == null) { - return null; + if (htmlNode != null) { + return !htmlNode.InnerText.Contains("No card drops"); } - return !htmlNode.InnerText.Contains("No card drops"); + Logging.LogNullError(nameof(htmlNode), Bot.BotName); + return null; } private bool FarmMultiple() { @@ -387,6 +391,7 @@ namespace ArchiSteamFarm { private async Task FarmSolo(uint appID) { if (appID == 0) { + Logging.LogNullError(nameof(appID), Bot.BotName); return true; } @@ -413,6 +418,7 @@ namespace ArchiSteamFarm { private async Task Farm(uint appID) { if (appID == 0) { + Logging.LogNullError(nameof(appID), Bot.BotName); return false; } @@ -441,6 +447,7 @@ namespace ArchiSteamFarm { private bool FarmHours(float maxHour, ConcurrentHashSet appIDs) { if ((maxHour < 0) || (appIDs == null) || (appIDs.Count == 0)) { + Logging.LogNullError(nameof(maxHour) + " || " + nameof(appIDs) + " || " + nameof(appIDs.Count), Bot.BotName); return false; } diff --git a/ArchiSteamFarm/Debugging.cs b/ArchiSteamFarm/Debugging.cs index 2160105c7..10d47951f 100644 --- a/ArchiSteamFarm/Debugging.cs +++ b/ArchiSteamFarm/Debugging.cs @@ -44,7 +44,7 @@ namespace ArchiSteamFarm { internal DebugListener(string filePath) { if (string.IsNullOrEmpty(filePath)) { - return; + throw new ArgumentNullException(nameof(filePath)); } FilePath = filePath; diff --git a/ArchiSteamFarm/GlobalConfig.cs b/ArchiSteamFarm/GlobalConfig.cs index 17ee7fdb3..a1eb3b823 100644 --- a/ArchiSteamFarm/GlobalConfig.cs +++ b/ArchiSteamFarm/GlobalConfig.cs @@ -112,6 +112,7 @@ namespace ArchiSteamFarm { internal static GlobalConfig Load(string filePath) { if (string.IsNullOrEmpty(filePath)) { + Logging.LogNullError(nameof(filePath)); return null; } @@ -120,6 +121,7 @@ namespace ArchiSteamFarm { } GlobalConfig globalConfig; + try { globalConfig = JsonConvert.DeserializeObject(File.ReadAllText(filePath)); } catch (Exception e) { @@ -128,6 +130,7 @@ namespace ArchiSteamFarm { } if (globalConfig == null) { + Logging.LogNullError(nameof(globalConfig)); return null; } diff --git a/ArchiSteamFarm/GlobalDatabase.cs b/ArchiSteamFarm/GlobalDatabase.cs index 03e904ccd..edfffdb9a 100644 --- a/ArchiSteamFarm/GlobalDatabase.cs +++ b/ArchiSteamFarm/GlobalDatabase.cs @@ -50,6 +50,7 @@ namespace ArchiSteamFarm { internal static GlobalDatabase Load(string filePath) { if (string.IsNullOrEmpty(filePath)) { + Logging.LogNullError(nameof(filePath)); return null; } @@ -58,6 +59,7 @@ namespace ArchiSteamFarm { } GlobalDatabase globalDatabase; + try { globalDatabase = JsonConvert.DeserializeObject(File.ReadAllText(filePath)); } catch (Exception e) { @@ -66,6 +68,7 @@ namespace ArchiSteamFarm { } if (globalDatabase == null) { + Logging.LogNullError(nameof(globalDatabase)); return null; } diff --git a/ArchiSteamFarm/Logging.cs b/ArchiSteamFarm/Logging.cs index e94e05a9d..dc39d3c20 100644 --- a/ArchiSteamFarm/Logging.cs +++ b/ArchiSteamFarm/Logging.cs @@ -57,6 +57,7 @@ namespace ArchiSteamFarm { internal static void LogGenericWTF(string message, string botName = "Main", [CallerMemberName] string previousMethodName = null) { if (string.IsNullOrEmpty(message)) { + LogNullError(nameof(message), botName); return; } @@ -65,6 +66,7 @@ namespace ArchiSteamFarm { internal static void LogGenericError(string message, string botName = "Main", [CallerMemberName] string previousMethodName = null) { if (string.IsNullOrEmpty(message)) { + LogNullError(nameof(message), botName); return; } @@ -74,6 +76,7 @@ namespace ArchiSteamFarm { internal static void LogGenericException(Exception exception, string botName = "Main", [CallerMemberName] string previousMethodName = null) { while (true) { if (exception == null) { + LogNullError(nameof(exception), botName); return; } @@ -91,6 +94,7 @@ namespace ArchiSteamFarm { internal static void LogGenericWarning(string message, string botName = "Main", [CallerMemberName] string previousMethodName = null) { if (string.IsNullOrEmpty(message)) { + LogNullError(nameof(message), botName); return; } @@ -99,6 +103,7 @@ namespace ArchiSteamFarm { internal static void LogGenericInfo(string message, string botName = "Main", [CallerMemberName] string previousMethodName = null) { if (string.IsNullOrEmpty(message)) { + LogNullError(nameof(message), botName); return; } @@ -107,16 +112,21 @@ namespace ArchiSteamFarm { [SuppressMessage("ReSharper", "ExplicitCallerInfoArgument")] internal static void LogNullError(string nullObjectName, string botName = "Main", [CallerMemberName] string previousMethodName = null) { - if (string.IsNullOrEmpty(nullObjectName)) { - return; - } + while (true) { + if (string.IsNullOrEmpty(nullObjectName)) { + nullObjectName = nameof(nullObjectName); + continue; + } - LogGenericError(nullObjectName + " is null!", botName, previousMethodName); + LogGenericError(nullObjectName + " is null!", botName, previousMethodName); + break; + } } [Conditional("DEBUG"), SuppressMessage("ReSharper", "UnusedMember.Global")] internal static void LogGenericDebug(string message, string botName = "Main", [CallerMemberName] string previousMethodName = null) { if (string.IsNullOrEmpty(message)) { + LogNullError(nameof(message), botName); return; } @@ -125,6 +135,7 @@ namespace ArchiSteamFarm { private static void Log(string message) { if (string.IsNullOrEmpty(message)) { + LogNullError(nameof(message)); return; } @@ -134,8 +145,7 @@ namespace ArchiSteamFarm { if (!Program.ConsoleIsBusy) { try { Console.Write(loggedMessage); - } - catch { + } catch { // Ignored } } diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 60cbe4139..3ff4b79b9 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -110,12 +110,9 @@ namespace ArchiSteamFarm { releaseURL += "/latest"; } - string response = null; Logging.LogGenericInfo("Checking new version..."); - for (byte i = 0; (i < WebBrowser.MaxRetries) && string.IsNullOrEmpty(response); i++) { - response = await WebBrowser.UrlGetToContent(releaseURL).ConfigureAwait(false); - } + string response = await WebBrowser.UrlGetToContentRetry(releaseURL).ConfigureAwait(false); if (string.IsNullOrEmpty(response)) { Logging.LogGenericWarning("Could not check latest version!"); return; @@ -202,14 +199,8 @@ namespace ArchiSteamFarm { return; } - byte[] result = null; - for (byte i = 0; (i < WebBrowser.MaxRetries) && (result == null); i++) { - Logging.LogGenericInfo("Downloading new version..."); - result = await WebBrowser.UrlGetToBytes(binaryAsset.DownloadURL).ConfigureAwait(false); - } - + byte[] result = await WebBrowser.UrlGetToBytesRetry(binaryAsset.DownloadURL).ConfigureAwait(false); if (result == null) { - Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries"); return; } @@ -280,7 +271,7 @@ namespace ArchiSteamFarm { Exit(); } - internal static string GetUserInput(EUserInputType userInputType, string botName = null, string extraInformation = null) { + internal static string GetUserInput(EUserInputType userInputType, string botName = "Main", string extraInformation = null) { if (userInputType == EUserInputType.Unknown) { return null; } @@ -295,49 +286,51 @@ namespace ArchiSteamFarm { ConsoleIsBusy = true; switch (userInputType) { case EUserInputType.DeviceID: - Console.Write((string.IsNullOrEmpty(botName) ? "" : "<" + botName + "> ") + "Please enter your Device ID (including \"android:\"): "); + Console.Write("<" + botName + "> Please enter your Device ID (including \"android:\"): "); break; case EUserInputType.Login: - Console.Write((string.IsNullOrEmpty(botName) ? "" : "<" + botName + "> ") + "Please enter your login: "); + Console.Write("<" + botName + "> Please enter your login: "); break; case EUserInputType.Password: - Console.Write((string.IsNullOrEmpty(botName) ? "" : "<" + botName + "> ") + "Please enter your password: "); + Console.Write("<" + botName + "> Please enter your password: "); break; case EUserInputType.PhoneNumber: - Console.Write((string.IsNullOrEmpty(botName) ? "" : "<" + botName + "> ") + "Please enter your full phone number (e.g. +1234567890): "); + Console.Write("<" + botName + "> Please enter your full phone number (e.g. +1234567890): "); break; case EUserInputType.SMS: - Console.Write((string.IsNullOrEmpty(botName) ? "" : "<" + botName + "> ") + "Please enter SMS code sent on your mobile: "); + Console.Write("<" + botName + "> Please enter SMS code sent on your mobile: "); break; case EUserInputType.SteamGuard: - Console.Write((string.IsNullOrEmpty(botName) ? "" : "<" + botName + "> ") + "Please enter the auth code sent to your email: "); + Console.Write("<" + botName + "> Please enter the auth code sent to your email: "); break; case EUserInputType.SteamParentalPIN: - Console.Write((string.IsNullOrEmpty(botName) ? "" : "<" + botName + "> ") + "Please enter steam parental PIN: "); + Console.Write("<" + botName + "> Please enter steam parental PIN: "); break; case EUserInputType.RevocationCode: - Console.WriteLine((string.IsNullOrEmpty(botName) ? "" : "<" + botName + "> ") + "PLEASE WRITE DOWN YOUR REVOCATION CODE: " + extraInformation); - Console.Write("Hit enter once ready..."); + Console.WriteLine("<" + botName + "> PLEASE WRITE DOWN YOUR REVOCATION CODE: " + extraInformation); + Console.Write("<" + botName + "> Hit enter once ready..."); break; case EUserInputType.TwoFactorAuthentication: - Console.Write((string.IsNullOrEmpty(botName) ? "" : "<" + botName + "> ") + "Please enter your 2 factor auth code from your authenticator app: "); + Console.Write("<" + botName + "> Please enter your 2 factor auth code from your authenticator app: "); break; case EUserInputType.WCFHostname: - Console.Write((string.IsNullOrEmpty(botName) ? "" : "<" + botName + "> ") + "Please enter your WCF hostname: "); + Console.Write("<" + botName + "> Please enter your WCF hostname: "); break; default: - Console.Write((string.IsNullOrEmpty(botName) ? "" : "<" + botName + "> ") + "Please enter not documented yet value of \"" + userInputType + "\": "); + Console.Write("<" + botName + "> Please enter not documented yet value of \"" + userInputType + "\": "); break; } result = Console.ReadLine(); + if (!Console.IsOutputRedirected) { Console.Clear(); // For security purposes } + ConsoleIsBusy = false; } - return string.IsNullOrEmpty(result) ? null : result.Trim(); + return !string.IsNullOrEmpty(result) ? result.Trim() : null; } internal static void OnBotShutdown() { @@ -378,11 +371,14 @@ namespace ArchiSteamFarm { private static void ParseArgs(IEnumerable args) { if (args == null) { + Logging.LogNullError(nameof(args)); return; } foreach (string arg in args) { switch (arg) { + case "": + break; case "--client": Mode = EMode.Client; break; @@ -416,7 +412,8 @@ namespace ArchiSteamFarm { } private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args) { - if ((sender == null) || (args == null)) { + if ((sender == null) || (args == null) || (args.ExceptionObject == null)) { + Logging.LogNullError(nameof(sender) + " || " + nameof(args) + " || " + nameof(args.ExceptionObject)); return; } @@ -424,7 +421,8 @@ namespace ArchiSteamFarm { } private static void UnobservedTaskExceptionHandler(object sender, UnobservedTaskExceptionEventArgs args) { - if ((sender == null) || (args == null)) { + if ((sender == null) || (args == null) || (args.Exception == null)) { + Logging.LogNullError(nameof(sender) + " || " + nameof(args) + " || " + nameof(args.Exception)); return; } @@ -469,7 +467,9 @@ namespace ArchiSteamFarm { } // Parse args - ParseArgs(args); + if (args != null) { + ParseArgs(args); + } // If we ran ASF as a client, we're done by now if (Mode == EMode.Client) { @@ -501,12 +501,12 @@ namespace ArchiSteamFarm { } Bot bot = new Bot(botName); - if ((bot.BotConfig != null) && bot.BotConfig.Enabled) { - if (bot.BotConfig.StartOnLaunch) { - isRunning = true; - } - } else { - Logging.LogGenericInfo("Not starting this instance because it's disabled in config file", botName); + if ((bot.BotConfig == null) || !bot.BotConfig.Enabled) { + continue; + } + + if (bot.BotConfig.StartOnLaunch) { + isRunning = true; } } diff --git a/ArchiSteamFarm/Trading.cs b/ArchiSteamFarm/Trading.cs index 8b80a3a55..1d702e741 100644 --- a/ArchiSteamFarm/Trading.cs +++ b/ArchiSteamFarm/Trading.cs @@ -95,7 +95,12 @@ namespace ArchiSteamFarm { } private async Task ParseTrade(Steam.TradeOffer tradeOffer) { - if ((tradeOffer == null) || (tradeOffer.State != Steam.TradeOffer.ETradeOfferState.Active)) { + if (tradeOffer == null) { + Logging.LogNullError(nameof(tradeOffer), Bot.BotName); + return; + } + + if (tradeOffer.State != Steam.TradeOffer.ETradeOfferState.Active) { return; } @@ -109,6 +114,7 @@ namespace ArchiSteamFarm { private async Task ShouldAcceptTrade(Steam.TradeOffer tradeOffer) { if (tradeOffer == null) { + Logging.LogNullError(nameof(tradeOffer), Bot.BotName); return false; } diff --git a/ArchiSteamFarm/Utilities.cs b/ArchiSteamFarm/Utilities.cs index 44d575238..bd829d0d3 100644 --- a/ArchiSteamFarm/Utilities.cs +++ b/ArchiSteamFarm/Utilities.cs @@ -34,17 +34,41 @@ namespace ArchiSteamFarm { [SuppressMessage("ReSharper", "UnusedParameter.Global")] internal static void Forget(this Task task) { } - internal static Task ForEachAsync(this IEnumerable sequence, Func action) => action == null ? Task.FromResult(true) : Task.WhenAll(sequence.Select(action)); + internal static Task ForEachAsync(this IEnumerable sequence, Func action) { + if (action != null) { + return Task.WhenAll(sequence.Select(action)); + } + + Logging.LogNullError(nameof(action)); + return Task.FromResult(true); + } internal static string GetCookieValue(this CookieContainer cookieContainer, string url, string name) { if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(name)) { + Logging.LogNullError(nameof(url) + " || " + nameof(name)); return null; } - CookieCollection cookies = cookieContainer.GetCookies(new Uri(url)); - return cookies.Count == 0 ? null : (from Cookie cookie in cookies where cookie.Name.Equals(name, StringComparison.Ordinal) select cookie.Value).FirstOrDefault(); + Uri uri; + + try { + uri = new Uri(url); + } catch (UriFormatException e) { + Logging.LogGenericException(e); + return null; + } + + CookieCollection cookies = cookieContainer.GetCookies(uri); + return cookies.Count == 0 ? null : (from Cookie cookie in cookies where cookie.Name.Equals(name) select cookie.Value).FirstOrDefault(); } - internal static Task SleepAsync(int miliseconds) => miliseconds < 0 ? Task.FromResult(true) : Task.Delay(miliseconds); + internal static Task SleepAsync(int miliseconds) { + if (miliseconds >= 0) { + return Task.Delay(miliseconds); + } + + Logging.LogNullError(nameof(miliseconds)); + return Task.FromResult(true); + } } } diff --git a/ArchiSteamFarm/WCF.cs b/ArchiSteamFarm/WCF.cs index 26bf3538e..83b9bd761 100644 --- a/ArchiSteamFarm/WCF.cs +++ b/ArchiSteamFarm/WCF.cs @@ -87,6 +87,11 @@ namespace ArchiSteamFarm { } internal string SendCommand(string input) { + if (string.IsNullOrEmpty(input)) { + Logging.LogNullError(nameof(input)); + return null; + } + if (Client == null) { Client = new Client(new BasicHttpBinding(), new EndpointAddress(URL)); } @@ -96,6 +101,7 @@ namespace ArchiSteamFarm { public string HandleCommand(string input) { if (string.IsNullOrEmpty(input)) { + Logging.LogNullError(nameof(input)); return null; } @@ -120,6 +126,11 @@ namespace ArchiSteamFarm { internal Client(Binding binding, EndpointAddress address) : base(binding, address) { } public string HandleCommand(string input) { + if (string.IsNullOrEmpty(input)) { + Logging.LogNullError(nameof(input)); + return null; + } + try { return Channel.HandleCommand(input); } catch (Exception e) { diff --git a/ArchiSteamFarm/WebBrowser.cs b/ArchiSteamFarm/WebBrowser.cs index 8c7fa1e46..3f0119ffc 100644 --- a/ArchiSteamFarm/WebBrowser.cs +++ b/ArchiSteamFarm/WebBrowser.cs @@ -82,42 +82,161 @@ namespace ArchiSteamFarm { HttpClient.DefaultRequestHeaders.UserAgent.ParseAdd(DefaultUserAgent); } - internal async Task UrlHead(string request, string referer = null) { + internal async Task UrlHeadRetry(string request, string referer = null) { if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); return false; } - using (HttpResponseMessage response = await UrlHeadToResponse(request, referer).ConfigureAwait(false)) { - return response != null; + bool result = false; + for (byte i = 0; (i < MaxRetries) && !result; i++) { + result = await UrlHead(request, referer).ConfigureAwait(false); } + + if (result) { + return true; + } + + Logging.LogGenericWTF("Request failed even after " + MaxRetries + " tries", Identifier); + return false; } - internal async Task UrlHeadToUri(string request, string referer = null) { + internal async Task UrlHeadToUriRetry(string request, string referer = null) { if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); return null; } - using (HttpResponseMessage response = await UrlHeadToResponse(request, referer).ConfigureAwait(false)) { - return response == null ? null : response.RequestMessage.RequestUri; + Uri result = null; + for (byte i = 0; (i < MaxRetries) && (result == null); i++) { + result = await UrlHeadToUri(request, referer).ConfigureAwait(false); } + + if (result != null) { + return result; + } + + Logging.LogGenericWTF("Request failed even after " + MaxRetries + " tries", Identifier); + return null; } - internal async Task UrlGetToContent(string request, string referer = null) { + internal async Task UrlGetToBytesRetry(string request, string referer = null) { if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); return null; } - using (HttpResponseMessage httpResponse = await UrlGetToResponse(request, referer).ConfigureAwait(false)) { - if (httpResponse == null) { - return null; - } - - return await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + byte[] result = null; + for (byte i = 0; (i < MaxRetries) && (result == null); i++) { + result = await UrlGetToBytes(request, referer).ConfigureAwait(false); } + + if (result != null) { + return result; + } + + Logging.LogGenericWTF("Request failed even after " + MaxRetries + " tries", Identifier); + return null; } - internal async Task UrlGetToBytes(string request, string referer = null) { + internal async Task UrlGetToContentRetry(string request, string referer = null) { if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); + return null; + } + + string result = null; + for (byte i = 0; (i < MaxRetries) && string.IsNullOrEmpty(result); i++) { + result = await UrlGetToContent(request, referer).ConfigureAwait(false); + } + + if (!string.IsNullOrEmpty(result)) { + return result; + } + + Logging.LogGenericWTF("Request failed even after " + MaxRetries + " tries", Identifier); + return null; + } + + internal async Task UrlGetToHtmlDocumentRetry(string request, string referer = null) { + if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); + return null; + } + + HtmlDocument result = null; + for (byte i = 0; (i < MaxRetries) && (result == null); i++) { + result = await UrlGetToHtmlDocument(request, referer).ConfigureAwait(false); + } + + if (result != null) { + return result; + } + + Logging.LogGenericWTF("Request failed even after " + MaxRetries + " tries", Identifier); + return null; + } + + internal async Task UrlGetToJObjectRetry(string request, string referer = null) { + if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); + return null; + } + + JObject result = null; + for (byte i = 0; (i < MaxRetries) && (result == null); i++) { + result = await UrlGetToJObject(request, referer).ConfigureAwait(false); + } + + if (result != null) { + return result; + } + + Logging.LogGenericWTF("Request failed even after " + MaxRetries + " tries", Identifier); + return null; + } + + internal async Task UrlGetToXMLRetry(string request, string referer = null) { + if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); + return null; + } + + XmlDocument result = null; + for (byte i = 0; (i < MaxRetries) && (result == null); i++) { + result = await UrlGetToXML(request, referer).ConfigureAwait(false); + } + + if (result != null) { + return result; + } + + Logging.LogGenericWTF("Request failed even after " + MaxRetries + " tries", Identifier); + return null; + } + + internal async Task UrlPostRetry(string request, Dictionary data = null, string referer = null) { + if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); + return false; + } + + bool result = false; + for (byte i = 0; (i < MaxRetries) && !result; i++) { + result = await UrlPost(request, data, referer).ConfigureAwait(false); + } + + if (result) { + return true; + } + + Logging.LogGenericWTF("Request failed even after " + MaxRetries + " tries", Identifier); + return false; + } + + private async Task UrlGetToBytes(string request, string referer = null) { + if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); return null; } @@ -130,8 +249,24 @@ namespace ArchiSteamFarm { } } - internal async Task UrlGetToHtmlDocument(string request, string referer = null) { + private async Task UrlGetToContent(string request, string referer = null) { if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); + return null; + } + + using (HttpResponseMessage httpResponse = await UrlGetToResponse(request, referer).ConfigureAwait(false)) { + if (httpResponse == null) { + return null; + } + + return await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + } + } + + private async Task UrlGetToHtmlDocument(string request, string referer = null) { + if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); return null; } @@ -145,8 +280,9 @@ namespace ArchiSteamFarm { return htmlDocument; } - internal async Task UrlGetToJObject(string request, string referer = null) { + private async Task UrlGetToJObject(string request, string referer = null) { if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); return null; } @@ -167,8 +303,18 @@ namespace ArchiSteamFarm { return jObject; } - internal async Task UrlGetToXML(string request, string referer = null) { + private async Task UrlGetToResponse(string request, string referer = null) { + if (!string.IsNullOrEmpty(request)) { + return await UrlRequest(request, HttpMethod.Get, null, referer).ConfigureAwait(false); + } + + Logging.LogNullError(nameof(request)); + return null; + } + + private async Task UrlGetToXML(string request, string referer = null) { if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); return null; } @@ -189,8 +335,40 @@ namespace ArchiSteamFarm { return xmlDocument; } - internal async Task UrlPost(string request, Dictionary data = null, string referer = null) { + private async Task UrlHead(string request, string referer = null) { if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); + return false; + } + + using (HttpResponseMessage response = await UrlHeadToResponse(request, referer).ConfigureAwait(false)) { + return response != null; + } + } + + private async Task UrlHeadToResponse(string request, string referer = null) { + if (!string.IsNullOrEmpty(request)) { + return await UrlRequest(request, HttpMethod.Head, null, referer).ConfigureAwait(false); + } + + Logging.LogNullError(nameof(request)); + return null; + } + + private async Task UrlHeadToUri(string request, string referer = null) { + if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); + return null; + } + + using (HttpResponseMessage response = await UrlHeadToResponse(request, referer).ConfigureAwait(false)) { + return response == null ? null : response.RequestMessage.RequestUri; + } + } + + private async Task UrlPost(string request, Dictionary data = null, string referer = null) { + if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request)); return false; } @@ -199,32 +377,18 @@ namespace ArchiSteamFarm { } } - private async Task UrlGetToResponse(string request, string referer = null) { - if (string.IsNullOrEmpty(request)) { - return null; - } - - return await UrlRequest(request, HttpMethod.Get, null, referer).ConfigureAwait(false); - } - - private async Task UrlHeadToResponse(string request, string referer = null) { - if (string.IsNullOrEmpty(request)) { - return null; - } - - return await UrlRequest(request, HttpMethod.Head, null, referer).ConfigureAwait(false); - } - private async Task UrlPostToResponse(string request, Dictionary data = null, string referer = null) { - if (string.IsNullOrEmpty(request)) { - return null; + if (!string.IsNullOrEmpty(request)) { + return await UrlRequest(request, HttpMethod.Post, data, referer).ConfigureAwait(false); } - return await UrlRequest(request, HttpMethod.Post, data, referer).ConfigureAwait(false); + Logging.LogNullError(nameof(request)); + return null; } private async Task UrlRequest(string request, HttpMethod httpMethod, Dictionary data = null, string referer = null) { if (string.IsNullOrEmpty(request) || (httpMethod == null)) { + Logging.LogNullError(nameof(request) + " || " + nameof(httpMethod)); return null; } diff --git a/ConfigGenerator/ASFConfig.cs b/ConfigGenerator/ASFConfig.cs index 102b54588..f8b6b40aa 100644 --- a/ConfigGenerator/ASFConfig.cs +++ b/ConfigGenerator/ASFConfig.cs @@ -72,6 +72,7 @@ namespace ConfigGenerator { internal void Rename(string botName) { if (string.IsNullOrEmpty(botName)) { + Logging.LogNullError(nameof(botName)); return; } diff --git a/ConfigGenerator/BotConfig.cs b/ConfigGenerator/BotConfig.cs index 8b903e002..3efe4b987 100644 --- a/ConfigGenerator/BotConfig.cs +++ b/ConfigGenerator/BotConfig.cs @@ -106,6 +106,7 @@ namespace ConfigGenerator { internal static BotConfig Load(string filePath) { if (string.IsNullOrEmpty(filePath)) { + Logging.LogNullError(nameof(filePath)); return null; } @@ -114,6 +115,7 @@ namespace ConfigGenerator { } BotConfig botConfig; + try { botConfig = JsonConvert.DeserializeObject(File.ReadAllText(filePath)); } catch (Exception e) { diff --git a/ConfigGenerator/ConfigPage.cs b/ConfigGenerator/ConfigPage.cs index 8eabfa172..43c4ee902 100644 --- a/ConfigGenerator/ConfigPage.cs +++ b/ConfigGenerator/ConfigPage.cs @@ -22,6 +22,7 @@ */ +using System; using System.IO; using System.Windows.Forms; @@ -31,7 +32,7 @@ namespace ConfigGenerator { internal ConfigPage(ASFConfig config) { if (config == null) { - return; + throw new ArgumentNullException(nameof(config)); } ASFConfig = config; diff --git a/ConfigGenerator/DialogBox.cs b/ConfigGenerator/DialogBox.cs index c8bc731c0..82ba258fc 100644 --- a/ConfigGenerator/DialogBox.cs +++ b/ConfigGenerator/DialogBox.cs @@ -31,6 +31,7 @@ namespace ConfigGenerator { internal static class DialogBox { internal static DialogResult InputBox(string title, string promptText, out string value) { if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(promptText)) { + Logging.LogNullError(nameof(title) + " || " + nameof(promptText)); value = null; return DialogResult.Abort; } @@ -80,6 +81,7 @@ namespace ConfigGenerator { internal static DialogResult YesNoBox(string title, string promptText) { if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(promptText)) { + Logging.LogNullError(nameof(title) + " || " + nameof(promptText)); return DialogResult.Abort; } diff --git a/ConfigGenerator/EnhancedPropertyGrid.cs b/ConfigGenerator/EnhancedPropertyGrid.cs index 652dabbb7..d345194a1 100644 --- a/ConfigGenerator/EnhancedPropertyGrid.cs +++ b/ConfigGenerator/EnhancedPropertyGrid.cs @@ -43,12 +43,13 @@ namespace ConfigGenerator { ToolbarVisible = false; } - protected override void OnPropertyValueChanged(PropertyValueChangedEventArgs e) { - if (e == null) { + protected override void OnPropertyValueChanged(PropertyValueChangedEventArgs args) { + if (args == null) { + Logging.LogNullError(nameof(args)); return; } - base.OnPropertyValueChanged(e); + base.OnPropertyValueChanged(args); ASFConfig.Save(); BotConfig botConfig = ASFConfig as BotConfig; @@ -74,12 +75,13 @@ namespace ConfigGenerator { } } - protected override void OnGotFocus(EventArgs e) { - if (e == null) { + protected override void OnGotFocus(EventArgs args) { + if (args == null) { + Logging.LogNullError(nameof(args)); return; } - base.OnGotFocus(e); + base.OnGotFocus(args); ASFConfig.Save(); } } diff --git a/ConfigGenerator/GlobalConfig.cs b/ConfigGenerator/GlobalConfig.cs index ef40c37e6..1954dce59 100644 --- a/ConfigGenerator/GlobalConfig.cs +++ b/ConfigGenerator/GlobalConfig.cs @@ -110,6 +110,7 @@ namespace ConfigGenerator { internal static GlobalConfig Load(string filePath) { if (string.IsNullOrEmpty(filePath)) { + Logging.LogNullError(nameof(filePath)); return null; } @@ -118,6 +119,7 @@ namespace ConfigGenerator { } GlobalConfig globalConfig; + try { globalConfig = JsonConvert.DeserializeObject(File.ReadAllText(filePath)); } catch (Exception e) { diff --git a/ConfigGenerator/Logging.cs b/ConfigGenerator/Logging.cs index ff743a9af..051c39079 100644 --- a/ConfigGenerator/Logging.cs +++ b/ConfigGenerator/Logging.cs @@ -23,22 +23,25 @@ */ using System; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Windows.Forms; using ConfigGenerator.Properties; namespace ConfigGenerator { internal static class Logging { - internal static void LogGenericInfo(string message) { + internal static void LogGenericInfo(string message, [CallerMemberName] string previousMethodName = null) { if (string.IsNullOrEmpty(message)) { + LogNullError(nameof(message)); return; } - MessageBox.Show(message, Resources.Information, MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBox.Show(previousMethodName + @"() " + message, Resources.Information, MessageBoxButtons.OK, MessageBoxIcon.Information); } internal static void LogGenericError(string message, [CallerMemberName] string previousMethodName = null) { if (string.IsNullOrEmpty(message)) { + LogNullError(nameof(message)); return; } @@ -48,6 +51,7 @@ namespace ConfigGenerator { internal static void LogGenericException(Exception exception, [CallerMemberName] string previousMethodName = null) { while (true) { if (exception == null) { + LogNullError(nameof(exception)); return; } @@ -64,10 +68,24 @@ namespace ConfigGenerator { internal static void LogGenericWarning(string message, [CallerMemberName] string previousMethodName = null) { if (string.IsNullOrEmpty(message)) { + LogNullError(nameof(message)); return; } MessageBox.Show(previousMethodName + @"() " + message, Resources.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning); } + + [SuppressMessage("ReSharper", "ExplicitCallerInfoArgument")] + internal static void LogNullError(string nullObjectName, [CallerMemberName] string previousMethodName = null) { + while (true) { + if (string.IsNullOrEmpty(nullObjectName)) { + nullObjectName = nameof(nullObjectName); + continue; + } + + LogGenericError(nullObjectName + " is null!", previousMethodName); + break; + } + } } } diff --git a/ConfigGenerator/MainForm.cs b/ConfigGenerator/MainForm.cs index 61f1bd8d7..606d6cf4c 100644 --- a/ConfigGenerator/MainForm.cs +++ b/ConfigGenerator/MainForm.cs @@ -45,8 +45,9 @@ namespace ConfigGenerator { InitializeComponent(); } - private void MainForm_Load(object sender, EventArgs e) { - if ((sender == null) || (e == null)) { + private void MainForm_Load(object sender, EventArgs args) { + if ((sender == null) || (args == null)) { + Logging.LogNullError(nameof(sender) + " || " + nameof(args)); return; } @@ -71,12 +72,13 @@ namespace ConfigGenerator { Tutorial.OnAction(Tutorial.EPhase.Start); } - private void MainTab_Selected(object sender, TabControlEventArgs e) { - if ((sender == null) || (e == null)) { + private void MainTab_Selected(object sender, TabControlEventArgs args) { + if ((sender == null) || (args == null)) { + Logging.LogNullError(nameof(sender) + " || " + nameof(args)); return; } - if (e.TabPage == RemoveTab) { + if (args.TabPage == RemoveTab) { ConfigPage configPage = OldTab as ConfigPage; if (configPage == null) { MainTab.SelectedIndex = -1; @@ -98,7 +100,7 @@ namespace ConfigGenerator { MainTab.SelectedIndex = 0; configPage.ASFConfig.Remove(); MainTab.TabPages.Remove(configPage); - } else if (e.TabPage == RenameTab) { + } else if (args.TabPage == RenameTab) { ConfigPage configPage = OldTab as ConfigPage; if (configPage == null) { MainTab.SelectedIndex = -1; @@ -128,7 +130,7 @@ namespace ConfigGenerator { configPage.ASFConfig.Rename(input); configPage.RefreshText(); - } else if (e.TabPage == NewTab) { + } else if (args.TabPage == NewTab) { ConfigPage configPage = OldTab as ConfigPage; if (configPage == null) { MainTab.SelectedIndex = -1; @@ -163,33 +165,36 @@ namespace ConfigGenerator { MainTab.TabPages.Insert(MainTab.TabPages.Count - ReservedTabs, newConfigPage); MainTab.SelectedTab = newConfigPage; Tutorial.OnAction(Tutorial.EPhase.BotNicknameFinished); - } else if (e.TabPage == ASFTab) { + } else if (args.TabPage == ASFTab) { Tutorial.OnAction(Tutorial.EPhase.GlobalConfigOpened); } } - private void MainTab_Deselecting(object sender, TabControlCancelEventArgs e) { - if ((sender == null) || (e == null)) { + private void MainTab_Deselecting(object sender, TabControlCancelEventArgs args) { + if ((sender == null) || (args == null)) { + Logging.LogNullError(nameof(sender) + " || " + nameof(args)); return; } - OldTab = e.TabPage; + OldTab = args.TabPage; } - private void MainForm_Shown(object sender, EventArgs e) { - if ((sender == null) || (e == null)) { + private void MainForm_Shown(object sender, EventArgs args) { + if ((sender == null) || (args == null)) { + Logging.LogNullError(nameof(sender) + " || " + nameof(args)); return; } Tutorial.OnAction(Tutorial.EPhase.Shown); } - private void MainForm_HelpButtonClicked(object sender, CancelEventArgs e) { - if ((sender == null) || (e == null)) { + private void MainForm_HelpButtonClicked(object sender, CancelEventArgs args) { + if ((sender == null) || (args == null)) { + Logging.LogNullError(nameof(sender) + " || " + nameof(args)); return; } - e.Cancel = true; + args.Cancel = true; Tutorial.OnAction(Tutorial.EPhase.Help); Process.Start("https://github.com/JustArchi/ArchiSteamFarm/wiki/Configuration"); Tutorial.OnAction(Tutorial.EPhase.HelpFinished); diff --git a/ConfigGenerator/Program.cs b/ConfigGenerator/Program.cs index f7ac45408..be9d85f58 100644 --- a/ConfigGenerator/Program.cs +++ b/ConfigGenerator/Program.cs @@ -84,7 +84,8 @@ namespace ConfigGenerator { } private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args) { - if ((sender == null) || (args == null)) { + if ((sender == null) || (args == null) || (args.ExceptionObject == null)) { + Logging.LogNullError(nameof(sender) + " || " + nameof(args) + " || " + nameof(args.ExceptionObject)); return; } @@ -92,7 +93,8 @@ namespace ConfigGenerator { } private static void UnobservedTaskExceptionHandler(object sender, UnobservedTaskExceptionEventArgs args) { - if ((sender == null) || (args == null)) { + if ((sender == null) || (args == null) || (args.Exception == null)) { + Logging.LogNullError(nameof(sender) + " || " + nameof(args) + " || " + nameof(args.Exception)); return; }