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"
31 #include "convert_json.h"
32 #include "content/packages.h"
33 #include "content/content.h"
34 #include "content/subgames.h"
35 #include "serverlist.h"
36 #include "mapgen/mapgen.h"
39 #include <IFileArchive.h>
40 #include <IFileSystem.h>
41 #include "client/renderingengine.h"
42 #include "network/networkprotocol.h"
45 /******************************************************************************/
46 std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
48 lua_getglobal(L, "gamedata");
50 lua_getfield(L, -1, name.c_str());
55 return luaL_checkstring(L, -1);
58 /******************************************************************************/
59 int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
61 lua_getglobal(L, "gamedata");
63 lua_getfield(L, -1, name.c_str());
65 if(lua_isnil(L, -1)) {
71 return luaL_checkinteger(L, -1);
74 /******************************************************************************/
75 int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
77 lua_getglobal(L, "gamedata");
79 lua_getfield(L, -1, name.c_str());
81 if(lua_isnil(L, -1)) {
87 return readParam<bool>(L, -1);
90 /******************************************************************************/
91 int ModApiMainMenu::l_update_formspec(lua_State *L)
93 GUIEngine* engine = getGuiEngine(L);
94 sanity_check(engine != NULL);
96 if (engine->m_startgame)
100 std::string formspec(luaL_checkstring(L, 1));
102 if (engine->m_formspecgui != 0) {
103 engine->m_formspecgui->setForm(formspec);
109 /******************************************************************************/
110 int ModApiMainMenu::l_set_formspec_prepend(lua_State *L)
112 GUIEngine *engine = getGuiEngine(L);
113 sanity_check(engine != NULL);
115 if (engine->m_startgame)
118 std::string formspec(luaL_checkstring(L, 1));
119 engine->setFormspecPrepend(formspec);
124 /******************************************************************************/
125 int ModApiMainMenu::l_start(lua_State *L)
127 GUIEngine* engine = getGuiEngine(L);
128 sanity_check(engine != NULL);
130 //update c++ gamedata from lua table
134 MainMenuData *data = engine->m_data;
136 data->selected_world = getIntegerData(L, "selected_world",valid) -1;
137 data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
138 data->do_reconnect = getBoolData(L, "do_reconnect", valid);
139 if (!data->do_reconnect) {
140 data->name = getTextData(L,"playername");
141 data->password = getTextData(L,"password");
142 data->address = getTextData(L,"address");
143 data->port = getTextData(L,"port");
145 data->serverdescription = getTextData(L,"serverdescription");
146 data->servername = getTextData(L,"servername");
148 //close menu next time
149 engine->m_startgame = true;
153 /******************************************************************************/
154 int ModApiMainMenu::l_close(lua_State *L)
156 GUIEngine* engine = getGuiEngine(L);
157 sanity_check(engine != NULL);
159 engine->m_kill = true;
163 /******************************************************************************/
164 int ModApiMainMenu::l_set_background(lua_State *L)
166 GUIEngine* engine = getGuiEngine(L);
167 sanity_check(engine != NULL);
169 std::string backgroundlevel(luaL_checkstring(L, 1));
170 std::string texturename(luaL_checkstring(L, 2));
172 bool tile_image = false;
174 unsigned int minsize = 16;
176 if (!lua_isnone(L, 3)) {
177 tile_image = readParam<bool>(L, 3);
180 if (!lua_isnone(L, 4)) {
181 minsize = lua_tonumber(L, 4);
184 if (backgroundlevel == "background") {
185 retval |= engine->setTexture(TEX_LAYER_BACKGROUND, texturename,
186 tile_image, minsize);
189 if (backgroundlevel == "overlay") {
190 retval |= engine->setTexture(TEX_LAYER_OVERLAY, texturename,
191 tile_image, minsize);
194 if (backgroundlevel == "header") {
195 retval |= engine->setTexture(TEX_LAYER_HEADER, texturename,
196 tile_image, minsize);
199 if (backgroundlevel == "footer") {
200 retval |= engine->setTexture(TEX_LAYER_FOOTER, texturename,
201 tile_image, minsize);
204 lua_pushboolean(L,retval);
208 /******************************************************************************/
209 int ModApiMainMenu::l_set_clouds(lua_State *L)
211 GUIEngine* engine = getGuiEngine(L);
212 sanity_check(engine != NULL);
214 bool value = readParam<bool>(L,1);
216 engine->m_clouds_enabled = value;
221 /******************************************************************************/
222 int ModApiMainMenu::l_get_textlist_index(lua_State *L)
224 // get_table_index accepts both tables and textlists
225 return l_get_table_index(L);
228 /******************************************************************************/
229 int ModApiMainMenu::l_get_table_index(lua_State *L)
231 GUIEngine* engine = getGuiEngine(L);
232 sanity_check(engine != NULL);
234 std::string tablename(luaL_checkstring(L, 1));
235 GUITable *table = engine->m_menu->getTable(tablename);
236 s32 selection = table ? table->getSelected() : 0;
239 lua_pushinteger(L, selection);
245 /******************************************************************************/
246 int ModApiMainMenu::l_get_worlds(lua_State *L)
248 std::vector<WorldSpec> worlds = getAvailableWorlds();
251 int top = lua_gettop(L);
252 unsigned int index = 1;
254 for (const WorldSpec &world : worlds) {
255 lua_pushnumber(L,index);
258 int top_lvl2 = lua_gettop(L);
260 lua_pushstring(L,"path");
261 lua_pushstring(L, world.path.c_str());
262 lua_settable(L, top_lvl2);
264 lua_pushstring(L,"name");
265 lua_pushstring(L, world.name.c_str());
266 lua_settable(L, top_lvl2);
268 lua_pushstring(L,"gameid");
269 lua_pushstring(L, world.gameid.c_str());
270 lua_settable(L, top_lvl2);
272 lua_settable(L, top);
278 /******************************************************************************/
279 int ModApiMainMenu::l_get_favorites(lua_State *L)
281 std::string listtype = "local";
283 if (!lua_isnone(L, 1)) {
284 listtype = luaL_checkstring(L, 1);
287 std::vector<ServerListSpec> servers;
289 if(listtype == "online") {
290 servers = ServerList::getOnline();
292 servers = ServerList::getLocal();
296 int top = lua_gettop(L);
297 unsigned int index = 1;
299 for (const Json::Value &server : servers) {
301 lua_pushnumber(L, index);
304 int top_lvl2 = lua_gettop(L);
306 if (!server["clients"].asString().empty()) {
307 std::string clients_raw = server["clients"].asString();
309 int numbervalue = strtol(clients_raw.c_str(), &endptr,10);
311 if ((!clients_raw.empty()) && (*endptr == 0)) {
312 lua_pushstring(L, "clients");
313 lua_pushnumber(L, numbervalue);
314 lua_settable(L, top_lvl2);
318 if (!server["clients_max"].asString().empty()) {
320 std::string clients_max_raw = server["clients_max"].asString();
322 int numbervalue = strtol(clients_max_raw.c_str(), &endptr,10);
324 if ((!clients_max_raw.empty()) && (*endptr == 0)) {
325 lua_pushstring(L, "clients_max");
326 lua_pushnumber(L, numbervalue);
327 lua_settable(L, top_lvl2);
331 if (!server["version"].asString().empty()) {
332 lua_pushstring(L, "version");
333 std::string topush = server["version"].asString();
334 lua_pushstring(L, topush.c_str());
335 lua_settable(L, top_lvl2);
338 if (!server["proto_min"].asString().empty()) {
339 lua_pushstring(L, "proto_min");
340 lua_pushinteger(L, server["proto_min"].asInt());
341 lua_settable(L, top_lvl2);
344 if (!server["proto_max"].asString().empty()) {
345 lua_pushstring(L, "proto_max");
346 lua_pushinteger(L, server["proto_max"].asInt());
347 lua_settable(L, top_lvl2);
350 if (!server["password"].asString().empty()) {
351 lua_pushstring(L, "password");
352 lua_pushboolean(L, server["password"].asBool());
353 lua_settable(L, top_lvl2);
356 if (!server["creative"].asString().empty()) {
357 lua_pushstring(L, "creative");
358 lua_pushboolean(L, server["creative"].asBool());
359 lua_settable(L, top_lvl2);
362 if (!server["damage"].asString().empty()) {
363 lua_pushstring(L, "damage");
364 lua_pushboolean(L, server["damage"].asBool());
365 lua_settable(L, top_lvl2);
368 if (!server["pvp"].asString().empty()) {
369 lua_pushstring(L, "pvp");
370 lua_pushboolean(L, server["pvp"].asBool());
371 lua_settable(L, top_lvl2);
374 if (!server["description"].asString().empty()) {
375 lua_pushstring(L, "description");
376 std::string topush = server["description"].asString();
377 lua_pushstring(L, topush.c_str());
378 lua_settable(L, top_lvl2);
381 if (!server["name"].asString().empty()) {
382 lua_pushstring(L, "name");
383 std::string topush = server["name"].asString();
384 lua_pushstring(L, topush.c_str());
385 lua_settable(L, top_lvl2);
388 if (!server["address"].asString().empty()) {
389 lua_pushstring(L, "address");
390 std::string topush = server["address"].asString();
391 lua_pushstring(L, topush.c_str());
392 lua_settable(L, top_lvl2);
395 if (!server["port"].asString().empty()) {
396 lua_pushstring(L, "port");
397 std::string topush = server["port"].asString();
398 lua_pushstring(L, topush.c_str());
399 lua_settable(L, top_lvl2);
402 if (server.isMember("ping")) {
403 float ping = server["ping"].asFloat();
404 lua_pushstring(L, "ping");
405 lua_pushnumber(L, ping);
406 lua_settable(L, top_lvl2);
409 if (server["clients_list"].isArray()) {
410 unsigned int index_lvl2 = 1;
411 lua_pushstring(L, "clients_list");
413 int top_lvl3 = lua_gettop(L);
414 for (const Json::Value &client : server["clients_list"]) {
415 lua_pushnumber(L, index_lvl2);
416 std::string topush = client.asString();
417 lua_pushstring(L, topush.c_str());
418 lua_settable(L, top_lvl3);
421 lua_settable(L, top_lvl2);
424 if (server["mods"].isArray()) {
425 unsigned int index_lvl2 = 1;
426 lua_pushstring(L, "mods");
428 int top_lvl3 = lua_gettop(L);
429 for (const Json::Value &mod : server["mods"]) {
431 lua_pushnumber(L, index_lvl2);
432 std::string topush = mod.asString();
433 lua_pushstring(L, topush.c_str());
434 lua_settable(L, top_lvl3);
437 lua_settable(L, top_lvl2);
440 lua_settable(L, top);
446 /******************************************************************************/
447 int ModApiMainMenu::l_delete_favorite(lua_State *L)
449 std::vector<ServerListSpec> servers;
451 std::string listtype = "local";
453 if (!lua_isnone(L,2)) {
454 listtype = luaL_checkstring(L,2);
457 if ((listtype != "local") &&
458 (listtype != "online"))
462 if(listtype == "online") {
463 servers = ServerList::getOnline();
465 servers = ServerList::getLocal();
468 int fav_idx = luaL_checkinteger(L,1) -1;
470 if ((fav_idx >= 0) &&
471 (fav_idx < (int) servers.size())) {
473 ServerList::deleteEntry(servers[fav_idx]);
479 /******************************************************************************/
480 int ModApiMainMenu::l_get_games(lua_State *L)
482 std::vector<SubgameSpec> games = getAvailableGames();
485 int top = lua_gettop(L);
486 unsigned int index = 1;
488 for (const SubgameSpec &game : games) {
489 lua_pushnumber(L, index);
491 int top_lvl2 = lua_gettop(L);
493 lua_pushstring(L, "id");
494 lua_pushstring(L, game.id.c_str());
495 lua_settable(L, top_lvl2);
497 lua_pushstring(L, "path");
498 lua_pushstring(L, game.path.c_str());
499 lua_settable(L, top_lvl2);
501 lua_pushstring(L, "type");
502 lua_pushstring(L, "game");
503 lua_settable(L, top_lvl2);
505 lua_pushstring(L, "gamemods_path");
506 lua_pushstring(L, game.gamemods_path.c_str());
507 lua_settable(L, top_lvl2);
509 lua_pushstring(L, "name");
510 lua_pushstring(L, game.name.c_str());
511 lua_settable(L, top_lvl2);
513 lua_pushstring(L, "author");
514 lua_pushstring(L, game.author.c_str());
515 lua_settable(L, top_lvl2);
517 lua_pushstring(L, "release");
518 lua_pushinteger(L, game.release);
519 lua_settable(L, top_lvl2);
521 lua_pushstring(L, "menuicon_path");
522 lua_pushstring(L, game.menuicon_path.c_str());
523 lua_settable(L, top_lvl2);
525 lua_pushstring(L, "addon_mods_paths");
527 int table2 = lua_gettop(L);
528 int internal_index = 1;
529 for (const std::string &addon_mods_path : game.addon_mods_paths) {
530 lua_pushnumber(L, internal_index);
531 lua_pushstring(L, addon_mods_path.c_str());
532 lua_settable(L, table2);
535 lua_settable(L, top_lvl2);
536 lua_settable(L, top);
542 /******************************************************************************/
543 int ModApiMainMenu::l_get_content_info(lua_State *L)
545 std::string path = luaL_checkstring(L, 1);
549 parseContentInfo(spec);
553 lua_pushstring(L, spec.name.c_str());
554 lua_setfield(L, -2, "name");
556 lua_pushstring(L, spec.type.c_str());
557 lua_setfield(L, -2, "type");
559 lua_pushstring(L, spec.author.c_str());
560 lua_setfield(L, -2, "author");
562 lua_pushinteger(L, spec.release);
563 lua_setfield(L, -2, "release");
565 lua_pushstring(L, spec.desc.c_str());
566 lua_setfield(L, -2, "description");
568 lua_pushstring(L, spec.path.c_str());
569 lua_setfield(L, -2, "path");
571 if (spec.type == "mod") {
574 parseModContents(spec);
579 for (const auto &dep : spec.depends) {
580 lua_pushstring(L, dep.c_str());
581 lua_rawseti(L, -2, i++);
583 lua_setfield(L, -2, "depends");
585 // Optional Dependencies
588 for (const auto &dep : spec.optdepends) {
589 lua_pushstring(L, dep.c_str());
590 lua_rawseti(L, -2, i++);
592 lua_setfield(L, -2, "optional_depends");
598 /******************************************************************************/
599 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
601 GUIEngine* engine = getGuiEngine(L);
602 sanity_check(engine != NULL);
604 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
607 engine->m_menumanager,
608 engine->m_texture_source);
613 /******************************************************************************/
614 int ModApiMainMenu::l_create_world(lua_State *L)
616 const char *name = luaL_checkstring(L, 1);
617 int gameidx = luaL_checkinteger(L,2) -1;
619 std::string path = porting::path_user + DIR_DELIM
623 std::vector<SubgameSpec> games = getAvailableGames();
625 if ((gameidx >= 0) &&
626 (gameidx < (int) games.size())) {
628 // Create world if it doesn't exist
629 if (!loadGameConfAndInitWorld(path, games[gameidx])) {
630 lua_pushstring(L, "Failed to initialize world");
635 lua_pushstring(L, "Invalid game index");
640 /******************************************************************************/
641 int ModApiMainMenu::l_delete_world(lua_State *L)
643 int world_id = luaL_checkinteger(L, 1) - 1;
644 std::vector<WorldSpec> worlds = getAvailableWorlds();
645 if (world_id < 0 || world_id >= (int) worlds.size()) {
646 lua_pushstring(L, "Invalid world index");
649 const WorldSpec &spec = worlds[world_id];
650 if (!fs::RecursiveDelete(spec.path)) {
651 lua_pushstring(L, "Failed to delete world");
657 /******************************************************************************/
658 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
660 GUIEngine* engine = getGuiEngine(L);
661 sanity_check(engine != NULL);
665 if (!lua_isnone(L,1) && !lua_isnil(L,1))
666 text = luaL_checkstring(L, 1);
668 engine->setTopleftText(text);
672 /******************************************************************************/
673 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
675 std::vector<const char *> names;
676 bool include_hidden = lua_isboolean(L, 1) && readParam<bool>(L, 1);
677 Mapgen::getMapgenNames(&names, include_hidden);
680 for (size_t i = 0; i != names.size(); i++) {
681 lua_pushstring(L, names[i]);
682 lua_rawseti(L, -2, i + 1);
689 /******************************************************************************/
690 int ModApiMainMenu::l_get_modpath(lua_State *L)
692 std::string modpath = fs::RemoveRelativePathComponents(
693 porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
694 lua_pushstring(L, modpath.c_str());
698 /******************************************************************************/
699 int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
701 std::string modpath = fs::RemoveRelativePathComponents(
702 porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM);
703 lua_pushstring(L, modpath.c_str());
707 /******************************************************************************/
708 int ModApiMainMenu::l_get_gamepath(lua_State *L)
710 std::string gamepath = fs::RemoveRelativePathComponents(
711 porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
712 lua_pushstring(L, gamepath.c_str());
716 /******************************************************************************/
717 int ModApiMainMenu::l_get_texturepath(lua_State *L)
719 std::string gamepath = fs::RemoveRelativePathComponents(
720 porting::path_user + DIR_DELIM + "textures");
721 lua_pushstring(L, gamepath.c_str());
725 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
727 std::string gamepath = fs::RemoveRelativePathComponents(
728 porting::path_share + DIR_DELIM + "textures");
729 lua_pushstring(L, gamepath.c_str());
733 int ModApiMainMenu::l_get_cache_path(lua_State *L)
735 lua_pushstring(L, fs::RemoveRelativePathComponents(porting::path_cache).c_str());
739 /******************************************************************************/
740 int ModApiMainMenu::l_create_dir(lua_State *L) {
741 const char *path = luaL_checkstring(L, 1);
743 if (ModApiMainMenu::mayModifyPath(path)) {
744 lua_pushboolean(L, fs::CreateAllDirs(path));
748 lua_pushboolean(L, false);
752 /******************************************************************************/
753 int ModApiMainMenu::l_delete_dir(lua_State *L)
755 const char *path = luaL_checkstring(L, 1);
757 std::string absolute_path = fs::RemoveRelativePathComponents(path);
759 if (ModApiMainMenu::mayModifyPath(absolute_path)) {
760 lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
764 lua_pushboolean(L, false);
768 /******************************************************************************/
769 int ModApiMainMenu::l_copy_dir(lua_State *L)
771 const char *source = luaL_checkstring(L, 1);
772 const char *destination = luaL_checkstring(L, 2);
774 bool keep_source = true;
776 if ((!lua_isnone(L,3)) &&
778 keep_source = readParam<bool>(L,3);
781 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
782 std::string absolute_source = fs::RemoveRelativePathComponents(source);
784 if ((ModApiMainMenu::mayModifyPath(absolute_destination))) {
785 bool retval = fs::CopyDir(absolute_source,absolute_destination);
787 if (retval && (!keep_source)) {
789 retval &= fs::RecursiveDelete(absolute_source);
791 lua_pushboolean(L,retval);
794 lua_pushboolean(L,false);
798 /******************************************************************************/
799 int ModApiMainMenu::l_extract_zip(lua_State *L)
801 const char *zipfile = luaL_checkstring(L, 1);
802 const char *destination = luaL_checkstring(L, 2);
804 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
806 if (ModApiMainMenu::mayModifyPath(absolute_destination)) {
807 fs::CreateAllDirs(absolute_destination);
809 io::IFileSystem *fs = RenderingEngine::get_filesystem();
811 if (!fs->addFileArchive(zipfile, false, false, io::EFAT_ZIP)) {
812 lua_pushboolean(L,false);
816 sanity_check(fs->getFileArchiveCount() > 0);
818 /**********************************************************************/
819 /* WARNING this is not threadsafe!! */
820 /**********************************************************************/
821 io::IFileArchive* opened_zip =
822 fs->getFileArchive(fs->getFileArchiveCount()-1);
824 const io::IFileList* files_in_zip = opened_zip->getFileList();
826 unsigned int number_of_files = files_in_zip->getFileCount();
828 for (unsigned int i=0; i < number_of_files; i++) {
829 std::string fullpath = destination;
830 fullpath += DIR_DELIM;
831 fullpath += files_in_zip->getFullFileName(i).c_str();
832 std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
834 if (!files_in_zip->isDirectory(i)) {
835 if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
836 fs->removeFileArchive(fs->getFileArchiveCount()-1);
837 lua_pushboolean(L,false);
841 io::IReadFile* toread = opened_zip->createAndOpenFile(i);
843 FILE *targetfile = fopen(fullpath.c_str(),"wb");
845 if (targetfile == NULL) {
846 fs->removeFileArchive(fs->getFileArchiveCount()-1);
847 lua_pushboolean(L,false);
851 char read_buffer[1024];
854 while (total_read < toread->getSize()) {
856 unsigned int bytes_read =
857 toread->read(read_buffer,sizeof(read_buffer));
858 if ((bytes_read == 0 ) ||
859 (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
862 fs->removeFileArchive(fs->getFileArchiveCount()-1);
863 lua_pushboolean(L,false);
866 total_read += bytes_read;
874 fs->removeFileArchive(fs->getFileArchiveCount()-1);
875 lua_pushboolean(L,true);
879 lua_pushboolean(L,false);
883 /******************************************************************************/
884 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
886 GUIEngine* engine = getGuiEngine(L);
887 sanity_check(engine != NULL);
889 lua_pushstring(L,engine->getScriptDir().c_str());
893 /******************************************************************************/
894 bool ModApiMainMenu::mayModifyPath(const std::string &path)
896 if (fs::PathStartsWith(path, fs::TempPath()))
899 if (fs::PathStartsWith(path, fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM "games")))
902 if (fs::PathStartsWith(path, fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM "mods")))
905 if (fs::PathStartsWith(path, fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM "textures")))
908 if (fs::PathStartsWith(path, fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM "worlds")))
911 if (fs::PathStartsWith(path, fs::RemoveRelativePathComponents(porting::path_cache)))
918 /******************************************************************************/
919 int ModApiMainMenu::l_may_modify_path(lua_State *L)
921 const char *target = luaL_checkstring(L, 1);
922 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
923 lua_pushboolean(L, ModApiMainMenu::mayModifyPath(absolute_destination));
927 /******************************************************************************/
928 int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
930 GUIEngine* engine = getGuiEngine(L);
931 sanity_check(engine != NULL);
933 const char *formname= luaL_checkstring(L, 1);
934 const char *title = luaL_checkstring(L, 2);
935 bool is_file_select = readParam<bool>(L, 3);
937 GUIFileSelectMenu* fileOpenMenu =
938 new GUIFileSelectMenu(RenderingEngine::get_gui_env(),
941 engine->m_menumanager,
945 fileOpenMenu->setTextDest(engine->m_buttonhandler);
946 fileOpenMenu->drop();
950 /******************************************************************************/
951 int ModApiMainMenu::l_download_file(lua_State *L)
953 const char *url = luaL_checkstring(L, 1);
954 const char *target = luaL_checkstring(L, 2);
957 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
959 if (ModApiMainMenu::mayModifyPath(absolute_destination)) {
960 if (GUIEngine::downloadFile(url,absolute_destination)) {
961 lua_pushboolean(L,true);
965 errorstream << "DOWNLOAD denied: " << absolute_destination
966 << " isn't a allowed path" << std::endl;
968 lua_pushboolean(L,false);
972 /******************************************************************************/
973 int ModApiMainMenu::l_get_video_drivers(lua_State *L)
975 std::vector<irr::video::E_DRIVER_TYPE> drivers = RenderingEngine::getSupportedVideoDrivers();
978 for (u32 i = 0; i != drivers.size(); i++) {
979 const char *name = RenderingEngine::getVideoDriverName(drivers[i]);
980 const char *fname = RenderingEngine::getVideoDriverFriendlyName(drivers[i]);
983 lua_pushstring(L, name);
984 lua_setfield(L, -2, "name");
985 lua_pushstring(L, fname);
986 lua_setfield(L, -2, "friendly_name");
988 lua_rawseti(L, -2, i + 1);
994 /******************************************************************************/
995 int ModApiMainMenu::l_get_video_modes(lua_State *L)
997 std::vector<core::vector3d<u32> > videomodes
998 = RenderingEngine::getSupportedVideoModes();
1001 for (u32 i = 0; i != videomodes.size(); i++) {
1003 lua_pushnumber(L, videomodes[i].X);
1004 lua_setfield(L, -2, "w");
1005 lua_pushnumber(L, videomodes[i].Y);
1006 lua_setfield(L, -2, "h");
1007 lua_pushnumber(L, videomodes[i].Z);
1008 lua_setfield(L, -2, "depth");
1010 lua_rawseti(L, -2, i + 1);
1016 /******************************************************************************/
1017 int ModApiMainMenu::l_gettext(lua_State *L)
1019 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
1020 lua_pushstring(L, text.c_str());
1025 /******************************************************************************/
1026 int ModApiMainMenu::l_get_screen_info(lua_State *L)
1029 int top = lua_gettop(L);
1030 lua_pushstring(L,"density");
1031 lua_pushnumber(L,RenderingEngine::getDisplayDensity());
1032 lua_settable(L, top);
1034 lua_pushstring(L,"display_width");
1035 lua_pushnumber(L,RenderingEngine::getDisplaySize().X);
1036 lua_settable(L, top);
1038 lua_pushstring(L,"display_height");
1039 lua_pushnumber(L,RenderingEngine::getDisplaySize().Y);
1040 lua_settable(L, top);
1042 const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
1043 lua_pushstring(L,"window_width");
1044 lua_pushnumber(L, window_size.X);
1045 lua_settable(L, top);
1047 lua_pushstring(L,"window_height");
1048 lua_pushnumber(L, window_size.Y);
1049 lua_settable(L, top);
1053 /******************************************************************************/
1054 int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
1056 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MIN);
1060 int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
1062 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX);
1066 /******************************************************************************/
1067 int ModApiMainMenu::l_do_async_callback(lua_State *L)
1069 GUIEngine* engine = getGuiEngine(L);
1071 size_t func_length, param_length;
1072 const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
1074 const char* serialized_param_raw = luaL_checklstring(L, 2, ¶m_length);
1076 sanity_check(serialized_func_raw != NULL);
1077 sanity_check(serialized_param_raw != NULL);
1079 std::string serialized_func = std::string(serialized_func_raw, func_length);
1080 std::string serialized_param = std::string(serialized_param_raw, param_length);
1082 lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param));
1087 /******************************************************************************/
1088 void ModApiMainMenu::Initialize(lua_State *L, int top)
1090 API_FCT(update_formspec);
1091 API_FCT(set_formspec_prepend);
1092 API_FCT(set_clouds);
1093 API_FCT(get_textlist_index);
1094 API_FCT(get_table_index);
1095 API_FCT(get_worlds);
1097 API_FCT(get_content_info);
1100 API_FCT(get_favorites);
1101 API_FCT(show_keys_menu);
1102 API_FCT(create_world);
1103 API_FCT(delete_world);
1104 API_FCT(delete_favorite);
1105 API_FCT(set_background);
1106 API_FCT(set_topleft_text);
1107 API_FCT(get_mapgen_names);
1108 API_FCT(get_modpath);
1109 API_FCT(get_clientmodpath);
1110 API_FCT(get_gamepath);
1111 API_FCT(get_texturepath);
1112 API_FCT(get_texturepath_share);
1113 API_FCT(get_cache_path);
1114 API_FCT(create_dir);
1115 API_FCT(delete_dir);
1117 API_FCT(extract_zip);
1118 API_FCT(may_modify_path);
1119 API_FCT(get_mainmenu_path);
1120 API_FCT(show_path_select_dialog);
1121 API_FCT(download_file);
1123 API_FCT(get_video_drivers);
1124 API_FCT(get_video_modes);
1125 API_FCT(get_screen_info);
1126 API_FCT(get_min_supp_proto);
1127 API_FCT(get_max_supp_proto);
1128 API_FCT(do_async_callback);
1131 /******************************************************************************/
1132 void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
1134 API_FCT(get_worlds);
1136 API_FCT(get_favorites);
1137 API_FCT(get_mapgen_names);
1138 API_FCT(get_modpath);
1139 API_FCT(get_clientmodpath);
1140 API_FCT(get_gamepath);
1141 API_FCT(get_texturepath);
1142 API_FCT(get_texturepath_share);
1143 API_FCT(get_cache_path);
1144 API_FCT(create_dir);
1145 API_FCT(delete_dir);
1147 //API_FCT(extract_zip); //TODO remove dependency to GuiEngine
1148 API_FCT(may_modify_path);
1149 API_FCT(download_file);
1150 //API_FCT(gettext); (gettext lib isn't threadsafe)