Skip to content

Commit 7aeedef

Browse files
authored
Add support for LUANTI_GAME_PATH and LUANTI_WORLD_PATH (#16816)
1 parent 3b67e73 commit 7aeedef

File tree

6 files changed

+105
-39
lines changed

6 files changed

+105
-39
lines changed

doc/luanti.6

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,17 @@ Display an interactive terminal over ncurses during execution.
114114

115115
.SH ENVIRONMENT VARIABLES
116116
.TP
117-
.B MINETEST_GAME_PATH
118-
Colon delimited list of directories to search for games
117+
.B LUANTI_GAME_PATH
118+
Colon delimited list of directories to search for games. This should
119+
be used instead of \fBMINETEST_GAME_PATH\fP, which is deprecated.
120+
119121
.TP
120-
.B MINETEST_MOD_PATH
121-
Colon delimited list of directories to search for mods
122+
.B LUANTI_MOD_PATH
123+
Colon delimited list of directories to search for mods. This should be
124+
used instead of \fBMINETEST_MOD_PATH\fP, which is deprecated.
125+
122126
.TP
123-
.B MINETEST_USER_PATH
127+
.B LUANTI_USER_PATH
124128
Path to Luanti user data directory (only on RUN_IN_PLACE=0 build)
125129
.TP
126130
.B LOG_TIMESTAMP

doc/world_format.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ For `load_mod_<mod>`, the possible values are:
163163
* Must be one of the following:
164164
* `mods/`: mods in the user path's mods folder (ex. `/home/user/.minetest/mods`)
165165
* `share/`: mods in the share's mods folder (ex. `/usr/share/minetest/mods`)
166-
* `/path/to/env`: you can use absolute paths to mods inside folders specified with the `MINETEST_MOD_PATH` `env` variable.
166+
* `/path/to/env`: you can use absolute paths to mods inside folders specified with the `LUANTI_MOD_PATH` `env` variable.
167167
* Other locations and absolute paths are not supported.
168168
* Note that `moddir` is the directory name, not the mod name specified in mod.conf.
169169

src/content/subgames.cpp

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,26 @@ struct GameFindPath
6363
std::string getSubgamePathEnv()
6464
{
6565
static bool has_warned = false;
66-
char *subgame_path = getenv("MINETEST_SUBGAME_PATH");
67-
if (subgame_path && !has_warned) {
68-
warningstream << "MINETEST_SUBGAME_PATH is deprecated, use MINETEST_GAME_PATH instead."
69-
<< std::endl;
70-
has_warned = true;
71-
}
7266

73-
char *game_path = getenv("MINETEST_GAME_PATH");
67+
if (const char *path = getenv("LUANTI_GAME_PATH"))
68+
return std::string(path);
7469

75-
if (game_path)
76-
return std::string(game_path);
77-
else if (subgame_path)
78-
return std::string(subgame_path);
70+
if (const char *path = getenv("MINETEST_GAME_PATH")) {
71+
if (!has_warned) {
72+
warningstream << "MINETEST_GAME_PATH is deprecated, use LUANTI_GAME_PATH instead."
73+
<< std::endl;
74+
has_warned = true;
75+
}
76+
return std::string(path);
77+
}
78+
if (const char *path = getenv("MINETEST_SUBGAME_PATH")) {
79+
if (!has_warned) {
80+
warningstream << "MINETEST_SUBGAME_PATH is deprecated, use LUANTI_GAME_PATH instead."
81+
<< std::endl;
82+
has_warned = true;
83+
}
84+
return std::string(path);
85+
}
7986
return "";
8087
}
8188

@@ -279,8 +286,20 @@ std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
279286

280287
std::string getWorldPathEnv()
281288
{
282-
char *world_path = getenv("MINETEST_WORLD_PATH");
283-
return world_path ? std::string(world_path) : "";
289+
static bool has_warned = false;
290+
291+
if (const char *path = getenv("LUANTI_WORLD_PATH"))
292+
return std::string(path);
293+
294+
if (const char *path = getenv("MINETEST_WORLD_PATH")) {
295+
if (!has_warned) {
296+
warningstream << "MINETEST_WORLD_PATH is deprecated, use LUANTI_WORLD_PATH instead."
297+
<< std::endl;
298+
has_warned = true;
299+
}
300+
return std::string(path);
301+
}
302+
return "";
284303
}
285304

286305
std::vector<WorldSpec> getAvailableWorlds()
@@ -413,10 +432,24 @@ void loadGameConfAndInitWorld(const std::string &path, const std::string &name,
413432

414433
std::vector<std::string> getEnvModPaths()
415434
{
416-
const char *c_mod_path = getenv("MINETEST_MOD_PATH");
435+
static bool has_warned = false;
436+
417437
std::vector<std::string> paths;
418-
Strfnd search_paths(c_mod_path ? c_mod_path : "");
419-
while (!search_paths.at_end())
420-
paths.push_back(search_paths.next(PATH_DELIM));
438+
const char *c_mod_path = nullptr;
439+
if ((c_mod_path = getenv("LUANTI_MOD_PATH"))) {
440+
// no-op
441+
} else if ((c_mod_path = getenv("MINETEST_MOD_PATH"))) {
442+
if (!has_warned) {
443+
warningstream << "MINETEST_MOD_PATH is deprecated, use LUANTI_MOD_PATH instead."
444+
<< std::endl;
445+
has_warned = true;
446+
}
447+
}
448+
449+
if (c_mod_path) {
450+
Strfnd search_paths(c_mod_path);
451+
while (!search_paths.at_end())
452+
paths.push_back(search_paths.next(PATH_DELIM));
453+
}
421454
return paths;
422455
}

src/content/subgames.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ SubgameSpec findWorldSubgame(const std::string &world_path);
5555

5656
std::set<std::string> getAvailableGameIds();
5757
std::vector<SubgameSpec> getAvailableGames();
58-
// Get the list of paths to mods in the environment variable $MINETEST_MOD_PATH
58+
// Get the list of paths to mods in the environment variable LUANTI_MOD_PATH
5959
std::vector<std::string> getEnvModPaths();
6060

6161
bool getWorldExists(const std::string &world_path);

src/porting.cpp

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include <csignal>
6666
#include <cstdarg>
6767
#include <cstdio>
68+
#include <optional>
6869
#include <signal.h>
6970
#include <atomic>
7071

@@ -153,6 +154,24 @@ void signal_handler_init(void)
153154

154155
#endif
155156

157+
/*
158+
Environment variables
159+
*/
160+
161+
static std::optional<std::string> getUserPathEnvVar()
162+
{
163+
if (const char *user_path = getenv("LUANTI_USER_PATH");
164+
user_path && *user_path) {
165+
return user_path;
166+
}
167+
if (const char *user_path = getenv("MINETEST_USER_PATH");
168+
user_path && *user_path) {
169+
warningstream << "MINETEST_USER_PATH is deprecated, "
170+
<< "use LUANTI_USER_PATH instead." << std::endl;
171+
return user_path;
172+
}
173+
return std::nullopt;
174+
}
156175

157176
/*
158177
Path mangler
@@ -464,9 +483,17 @@ bool setSystemPaths()
464483
path_share += DIR_DELIM "..";
465484
}
466485

467-
// Use %MINETEST_USER_PATH%
468-
DWORD len = GetEnvironmentVariable("MINETEST_USER_PATH", buf, sizeof(buf));
469-
FATAL_ERROR_IF(len > sizeof(buf), "Failed to get MINETEST_USER_PATH (too large for buffer)");
486+
// Use %LUANTI_USER_PATH%
487+
DWORD len = GetEnvironmentVariable("LUANTI_USER_PATH", buf, sizeof(buf));
488+
if (!len) {
489+
len = GetEnvironmentVariable("MINETEST_USER_PATH", buf, sizeof(buf));
490+
if (len) {
491+
warningstream << "MINETEST_USER_PATH is deprecated, "
492+
<< "use LUANTI_USER_PATH instead." << std::endl;
493+
}
494+
}
495+
496+
FATAL_ERROR_IF(len >= sizeof(buf), "Failed to get LUANTI_USER_PATH (too large for buffer)");
470497
if (len == 0) {
471498
// Use "C:\Users\<user>\AppData\Roaming\<PROJECT_NAME_C>"
472499
len = GetEnvironmentVariable("APPDATA", buf, sizeof(buf));
@@ -532,9 +559,9 @@ bool setSystemPaths()
532559
break;
533560
}
534561

535-
const char *const env_user_path = getenv("MINETEST_USER_PATH");
536-
if (env_user_path && env_user_path[0] != '\0') {
537-
path_user = std::string(env_user_path);
562+
auto user_path_env = getUserPathEnvVar();
563+
if (user_path_env) {
564+
path_user = std::move(user_path_env.value());
538565
} else {
539566
// TODO: luanti with migration
540567
path_user = std::string(getHomeOrFail()) + DIR_DELIM "." "minetest";
@@ -552,6 +579,7 @@ bool setSystemPaths()
552579
CFBundleRef main_bundle = CFBundleGetMainBundle();
553580
CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
554581
char path[PATH_MAX];
582+
555583
if (CFURLGetFileSystemRepresentation(resources_url,
556584
TRUE, (UInt8 *)path, PATH_MAX)) {
557585
path_share = std::string(path);
@@ -560,9 +588,9 @@ bool setSystemPaths()
560588
}
561589
CFRelease(resources_url);
562590

563-
const char *const env_user_path = getenv("MINETEST_USER_PATH");
564-
if (env_user_path && env_user_path[0] != '\0') {
565-
path_user = std::string(env_user_path);
591+
auto user_path_env = getUserPathEnvVar();
592+
if (user_path_env) {
593+
path_user = std::move(user_path_env.value());
566594
} else {
567595
// TODO: luanti with migration
568596
path_user = std::string(getHomeOrFail())
@@ -577,9 +605,10 @@ bool setSystemPaths()
577605
bool setSystemPaths()
578606
{
579607
path_share = STATIC_SHAREDIR;
580-
const char *const env_user_path = getenv("MINETEST_USER_PATH");
581-
if (env_user_path && env_user_path[0] != '\0') {
582-
path_user = std::string(env_user_path);
608+
609+
auto user_path_env = getUserPathEnvVar();
610+
if (user_path_env) {
611+
path_user = std::move(user_path_env.value());
583612
} else {
584613
// TODO: luanti with migration
585614
path_user = std::string(getHomeOrFail()) + DIR_DELIM "." "minetest";

src/unittest/test_servermodmanager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void TestServerModManager::runTests(IGameDef *gamedef)
5656
ofs2 << "-- intentionally empty\n";
5757
}
5858

59-
setenv("MINETEST_MOD_PATH", test_mods.c_str(), 1);
59+
setenv("LUANTI_MOD_PATH", test_mods.c_str(), 1);
6060

6161
m_worlddir = getTestTempDirectory().append(DIR_DELIM "world");
6262
fs::CreateDir(m_worlddir);
@@ -71,9 +71,9 @@ void TestServerModManager::runTests(IGameDef *gamedef)
7171
TEST(testGetModNames);
7272
TEST(testGetModMediaPathsWrongDir);
7373
TEST(testGetModMediaPaths);
74-
// TODO: test MINETEST_GAME_PATH
74+
// TODO: test LUANTI_GAME_PATH
7575

76-
unsetenv("MINETEST_MOD_PATH");
76+
unsetenv("LUANTI_MOD_PATH");
7777
}
7878

7979
void TestServerModManager::testCreation()

0 commit comments

Comments
 (0)