Allow getting unicode environment variables in windows

This commit is contained in:
Benau 2019-06-29 00:31:30 +08:00
parent bc555ac514
commit b3200937da
3 changed files with 28 additions and 10 deletions

View File

@ -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<wchar_t> 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

View File

@ -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<wchar_t> 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"

View File

@ -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<wchar_t> 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<TextBoxWidget>("local_username");