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 "gui/guiEngine.h"
25 #include "gui/guiMainMenu.h"
26 #include "gui/guiKeyChangeMenu.h"
27 #include "gui/guiPathSelectMenu.h"
32 #include "convert_json.h"
33 #include "serverlist.h"
34 #include "mapgen/mapgen.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 (const WorldSpec &world : worlds) {
237 lua_pushnumber(L,index);
240 int top_lvl2 = lua_gettop(L);
242 lua_pushstring(L,"path");
243 lua_pushstring(L, world.path.c_str());
244 lua_settable(L, top_lvl2);
246 lua_pushstring(L,"name");
247 lua_pushstring(L, world.name.c_str());
248 lua_settable(L, top_lvl2);
250 lua_pushstring(L,"gameid");
251 lua_pushstring(L, world.gameid.c_str());
252 lua_settable(L, top_lvl2);
254 lua_settable(L, top);
260 /******************************************************************************/
261 int ModApiMainMenu::l_get_favorites(lua_State *L)
263 std::string listtype = "local";
265 if (!lua_isnone(L,1)) {
266 listtype = luaL_checkstring(L,1);
269 std::vector<ServerListSpec> servers;
271 if(listtype == "online") {
272 servers = ServerList::getOnline();
274 servers = ServerList::getLocal();
278 int top = lua_gettop(L);
279 unsigned int index = 1;
281 for (const Json::Value &server : servers) {
283 lua_pushnumber(L,index);
286 int top_lvl2 = lua_gettop(L);
288 if (!server["clients"].asString().empty()) {
289 std::string clients_raw = server["clients"].asString();
291 int numbervalue = strtol(clients_raw.c_str(),&endptr,10);
293 if ((!clients_raw.empty()) && (*endptr == 0)) {
294 lua_pushstring(L,"clients");
295 lua_pushnumber(L,numbervalue);
296 lua_settable(L, top_lvl2);
300 if (!server["clients_max"].asString().empty()) {
302 std::string clients_max_raw = server["clients_max"].asString();
304 int numbervalue = strtol(clients_max_raw.c_str(),&endptr,10);
306 if ((!clients_max_raw.empty()) && (*endptr == 0)) {
307 lua_pushstring(L,"clients_max");
308 lua_pushnumber(L,numbervalue);
309 lua_settable(L, top_lvl2);
313 if (!server["version"].asString().empty()) {
314 lua_pushstring(L,"version");
315 std::string topush = server["version"].asString();
316 lua_pushstring(L,topush.c_str());
317 lua_settable(L, top_lvl2);
320 if (!server["proto_min"].asString().empty()) {
321 lua_pushstring(L,"proto_min");
322 lua_pushinteger(L, server["proto_min"].asInt());
323 lua_settable(L, top_lvl2);
326 if (!server["proto_max"].asString().empty()) {
327 lua_pushstring(L,"proto_max");
328 lua_pushinteger(L, server["proto_max"].asInt());
329 lua_settable(L, top_lvl2);
332 if (!server["password"].asString().empty()) {
333 lua_pushstring(L,"password");
334 lua_pushboolean(L, server["password"].asBool());
335 lua_settable(L, top_lvl2);
338 if (!server["creative"].asString().empty()) {
339 lua_pushstring(L,"creative");
340 lua_pushboolean(L, server["creative"].asBool());
341 lua_settable(L, top_lvl2);
344 if (!server["damage"].asString().empty()) {
345 lua_pushstring(L,"damage");
346 lua_pushboolean(L, server["damage"].asBool());
347 lua_settable(L, top_lvl2);
350 if (!server["pvp"].asString().empty()) {
351 lua_pushstring(L,"pvp");
352 lua_pushboolean(L, server["pvp"].asBool());
353 lua_settable(L, top_lvl2);
356 if (!server["description"].asString().empty()) {
357 lua_pushstring(L,"description");
358 std::string topush = server["description"].asString();
359 lua_pushstring(L,topush.c_str());
360 lua_settable(L, top_lvl2);
363 if (!server["name"].asString().empty()) {
364 lua_pushstring(L,"name");
365 std::string topush = server["name"].asString();
366 lua_pushstring(L,topush.c_str());
367 lua_settable(L, top_lvl2);
370 if (!server["address"].asString().empty()) {
371 lua_pushstring(L,"address");
372 std::string topush = server["address"].asString();
373 lua_pushstring(L,topush.c_str());
374 lua_settable(L, top_lvl2);
377 if (!server["port"].asString().empty()) {
378 lua_pushstring(L,"port");
379 std::string topush = server["port"].asString();
380 lua_pushstring(L,topush.c_str());
381 lua_settable(L, top_lvl2);
384 if (server.isMember("ping")) {
385 float ping = server["ping"].asFloat();
386 lua_pushstring(L, "ping");
387 lua_pushnumber(L, ping);
388 lua_settable(L, top_lvl2);
391 lua_settable(L, top);
397 /******************************************************************************/
398 int ModApiMainMenu::l_delete_favorite(lua_State *L)
400 std::vector<ServerListSpec> servers;
402 std::string listtype = "local";
404 if (!lua_isnone(L,2)) {
405 listtype = luaL_checkstring(L,2);
408 if ((listtype != "local") &&
409 (listtype != "online"))
413 if(listtype == "online") {
414 servers = ServerList::getOnline();
416 servers = ServerList::getLocal();
419 int fav_idx = luaL_checkinteger(L,1) -1;
421 if ((fav_idx >= 0) &&
422 (fav_idx < (int) servers.size())) {
424 ServerList::deleteEntry(servers[fav_idx]);
430 /******************************************************************************/
431 int ModApiMainMenu::l_get_games(lua_State *L)
433 std::vector<SubgameSpec> games = getAvailableGames();
436 int top = lua_gettop(L);
437 unsigned int index = 1;
439 for (const SubgameSpec &game : games) {
440 lua_pushnumber(L, index);
442 int top_lvl2 = lua_gettop(L);
444 lua_pushstring(L, "id");
445 lua_pushstring(L, game.id.c_str());
446 lua_settable(L, top_lvl2);
448 lua_pushstring(L, "path");
449 lua_pushstring(L, game.path.c_str());
450 lua_settable(L, top_lvl2);
452 lua_pushstring(L, "gamemods_path");
453 lua_pushstring(L, game.gamemods_path.c_str());
454 lua_settable(L, top_lvl2);
456 lua_pushstring(L, "name");
457 lua_pushstring(L, game.name.c_str());
458 lua_settable(L, top_lvl2);
460 lua_pushstring(L, "menuicon_path");
461 lua_pushstring(L, game.menuicon_path.c_str());
462 lua_settable(L, top_lvl2);
464 lua_pushstring(L, "addon_mods_paths");
466 int table2 = lua_gettop(L);
467 int internal_index = 1;
468 for (const std::string &addon_mods_path : game.addon_mods_paths) {
469 lua_pushnumber(L, internal_index);
470 lua_pushstring(L, addon_mods_path.c_str());
471 lua_settable(L, table2);
474 lua_settable(L, top_lvl2);
475 lua_settable(L, top);
481 /******************************************************************************/
482 int ModApiMainMenu::l_get_mod_info(lua_State *L)
484 std::string path = luaL_checkstring(L, 1);
488 parseModContents(spec);
492 lua_pushstring(L, spec.name.c_str());
493 lua_setfield(L, -2, "name");
495 lua_pushstring(L, spec.is_modpack ? "modpack" : "mod");
496 lua_setfield(L, -2, "type");
498 lua_pushstring(L, spec.desc.c_str());
499 lua_setfield(L, -2, "description");
501 lua_pushstring(L, spec.path.c_str());
502 lua_setfield(L, -2, "path");
507 for (const auto &dep : spec.depends) {
508 lua_pushstring(L, dep.c_str());
509 lua_rawseti(L, -2, i);
512 lua_setfield(L, -2, "depends");
514 // Optional Dependencies
517 for (const auto &dep : spec.optdepends) {
518 lua_pushstring(L, dep.c_str());
519 lua_rawseti(L, -2, i);
522 lua_setfield(L, -2, "optional_depends");
527 /******************************************************************************/
528 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
530 GUIEngine* engine = getGuiEngine(L);
531 sanity_check(engine != NULL);
533 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
536 engine->m_menumanager);
541 /******************************************************************************/
542 int ModApiMainMenu::l_create_world(lua_State *L)
544 const char *name = luaL_checkstring(L, 1);
545 int gameidx = luaL_checkinteger(L,2) -1;
547 std::string path = porting::path_user + DIR_DELIM
551 std::vector<SubgameSpec> games = getAvailableGames();
553 if ((gameidx >= 0) &&
554 (gameidx < (int) games.size())) {
556 // Create world if it doesn't exist
557 if (!loadGameConfAndInitWorld(path, games[gameidx])) {
558 lua_pushstring(L, "Failed to initialize world");
563 lua_pushstring(L, "Invalid game index");
568 /******************************************************************************/
569 int ModApiMainMenu::l_delete_world(lua_State *L)
571 int worldidx = luaL_checkinteger(L,1) -1;
573 std::vector<WorldSpec> worlds = getAvailableWorlds();
575 if ((worldidx >= 0) &&
576 (worldidx < (int) worlds.size())) {
578 WorldSpec spec = worlds[worldidx];
580 std::vector<std::string> paths;
581 paths.push_back(spec.path);
582 fs::GetRecursiveSubPaths(spec.path, paths, true);
585 if (!fs::DeletePaths(paths)) {
586 lua_pushstring(L, "Failed to delete world");
593 lua_pushstring(L, "Invalid world index");
598 /******************************************************************************/
599 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
601 GUIEngine* engine = getGuiEngine(L);
602 sanity_check(engine != NULL);
606 if (!lua_isnone(L,1) && !lua_isnil(L,1))
607 text = luaL_checkstring(L, 1);
609 engine->setTopleftText(text);
613 /******************************************************************************/
614 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
616 std::vector<const char *> names;
617 Mapgen::getMapgenNames(&names, lua_toboolean(L, 1));
620 for (size_t i = 0; i != names.size(); i++) {
621 lua_pushstring(L, names[i]);
622 lua_rawseti(L, -2, i + 1);
629 /******************************************************************************/
630 int ModApiMainMenu::l_get_modpath(lua_State *L)
632 std::string modpath = fs::RemoveRelativePathComponents(
633 porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
634 lua_pushstring(L, modpath.c_str());
638 /******************************************************************************/
639 int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
641 std::string modpath = fs::RemoveRelativePathComponents(
642 porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM);
643 lua_pushstring(L, modpath.c_str());
647 /******************************************************************************/
648 int ModApiMainMenu::l_get_gamepath(lua_State *L)
650 std::string gamepath = fs::RemoveRelativePathComponents(
651 porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
652 lua_pushstring(L, gamepath.c_str());
656 /******************************************************************************/
657 int ModApiMainMenu::l_get_texturepath(lua_State *L)
659 std::string gamepath = fs::RemoveRelativePathComponents(
660 porting::path_user + DIR_DELIM + "textures");
661 lua_pushstring(L, gamepath.c_str());
665 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
667 std::string gamepath = fs::RemoveRelativePathComponents(
668 porting::path_share + DIR_DELIM + "textures");
669 lua_pushstring(L, gamepath.c_str());
673 /******************************************************************************/
674 int ModApiMainMenu::l_create_dir(lua_State *L) {
675 const char *path = luaL_checkstring(L, 1);
677 if (ModApiMainMenu::isMinetestPath(path)) {
678 lua_pushboolean(L, fs::CreateAllDirs(path));
682 lua_pushboolean(L, false);
686 /******************************************************************************/
687 int ModApiMainMenu::l_delete_dir(lua_State *L)
689 const char *path = luaL_checkstring(L, 1);
691 std::string absolute_path = fs::RemoveRelativePathComponents(path);
693 if (ModApiMainMenu::isMinetestPath(absolute_path)) {
694 lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
698 lua_pushboolean(L, false);
702 /******************************************************************************/
703 int ModApiMainMenu::l_copy_dir(lua_State *L)
705 const char *source = luaL_checkstring(L, 1);
706 const char *destination = luaL_checkstring(L, 2);
708 bool keep_source = true;
710 if ((!lua_isnone(L,3)) &&
712 keep_source = lua_toboolean(L,3);
715 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
716 std::string absolute_source = fs::RemoveRelativePathComponents(source);
718 if ((ModApiMainMenu::isMinetestPath(absolute_destination))) {
719 bool retval = fs::CopyDir(absolute_source,absolute_destination);
721 if (retval && (!keep_source)) {
723 retval &= fs::RecursiveDelete(absolute_source);
725 lua_pushboolean(L,retval);
728 lua_pushboolean(L,false);
732 /******************************************************************************/
733 int ModApiMainMenu::l_extract_zip(lua_State *L)
735 const char *zipfile = luaL_checkstring(L, 1);
736 const char *destination = luaL_checkstring(L, 2);
738 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
740 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
741 fs::CreateAllDirs(absolute_destination);
743 io::IFileSystem *fs = RenderingEngine::get_filesystem();
745 if (!fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP)) {
746 lua_pushboolean(L,false);
750 sanity_check(fs->getFileArchiveCount() > 0);
752 /**********************************************************************/
753 /* WARNING this is not threadsafe!! */
754 /**********************************************************************/
755 io::IFileArchive* opened_zip =
756 fs->getFileArchive(fs->getFileArchiveCount()-1);
758 const io::IFileList* files_in_zip = opened_zip->getFileList();
760 unsigned int number_of_files = files_in_zip->getFileCount();
762 for (unsigned int i=0; i < number_of_files; i++) {
763 std::string fullpath = destination;
764 fullpath += DIR_DELIM;
765 fullpath += files_in_zip->getFullFileName(i).c_str();
766 std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
768 if (!files_in_zip->isDirectory(i)) {
769 if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
770 fs->removeFileArchive(fs->getFileArchiveCount()-1);
771 lua_pushboolean(L,false);
775 io::IReadFile* toread = opened_zip->createAndOpenFile(i);
777 FILE *targetfile = fopen(fullpath.c_str(),"wb");
779 if (targetfile == NULL) {
780 fs->removeFileArchive(fs->getFileArchiveCount()-1);
781 lua_pushboolean(L,false);
785 char read_buffer[1024];
788 while (total_read < toread->getSize()) {
790 unsigned int bytes_read =
791 toread->read(read_buffer,sizeof(read_buffer));
792 if ((bytes_read == 0 ) ||
793 (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
796 fs->removeFileArchive(fs->getFileArchiveCount()-1);
797 lua_pushboolean(L,false);
800 total_read += bytes_read;
808 fs->removeFileArchive(fs->getFileArchiveCount()-1);
809 lua_pushboolean(L,true);
813 lua_pushboolean(L,false);
817 /******************************************************************************/
818 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
820 GUIEngine* engine = getGuiEngine(L);
821 sanity_check(engine != NULL);
823 lua_pushstring(L,engine->getScriptDir().c_str());
827 /******************************************************************************/
828 bool ModApiMainMenu::isMinetestPath(std::string path)
830 if (fs::PathStartsWith(path,fs::TempPath()))
834 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games")))
838 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods")))
842 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds")))
849 /******************************************************************************/
850 int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
852 GUIEngine* engine = getGuiEngine(L);
853 sanity_check(engine != NULL);
855 const char *formname= luaL_checkstring(L, 1);
856 const char *title = luaL_checkstring(L, 2);
857 bool is_file_select = lua_toboolean(L, 3);
859 GUIFileSelectMenu* fileOpenMenu =
860 new GUIFileSelectMenu(RenderingEngine::get_gui_env(),
863 engine->m_menumanager,
867 fileOpenMenu->setTextDest(engine->m_buttonhandler);
868 fileOpenMenu->drop();
872 /******************************************************************************/
873 int ModApiMainMenu::l_download_file(lua_State *L)
875 const char *url = luaL_checkstring(L, 1);
876 const char *target = luaL_checkstring(L, 2);
879 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
881 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
882 if (GUIEngine::downloadFile(url,absolute_destination)) {
883 lua_pushboolean(L,true);
887 errorstream << "DOWNLOAD denied: " << absolute_destination
888 << " isn't a allowed path" << std::endl;
890 lua_pushboolean(L,false);
894 /******************************************************************************/
895 int ModApiMainMenu::l_get_video_drivers(lua_State *L)
897 std::vector<irr::video::E_DRIVER_TYPE> drivers = RenderingEngine::getSupportedVideoDrivers();
900 for (u32 i = 0; i != drivers.size(); i++) {
901 const char *name = RenderingEngine::getVideoDriverName(drivers[i]);
902 const char *fname = RenderingEngine::getVideoDriverFriendlyName(drivers[i]);
905 lua_pushstring(L, name);
906 lua_setfield(L, -2, "name");
907 lua_pushstring(L, fname);
908 lua_setfield(L, -2, "friendly_name");
910 lua_rawseti(L, -2, i + 1);
916 /******************************************************************************/
917 int ModApiMainMenu::l_get_video_modes(lua_State *L)
919 std::vector<core::vector3d<u32> > videomodes
920 = RenderingEngine::getSupportedVideoModes();
923 for (u32 i = 0; i != videomodes.size(); i++) {
925 lua_pushnumber(L, videomodes[i].X);
926 lua_setfield(L, -2, "w");
927 lua_pushnumber(L, videomodes[i].Y);
928 lua_setfield(L, -2, "h");
929 lua_pushnumber(L, videomodes[i].Z);
930 lua_setfield(L, -2, "depth");
932 lua_rawseti(L, -2, i + 1);
938 /******************************************************************************/
939 int ModApiMainMenu::l_gettext(lua_State *L)
941 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
942 lua_pushstring(L, text.c_str());
947 /******************************************************************************/
948 int ModApiMainMenu::l_get_screen_info(lua_State *L)
951 int top = lua_gettop(L);
952 lua_pushstring(L,"density");
953 lua_pushnumber(L,RenderingEngine::getDisplayDensity());
954 lua_settable(L, top);
956 lua_pushstring(L,"display_width");
957 lua_pushnumber(L,RenderingEngine::getDisplaySize().X);
958 lua_settable(L, top);
960 lua_pushstring(L,"display_height");
961 lua_pushnumber(L,RenderingEngine::getDisplaySize().Y);
962 lua_settable(L, top);
964 const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
965 lua_pushstring(L,"window_width");
966 lua_pushnumber(L, window_size.X);
967 lua_settable(L, top);
969 lua_pushstring(L,"window_height");
970 lua_pushnumber(L, window_size.Y);
971 lua_settable(L, top);
975 /******************************************************************************/
976 int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
978 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MIN);
982 int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
984 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX);
988 /******************************************************************************/
989 int ModApiMainMenu::l_do_async_callback(lua_State *L)
991 GUIEngine* engine = getGuiEngine(L);
993 size_t func_length, param_length;
994 const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
996 const char* serialized_param_raw = luaL_checklstring(L, 2, ¶m_length);
998 sanity_check(serialized_func_raw != NULL);
999 sanity_check(serialized_param_raw != NULL);
1001 std::string serialized_func = std::string(serialized_func_raw, func_length);
1002 std::string serialized_param = std::string(serialized_param_raw, param_length);
1004 lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param));
1009 /******************************************************************************/
1010 void ModApiMainMenu::Initialize(lua_State *L, int top)
1012 API_FCT(update_formspec);
1013 API_FCT(set_clouds);
1014 API_FCT(get_textlist_index);
1015 API_FCT(get_table_index);
1016 API_FCT(get_worlds);
1018 API_FCT(get_mod_info);
1021 API_FCT(get_favorites);
1022 API_FCT(show_keys_menu);
1023 API_FCT(create_world);
1024 API_FCT(delete_world);
1025 API_FCT(delete_favorite);
1026 API_FCT(set_background);
1027 API_FCT(set_topleft_text);
1028 API_FCT(get_mapgen_names);
1029 API_FCT(get_modpath);
1030 API_FCT(get_clientmodpath);
1031 API_FCT(get_gamepath);
1032 API_FCT(get_texturepath);
1033 API_FCT(get_texturepath_share);
1034 API_FCT(create_dir);
1035 API_FCT(delete_dir);
1037 API_FCT(extract_zip);
1038 API_FCT(get_mainmenu_path);
1039 API_FCT(show_path_select_dialog);
1040 API_FCT(download_file);
1042 API_FCT(get_video_drivers);
1043 API_FCT(get_video_modes);
1044 API_FCT(get_screen_info);
1045 API_FCT(get_min_supp_proto);
1046 API_FCT(get_max_supp_proto);
1047 API_FCT(do_async_callback);
1050 /******************************************************************************/
1051 void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
1054 API_FCT(get_worlds);
1056 API_FCT(get_favorites);
1057 API_FCT(get_mapgen_names);
1058 API_FCT(get_modpath);
1059 API_FCT(get_clientmodpath);
1060 API_FCT(get_gamepath);
1061 API_FCT(get_texturepath);
1062 API_FCT(get_texturepath_share);
1063 API_FCT(create_dir);
1064 API_FCT(delete_dir);
1066 //API_FCT(extract_zip); //TODO remove dependency to GuiEngine
1067 API_FCT(download_file);
1068 //API_FCT(gettext); (gettext lib isn't threadsafe)