]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_mainmenu.cpp
Pass a errfunc to lua_pcall to get a traceback
[minetest.git] / src / script / cpp_api / s_mainmenu.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "cpp_api/s_mainmenu.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
23
24 void ScriptApiMainMenu::setMainMenuErrorMessage(std::string errormessage)
25 {
26         SCRIPTAPI_PRECHECKHEADER
27
28         lua_getglobal(L, "gamedata");
29         int gamedata_idx = lua_gettop(L);
30         lua_pushstring(L, "errormessage");
31         lua_pushstring(L, errormessage.c_str());
32         lua_settable(L, gamedata_idx);
33         lua_pop(L, 1);
34 }
35
36 void ScriptApiMainMenu::handleMainMenuEvent(std::string text)
37 {
38         SCRIPTAPI_PRECHECKHEADER
39
40         lua_pushcfunction(L, script_error_handler);
41         int errorhandler = lua_gettop(L);
42
43         // Get handler function
44         lua_getglobal(L, "engine");
45         lua_getfield(L, -1, "event_handler");
46         lua_remove(L, -2); // Remove engine
47         if(lua_isnil(L, -1)) {
48                 lua_pop(L, 1); // Pop event_handler
49                 return;
50         }
51         luaL_checktype(L, -1, LUA_TFUNCTION);
52
53         // Call it
54         lua_pushstring(L, text.c_str());
55         if(lua_pcall(L, 1, 0, errorhandler))
56                 scriptError();
57         lua_pop(L, 1); // Pop error handler
58 }
59
60 void ScriptApiMainMenu::handleMainMenuButtons(std::map<std::string, std::string> fields)
61 {
62         SCRIPTAPI_PRECHECKHEADER
63
64         lua_pushcfunction(L, script_error_handler);
65         int errorhandler = lua_gettop(L);
66
67         // Get handler function
68         lua_getglobal(L, "engine");
69         lua_getfield(L, -1, "button_handler");
70         lua_remove(L, -2); // Remove engine
71         if(lua_isnil(L, -1)) {
72                 lua_pop(L, 1); // Pop button handler
73                 return;
74         }
75         luaL_checktype(L, -1, LUA_TFUNCTION);
76
77         // Convert fields to lua table
78         lua_newtable(L);
79         for(std::map<std::string, std::string>::const_iterator
80                 i = fields.begin(); i != fields.end(); i++){
81                 const std::string &name = i->first;
82                 const std::string &value = i->second;
83                 lua_pushstring(L, name.c_str());
84                 lua_pushlstring(L, value.c_str(), value.size());
85                 lua_settable(L, -3);
86         }
87
88         // Call it
89         if(lua_pcall(L, 1, 0, errorhandler))
90                 scriptError();
91         lua_pop(L, 1); // Pop error handler
92 }