From d74f13c567281e90fc1ebb33231687c001d9b41b Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sun, 25 Aug 2019 19:25:28 +0200 Subject: [PATCH] Code review --- ArchiSteamFarm/Bot.cs | 17 ++++++++--------- .../Collections/ConcurrentEnumerator.cs | 6 +++++- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 7f4451815..94d1ddca6 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -203,21 +203,12 @@ namespace ArchiSteamFarm { throw new ArgumentNullException(nameof(botName) + " || " + nameof(botConfig) + " || " + nameof(botDatabase)); } - if (Bots.ContainsKey(botName)) { - throw new ArgumentException(string.Format(Strings.ErrorIsInvalid, nameof(botName))); - } - BotName = botName; BotConfig = botConfig; BotDatabase = botDatabase; ArchiLogger = new ArchiLogger(botName); - // Register bot as available for ASF - if (!Bots.TryAdd(botName, this)) { - throw new ArgumentException(string.Format(Strings.ErrorIsInvalid, nameof(botName))); - } - if (HasMobileAuthenticator) { BotDatabase.MobileAuthenticator.Init(this); } @@ -1080,6 +1071,7 @@ namespace ArchiSteamFarm { } Bot bot; + await BotsSemaphore.WaitAsync().ConfigureAwait(false); try { @@ -1088,6 +1080,13 @@ namespace ArchiSteamFarm { } bot = new Bot(botName, botConfig, botDatabase); + + if (!Bots.TryAdd(botName, bot)) { + ASF.ArchiLogger.LogNullError(nameof(bot)); + bot.Dispose(); + + return; + } } finally { BotsSemaphore.Release(); } diff --git a/ArchiSteamFarm/Collections/ConcurrentEnumerator.cs b/ArchiSteamFarm/Collections/ConcurrentEnumerator.cs index bd3f3196d..377f7ad93 100644 --- a/ArchiSteamFarm/Collections/ConcurrentEnumerator.cs +++ b/ArchiSteamFarm/Collections/ConcurrentEnumerator.cs @@ -42,7 +42,11 @@ namespace ArchiSteamFarm.Collections { Enumerator = collection.GetEnumerator(); } - public void Dispose() => Lock.Dispose(); + public void Dispose() { + Enumerator.Dispose(); + Lock.Dispose(); + } + public bool MoveNext() => Enumerator.MoveNext(); public void Reset() => Enumerator.Reset(); }