Skip to content

Commit

Permalink
Add minetest.settings to CSM API and allow CSMs to provide `setting…
Browse files Browse the repository at this point in the history
…types.txt` (#12131)

Co-authored-by: sfan5 <sfan5@live.de>
Co-authored-by: SmallJoker <SmallJoker@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 2, 2022
1 parent 839600e commit 6ec6acc
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions builtin/client/init.lua
Expand Up @@ -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")
7 changes: 7 additions & 0 deletions 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
30 changes: 30 additions & 0 deletions builtin/mainmenu/dlg_settings_advanced.lua
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions clientmods/preview/init.lua
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions clientmods/preview/settingtypes.txt
@@ -0,0 +1 @@
preview_csm_test_setting (Test CSM setting) bool false
8 changes: 8 additions & 0 deletions doc/client_lua_api.txt
Expand Up @@ -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
---------------

Expand Down
6 changes: 6 additions & 0 deletions src/script/cpp_api/s_security.cpp
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_util.cpp
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/script/scripting_client.cpp
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6ec6acc

Please sign in to comment.