git repos / blockattack-game

commit d50cee1b

sago007 · 2019-03-02 14:50
d50cee1b6f49287374d2e98615caadc35298a7ae patch · browse files
parent b372dcb1f3cadefbedd74cd39fe4ad7d6744aa0e

Use the username on Windows

Changed files

M source/code/os.cpp before
M source/code/sago/platform_folders.cpp before
M source/code/sago/platform_folders.h before
diff --git a/source/code/os.cpp b/source/code/os.cpp index 848608e..392cc38 100644 --- a/source/code/os.cpp +++ b/source/code/os.cpp
@@ -26,6 +26,7 @@ https://blockattack.net
#include <physfs.h>
#include "sago/platform_folders.h"
#include "version.h"
+#include "common.h"
#if defined(__unix__)
#include <pwd.h>
@@ -55,7 +56,15 @@ void setPathToSaveFiles(const std::string& path) {
std::string defaultPlayerName() {
std::string ret;
- #if defined(__unix__)
+#if defined(_WIN32)
+ wchar_t win_buffer[50];
+ DWORD win_buffer_size = sizeof(win_buffer);
+ bool success = GetUserNameW(win_buffer, &win_buffer_size);
+ if (success) {
+ ret = sago::internal::win32_utf16_to_utf8(win_buffer);
+ }
+#endif
+#if defined(__unix__)
int uid = getuid();
struct passwd* pw = getpwuid(uid);
if (pw && pw->pw_gecos) {
@@ -65,7 +74,10 @@ std::string defaultPlayerName() {
if (pw && pw->pw_name && ret.empty()) {
ret = pw->pw_name;
}
- #endif
+#endif
+ if (ret.empty()) {
+ ret = _("Player 1");
+ }
return ret;
}
diff --git a/source/code/sago/platform_folders.cpp b/source/code/sago/platform_folders.cpp index 1c9b99c..c5204cd 100644 --- a/source/code/sago/platform_folders.cpp +++ b/source/code/sago/platform_folders.cpp
@@ -80,7 +80,10 @@ static std::string getHome() {
// For SHGetFolderPathW and various CSIDL "magic numbers"
#include <shlobj.h>
-static std::string win32_utf16_to_utf8(const wchar_t* wstr) {
+namespace sago {
+namespace internal {
+
+std::string win32_utf16_to_utf8(const wchar_t* wstr) {
std::string res;
// If the 6th parameter is 0 then WideCharToMultiByte returns the number of bytes needed to store the result.
int actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
@@ -98,6 +101,9 @@ static std::string win32_utf16_to_utf8(const wchar_t* wstr) {
return res;
}
+} // namesapce internal
+} // namespace sago
+
class FreeCoTaskMemory {
LPWSTR pointer = NULL;
public:
@@ -116,7 +122,7 @@ static std::string GetKnownWindowsFolder(REFKNOWNFOLDERID folderId, const char*
if (!SUCCEEDED(hr)) {
throw std::runtime_error(errorMsg);
}
- return win32_utf16_to_utf8(wszPath);
+ return sago::internal::win32_utf16_to_utf8(wszPath);
}
static std::string GetAppData() {
diff --git a/source/code/sago/platform_folders.h b/source/code/sago/platform_folders.h index fc9dd3b..646b794 100644 --- a/source/code/sago/platform_folders.h +++ b/source/code/sago/platform_folders.h
@@ -42,7 +42,11 @@ namespace internal {
#if !defined(_WIN32) && !defined(__APPLE__)
void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector<std::string>& folders);
#endif
+ #ifdef _WIN32
+ std::string win32_utf16_to_utf8(const wchar_t* wstr);
+ #endif
}
+
#endif //DOXYGEN_SHOULD_SKIP_THIS
/**