From fdd2efbbbe1dedb82f524ba670a889b1d7f57f1d 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 | 8 +++++- src/script/lua_api/l_util.cpp | 3 +++ src/script/scripting_client.cpp | 2 ++ 9 files changed, 62 insertions(+), 1 deletion(-) 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 74edcd3f3..e64078e44 100644 --- a/builtin/client/init.lua +++ b/builtin/client/init.lua @@ -10,3 +10,4 @@ dofile(clientpath .. "chatcommands.lua") dofile(commonpath .. "vector.lua") dofile(clientpath .. "death_formspec.lua") dofile(clientpath .. "sscsm.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 6a44df412..18a16a279 100644 --- a/builtin/mainmenu/dlg_settings_advanced.lua +++ b/builtin/mainmenu/dlg_settings_advanced.lua @@ -404,6 +404,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 ad9fa5e0f..682a97335 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -932,6 +932,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 3e276efc2..aaff8ecb1 100644 --- a/src/script/cpp_api/s_security.cpp +++ b/src/script/cpp_api/s_security.cpp @@ -18,7 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #include "cpp_api/s_security.h" - +#include "lua_api/l_base.h" #include "filesys.h" #include "porting.h" #include "server.h" @@ -372,6 +372,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 4a2eaed80..138f0a879 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -559,6 +559,9 @@ void ModApiUtil::InitializeClient(lua_State *L, int top) API_FCT(get_version); API_FCT(sha1); + + 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 bcc820def..a55928788 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);