Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minetest.settings to CSM API and allow CSMs to provide settingtypes.txt #12131

Merged
merged 8 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builtin/client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ dofile(commonpath .. "after.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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
preview_csm_test_setting (Test CSM setting) bool false
8 changes: 8 additions & 0 deletions doc/client_lua_api.txt
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,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
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,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
Original file line number Diff line number Diff line change
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