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