diff --git a/res/layouts/pages/settings.xml.lua b/res/layouts/pages/settings.xml.lua index 8b41491a5..453583518 100644 --- a/res/layouts/pages/settings.xml.lua +++ b/res/layouts/pages/settings.xml.lua @@ -1,7 +1,7 @@ function on_open() document.langs_btn.text = string.format( "%s: %s", gui.str("Language", "settings"), - gui.get_locales_info()[core.get_setting("ui.language")].name + gui.get_locales_info()[app.get_setting("ui.language")].name ) sections = RadioGroup({ audio=document.s_aud, diff --git a/res/layouts/pages/settings_audio.xml.lua b/res/layouts/pages/settings_audio.xml.lua index f8a23baa9..c9b4be4b6 100644 --- a/res/layouts/pages/settings_audio.xml.lua +++ b/res/layouts/pages/settings_audio.xml.lua @@ -1,25 +1,25 @@ function create_setting(id, name, step, postfix) - local info = core.get_setting_info(id) + local info = app.get_setting_info(id) postfix = postfix or "" document.root:add(gui.template("track_setting", { id=id, name=gui.str(name, "settings"), - value=core.get_setting(id), + value=app.get_setting(id), min=info.min, max=info.max, step=step, postfix=postfix })) - update_setting(core.get_setting(id), id, name, postfix) + update_setting(app.get_setting(id), id, name, postfix) end function update_setting(x, id, name, postfix) - core.set_setting(id, x) + app.set_setting(id, x) -- updating label document[id..".L"].text = string.format( "%s: %s%s", gui.str(name, "settings"), - core.str_setting(id), + app.str_setting(id), postfix ) end @@ -54,7 +54,7 @@ function on_open() create_setting("audio.volume-music", "Music", 0.01) document.root:add("") document.root:add("".. + "", core.get_setting("display.window-mode")) + "", app.get_setting("display.window-mode")) ) create_checkbox("camera.shaking", "Camera Shaking") diff --git a/res/layouts/pages/settings_graphics.xml.lua b/res/layouts/pages/settings_graphics.xml.lua index d61057799..3b7f7b13a 100644 --- a/res/layouts/pages/settings_graphics.xml.lua +++ b/res/layouts/pages/settings_graphics.xml.lua @@ -1,12 +1,12 @@ function create_setting(id, name, step, postfix, tooltip, changeonrelease) - local info = core.get_setting_info(id) + local info = app.get_setting_info(id) postfix = postfix or "" tooltip = tooltip or "" changeonrelease = changeonrelease or "" document.root:add(gui.template("track_setting", { id=id, name=gui.str(name, "settings"), - value=core.get_setting(id), + value=app.get_setting(id), min=info.min, max=info.max, step=step, @@ -14,15 +14,15 @@ function create_setting(id, name, step, postfix, tooltip, changeonrelease) tooltip=tooltip, changeonrelease=changeonrelease })) - update_setting(core.get_setting(id), id, name, postfix) + update_setting(app.get_setting(id), id, name, postfix) end function update_setting(x, id, name, postfix) -- updating label document[id..".L"].text = string.format( - "%s: %s%s", - gui.str(name, "settings"), - core.str_setting(id), + "%s: %s%s", + gui.str(name, "settings"), + app.str_setting(id), postfix ) end @@ -30,8 +30,8 @@ end function create_checkbox(id, name, tooltip) tooltip = tooltip or '' document.root:add(string.format( - "%s", - id, core.str_setting(id), gui.str(tooltip, "settings"), gui.str(name, "settings") + "%s", + id, app.str_setting(id), gui.str(tooltip, "settings"), gui.str(name, "settings") )) end diff --git a/res/layouts/pages/settings_reset.xml.lua b/res/layouts/pages/settings_reset.xml.lua index a8ca2a9fc..59583eefa 100644 --- a/res/layouts/pages/settings_reset.xml.lua +++ b/res/layouts/pages/settings_reset.xml.lua @@ -11,7 +11,7 @@ function reset(category) end function reset_setting(name) - core.set_setting(name, core.get_setting_info(name).def) + app.set_setting(name, app.get_setting_info(name).def) end function reset_audio() diff --git a/res/layouts/pages/worlds.xml.lua b/res/layouts/pages/worlds.xml.lua index 1cbcd53db..748bdfb0b 100644 --- a/res/layouts/pages/worlds.xml.lua +++ b/res/layouts/pages/worlds.xml.lua @@ -1,7 +1,7 @@ function on_open() local worlds = world.get_list() for _, info in ipairs(worlds) do - local major, minor = core.get_version() + local major, minor = app.get_version() if info.version[1] > major or info.version[2] > minor then info.versionColor = "#A02010" else diff --git a/res/layouts/templates/language.xml b/res/layouts/templates/language.xml index fea34b313..692744c96 100644 --- a/res/layouts/templates/language.xml +++ b/res/layouts/templates/language.xml @@ -1,4 +1,4 @@ diff --git a/res/layouts/templates/track_setting.xml b/res/layouts/templates/track_setting.xml index 519ba334b..f01b8f842 100644 --- a/res/layouts/templates/track_setting.xml +++ b/res/layouts/templates/track_setting.xml @@ -2,6 +2,6 @@ diff --git a/res/scripts/hud.lua b/res/scripts/hud.lua index 59d8de36a..322f03db5 100644 --- a/res/scripts/hud.lua +++ b/res/scripts/hud.lua @@ -29,7 +29,7 @@ local function configure_SSAO() end events.on("core:setting.graphics.ssao.set", update_ssao_quality) - update_ssao_quality(core.get_setting("graphics.ssao")) + update_ssao_quality(__vc_app.get_setting("graphics.ssao")) end local function update_hand() diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 2ba6f73b2..d7ee98b97 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -1,4 +1,4 @@ -local enable_experimental = core.get_setting("debug.enable-experimental") +local enable_experimental = __vc_app.get_setting("debug.enable-experimental") ------------------------------------------------ ------ Extended kit of standard functions ------ @@ -24,12 +24,6 @@ function tb_frame_tostring(frame) return s end -local core_set_setting = core.set_setting -function core.set_setting(name, value, ...) - core_set_setting(name, value, ...) - events.emit("core:setting."..name..".set", value) -end - local __vc__app_script_coroutine local function complete_app_lib(app) @@ -37,6 +31,7 @@ local function complete_app_lib(app) local __core_reset_content = core.reset_content local __core_reconfig_packs = core.reconfig_packs local __app_tick = coroutine.yield + local __app_set_setting = app.set_setting core.load_content = nil core.reset_content = nil @@ -48,10 +43,12 @@ local function complete_app_lib(app) app.close_world = core.close_world app.reopen_world = core.reopen_world app.delete_world = core.delete_world - app.get_setting = core.get_setting - app.set_setting = core.set_setting + + app.set_setting = function(name, value, ...) + __app_set_setting(name, value, ...) + events.emit("core:setting."..name..".set", value) + end app.tick = __app_tick - app.get_setting_info = core.get_setting_info local function call_in_app_script_co(func, ...) if coroutine.running() ~= __vc__app_script_coroutine then diff --git a/src/logic/scripting/lua/libs/libapp.cpp b/src/logic/scripting/lua/libs/libapp.cpp index 4cc2c302d..b330e8a90 100644 --- a/src/logic/scripting/lua/libs/libapp.cpp +++ b/src/logic/scripting/lua/libs/libapp.cpp @@ -1,6 +1,7 @@ #include "api_lua.hpp" #include "io/io.hpp" +#include "io/settings_io.hpp" #include "io/devices/MemoryDevice.hpp" #include "logic/scripting/scripting.hpp" #include "content/ContentControl.hpp" @@ -113,6 +114,73 @@ static int l_set_title(lua::State* L) { return 0; } +/// @brief Get a setting value +/// @param name The name of the setting +/// @return The value of the setting +static int l_get_setting(lua::State* L) { + auto name = lua::require_string(L, 1); + const auto value = engine->getSettingsHandler().getValue(name); + return lua::pushvalue(L, value); +} + +/// @brief Set a setting value +/// @param name The name of the setting +/// @param value The new value for the setting +static int l_set_setting(lua::State* L) { + auto name = lua::require_string(L, 1); + const auto value = lua::tovalue(L, 2); + engine->getSettingsHandler().setValue(name, value); + return 0; +} + +/// @brief Convert a setting value to a string +/// @param name The name of the setting +/// @return The string representation of the setting value +static int l_str_setting(lua::State* L) { + auto name = lua::require_string(L, 1); + const auto string = engine->getSettingsHandler().toString(name); + return lua::pushstring(L, string); +} + +/// @brief Get information about a setting +/// @param name The name of the setting +/// @return A table with information about the setting +static int l_get_setting_info(lua::State* L) { + auto name = lua::require_string(L, 1); + auto setting = engine->getSettingsHandler().getSetting(name); + lua::createtable(L, 0, 1); + if (auto number = dynamic_cast(setting)) { + lua::pushnumber(L, number->getMin()); + lua::setfield(L, "min"); + lua::pushnumber(L, number->getMax()); + lua::setfield(L, "max"); + lua::pushnumber(L, number->getDefault()); + lua::setfield(L, "def"); + return 1; + } + if (auto integer = dynamic_cast(setting)) { + lua::pushinteger(L, integer->getMin()); + lua::setfield(L, "min"); + lua::pushinteger(L, integer->getMax()); + lua::setfield(L, "max"); + lua::pushinteger(L, integer->getDefault()); + lua::setfield(L, "def"); + return 1; + } + if (auto boolean = dynamic_cast(setting)) { + lua::pushboolean(L, boolean->getDefault()); + lua::setfield(L, "def"); + return 1; + } + if (auto string = dynamic_cast(setting)) { + lua::pushstring(L, string->getDefault()); + lua::setfield(L, "def"); + return 1; + } + lua::pop(L); + throw std::runtime_error("unsupported setting type"); +} + static int l_open_folder(lua::State* L) { platform::open_folder(io::resolve(lua::require_string(L, 1))); return 0; @@ -147,6 +215,10 @@ const luaL_Reg applib[] = { {"set_title", lua::wrap}, {"open_folder", lua::wrap}, {"open_url", lua::wrap}, + {"get_setting", lua::wrap}, + {"set_setting", lua::wrap}, + {"str_setting", lua::wrap}, + {"get_setting_info", lua::wrap}, // for other functions see libcore.cpp and stdlib.lua {nullptr, nullptr} }; diff --git a/src/logic/scripting/lua/libs/libcore.cpp b/src/logic/scripting/lua/libs/libcore.cpp index 6f3056ea4..be7ee614e 100644 --- a/src/logic/scripting/lua/libs/libcore.cpp +++ b/src/logic/scripting/lua/libs/libcore.cpp @@ -1,7 +1,3 @@ -#include -#include -#include - #include "api_lua.hpp" #include "constants.hpp" #include "assets/Assets.hpp" @@ -10,7 +6,6 @@ #include "engine/Engine.hpp" #include "engine/EnginePaths.hpp" #include "io/io.hpp" -#include "io/settings_io.hpp" #include "frontend/menu.hpp" #include "frontend/screens/MenuScreen.hpp" #include "graphics/core/Texture.hpp" @@ -22,6 +17,10 @@ #include "world/generator/WorldGenerator.hpp" #include "window/Window.hpp" +#include +#include +#include + using namespace scripting; static int l_load_content(lua::State* L) { @@ -162,73 +161,6 @@ static int l_reconfig_packs(lua::State* L) { return 0; } -/// @brief Get a setting value -/// @param name The name of the setting -/// @return The value of the setting -static int l_get_setting(lua::State* L) { - auto name = lua::require_string(L, 1); - const auto value = engine->getSettingsHandler().getValue(name); - return lua::pushvalue(L, value); -} - -/// @brief Set a setting value -/// @param name The name of the setting -/// @param value The new value for the setting -static int l_set_setting(lua::State* L) { - auto name = lua::require_string(L, 1); - const auto value = lua::tovalue(L, 2); - engine->getSettingsHandler().setValue(name, value); - return 0; -} - -/// @brief Convert a setting value to a string -/// @param name The name of the setting -/// @return The string representation of the setting value -static int l_str_setting(lua::State* L) { - auto name = lua::require_string(L, 1); - const auto string = engine->getSettingsHandler().toString(name); - return lua::pushstring(L, string); -} - -/// @brief Get information about a setting -/// @param name The name of the setting -/// @return A table with information about the setting -static int l_get_setting_info(lua::State* L) { - auto name = lua::require_string(L, 1); - auto setting = engine->getSettingsHandler().getSetting(name); - lua::createtable(L, 0, 1); - if (auto number = dynamic_cast(setting)) { - lua::pushnumber(L, number->getMin()); - lua::setfield(L, "min"); - lua::pushnumber(L, number->getMax()); - lua::setfield(L, "max"); - lua::pushnumber(L, number->getDefault()); - lua::setfield(L, "def"); - return 1; - } - if (auto integer = dynamic_cast(setting)) { - lua::pushinteger(L, integer->getMin()); - lua::setfield(L, "min"); - lua::pushinteger(L, integer->getMax()); - lua::setfield(L, "max"); - lua::pushinteger(L, integer->getDefault()); - lua::setfield(L, "def"); - return 1; - } - if (auto boolean = dynamic_cast(setting)) { - lua::pushboolean(L, boolean->getDefault()); - lua::setfield(L, "def"); - return 1; - } - if (auto string = dynamic_cast(setting)) { - lua::pushstring(L, string->getDefault()); - lua::setfield(L, "def"); - return 1; - } - lua::pop(L); - throw std::runtime_error("unsupported setting type"); -} - /// @brief Quit the game static int l_quit(lua::State*) { engine->quit(); @@ -278,10 +210,6 @@ const luaL_Reg corelib[] = { {"close_world", lua::wrap}, {"delete_world", lua::wrap}, {"reconfig_packs", lua::wrap}, - {"get_setting", lua::wrap}, - {"set_setting", lua::wrap}, - {"str_setting", lua::wrap}, - {"get_setting_info", lua::wrap}, {"quit", lua::wrap}, {"capture_output", lua::wrap}, {nullptr, nullptr} diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index eef391b54..2a9eb59cb 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -66,6 +66,8 @@ static void create_libs(State* L, StateType stateType) { if (stateType == StateType::SCRIPT) { openlib(L, "app", applib); + lua::getglobal(L, "app"); + lua::setglobal(L, "__vc_app"); } else if (stateType == StateType::BASE) { openlib(L, "__vc_app", applib); }