Organize builtin into subdirectories
[oweals/minetest.git] / src / script / scripting_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 "scripting_mainmenu.h"
21 #include "mods.h"
22 #include "porting.h"
23 #include "log.h"
24 #include "filesys.h"
25 #include "cpp_api/s_internal.h"
26 #include "lua_api/l_base.h"
27 #include "lua_api/l_mainmenu.h"
28 #include "lua_api/l_util.h"
29 #include "lua_api/l_settings.h"
30
31 extern "C" {
32 #include "lualib.h"
33 }
34
35 #define MAINMENU_NUM_ASYNC_THREADS 4
36
37
38 MainMenuScripting::MainMenuScripting(GUIEngine* guiengine)
39 {
40         setGuiEngine(guiengine);
41
42         //TODO add security
43
44         luaL_openlibs(getStack());
45
46         SCRIPTAPI_PRECHECKHEADER
47
48         lua_newtable(L);
49         lua_setglobal(L, "engine");
50         lua_getglobal(L, "engine");
51         int top = lua_gettop(L);
52
53         lua_pushstring(L, DIR_DELIM);
54         lua_setglobal(L, "DIR_DELIM");
55
56         lua_newtable(L);
57         lua_setglobal(L, "gamedata");
58
59         // Initialize our lua_api modules
60         initializeModApi(L, top);
61         lua_pop(L, 1);
62
63         // Push builtin initialization type
64         lua_pushstring(L, "mainmenu");
65         lua_setglobal(L, "INIT");
66
67         infostream << "SCRIPTAPI: Initialized main menu modules" << std::endl;
68 }
69
70 /******************************************************************************/
71 void MainMenuScripting::initializeModApi(lua_State *L, int top)
72 {
73         // Initialize mod API modules
74         ModApiMainMenu::Initialize(L, top);
75         ModApiUtil::Initialize(L, top);
76
77         // Register reference classes (userdata)
78         LuaSettings::Register(L);
79
80         // Register functions to async environment
81         ModApiMainMenu::InitializeAsync(asyncEngine);
82         ModApiUtil::InitializeAsync(asyncEngine);
83
84         // Initialize async environment
85         //TODO possibly make number of async threads configurable
86         asyncEngine.initialize(MAINMENU_NUM_ASYNC_THREADS);
87 }
88
89 /******************************************************************************/
90 void MainMenuScripting::step() {
91         asyncEngine.step(getStack(), m_errorhandler);
92 }
93
94 /******************************************************************************/
95 unsigned int MainMenuScripting::queueAsync(std::string serialized_func,
96                 std::string serialized_param) {
97         return asyncEngine.queueAsyncJob(serialized_func, serialized_param);
98 }
99