2013-09-09 22:50:25 +02:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 PilzAdam <pilzadam@minetest.net>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lua_api/l_settings.h"
|
|
|
|
#include "lua_api/l_internal.h"
|
2014-09-06 02:08:51 +02:00
|
|
|
#include "cpp_api/s_security.h"
|
2021-01-20 16:58:59 +01:00
|
|
|
#include "threading/mutex_auto_lock.h"
|
2020-01-25 16:56:54 +01:00
|
|
|
#include "util/string.h" // FlagDesc
|
2013-09-09 22:50:25 +02:00
|
|
|
#include "settings.h"
|
2017-09-09 19:49:12 +02:00
|
|
|
#include "noise.h"
|
2013-09-09 22:50:25 +02:00
|
|
|
#include "log.h"
|
|
|
|
|
2014-12-12 20:49:19 +01:00
|
|
|
|
2021-03-04 20:38:28 +01:00
|
|
|
/* This protects:
|
|
|
|
* 'secure.*' settings from being set
|
|
|
|
* some mapgen settings from being set
|
|
|
|
* (not security-criticial, just to avoid messing up user configs)
|
|
|
|
*/
|
|
|
|
#define CHECK_SETTING_SECURITY(L, name) \
|
|
|
|
if (o->m_settings == g_settings) { \
|
|
|
|
if (checkSettingSecurity(L, name) == -1) \
|
|
|
|
return 0; \
|
2014-12-12 20:49:19 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 20:38:28 +01:00
|
|
|
static inline int checkSettingSecurity(lua_State* L, const std::string &name)
|
|
|
|
{
|
|
|
|
if (ScriptApiSecurity::isSecure(L) && name.compare(0, 7, "secure.") == 0)
|
|
|
|
throw LuaError("Attempt to set secure setting.");
|
|
|
|
|
|
|
|
bool is_mainmenu = false;
|
|
|
|
#ifndef SERVER
|
|
|
|
is_mainmenu = ModApiBase::getGuiEngine(L) != nullptr;
|
|
|
|
#endif
|
|
|
|
if (!is_mainmenu && (name == "mg_name" || name == "mg_flags")) {
|
|
|
|
errorstream << "Tried to set global setting " << name << ", ignoring. "
|
|
|
|
"minetest.set_mapgen_setting() should be used instead." << std::endl;
|
|
|
|
infostream << script_get_backtrace(L) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-12 20:49:19 +01:00
|
|
|
LuaSettings::LuaSettings(Settings *settings, const std::string &filename) :
|
|
|
|
m_settings(settings),
|
2017-06-19 23:54:58 +02:00
|
|
|
m_filename(filename)
|
2014-12-12 20:49:19 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaSettings::LuaSettings(const std::string &filename, bool write_allowed) :
|
|
|
|
m_filename(filename),
|
|
|
|
m_is_own_settings(true),
|
|
|
|
m_write_allowed(write_allowed)
|
|
|
|
{
|
|
|
|
m_settings = new Settings();
|
|
|
|
m_settings->readConfigFile(filename.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaSettings::~LuaSettings()
|
|
|
|
{
|
|
|
|
if (m_is_own_settings)
|
|
|
|
delete m_settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LuaSettings::create(lua_State *L, Settings *settings,
|
|
|
|
const std::string &filename)
|
|
|
|
{
|
|
|
|
LuaSettings *o = new LuaSettings(settings, filename);
|
|
|
|
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
|
|
|
|
luaL_getmetatable(L, className);
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-09 22:50:25 +02:00
|
|
|
// garbage collector
|
|
|
|
int LuaSettings::gc_object(lua_State* L)
|
|
|
|
{
|
|
|
|
LuaSettings* o = *(LuaSettings **)(lua_touserdata(L, 1));
|
|
|
|
delete o;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-12 20:49:19 +01:00
|
|
|
|
2013-09-09 22:50:25 +02:00
|
|
|
// get(self, key) -> value
|
|
|
|
int LuaSettings::l_get(lua_State* L)
|
|
|
|
{
|
|
|
|
NO_MAP_LOCK_REQUIRED;
|
|
|
|
LuaSettings* o = checkobject(L, 1);
|
|
|
|
|
|
|
|
std::string key = std::string(luaL_checkstring(L, 2));
|
|
|
|
if (o->m_settings->exists(key)) {
|
|
|
|
std::string value = o->m_settings->get(key);
|
|
|
|
lua_pushstring(L, value.c_str());
|
|
|
|
} else {
|
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_bool(self, key) -> boolean
|
|
|
|
int LuaSettings::l_get_bool(lua_State* L)
|
|
|
|
{
|
|
|
|
NO_MAP_LOCK_REQUIRED;
|
|
|
|
LuaSettings* o = checkobject(L, 1);
|
|
|
|
|
|
|
|
std::string key = std::string(luaL_checkstring(L, 2));
|
|
|
|
if (o->m_settings->exists(key)) {
|
|
|
|
bool value = o->m_settings->getBool(key);
|
|
|
|
lua_pushboolean(L, value);
|
|
|
|
} else {
|
2017-12-17 16:27:37 +01:00
|
|
|
// Push default value
|
|
|
|
if (lua_isboolean(L, 3))
|
2018-06-30 17:11:38 +02:00
|
|
|
lua_pushboolean(L, readParam<bool>(L, 3));
|
2017-12-17 16:27:37 +01:00
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
2013-09-09 22:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-09-09 19:49:12 +02:00
|
|
|
// get_np_group(self, key) -> value
|
|
|
|
int LuaSettings::l_get_np_group(lua_State *L)
|
|
|
|
{
|
|
|
|
NO_MAP_LOCK_REQUIRED;
|
|
|
|
LuaSettings *o = checkobject(L, 1);
|
|
|
|
|
|
|
|
std::string key = std::string(luaL_checkstring(L, 2));
|
|
|
|
if (o->m_settings->exists(key)) {
|
|
|
|
NoiseParams np;
|
|
|
|
o->m_settings->getNoiseParams(key, np);
|
|
|
|
push_noiseparams(L, &np);
|
|
|
|
} else {
|
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-03-04 20:38:28 +01:00
|
|
|
// get_flags(self, key) -> table or nil
|
2020-01-25 16:56:54 +01:00
|
|
|
int LuaSettings::l_get_flags(lua_State *L)
|
|
|
|
{
|
|
|
|
NO_MAP_LOCK_REQUIRED;
|
|
|
|
LuaSettings *o = checkobject(L, 1);
|
|
|
|
std::string key = std::string(luaL_checkstring(L, 2));
|
|
|
|
|
|
|
|
u32 flags = 0;
|
|
|
|
auto flagdesc = o->m_settings->getFlagDescFallback(key);
|
|
|
|
if (o->m_settings->getFlagStrNoEx(key, flags, flagdesc)) {
|
|
|
|
lua_newtable(L);
|
|
|
|
int table = lua_gettop(L);
|
|
|
|
for (size_t i = 0; flagdesc[i].name; ++i) {
|
|
|
|
lua_pushboolean(L, flags & flagdesc[i].flag);
|
|
|
|
lua_setfield(L, table, flagdesc[i].name);
|
|
|
|
}
|
|
|
|
lua_pushvalue(L, table);
|
|
|
|
} else {
|
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-09-09 22:50:25 +02:00
|
|
|
// set(self, key, value)
|
|
|
|
int LuaSettings::l_set(lua_State* L)
|
|
|
|
{
|
|
|
|
NO_MAP_LOCK_REQUIRED;
|
|
|
|
LuaSettings* o = checkobject(L, 1);
|
|
|
|
|
|
|
|
std::string key = std::string(luaL_checkstring(L, 2));
|
|
|
|
const char* value = luaL_checkstring(L, 3);
|
|
|
|
|
2021-03-04 20:38:28 +01:00
|
|
|
CHECK_SETTING_SECURITY(L, key);
|
2014-12-12 20:49:19 +01:00
|
|
|
|
2014-12-12 06:44:17 +01:00
|
|
|
if (!o->m_settings->set(key, value))
|
|
|
|
throw LuaError("Invalid sequence found in setting parameters");
|
2013-09-09 22:50:25 +02:00
|
|
|
|
2014-12-12 06:44:17 +01:00
|
|
|
return 0;
|
2013-09-09 22:50:25 +02:00
|
|
|
}
|
|
|
|
|
2014-12-12 20:49:19 +01:00
|
|
|
// set_bool(self, key, value)
|
|
|
|
int LuaSettings::l_set_bool(lua_State* L)
|
|
|
|
{
|
|
|
|
NO_MAP_LOCK_REQUIRED;
|
|
|
|
LuaSettings* o = checkobject(L, 1);
|
|
|
|
|
|
|
|
std::string key = std::string(luaL_checkstring(L, 2));
|
2018-06-30 17:11:38 +02:00
|
|
|
bool value = readParam<bool>(L, 3);
|
2014-12-12 20:49:19 +01:00
|
|
|
|
2021-03-04 20:38:28 +01:00
|
|
|
CHECK_SETTING_SECURITY(L, key);
|
2014-12-12 20:49:19 +01:00
|
|
|
|
|
|
|
o->m_settings->setBool(key, value);
|
|
|
|
|
2021-03-04 20:38:28 +01:00
|
|
|
return 0;
|
2014-12-12 20:49:19 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 20:38:28 +01:00
|
|
|
// set_np_group(self, key, value)
|
2017-09-09 19:49:12 +02:00
|
|
|
int LuaSettings::l_set_np_group(lua_State *L)
|
|
|
|
{
|
|
|
|
NO_MAP_LOCK_REQUIRED;
|
|
|
|
LuaSettings *o = checkobject(L, 1);
|
|
|
|
|
|
|
|
std::string key = std::string(luaL_checkstring(L, 2));
|
|
|
|
NoiseParams value;
|
|
|
|
read_noiseparams(L, 3, &value);
|
|
|
|
|
2021-03-04 20:38:28 +01:00
|
|
|
CHECK_SETTING_SECURITY(L, key);
|
2017-09-09 19:49:12 +02:00
|
|
|
|
2020-11-22 17:49:30 +01:00
|
|
|
o->m_settings->setNoiseParams(key, value);
|
2017-09-09 19:49:12 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-09 22:50:25 +02:00
|
|
|
// remove(self, key) -> success
|
|
|
|
int LuaSettings::l_remove(lua_State* L)
|
|
|
|
{
|
|
|
|
NO_MAP_LOCK_REQUIRED;
|
|
|
|
LuaSettings* o = checkobject(L, 1);
|
|
|
|
|
|
|
|
std::string key = std::string(luaL_checkstring(L, 2));
|
|
|
|
|
2021-03-04 20:38:28 +01:00
|
|
|
CHECK_SETTING_SECURITY(L, key);
|
2014-12-12 20:49:19 +01:00
|
|
|
|
2013-09-09 22:50:25 +02:00
|
|
|
bool success = o->m_settings->remove(key);
|
|
|
|
lua_pushboolean(L, success);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_names(self) -> {key1, ...}
|
|
|
|
int LuaSettings::l_get_names(lua_State* L)
|
|
|
|
{
|
|
|
|
NO_MAP_LOCK_REQUIRED;
|
|
|
|
LuaSettings* o = checkobject(L, 1);
|
|
|
|
|
|
|
|
std::vector<std::string> keys = o->m_settings->getNames();
|
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
for (unsigned int i=0; i < keys.size(); i++)
|
|
|
|
{
|
|
|
|
lua_pushstring(L, keys[i].c_str());
|
|
|
|
lua_rawseti(L, -2, i + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write(self) -> success
|
|
|
|
int LuaSettings::l_write(lua_State* L)
|
|
|
|
{
|
|
|
|
NO_MAP_LOCK_REQUIRED;
|
|
|
|
LuaSettings* o = checkobject(L, 1);
|
|
|
|
|
2016-12-05 20:59:15 +01:00
|
|
|
if (!o->m_write_allowed) {
|
|
|
|
throw LuaError("Settings: writing " + o->m_filename +
|
|
|
|
" not allowed with mod security on.");
|
|
|
|
}
|
|
|
|
|
2013-09-09 22:50:25 +02:00
|
|
|
bool success = o->m_settings->updateConfigFile(o->m_filename.c_str());
|
|
|
|
lua_pushboolean(L, success);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-20 16:58:59 +01:00
|
|
|
static void push_settings_table(lua_State *L, const Settings *settings)
|
2013-09-09 22:50:25 +02:00
|
|
|
{
|
2021-01-20 16:58:59 +01:00
|
|
|
std::vector<std::string> keys = settings->getNames();
|
2013-09-09 22:50:25 +02:00
|
|
|
lua_newtable(L);
|
2017-08-19 22:23:47 +02:00
|
|
|
for (const std::string &key : keys) {
|
2021-01-20 16:58:59 +01:00
|
|
|
std::string value;
|
|
|
|
Settings *group = nullptr;
|
|
|
|
|
|
|
|
if (settings->getNoEx(key, value)) {
|
|
|
|
lua_pushstring(L, value.c_str());
|
|
|
|
} else if (settings->getGroupNoEx(key, group)) {
|
|
|
|
// Recursively push tables
|
|
|
|
push_settings_table(L, group);
|
|
|
|
} else {
|
|
|
|
// Impossible case (multithreading) due to MutexAutoLock
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-08-19 22:23:47 +02:00
|
|
|
lua_setfield(L, -2, key.c_str());
|
2013-09-09 22:50:25 +02:00
|
|
|
}
|
2021-01-20 16:58:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// to_table(self) -> {[key1]=value1,...}
|
|
|
|
int LuaSettings::l_to_table(lua_State* L)
|
|
|
|
{
|
|
|
|
NO_MAP_LOCK_REQUIRED;
|
|
|
|
LuaSettings* o = checkobject(L, 1);
|
2013-09-09 22:50:25 +02:00
|
|
|
|
2021-01-20 16:58:59 +01:00
|
|
|
MutexAutoLock(o->m_settings->m_mutex);
|
|
|
|
push_settings_table(L, o->m_settings);
|
2013-09-09 22:50:25 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LuaSettings::Register(lua_State* L)
|
|
|
|
{
|
|
|
|
lua_newtable(L);
|
|
|
|
int methodtable = lua_gettop(L);
|
|
|
|
luaL_newmetatable(L, className);
|
|
|
|
int metatable = lua_gettop(L);
|
|
|
|
|
|
|
|
lua_pushliteral(L, "__metatable");
|
|
|
|
lua_pushvalue(L, methodtable);
|
|
|
|
lua_settable(L, metatable); // hide metatable from Lua getmetatable()
|
|
|
|
|
|
|
|
lua_pushliteral(L, "__index");
|
|
|
|
lua_pushvalue(L, methodtable);
|
|
|
|
lua_settable(L, metatable);
|
|
|
|
|
|
|
|
lua_pushliteral(L, "__gc");
|
|
|
|
lua_pushcfunction(L, gc_object);
|
|
|
|
lua_settable(L, metatable);
|
|
|
|
|
|
|
|
lua_pop(L, 1); // drop metatable
|
|
|
|
|
2021-05-25 20:20:16 +02:00
|
|
|
luaL_register(L, nullptr, methods); // fill methodtable
|
2013-09-09 22:50:25 +02:00
|
|
|
lua_pop(L, 1); // drop methodtable
|
|
|
|
|
|
|
|
// Can be created from Lua (Settings(filename))
|
|
|
|
lua_register(L, className, create_object);
|
|
|
|
}
|
|
|
|
|
|
|
|
// LuaSettings(filename)
|
2014-12-12 20:49:19 +01:00
|
|
|
// Creates a LuaSettings and leaves it on top of the stack
|
2013-09-09 22:50:25 +02:00
|
|
|
int LuaSettings::create_object(lua_State* L)
|
|
|
|
{
|
|
|
|
NO_MAP_LOCK_REQUIRED;
|
2016-12-21 10:20:06 +01:00
|
|
|
bool write_allowed = true;
|
2013-09-09 22:50:25 +02:00
|
|
|
const char* filename = luaL_checkstring(L, 1);
|
2016-12-05 20:59:15 +01:00
|
|
|
CHECK_SECURE_PATH_POSSIBLE_WRITE(L, filename, &write_allowed);
|
|
|
|
LuaSettings* o = new LuaSettings(filename, write_allowed);
|
2013-09-09 22:50:25 +02:00
|
|
|
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
|
|
|
|
luaL_getmetatable(L, className);
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaSettings* LuaSettings::checkobject(lua_State* L, int narg)
|
|
|
|
{
|
|
|
|
NO_MAP_LOCK_REQUIRED;
|
|
|
|
luaL_checktype(L, narg, LUA_TUSERDATA);
|
|
|
|
void *ud = luaL_checkudata(L, narg, className);
|
2014-12-12 20:49:19 +01:00
|
|
|
if (!ud)
|
|
|
|
luaL_typerror(L, narg, className);
|
|
|
|
return *(LuaSettings**) ud; // unbox pointer
|
2013-09-09 22:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char LuaSettings::className[] = "Settings";
|
2017-04-08 09:28:37 +02:00
|
|
|
const luaL_Reg LuaSettings::methods[] = {
|
2013-09-09 22:50:25 +02:00
|
|
|
luamethod(LuaSettings, get),
|
|
|
|
luamethod(LuaSettings, get_bool),
|
2017-09-09 19:49:12 +02:00
|
|
|
luamethod(LuaSettings, get_np_group),
|
2020-01-25 16:56:54 +01:00
|
|
|
luamethod(LuaSettings, get_flags),
|
2013-09-09 22:50:25 +02:00
|
|
|
luamethod(LuaSettings, set),
|
2014-12-12 20:49:19 +01:00
|
|
|
luamethod(LuaSettings, set_bool),
|
2017-09-09 19:49:12 +02:00
|
|
|
luamethod(LuaSettings, set_np_group),
|
2013-09-09 22:50:25 +02:00
|
|
|
luamethod(LuaSettings, remove),
|
|
|
|
luamethod(LuaSettings, get_names),
|
|
|
|
luamethod(LuaSettings, write),
|
|
|
|
luamethod(LuaSettings, to_table),
|
|
|
|
{0,0}
|
|
|
|
};
|