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.
This commit is contained in:
William Breathitt Gray 2022-09-16 07:19:44 -04:00 committed by GitHub
parent 1d04903c19
commit 19e936362a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 15 deletions

@ -98,15 +98,15 @@ Where each location is on each platform:
* Windows installed: * Windows installed:
* `bin` = `C:\Program Files\Minetest\bin (Depends on the install location)` * `bin` = `C:\Program Files\Minetest\bin (Depends on the install location)`
* `share` = `C:\Program Files\Minetest (Depends on the install location)` * `share` = `C:\Program Files\Minetest (Depends on the install location)`
* `user` = `%APPDATA%\Minetest` * `user` = `%APPDATA%\Minetest` or `%MINETEST_USER_PATH%`
* Linux installed: * Linux installed:
* `bin` = `/usr/bin` * `bin` = `/usr/bin`
* `share` = `/usr/share/minetest` * `share` = `/usr/share/minetest`
* `user` = `~/.minetest` * `user` = `~/.minetest` or `$MINETEST_USER_PATH`
* macOS: * macOS:
* `bin` = `Contents/MacOS` * `bin` = `Contents/MacOS`
* `share` = `Contents/Resources` * `share` = `Contents/Resources`
* `user` = `Contents/User OR ~/Library/Application Support/minetest` * `user` = `Contents/User` or `~/Library/Application Support/minetest` or `$MINETEST_USER_PATH`
Worlds can be found as separate folders in: `user/worlds/` Worlds can be found as separate folders in: `user/worlds/`

@ -124,6 +124,9 @@ Colon delimited list of directories to search for games.
.TP .TP
.B MINETEST_MOD_PATH .B MINETEST_MOD_PATH
Colon delimited list of directories to search for mods. Colon delimited list of directories to search for mods.
.TP
.B MINETEST_USER_PATH
Path to Minetest user data directory.
.SH BUGS .SH BUGS
Please report all bugs at https://github.com/minetest/minetest/issues. Please report all bugs at https://github.com/minetest/minetest/issues.

@ -527,7 +527,7 @@ static bool create_userdata_path()
} }
#else #else
// Create user data directory // Create user data directory
success = fs::CreateDir(porting::path_user); success = fs::CreateAllDirs(porting::path_user);
#endif #endif
return success; return success;

@ -422,11 +422,18 @@ bool setSystemPaths()
path_share += DIR_DELIM ".."; path_share += DIR_DELIM "..";
} }
// Use "C:\Users\<user>\AppData\Roaming\<PROJECT_NAME_C>" // Use %MINETEST_USER_PATH%
DWORD len = GetEnvironmentVariable("APPDATA", buf, sizeof(buf)); DWORD len = GetEnvironmentVariable("MINETEST_USER_PATH", buf, sizeof(buf));
FATAL_ERROR_IF(len == 0 || len > sizeof(buf), "Failed to get APPDATA"); 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; return true;
} }
@ -486,8 +493,13 @@ bool setSystemPaths()
} }
#ifndef __ANDROID__ #ifndef __ANDROID__
path_user = std::string(getHomeOrFail()) + DIR_DELIM "." const char *const minetest_user_path = getenv("MINETEST_USER_PATH");
+ PROJECT_NAME; 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 #endif
return true; return true;
@ -510,9 +522,14 @@ bool setSystemPaths()
} }
CFRelease(resources_url); CFRelease(resources_url);
path_user = std::string(getHomeOrFail()) const char *const minetest_user_path = getenv("MINETEST_USER_PATH");
+ "/Library/Application Support/" if (minetest_user_path && minetest_user_path[0] != '\0') {
+ PROJECT_NAME; path_user = std::string(minetest_user_path);
} else {
path_user = std::string(getHomeOrFail())
+ "/Library/Application Support/"
+ PROJECT_NAME;
}
return true; return true;
} }
@ -522,8 +539,13 @@ bool setSystemPaths()
bool setSystemPaths() bool setSystemPaths()
{ {
path_share = STATIC_SHAREDIR; path_share = STATIC_SHAREDIR;
path_user = std::string(getHomeOrFail()) + DIR_DELIM "." const char *const minetest_user_path = getenv("MINETEST_USER_PATH");
+ lowercase(PROJECT_NAME); 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; return true;
} }