Skip to content

Commit

Permalink
Invoke game state change operations that generate lots of new compone…
Browse files Browse the repository at this point in the history
…nts 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.
  • Loading branch information
ethanmoffat committed May 20, 2023
1 parent 3a3935e commit 08ce610
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
19 changes: 12 additions & 7 deletions EndlessClient/Controllers/LoginController.cs
Expand Up @@ -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);
Expand Down Expand Up @@ -227,12 +229,15 @@ public async Task LoginToCharacter(Character character)
_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)
Expand Down
12 changes: 9 additions & 3 deletions 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;
Expand Down Expand Up @@ -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();
});
}
}

Expand All @@ -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()
Expand Down

0 comments on commit 08ce610

Please sign in to comment.