From d6ebeca4838fe008b19d7c34450a6ddae6555e5f Mon Sep 17 00:00:00 2001 From: Chase <60807752+Chase-Percy@users.noreply.github.com> Date: Wed, 26 Apr 2023 15:34:39 +0800 Subject: [PATCH] Fix #19935: Part of old multiplayer name remains when entering a shorter name (#20004) * clear _playerName * Change _playerName to a u8string * Explicitly set underlying string ptr on change --- src/openrct2-ui/windows/ServerList.cpp | 25 +++++++++++-------------- src/openrct2/interface/Window.cpp | 2 +- src/openrct2/interface/Window.h | 2 +- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/openrct2-ui/windows/ServerList.cpp b/src/openrct2-ui/windows/ServerList.cpp index fae9da1633..9244d3f1f5 100644 --- a/src/openrct2-ui/windows/ServerList.cpp +++ b/src/openrct2-ui/windows/ServerList.cpp @@ -33,6 +33,8 @@ # define WHEIGHT_MAX 800 # define ITEM_HEIGHT (3 + 9 + 3) +constexpr size_t MaxPlayerNameLength = 32; + enum { WIDX_BACKGROUND, @@ -76,7 +78,7 @@ void JoinServer(std::string address); class ServerListWindow final : public Window { private: - char _playerName[32 + 1] = {}; + u8string _playerName; ServerList _serverList; std::future, StringId>> _fetchFuture; uint32_t _numPlayersOnline = 0; @@ -90,7 +92,8 @@ public: void OnOpen() override { - window_server_list_widgets[WIDX_PLAYER_NAME_INPUT].string = _playerName; + _playerName = gConfigNetwork.PlayerName; + window_server_list_widgets[WIDX_PLAYER_NAME_INPUT].string = const_cast(_playerName.c_str()); widgets = window_server_list_widgets; InitScrollWidgets(); no_list_items = 0; @@ -106,8 +109,6 @@ public: WindowSetResize(*this, WWIDTH_MIN, WHEIGHT_MIN, WWIDTH_MAX, WHEIGHT_MAX); - SafeStrCpy(_playerName, gConfigNetwork.PlayerName.c_str(), sizeof(_playerName)); - no_list_items = static_cast(_serverList.GetCount()); ServerListFetchServersBegin(); @@ -117,6 +118,7 @@ public: { _serverList = {}; _fetchFuture = {}; + ConfigSaveDefault(); } void OnMouseUp(WidgetIndex widgetIndex) override @@ -127,7 +129,7 @@ public: Close(); break; case WIDX_PLAYER_NAME_INPUT: - WindowStartTextbox(*this, widgetIndex, STR_STRING, _playerName, 63); + WindowStartTextbox(*this, widgetIndex, STR_STRING, _playerName.c_str(), MaxPlayerNameLength); break; case WIDX_LIST: { @@ -267,7 +269,7 @@ public: if (text.empty()) return; - std::string temp = static_cast(text); + auto temp = u8string{ text }; switch (widgetIndex) { @@ -275,14 +277,9 @@ public: if (_playerName == text) return; - text.copy(_playerName, sizeof(_playerName)); - - // Don't allow empty player names - if (_playerName[0] != '\0') - { - gConfigNetwork.PlayerName = _playerName; - ConfigSaveDefault(); - } + _playerName = temp; + gConfigNetwork.PlayerName = _playerName; + window_server_list_widgets[WIDX_PLAYER_NAME_INPUT].string = const_cast(_playerName.c_str()); InvalidateWidget(WIDX_PLAYER_NAME_INPUT); break; diff --git a/src/openrct2/interface/Window.cpp b/src/openrct2/interface/Window.cpp index 6664ffb5ca..4dae25480d 100644 --- a/src/openrct2/interface/Window.cpp +++ b/src/openrct2/interface/Window.cpp @@ -1973,7 +1973,7 @@ void TextinputCancel() } void WindowStartTextbox( - WindowBase& call_w, WidgetIndex call_widget, StringId existing_text, char* existing_args, int32_t maxLength) + WindowBase& call_w, WidgetIndex call_widget, StringId existing_text, const char* existing_args, int32_t maxLength) { if (gUsingWidgetTextBox) WindowCancelTextbox(); diff --git a/src/openrct2/interface/Window.h b/src/openrct2/interface/Window.h index 5e0bf40d3b..f274acbf35 100644 --- a/src/openrct2/interface/Window.h +++ b/src/openrct2/interface/Window.h @@ -723,7 +723,7 @@ void WindowMoveAndSnap(WindowBase& w, ScreenCoordsXY newWindowCoords, int32_t sn int32_t WindowCanResize(const WindowBase& w); void WindowStartTextbox( - WindowBase& call_w, WidgetIndex call_widget, StringId existing_text, char* existing_args, int32_t maxLength); + WindowBase& call_w, WidgetIndex call_widget, StringId existing_text, const char* existing_args, int32_t maxLength); void WindowCancelTextbox(); void WindowUpdateTextboxCaret(); void WindowUpdateTextbox();