From 75b072d2664358f52a85791b9a9314c12e502581 Mon Sep 17 00:00:00 2001 From: Alexander Overvoorde Date: Wed, 10 Feb 2016 21:49:10 +0100 Subject: [PATCH 1/5] Change default player name to use the OS username if available (resolves #2761) --- src/config.c | 3 +++ src/platform/platform.h | 1 + src/platform/posix.c | 11 +++++++++++ src/platform/windows.c | 12 ++++++++++++ 4 files changed, 27 insertions(+) diff --git a/src/config.c b/src/config.c index 2eb1f44c9a..f9ae7d5ac9 100644 --- a/src/config.c +++ b/src/config.c @@ -374,6 +374,9 @@ void config_set_defaults() else if (strcmp(property->property_name, "temperature_format") == 0){ destValue->value_uint8 = platform_get_locale_temperature_format(); } + else if (strcmp(property->property_name, "player_name") == 0) { + destValue->value_string = _strdup(platform_get_username()); + } else { // Use static default if (property->type == CONFIG_VALUE_TYPE_STRING) { diff --git a/src/platform/platform.h b/src/platform/platform.h index 24717e007b..2f69731ca2 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -157,6 +157,7 @@ void platform_resolve_user_data_path(); void platform_resolve_openrct_data_path(); void platform_get_openrct_data_path(utf8 *outPath); void platform_get_user_directory(utf8 *outPath, const utf8 *subDirectory); +utf8* platform_get_username(); void platform_show_messagebox(utf8 *message); int platform_open_common_file_dialog(int type, utf8 *title, utf8 *filename, utf8 *filterPattern, utf8 *filterName); utf8 *platform_open_directory_browser(utf8 *title); diff --git a/src/platform/posix.c b/src/platform/posix.c index 04cb3e89b4..73d1ee61e5 100644 --- a/src/platform/posix.c +++ b/src/platform/posix.c @@ -43,6 +43,7 @@ #include #include #include +#include // The name of the mutex used to prevent multiple instances of the game from running #define SINGLE_INSTANCE_MUTEX_NAME "openrct2.lock" @@ -876,4 +877,14 @@ datetime64 platform_get_datetime_now_utc() return utcNow; } +utf8* platform_get_username() { + char* username = getlogin(); + + if (username) { + return username; + } else { + return "Player"; + } +} + #endif diff --git a/src/platform/windows.c b/src/platform/windows.c index 8f0be894b3..a31dc9f0de 100644 --- a/src/platform/windows.c +++ b/src/platform/windows.c @@ -23,6 +23,7 @@ #ifdef __WINDOWS__ #include +#include #include #include #include @@ -975,4 +976,15 @@ datetime64 platform_get_datetime_now_utc() return utcNow; } +utf8* platform_get_username() { + static char username[UNLEN + 1]; + + DWORD usernameLength = UNLEN + 1; + if (!GetUserName(username, &usernameLength)) { + strcpy(username, "Player"); + } + + return username; +} + #endif From 4f0fc1065b86b4546d3f6435c36fd710bc1ad53b Mon Sep 17 00:00:00 2001 From: Alexander Overvoorde Date: Wed, 10 Feb 2016 21:58:06 +0100 Subject: [PATCH 2/5] Change to more reliable way to access username on posix --- src/platform/posix.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/platform/posix.c b/src/platform/posix.c index 73d1ee61e5..dea5a78f4c 100644 --- a/src/platform/posix.c +++ b/src/platform/posix.c @@ -43,7 +43,6 @@ #include #include #include -#include // The name of the mutex used to prevent multiple instances of the game from running #define SINGLE_INSTANCE_MUTEX_NAME "openrct2.lock" @@ -878,10 +877,10 @@ datetime64 platform_get_datetime_now_utc() } utf8* platform_get_username() { - char* username = getlogin(); + struct passwd* pw = getpwuid(getuid()); - if (username) { - return username; + if (pw) { + return pw->pw_name; } else { return "Player"; } From a5b47a8481693c944062232d48cf24d2c56aab2e Mon Sep 17 00:00:00 2001 From: Alexander Overvoorde Date: Wed, 10 Feb 2016 22:33:39 +0100 Subject: [PATCH 3/5] Change lmcons.h include capitalization (breaks mingw) --- src/platform/windows.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform/windows.c b/src/platform/windows.c index a31dc9f0de..a43920d060 100644 --- a/src/platform/windows.c +++ b/src/platform/windows.c @@ -23,7 +23,7 @@ #ifdef __WINDOWS__ #include -#include +#include #include #include #include From 65f25f8340fd67f85cb2de496b76932adcb20a94 Mon Sep 17 00:00:00 2001 From: Alexander Overvoorde Date: Wed, 10 Feb 2016 22:43:17 +0100 Subject: [PATCH 4/5] Get fallback player name from language files --- src/localisation/string_ids.h | 1 + src/platform/posix.c | 3 ++- src/platform/windows.c | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/localisation/string_ids.h b/src/localisation/string_ids.h index 893142ad15..9cce3826d0 100644 --- a/src/localisation/string_ids.h +++ b/src/localisation/string_ids.h @@ -475,6 +475,7 @@ enum { STR_WATER_RIDES_TIP = 1227, STR_SHOPS_STALLS_TIP = 1228, + STR_PLAYER_DEFAULT_NAME = 1315, STR_X_PLAYER = 1317, STR_X_PLAYERS = 1318, diff --git a/src/platform/posix.c b/src/platform/posix.c index dea5a78f4c..d683164229 100644 --- a/src/platform/posix.c +++ b/src/platform/posix.c @@ -33,6 +33,7 @@ #include "../addresses.h" #include "../config.h" #include "../localisation/language.h" +#include "../localisation/string_ids.h" #include "../openrct2.h" #include "../util/util.h" #include "platform.h" @@ -882,7 +883,7 @@ utf8* platform_get_username() { if (pw) { return pw->pw_name; } else { - return "Player"; + return language_get_string(STR_PLAYER_DEFAULT_NAME); } } diff --git a/src/platform/windows.c b/src/platform/windows.c index a43920d060..f27e2b563c 100644 --- a/src/platform/windows.c +++ b/src/platform/windows.c @@ -31,6 +31,7 @@ #include "../addresses.h" #include "../openrct2.h" #include "../localisation/language.h" +#include "../localisation/string_ids.h" #include "../util/util.h" #include "../config.h" #include "platform.h" @@ -981,7 +982,7 @@ utf8* platform_get_username() { DWORD usernameLength = UNLEN + 1; if (!GetUserName(username, &usernameLength)) { - strcpy(username, "Player"); + strcpy(username, language_get_string(STR_PLAYER_DEFAULT_NAME)); } return username; From 05c1aabc390104b7036f748dbc85f127c771245b Mon Sep 17 00:00:00 2001 From: Alexander Overvoorde Date: Fri, 12 Feb 2016 00:31:23 +0100 Subject: [PATCH 5/5] Move fallback name logic out of platform_get_username --- src/config.c | 8 +++++++- src/platform/posix.c | 3 +-- src/platform/windows.c | 3 +-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/config.c b/src/config.c index f9ae7d5ac9..992a2f29ce 100644 --- a/src/config.c +++ b/src/config.c @@ -375,7 +375,13 @@ void config_set_defaults() destValue->value_uint8 = platform_get_locale_temperature_format(); } else if (strcmp(property->property_name, "player_name") == 0) { - destValue->value_string = _strdup(platform_get_username()); + utf8* username = platform_get_username(); + + if (username) { + destValue->value_string = _strdup(username); + } else { + destValue->value_string = _strdup(language_get_string(STR_PLAYER_DEFAULT_NAME)); + } } else { // Use static default diff --git a/src/platform/posix.c b/src/platform/posix.c index d683164229..850ad1df5a 100644 --- a/src/platform/posix.c +++ b/src/platform/posix.c @@ -33,7 +33,6 @@ #include "../addresses.h" #include "../config.h" #include "../localisation/language.h" -#include "../localisation/string_ids.h" #include "../openrct2.h" #include "../util/util.h" #include "platform.h" @@ -883,7 +882,7 @@ utf8* platform_get_username() { if (pw) { return pw->pw_name; } else { - return language_get_string(STR_PLAYER_DEFAULT_NAME); + return NULL; } } diff --git a/src/platform/windows.c b/src/platform/windows.c index f27e2b563c..31c8fc7fd5 100644 --- a/src/platform/windows.c +++ b/src/platform/windows.c @@ -31,7 +31,6 @@ #include "../addresses.h" #include "../openrct2.h" #include "../localisation/language.h" -#include "../localisation/string_ids.h" #include "../util/util.h" #include "../config.h" #include "platform.h" @@ -982,7 +981,7 @@ utf8* platform_get_username() { DWORD usernameLength = UNLEN + 1; if (!GetUserName(username, &usernameLength)) { - strcpy(username, language_get_string(STR_PLAYER_DEFAULT_NAME)); + return NULL; } return username;