1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Mainmenu: Move core.on_before_close to s_mainmenu like other callbacks, and doc

This commit is contained in:
Desour 2025-04-20 21:57:38 +02:00 committed by sfan5
parent 2c83c67b7d
commit 4a8f84b259
6 changed files with 31 additions and 20 deletions

View file

@ -21,7 +21,6 @@ function check_cache_age(key, max_age)
end
function core.on_before_close()
-- called before the menu is closed, either exit or to join a game
cache_settings:write()
end

View file

@ -25,6 +25,8 @@ Callbacks
* `core.event_handler(event)`
* `event`: `"MenuQuit"`, `"KeyEnter"`, `"ExitButton"`, `"EditBoxEnter"` or
`"FullscreenChange"`
* `core.on_before_close()`: called before the menu is closed, either to exit or
to join a game
Gamedata

View file

@ -34,7 +34,7 @@ void ScriptApiMainMenu::handleMainMenuEvent(const std::string &text)
lua_getfield(L, -1, "event_handler");
lua_remove(L, -2); // Remove core
if (lua_isnil(L, -1)) {
lua_pop(L, 1); // Pop event_handler
lua_pop(L, 2); // Pop event_handler, error handler
return;
}
luaL_checktype(L, -1, LUA_TFUNCTION);
@ -56,7 +56,7 @@ void ScriptApiMainMenu::handleMainMenuButtons(const StringMap &fields)
lua_getfield(L, -1, "button_handler");
lua_remove(L, -2); // Remove core
if (lua_isnil(L, -1)) {
lua_pop(L, 1); // Pop button handler
lua_pop(L, 2); // Pop button handler, error handler
return;
}
luaL_checktype(L, -1, LUA_TFUNCTION);
@ -76,3 +76,25 @@ void ScriptApiMainMenu::handleMainMenuButtons(const StringMap &fields)
PCALL_RES(lua_pcall(L, 1, 0, error_handler));
lua_pop(L, 1); // Pop error handler
}
void ScriptApiMainMenu::beforeClose()
{
SCRIPTAPI_PRECHECKHEADER
int error_handler = PUSH_ERROR_HANDLER(L);
// Get handler function
lua_getglobal(L, "core");
lua_getfield(L, -1, "on_before_close");
lua_remove(L, -2); // Remove core
if (lua_isnil(L, -1)) {
lua_pop(L, 2); // Pop callback, error handler
return;
}
luaL_checktype(L, -1, LUA_TFUNCTION);
// Call it
PCALL_RES(lua_pcall(L, 0, 0, error_handler));
lua_pop(L, 1); // Pop error handler
}

View file

@ -27,4 +27,9 @@ public:
* @param fields data in field format
*/
void handleMainMenuButtons(const StringMap &fields);
/**
* Called before the menu is closed, either to exit or to join a game
*/
void beforeClose();
};

View file

@ -114,20 +114,6 @@ bool MainMenuScripting::checkPathAccess(const std::string &abs_path, bool write_
return !write_required;
}
void MainMenuScripting::beforeClose()
{
SCRIPTAPI_PRECHECKHEADER
int error_handler = PUSH_ERROR_HANDLER(L);
lua_getglobal(L, "core");
lua_getfield(L, -1, "on_before_close");
PCALL_RES(lua_pcall(L, 0, 0, error_handler));
lua_pop(L, 2); // Pop core, error handler
}
void MainMenuScripting::step()
{
asyncEngine.step(getStack());

View file

@ -24,9 +24,6 @@ public:
// Global step handler to pass back async events
void step();
// Calls core.on_before_close()
void beforeClose();
// Pass async events from engine to async threads
u32 queueAsync(std::string &&serialized_func,
std::string &&serialized_param);