diff --git a/src/config/stk_config.cpp b/src/config/stk_config.cpp index eb6780d0177..05a544ba2d3 100644 --- a/src/config/stk_config.cpp +++ b/src/config/stk_config.cpp @@ -265,6 +265,8 @@ void STKConfig::init_defaults() m_banana_item_return_ticks = -100; m_bubblegum_item_return_ticks = -100; + m_max_username_length = 50; + m_score_increase.clear(); m_leader_intervals.clear(); m_switch_items.clear(); diff --git a/src/config/stk_config.hpp b/src/config/stk_config.hpp index bc157bb6427..2972a6c6d50 100644 --- a/src/config/stk_config.hpp +++ b/src/config/stk_config.hpp @@ -266,6 +266,9 @@ class STKConfig : public NoCopy * version. */ std::set m_network_capabilities; + int m_max_username_length; + + private: /** True if stk_config has been loaded. This is necessary if the * --stk-config command line parameter has been specified to avoid diff --git a/src/states_screens/online/register_screen.cpp b/src/states_screens/online/register_screen.cpp index fd3b0c62b00..91b4a1b1505 100644 --- a/src/states_screens/online/register_screen.cpp +++ b/src/states_screens/online/register_screen.cpp @@ -36,6 +36,8 @@ #include "utils/log.hpp" #include "utils/translation.hpp" +#include "config/stk_config.hpp" + #ifdef __SWITCH__ extern "C" { #define u64 uint64_t @@ -254,10 +256,21 @@ void RegisterScreen::makeEntryFieldsVisible() /** If necessary creates the local user. * \param local_name Name of the local user. */ -void RegisterScreen::handleLocalName(const stringw &local_name) +bool RegisterScreen::handleLocalName(const stringw &local_name) { - if (local_name.size() == 0) - return; + if (local_name.empty()) + { + m_info_widget->setErrorColor(); + m_info_widget->setText(_("User name cannot be empty."), false); + return false; + } + + if (local_name.size() > stk_config->m_max_username_length){ + m_info_widget->setErrorColor(); + m_info_widget->setText(_("Username is too long '%s'.", local_name), + false); + return false; + } // If a local player with that name does not exist, create one if(!PlayerManager::get()->getPlayer(local_name)) @@ -283,6 +296,7 @@ void RegisterScreen::handleLocalName(const stringw &local_name) m_info_widget->setErrorColor(); m_info_widget->setText(_("Could not create player '%s'.", local_name), false); + return false; } } else @@ -290,7 +304,9 @@ void RegisterScreen::handleLocalName(const stringw &local_name) m_info_widget->setErrorColor(); m_info_widget->setText(_("Could not create player '%s'.", local_name), false); + return false; } + return true; } // handleLocalName // ----------------------------------------------------------------------------- @@ -301,15 +317,9 @@ void RegisterScreen::doRegister() { stringw local_name = getWidget("local_username") ->getText().trim(); - - if (local_name.empty()) - { - m_info_widget->setErrorColor(); - m_info_widget->setText(_("User name cannot be empty."), false); - return; - } - handleLocalName(local_name); + if (!handleLocalName(local_name)) + return; // If no online account is requested, don't register if(m_account_mode==ACCOUNT_EXISTING_ONLINE) diff --git a/src/states_screens/online/register_screen.hpp b/src/states_screens/online/register_screen.hpp index 58876339791..246edef0eda 100644 --- a/src/states_screens/online/register_screen.hpp +++ b/src/states_screens/online/register_screen.hpp @@ -38,7 +38,7 @@ class RegisterScreen : public GUIEngine::Screen, friend class GUIEngine::ScreenSingleton; void makeEntryFieldsVisible(); - void handleLocalName(const irr::core::stringw &local_name); + bool handleLocalName(const irr::core::stringw &local_name); void doRegister(); void init() OVERRIDE; RegisterScreen();