From 08ce610eb2a7d926407b1b8859f72d316e8861f4 Mon Sep 17 00:00:00 2001 From: Ethan Moffat Date: Sat, 20 May 2023 01:07:44 -0700 Subject: [PATCH] Invoke game state change operations that generate lots of new components on the main update thread. Should fix crashes with GameComponentsCollection in MonoGame due to async threads modifying the components collection. Also improves performance of switching to in-game. --- EndlessClient/Controllers/LoginController.cs | 19 ++++++++++++------- .../Controllers/MainButtonController.cs | 12 +++++++++--- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/EndlessClient/Controllers/LoginController.cs b/EndlessClient/Controllers/LoginController.cs index 4431105de..de1974a48 100644 --- a/EndlessClient/Controllers/LoginController.cs +++ b/EndlessClient/Controllers/LoginController.cs @@ -101,7 +101,9 @@ public async Task LoginToAccount(ILoginParameters loginParameters) var reply = loginToServerOperation.Result; if (reply == LoginReply.Ok) - _gameStateActions.ChangeToState(GameStates.LoggedIn); + { + await DispatcherGameComponent.Invoke(() => _gameStateActions.ChangeToState(GameStates.LoggedIn)); + } else { _errorDisplayAction.ShowLoginError(reply); @@ -227,12 +229,15 @@ await DispatcherGameComponent.Invoke(() => _clientWindowSizeRepository.Resizable = true; } - _gameStateActions.ChangeToState(GameStates.PlayingTheGame); - _chatTextBoxActions.FocusChatTextBox(); - _statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_WARNING, - EOResourceID.LOADING_GAME_HINT_FIRST); - _firstTimePlayerActions.WarnFirstTimePlayers(); - _mapChangedActions.ActiveCharacterEnterMapForLogin(); + await DispatcherGameComponent.Invoke(() => + { + _gameStateActions.ChangeToState(GameStates.PlayingTheGame); + _chatTextBoxActions.FocusChatTextBox(); + _statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_WARNING, + EOResourceID.LOADING_GAME_HINT_FIRST); + _firstTimePlayerActions.WarnFirstTimePlayers(); + _mapChangedActions.ActiveCharacterEnterMapForLogin(); + }); } private void SetInitialStateAndShowError(NoDataSentException ex) diff --git a/EndlessClient/Controllers/MainButtonController.cs b/EndlessClient/Controllers/MainButtonController.cs index fc95380af..5f50375bb 100644 --- a/EndlessClient/Controllers/MainButtonController.cs +++ b/EndlessClient/Controllers/MainButtonController.cs @@ -1,6 +1,7 @@ using AutomaticTypeMapper; using EndlessClient.Dialogs.Actions; using EndlessClient.GameExecution; +using EndlessClient.Rendering; using EOLib.Domain; using EOLib.Domain.Protocol; using EOLib.Net.Communication; @@ -66,8 +67,11 @@ public async Task ClickCreateAccount() if (result) { - _gameStateActions.ChangeToState(GameStates.CreateAccount); - _accountDialogDisplayActions.ShowInitialCreateWarningDialog(); + await DispatcherGameComponent.Invoke(() => + { + _gameStateActions.ChangeToState(GameStates.CreateAccount); + _accountDialogDisplayActions.ShowInitialCreateWarningDialog(); + }); } } @@ -76,7 +80,9 @@ public async Task ClickLogin() var result = await StartNetworkConnection().ConfigureAwait(false); if (result) - _gameStateActions.ChangeToState(GameStates.Login); + { + await DispatcherGameComponent.Invoke(() => _gameStateActions.ChangeToState(GameStates.Login)); + } } public void ClickViewCredits()