Skip to content

Commit

Permalink
chatscreen: Fix possible crash handling chat / cheat codes
Browse files Browse the repository at this point in the history
If effects of the cheat code modified overlay screens / widgets in certain ways
  • Loading branch information
past-due committed Oct 10, 2023
1 parent a0a6ea3 commit 69b9a0c
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions src/screens/chatscreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,43 +201,64 @@ void WzInGameChatBoxForm::initialize(WzChatMode initialChatMode, const W_EDITBOX
ASSERT_OR_RETURN(, strongParent != nullptr, "No parent?");

auto enteredText = widg.getString();
const char* pStr = enteredText.toUtf8().c_str();
auto message = InGameChatMessage(selectedPlayer, pStr);
attemptCheatCode(message.text); // parse the message
WzChatMode currChatMode = strongParent->chatMode;
inputLoseFocus();

switch (strongParent->chatMode)
{
case WzChatMode::Glob:
parseChatMessageModifiers(message);
break;
case WzChatMode::Team:
message.toAllies = true;
break;
}
auto weakParent = std::weak_ptr<WzInGameChatBoxForm>(strongParent);
widgScheduleTask([enteredText, currChatMode, weakParent]() {

if (strlen(message.text))
{
message.send();
}
// must process the enteredText inside a scheduled task as side effects of attemptCheatCode may modify the overlay screens / widgets

inputLoseFocus();
const char* pStr = enteredText.toUtf8().c_str();
auto message = InGameChatMessage(selectedPlayer, pStr);
attemptCheatCode(message.text); // parse the message

switch (currChatMode)
{
case WzChatMode::Glob:
parseChatMessageModifiers(message);
break;
case WzChatMode::Team:
message.toAllies = true;
break;
}

strongParent->endChatBoxEditing();
strongParent->closeParentScreen();
if (strlen(message.text))
{
message.send();
}

auto parent = weakParent.lock();
ASSERT_OR_RETURN(, parent != nullptr, "No parent?");
parent->endChatBoxEditing();
parent->closeParentScreen();
});
});

chatBox->setOnEscapeHandler([](W_EDITBOX& widg) {
auto strongParent = std::dynamic_pointer_cast<WzInGameChatBoxForm>(widg.parent());
ASSERT_OR_RETURN(, strongParent != nullptr, "No parent?");

inputLoseFocus();
strongParent->closeParentScreen();

auto weakParent = std::weak_ptr<WzInGameChatBoxForm>(strongParent);
widgScheduleTask([weakParent]() {
auto parent = weakParent.lock();
ASSERT_OR_RETURN(, parent != nullptr, "No parent?");
parent->closeParentScreen();
});
});

chatBox->setOnEditingStoppedHandler([](W_EDITBOX& widg) {
auto strongParent = std::dynamic_pointer_cast<WzInGameChatBoxForm>(widg.parent());
ASSERT_OR_RETURN(, strongParent != nullptr, "No parent?");
strongParent->endChatBoxEditing();

auto weakParent = std::weak_ptr<WzInGameChatBoxForm>(strongParent);
widgScheduleTask([weakParent]() {
auto parent = weakParent.lock();
ASSERT_OR_RETURN(, parent != nullptr, "No parent?");
parent->endChatBoxEditing();
});
});

if (onTabHandler)
Expand Down

0 comments on commit 69b9a0c

Please sign in to comment.