Skip to content

Commit

Permalink
Add world-independent storage directory for mods (#12315)
Browse files Browse the repository at this point in the history
Fixes #4821
  • Loading branch information
rubenwardy committed Mar 24, 2024
1 parent b42b03b commit 6c4a110
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -64,6 +64,7 @@ AppDir
/clientmods/*
!/clientmods/preview/
/client/mod_storage/
/mod_data

## Configuration/log files
minetest.conf
Expand Down
6 changes: 6 additions & 0 deletions doc/lua_api.md
Expand Up @@ -5306,6 +5306,12 @@ Utilities

* `minetest.get_worldpath()`: returns e.g. `"/home/user/.minetest/world"`
* Useful for storing custom data
* `minetest.get_mod_data_path()`: returns e.g. `"/home/user/.minetest/mod_data/mymod"`
* Useful for storing custom data *independently of worlds*.
* Must be called during mod load time.
* Can read or write to this directory at any time.
* It's possible that multiple Minetest instances are running at the same
time, which may lead to corruption if you are not careful.
* `minetest.is_singleplayer()`
* `minetest.features`: Table containing API feature flags

Expand Down
1 change: 1 addition & 0 deletions src/gamedef.h
Expand Up @@ -75,6 +75,7 @@ class IGameDef
virtual const ModSpec* getModSpec(const std::string &modname) const = 0;
virtual const SubgameSpec* getGameSpec() const { return nullptr; }
virtual std::string getWorldPath() const { return ""; }
virtual std::string getModDataPath() const { return ""; }
virtual ModStorageDatabase *getModStorageDatabase() = 0;

virtual bool joinModChannel(const std::string &channel) = 0;
Expand Down
7 changes: 7 additions & 0 deletions src/script/cpp_api/s_security.cpp
Expand Up @@ -600,6 +600,13 @@ bool ScriptApiSecurity::checkPath(lua_State *L, const char *path,
}
}

// Allow read/write access to all mod common dirs
str = fs::AbsolutePath(gamedef->getModDataPath());
if (!str.empty() && fs::PathStartsWith(abs_path, str)) {
if (write_allowed) *write_allowed = true;
return true;
}

str = fs::AbsolutePath(gamedef->getWorldPath());
if (!str.empty()) {
// Don't allow access to other paths in the world mod/game path.
Expand Down
20 changes: 20 additions & 0 deletions src/script/lua_api/l_server.cpp
Expand Up @@ -29,6 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "environment.h"
#include "remoteplayer.h"
#include "log.h"
#include "filesys.h"
#include <algorithm>

// request_shutdown()
Expand Down Expand Up @@ -507,6 +508,24 @@ int ModApiServer::l_get_worldpath(lua_State *L)
return 1;
}

// get_mod_data_path()
int ModApiServer::l_get_mod_data_path(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;

std::string modname = ScriptApiBase::getCurrentModNameInsecure(L);
if (modname.empty())
return 0;

const Server *srv = getServer(L);
std::string path = srv->getModDataPath() + DIR_DELIM + modname;
if (!fs::CreateAllDirs(path))
throw LuaError("Failed to create dir");

lua_pushstring(L, path.c_str());
return 1;
}

// sound_play(spec, parameters, [ephemeral])
int ModApiServer::l_sound_play(lua_State *L)
{
Expand Down Expand Up @@ -716,6 +735,7 @@ void ModApiServer::Initialize(lua_State *L, int top)
API_FCT(get_server_status);
API_FCT(get_server_uptime);
API_FCT(get_server_max_lag);
API_FCT(get_mod_data_path);
API_FCT(get_worldpath);
API_FCT(is_singleplayer);

Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_server.h
Expand Up @@ -39,6 +39,9 @@ class ModApiServer : public ModApiBase
// get_worldpath()
static int l_get_worldpath(lua_State *L);

// get_mod_data_path()
static int l_get_mod_data_path(lua_State *L);

// is_singleplayer()
static int l_is_singleplayer(lua_State *L);

Expand Down
4 changes: 4 additions & 0 deletions src/server.cpp
Expand Up @@ -317,6 +317,10 @@ Server::Server(
"Number of map edit events");

m_lag_gauge->set(g_settings->getFloat("dedicated_server_step"));

m_path_mod_data = porting::path_user + DIR_DELIM "mod_data";
if (!fs::CreateDir(m_path_mod_data))
throw ServerError("Failed to create mod data dir");
}

Server::~Server()
Expand Down
2 changes: 2 additions & 0 deletions src/server.h
Expand Up @@ -306,6 +306,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
virtual const SubgameSpec* getGameSpec() const { return &m_gamespec; }
static std::string getBuiltinLuaPath();
virtual std::string getWorldPath() const { return m_path_world; }
virtual std::string getModDataPath() const { return m_path_mod_data; }

inline bool isSingleplayer() const
{ return m_simple_singleplayer_mode; }
Expand Down Expand Up @@ -609,6 +610,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
*/
// World directory
std::string m_path_world;
std::string m_path_mod_data;
// Subgame specification
SubgameSpec m_gamespec;
// If true, do not allow multiple players and hide some multiplayer
Expand Down

0 comments on commit 6c4a110

Please sign in to comment.