mirror of
https://github.com/luanti-org/luanti.git
synced 2025-09-05 18:41:05 +00:00
In-game settings menu using separate Lua environment (#15614)
This commit is contained in:
parent
3cb07d5fb6
commit
eeb6cab4c4
48 changed files with 652 additions and 290 deletions
|
@ -29,11 +29,14 @@ set(common_SCRIPT_LUA_API_SRCS
|
|||
set(client_SCRIPT_LUA_API_SRCS
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_camera.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_client.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_client_common.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_client_sound.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_localplayer.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_mainmenu.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_mainmenu_sound.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_menu_common.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_minimap.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_particles_local.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_pause_menu.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_storage.cpp
|
||||
PARENT_SCOPE)
|
||||
|
|
|
@ -127,21 +127,6 @@ int ModApiClient::l_get_player_names(lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
// show_formspec(formspec)
|
||||
int ModApiClient::l_show_formspec(lua_State *L)
|
||||
{
|
||||
if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
|
||||
return 0;
|
||||
|
||||
ClientEvent *event = new ClientEvent();
|
||||
event->type = CE_SHOW_LOCAL_FORMSPEC;
|
||||
event->show_formspec.formname = new std::string(luaL_checkstring(L, 1));
|
||||
event->show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
|
||||
getClient(L)->pushToEventQueue(event);
|
||||
lua_pushboolean(L, true);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// disconnect()
|
||||
int ModApiClient::l_disconnect(lua_State *L)
|
||||
{
|
||||
|
@ -325,7 +310,6 @@ void ModApiClient::Initialize(lua_State *L, int top)
|
|||
API_FCT(send_chat_message);
|
||||
API_FCT(clear_out_chat_queue);
|
||||
API_FCT(get_player_names);
|
||||
API_FCT(show_formspec);
|
||||
API_FCT(gettext);
|
||||
API_FCT(get_node_or_nil);
|
||||
API_FCT(disconnect);
|
||||
|
|
|
@ -33,9 +33,6 @@ private:
|
|||
// get_player_names()
|
||||
static int l_get_player_names(lua_State *L);
|
||||
|
||||
// show_formspec(name, formspec)
|
||||
static int l_show_formspec(lua_State *L);
|
||||
|
||||
// disconnect()
|
||||
static int l_disconnect(lua_State *L);
|
||||
|
||||
|
|
33
src/script/lua_api/l_client_common.cpp
Normal file
33
src/script/lua_api/l_client_common.cpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
// Luanti
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
||||
// Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
|
||||
// Copyright (C) 2025 grorp
|
||||
|
||||
#include "l_client_common.h"
|
||||
|
||||
#include "client/client.h"
|
||||
#include "client/clientevent.h"
|
||||
#include "cpp_api/s_base.h"
|
||||
#include "lua_api/l_internal.h"
|
||||
|
||||
|
||||
// show_formspec(formspec)
|
||||
int ModApiClientCommon::l_show_formspec(lua_State *L)
|
||||
{
|
||||
ClientEvent *event = new ClientEvent();
|
||||
event->type = getScriptApiBase(L)->getType() == ScriptingType::PauseMenu
|
||||
? CE_SHOW_PAUSE_MENU_FORMSPEC
|
||||
: CE_SHOW_CSM_FORMSPEC;
|
||||
event->show_formspec.formname = new std::string(luaL_checkstring(L, 1));
|
||||
event->show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
|
||||
getClient(L)->pushToEventQueue(event);
|
||||
lua_pushboolean(L, true);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void ModApiClientCommon::Initialize(lua_State *L, int top)
|
||||
{
|
||||
API_FCT(show_formspec);
|
||||
}
|
19
src/script/lua_api/l_client_common.h
Normal file
19
src/script/lua_api/l_client_common.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
// Luanti
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
||||
// Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
|
||||
// Copyright (C) 2025 grorp
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "lua_api/l_base.h"
|
||||
|
||||
class ModApiClientCommon : public ModApiBase
|
||||
{
|
||||
private:
|
||||
// show_formspec(name, formspec)
|
||||
static int l_show_formspec(lua_State *L);
|
||||
|
||||
public:
|
||||
static void Initialize(lua_State *L, int top);
|
||||
};
|
|
@ -875,27 +875,6 @@ int ModApiMainMenu::l_download_file(lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
int ModApiMainMenu::l_get_video_drivers(lua_State *L)
|
||||
{
|
||||
auto drivers = RenderingEngine::getSupportedVideoDrivers();
|
||||
|
||||
lua_newtable(L);
|
||||
for (u32 i = 0; i != drivers.size(); i++) {
|
||||
auto &info = RenderingEngine::getVideoDriverInfo(drivers[i]);
|
||||
|
||||
lua_newtable(L);
|
||||
lua_pushstring(L, info.name.c_str());
|
||||
lua_setfield(L, -2, "name");
|
||||
lua_pushstring(L, info.friendly_name.c_str());
|
||||
lua_setfield(L, -2, "friendly_name");
|
||||
|
||||
lua_rawseti(L, -2, i + 1);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
int ModApiMainMenu::l_get_language(lua_State *L)
|
||||
{
|
||||
|
@ -907,16 +886,6 @@ int ModApiMainMenu::l_get_language(lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
int ModApiMainMenu::l_gettext(lua_State *L)
|
||||
{
|
||||
const char *srctext = luaL_checkstring(L, 1);
|
||||
const char *text = *srctext ? gettext(srctext) : "";
|
||||
lua_pushstring(L, text);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
int ModApiMainMenu::l_get_window_info(lua_State *L)
|
||||
{
|
||||
|
@ -949,14 +918,6 @@ int ModApiMainMenu::l_get_window_info(lua_State *L)
|
|||
}
|
||||
|
||||
/******************************************************************************/
|
||||
int ModApiMainMenu::l_get_active_driver(lua_State *L)
|
||||
{
|
||||
auto drivertype = RenderingEngine::get_video_driver()->getDriverType();
|
||||
lua_pushstring(L, RenderingEngine::getVideoDriverInfo(drivertype).name.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int ModApiMainMenu::l_get_active_renderer(lua_State *L)
|
||||
{
|
||||
lua_pushstring(L, RenderingEngine::get_video_driver()->getName());
|
||||
|
@ -980,14 +941,6 @@ int ModApiMainMenu::l_get_active_irrlicht_device(lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
int ModApiMainMenu::l_irrlicht_device_supports_touch(lua_State *L)
|
||||
{
|
||||
lua_pushboolean(L, RenderingEngine::get_raw_device()->supportsTouchEvents());
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
|
||||
{
|
||||
|
@ -1122,13 +1075,9 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
|
|||
API_FCT(show_path_select_dialog);
|
||||
API_FCT(download_file);
|
||||
API_FCT(get_language);
|
||||
API_FCT(gettext);
|
||||
API_FCT(get_video_drivers);
|
||||
API_FCT(get_window_info);
|
||||
API_FCT(get_active_driver);
|
||||
API_FCT(get_active_renderer);
|
||||
API_FCT(get_active_irrlicht_device);
|
||||
API_FCT(irrlicht_device_supports_touch);
|
||||
API_FCT(get_min_supp_proto);
|
||||
API_FCT(get_max_supp_proto);
|
||||
API_FCT(get_formspec_version);
|
||||
|
@ -1165,5 +1114,4 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
|
|||
API_FCT(get_max_supp_proto);
|
||||
API_FCT(get_formspec_version);
|
||||
API_FCT(get_language);
|
||||
API_FCT(gettext);
|
||||
}
|
||||
|
|
|
@ -53,8 +53,6 @@ private:
|
|||
|
||||
static int l_get_language(lua_State *L);
|
||||
|
||||
static int l_gettext(lua_State *L);
|
||||
|
||||
//packages
|
||||
|
||||
static int l_get_games(lua_State *L);
|
||||
|
@ -89,14 +87,10 @@ private:
|
|||
|
||||
static int l_get_window_info(lua_State *L);
|
||||
|
||||
static int l_get_active_driver(lua_State *L);
|
||||
|
||||
static int l_get_active_renderer(lua_State *L);
|
||||
|
||||
static int l_get_active_irrlicht_device(lua_State *L);
|
||||
|
||||
static int l_irrlicht_device_supports_touch(lua_State *L);
|
||||
|
||||
//filesystem
|
||||
|
||||
static int l_get_mainmenu_path(lua_State *L);
|
||||
|
@ -133,8 +127,6 @@ private:
|
|||
|
||||
static int l_download_file(lua_State *L);
|
||||
|
||||
static int l_get_video_drivers(lua_State *L);
|
||||
|
||||
//version compatibility
|
||||
static int l_get_min_supp_proto(lua_State *L);
|
||||
|
||||
|
|
49
src/script/lua_api/l_menu_common.cpp
Normal file
49
src/script/lua_api/l_menu_common.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
// Luanti
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (C) 2013 sapier
|
||||
// Copyright (C) 2025 grorp
|
||||
|
||||
#include "l_menu_common.h"
|
||||
|
||||
#include "client/renderingengine.h"
|
||||
#include "gettext.h"
|
||||
#include "lua_api/l_internal.h"
|
||||
|
||||
|
||||
int ModApiMenuCommon::l_gettext(lua_State *L)
|
||||
{
|
||||
const char *srctext = luaL_checkstring(L, 1);
|
||||
const char *text = *srctext ? gettext(srctext) : "";
|
||||
lua_pushstring(L, text);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int ModApiMenuCommon::l_get_active_driver(lua_State *L)
|
||||
{
|
||||
auto drivertype = RenderingEngine::get_video_driver()->getDriverType();
|
||||
lua_pushstring(L, RenderingEngine::getVideoDriverInfo(drivertype).name.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int ModApiMenuCommon::l_irrlicht_device_supports_touch(lua_State *L)
|
||||
{
|
||||
lua_pushboolean(L, RenderingEngine::get_raw_device()->supportsTouchEvents());
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void ModApiMenuCommon::Initialize(lua_State *L, int top)
|
||||
{
|
||||
API_FCT(gettext);
|
||||
API_FCT(get_active_driver);
|
||||
API_FCT(irrlicht_device_supports_touch);
|
||||
}
|
||||
|
||||
|
||||
void ModApiMenuCommon::InitializeAsync(lua_State *L, int top)
|
||||
{
|
||||
API_FCT(gettext);
|
||||
}
|
20
src/script/lua_api/l_menu_common.h
Normal file
20
src/script/lua_api/l_menu_common.h
Normal file
|
@ -0,0 +1,20 @@
|
|||
// Luanti
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (C) 2013 sapier
|
||||
// Copyright (C) 2025 grorp
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "l_base.h"
|
||||
|
||||
class ModApiMenuCommon: public ModApiBase
|
||||
{
|
||||
private:
|
||||
static int l_gettext(lua_State *L);
|
||||
static int l_get_active_driver(lua_State *L);
|
||||
static int l_irrlicht_device_supports_touch(lua_State *L);
|
||||
|
||||
public:
|
||||
static void Initialize(lua_State *L, int top);
|
||||
static void InitializeAsync(lua_State *L, int top);
|
||||
};
|
28
src/script/lua_api/l_pause_menu.cpp
Normal file
28
src/script/lua_api/l_pause_menu.cpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Luanti
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (C) 2025 grorp
|
||||
|
||||
#include "l_pause_menu.h"
|
||||
#include "gui/mainmenumanager.h"
|
||||
#include "lua_api/l_internal.h"
|
||||
|
||||
|
||||
int ModApiPauseMenu::l_show_keys_menu(lua_State *L)
|
||||
{
|
||||
g_gamecallback->keyConfig();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int ModApiPauseMenu::l_show_touchscreen_layout(lua_State *L)
|
||||
{
|
||||
g_gamecallback->touchscreenLayout();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void ModApiPauseMenu::Initialize(lua_State *L, int top)
|
||||
{
|
||||
API_FCT(show_keys_menu);
|
||||
API_FCT(show_touchscreen_layout);
|
||||
}
|
17
src/script/lua_api/l_pause_menu.h
Normal file
17
src/script/lua_api/l_pause_menu.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
// Luanti
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (C) 2025 grorp
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "l_base.h"
|
||||
|
||||
class ModApiPauseMenu: public ModApiBase
|
||||
{
|
||||
private:
|
||||
static int l_show_keys_menu(lua_State *L);
|
||||
static int l_show_touchscreen_layout(lua_State *L);
|
||||
|
||||
public:
|
||||
static void Initialize(lua_State *L, int top);
|
||||
};
|
|
@ -30,8 +30,9 @@
|
|||
static inline int checkSettingSecurity(lua_State* L, const std::string &name)
|
||||
{
|
||||
#if CHECK_CLIENT_BUILD()
|
||||
// Main menu is allowed everything
|
||||
if (ModApiBase::getGuiEngine(L) != nullptr)
|
||||
// Main menu and pause menu are allowed everything
|
||||
auto context = ModApiBase::getScriptApiBase(L)->getType();
|
||||
if (context == ScriptingType::MainMenu || context == ScriptingType::PauseMenu)
|
||||
return 0;
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue