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