diff options
author | William Breathitt Gray <vilhelm.gray@gmail.com> | 2022-09-16 07:19:44 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-16 13:19:44 +0200 |
commit | 19e936362aab3806a9554ab811be0562dcb41509 (patch) | |
tree | a486e08bae52c7032425bf051ec069f23a8d1696 /src/porting.cpp | |
parent | 1d04903c1973591e795d3275b900d76d7cb7877a (diff) | |
download | minetest-19e936362aab3806a9554ab811be0562dcb41509.tar.xz |
Add support for MINETEST_USERDATA environment variable (#12639)
The MINETEST_USER_PATH environment variable can be used to define a
custom path for Minetest user data. If MINETEST_USER_PATH is empty or
unset, the HOME (or APPDATA on Windows) environment variable is used as
the default user data path; this ensures backwards compatibility with
existing user setups.
Diffstat (limited to 'src/porting.cpp')
-rw-r--r-- | src/porting.cpp | 44 |
1 files changed, 33 insertions, 11 deletions
diff --git a/src/porting.cpp b/src/porting.cpp index 09627431c..f8bd74f9a 100644 --- a/src/porting.cpp +++ b/src/porting.cpp @@ -422,11 +422,18 @@ bool setSystemPaths() path_share += DIR_DELIM ".."; } - // Use "C:\Users\<user>\AppData\Roaming\<PROJECT_NAME_C>" - DWORD len = GetEnvironmentVariable("APPDATA", buf, sizeof(buf)); - FATAL_ERROR_IF(len == 0 || len > sizeof(buf), "Failed to get APPDATA"); + // Use %MINETEST_USER_PATH% + DWORD len = GetEnvironmentVariable("MINETEST_USER_PATH", buf, sizeof(buf)); + FATAL_ERROR_IF(len > sizeof(buf), "Failed to get MINETEST_USER_PATH (too large for buffer)"); + if (len == 0) { + // Use "C:\Users\<user>\AppData\Roaming\<PROJECT_NAME_C>" + len = GetEnvironmentVariable("APPDATA", buf, sizeof(buf)); + FATAL_ERROR_IF(len == 0 || len > sizeof(buf), "Failed to get APPDATA"); + path_user = std::string(buf) + DIR_DELIM + PROJECT_NAME_C; + } else { + path_user = std::string(buf); + } - path_user = std::string(buf) + DIR_DELIM + PROJECT_NAME_C; return true; } @@ -486,8 +493,13 @@ bool setSystemPaths() } #ifndef __ANDROID__ - path_user = std::string(getHomeOrFail()) + DIR_DELIM "." - + PROJECT_NAME; + const char *const minetest_user_path = getenv("MINETEST_USER_PATH"); + if (minetest_user_path && minetest_user_path[0] != '\0') { + path_user = std::string(minetest_user_path); + } else { + path_user = std::string(getHomeOrFail()) + DIR_DELIM "." + + PROJECT_NAME; + } #endif return true; @@ -510,9 +522,14 @@ bool setSystemPaths() } CFRelease(resources_url); - path_user = std::string(getHomeOrFail()) - + "/Library/Application Support/" - + PROJECT_NAME; + const char *const minetest_user_path = getenv("MINETEST_USER_PATH"); + if (minetest_user_path && minetest_user_path[0] != '\0') { + path_user = std::string(minetest_user_path); + } else { + path_user = std::string(getHomeOrFail()) + + "/Library/Application Support/" + + PROJECT_NAME; + } return true; } @@ -522,8 +539,13 @@ bool setSystemPaths() bool setSystemPaths() { path_share = STATIC_SHAREDIR; - path_user = std::string(getHomeOrFail()) + DIR_DELIM "." - + lowercase(PROJECT_NAME); + const char *const minetest_user_path = getenv("MINETEST_USER_PATH"); + if (minetest_user_path && minetest_user_path[0] != '\0') { + path_user = std::string(minetest_user_path); + } else { + path_user = std::string(getHomeOrFail()) + DIR_DELIM "." + + lowercase(PROJECT_NAME); + } return true; } |