From b3200937dac9375bc76306809a58cd6d1befa156 Mon Sep 17 00:00:00 2001 From: Benau Date: Sat, 29 Jun 2019 00:31:30 +0800 Subject: [PATCH] Allow getting unicode environment variables in windows --- src/config/player_manager.cpp | 15 +++++++++++---- src/io/file_manager.cpp | 8 ++++++-- src/states_screens/online/register_screen.cpp | 15 +++++++++++---- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/config/player_manager.cpp b/src/config/player_manager.cpp index 522d6bf22..7819b120a 100644 --- a/src/config/player_manager.cpp +++ b/src/config/player_manager.cpp @@ -348,12 +348,19 @@ void PlayerManager::addDefaultPlayer() { std::string username = "Player"; - if(getenv("USERNAME")!=NULL) // for windows - username = getenv("USERNAME"); - else if(getenv("USER")!=NULL) // Linux, Macs +#if defined(WIN32) + std::vector env; + // An environment variable has a maximum size limit of 32,767 characters + env.resize(32767, 0); + DWORD length = GetEnvironmentVariable(L"USERNAME", env.data(), 32767); + if (length != 0) + username = StringUtils::wideToUtf8(env.data()); +#else + if (getenv("USER") != NULL) // Linux, Macs username = getenv("USER"); - else if(getenv("LOGNAME")!=NULL) // Linux, Macs + else if (getenv("LOGNAME") != NULL) // Linux, Macs username = getenv("LOGNAME"); +#endif // Set the name as the default name, but don't mark it as 'default' // yet, since not having a default player forces the player selection diff --git a/src/io/file_manager.cpp b/src/io/file_manager.cpp index cd82c0688..49329e0c5 100644 --- a/src/io/file_manager.cpp +++ b/src/io/file_manager.cpp @@ -859,9 +859,13 @@ void FileManager::checkAndCreateConfigDir() // Try to use the APPDATA directory to store config files and highscore // lists. If not defined, used the current directory. - if (getenv("APPDATA") != NULL) + std::vector env; + // An environment variable has a maximum size limit of 32,767 characters + env.resize(32767, 0); + DWORD length = GetEnvironmentVariable(L"APPDATA", env.data(), 32767); + if (length != 0) { - m_user_config_dir = getenv("APPDATA"); + m_user_config_dir = StringUtils::wideToUtf8(env.data()); if (!checkAndCreateDirectory(m_user_config_dir)) { Log::error("[FileManager]", "Can't create config dir '%s" diff --git a/src/states_screens/online/register_screen.cpp b/src/states_screens/online/register_screen.cpp index 71fb4fbd6..56746e7ff 100644 --- a/src/states_screens/online/register_screen.cpp +++ b/src/states_screens/online/register_screen.cpp @@ -89,12 +89,19 @@ void RegisterScreen::init() } else if (PlayerManager::get()->getNumPlayers() == 0) { - if (getenv("USERNAME") != NULL) // for windows - username = getenv("USERNAME"); - else if (getenv("USER") != NULL) // Linux, Macs +#if defined(WIN32) + std::vector env; + // An environment variable has a maximum size limit of 32,767 characters + env.resize(32767, 0); + DWORD length = GetEnvironmentVariable(L"USERNAME", env.data(), 32767); + if (length != 0) + username = env.data(); +#else + if (getenv("USER") != NULL) // Linux, Macs username = getenv("USER"); - else if (getenv("LOGNAME") != NULL) // Linux, Macs + else if (getenv("LOGNAME") != NULL) // Linux, Macs username = getenv("LOGNAME"); +#endif } TextBoxWidget* local_username = getWidget("local_username");