diff --git a/ArchiSteamFarm/ArchiHandler.cs b/ArchiSteamFarm/ArchiHandler.cs index ab58f865a..983b3efe5 100644 --- a/ArchiSteamFarm/ArchiHandler.cs +++ b/ArchiSteamFarm/ArchiHandler.cs @@ -52,10 +52,12 @@ namespace ArchiSteamFarm { PurchaseResult = (EPurchaseResult) body.purchase_result_details; using (MemoryStream ms = new MemoryStream(body.purchase_receipt_info)) { - if (ReceiptInfo.TryReadAsBinary(ms)) { - foreach (KeyValue lineItem in ReceiptInfo["lineitems"].Children) { - Items.Add((uint) lineItem["PackageID"].AsUnsignedLong(), lineItem["ItemDescription"].AsString()); - } + if (!ReceiptInfo.TryReadAsBinary(ms)) { + return; + } + + foreach (KeyValue lineItem in ReceiptInfo["lineitems"].Children) { + Items.Add((uint) lineItem["PackageID"].AsUnsignedLong(), lineItem["ItemDescription"].AsString()); } } } @@ -91,11 +93,13 @@ namespace ArchiSteamFarm { internal void PlayGames(params ulong[] gameIDs) { var request = new ClientMsgProtobuf(EMsg.ClientGamesPlayed); foreach (ulong gameID in gameIDs) { - if (gameID != 0) { - request.Body.games_played.Add(new CMsgClientGamesPlayed.GamePlayed { - game_id = new GameID(gameID), - }); + if (gameID == 0) { + continue; } + + request.Body.games_played.Add(new CMsgClientGamesPlayed.GamePlayed { + game_id = new GameID(gameID), + }); } Client.Send(request); } @@ -108,15 +112,17 @@ namespace ArchiSteamFarm { } public sealed override void HandleMsg(IPacketMsg packetMsg) { - if (packetMsg != null) { - switch (packetMsg.MsgType) { - case EMsg.ClientPurchaseResponse: - HandlePurchaseResponse(packetMsg); - break; - case EMsg.ClientUserNotifications: - HandleUserNotifications(packetMsg); - break; - } + if (packetMsg == null) { + return; + } + + switch (packetMsg.MsgType) { + case EMsg.ClientPurchaseResponse: + HandlePurchaseResponse(packetMsg); + break; + case EMsg.ClientUserNotifications: + HandleUserNotifications(packetMsg); + break; } } diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index a32a6b794..64dd93a31 100644 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -24,6 +24,7 @@ using SteamKit2; using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Security.Cryptography; @@ -34,7 +35,7 @@ namespace ArchiSteamFarm { internal class Bot { private const ushort CallbackSleep = 500; // In miliseconds - private static readonly Dictionary Bots = new Dictionary(); + private static readonly ConcurrentDictionary Bots = new ConcurrentDictionary(); private readonly string ConfigFile; private readonly string SentryFile; @@ -67,19 +68,13 @@ namespace ArchiSteamFarm { internal bool Statistics { get; private set; } = true; internal static int GetRunningBotsCount() { - int result; - lock (Bots) { - result = Bots.Count; - } - return result; - } + return Bots.Count; + } internal static async Task ShutdownAllBots() { List tasks = new List(); - lock (Bots) { - foreach (Bot bot in Bots.Values) { - tasks.Add(Task.Run(async () => await bot.Shutdown().ConfigureAwait(false))); - } + foreach (Bot bot in Bots.Values) { + tasks.Add(Task.Run(async () => await bot.Shutdown().ConfigureAwait(false))); } await Task.WhenAll(tasks).ConfigureAwait(false); } @@ -102,9 +97,7 @@ namespace ArchiSteamFarm { return; } - lock (Bots) { - Bots.Add(BotName, this); - } + Bots.AddOrUpdate(BotName, this, (key, value) => this); // Initialize SteamClient = new SteamClient(); @@ -203,7 +196,7 @@ namespace ArchiSteamFarm { } } } - } catch (XmlException e) { + } catch (Exception e) { Logging.LogGenericException(BotName, e); Logging.LogGenericError(BotName, "Your config for this bot instance is invalid, it won't run!"); return false; @@ -243,9 +236,7 @@ namespace ArchiSteamFarm { } await botToShutdown.Stop().ConfigureAwait(false); - lock (Bots) { - Bots.Remove(botNameToShutdown); - } + Bots.TryRemove(botNameToShutdown, out botToShutdown); Program.OnBotShutdown(botToShutdown); return true; diff --git a/ArchiSteamFarm/CardsFarmer.cs b/ArchiSteamFarm/CardsFarmer.cs index 2342dae4e..fafc664a9 100644 --- a/ArchiSteamFarm/CardsFarmer.cs +++ b/ArchiSteamFarm/CardsFarmer.cs @@ -106,7 +106,7 @@ namespace ArchiSteamFarm { } Logging.LogGenericInfo(Bot.BotName, "Farming in progress..."); - NowFarming = true; + NowFarming = appIDs.Count > 0; Semaphore.Release(); // Start farming diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 334950263..ea5753684 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -39,19 +39,21 @@ namespace ArchiSteamFarm { TwoFactorAuthentication, } - internal const ulong ArchiSCFarmGroup = 103582791440160998; - internal const string ConfigDirectoryPath = "config"; private const string LatestGithubReleaseURL = "https://api.github.com/repos/JustArchi/ArchiSteamFarm/releases/latest"; - internal static readonly object ConsoleLock = new object(); + internal const ulong ArchiSCFarmGroup = 103582791440160998; + internal const string ConfigDirectoryPath = "config"; + private static readonly SemaphoreSlim SteamSemaphore = new SemaphoreSlim(1); private static readonly ManualResetEvent ShutdownResetEvent = new ManualResetEvent(false); private static readonly Assembly Assembly = Assembly.GetExecutingAssembly(); private static readonly string ExecutablePath = Assembly.Location; private static readonly AssemblyName AssemblyName = Assembly.GetName(); - private static readonly string ExeName = AssemblyName.Name + ".exe"; + //private static readonly string ExeName = AssemblyName.Name + ".exe"; private static readonly string Version = AssemblyName.Version.ToString(); + internal static readonly object ConsoleLock = new object(); + private static async Task CheckForUpdate() { JObject response = await Utilities.UrlToJObject(LatestGithubReleaseURL).ConfigureAwait(false); if (response == null) { @@ -69,11 +71,11 @@ namespace ArchiSteamFarm { Logging.LogGenericNotice("", "Remote version: " + remoteVersion); int comparisonResult = localVersion.CompareTo(remoteVersion); - if (localVersion.CompareTo(remoteVersion) < 0) { + if (comparisonResult < 0) { Logging.LogGenericNotice("", "New version is available!"); Logging.LogGenericNotice("", "Consider updating yourself!"); await Utilities.SleepAsync(5000).ConfigureAwait(false); - } else if (localVersion.CompareTo(remoteVersion) > 0) { + } else if (comparisonResult > 0) { Logging.LogGenericNotice("", "You're currently using pre-release version!"); Logging.LogGenericNotice("", "Be careful!"); }