| 1 | /* |
| 2 | =========================================================================== |
| 3 | blockattack - Block Attack - Rise of the Blocks |
| 4 | Copyright (C) 2005-2015 Poul Sander |
| 5 | |
| 6 | This program is free software: you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation, either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see http://www.gnu.org/licenses/ |
| 18 | |
| 19 | Source information and contacts persons can be found at |
| 20 | https://blockattack.net |
| 21 | =========================================================================== |
| 22 | */ |
| 23 | |
| 24 | #include "os.hpp" |
| 25 | #include <iostream> |
| 26 | #include <physfs.h> |
| 27 | #include "platform_folders.h" |
| 28 | #include "version.h" |
| 29 | #include "common.h" |
| 30 | |
| 31 | #if defined(__unix__) |
| 32 | #include <pwd.h> |
| 33 | #include <unistd.h> |
| 34 | #endif |
| 35 | #include <boost/algorithm/string.hpp> |
| 36 | #include <dirent.h> |
| 37 | |
| 38 | static sago::PlatformFolders pf; |
| 39 | |
| 40 | static std::string overrideSavePath = ""; |
| 41 | |
| 42 | /** |
| 43 | * Returns the path to where all settings must be saved. |
| 44 | * On unix-like systems this is the home-folder under: ~/.local/share/GAMENAME |
| 45 | * In Windows it is My Documents/My Games |
| 46 | * Consider changing this for Vista that has a special save games folder |
| 47 | */ |
| 48 | std::string getPathToSaveFiles() { |
| 49 | if (overrideSavePath.length() > 0) { |
| 50 | return overrideSavePath; |
| 51 | } |
| 52 | return pf.getSaveGamesFolder1()+"/"+GAMENAME; |
| 53 | } |
| 54 | |
| 55 | std::string getPathToStateFiles() { |
| 56 | return getPathToSaveFiles()+"/state"; |
| 57 | } |
| 58 | |
| 59 | void setPathToSaveFiles(const std::string& path) { |
| 60 | overrideSavePath = path; |
| 61 | } |
| 62 | |
| 63 | #if defined(_WIN32) |
| 64 | #include "shlwapi.h" |
| 65 | |
| 66 | static std::wstring win32_utf8_to_utf16(const char* str) { |
| 67 | std::wstring res; |
| 68 | // If the 6th parameter is 0 then WideCharToMultiByte returns the number of bytes needed to store the result. |
| 69 | int actualSize = MultiByteToWideChar(CP_UTF8, 0, str, -1, nullptr, 0); |
| 70 | if (actualSize > 0) { |
| 71 | //If the converted UTF-8 string could not be in the initial buffer. Allocate one that can hold it. |
| 72 | std::vector<wchar_t> buffer(actualSize); |
| 73 | actualSize = MultiByteToWideChar(CP_UTF8, 0, str, -1, &buffer[0], buffer.size()); |
| 74 | res = buffer.data(); |
| 75 | } |
| 76 | if (actualSize == 0) { |
| 77 | // WideCharToMultiByte return 0 for errors. |
| 78 | const std::string errorMsg = "UTF8 to UTF16 failed with error code: " + GetLastError(); |
| 79 | throw std::runtime_error(errorMsg.c_str()); |
| 80 | } |
| 81 | return res; |
| 82 | } |
| 83 | #endif |
| 84 | |
| 85 | std::string defaultPlayerName() { |
| 86 | std::string ret; |
| 87 | #if defined(_WIN32) |
| 88 | wchar_t win_buffer[50]; |
| 89 | DWORD win_buffer_size = sizeof(win_buffer); |
| 90 | bool success = GetUserNameW(win_buffer, &win_buffer_size); |
| 91 | if (success) { |
| 92 | ret = sago::internal::win32_utf16_to_utf8(win_buffer); |
| 93 | } |
| 94 | #endif |
| 95 | #if defined(__unix__) |
| 96 | int uid = getuid(); |
| 97 | struct passwd* pw = getpwuid(uid); |
| 98 | if (pw && pw->pw_gecos) { |
| 99 | ret = pw->pw_gecos; |
| 100 | ret = ret.substr(0, ret.find_first_of(',',0)); |
| 101 | } |
| 102 | if (pw && pw->pw_name && ret.empty()) { |
| 103 | ret = pw->pw_name; |
| 104 | } |
| 105 | #endif |
| 106 | if (ret.empty()) { |
| 107 | ret = _("Player 1"); |
| 108 | } |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | bool OsPathIsRelative(const std::string& path) { |
| 113 | #if defined(_WIN32) |
| 114 | return PathIsRelativeW(win32_utf8_to_utf16(path.c_str()).c_str()); |
| 115 | #else |
| 116 | return path[0] != '/'; |
| 117 | #endif |
| 118 | } |
| 119 | |
| 120 | void OsCreateFolder(const std::string& path) { |
| 121 | //Once all supported systems works with C++17 then we can use "std::filesystem::create_directories" instead |
| 122 | #if defined(_WIN32) |
| 123 | //Now for Windows Vista+ |
| 124 | std::vector<std::string> element_vector; |
| 125 | std::string new_path = path; |
| 126 | boost::replace_all(new_path, "/", "\\"); |
| 127 | size_t pos = new_path.find("\\"); |
| 128 | while ( pos != std::string::npos) { |
| 129 | std::string part_of_path = new_path.substr(0, pos); |
| 130 | CreateDirectoryW(win32_utf8_to_utf16(part_of_path.c_str()).c_str(), NULL); |
| 131 | pos = new_path.find("\\",pos+1); |
| 132 | } |
| 133 | CreateDirectoryW(win32_utf8_to_utf16(path.c_str()).c_str(), NULL); |
| 134 | #else |
| 135 | std::string cmd = "mkdir -p '"+path+"/'"; |
| 136 | int retcode = system(cmd.c_str()); |
| 137 | if (retcode != 0) { |
| 138 | std::cerr << "Failed to create: " << path+"/" << "\n"; |
| 139 | } |
| 140 | #endif |
| 141 | } |
| 142 | |
| 143 | void OsCreateSaveFolder() { |
| 144 | std::string path = getPathToSaveFiles(); |
| 145 | OsCreateFolder(path+"/mods"); |
| 146 | } |
| 147 | |
| 148 | std::vector<std::string> OsGetDirFileList(const std::string& path) { |
| 149 | //At the moment dirent.h is used for all OS. Should have a Unicode version on Windows. |
| 150 | std::vector<std::string> ret; |
| 151 | DIR* dir = opendir(path.c_str()); |
| 152 | if (dir) { |
| 153 | struct dirent* ent; |
| 154 | while ((ent = readdir (dir)) ) { |
| 155 | if (ent->d_name[0] == '.') { |
| 156 | continue; |
| 157 | } |
| 158 | ret.push_back(ent->d_name); |
| 159 | } |
| 160 | closedir (dir); |
| 161 | } |
| 162 | else { |
| 163 | std::cerr << "Failed to list: " << path+"/" << "\n"; |
| 164 | } |
| 165 | return ret; |
| 166 | } |