From 6ec6acc539321709ed8517f1a571777a04f5c24c Mon Sep 17 00:00:00 2001 From: AFCMS Date: Tue, 2 Aug 2022 11:58:08 +0200 Subject: [PATCH] Add `minetest.settings` to CSM API and allow CSMs to provide `settingtypes.txt` (#12131) Co-authored-by: sfan5 Co-authored-by: SmallJoker --- builtin/client/init.lua | 1 + builtin/client/misc.lua | 7 +++++ builtin/mainmenu/dlg_settings_advanced.lua | 30 ++++++++++++++++++++++ clientmods/preview/init.lua | 3 +++ clientmods/preview/settingtypes.txt | 1 + doc/client_lua_api.txt | 8 ++++++ src/script/cpp_api/s_security.cpp | 6 +++++ src/script/lua_api/l_util.cpp | 3 +++ src/script/scripting_client.cpp | 2 ++ 9 files changed, 61 insertions(+) create mode 100644 builtin/client/misc.lua create mode 100644 clientmods/preview/settingtypes.txt diff --git a/builtin/client/init.lua b/builtin/client/init.lua index 0133fc16d..3719a90ee 100644 --- a/builtin/client/init.lua +++ b/builtin/client/init.lua @@ -9,3 +9,4 @@ dofile(commonpath .. "mod_storage.lua") dofile(commonpath .. "chatcommands.lua") dofile(clientpath .. "chatcommands.lua") dofile(clientpath .. "death_formspec.lua") +dofile(clientpath .. "misc.lua") diff --git a/builtin/client/misc.lua b/builtin/client/misc.lua new file mode 100644 index 000000000..80e0f2904 --- /dev/null +++ b/builtin/client/misc.lua @@ -0,0 +1,7 @@ +function core.setting_get_pos(name) + local value = core.settings:get(name) + if not value then + return nil + end + return core.string_to_pos(value) +end diff --git a/builtin/mainmenu/dlg_settings_advanced.lua b/builtin/mainmenu/dlg_settings_advanced.lua index c395f0b08..69562e6a5 100644 --- a/builtin/mainmenu/dlg_settings_advanced.lua +++ b/builtin/mainmenu/dlg_settings_advanced.lua @@ -405,6 +405,36 @@ local function parse_config_file(read_all, parse_mods) file:close() end end + + -- Parse client mods + local clientmods_category_initialized = false + local clientmods = {} + get_mods(core.get_clientmodpath(), "clientmods", clientmods) + for _, mod in ipairs(clientmods) do + local path = mod.path .. DIR_DELIM .. FILENAME + local file = io.open(path, "r") + if file then + if not clientmods_category_initialized then + fgettext_ne("Client Mods") -- not used, but needed for xgettext + table.insert(settings, { + name = "Client Mods", + level = 0, + type = "category", + }) + clientmods_category_initialized = true + end + + table.insert(settings, { + name = mod.name, + level = 1, + type = "category", + }) + + parse_single_file(file, path, read_all, settings, 2, false) + + file:close() + end + end end return settings diff --git a/clientmods/preview/init.lua b/clientmods/preview/init.lua index 977ed0ec3..46d59ec9a 100644 --- a/clientmods/preview/init.lua +++ b/clientmods/preview/init.lua @@ -165,6 +165,9 @@ core.after(5, function() print("[PREVIEW] Find node near: " .. dump(core.find_node_near({x=0, y=20, z=0}, 10, {"group:tree", "default:dirt", "default:stone"}))) + + print("[PREVIEW] Settings: preview_csm_test_setting = " .. + tostring(core.settings:get_bool("preview_csm_test_setting", false))) end) core.register_on_dignode(function(pos, node) diff --git a/clientmods/preview/settingtypes.txt b/clientmods/preview/settingtypes.txt new file mode 100644 index 000000000..fea9e71f3 --- /dev/null +++ b/clientmods/preview/settingtypes.txt @@ -0,0 +1 @@ +preview_csm_test_setting (Test CSM setting) bool false \ No newline at end of file diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index d08cd9b5e..8a450ba13 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -938,6 +938,14 @@ Call these functions only at load time! * `minetest.display_chat_message(message)` returns true on success * Shows a chat message to the current player. +Setting-related +--------------- + +* `minetest.settings`: Settings object containing all of the settings from the + main config file (`minetest.conf`). Check lua_api.txt for class reference. +* `minetest.setting_get_pos(name)`: Loads a setting from the main settings and + parses it as a position (in the format `(1,2,3)`). Returns a position or nil. + Class reference --------------- diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp index 88e22f16f..316b19926 100644 --- a/src/script/cpp_api/s_security.cpp +++ b/src/script/cpp_api/s_security.cpp @@ -417,6 +417,12 @@ void ScriptApiSecurity::setLuaEnv(lua_State *L, int thread) bool ScriptApiSecurity::isSecure(lua_State *L) { +#ifndef SERVER + auto script = ModApiBase::getScriptApiBase(L); + // CSM keeps no globals backup but is always secure + if (script->getType() == ScriptingType::Client) + return true; +#endif lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP); bool secure = !lua_isnil(L, -1); lua_pop(L, 1); diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index f774daf97..47a68ad75 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -659,6 +659,9 @@ void ModApiUtil::InitializeClient(lua_State *L, int top) API_FCT(sha1); API_FCT(colorspec_to_colorstring); API_FCT(colorspec_to_bytes); + + LuaSettings::create(L, g_settings, g_settings_path); + lua_setfield(L, top, "settings"); } void ModApiUtil::InitializeAsync(lua_State *L, int top) diff --git a/src/script/scripting_client.cpp b/src/script/scripting_client.cpp index 6643a9509..377205379 100644 --- a/src/script/scripting_client.cpp +++ b/src/script/scripting_client.cpp @@ -35,6 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_nodemeta.h" #include "lua_api/l_localplayer.h" #include "lua_api/l_camera.h" +#include "lua_api/l_settings.h" ClientScripting::ClientScripting(Client *client): ScriptApiBase(ScriptingType::Client) @@ -73,6 +74,7 @@ void ClientScripting::InitializeModApi(lua_State *L, int top) LuaLocalPlayer::Register(L); LuaCamera::Register(L); ModChannelRef::Register(L); + LuaSettings::Register(L); ModApiUtil::InitializeClient(L, top); ModApiClient::Initialize(L, top);