From 47650191032771fd38b72e5090efcc50639523b1 Mon Sep 17 00:00:00 2001 From: luk3yx Date: Mon, 30 May 2022 19:44:52 +1200 Subject: [PATCH] Stop main menu and CSM formspec input errors from crashing the game entirely on Android --- src/client/game.cpp | 14 ++++++++++++-- src/gui/guiEngine.cpp | 24 ++++++++++++++++++------ src/gui/guiEngine.h | 2 ++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/client/game.cpp b/src/client/game.cpp index 582b7f783..ac557aebb 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -195,8 +195,18 @@ struct LocalFormspecHandler : public TextDest return; } - if (m_client->modsLoaded()) - m_client->getScript()->on_formspec_input(m_formname, fields); + if (m_client->modsLoaded()) { + try { + m_client->getScript()->on_formspec_input(m_formname, fields); + } catch (LuaError &e) { + const std::string error_message = std::string("LuaError: ") + e.what() + + strgettext("\nCheck debug.txt for details."); + m_client->setFatalError(error_message); +#ifdef __ANDROID__ + porting::handleError("LuaError (on_formspec_input)", error_message); +#endif + } + } } Client *m_client = nullptr; diff --git a/src/gui/guiEngine.cpp b/src/gui/guiEngine.cpp index c6d0a8b5a..6a0f24ec7 100644 --- a/src/gui/guiEngine.cpp +++ b/src/gui/guiEngine.cpp @@ -48,7 +48,11 @@ with this program; if not, write to the Free Software Foundation, Inc., /******************************************************************************/ void TextDestGuiEngine::gotText(const StringMap &fields) { - m_engine->getScriptIface()->handleMainMenuButtons(fields); + try { + m_engine->getScriptIface()->handleMainMenuButtons(fields); + } catch (LuaError &e) { + m_engine->handleMainMenuLuaError(e.what()); + } } /******************************************************************************/ @@ -199,11 +203,7 @@ GUIEngine::GUIEngine(JoystickController *joystick, run(); } catch (LuaError &e) { - errorstream << "Main menu error: " << e.what() << std::endl; - m_data->script_data.errormessage = e.what(); -#ifdef __ANDROID__ - porting::handleError("Main menu error", e.what()); -#endif + handleMainMenuLuaError(e.what()); } m_menu->quitMenu(); @@ -211,6 +211,18 @@ GUIEngine::GUIEngine(JoystickController *joystick, m_menu = NULL; } +void GUIEngine::handleMainMenuLuaError(const char* errmsg) { + errorstream << "Main menu error: " << errmsg << std::endl; + m_data->script_data.errormessage = errmsg; +#ifdef __ANDROID__ + porting::handleError("Main menu error", errmsg); +#endif + + // Make the menu quit. Since an error message has been set this won't + // actually start the game. + m_startgame = true; +} + /******************************************************************************/ bool GUIEngine::loadMainMenuScript() { diff --git a/src/gui/guiEngine.h b/src/gui/guiEngine.h index 4888c822a..d284db613 100644 --- a/src/gui/guiEngine.h +++ b/src/gui/guiEngine.h @@ -157,6 +157,8 @@ public: /** default destructor */ virtual ~GUIEngine(); + void handleMainMenuLuaError(const char* errmsg); + /** * return MainMenuScripting interface */