| 1 | /* SPDX-License-Identifier: MIT License |
| 2 | |
| 3 | Copyright © 2021 Thomas “illwieckz” Debesse |
| 4 | |
| 5 | Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | copy of this software and associated documentation files (the “Software”), |
| 7 | to deal in the Software without restriction, including without limitation |
| 8 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | and/or sell copies of the Software, and to permit persons to whom the |
| 10 | Software is furnished to do so, subject to the following conditions: |
| 11 | |
| 12 | The above copyright notice and this permission notice shall be included |
| 13 | in all copies or substantial portions of the Software. |
| 14 | |
| 15 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | DEALINGS IN THE SOFTWARE. */ |
| 22 | |
| 23 | /* transformpath: transform file path based on keywords. |
| 24 | |
| 25 | This is not an environment variable parser, this only supports |
| 26 | a set of keywords using common environment name syntax when it |
| 27 | exists to make the strings easier to read. |
| 28 | |
| 29 | Supported substitution keywords, |
| 30 | Windows: |
| 31 | |
| 32 | - %HOMEPATH% |
| 33 | - %USERPROFILE% |
| 34 | - %ProgramFiles% |
| 35 | - %ProgramFiles(x86)% |
| 36 | - %ProgramW6432% |
| 37 | - %APPDATA% |
| 38 | - [CSIDL_MYDOCUMENTS] |
| 39 | |
| 40 | Supported substitution keywords, |
| 41 | Linux, FreeBSD, macOS: |
| 42 | |
| 43 | - ~ |
| 44 | - ${HOME} |
| 45 | |
| 46 | Supported substitution keywords, |
| 47 | Linux, FreeBSD, other XDG systems: |
| 48 | |
| 49 | - ${XDG_CONFIG_HOME} |
| 50 | - ${XDG_DATA_HOME} |
| 51 | |
| 52 | Examples, |
| 53 | game engine directories: |
| 54 | |
| 55 | - Windows: %ProgramFiles%\Unvanquished |
| 56 | - Linux: ${XDG_DATA_HOME}/unvanquished/base |
| 57 | - macOS: ${HOME}/Games/Unvanquished |
| 58 | |
| 59 | Examples, |
| 60 | game home directories: |
| 61 | |
| 62 | - Windows: [CSIDL_MYDOCUMENTS]\My Games\Unvanquished |
| 63 | - Linux: ${XDG_DATA_HOME}/unvanquished |
| 64 | - macOS: ${HOME}/Application Data/Unvanquished |
| 65 | |
| 66 | */ |
| 67 | |
| 68 | #include "globaldefs.h" |
| 69 | #include "stringio.h" |
| 70 | |
| 71 | #include <cstring> |
| 72 | #include <string> |
| 73 | |
| 74 | #if GDEF_OS_WINDOWS |
| 75 | #include <windows.h> |
| 76 | #include <iostream> |
| 77 | #include <shlobj.h> |
| 78 | #pragma comment(lib, "shell32.lib") |
| 79 | #endif // !GDEF_OS_WINDOWS |
| 80 | |
| 81 | #if GDEF_OS_LINUX || GDEF_OS_BSD || GDEF_OS_MACOS |
| 82 | #include <unistd.h> |
| 83 | #include <sys/types.h> |
| 84 | #include <pwd.h> |
| 85 | #endif // !GDEF_OS_LINUX && !GDEF_OS_BSD && !GDEF_OS_MACOS |
| 86 | |
| 87 | static std::string getEnvVar( const char* name ) |
| 88 | { |
| 89 | const char *env = getenv( name ); |
| 90 | env = env ? env : ""; |
| 91 | std::string path( env ); |
| 92 | return path; |
| 93 | } |
| 94 | |
| 95 | #if GDEF_OS_WINDOWS |
| 96 | static std::string getUserProfilePath(); |
| 97 | #endif // !GDEF_OS_WINDOWS |
| 98 | |
| 99 | static std::string getUserName() |
| 100 | { |
| 101 | #if GDEF_OS_WINDOWS |
| 102 | std::string path = getEnvVar( "USERNAME" ); |
| 103 | |
| 104 | if ( ! path.empty() ) |
| 105 | { |
| 106 | return path; |
| 107 | } |
| 108 | |
| 109 | globalErrorStream() << "%USERNAME% not found.\n"; |
| 110 | |
| 111 | return ""; |
| 112 | #endif // !GDEF_OS_WINDOWS |
| 113 | |
| 114 | #if GDEF_OS_LINUX || GDEF_OS_BSD || GDEF_OS_MACOS |
| 115 | std::string path = getEnvVar( "USERNAME" ); |
| 116 | |
| 117 | if ( ! path.empty() ) |
| 118 | { |
| 119 | return path; |
| 120 | } |
| 121 | |
| 122 | globalErrorStream() << "${USERNAME} not found, guessing…\n"; |
| 123 | |
| 124 | path = getEnvVar( "LOGNAME" ); |
| 125 | |
| 126 | if ( ! path.empty() ) |
| 127 | { |
| 128 | return path; |
| 129 | } |
| 130 | |
| 131 | globalErrorStream() << "${LOGNAME} not found, guessing…\n"; |
| 132 | |
| 133 | path = getEnvVar( "USER" ); |
| 134 | |
| 135 | if ( ! path.empty() ) |
| 136 | { |
| 137 | return path; |
| 138 | } |
| 139 | |
| 140 | globalErrorStream() << "${USER} not found.\n"; |
| 141 | |
| 142 | return ""; |
| 143 | #endif // !GDEF_OS_LINUX && !GDEF_OS_BSD && !GDEF_OS_MACOS |
| 144 | } |
| 145 | |
| 146 | static std::string getHomePath() |
| 147 | { |
| 148 | #if GDEF_OS_WINDOWS |
| 149 | std::string path = getEnvVar( "HOMEPATH" ); |
| 150 | |
| 151 | if ( ! path.empty() ) |
| 152 | { |
| 153 | return path; |
| 154 | } |
| 155 | |
| 156 | globalErrorStream() << "%HOMEPATH% not found, guessing…\n"; |
| 157 | |
| 158 | std::string path1 = getUserProfilePath(); |
| 159 | |
| 160 | if ( ! path1.empty() ) |
| 161 | { |
| 162 | return path1; |
| 163 | } |
| 164 | |
| 165 | globalErrorStream() << "%HOMEPATH% not found.\n"; |
| 166 | |
| 167 | return ""; |
| 168 | #endif // !GDEF_OS_WINDOWS |
| 169 | |
| 170 | #if GDEF_OS_LINUX || GDEF_OS_BSD || GDEF_OS_MACOS |
| 171 | // Get the path environment variable. |
| 172 | std::string path = getEnvVar( "HOME" ); |
| 173 | |
| 174 | // Look up path directory in password database. |
| 175 | if( ! path.empty() ) |
| 176 | { |
| 177 | return path; |
| 178 | } |
| 179 | |
| 180 | globalErrorStream() << "${HOME} not found, guessing…\n"; |
| 181 | |
| 182 | static char buf[ 4096 ]; |
| 183 | struct passwd pw, *pwp; |
| 184 | |
| 185 | if ( getpwuid_r( getuid(), &pw, buf, sizeof( buf ), &pwp ) == 0 ) |
| 186 | { |
| 187 | return std::string( pw.pw_dir ); |
| 188 | } |
| 189 | |
| 190 | globalErrorStream() << "${HOME} not found, guessing…\n"; |
| 191 | |
| 192 | std::string path1( "/home/" ); |
| 193 | |
| 194 | std::string path2 = getUserName(); |
| 195 | |
| 196 | if ( ! path2.empty() ) |
| 197 | { |
| 198 | return path1 + path2; |
| 199 | } |
| 200 | |
| 201 | globalErrorStream() << "${HOME} not found…\n"; |
| 202 | |
| 203 | return ""; |
| 204 | #endif // !GDEF_OS_LINUX && !GDEF_OS_BSD && !GDEF_OS_MACOS |
| 205 | } |
| 206 | |
| 207 | #if GDEF_OS_WINDOWS |
| 208 | static std::string getSystemDrive() |
| 209 | { |
| 210 | std::string path = getEnvVar( "SYSTEMDRIVE" ); |
| 211 | |
| 212 | if ( ! path.empty() ) |
| 213 | { |
| 214 | return path; |
| 215 | } |
| 216 | |
| 217 | globalErrorStream() << "%SYSTEMDRIVE% not found, guessing…\n"; |
| 218 | |
| 219 | return "C:"; |
| 220 | } |
| 221 | |
| 222 | static std::string getUserProfilePath() |
| 223 | { |
| 224 | std::string path = getEnvVar( "USERPROFILE" ); |
| 225 | |
| 226 | if ( ! path.empty() ) |
| 227 | { |
| 228 | return path; |
| 229 | } |
| 230 | |
| 231 | globalErrorStream() << "%USERPROFILE% not found, guessing…\n"; |
| 232 | |
| 233 | std::string path1 = getSystemDrive(); |
| 234 | std::string path2 = getUserName(); |
| 235 | |
| 236 | if ( ! path2.empty() ) |
| 237 | { |
| 238 | return path1 + "\\Users\\" + path2; |
| 239 | } |
| 240 | |
| 241 | globalErrorStream() << "%USERPROFILE% not found.\n"; |
| 242 | |
| 243 | return ""; |
| 244 | } |
| 245 | |
| 246 | static std::string getProgramFilesPath() |
| 247 | { |
| 248 | std::string path = getEnvVar( "ProgramFiles" ); |
| 249 | |
| 250 | if ( ! path.empty() ) |
| 251 | { |
| 252 | return path; |
| 253 | } |
| 254 | |
| 255 | globalErrorStream() << "%ProgramFiles% not found, guessing…\n"; |
| 256 | |
| 257 | std::string path1 = getSystemDrive(); |
| 258 | return path1 + "\\Program Files"; |
| 259 | } |
| 260 | |
| 261 | static std::string getProgramFilesX86Path() |
| 262 | { |
| 263 | std::string path = getEnvVar( "ProgramFiles(x86)" ); |
| 264 | |
| 265 | if ( ! path.empty() ) |
| 266 | { |
| 267 | return path; |
| 268 | } |
| 269 | |
| 270 | globalErrorStream() << "%ProgramFiles(x86)% not found, guessing…\n"; |
| 271 | |
| 272 | return getProgramFilesPath(); |
| 273 | } |
| 274 | |
| 275 | static std::string getProgramW6432Path() |
| 276 | { |
| 277 | std::string path = getEnvVar( "ProgramFilesW6432" ); |
| 278 | |
| 279 | if ( ! path.empty() ) |
| 280 | { |
| 281 | return path; |
| 282 | } |
| 283 | |
| 284 | globalErrorStream() << "%ProgramW6432% not found, guessing…\n"; |
| 285 | |
| 286 | return getProgramFilesPath(); |
| 287 | } |
| 288 | |
| 289 | static std::string getAppDataPath() |
| 290 | { |
| 291 | std::string path = getEnvVar( "APPDATA" ); |
| 292 | |
| 293 | if ( ! path.empty() ) |
| 294 | { |
| 295 | return path; |
| 296 | } |
| 297 | |
| 298 | globalErrorStream() << "%APPDATA% not found, guessing…\n"; |
| 299 | |
| 300 | std::string path1 = getUserProfilePath(); |
| 301 | |
| 302 | if ( ! path1.empty() ) |
| 303 | { |
| 304 | return path1 + "\\AppData\\Roaming"; |
| 305 | } |
| 306 | |
| 307 | globalErrorStream() << "%APPDATA% not found.\n"; |
| 308 | |
| 309 | return std::string( "" ); |
| 310 | } |
| 311 | |
| 312 | /* TODO: see also qFOLDERID_SavedGames in mainframe.cpp, |
| 313 | HomePaths_Realise and other things like that, |
| 314 | they look to be game paths, not NetRadiant paths. */ |
| 315 | |
| 316 | static std::string getMyDocumentsPath() |
| 317 | { |
| 318 | CHAR path[ MAX_PATH ]; |
| 319 | HRESULT result = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, path); |
| 320 | |
| 321 | if ( result == S_OK ) |
| 322 | { |
| 323 | return std::string( path ); |
| 324 | } |
| 325 | |
| 326 | globalErrorStream() << "[CSIDL_MYDOCUMENTS] not found, guessing…\n"; |
| 327 | |
| 328 | std::string path1 = getHomePath(); |
| 329 | |
| 330 | if ( ! path1.empty() ) |
| 331 | { |
| 332 | path1 += "\\Documents"; |
| 333 | |
| 334 | return path1; |
| 335 | } |
| 336 | |
| 337 | globalErrorStream() << "[CSIDL_MYDOCUMENTS] not found.\n"; |
| 338 | |
| 339 | return ""; |
| 340 | } |
| 341 | #endif // !GDEF_OS_WINDOWS |
| 342 | |
| 343 | #if GDEF_OS_XDG |
| 344 | static std::string getXdgConfigHomePath() |
| 345 | { |
| 346 | /* FIXME: we may want to rely on g_get_user_config_dir() |
| 347 | provided by GLib. */ |
| 348 | |
| 349 | std::string path = getEnvVar( "XDG_CONFIG_HOME" ); |
| 350 | |
| 351 | if ( ! path.empty() ) |
| 352 | { |
| 353 | return path; |
| 354 | } |
| 355 | |
| 356 | // This is not an error. |
| 357 | // globalErrorStream() << "${XDG_CONFIG_HOME} not found, guessing…\n"; |
| 358 | |
| 359 | std::string path1 = getHomePath(); |
| 360 | |
| 361 | if ( ! path1.empty() ) |
| 362 | { |
| 363 | return path1 + "/.config"; |
| 364 | } |
| 365 | |
| 366 | globalErrorStream() << "${XDG_CONFIG_HOME} not found.\n"; |
| 367 | |
| 368 | return ""; |
| 369 | } |
| 370 | |
| 371 | static std::string getXdgDataHomePath() |
| 372 | { |
| 373 | std::string path = getEnvVar( "XDG_DATA_HOME" ); |
| 374 | |
| 375 | if ( ! path.empty() ) |
| 376 | { |
| 377 | return path; |
| 378 | } |
| 379 | |
| 380 | // This is not an error. |
| 381 | // globalErrorStream() << "${XDG_DATA_HOME} not found, guessing…\n"; |
| 382 | |
| 383 | std::string path1 = getHomePath(); |
| 384 | |
| 385 | if ( ! path1.empty() ) |
| 386 | { |
| 387 | return path1 + "/.local/share"; |
| 388 | } |
| 389 | |
| 390 | globalErrorStream() << "${XDG_DATA_HOME} not found.\n"; |
| 391 | |
| 392 | return ""; |
| 393 | } |
| 394 | #endif // GDEF_OS_XDG |
| 395 | |
| 396 | struct pathTransformer_t |
| 397 | { |
| 398 | std::string pattern; |
| 399 | std::string ( *function )(); |
| 400 | }; |
| 401 | |
| 402 | static const pathTransformer_t pathTransformers[] = |
| 403 | { |
| 404 | #if GDEF_OS_WINDOWS |
| 405 | { "%HOMEPATH%", getHomePath }, |
| 406 | { "%USERPROFILE%", getUserProfilePath }, |
| 407 | { "%ProgramFiles%", getProgramFilesPath }, |
| 408 | { "%ProgramFiles(x86)%", getProgramFilesX86Path }, |
| 409 | { "%ProgramW6432%", getProgramW6432Path }, |
| 410 | { "%APPDATA%", getAppDataPath }, |
| 411 | { "[CSIDL_MYDOCUMENTS]", getMyDocumentsPath }, |
| 412 | #endif // GDEF_OS_WINDOWS |
| 413 | |
| 414 | #if GDEF_OS_LINUX || GDEF_OS_BSD || GDEF_OS_MACOS |
| 415 | { "~", getHomePath }, |
| 416 | { "${HOME}", getHomePath }, |
| 417 | #endif // GDEF_OS_LINUX || GDEF_OS_BSD || GDEF_OS_MACOS |
| 418 | |
| 419 | #if GDEF_OS_XDG |
| 420 | { "${XDG_CONFIG_HOME}", getXdgConfigHomePath }, |
| 421 | { "${XDG_DATA_HOME}", getXdgDataHomePath }, |
| 422 | #endif // GDEF_OS_XDG |
| 423 | }; |
| 424 | |
| 425 | /* If no transformation succeeds, the path will be returned untransformed. */ |
| 426 | std::string transformPath( std::string transformedPath ) |
| 427 | { |
| 428 | for ( const pathTransformer_t &pathTransformer : pathTransformers ) |
| 429 | { |
| 430 | if ( transformedPath.find( pathTransformer.pattern, 0 ) == 0 ) |
| 431 | { |
| 432 | globalOutputStream() << "Path Transforming: '" << transformedPath.c_str() << "'\n"; |
| 433 | |
| 434 | std::string path = pathTransformer.function(); |
| 435 | |
| 436 | if ( ! path.empty() ) |
| 437 | { |
| 438 | transformedPath.replace( 0, pathTransformer.pattern.length(), path ); |
| 439 | |
| 440 | globalOutputStream() << "Path Transformed: '" << transformedPath.c_str() << "'\n"; |
| 441 | |
| 442 | return transformedPath; |
| 443 | } |
| 444 | |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | globalErrorStream() << "Path not transformed: '" << transformedPath.c_str() << "'\n"; |
| 450 | |
| 451 | return transformedPath; |
| 452 | } |