Skip to content

Commit 6ec6acc

Browse files
AFCMSsfan5SmallJoker
authored
Add minetest.settings to CSM API and allow CSMs to provide settingtypes.txt (luanti-org#12131)
Co-authored-by: sfan5 <[email protected]> Co-authored-by: SmallJoker <[email protected]>
1 parent 839600e commit 6ec6acc

File tree

9 files changed

+61
-0
lines changed

9 files changed

+61
-0
lines changed

builtin/client/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ dofile(commonpath .. "mod_storage.lua")
99
dofile(commonpath .. "chatcommands.lua")
1010
dofile(clientpath .. "chatcommands.lua")
1111
dofile(clientpath .. "death_formspec.lua")
12+
dofile(clientpath .. "misc.lua")

builtin/client/misc.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function core.setting_get_pos(name)
2+
local value = core.settings:get(name)
3+
if not value then
4+
return nil
5+
end
6+
return core.string_to_pos(value)
7+
end

builtin/mainmenu/dlg_settings_advanced.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,36 @@ local function parse_config_file(read_all, parse_mods)
405405
file:close()
406406
end
407407
end
408+
409+
-- Parse client mods
410+
local clientmods_category_initialized = false
411+
local clientmods = {}
412+
get_mods(core.get_clientmodpath(), "clientmods", clientmods)
413+
for _, mod in ipairs(clientmods) do
414+
local path = mod.path .. DIR_DELIM .. FILENAME
415+
local file = io.open(path, "r")
416+
if file then
417+
if not clientmods_category_initialized then
418+
fgettext_ne("Client Mods") -- not used, but needed for xgettext
419+
table.insert(settings, {
420+
name = "Client Mods",
421+
level = 0,
422+
type = "category",
423+
})
424+
clientmods_category_initialized = true
425+
end
426+
427+
table.insert(settings, {
428+
name = mod.name,
429+
level = 1,
430+
type = "category",
431+
})
432+
433+
parse_single_file(file, path, read_all, settings, 2, false)
434+
435+
file:close()
436+
end
437+
end
408438
end
409439

410440
return settings

clientmods/preview/init.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ core.after(5, function()
165165

166166
print("[PREVIEW] Find node near: " .. dump(core.find_node_near({x=0, y=20, z=0}, 10,
167167
{"group:tree", "default:dirt", "default:stone"})))
168+
169+
print("[PREVIEW] Settings: preview_csm_test_setting = " ..
170+
tostring(core.settings:get_bool("preview_csm_test_setting", false)))
168171
end)
169172

170173
core.register_on_dignode(function(pos, node)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
preview_csm_test_setting (Test CSM setting) bool false

doc/client_lua_api.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,14 @@ Call these functions only at load time!
938938
* `minetest.display_chat_message(message)` returns true on success
939939
* Shows a chat message to the current player.
940940

941+
Setting-related
942+
---------------
943+
944+
* `minetest.settings`: Settings object containing all of the settings from the
945+
main config file (`minetest.conf`). Check lua_api.txt for class reference.
946+
* `minetest.setting_get_pos(name)`: Loads a setting from the main settings and
947+
parses it as a position (in the format `(1,2,3)`). Returns a position or nil.
948+
941949
Class reference
942950
---------------
943951

src/script/cpp_api/s_security.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,12 @@ void ScriptApiSecurity::setLuaEnv(lua_State *L, int thread)
417417

418418
bool ScriptApiSecurity::isSecure(lua_State *L)
419419
{
420+
#ifndef SERVER
421+
auto script = ModApiBase::getScriptApiBase(L);
422+
// CSM keeps no globals backup but is always secure
423+
if (script->getType() == ScriptingType::Client)
424+
return true;
425+
#endif
420426
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
421427
bool secure = !lua_isnil(L, -1);
422428
lua_pop(L, 1);

src/script/lua_api/l_util.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,9 @@ void ModApiUtil::InitializeClient(lua_State *L, int top)
659659
API_FCT(sha1);
660660
API_FCT(colorspec_to_colorstring);
661661
API_FCT(colorspec_to_bytes);
662+
663+
LuaSettings::create(L, g_settings, g_settings_path);
664+
lua_setfield(L, top, "settings");
662665
}
663666

664667
void ModApiUtil::InitializeAsync(lua_State *L, int top)

src/script/scripting_client.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3535
#include "lua_api/l_nodemeta.h"
3636
#include "lua_api/l_localplayer.h"
3737
#include "lua_api/l_camera.h"
38+
#include "lua_api/l_settings.h"
3839

3940
ClientScripting::ClientScripting(Client *client):
4041
ScriptApiBase(ScriptingType::Client)
@@ -73,6 +74,7 @@ void ClientScripting::InitializeModApi(lua_State *L, int top)
7374
LuaLocalPlayer::Register(L);
7475
LuaCamera::Register(L);
7576
ModChannelRef::Register(L);
77+
LuaSettings::Register(L);
7678

7779
ModApiUtil::InitializeClient(L, top);
7880
ModApiClient::Initialize(L, top);

0 commit comments

Comments
 (0)