From 070e91a6f9878e8c3f5795870e968a49ca53c8ef Mon Sep 17 00:00:00 2001 From: JustArchi Date: Fri, 17 Nov 2017 17:50:14 +0100 Subject: [PATCH] Misc improvement --- ArchiSteamFarm.sln.DotSettings | 6 ++++++ ArchiSteamFarm/ASF.cs | 4 ++-- ArchiSteamFarm/Bot.cs | 39 +++++++++++++++++++--------------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/ArchiSteamFarm.sln.DotSettings b/ArchiSteamFarm.sln.DotSettings index b0e1ec294..659c79b11 100644 --- a/ArchiSteamFarm.sln.DotSettings +++ b/ArchiSteamFarm.sln.DotSettings @@ -49,6 +49,7 @@ WARNING SUGGESTION SUGGESTION + SUGGESTION WARNING SUGGESTION SUGGESTION @@ -129,6 +130,8 @@ END_OF_LINE 1 1 + False + False END_OF_LINE NEVER @@ -139,6 +142,7 @@ NEVER False False + NEVER LINE_BREAK LINE_BREAK @@ -411,6 +415,8 @@ NOTIFY True True + True + True True True True diff --git a/ArchiSteamFarm/ASF.cs b/ArchiSteamFarm/ASF.cs index 920847cf7..9d66c6e88 100644 --- a/ArchiSteamFarm/ASF.cs +++ b/ArchiSteamFarm/ASF.cs @@ -229,7 +229,7 @@ namespace ArchiSteamFarm { await Bot.InitializeSteamConfiguration(Program.GlobalConfig.SteamProtocols, Program.GlobalDatabase.CellID, Program.GlobalDatabase.ServerListProvider).ConfigureAwait(false); foreach (string botName in Directory.EnumerateFiles(SharedInfo.ConfigDirectory, "*.json").Select(Path.GetFileNameWithoutExtension).Where(botName => !string.IsNullOrEmpty(botName) && IsValidBotName(botName)).OrderBy(botName => botName)) { - Bot.RegisterBot(botName); + await Bot.RegisterBot(botName).ConfigureAwait(false); } if (Bot.Bots.Count == 0) { @@ -271,7 +271,7 @@ namespace ArchiSteamFarm { return; } - Bot.RegisterBot(botName); + await Bot.RegisterBot(botName).ConfigureAwait(false); } private static bool IsUnixVersion(string version) { diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index a34289a58..f5313962e 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -54,6 +54,7 @@ namespace ArchiSteamFarm { internal static readonly ConcurrentDictionary Bots = new ConcurrentDictionary(); + private static readonly SemaphoreSlim BotsSemaphore = new SemaphoreSlim(1, 1); private static readonly SemaphoreSlim GiftsSemaphore = new SemaphoreSlim(1, 1); private static readonly SemaphoreSlim LoginSemaphore = new SemaphoreSlim(1, 1); @@ -677,7 +678,7 @@ namespace ArchiSteamFarm { BotConfig = args.BotConfig; InitModules(); - InitStart().Forget(); + InitStart(); } finally { InitializationSemaphore.Release(); } @@ -711,22 +712,28 @@ namespace ArchiSteamFarm { return false; } - internal static void RegisterBot(string botName) { + internal static async Task RegisterBot(string botName) { if (string.IsNullOrEmpty(botName)) { ASF.ArchiLogger.LogNullError(nameof(botName)); return; } - Bot bot; - - try { - bot = new Bot(botName); - } catch (ArgumentException e) { - ASF.ArchiLogger.LogGenericException(e); + if (Bots.ContainsKey(botName)) { return; } - bot.InitStart().Forget(); + await BotsSemaphore.WaitAsync().ConfigureAwait(false); + + try { + if (Bots.ContainsKey(botName)) { + return; + } + + Bot bot = new Bot(botName); + bot.InitStart(); + } finally { + BotsSemaphore.Release(); + } } internal void RequestPersonaStateUpdate() { @@ -1089,9 +1096,7 @@ namespace ArchiSteamFarm { return null; } - var response = new { - Bots = bots - }; + var response = new { Bots = bots }; try { return JsonConvert.SerializeObject(response); @@ -1276,7 +1281,7 @@ namespace ArchiSteamFarm { } ConnectionFailureTimer = new Timer( - e => InitPermanentConnectionFailure(), + async e => await InitPermanentConnectionFailure().ConfigureAwait(false), null, TimeSpan.FromMinutes(Math.Ceiling(Program.GlobalConfig.ConnectionTimeout / 30.0)), // Delay Timeout.InfiniteTimeSpan // Period @@ -1345,17 +1350,17 @@ namespace ArchiSteamFarm { } } - private void InitPermanentConnectionFailure() { + private async Task InitPermanentConnectionFailure() { if (!KeepRunning) { return; } ArchiLogger.LogGenericWarning(Strings.BotHeartBeatFailed); Destroy(true); - RegisterBot(BotName); + await RegisterBot(BotName).ConfigureAwait(false); } - private async Task InitStart() { + private void InitStart() { if ((BotConfig == null) || (BotDatabase == null)) { return; } @@ -1366,7 +1371,7 @@ namespace ArchiSteamFarm { } // Start - await Start().ConfigureAwait(false); + Start().Forget(); } private static bool IsAllowedToExecuteCommands(ulong steamID) {