Move files to subdirectories (#6599)
[oweals/minetest.git] / src / script / lua_api / l_mainmenu.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 sapier
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "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"
28 #include "subgame.h"
29 #include "version.h"
30 #include "porting.h"
31 #include "filesys.h"
32 #include "convert_json.h"
33 #include "serverlist.h"
34 #include "mapgen/mapgen.h"
35 #include "settings.h"
36
37 #include <IFileArchive.h>
38 #include <IFileSystem.h>
39 #include "client/renderingengine.h"
40
41
42 /******************************************************************************/
43 std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
44 {
45         lua_getglobal(L, "gamedata");
46
47         lua_getfield(L, -1, name.c_str());
48
49         if(lua_isnil(L, -1))
50                 return "";
51
52         return luaL_checkstring(L, -1);
53 }
54
55 /******************************************************************************/
56 int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
57 {
58         lua_getglobal(L, "gamedata");
59
60         lua_getfield(L, -1, name.c_str());
61
62         if(lua_isnil(L, -1)) {
63                 valid = false;
64                 return -1;
65                 }
66
67         valid = true;
68         return luaL_checkinteger(L, -1);
69 }
70
71 /******************************************************************************/
72 int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
73 {
74         lua_getglobal(L, "gamedata");
75
76         lua_getfield(L, -1, name.c_str());
77
78         if(lua_isnil(L, -1)) {
79                 valid = false;
80                 return false;
81                 }
82
83         valid = true;
84         return lua_toboolean(L, -1);
85 }
86
87 /******************************************************************************/
88 int ModApiMainMenu::l_update_formspec(lua_State *L)
89 {
90         GUIEngine* engine = getGuiEngine(L);
91         sanity_check(engine != NULL);
92
93         if (engine->m_startgame)
94                 return 0;
95
96         //read formspec
97         std::string formspec(luaL_checkstring(L, 1));
98
99         if (engine->m_formspecgui != 0) {
100                 engine->m_formspecgui->setForm(formspec);
101         }
102
103         return 0;
104 }
105
106 /******************************************************************************/
107 int ModApiMainMenu::l_start(lua_State *L)
108 {
109         GUIEngine* engine = getGuiEngine(L);
110         sanity_check(engine != NULL);
111
112         //update c++ gamedata from lua table
113
114         bool valid = false;
115
116         MainMenuData *data = engine->m_data;
117
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");
126         }
127         data->serverdescription = getTextData(L,"serverdescription");
128         data->servername        = getTextData(L,"servername");
129
130         //close menu next time
131         engine->m_startgame = true;
132         return 0;
133 }
134
135 /******************************************************************************/
136 int ModApiMainMenu::l_close(lua_State *L)
137 {
138         GUIEngine* engine = getGuiEngine(L);
139         sanity_check(engine != NULL);
140
141         engine->m_kill = true;
142         return 0;
143 }
144
145 /******************************************************************************/
146 int ModApiMainMenu::l_set_background(lua_State *L)
147 {
148         GUIEngine* engine = getGuiEngine(L);
149         sanity_check(engine != NULL);
150
151         std::string backgroundlevel(luaL_checkstring(L, 1));
152         std::string texturename(luaL_checkstring(L, 2));
153
154         bool tile_image = false;
155         bool retval     = false;
156         unsigned int minsize = 16;
157
158         if (!lua_isnone(L, 3)) {
159                 tile_image = lua_toboolean(L, 3);
160         }
161
162         if (!lua_isnone(L, 4)) {
163                 minsize = lua_tonumber(L, 4);
164         }
165
166         if (backgroundlevel == "background") {
167                 retval |= engine->setTexture(TEX_LAYER_BACKGROUND, texturename,
168                                 tile_image, minsize);
169         }
170
171         if (backgroundlevel == "overlay") {
172                 retval |= engine->setTexture(TEX_LAYER_OVERLAY, texturename,
173                                 tile_image, minsize);
174         }
175
176         if (backgroundlevel == "header") {
177                 retval |= engine->setTexture(TEX_LAYER_HEADER,  texturename,
178                                 tile_image, minsize);
179         }
180
181         if (backgroundlevel == "footer") {
182                 retval |= engine->setTexture(TEX_LAYER_FOOTER, texturename,
183                                 tile_image, minsize);
184         }
185
186         lua_pushboolean(L,retval);
187         return 1;
188 }
189
190 /******************************************************************************/
191 int ModApiMainMenu::l_set_clouds(lua_State *L)
192 {
193         GUIEngine* engine = getGuiEngine(L);
194         sanity_check(engine != NULL);
195
196         bool value = lua_toboolean(L,1);
197
198         engine->m_clouds_enabled = value;
199
200         return 0;
201 }
202
203 /******************************************************************************/
204 int ModApiMainMenu::l_get_textlist_index(lua_State *L)
205 {
206         // get_table_index accepts both tables and textlists
207         return l_get_table_index(L);
208 }
209
210 /******************************************************************************/
211 int ModApiMainMenu::l_get_table_index(lua_State *L)
212 {
213         GUIEngine* engine = getGuiEngine(L);
214         sanity_check(engine != NULL);
215
216         std::string tablename(luaL_checkstring(L, 1));
217         GUITable *table = engine->m_menu->getTable(tablename);
218         s32 selection = table ? table->getSelected() : 0;
219
220         if (selection >= 1)
221                 lua_pushinteger(L, selection);
222         else
223                 lua_pushnil(L);
224         return 1;
225 }
226
227 /******************************************************************************/
228 int ModApiMainMenu::l_get_worlds(lua_State *L)
229 {
230         std::vector<WorldSpec> worlds = getAvailableWorlds();
231
232         lua_newtable(L);
233         int top = lua_gettop(L);
234         unsigned int index = 1;
235
236         for (const WorldSpec &world : worlds) {
237                 lua_pushnumber(L,index);
238
239                 lua_newtable(L);
240                 int top_lvl2 = lua_gettop(L);
241
242                 lua_pushstring(L,"path");
243                 lua_pushstring(L, world.path.c_str());
244                 lua_settable(L, top_lvl2);
245
246                 lua_pushstring(L,"name");
247                 lua_pushstring(L, world.name.c_str());
248                 lua_settable(L, top_lvl2);
249
250                 lua_pushstring(L,"gameid");
251                 lua_pushstring(L, world.gameid.c_str());
252                 lua_settable(L, top_lvl2);
253
254                 lua_settable(L, top);
255                 index++;
256         }
257         return 1;
258 }
259
260 /******************************************************************************/
261 int ModApiMainMenu::l_get_games(lua_State *L)
262 {
263         std::vector<SubgameSpec> games = getAvailableGames();
264
265         lua_newtable(L);
266         int top = lua_gettop(L);
267         unsigned int index = 1;
268
269         for (const SubgameSpec &game : games) {
270                 lua_pushnumber(L,index);
271                 lua_newtable(L);
272                 int top_lvl2 = lua_gettop(L);
273
274                 lua_pushstring(L,"id");
275                 lua_pushstring(L, game.id.c_str());
276                 lua_settable(L, top_lvl2);
277
278                 lua_pushstring(L,"path");
279                 lua_pushstring(L, game.path.c_str());
280                 lua_settable(L, top_lvl2);
281
282                 lua_pushstring(L,"gamemods_path");
283                 lua_pushstring(L, game.gamemods_path.c_str());
284                 lua_settable(L, top_lvl2);
285
286                 lua_pushstring(L,"name");
287                 lua_pushstring(L, game.name.c_str());
288                 lua_settable(L, top_lvl2);
289
290                 lua_pushstring(L,"menuicon_path");
291                 lua_pushstring(L, game.menuicon_path.c_str());
292                 lua_settable(L, top_lvl2);
293
294                 lua_pushstring(L,"addon_mods_paths");
295                 lua_newtable(L);
296                 int table2 = lua_gettop(L);
297                 int internal_index=1;
298                 for (const std::string &addon_mods_path : game.addon_mods_paths) {
299                         lua_pushnumber(L,internal_index);
300                         lua_pushstring(L, addon_mods_path.c_str());
301                         lua_settable(L, table2);
302                         internal_index++;
303                 }
304                 lua_settable(L, top_lvl2);
305                 lua_settable(L, top);
306                 index++;
307         }
308         return 1;
309 }
310 /******************************************************************************/
311 int ModApiMainMenu::l_get_favorites(lua_State *L)
312 {
313         std::string listtype = "local";
314
315         if (!lua_isnone(L,1)) {
316                 listtype = luaL_checkstring(L,1);
317         }
318
319         std::vector<ServerListSpec> servers;
320
321         if(listtype == "online") {
322                 servers = ServerList::getOnline();
323         } else {
324                 servers = ServerList::getLocal();
325         }
326
327         lua_newtable(L);
328         int top = lua_gettop(L);
329         unsigned int index = 1;
330
331         for (const Json::Value &server : servers) {
332
333                 lua_pushnumber(L,index);
334
335                 lua_newtable(L);
336                 int top_lvl2 = lua_gettop(L);
337
338                 if (!server["clients"].asString().empty()) {
339                         std::string clients_raw = server["clients"].asString();
340                         char* endptr = 0;
341                         int numbervalue = strtol(clients_raw.c_str(),&endptr,10);
342
343                         if ((!clients_raw.empty()) && (*endptr == 0)) {
344                                 lua_pushstring(L,"clients");
345                                 lua_pushnumber(L,numbervalue);
346                                 lua_settable(L, top_lvl2);
347                         }
348                 }
349
350                 if (!server["clients_max"].asString().empty()) {
351
352                         std::string clients_max_raw = server["clients_max"].asString();
353                         char* endptr = 0;
354                         int numbervalue = strtol(clients_max_raw.c_str(),&endptr,10);
355
356                         if ((!clients_max_raw.empty()) && (*endptr == 0)) {
357                                 lua_pushstring(L,"clients_max");
358                                 lua_pushnumber(L,numbervalue);
359                                 lua_settable(L, top_lvl2);
360                         }
361                 }
362
363                 if (!server["version"].asString().empty()) {
364                         lua_pushstring(L,"version");
365                         std::string topush = server["version"].asString();
366                         lua_pushstring(L,topush.c_str());
367                         lua_settable(L, top_lvl2);
368                 }
369
370                 if (!server["proto_min"].asString().empty()) {
371                         lua_pushstring(L,"proto_min");
372                         lua_pushinteger(L, server["proto_min"].asInt());
373                         lua_settable(L, top_lvl2);
374                 }
375
376                 if (!server["proto_max"].asString().empty()) {
377                         lua_pushstring(L,"proto_max");
378                         lua_pushinteger(L, server["proto_max"].asInt());
379                         lua_settable(L, top_lvl2);
380                 }
381
382                 if (!server["password"].asString().empty()) {
383                         lua_pushstring(L,"password");
384                         lua_pushboolean(L, server["password"].asBool());
385                         lua_settable(L, top_lvl2);
386                 }
387
388                 if (!server["creative"].asString().empty()) {
389                         lua_pushstring(L,"creative");
390                         lua_pushboolean(L, server["creative"].asBool());
391                         lua_settable(L, top_lvl2);
392                 }
393
394                 if (!server["damage"].asString().empty()) {
395                         lua_pushstring(L,"damage");
396                         lua_pushboolean(L, server["damage"].asBool());
397                         lua_settable(L, top_lvl2);
398                 }
399
400                 if (!server["pvp"].asString().empty()) {
401                         lua_pushstring(L,"pvp");
402                         lua_pushboolean(L, server["pvp"].asBool());
403                         lua_settable(L, top_lvl2);
404                 }
405
406                 if (!server["description"].asString().empty()) {
407                         lua_pushstring(L,"description");
408                         std::string topush = server["description"].asString();
409                         lua_pushstring(L,topush.c_str());
410                         lua_settable(L, top_lvl2);
411                 }
412
413                 if (!server["name"].asString().empty()) {
414                         lua_pushstring(L,"name");
415                         std::string topush = server["name"].asString();
416                         lua_pushstring(L,topush.c_str());
417                         lua_settable(L, top_lvl2);
418                 }
419
420                 if (!server["address"].asString().empty()) {
421                         lua_pushstring(L,"address");
422                         std::string topush = server["address"].asString();
423                         lua_pushstring(L,topush.c_str());
424                         lua_settable(L, top_lvl2);
425                 }
426
427                 if (!server["port"].asString().empty()) {
428                         lua_pushstring(L,"port");
429                         std::string topush = server["port"].asString();
430                         lua_pushstring(L,topush.c_str());
431                         lua_settable(L, top_lvl2);
432                 }
433
434                 if (server.isMember("ping")) {
435                         float ping = server["ping"].asFloat();
436                         lua_pushstring(L, "ping");
437                         lua_pushnumber(L, ping);
438                         lua_settable(L, top_lvl2);
439                 }
440
441                 lua_settable(L, top);
442                 index++;
443         }
444         return 1;
445 }
446
447 /******************************************************************************/
448 int ModApiMainMenu::l_delete_favorite(lua_State *L)
449 {
450         std::vector<ServerListSpec> servers;
451
452         std::string listtype = "local";
453
454         if (!lua_isnone(L,2)) {
455                 listtype = luaL_checkstring(L,2);
456         }
457
458         if ((listtype != "local") &&
459                 (listtype != "online"))
460                 return 0;
461
462
463         if(listtype == "online") {
464                 servers = ServerList::getOnline();
465         } else {
466                 servers = ServerList::getLocal();
467         }
468
469         int fav_idx     = luaL_checkinteger(L,1) -1;
470
471         if ((fav_idx >= 0) &&
472                         (fav_idx < (int) servers.size())) {
473
474                 ServerList::deleteEntry(servers[fav_idx]);
475         }
476
477         return 0;
478 }
479
480 /******************************************************************************/
481 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
482 {
483         GUIEngine* engine = getGuiEngine(L);
484         sanity_check(engine != NULL);
485
486         GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
487                                                                 engine->m_parent,
488                                                                 -1,
489                                                                 engine->m_menumanager);
490         kmenu->drop();
491         return 0;
492 }
493
494 /******************************************************************************/
495 int ModApiMainMenu::l_create_world(lua_State *L)
496 {
497         const char *name        = luaL_checkstring(L, 1);
498         int gameidx                     = luaL_checkinteger(L,2) -1;
499
500         std::string path = porting::path_user + DIR_DELIM
501                         "worlds" + DIR_DELIM
502                         + name;
503
504         std::vector<SubgameSpec> games = getAvailableGames();
505
506         if ((gameidx >= 0) &&
507                         (gameidx < (int) games.size())) {
508
509                 // Create world if it doesn't exist
510                 if (!loadGameConfAndInitWorld(path, games[gameidx])) {
511                         lua_pushstring(L, "Failed to initialize world");
512                 } else {
513                         lua_pushnil(L);
514                 }
515         } else {
516                 lua_pushstring(L, "Invalid game index");
517         }
518         return 1;
519 }
520
521 /******************************************************************************/
522 int ModApiMainMenu::l_delete_world(lua_State *L)
523 {
524         int worldidx    = luaL_checkinteger(L,1) -1;
525
526         std::vector<WorldSpec> worlds = getAvailableWorlds();
527
528         if ((worldidx >= 0) &&
529                 (worldidx < (int) worlds.size())) {
530
531                 WorldSpec spec = worlds[worldidx];
532
533                 std::vector<std::string> paths;
534                 paths.push_back(spec.path);
535                 fs::GetRecursiveSubPaths(spec.path, paths);
536
537                 // Delete files
538                 if (!fs::DeletePaths(paths)) {
539                         lua_pushstring(L, "Failed to delete world");
540                 }
541                 else {
542                         lua_pushnil(L);
543                 }
544         }
545         else {
546                 lua_pushstring(L, "Invalid world index");
547         }
548         return 1;
549 }
550
551 /******************************************************************************/
552 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
553 {
554         GUIEngine* engine = getGuiEngine(L);
555         sanity_check(engine != NULL);
556
557         std::string text;
558
559         if (!lua_isnone(L,1) && !lua_isnil(L,1))
560                 text = luaL_checkstring(L, 1);
561
562         engine->setTopleftText(text);
563         return 0;
564 }
565
566 /******************************************************************************/
567 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
568 {
569         std::vector<const char *> names;
570         Mapgen::getMapgenNames(&names, lua_toboolean(L, 1));
571
572         lua_newtable(L);
573         for (size_t i = 0; i != names.size(); i++) {
574                 lua_pushstring(L, names[i]);
575                 lua_rawseti(L, -2, i + 1);
576         }
577
578         return 1;
579 }
580
581
582 /******************************************************************************/
583 int ModApiMainMenu::l_get_modpath(lua_State *L)
584 {
585         std::string modpath = fs::RemoveRelativePathComponents(
586                 porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
587         lua_pushstring(L, modpath.c_str());
588         return 1;
589 }
590
591 /******************************************************************************/
592 int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
593 {
594         std::string modpath = fs::RemoveRelativePathComponents(
595                 porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM);
596         lua_pushstring(L, modpath.c_str());
597         return 1;
598 }
599
600 /******************************************************************************/
601 int ModApiMainMenu::l_get_gamepath(lua_State *L)
602 {
603         std::string gamepath = fs::RemoveRelativePathComponents(
604                 porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
605         lua_pushstring(L, gamepath.c_str());
606         return 1;
607 }
608
609 /******************************************************************************/
610 int ModApiMainMenu::l_get_texturepath(lua_State *L)
611 {
612         std::string gamepath = fs::RemoveRelativePathComponents(
613                 porting::path_user + DIR_DELIM + "textures");
614         lua_pushstring(L, gamepath.c_str());
615         return 1;
616 }
617
618 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
619 {
620         std::string gamepath = fs::RemoveRelativePathComponents(
621                 porting::path_share + DIR_DELIM + "textures");
622         lua_pushstring(L, gamepath.c_str());
623         return 1;
624 }
625
626 /******************************************************************************/
627 int ModApiMainMenu::l_create_dir(lua_State *L) {
628         const char *path = luaL_checkstring(L, 1);
629
630         if (ModApiMainMenu::isMinetestPath(path)) {
631                 lua_pushboolean(L, fs::CreateAllDirs(path));
632                 return 1;
633         }
634
635         lua_pushboolean(L, false);
636         return 1;
637 }
638
639 /******************************************************************************/
640 int ModApiMainMenu::l_delete_dir(lua_State *L)
641 {
642         const char *path = luaL_checkstring(L, 1);
643
644         std::string absolute_path = fs::RemoveRelativePathComponents(path);
645
646         if (ModApiMainMenu::isMinetestPath(absolute_path)) {
647                 lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
648                 return 1;
649         }
650
651         lua_pushboolean(L, false);
652         return 1;
653 }
654
655 /******************************************************************************/
656 int ModApiMainMenu::l_copy_dir(lua_State *L)
657 {
658         const char *source      = luaL_checkstring(L, 1);
659         const char *destination = luaL_checkstring(L, 2);
660
661         bool keep_source = true;
662
663         if ((!lua_isnone(L,3)) &&
664                         (!lua_isnil(L,3))) {
665                 keep_source = lua_toboolean(L,3);
666         }
667
668         std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
669         std::string absolute_source = fs::RemoveRelativePathComponents(source);
670
671         if ((ModApiMainMenu::isMinetestPath(absolute_destination))) {
672                 bool retval = fs::CopyDir(absolute_source,absolute_destination);
673
674                 if (retval && (!keep_source)) {
675
676                         retval &= fs::RecursiveDelete(absolute_source);
677                 }
678                 lua_pushboolean(L,retval);
679                 return 1;
680         }
681         lua_pushboolean(L,false);
682         return 1;
683 }
684
685 /******************************************************************************/
686 int ModApiMainMenu::l_extract_zip(lua_State *L)
687 {
688         const char *zipfile     = luaL_checkstring(L, 1);
689         const char *destination = luaL_checkstring(L, 2);
690
691         std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
692
693         if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
694                 fs::CreateAllDirs(absolute_destination);
695
696                 io::IFileSystem *fs = RenderingEngine::get_filesystem();
697
698                 if (!fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP)) {
699                         lua_pushboolean(L,false);
700                         return 1;
701                 }
702
703                 sanity_check(fs->getFileArchiveCount() > 0);
704
705                 /**********************************************************************/
706                 /* WARNING this is not threadsafe!!                                   */
707                 /**********************************************************************/
708                 io::IFileArchive* opened_zip =
709                         fs->getFileArchive(fs->getFileArchiveCount()-1);
710
711                 const io::IFileList* files_in_zip = opened_zip->getFileList();
712
713                 unsigned int number_of_files = files_in_zip->getFileCount();
714
715                 for (unsigned int i=0; i < number_of_files; i++) {
716                         std::string fullpath = destination;
717                         fullpath += DIR_DELIM;
718                         fullpath += files_in_zip->getFullFileName(i).c_str();
719                         std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
720
721                         if (!files_in_zip->isDirectory(i)) {
722                                 if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
723                                         fs->removeFileArchive(fs->getFileArchiveCount()-1);
724                                         lua_pushboolean(L,false);
725                                         return 1;
726                                 }
727
728                                 io::IReadFile* toread = opened_zip->createAndOpenFile(i);
729
730                                 FILE *targetfile = fopen(fullpath.c_str(),"wb");
731
732                                 if (targetfile == NULL) {
733                                         fs->removeFileArchive(fs->getFileArchiveCount()-1);
734                                         lua_pushboolean(L,false);
735                                         return 1;
736                                 }
737
738                                 char read_buffer[1024];
739                                 long total_read = 0;
740
741                                 while (total_read < toread->getSize()) {
742
743                                         unsigned int bytes_read =
744                                                         toread->read(read_buffer,sizeof(read_buffer));
745                                         if ((bytes_read == 0 ) ||
746                                                 (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
747                                         {
748                                                 fclose(targetfile);
749                                                 fs->removeFileArchive(fs->getFileArchiveCount()-1);
750                                                 lua_pushboolean(L,false);
751                                                 return 1;
752                                         }
753                                         total_read += bytes_read;
754                                 }
755
756                                 fclose(targetfile);
757                         }
758
759                 }
760
761                 fs->removeFileArchive(fs->getFileArchiveCount()-1);
762                 lua_pushboolean(L,true);
763                 return 1;
764         }
765
766         lua_pushboolean(L,false);
767         return 1;
768 }
769
770 /******************************************************************************/
771 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
772 {
773         GUIEngine* engine = getGuiEngine(L);
774         sanity_check(engine != NULL);
775
776         lua_pushstring(L,engine->getScriptDir().c_str());
777         return 1;
778 }
779
780 /******************************************************************************/
781 bool ModApiMainMenu::isMinetestPath(std::string path)
782 {
783         if (fs::PathStartsWith(path,fs::TempPath()))
784                 return true;
785
786         /* games */
787         if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games")))
788                 return true;
789
790         /* mods */
791         if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods")))
792                 return true;
793
794         /* worlds */
795         if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds")))
796                 return true;
797
798
799         return false;
800 }
801
802 /******************************************************************************/
803 int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
804 {
805         GUIEngine* engine = getGuiEngine(L);
806         sanity_check(engine != NULL);
807
808         const char *formname= luaL_checkstring(L, 1);
809         const char *title       = luaL_checkstring(L, 2);
810         bool is_file_select = lua_toboolean(L, 3);
811
812         GUIFileSelectMenu* fileOpenMenu =
813                 new GUIFileSelectMenu(RenderingEngine::get_gui_env(),
814                                                                 engine->m_parent,
815                                                                 -1,
816                                                                 engine->m_menumanager,
817                                                                 title,
818                                                                 formname,
819                                                                 is_file_select);
820         fileOpenMenu->setTextDest(engine->m_buttonhandler);
821         fileOpenMenu->drop();
822         return 0;
823 }
824
825 /******************************************************************************/
826 int ModApiMainMenu::l_download_file(lua_State *L)
827 {
828         const char *url    = luaL_checkstring(L, 1);
829         const char *target = luaL_checkstring(L, 2);
830
831         //check path
832         std::string absolute_destination = fs::RemoveRelativePathComponents(target);
833
834         if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
835                 if (GUIEngine::downloadFile(url,absolute_destination)) {
836                         lua_pushboolean(L,true);
837                         return 1;
838                 }
839         } else {
840                 errorstream << "DOWNLOAD denied: " << absolute_destination
841                                 << " isn't a allowed path" << std::endl;
842         }
843         lua_pushboolean(L,false);
844         return 1;
845 }
846
847 /******************************************************************************/
848 int ModApiMainMenu::l_get_video_drivers(lua_State *L)
849 {
850         std::vector<irr::video::E_DRIVER_TYPE> drivers = RenderingEngine::getSupportedVideoDrivers();
851
852         lua_newtable(L);
853         for (u32 i = 0; i != drivers.size(); i++) {
854                 const char *name  = RenderingEngine::getVideoDriverName(drivers[i]);
855                 const char *fname = RenderingEngine::getVideoDriverFriendlyName(drivers[i]);
856
857                 lua_newtable(L);
858                 lua_pushstring(L, name);
859                 lua_setfield(L, -2, "name");
860                 lua_pushstring(L, fname);
861                 lua_setfield(L, -2, "friendly_name");
862
863                 lua_rawseti(L, -2, i + 1);
864         }
865
866         return 1;
867 }
868
869 /******************************************************************************/
870 int ModApiMainMenu::l_get_video_modes(lua_State *L)
871 {
872         std::vector<core::vector3d<u32> > videomodes
873                 = RenderingEngine::getSupportedVideoModes();
874
875         lua_newtable(L);
876         for (u32 i = 0; i != videomodes.size(); i++) {
877                 lua_newtable(L);
878                 lua_pushnumber(L, videomodes[i].X);
879                 lua_setfield(L, -2, "w");
880                 lua_pushnumber(L, videomodes[i].Y);
881                 lua_setfield(L, -2, "h");
882                 lua_pushnumber(L, videomodes[i].Z);
883                 lua_setfield(L, -2, "depth");
884
885                 lua_rawseti(L, -2, i + 1);
886         }
887
888         return 1;
889 }
890
891 /******************************************************************************/
892 int ModApiMainMenu::l_gettext(lua_State *L)
893 {
894         std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
895         lua_pushstring(L, text.c_str());
896
897         return 1;
898 }
899
900 /******************************************************************************/
901 int ModApiMainMenu::l_get_screen_info(lua_State *L)
902 {
903         lua_newtable(L);
904         int top = lua_gettop(L);
905         lua_pushstring(L,"density");
906         lua_pushnumber(L,RenderingEngine::getDisplayDensity());
907         lua_settable(L, top);
908
909         lua_pushstring(L,"display_width");
910         lua_pushnumber(L,RenderingEngine::getDisplaySize().X);
911         lua_settable(L, top);
912
913         lua_pushstring(L,"display_height");
914         lua_pushnumber(L,RenderingEngine::getDisplaySize().Y);
915         lua_settable(L, top);
916
917         const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
918         lua_pushstring(L,"window_width");
919         lua_pushnumber(L, window_size.X);
920         lua_settable(L, top);
921
922         lua_pushstring(L,"window_height");
923         lua_pushnumber(L, window_size.Y);
924         lua_settable(L, top);
925         return 1;
926 }
927
928 /******************************************************************************/
929 int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
930 {
931         lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MIN);
932         return 1;
933 }
934
935 int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
936 {
937         lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX);
938         return 1;
939 }
940
941 /******************************************************************************/
942 int ModApiMainMenu::l_do_async_callback(lua_State *L)
943 {
944         GUIEngine* engine = getGuiEngine(L);
945
946         size_t func_length, param_length;
947         const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
948
949         const char* serialized_param_raw = luaL_checklstring(L, 2, &param_length);
950
951         sanity_check(serialized_func_raw != NULL);
952         sanity_check(serialized_param_raw != NULL);
953
954         std::string serialized_func = std::string(serialized_func_raw, func_length);
955         std::string serialized_param = std::string(serialized_param_raw, param_length);
956
957         lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param));
958
959         return 1;
960 }
961
962 /******************************************************************************/
963 void ModApiMainMenu::Initialize(lua_State *L, int top)
964 {
965         API_FCT(update_formspec);
966         API_FCT(set_clouds);
967         API_FCT(get_textlist_index);
968         API_FCT(get_table_index);
969         API_FCT(get_worlds);
970         API_FCT(get_games);
971         API_FCT(start);
972         API_FCT(close);
973         API_FCT(get_favorites);
974         API_FCT(show_keys_menu);
975         API_FCT(create_world);
976         API_FCT(delete_world);
977         API_FCT(delete_favorite);
978         API_FCT(set_background);
979         API_FCT(set_topleft_text);
980         API_FCT(get_mapgen_names);
981         API_FCT(get_modpath);
982         API_FCT(get_clientmodpath);
983         API_FCT(get_gamepath);
984         API_FCT(get_texturepath);
985         API_FCT(get_texturepath_share);
986         API_FCT(create_dir);
987         API_FCT(delete_dir);
988         API_FCT(copy_dir);
989         API_FCT(extract_zip);
990         API_FCT(get_mainmenu_path);
991         API_FCT(show_path_select_dialog);
992         API_FCT(download_file);
993         API_FCT(gettext);
994         API_FCT(get_video_drivers);
995         API_FCT(get_video_modes);
996         API_FCT(get_screen_info);
997         API_FCT(get_min_supp_proto);
998         API_FCT(get_max_supp_proto);
999         API_FCT(do_async_callback);
1000 }
1001
1002 /******************************************************************************/
1003 void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
1004 {
1005
1006         API_FCT(get_worlds);
1007         API_FCT(get_games);
1008         API_FCT(get_favorites);
1009         API_FCT(get_mapgen_names);
1010         API_FCT(get_modpath);
1011         API_FCT(get_clientmodpath);
1012         API_FCT(get_gamepath);
1013         API_FCT(get_texturepath);
1014         API_FCT(get_texturepath_share);
1015         API_FCT(create_dir);
1016         API_FCT(delete_dir);
1017         API_FCT(copy_dir);
1018         //API_FCT(extract_zip); //TODO remove dependency to GuiEngine
1019         API_FCT(download_file);
1020         //API_FCT(gettext); (gettext lib isn't threadsafe)
1021 }