From d74a8a9ee06e2d95aafe7b31d302904ec3a0e972 Mon Sep 17 00:00:00 2001 From: Ethan Moffat Date: Wed, 29 Jan 2025 22:03:45 +0000 Subject: [PATCH] Add missing 'break' to BotFramework and ConfigureAwait(false) to ScriptedBot async calls. --- EOBot/BotFramework.cs | 10 ++++++---- .../BuiltInIdentifierConfigurator.cs | 18 +++++++++--------- EOBot/Program.cs | 4 ++-- EOBot/ScriptedBot.cs | 4 ++-- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/EOBot/BotFramework.cs b/EOBot/BotFramework.cs index bfac9a9d0..8175433fd 100644 --- a/EOBot/BotFramework.cs +++ b/EOBot/BotFramework.cs @@ -64,11 +64,13 @@ public async Task InitializeAsync(IBotFactory botFactory, int delayBetweenInitsM { var bot = botFactory.CreateBot(i); bot.WorkCompleted += () => _doneSignal.Release(); - await bot.InitializeAsync(_host, _port); + await bot.InitializeAsync(_host, _port).ConfigureAwait(false); _botsList.Add(bot); ConsoleHelper.WriteMessage(ConsoleHelper.Type.None, $"Bot {i} initialized."); - await Task.Delay(delayBetweenInitsMS); // minimum for this is 1sec server-side + await Task.Delay(delayBetweenInitsMS).ConfigureAwait(false); // minimum for this is 1sec server-side + + break; } catch (Exception ex) { @@ -83,7 +85,7 @@ public async Task InitializeAsync(IBotFactory botFactory, int delayBetweenInitsM { var retryDelayTime = TimeSpan.FromMilliseconds(delayBetweenInitsMS + (1000 * attempt * attempt)); ConsoleHelper.WriteMessage(ConsoleHelper.Type.Warning, $"Bot {i} failed to initialize. Retrying in {retryDelayTime.TotalMilliseconds}ms...", ConsoleColor.DarkYellow); - await Task.Delay(retryDelayTime); + await Task.Delay(retryDelayTime).ConfigureAwait(false); } } } @@ -123,7 +125,7 @@ public async Task RunAsync() var continuation = Task.WhenAll(botTasks); try { - await continuation; + await continuation.ConfigureAwait(false); } catch { } diff --git a/EOBot/Interpreter/BuiltInIdentifierConfigurator.cs b/EOBot/Interpreter/BuiltInIdentifierConfigurator.cs index 25d7d98a6..e1cdc5d30 100644 --- a/EOBot/Interpreter/BuiltInIdentifierConfigurator.cs +++ b/EOBot/Interpreter/BuiltInIdentifierConfigurator.cs @@ -116,14 +116,14 @@ private async Task ConnectAsync(string host, int port) configRepo.VersionBuild = ((IntVariable)_state.SymbolTable[PredefinedIdentifiers.VERSION].Identifiable).Value; var connectionActions = c.Resolve(); - var connectResult = await connectionActions.ConnectToServer(); + var connectResult = await connectionActions.ConnectToServer().ConfigureAwait(false); if (connectResult != ConnectResult.Success) throw new ArgumentException($"Bot {_botIndex}: Unable to connect to server! Host={host} Port={port}"); var backgroundReceiveActions = c.Resolve(); backgroundReceiveActions.RunBackgroundReceiveLoop(); - var handshakeResult = await connectionActions.BeginHandshake(_random.Next(Constants.MaxChallenge)); + var handshakeResult = await connectionActions.BeginHandshake(_random.Next(Constants.MaxChallenge)).ConfigureAwait(false); if (handshakeResult.ReplyCode != InitReply.Ok) throw new InvalidOperationException($"Bot {_botIndex}: Invalid response from server or connection failed! Must receive an OK reply."); @@ -149,20 +149,20 @@ private void Disconnect() private async Task CreateAccountAsync(string user, string pass) { - return (int)await _botHelper.CreateAccountAsync(user, pass); + return (int)await _botHelper.CreateAccountAsync(user, pass).ConfigureAwait(false); } private async Task LoginAsync(string user, string pass) { - return (int)await _botHelper.LoginToAccountAsync(user, pass); + return (int)await _botHelper.LoginToAccountAsync(user, pass).ConfigureAwait(false); } private async Task CreateAndLoginAsync(string user, string pass) { - var accountReply = (AccountReply)await CreateAccountAsync(user, pass); + var accountReply = (AccountReply)await CreateAccountAsync(user, pass).ConfigureAwait(false); if (accountReply == AccountReply.Created || accountReply == AccountReply.Exists) { - return await LoginAsync(user, pass); + return await LoginAsync(user, pass).ConfigureAwait(false); } return (int)LoginReply.WrongUser; @@ -170,17 +170,17 @@ private async Task CreateAndLoginAsync(string user, string pass) private async Task ChangePasswordAsync(string user, string oldPass, string newPass) { - return (int)await _botHelper.ChangePasswordAsync(user, oldPass, newPass); + return (int)await _botHelper.ChangePasswordAsync(user, oldPass, newPass).ConfigureAwait(false); } private async Task CreateCharacterAsync(string charName) { - return (int)await _botHelper.CreateCharacterAsync(charName); + return (int)await _botHelper.CreateCharacterAsync(charName).ConfigureAwait(false); } private async Task DeleteCharacterAsync(string charName, bool force) { - return (int)await _botHelper.DeleteCharacterAsync(charName, force); + return (int)await _botHelper.DeleteCharacterAsync(charName, force).ConfigureAwait(false); } private Task LoginToCharacterAsync(string charName) diff --git a/EOBot/Program.cs b/EOBot/Program.cs index 5074d1e5c..c7be4aba3 100644 --- a/EOBot/Program.cs +++ b/EOBot/Program.cs @@ -217,8 +217,8 @@ static async Task Main(string[] args) { using (f = new BotFramework(parsedArgs)) { - await f.InitializeAsync(botFactory, parsedArgs.InitDelay); - await f.RunAsync(); + await f.InitializeAsync(botFactory, parsedArgs.InitDelay).ConfigureAwait(false); + await f.RunAsync().ConfigureAwait(false); } Console.WriteLine(); diff --git a/EOBot/ScriptedBot.cs b/EOBot/ScriptedBot.cs index 246cd2bd8..48e2c2eaf 100644 --- a/EOBot/ScriptedBot.cs +++ b/EOBot/ScriptedBot.cs @@ -32,7 +32,7 @@ public override async Task InitializeAsync(string host, int port) throw new InvalidOperationException("Something went wrong getting the connect function out of the symbol table"); // call connect function that uses user-defined $version variable instead of base logic that has it hard-coded - await connectFunction.CallAsync(new StringVariable(_parsedArgs.Host), new IntVariable(_parsedArgs.Port)); + await connectFunction.CallAsync(new StringVariable(_parsedArgs.Host), new IntVariable(_parsedArgs.Port)).ConfigureAwait(false); WorkCompleted += () => { @@ -51,7 +51,7 @@ protected override async Task DoWorkAsync(CancellationToken ct) if (_programState == null) throw new InvalidOperationException("Scripted bot must be initialized before it is run"); - await _interpreter.Run(_programState); + await _interpreter.Run(_programState).ConfigureAwait(false); } } }