remove settings-related functions from core

This commit is contained in:
MihailRis 2026-01-07 02:39:29 +03:00
parent 5260062181
commit a6301c8837
14 changed files with 119 additions and 120 deletions

View file

@ -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,

View file

@ -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("<label context='settings'>@Microphone</label>")
document.root:add("<select id='input_device_select' "..
"onselect='function(opt) core.set_setting(\"audio.input-device\", opt) end'/>")
"onselect='function(opt) app.set_setting(\"audio.input-device\", opt) end'/>")
document.root:add("<container id='input_volume_outer' color='#000000' size='4'>"
.."<container id='input_volume_inner' color='#00FF00FF' pos='1' size='2'/>"
.."</container>")

View file

@ -40,12 +40,12 @@ function refresh_sensitivity()
document.sensitivity_label.text = string.format(
"%s: %s",
gui.str("Mouse Sensitivity", "settings"),
core.str_setting("camera.sensitivity")
app.str_setting("camera.sensitivity")
)
end
function change_sensitivity(val)
core.set_setting("camera.sensitivity", val)
app.set_setting("camera.sensitivity", val)
refresh_sensitivity()
end
@ -93,7 +93,7 @@ function refresh_binding_marks()
end
function on_open()
document.sensitivity_track.value = core.get_setting("camera.sensitivity")
document.sensitivity_track.value = app.get_setting("camera.sensitivity")
refresh_sensitivity()
local panel = document.bindings_panel

View file

@ -10,14 +10,14 @@ tostring_overrides["display.framerate"] = function(x)
end
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,
@ -25,7 +25,7 @@ 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)
@ -34,13 +34,13 @@ function update_setting(x, id, name, postfix)
if func then
str = func(x)
else
str = core.str_setting(id)
str = app.str_setting(id)
end
-- updating label
document[id..".L"].text = string.format(
"%s: %s%s",
gui.str(name, "settings"),
str,
"%s: %s%s",
gui.str(name, "settings"),
str,
postfix
)
end
@ -48,8 +48,8 @@ end
function create_checkbox(id, name, tooltip)
tooltip = tooltip or ''
document.root:add(string.format(
"<checkbox consumer='function(x) core.set_setting(\"%s\", x) end' checked='%s' tooltip='%s'>%s</checkbox>",
id, core.str_setting(id), gui.str(tooltip, "settings"), gui.str(name, "settings")
"<checkbox consumer='function(x) app.set_setting(\"%s\", x) end' checked='%s' tooltip='%s'>%s</checkbox>",
id, app.str_setting(id), gui.str(tooltip, "settings"), gui.str(name, "settings")
))
end
@ -59,11 +59,11 @@ function on_open()
create_setting("display.framerate", "Framerate", 1, "", "", true)
document.root:add(string.format(
"<select context='settings' onselect='function(opt) core.set_setting(\"display.window-mode\", tonumber(opt)) end' selected='%s'>"..
"<select context='settings' onselect='function(opt) app.set_setting(\"display.window-mode\", tonumber(opt)) end' selected='%s'>"..
"<option value='0'>@Windowed</option>"..
"<option value='1'>@Fullscreen</option>"..
"<option value='2'>@Borderless</option>"..
"</select>", core.get_setting("display.window-mode"))
"</select>", app.get_setting("display.window-mode"))
)
create_checkbox("camera.shaking", "Camera Shaking")

View file

@ -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(
"<checkbox consumer='function(x) core.set_setting(\"%s\", x) end' checked='%s' tooltip='%s'>%s</checkbox>",
id, core.str_setting(id), gui.str(tooltip, "settings"), gui.str(name, "settings")
"<checkbox consumer='function(x) app.set_setting(\"%s\", x) end' checked='%s' tooltip='%s'>%s</checkbox>",
id, app.str_setting(id), gui.str(tooltip, "settings"), gui.str(name, "settings")
))
end

View file

@ -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()

View file

@ -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

View file

@ -1,4 +1,4 @@
<button
onclick='core.set_setting("ui.language", "%{id}") menu:back()'>
onclick='app.set_setting("ui.language", "%{id}") menu:back()'>
%{name}
</button>

View file

@ -2,6 +2,6 @@
<label id='%{id}.L' margin='0,3,0,0'>%{name}: %{value}%{postfix}</label>
<trackbar
value='%{value}' min='%{min}' max='%{max}' step='%{step}' tooltip='%{tooltip}'
consumer='function(x) core.set_setting("%{id}", x) end' change-on-release='%{changeonrelease}'
consumer='function(x) app.set_setting("%{id}", x) end' change-on-release='%{changeonrelease}'
sub-consumer='function(x) update_setting(x, "%{id}", "%{name}", "%{postfix}") end'/>
</panel>

View file

@ -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()

View file

@ -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

View file

@ -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<NumberSetting*>(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<IntegerSetting*>(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<FlagSetting*>(setting)) {
lua::pushboolean(L, boolean->getDefault());
lua::setfield(L, "def");
return 1;
}
if (auto string = dynamic_cast<StringSetting*>(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<l_set_title>},
{"open_folder", lua::wrap<l_open_folder>},
{"open_url", lua::wrap<l_open_url>},
{"get_setting", lua::wrap<l_get_setting>},
{"set_setting", lua::wrap<l_set_setting>},
{"str_setting", lua::wrap<l_str_setting>},
{"get_setting_info", lua::wrap<l_get_setting_info>},
// for other functions see libcore.cpp and stdlib.lua
{nullptr, nullptr}
};

View file

@ -1,7 +1,3 @@
#include <memory>
#include <vector>
#include <sstream>
#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 <memory>
#include <vector>
#include <sstream>
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<NumberSetting*>(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<IntegerSetting*>(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<FlagSetting*>(setting)) {
lua::pushboolean(L, boolean->getDefault());
lua::setfield(L, "def");
return 1;
}
if (auto string = dynamic_cast<StringSetting*>(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<l_close_world>},
{"delete_world", lua::wrap<l_delete_world>},
{"reconfig_packs", lua::wrap<l_reconfig_packs>},
{"get_setting", lua::wrap<l_get_setting>},
{"set_setting", lua::wrap<l_set_setting>},
{"str_setting", lua::wrap<l_str_setting>},
{"get_setting_info", lua::wrap<l_get_setting_info>},
{"quit", lua::wrap<l_quit>},
{"capture_output", lua::wrap<l_capture_output>},
{nullptr, nullptr}

View file

@ -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);
}