3 Copyright (C) 2013 sapier
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.
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.
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.
20 #include "lua_api/l_mainmenu.h"
21 #include "lua_api/l_internal.h"
22 #include "common/c_content.h"
23 #include "cpp_api/s_async.h"
24 #include "guiEngine.h"
25 #include "guiMainMenu.h"
26 #include "guiKeyChangeMenu.h"
27 #include "guiPathSelectMenu.h"
32 #include "convert_json.h"
33 #include "serverlist.h"
37 #include <IFileArchive.h>
38 #include <IFileSystem.h>
39 #include "client/renderingengine.h"
42 /******************************************************************************/
43 std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
45 lua_getglobal(L, "gamedata");
47 lua_getfield(L, -1, name.c_str());
52 return luaL_checkstring(L, -1);
55 /******************************************************************************/
56 int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
58 lua_getglobal(L, "gamedata");
60 lua_getfield(L, -1, name.c_str());
62 if(lua_isnil(L, -1)) {
68 return luaL_checkinteger(L, -1);
71 /******************************************************************************/
72 int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
74 lua_getglobal(L, "gamedata");
76 lua_getfield(L, -1, name.c_str());
78 if(lua_isnil(L, -1)) {
84 return lua_toboolean(L, -1);
87 /******************************************************************************/
88 int ModApiMainMenu::l_update_formspec(lua_State *L)
90 GUIEngine* engine = getGuiEngine(L);
91 sanity_check(engine != NULL);
93 if (engine->m_startgame)
97 std::string formspec(luaL_checkstring(L, 1));
99 if (engine->m_formspecgui != 0) {
100 engine->m_formspecgui->setForm(formspec);
106 /******************************************************************************/
107 int ModApiMainMenu::l_start(lua_State *L)
109 GUIEngine* engine = getGuiEngine(L);
110 sanity_check(engine != NULL);
112 //update c++ gamedata from lua table
116 MainMenuData *data = engine->m_data;
118 data->selected_world = getIntegerData(L, "selected_world",valid) -1;
119 data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
120 data->do_reconnect = getBoolData(L, "do_reconnect", valid);
121 if (!data->do_reconnect) {
122 data->name = getTextData(L,"playername");
123 data->password = getTextData(L,"password");
124 data->address = getTextData(L,"address");
125 data->port = getTextData(L,"port");
127 data->serverdescription = getTextData(L,"serverdescription");
128 data->servername = getTextData(L,"servername");
130 //close menu next time
131 engine->m_startgame = true;
135 /******************************************************************************/
136 int ModApiMainMenu::l_close(lua_State *L)
138 GUIEngine* engine = getGuiEngine(L);
139 sanity_check(engine != NULL);
141 engine->m_kill = true;
145 /******************************************************************************/
146 int ModApiMainMenu::l_set_background(lua_State *L)
148 GUIEngine* engine = getGuiEngine(L);
149 sanity_check(engine != NULL);
151 std::string backgroundlevel(luaL_checkstring(L, 1));
152 std::string texturename(luaL_checkstring(L, 2));
154 bool tile_image = false;
156 unsigned int minsize = 16;
158 if (!lua_isnone(L, 3)) {
159 tile_image = lua_toboolean(L, 3);
162 if (!lua_isnone(L, 4)) {
163 minsize = lua_tonumber(L, 4);
166 if (backgroundlevel == "background") {
167 retval |= engine->setTexture(TEX_LAYER_BACKGROUND, texturename,
168 tile_image, minsize);
171 if (backgroundlevel == "overlay") {
172 retval |= engine->setTexture(TEX_LAYER_OVERLAY, texturename,
173 tile_image, minsize);
176 if (backgroundlevel == "header") {
177 retval |= engine->setTexture(TEX_LAYER_HEADER, texturename,
178 tile_image, minsize);
181 if (backgroundlevel == "footer") {
182 retval |= engine->setTexture(TEX_LAYER_FOOTER, texturename,
183 tile_image, minsize);
186 lua_pushboolean(L,retval);
190 /******************************************************************************/
191 int ModApiMainMenu::l_set_clouds(lua_State *L)
193 GUIEngine* engine = getGuiEngine(L);
194 sanity_check(engine != NULL);
196 bool value = lua_toboolean(L,1);
198 engine->m_clouds_enabled = value;
203 /******************************************************************************/
204 int ModApiMainMenu::l_get_textlist_index(lua_State *L)
206 // get_table_index accepts both tables and textlists
207 return l_get_table_index(L);
210 /******************************************************************************/
211 int ModApiMainMenu::l_get_table_index(lua_State *L)
213 GUIEngine* engine = getGuiEngine(L);
214 sanity_check(engine != NULL);
216 std::string tablename(luaL_checkstring(L, 1));
217 GUITable *table = engine->m_menu->getTable(tablename);
218 s32 selection = table ? table->getSelected() : 0;
221 lua_pushinteger(L, selection);
227 /******************************************************************************/
228 int ModApiMainMenu::l_get_worlds(lua_State *L)
230 std::vector<WorldSpec> worlds = getAvailableWorlds();
233 int top = lua_gettop(L);
234 unsigned int index = 1;
236 for (unsigned int i = 0; i < worlds.size(); i++)
238 lua_pushnumber(L,index);
241 int top_lvl2 = lua_gettop(L);
243 lua_pushstring(L,"path");
244 lua_pushstring(L,worlds[i].path.c_str());
245 lua_settable(L, top_lvl2);
247 lua_pushstring(L,"name");
248 lua_pushstring(L,worlds[i].name.c_str());
249 lua_settable(L, top_lvl2);
251 lua_pushstring(L,"gameid");
252 lua_pushstring(L,worlds[i].gameid.c_str());
253 lua_settable(L, top_lvl2);
255 lua_settable(L, top);
261 /******************************************************************************/
262 int ModApiMainMenu::l_get_games(lua_State *L)
264 std::vector<SubgameSpec> games = getAvailableGames();
267 int top = lua_gettop(L);
268 unsigned int index = 1;
270 for (unsigned int i = 0; i < games.size(); i++)
272 lua_pushnumber(L,index);
274 int top_lvl2 = lua_gettop(L);
276 lua_pushstring(L,"id");
277 lua_pushstring(L,games[i].id.c_str());
278 lua_settable(L, top_lvl2);
280 lua_pushstring(L,"path");
281 lua_pushstring(L,games[i].path.c_str());
282 lua_settable(L, top_lvl2);
284 lua_pushstring(L,"gamemods_path");
285 lua_pushstring(L,games[i].gamemods_path.c_str());
286 lua_settable(L, top_lvl2);
288 lua_pushstring(L,"name");
289 lua_pushstring(L,games[i].name.c_str());
290 lua_settable(L, top_lvl2);
292 lua_pushstring(L,"menuicon_path");
293 lua_pushstring(L,games[i].menuicon_path.c_str());
294 lua_settable(L, top_lvl2);
296 lua_pushstring(L,"addon_mods_paths");
298 int table2 = lua_gettop(L);
299 int internal_index=1;
300 for (std::set<std::string>::iterator iter = games[i].addon_mods_paths.begin();
301 iter != games[i].addon_mods_paths.end(); ++iter) {
302 lua_pushnumber(L,internal_index);
303 lua_pushstring(L,(*iter).c_str());
304 lua_settable(L, table2);
307 lua_settable(L, top_lvl2);
308 lua_settable(L, top);
313 /******************************************************************************/
314 int ModApiMainMenu::l_get_favorites(lua_State *L)
316 std::string listtype = "local";
318 if (!lua_isnone(L,1)) {
319 listtype = luaL_checkstring(L,1);
322 std::vector<ServerListSpec> servers;
324 if(listtype == "online") {
325 servers = ServerList::getOnline();
327 servers = ServerList::getLocal();
331 int top = lua_gettop(L);
332 unsigned int index = 1;
334 for (unsigned int i = 0; i < servers.size(); i++)
337 lua_pushnumber(L,index);
340 int top_lvl2 = lua_gettop(L);
342 if (servers[i]["clients"].asString().size()) {
343 std::string clients_raw = servers[i]["clients"].asString();
345 int numbervalue = strtol(clients_raw.c_str(),&endptr,10);
347 if ((clients_raw != "") && (*endptr == 0)) {
348 lua_pushstring(L,"clients");
349 lua_pushnumber(L,numbervalue);
350 lua_settable(L, top_lvl2);
354 if (servers[i]["clients_max"].asString().size()) {
356 std::string clients_max_raw = servers[i]["clients_max"].asString();
358 int numbervalue = strtol(clients_max_raw.c_str(),&endptr,10);
360 if ((clients_max_raw != "") && (*endptr == 0)) {
361 lua_pushstring(L,"clients_max");
362 lua_pushnumber(L,numbervalue);
363 lua_settable(L, top_lvl2);
367 if (servers[i]["version"].asString().size()) {
368 lua_pushstring(L,"version");
369 std::string topush = servers[i]["version"].asString();
370 lua_pushstring(L,topush.c_str());
371 lua_settable(L, top_lvl2);
374 if (servers[i]["proto_min"].asString().size()) {
375 lua_pushstring(L,"proto_min");
376 lua_pushinteger(L,servers[i]["proto_min"].asInt());
377 lua_settable(L, top_lvl2);
380 if (servers[i]["proto_max"].asString().size()) {
381 lua_pushstring(L,"proto_max");
382 lua_pushinteger(L,servers[i]["proto_max"].asInt());
383 lua_settable(L, top_lvl2);
386 if (servers[i]["password"].asString().size()) {
387 lua_pushstring(L,"password");
388 lua_pushboolean(L,servers[i]["password"].asBool());
389 lua_settable(L, top_lvl2);
392 if (servers[i]["creative"].asString().size()) {
393 lua_pushstring(L,"creative");
394 lua_pushboolean(L,servers[i]["creative"].asBool());
395 lua_settable(L, top_lvl2);
398 if (servers[i]["damage"].asString().size()) {
399 lua_pushstring(L,"damage");
400 lua_pushboolean(L,servers[i]["damage"].asBool());
401 lua_settable(L, top_lvl2);
404 if (servers[i]["pvp"].asString().size()) {
405 lua_pushstring(L,"pvp");
406 lua_pushboolean(L,servers[i]["pvp"].asBool());
407 lua_settable(L, top_lvl2);
410 if (servers[i]["description"].asString().size()) {
411 lua_pushstring(L,"description");
412 std::string topush = servers[i]["description"].asString();
413 lua_pushstring(L,topush.c_str());
414 lua_settable(L, top_lvl2);
417 if (servers[i]["name"].asString().size()) {
418 lua_pushstring(L,"name");
419 std::string topush = servers[i]["name"].asString();
420 lua_pushstring(L,topush.c_str());
421 lua_settable(L, top_lvl2);
424 if (servers[i]["address"].asString().size()) {
425 lua_pushstring(L,"address");
426 std::string topush = servers[i]["address"].asString();
427 lua_pushstring(L,topush.c_str());
428 lua_settable(L, top_lvl2);
431 if (servers[i]["port"].asString().size()) {
432 lua_pushstring(L,"port");
433 std::string topush = servers[i]["port"].asString();
434 lua_pushstring(L,topush.c_str());
435 lua_settable(L, top_lvl2);
438 if (servers[i].isMember("ping")) {
439 float ping = servers[i]["ping"].asFloat();
440 lua_pushstring(L, "ping");
441 lua_pushnumber(L, ping);
442 lua_settable(L, top_lvl2);
445 lua_settable(L, top);
451 /******************************************************************************/
452 int ModApiMainMenu::l_delete_favorite(lua_State *L)
454 std::vector<ServerListSpec> servers;
456 std::string listtype = "local";
458 if (!lua_isnone(L,2)) {
459 listtype = luaL_checkstring(L,2);
462 if ((listtype != "local") &&
463 (listtype != "online"))
467 if(listtype == "online") {
468 servers = ServerList::getOnline();
470 servers = ServerList::getLocal();
473 int fav_idx = luaL_checkinteger(L,1) -1;
475 if ((fav_idx >= 0) &&
476 (fav_idx < (int) servers.size())) {
478 ServerList::deleteEntry(servers[fav_idx]);
484 /******************************************************************************/
485 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
487 GUIEngine* engine = getGuiEngine(L);
488 sanity_check(engine != NULL);
490 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
493 engine->m_menumanager);
498 /******************************************************************************/
499 int ModApiMainMenu::l_create_world(lua_State *L)
501 const char *name = luaL_checkstring(L, 1);
502 int gameidx = luaL_checkinteger(L,2) -1;
504 std::string path = porting::path_user + DIR_DELIM
508 std::vector<SubgameSpec> games = getAvailableGames();
510 if ((gameidx >= 0) &&
511 (gameidx < (int) games.size())) {
513 // Create world if it doesn't exist
514 if (!loadGameConfAndInitWorld(path, games[gameidx])) {
515 lua_pushstring(L, "Failed to initialize world");
520 lua_pushstring(L, "Invalid game index");
525 /******************************************************************************/
526 int ModApiMainMenu::l_delete_world(lua_State *L)
528 int worldidx = luaL_checkinteger(L,1) -1;
530 std::vector<WorldSpec> worlds = getAvailableWorlds();
532 if ((worldidx >= 0) &&
533 (worldidx < (int) worlds.size())) {
535 WorldSpec spec = worlds[worldidx];
537 std::vector<std::string> paths;
538 paths.push_back(spec.path);
539 fs::GetRecursiveSubPaths(spec.path, paths);
542 if (!fs::DeletePaths(paths)) {
543 lua_pushstring(L, "Failed to delete world");
550 lua_pushstring(L, "Invalid world index");
555 /******************************************************************************/
556 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
558 GUIEngine* engine = getGuiEngine(L);
559 sanity_check(engine != NULL);
561 std::string text = "";
563 if (!lua_isnone(L,1) && !lua_isnil(L,1))
564 text = luaL_checkstring(L, 1);
566 engine->setTopleftText(text);
570 /******************************************************************************/
571 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
573 std::vector<const char *> names;
574 Mapgen::getMapgenNames(&names, lua_toboolean(L, 1));
577 for (size_t i = 0; i != names.size(); i++) {
578 lua_pushstring(L, names[i]);
579 lua_rawseti(L, -2, i + 1);
586 /******************************************************************************/
587 int ModApiMainMenu::l_get_modpath(lua_State *L)
589 std::string modpath = fs::RemoveRelativePathComponents(
590 porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
591 lua_pushstring(L, modpath.c_str());
595 /******************************************************************************/
596 int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
598 std::string modpath = fs::RemoveRelativePathComponents(
599 porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM);
600 lua_pushstring(L, modpath.c_str());
604 /******************************************************************************/
605 int ModApiMainMenu::l_get_gamepath(lua_State *L)
607 std::string gamepath = fs::RemoveRelativePathComponents(
608 porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
609 lua_pushstring(L, gamepath.c_str());
613 /******************************************************************************/
614 int ModApiMainMenu::l_get_texturepath(lua_State *L)
616 std::string gamepath = fs::RemoveRelativePathComponents(
617 porting::path_user + DIR_DELIM + "textures");
618 lua_pushstring(L, gamepath.c_str());
622 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
624 std::string gamepath = fs::RemoveRelativePathComponents(
625 porting::path_share + DIR_DELIM + "textures");
626 lua_pushstring(L, gamepath.c_str());
630 /******************************************************************************/
631 int ModApiMainMenu::l_create_dir(lua_State *L) {
632 const char *path = luaL_checkstring(L, 1);
634 if (ModApiMainMenu::isMinetestPath(path)) {
635 lua_pushboolean(L, fs::CreateAllDirs(path));
639 lua_pushboolean(L, false);
643 /******************************************************************************/
644 int ModApiMainMenu::l_delete_dir(lua_State *L)
646 const char *path = luaL_checkstring(L, 1);
648 std::string absolute_path = fs::RemoveRelativePathComponents(path);
650 if (ModApiMainMenu::isMinetestPath(absolute_path)) {
651 lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
655 lua_pushboolean(L, false);
659 /******************************************************************************/
660 int ModApiMainMenu::l_copy_dir(lua_State *L)
662 const char *source = luaL_checkstring(L, 1);
663 const char *destination = luaL_checkstring(L, 2);
665 bool keep_source = true;
667 if ((!lua_isnone(L,3)) &&
669 keep_source = lua_toboolean(L,3);
672 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
673 std::string absolute_source = fs::RemoveRelativePathComponents(source);
675 if ((ModApiMainMenu::isMinetestPath(absolute_destination))) {
676 bool retval = fs::CopyDir(absolute_source,absolute_destination);
678 if (retval && (!keep_source)) {
680 retval &= fs::RecursiveDelete(absolute_source);
682 lua_pushboolean(L,retval);
685 lua_pushboolean(L,false);
689 /******************************************************************************/
690 int ModApiMainMenu::l_extract_zip(lua_State *L)
692 const char *zipfile = luaL_checkstring(L, 1);
693 const char *destination = luaL_checkstring(L, 2);
695 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
697 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
698 fs::CreateAllDirs(absolute_destination);
700 io::IFileSystem *fs = RenderingEngine::get_filesystem();
702 if (!fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP)) {
703 lua_pushboolean(L,false);
707 sanity_check(fs->getFileArchiveCount() > 0);
709 /**********************************************************************/
710 /* WARNING this is not threadsafe!! */
711 /**********************************************************************/
712 io::IFileArchive* opened_zip =
713 fs->getFileArchive(fs->getFileArchiveCount()-1);
715 const io::IFileList* files_in_zip = opened_zip->getFileList();
717 unsigned int number_of_files = files_in_zip->getFileCount();
719 for (unsigned int i=0; i < number_of_files; i++) {
720 std::string fullpath = destination;
721 fullpath += DIR_DELIM;
722 fullpath += files_in_zip->getFullFileName(i).c_str();
723 std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
725 if (!files_in_zip->isDirectory(i)) {
726 if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
727 fs->removeFileArchive(fs->getFileArchiveCount()-1);
728 lua_pushboolean(L,false);
732 io::IReadFile* toread = opened_zip->createAndOpenFile(i);
734 FILE *targetfile = fopen(fullpath.c_str(),"wb");
736 if (targetfile == NULL) {
737 fs->removeFileArchive(fs->getFileArchiveCount()-1);
738 lua_pushboolean(L,false);
742 char read_buffer[1024];
745 while (total_read < toread->getSize()) {
747 unsigned int bytes_read =
748 toread->read(read_buffer,sizeof(read_buffer));
749 if ((bytes_read == 0 ) ||
750 (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
753 fs->removeFileArchive(fs->getFileArchiveCount()-1);
754 lua_pushboolean(L,false);
757 total_read += bytes_read;
765 fs->removeFileArchive(fs->getFileArchiveCount()-1);
766 lua_pushboolean(L,true);
770 lua_pushboolean(L,false);
774 /******************************************************************************/
775 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
777 GUIEngine* engine = getGuiEngine(L);
778 sanity_check(engine != NULL);
780 lua_pushstring(L,engine->getScriptDir().c_str());
784 /******************************************************************************/
785 bool ModApiMainMenu::isMinetestPath(std::string path)
787 if (fs::PathStartsWith(path,fs::TempPath()))
791 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games")))
795 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods")))
799 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds")))
806 /******************************************************************************/
807 int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
809 GUIEngine* engine = getGuiEngine(L);
810 sanity_check(engine != NULL);
812 const char *formname= luaL_checkstring(L, 1);
813 const char *title = luaL_checkstring(L, 2);
814 bool is_file_select = lua_toboolean(L, 3);
816 GUIFileSelectMenu* fileOpenMenu =
817 new GUIFileSelectMenu(RenderingEngine::get_gui_env(),
820 engine->m_menumanager,
824 fileOpenMenu->setTextDest(engine->m_buttonhandler);
825 fileOpenMenu->drop();
829 /******************************************************************************/
830 int ModApiMainMenu::l_download_file(lua_State *L)
832 const char *url = luaL_checkstring(L, 1);
833 const char *target = luaL_checkstring(L, 2);
836 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
838 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
839 if (GUIEngine::downloadFile(url,absolute_destination)) {
840 lua_pushboolean(L,true);
844 errorstream << "DOWNLOAD denied: " << absolute_destination
845 << " isn't a allowed path" << std::endl;
847 lua_pushboolean(L,false);
851 /******************************************************************************/
852 int ModApiMainMenu::l_get_video_drivers(lua_State *L)
854 std::vector<irr::video::E_DRIVER_TYPE> drivers = RenderingEngine::getSupportedVideoDrivers();
857 for (u32 i = 0; i != drivers.size(); i++) {
858 const char *name = RenderingEngine::getVideoDriverName(drivers[i]);
859 const char *fname = RenderingEngine::getVideoDriverFriendlyName(drivers[i]);
862 lua_pushstring(L, name);
863 lua_setfield(L, -2, "name");
864 lua_pushstring(L, fname);
865 lua_setfield(L, -2, "friendly_name");
867 lua_rawseti(L, -2, i + 1);
873 /******************************************************************************/
874 int ModApiMainMenu::l_get_video_modes(lua_State *L)
876 std::vector<core::vector3d<u32> > videomodes
877 = RenderingEngine::getSupportedVideoModes();
880 for (u32 i = 0; i != videomodes.size(); i++) {
882 lua_pushnumber(L, videomodes[i].X);
883 lua_setfield(L, -2, "w");
884 lua_pushnumber(L, videomodes[i].Y);
885 lua_setfield(L, -2, "h");
886 lua_pushnumber(L, videomodes[i].Z);
887 lua_setfield(L, -2, "depth");
889 lua_rawseti(L, -2, i + 1);
895 /******************************************************************************/
896 int ModApiMainMenu::l_gettext(lua_State *L)
898 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
899 lua_pushstring(L, text.c_str());
904 /******************************************************************************/
905 int ModApiMainMenu::l_get_screen_info(lua_State *L)
908 int top = lua_gettop(L);
909 lua_pushstring(L,"density");
910 lua_pushnumber(L,RenderingEngine::getDisplayDensity());
911 lua_settable(L, top);
913 lua_pushstring(L,"display_width");
914 lua_pushnumber(L,RenderingEngine::getDisplaySize().X);
915 lua_settable(L, top);
917 lua_pushstring(L,"display_height");
918 lua_pushnumber(L,RenderingEngine::getDisplaySize().Y);
919 lua_settable(L, top);
921 const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
922 lua_pushstring(L,"window_width");
923 lua_pushnumber(L, window_size.X);
924 lua_settable(L, top);
926 lua_pushstring(L,"window_height");
927 lua_pushnumber(L, window_size.Y);
928 lua_settable(L, top);
932 /******************************************************************************/
933 int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
935 u16 proto_version_min = g_settings->getFlag("send_pre_v25_init") ?
936 CLIENT_PROTOCOL_VERSION_MIN_LEGACY : CLIENT_PROTOCOL_VERSION_MIN;
937 lua_pushinteger(L, proto_version_min);
941 int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
943 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX);
947 /******************************************************************************/
948 int ModApiMainMenu::l_do_async_callback(lua_State *L)
950 GUIEngine* engine = getGuiEngine(L);
952 size_t func_length, param_length;
953 const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
955 const char* serialized_param_raw = luaL_checklstring(L, 2, ¶m_length);
957 sanity_check(serialized_func_raw != NULL);
958 sanity_check(serialized_param_raw != NULL);
960 std::string serialized_func = std::string(serialized_func_raw, func_length);
961 std::string serialized_param = std::string(serialized_param_raw, param_length);
963 lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param));
968 /******************************************************************************/
969 void ModApiMainMenu::Initialize(lua_State *L, int top)
971 API_FCT(update_formspec);
973 API_FCT(get_textlist_index);
974 API_FCT(get_table_index);
979 API_FCT(get_favorites);
980 API_FCT(show_keys_menu);
981 API_FCT(create_world);
982 API_FCT(delete_world);
983 API_FCT(delete_favorite);
984 API_FCT(set_background);
985 API_FCT(set_topleft_text);
986 API_FCT(get_mapgen_names);
987 API_FCT(get_modpath);
988 API_FCT(get_clientmodpath);
989 API_FCT(get_gamepath);
990 API_FCT(get_texturepath);
991 API_FCT(get_texturepath_share);
995 API_FCT(extract_zip);
996 API_FCT(get_mainmenu_path);
997 API_FCT(show_path_select_dialog);
998 API_FCT(download_file);
1000 API_FCT(get_video_drivers);
1001 API_FCT(get_video_modes);
1002 API_FCT(get_screen_info);
1003 API_FCT(get_min_supp_proto);
1004 API_FCT(get_max_supp_proto);
1005 API_FCT(do_async_callback);
1008 /******************************************************************************/
1009 void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
1012 API_FCT(get_worlds);
1014 API_FCT(get_favorites);
1015 API_FCT(get_mapgen_names);
1016 API_FCT(get_modpath);
1017 API_FCT(get_clientmodpath);
1018 API_FCT(get_gamepath);
1019 API_FCT(get_texturepath);
1020 API_FCT(get_texturepath_share);
1021 API_FCT(create_dir);
1022 API_FCT(delete_dir);
1024 //API_FCT(extract_zip); //TODO remove dependency to GuiEngine
1025 API_FCT(download_file);
1026 //API_FCT(gettext); (gettext lib isn't threadsafe)