git repos / blockattack-game

commit 88b5bd84

sago007 · 2019-03-09 14:18
88b5bd846f1d564be76f241b654f4575b5153401 patch · browse files
parent 0e662388489037019d86438f4d8d34ec3ba1b46e

Create the directory on windows even if they contain Unicode

Changed files

M source/code/os.cpp before
diff --git a/source/code/os.cpp b/source/code/os.cpp index 392cc38..57ec3d1 100644 --- a/source/code/os.cpp +++ b/source/code/os.cpp
@@ -54,6 +54,26 @@ void setPathToSaveFiles(const std::string& path) {
overrideSavePath = path;
}
+#if defined(_WIN32)
+static std::wstring win32_utf16_to_utf8(const char* str) {
+ std::wstring res;
+ // If the 6th parameter is 0 then WideCharToMultiByte returns the number of bytes needed to store the result.
+ int actualSize = MultiByteToWideChar(CP_UTF8, 0, str, -1, nullptr, 0);
+ if (actualSize > 0) {
+ //If the converted UTF-8 string could not be in the initial buffer. Allocate one that can hold it.
+ std::vector<wchar_t> buffer(actualSize);
+ actualSize = MultiByteToWideChar(CP_UTF8, 0, str, -1, &buffer[0], buffer.size());
+ res = buffer.data();
+ }
+ if (actualSize == 0) {
+ // WideCharToMultiByte return 0 for errors.
+ const std::string errorMsg = "UTF8 to UTF16 failed with error code: " + GetLastError();
+ throw std::runtime_error(errorMsg.c_str());
+ }
+ return res;
+}
+#endif
+
std::string defaultPlayerName() {
std::string ret;
#if defined(_WIN32)
@@ -90,8 +110,8 @@ void OsCreateSaveFolder() {
}
#elif defined(_WIN32)
//Now for Windows NT/2k/xp/2k3 etc.
- CreateDirectory(pf.getSaveGamesFolder1().c_str(), nullptr);
+ CreateDirectoryW(win32_utf16_to_utf8(pf.getSaveGamesFolder1().c_str()).c_str(), nullptr);
std::string tempA = getPathToSaveFiles();
- CreateDirectory(tempA.c_str(),nullptr);
+ CreateDirectoryW(win32_utf16_to_utf8(tempA.c_str()).c_str(), nullptr);
#endif
}