1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-21 05:53:02 +01:00

Upgrade platform_get_username()

This commit is contained in:
Gymnasiast
2022-01-05 14:42:46 +01:00
parent 2e7e90a018
commit a21e545917
7 changed files with 27 additions and 25 deletions

View File

@@ -388,7 +388,7 @@ namespace Config
auto playerName = reader->GetString("player_name", "");
if (playerName.empty())
{
playerName = platform_get_username();
playerName = Platform::GetUsername();
if (playerName.empty())
{
playerName = "Player";

View File

@@ -253,6 +253,17 @@ namespace Platform
{
return true;
}
std::string GetUsername()
{
std::string result;
auto pw = getpwuid(getuid());
if (pw != nullptr)
{
result = std::string(pw->pw_name);
}
return result;
}
} // namespace Platform
#endif

View File

@@ -16,6 +16,7 @@
# include "../Version.h"
# include <datetimeapi.h>
# include <lmcons.h>
# include <memory>
# include <shlobj.h>
# undef GetEnvironmentVariable
@@ -613,6 +614,18 @@ namespace Platform
// using the same window in an unaccelerated and accelerated context is unsupported by SDL2
return openGL;
}
std::string GetUsername()
{
std::string result;
wchar_t usernameW[UNLEN + 1]{};
DWORD usernameLength = UNLEN + 1;
if (GetUserNameW(usernameW, &usernameLength))
{
result = String::ToUtf8(usernameW);
}
return result;
}
} // namespace Platform
#endif

View File

@@ -48,6 +48,8 @@ namespace Platform
bool OriginalGameDataExists(std::string_view path);
std::string GetUsername();
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__)
std::string GetEnvironmentPath(const char* name);
std::string GetHomePath();

View File

@@ -337,17 +337,6 @@ datetime64 platform_get_datetime_now_utc()
return utcNow;
}
std::string platform_get_username()
{
std::string result;
auto pw = getpwuid(getuid());
if (pw != nullptr)
{
result = std::string(pw->pw_name);
}
return result;
}
bool platform_process_is_elevated()
{
# ifndef __EMSCRIPTEN__

View File

@@ -460,18 +460,6 @@ datetime64 platform_get_datetime_now_utc()
return utcNow;
}
std::string platform_get_username()
{
std::string result;
wchar_t usernameW[UNLEN + 1]{};
DWORD usernameLength = UNLEN + 1;
if (GetUserNameW(usernameW, &usernameLength))
{
result = String::ToUtf8(usernameW);
}
return result;
}
bool platform_process_is_elevated()
{
BOOL isElevated = FALSE;

View File

@@ -105,7 +105,6 @@ bool platform_file_delete(const utf8* path);
uint32_t platform_get_ticks();
void platform_sleep(uint32_t ms);
void platform_get_user_directory(utf8* outPath, const utf8* subDirectory, size_t outSize);
std::string platform_get_username();
bool platform_open_common_file_dialog(utf8* outFilename, file_dialog_desc* desc, size_t outSize);
utf8* platform_open_directory_browser(const utf8* title);
CurrencyType platform_get_locale_currency();