From 679aae801dad8a665ce653ad3e6f6af039d8e5ff Mon Sep 17 00:00:00 2001 From: Ted John Date: Tue, 28 Nov 2017 22:46:37 +0000 Subject: [PATCH] Fix GetFolderPath for Windows --- src/openrct2/PlatformEnvironment.cpp | 1 + src/openrct2/platform/Platform2.cpp | 39 +++++++++++++++++++++------- src/openrct2/platform/platform.h | 1 + src/openrct2/util/util.c | 4 +++ 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/openrct2/PlatformEnvironment.cpp b/src/openrct2/PlatformEnvironment.cpp index 9c34a46600..57ca672e14 100644 --- a/src/openrct2/PlatformEnvironment.cpp +++ b/src/openrct2/PlatformEnvironment.cpp @@ -105,6 +105,7 @@ private: case PATHID::NETWORK_USERS: case PATHID::SCORES: case PATHID::SCORES_LEGACY: + default: return DIRBASE::USER; } } diff --git a/src/openrct2/platform/Platform2.cpp b/src/openrct2/platform/Platform2.cpp index e5ab67e23e..f7bac368a1 100644 --- a/src/openrct2/platform/Platform2.cpp +++ b/src/openrct2/platform/Platform2.cpp @@ -18,6 +18,8 @@ #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #include + #include + #undef GetEnvironmentVariable #else #include #endif @@ -25,6 +27,7 @@ #include "../core/Path.hpp" #include "../core/String.hpp" +#include "../core/Util.hpp" #include "Platform2.h" #include "platform.h" @@ -38,10 +41,22 @@ namespace Platform std::string GetEnvironmentVariable(const std::string &name) { #ifdef _WIN32 + std::wstring result; auto wname = String::ToUtf16(name); - wchar_t wvalue[MAX_PATH]; - GetEnvironmentVariableW(wname.c_str(), wvalue, sizeof(wvalue)); - return String::ToUtf8(wvalue); + wchar_t wvalue[256]; + auto valueSize = GetEnvironmentVariableW(wname.c_str(), wvalue, (DWORD)Util::CountOf(wvalue)); + if (valueSize < Util::CountOf(wvalue)) + { + result = wvalue; + } + else + { + auto wlvalue = new wchar_t[valueSize]; + GetEnvironmentVariableW(wname.c_str(), wlvalue, valueSize); + result = wlvalue; + delete wlvalue; + } + return String::ToUtf8(result); #else return String::ToStd(getenv(name.c_str())); #endif @@ -78,10 +93,12 @@ namespace Platform case SPECIAL_FOLDER::USER_CONFIG: case SPECIAL_FOLDER::USER_DATA: { - wchar_t wpath[MAX_PATH]; - if (SUCCEEDED(SHGetFolderPathW(nullptr, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, nullptr, 0, wpath))) + wchar_t * wpath = nullptr; + if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_CREATE, nullptr, &wpath))) { - return ToUtf8(std::wstring(wpath)); + auto path = String::ToUtf8(std::wstring(wpath)); + CoTaskMemFree(wpath); + return path; } else { @@ -90,10 +107,12 @@ namespace Platform } case SPECIAL_FOLDER::USER_HOME: { - wchar_t wpath[MAX_PATH]; - if (SUCCEEDED(SHGetFolderPathW(nullptr, CSIDL_PROFILE | CSIDL_FLAG_CREATE, nullptr, 0, wpath))) + wchar_t * wpath = nullptr; + if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Profile, KF_FLAG_CREATE, nullptr, &wpath))) { - return ToUtf8(std::wstring(wpath)); + auto path = String::ToUtf8(std::wstring(wpath)); + CoTaskMemFree(wpath); + return path; } else { @@ -161,6 +180,8 @@ namespace Platform case SPECIAL_FOLDER::USER_HOME: return getpwuid(getuid())->pw_dir; #endif + default: + return std::string(); } } diff --git a/src/openrct2/platform/platform.h b/src/openrct2/platform/platform.h index eb9b7a387e..78ed6fb748 100644 --- a/src/openrct2/platform/platform.h +++ b/src/openrct2/platform/platform.h @@ -153,6 +153,7 @@ void core_init(); #define WIN32_LEAN_AND_MEAN #endif #include + #undef CreateDirectory #undef CreateWindow #undef GetMessage diff --git a/src/openrct2/util/util.c b/src/openrct2/util/util.c index dadc9f3bed..273132fda6 100644 --- a/src/openrct2/util/util.c +++ b/src/openrct2/util/util.c @@ -385,6 +385,10 @@ char *safe_strcat(char *destination, const char *source, size_t size) char *safe_strcat_path(char *destination, const char *source, size_t size) { path_end_with_separator(destination, size); + if (source[0] == *PATH_SEPARATOR) + { + source = source + 1; + } return safe_strcat(destination, source, size); }