aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJude Melton-Houghton <jwmhjwmh@gmail.com>2022-11-28 07:21:43 -0500
committerGitHub <noreply@github.com>2022-11-28 07:21:43 -0500
commitd0a118f5b1a2cada1d46d4acac55a998e268154d (patch)
tree72e38e81f8f9ef0d89a6deafa74daa6a261ad49e /src
parent3fd5bff128cacb068c0758d0b33b425dc88beed7 (diff)
downloadminetest-d0a118f5b1a2cada1d46d4acac55a998e268154d.tar.xz
Add `minetest.get_game_info` and allow reading `game.conf` (#12989)
Co-authored-by: sfan5 <sfan5@live.de>
Diffstat (limited to 'src')
-rw-r--r--src/gamedef.h2
-rw-r--r--src/script/cpp_api/s_security.cpp11
-rw-r--r--src/script/lua_api/l_server.cpp16
-rw-r--r--src/script/lua_api/l_server.h3
-rw-r--r--src/server.h1
5 files changed, 33 insertions, 0 deletions
diff --git a/src/gamedef.h b/src/gamedef.h
index e32d10509..df1fa2458 100644
--- a/src/gamedef.h
+++ b/src/gamedef.h
@@ -40,6 +40,7 @@ namespace irr { namespace scene {
class ISceneManager;
}}
+struct SubgameSpec;
struct ModSpec;
/*
An interface for fetching game-global definitions like tool and
@@ -72,6 +73,7 @@ public:
virtual const std::vector<ModSpec> &getMods() const = 0;
virtual const ModSpec* getModSpec(const std::string &modname) const = 0;
+ virtual const SubgameSpec* getGameSpec() const { return nullptr; }
virtual std::string getWorldPath() const { return ""; }
virtual ModStorageDatabase *getModStorageDatabase() = 0;
diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp
index 0d989ae04..09e971007 100644
--- a/src/script/cpp_api/s_security.cpp
+++ b/src/script/cpp_api/s_security.cpp
@@ -579,6 +579,17 @@ bool ScriptApiSecurity::checkPath(lua_State *L, const char *path,
}
lua_pop(L, 1); // Pop mod name
+ // Allow read-only access to game directory
+ if (!write_required) {
+ const SubgameSpec *game_spec = gamedef->getGameSpec();
+ if (game_spec && !game_spec->path.empty()) {
+ str = fs::AbsolutePath(game_spec->path);
+ if (!str.empty() && fs::PathStartsWith(abs_path, str)) {
+ return true;
+ }
+ }
+ }
+
// Allow read-only access to all mod directories
if (!write_required) {
const std::vector<ModSpec> &mods = gamedef->getMods();
diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp
index a5daae346..fb2018aac 100644
--- a/src/script/lua_api/l_server.cpp
+++ b/src/script/lua_api/l_server.cpp
@@ -424,6 +424,20 @@ int ModApiServer::l_get_modnames(lua_State *L)
return 1;
}
+// get_game_info()
+int ModApiServer::l_get_game_info(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const SubgameSpec *game_spec = getGameDef(L)->getGameSpec();
+ assert(game_spec);
+ lua_newtable(L);
+ setstringfield(L, -1, "id", game_spec->id);
+ setstringfield(L, -1, "title", game_spec->title);
+ setstringfield(L, -1, "author", game_spec->author);
+ setstringfield(L, -1, "path", game_spec->path);
+ return 1;
+}
+
// get_worldpath()
int ModApiServer::l_get_worldpath(lua_State *L)
{
@@ -608,6 +622,7 @@ void ModApiServer::Initialize(lua_State *L, int top)
API_FCT(get_current_modname);
API_FCT(get_modpath);
API_FCT(get_modnames);
+ API_FCT(get_game_info);
API_FCT(print);
@@ -643,4 +658,5 @@ void ModApiServer::InitializeAsync(lua_State *L, int top)
API_FCT(get_current_modname);
API_FCT(get_modpath);
API_FCT(get_modnames);
+ API_FCT(get_game_info);
}
diff --git a/src/script/lua_api/l_server.h b/src/script/lua_api/l_server.h
index a4f38c34e..15d61669e 100644
--- a/src/script/lua_api/l_server.h
+++ b/src/script/lua_api/l_server.h
@@ -52,6 +52,9 @@ private:
// the returned list is sorted alphabetically for you
static int l_get_modnames(lua_State *L);
+ // get_game_info()
+ static int l_get_game_info(lua_State *L);
+
// print(text)
static int l_print(lua_State *L);
diff --git a/src/server.h b/src/server.h
index 06da14a9a..365fdf625 100644
--- a/src/server.h
+++ b/src/server.h
@@ -284,6 +284,7 @@ public:
virtual const std::vector<ModSpec> &getMods() const;
virtual const ModSpec* getModSpec(const std::string &modname) const;
+ virtual const SubgameSpec* getGameSpec() const { return &m_gamespec; }
static std::string getBuiltinLuaPath();
virtual std::string getWorldPath() const { return m_path_world; }