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_modstore_details(lua_State *L)
316 const char *modid = luaL_checkstring(L, 1);
320 std::string url = "";
322 url = g_settings->get("modstore_details_url");
324 catch(SettingNotFoundException &e) {
329 size_t idpos = url.find("*");
331 url.insert(idpos,modid);
333 details = getModstoreUrl(url);
335 ModStoreModDetails current_mod = readModStoreModDetails(details);
337 if ( current_mod.valid) {
339 int top = lua_gettop(L);
341 lua_pushstring(L,"id");
342 lua_pushnumber(L,current_mod.id);
343 lua_settable(L, top);
345 lua_pushstring(L,"title");
346 lua_pushstring(L,current_mod.title.c_str());
347 lua_settable(L, top);
349 lua_pushstring(L,"basename");
350 lua_pushstring(L,current_mod.basename.c_str());
351 lua_settable(L, top);
353 lua_pushstring(L,"description");
354 lua_pushstring(L,current_mod.description.c_str());
355 lua_settable(L, top);
357 lua_pushstring(L,"author");
358 lua_pushstring(L,current_mod.author.username.c_str());
359 lua_settable(L, top);
361 lua_pushstring(L,"download_url");
362 lua_pushstring(L,current_mod.versions[0].file.c_str());
363 lua_settable(L, top);
365 lua_pushstring(L,"versions");
367 int versionstop = lua_gettop(L);
368 for (unsigned int i=0;i < current_mod.versions.size(); i++) {
369 lua_pushnumber(L,i+1);
371 int current_element = lua_gettop(L);
373 lua_pushstring(L,"date");
374 lua_pushstring(L,current_mod.versions[i].date.c_str());
375 lua_settable(L,current_element);
377 lua_pushstring(L,"download_url");
378 lua_pushstring(L,current_mod.versions[i].file.c_str());
379 lua_settable(L,current_element);
381 lua_settable(L,versionstop);
383 lua_settable(L, top);
385 lua_pushstring(L,"screenshot_url");
386 lua_pushstring(L,current_mod.titlepic.file.c_str());
387 lua_settable(L, top);
389 lua_pushstring(L,"license");
390 lua_pushstring(L,current_mod.license.shortinfo.c_str());
391 lua_settable(L, top);
393 lua_pushstring(L,"rating");
394 lua_pushnumber(L,current_mod.rating);
395 lua_settable(L, top);
406 /******************************************************************************/
407 int ModApiMainMenu::l_get_modstore_list(lua_State *L)
410 std::string url = "";
412 url = g_settings->get("modstore_listmods_url");
414 catch(SettingNotFoundException &e) {
419 mods = getModstoreUrl(url);
421 std::vector<ModStoreMod> moddata = readModStoreList(mods);
424 int top = lua_gettop(L);
425 unsigned int index = 1;
427 for (unsigned int i = 0; i < moddata.size(); i++)
429 if (moddata[i].valid) {
430 lua_pushnumber(L,index);
433 int top_lvl2 = lua_gettop(L);
435 lua_pushstring(L,"id");
436 lua_pushnumber(L,moddata[i].id);
437 lua_settable(L, top_lvl2);
439 lua_pushstring(L,"title");
440 lua_pushstring(L,moddata[i].title.c_str());
441 lua_settable(L, top_lvl2);
443 lua_pushstring(L,"basename");
444 lua_pushstring(L,moddata[i].basename.c_str());
445 lua_settable(L, top_lvl2);
447 lua_settable(L, top);
454 /******************************************************************************/
455 int ModApiMainMenu::l_get_favorites(lua_State *L)
457 std::string listtype = "local";
459 if (!lua_isnone(L,1)) {
460 listtype = luaL_checkstring(L,1);
463 std::vector<ServerListSpec> servers;
465 if(listtype == "online") {
466 servers = ServerList::getOnline();
468 servers = ServerList::getLocal();
472 int top = lua_gettop(L);
473 unsigned int index = 1;
475 for (unsigned int i = 0; i < servers.size(); i++)
478 lua_pushnumber(L,index);
481 int top_lvl2 = lua_gettop(L);
483 if (servers[i]["clients"].asString().size()) {
484 std::string clients_raw = servers[i]["clients"].asString();
486 int numbervalue = strtol(clients_raw.c_str(),&endptr,10);
488 if ((clients_raw != "") && (*endptr == 0)) {
489 lua_pushstring(L,"clients");
490 lua_pushnumber(L,numbervalue);
491 lua_settable(L, top_lvl2);
495 if (servers[i]["clients_max"].asString().size()) {
497 std::string clients_max_raw = servers[i]["clients_max"].asString();
499 int numbervalue = strtol(clients_max_raw.c_str(),&endptr,10);
501 if ((clients_max_raw != "") && (*endptr == 0)) {
502 lua_pushstring(L,"clients_max");
503 lua_pushnumber(L,numbervalue);
504 lua_settable(L, top_lvl2);
508 if (servers[i]["version"].asString().size()) {
509 lua_pushstring(L,"version");
510 std::string topush = servers[i]["version"].asString();
511 lua_pushstring(L,topush.c_str());
512 lua_settable(L, top_lvl2);
515 if (servers[i]["proto_min"].asString().size()) {
516 lua_pushstring(L,"proto_min");
517 lua_pushinteger(L,servers[i]["proto_min"].asInt());
518 lua_settable(L, top_lvl2);
521 if (servers[i]["proto_max"].asString().size()) {
522 lua_pushstring(L,"proto_max");
523 lua_pushinteger(L,servers[i]["proto_max"].asInt());
524 lua_settable(L, top_lvl2);
527 if (servers[i]["password"].asString().size()) {
528 lua_pushstring(L,"password");
529 lua_pushboolean(L,servers[i]["password"].asBool());
530 lua_settable(L, top_lvl2);
533 if (servers[i]["creative"].asString().size()) {
534 lua_pushstring(L,"creative");
535 lua_pushboolean(L,servers[i]["creative"].asBool());
536 lua_settable(L, top_lvl2);
539 if (servers[i]["damage"].asString().size()) {
540 lua_pushstring(L,"damage");
541 lua_pushboolean(L,servers[i]["damage"].asBool());
542 lua_settable(L, top_lvl2);
545 if (servers[i]["pvp"].asString().size()) {
546 lua_pushstring(L,"pvp");
547 lua_pushboolean(L,servers[i]["pvp"].asBool());
548 lua_settable(L, top_lvl2);
551 if (servers[i]["description"].asString().size()) {
552 lua_pushstring(L,"description");
553 std::string topush = servers[i]["description"].asString();
554 lua_pushstring(L,topush.c_str());
555 lua_settable(L, top_lvl2);
558 if (servers[i]["name"].asString().size()) {
559 lua_pushstring(L,"name");
560 std::string topush = servers[i]["name"].asString();
561 lua_pushstring(L,topush.c_str());
562 lua_settable(L, top_lvl2);
565 if (servers[i]["address"].asString().size()) {
566 lua_pushstring(L,"address");
567 std::string topush = servers[i]["address"].asString();
568 lua_pushstring(L,topush.c_str());
569 lua_settable(L, top_lvl2);
572 if (servers[i]["port"].asString().size()) {
573 lua_pushstring(L,"port");
574 std::string topush = servers[i]["port"].asString();
575 lua_pushstring(L,topush.c_str());
576 lua_settable(L, top_lvl2);
579 if (servers[i].isMember("ping")) {
580 float ping = servers[i]["ping"].asFloat();
581 lua_pushstring(L, "ping");
582 lua_pushnumber(L, ping);
583 lua_settable(L, top_lvl2);
586 lua_settable(L, top);
592 /******************************************************************************/
593 int ModApiMainMenu::l_delete_favorite(lua_State *L)
595 std::vector<ServerListSpec> servers;
597 std::string listtype = "local";
599 if (!lua_isnone(L,2)) {
600 listtype = luaL_checkstring(L,2);
603 if ((listtype != "local") &&
604 (listtype != "online"))
608 if(listtype == "online") {
609 servers = ServerList::getOnline();
611 servers = ServerList::getLocal();
614 int fav_idx = luaL_checkinteger(L,1) -1;
616 if ((fav_idx >= 0) &&
617 (fav_idx < (int) servers.size())) {
619 ServerList::deleteEntry(servers[fav_idx]);
625 /******************************************************************************/
626 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
628 GUIEngine* engine = getGuiEngine(L);
629 sanity_check(engine != NULL);
631 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
634 engine->m_menumanager);
639 /******************************************************************************/
640 int ModApiMainMenu::l_create_world(lua_State *L)
642 const char *name = luaL_checkstring(L, 1);
643 int gameidx = luaL_checkinteger(L,2) -1;
645 std::string path = porting::path_user + DIR_DELIM
649 std::vector<SubgameSpec> games = getAvailableGames();
651 if ((gameidx >= 0) &&
652 (gameidx < (int) games.size())) {
654 // Create world if it doesn't exist
655 if (!loadGameConfAndInitWorld(path, games[gameidx])) {
656 lua_pushstring(L, "Failed to initialize world");
661 lua_pushstring(L, "Invalid game index");
666 /******************************************************************************/
667 int ModApiMainMenu::l_delete_world(lua_State *L)
669 int worldidx = luaL_checkinteger(L,1) -1;
671 std::vector<WorldSpec> worlds = getAvailableWorlds();
673 if ((worldidx >= 0) &&
674 (worldidx < (int) worlds.size())) {
676 WorldSpec spec = worlds[worldidx];
678 std::vector<std::string> paths;
679 paths.push_back(spec.path);
680 fs::GetRecursiveSubPaths(spec.path, paths);
683 if (!fs::DeletePaths(paths)) {
684 lua_pushstring(L, "Failed to delete world");
691 lua_pushstring(L, "Invalid world index");
696 /******************************************************************************/
697 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
699 GUIEngine* engine = getGuiEngine(L);
700 sanity_check(engine != NULL);
702 std::string text = "";
704 if (!lua_isnone(L,1) && !lua_isnil(L,1))
705 text = luaL_checkstring(L, 1);
707 engine->setTopleftText(text);
711 /******************************************************************************/
712 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
714 std::vector<const char *> names;
715 Mapgen::getMapgenNames(&names, lua_toboolean(L, 1));
718 for (size_t i = 0; i != names.size(); i++) {
719 lua_pushstring(L, names[i]);
720 lua_rawseti(L, -2, i + 1);
727 /******************************************************************************/
728 int ModApiMainMenu::l_get_modpath(lua_State *L)
730 std::string modpath = fs::RemoveRelativePathComponents(
731 porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
732 lua_pushstring(L, modpath.c_str());
736 /******************************************************************************/
737 int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
739 std::string modpath = fs::RemoveRelativePathComponents(
740 porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM);
741 lua_pushstring(L, modpath.c_str());
745 /******************************************************************************/
746 int ModApiMainMenu::l_get_gamepath(lua_State *L)
748 std::string gamepath = fs::RemoveRelativePathComponents(
749 porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
750 lua_pushstring(L, gamepath.c_str());
754 /******************************************************************************/
755 int ModApiMainMenu::l_get_texturepath(lua_State *L)
757 std::string gamepath = fs::RemoveRelativePathComponents(
758 porting::path_user + DIR_DELIM + "textures");
759 lua_pushstring(L, gamepath.c_str());
763 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
765 std::string gamepath = fs::RemoveRelativePathComponents(
766 porting::path_share + DIR_DELIM + "textures");
767 lua_pushstring(L, gamepath.c_str());
771 /******************************************************************************/
772 int ModApiMainMenu::l_create_dir(lua_State *L) {
773 const char *path = luaL_checkstring(L, 1);
775 if (ModApiMainMenu::isMinetestPath(path)) {
776 lua_pushboolean(L, fs::CreateAllDirs(path));
780 lua_pushboolean(L, false);
784 /******************************************************************************/
785 int ModApiMainMenu::l_delete_dir(lua_State *L)
787 const char *path = luaL_checkstring(L, 1);
789 std::string absolute_path = fs::RemoveRelativePathComponents(path);
791 if (ModApiMainMenu::isMinetestPath(absolute_path)) {
792 lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
796 lua_pushboolean(L, false);
800 /******************************************************************************/
801 int ModApiMainMenu::l_copy_dir(lua_State *L)
803 const char *source = luaL_checkstring(L, 1);
804 const char *destination = luaL_checkstring(L, 2);
806 bool keep_source = true;
808 if ((!lua_isnone(L,3)) &&
810 keep_source = lua_toboolean(L,3);
813 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
814 std::string absolute_source = fs::RemoveRelativePathComponents(source);
816 if ((ModApiMainMenu::isMinetestPath(absolute_source)) &&
817 (ModApiMainMenu::isMinetestPath(absolute_destination))) {
818 bool retval = fs::CopyDir(absolute_source,absolute_destination);
820 if (retval && (!keep_source)) {
822 retval &= fs::RecursiveDelete(absolute_source);
824 lua_pushboolean(L,retval);
827 lua_pushboolean(L,false);
831 /******************************************************************************/
832 int ModApiMainMenu::l_extract_zip(lua_State *L)
834 const char *zipfile = luaL_checkstring(L, 1);
835 const char *destination = luaL_checkstring(L, 2);
837 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
839 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
840 fs::CreateAllDirs(absolute_destination);
842 io::IFileSystem *fs = RenderingEngine::get_filesystem();
844 if (!fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP)) {
845 lua_pushboolean(L,false);
849 sanity_check(fs->getFileArchiveCount() > 0);
851 /**********************************************************************/
852 /* WARNING this is not threadsafe!! */
853 /**********************************************************************/
854 io::IFileArchive* opened_zip =
855 fs->getFileArchive(fs->getFileArchiveCount()-1);
857 const io::IFileList* files_in_zip = opened_zip->getFileList();
859 unsigned int number_of_files = files_in_zip->getFileCount();
861 for (unsigned int i=0; i < number_of_files; i++) {
862 std::string fullpath = destination;
863 fullpath += DIR_DELIM;
864 fullpath += files_in_zip->getFullFileName(i).c_str();
865 std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
867 if (!files_in_zip->isDirectory(i)) {
868 if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
869 fs->removeFileArchive(fs->getFileArchiveCount()-1);
870 lua_pushboolean(L,false);
874 io::IReadFile* toread = opened_zip->createAndOpenFile(i);
876 FILE *targetfile = fopen(fullpath.c_str(),"wb");
878 if (targetfile == NULL) {
879 fs->removeFileArchive(fs->getFileArchiveCount()-1);
880 lua_pushboolean(L,false);
884 char read_buffer[1024];
887 while (total_read < toread->getSize()) {
889 unsigned int bytes_read =
890 toread->read(read_buffer,sizeof(read_buffer));
891 if ((bytes_read == 0 ) ||
892 (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
895 fs->removeFileArchive(fs->getFileArchiveCount()-1);
896 lua_pushboolean(L,false);
899 total_read += bytes_read;
907 fs->removeFileArchive(fs->getFileArchiveCount()-1);
908 lua_pushboolean(L,true);
912 lua_pushboolean(L,false);
916 /******************************************************************************/
917 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
919 GUIEngine* engine = getGuiEngine(L);
920 sanity_check(engine != NULL);
922 lua_pushstring(L,engine->getScriptDir().c_str());
926 /******************************************************************************/
927 bool ModApiMainMenu::isMinetestPath(std::string path)
929 if (fs::PathStartsWith(path,fs::TempPath()))
933 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games")))
937 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods")))
941 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds")))
948 /******************************************************************************/
949 int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
951 GUIEngine* engine = getGuiEngine(L);
952 sanity_check(engine != NULL);
954 const char *formname= luaL_checkstring(L, 1);
955 const char *title = luaL_checkstring(L, 2);
956 bool is_file_select = lua_toboolean(L, 3);
958 GUIFileSelectMenu* fileOpenMenu =
959 new GUIFileSelectMenu(RenderingEngine::get_gui_env(),
962 engine->m_menumanager,
966 fileOpenMenu->setTextDest(engine->m_buttonhandler);
967 fileOpenMenu->drop();
971 /******************************************************************************/
972 int ModApiMainMenu::l_download_file(lua_State *L)
974 const char *url = luaL_checkstring(L, 1);
975 const char *target = luaL_checkstring(L, 2);
978 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
980 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
981 if (GUIEngine::downloadFile(url,absolute_destination)) {
982 lua_pushboolean(L,true);
986 errorstream << "DOWNLOAD denied: " << absolute_destination
987 << " isn't a allowed path" << std::endl;
989 lua_pushboolean(L,false);
993 /******************************************************************************/
994 int ModApiMainMenu::l_get_video_drivers(lua_State *L)
996 std::vector<irr::video::E_DRIVER_TYPE> drivers = RenderingEngine::getSupportedVideoDrivers();
999 for (u32 i = 0; i != drivers.size(); i++) {
1000 const char *name = RenderingEngine::getVideoDriverName(drivers[i]);
1001 const char *fname = RenderingEngine::getVideoDriverFriendlyName(drivers[i]);
1004 lua_pushstring(L, name);
1005 lua_setfield(L, -2, "name");
1006 lua_pushstring(L, fname);
1007 lua_setfield(L, -2, "friendly_name");
1009 lua_rawseti(L, -2, i + 1);
1015 /******************************************************************************/
1016 int ModApiMainMenu::l_get_video_modes(lua_State *L)
1018 std::vector<core::vector3d<u32> > videomodes
1019 = RenderingEngine::getSupportedVideoModes();
1022 for (u32 i = 0; i != videomodes.size(); i++) {
1024 lua_pushnumber(L, videomodes[i].X);
1025 lua_setfield(L, -2, "w");
1026 lua_pushnumber(L, videomodes[i].Y);
1027 lua_setfield(L, -2, "h");
1028 lua_pushnumber(L, videomodes[i].Z);
1029 lua_setfield(L, -2, "depth");
1031 lua_rawseti(L, -2, i + 1);
1037 /******************************************************************************/
1038 int ModApiMainMenu::l_gettext(lua_State *L)
1040 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
1041 lua_pushstring(L, text.c_str());
1046 /******************************************************************************/
1047 int ModApiMainMenu::l_get_screen_info(lua_State *L)
1050 int top = lua_gettop(L);
1051 lua_pushstring(L,"density");
1052 lua_pushnumber(L,RenderingEngine::getDisplayDensity());
1053 lua_settable(L, top);
1055 lua_pushstring(L,"display_width");
1056 lua_pushnumber(L,RenderingEngine::getDisplaySize().X);
1057 lua_settable(L, top);
1059 lua_pushstring(L,"display_height");
1060 lua_pushnumber(L,RenderingEngine::getDisplaySize().Y);
1061 lua_settable(L, top);
1063 const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
1064 lua_pushstring(L,"window_width");
1065 lua_pushnumber(L, window_size.X);
1066 lua_settable(L, top);
1068 lua_pushstring(L,"window_height");
1069 lua_pushnumber(L, window_size.Y);
1070 lua_settable(L, top);
1074 /******************************************************************************/
1075 int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
1077 u16 proto_version_min = g_settings->getFlag("send_pre_v25_init") ?
1078 CLIENT_PROTOCOL_VERSION_MIN_LEGACY : CLIENT_PROTOCOL_VERSION_MIN;
1079 lua_pushinteger(L, proto_version_min);
1083 int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
1085 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX);
1089 /******************************************************************************/
1090 int ModApiMainMenu::l_do_async_callback(lua_State *L)
1092 GUIEngine* engine = getGuiEngine(L);
1094 size_t func_length, param_length;
1095 const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
1097 const char* serialized_param_raw = luaL_checklstring(L, 2, ¶m_length);
1099 sanity_check(serialized_func_raw != NULL);
1100 sanity_check(serialized_param_raw != NULL);
1102 std::string serialized_func = std::string(serialized_func_raw, func_length);
1103 std::string serialized_param = std::string(serialized_param_raw, param_length);
1105 lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param));
1110 /******************************************************************************/
1111 void ModApiMainMenu::Initialize(lua_State *L, int top)
1113 API_FCT(update_formspec);
1114 API_FCT(set_clouds);
1115 API_FCT(get_textlist_index);
1116 API_FCT(get_table_index);
1117 API_FCT(get_worlds);
1121 API_FCT(get_favorites);
1122 API_FCT(show_keys_menu);
1123 API_FCT(create_world);
1124 API_FCT(delete_world);
1125 API_FCT(delete_favorite);
1126 API_FCT(set_background);
1127 API_FCT(set_topleft_text);
1128 API_FCT(get_mapgen_names);
1129 API_FCT(get_modpath);
1130 API_FCT(get_clientmodpath);
1131 API_FCT(get_gamepath);
1132 API_FCT(get_texturepath);
1133 API_FCT(get_texturepath_share);
1134 API_FCT(create_dir);
1135 API_FCT(delete_dir);
1137 API_FCT(extract_zip);
1138 API_FCT(get_mainmenu_path);
1139 API_FCT(show_path_select_dialog);
1140 API_FCT(download_file);
1141 API_FCT(get_modstore_details);
1142 API_FCT(get_modstore_list);
1144 API_FCT(get_video_drivers);
1145 API_FCT(get_video_modes);
1146 API_FCT(get_screen_info);
1147 API_FCT(get_min_supp_proto);
1148 API_FCT(get_max_supp_proto);
1149 API_FCT(do_async_callback);
1152 /******************************************************************************/
1153 void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
1156 API_FCT(get_worlds);
1158 API_FCT(get_favorites);
1159 API_FCT(get_mapgen_names);
1160 API_FCT(get_modpath);
1161 API_FCT(get_clientmodpath);
1162 API_FCT(get_gamepath);
1163 API_FCT(get_texturepath);
1164 API_FCT(get_texturepath_share);
1165 API_FCT(create_dir);
1166 API_FCT(delete_dir);
1168 //API_FCT(extract_zip); //TODO remove dependency to GuiEngine
1169 API_FCT(download_file);
1170 API_FCT(get_modstore_details);
1171 API_FCT(get_modstore_list);
1172 //API_FCT(gettext); (gettext lib isn't threadsafe)