]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_mainmenu.cpp
Deprecate game.conf name, use title instead (#12030)
[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 "scripting_mainmenu.h"
25 #include "gui/guiEngine.h"
26 #include "gui/guiMainMenu.h"
27 #include "gui/guiKeyChangeMenu.h"
28 #include "gui/guiPathSelectMenu.h"
29 #include "version.h"
30 #include "porting.h"
31 #include "filesys.h"
32 #include "convert_json.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 #include "client/client.h"
39 #include "client/renderingengine.h"
40 #include "network/networkprotocol.h"
41
42
43 /******************************************************************************/
44 std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
45 {
46         lua_getglobal(L, "gamedata");
47
48         lua_getfield(L, -1, name.c_str());
49
50         if(lua_isnil(L, -1))
51                 return "";
52
53         return luaL_checkstring(L, -1);
54 }
55
56 /******************************************************************************/
57 int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
58 {
59         lua_getglobal(L, "gamedata");
60
61         lua_getfield(L, -1, name.c_str());
62
63         if(lua_isnil(L, -1)) {
64                 valid = false;
65                 return -1;
66                 }
67
68         valid = true;
69         return luaL_checkinteger(L, -1);
70 }
71
72 /******************************************************************************/
73 int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
74 {
75         lua_getglobal(L, "gamedata");
76
77         lua_getfield(L, -1, name.c_str());
78
79         if(lua_isnil(L, -1)) {
80                 valid = false;
81                 return false;
82                 }
83
84         valid = true;
85         return readParam<bool>(L, -1);
86 }
87
88 /******************************************************************************/
89 int ModApiMainMenu::l_update_formspec(lua_State *L)
90 {
91         GUIEngine* engine = getGuiEngine(L);
92         sanity_check(engine != NULL);
93
94         if (engine->m_startgame)
95                 return 0;
96
97         //read formspec
98         std::string formspec(luaL_checkstring(L, 1));
99
100         if (engine->m_formspecgui != 0) {
101                 engine->m_formspecgui->setForm(formspec);
102         }
103
104         return 0;
105 }
106
107 /******************************************************************************/
108 int ModApiMainMenu::l_set_formspec_prepend(lua_State *L)
109 {
110         GUIEngine *engine = getGuiEngine(L);
111         sanity_check(engine != NULL);
112
113         if (engine->m_startgame)
114                 return 0;
115
116         std::string formspec(luaL_checkstring(L, 1));
117         engine->setFormspecPrepend(formspec);
118
119         return 0;
120 }
121
122 /******************************************************************************/
123 int ModApiMainMenu::l_start(lua_State *L)
124 {
125         GUIEngine* engine = getGuiEngine(L);
126         sanity_check(engine != NULL);
127
128         //update c++ gamedata from lua table
129
130         bool valid = false;
131
132         MainMenuData *data = engine->m_data;
133
134         data->selected_world = getIntegerData(L, "selected_world",valid) -1;
135         data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
136         data->do_reconnect = getBoolData(L, "do_reconnect", valid);
137         if (!data->do_reconnect) {
138                 data->name     = getTextData(L,"playername");
139                 data->password = getTextData(L,"password");
140                 data->address  = getTextData(L,"address");
141                 data->port     = getTextData(L,"port");
142         }
143         data->serverdescription = getTextData(L,"serverdescription");
144         data->servername        = getTextData(L,"servername");
145
146         //close menu next time
147         engine->m_startgame = true;
148         return 0;
149 }
150
151 /******************************************************************************/
152 int ModApiMainMenu::l_close(lua_State *L)
153 {
154         GUIEngine* engine = getGuiEngine(L);
155         sanity_check(engine != NULL);
156
157         engine->m_kill = true;
158         return 0;
159 }
160
161 /******************************************************************************/
162 int ModApiMainMenu::l_set_background(lua_State *L)
163 {
164         GUIEngine* engine = getGuiEngine(L);
165         sanity_check(engine != NULL);
166
167         std::string backgroundlevel(luaL_checkstring(L, 1));
168         std::string texturename(luaL_checkstring(L, 2));
169
170         bool tile_image = false;
171         bool retval     = false;
172         unsigned int minsize = 16;
173
174         if (!lua_isnone(L, 3)) {
175                 tile_image = readParam<bool>(L, 3);
176         }
177
178         if (!lua_isnone(L, 4)) {
179                 minsize = lua_tonumber(L, 4);
180         }
181
182         if (backgroundlevel == "background") {
183                 retval |= engine->setTexture(TEX_LAYER_BACKGROUND, texturename,
184                                 tile_image, minsize);
185         }
186
187         if (backgroundlevel == "overlay") {
188                 retval |= engine->setTexture(TEX_LAYER_OVERLAY, texturename,
189                                 tile_image, minsize);
190         }
191
192         if (backgroundlevel == "header") {
193                 retval |= engine->setTexture(TEX_LAYER_HEADER,  texturename,
194                                 tile_image, minsize);
195         }
196
197         if (backgroundlevel == "footer") {
198                 retval |= engine->setTexture(TEX_LAYER_FOOTER, texturename,
199                                 tile_image, minsize);
200         }
201
202         lua_pushboolean(L,retval);
203         return 1;
204 }
205
206 /******************************************************************************/
207 int ModApiMainMenu::l_set_clouds(lua_State *L)
208 {
209         GUIEngine* engine = getGuiEngine(L);
210         sanity_check(engine != NULL);
211
212         bool value = readParam<bool>(L,1);
213
214         engine->m_clouds_enabled = value;
215
216         return 0;
217 }
218
219 /******************************************************************************/
220 int ModApiMainMenu::l_get_textlist_index(lua_State *L)
221 {
222         // get_table_index accepts both tables and textlists
223         return l_get_table_index(L);
224 }
225
226 /******************************************************************************/
227 int ModApiMainMenu::l_get_table_index(lua_State *L)
228 {
229         GUIEngine* engine = getGuiEngine(L);
230         sanity_check(engine != NULL);
231
232         std::string tablename(luaL_checkstring(L, 1));
233         GUITable *table = engine->m_menu->getTable(tablename);
234         s32 selection = table ? table->getSelected() : 0;
235
236         if (selection >= 1)
237                 lua_pushinteger(L, selection);
238         else
239                 lua_pushnil(L);
240         return 1;
241 }
242
243 /******************************************************************************/
244 int ModApiMainMenu::l_get_worlds(lua_State *L)
245 {
246         std::vector<WorldSpec> worlds = getAvailableWorlds();
247
248         lua_newtable(L);
249         int top = lua_gettop(L);
250         unsigned int index = 1;
251
252         for (const WorldSpec &world : worlds) {
253                 lua_pushnumber(L,index);
254
255                 lua_newtable(L);
256                 int top_lvl2 = lua_gettop(L);
257
258                 lua_pushstring(L,"path");
259                 lua_pushstring(L, world.path.c_str());
260                 lua_settable(L, top_lvl2);
261
262                 lua_pushstring(L,"name");
263                 lua_pushstring(L, world.name.c_str());
264                 lua_settable(L, top_lvl2);
265
266                 lua_pushstring(L,"gameid");
267                 lua_pushstring(L, world.gameid.c_str());
268                 lua_settable(L, top_lvl2);
269
270                 lua_settable(L, top);
271                 index++;
272         }
273         return 1;
274 }
275
276 /******************************************************************************/
277 int ModApiMainMenu::l_get_games(lua_State *L)
278 {
279         std::vector<SubgameSpec> games = getAvailableGames();
280
281         lua_newtable(L);
282         int top = lua_gettop(L);
283         unsigned int index = 1;
284
285         for (const SubgameSpec &game : games) {
286                 lua_pushnumber(L, index);
287                 lua_newtable(L);
288                 int top_lvl2 = lua_gettop(L);
289
290                 lua_pushstring(L,  "id");
291                 lua_pushstring(L,  game.id.c_str());
292                 lua_settable(L,    top_lvl2);
293
294                 lua_pushstring(L,  "path");
295                 lua_pushstring(L,  game.path.c_str());
296                 lua_settable(L,    top_lvl2);
297
298                 lua_pushstring(L,  "type");
299                 lua_pushstring(L,  "game");
300                 lua_settable(L,    top_lvl2);
301
302                 lua_pushstring(L,  "gamemods_path");
303                 lua_pushstring(L,  game.gamemods_path.c_str());
304                 lua_settable(L,    top_lvl2);
305
306                 lua_pushstring(L,  "name");
307                 lua_pushstring(L,  game.title.c_str());
308                 lua_settable(L,    top_lvl2);
309
310                 lua_pushstring(L,  "title");
311                 lua_pushstring(L,  game.title.c_str());
312                 lua_settable(L,    top_lvl2);
313
314                 lua_pushstring(L,  "author");
315                 lua_pushstring(L,  game.author.c_str());
316                 lua_settable(L,    top_lvl2);
317
318                 lua_pushstring(L,  "release");
319                 lua_pushinteger(L, game.release);
320                 lua_settable(L,    top_lvl2);
321
322                 lua_pushstring(L,  "menuicon_path");
323                 lua_pushstring(L,  game.menuicon_path.c_str());
324                 lua_settable(L,    top_lvl2);
325
326                 lua_pushstring(L, "addon_mods_paths");
327                 lua_newtable(L);
328                 int table2 = lua_gettop(L);
329                 int internal_index = 1;
330                 for (const auto &addon_mods_path : game.addon_mods_paths) {
331                         lua_pushnumber(L, internal_index);
332                         lua_pushstring(L, addon_mods_path.second.c_str());
333                         lua_settable(L,   table2);
334                         internal_index++;
335                 }
336                 lua_settable(L, top_lvl2);
337                 lua_settable(L, top);
338                 index++;
339         }
340         return 1;
341 }
342
343 /******************************************************************************/
344 int ModApiMainMenu::l_get_content_info(lua_State *L)
345 {
346         std::string path = luaL_checkstring(L, 1);
347
348         ContentSpec spec;
349         spec.path = path;
350         parseContentInfo(spec);
351
352         lua_newtable(L);
353
354         lua_pushstring(L, spec.name.c_str());
355         lua_setfield(L, -2, "name");
356
357         lua_pushstring(L, spec.type.c_str());
358         lua_setfield(L, -2, "type");
359
360         lua_pushstring(L, spec.author.c_str());
361         lua_setfield(L, -2, "author");
362
363         if (!spec.title.empty()) {
364                 lua_pushstring(L, spec.title.c_str());
365                 lua_setfield(L, -2, "title");
366         }
367
368         lua_pushinteger(L, spec.release);
369         lua_setfield(L, -2, "release");
370
371         lua_pushstring(L, spec.desc.c_str());
372         lua_setfield(L, -2, "description");
373
374         lua_pushstring(L, spec.path.c_str());
375         lua_setfield(L, -2, "path");
376
377         if (spec.type == "mod") {
378                 ModSpec spec;
379                 spec.path = path;
380                 parseModContents(spec);
381
382                 // Dependencies
383                 lua_newtable(L);
384                 int i = 1;
385                 for (const auto &dep : spec.depends) {
386                         lua_pushstring(L, dep.c_str());
387                         lua_rawseti(L, -2, i++);
388                 }
389                 lua_setfield(L, -2, "depends");
390
391                 // Optional Dependencies
392                 lua_newtable(L);
393                 i = 1;
394                 for (const auto &dep : spec.optdepends) {
395                         lua_pushstring(L, dep.c_str());
396                         lua_rawseti(L, -2, i++);
397                 }
398                 lua_setfield(L, -2, "optional_depends");
399         }
400
401         return 1;
402 }
403
404 /******************************************************************************/
405 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
406 {
407         GUIEngine* engine = getGuiEngine(L);
408         sanity_check(engine != NULL);
409
410         GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(
411                         engine->m_rendering_engine->get_gui_env(),
412                         engine->m_parent,
413                         -1,
414                         engine->m_menumanager,
415                         engine->m_texture_source);
416         kmenu->drop();
417         return 0;
418 }
419
420 /******************************************************************************/
421 int ModApiMainMenu::l_create_world(lua_State *L)
422 {
423         const char *name        = luaL_checkstring(L, 1);
424         int gameidx                     = luaL_checkinteger(L,2) -1;
425
426         StringMap use_settings;
427         luaL_checktype(L, 3, LUA_TTABLE);
428         lua_pushnil(L);
429         while (lua_next(L, 3) != 0) {
430                 // key at index -2 and value at index -1
431                 use_settings[luaL_checkstring(L, -2)] = luaL_checkstring(L, -1);
432                 lua_pop(L, 1);
433         }
434         lua_pop(L, 1);
435
436         std::string path = porting::path_user + DIR_DELIM
437                         "worlds" + DIR_DELIM
438                         + sanitizeDirName(name, "world_");
439
440         std::vector<SubgameSpec> games = getAvailableGames();
441         if (gameidx < 0 || gameidx >= (int) games.size()) {
442                 lua_pushstring(L, "Invalid game index");
443                 return 1;
444         }
445
446         // Set the settings for world creation
447         // this is a bad hack but the best we have right now..
448         StringMap backup;
449         for (auto it : use_settings) {
450                 if (g_settings->existsLocal(it.first))
451                         backup[it.first] = g_settings->get(it.first);
452                 g_settings->set(it.first, it.second);
453         }
454
455         // Create world if it doesn't exist
456         try {
457                 loadGameConfAndInitWorld(path, name, games[gameidx], true);
458                 lua_pushnil(L);
459         } catch (const BaseException &e) {
460                 auto err = std::string("Failed to initialize world: ") + e.what();
461                 lua_pushstring(L, err.c_str());
462         }
463
464         // Restore previous settings
465         for (auto it : use_settings) {
466                 auto it2 = backup.find(it.first);
467                 if (it2 == backup.end())
468                         g_settings->remove(it.first); // wasn't set before
469                 else
470                         g_settings->set(it.first, it2->second); // was set before
471         }
472
473         return 1;
474 }
475
476 /******************************************************************************/
477 int ModApiMainMenu::l_delete_world(lua_State *L)
478 {
479         int world_id = luaL_checkinteger(L, 1) - 1;
480         std::vector<WorldSpec> worlds = getAvailableWorlds();
481         if (world_id < 0 || world_id >= (int) worlds.size()) {
482                 lua_pushstring(L, "Invalid world index");
483                 return 1;
484         }
485         const WorldSpec &spec = worlds[world_id];
486         if (!fs::RecursiveDelete(spec.path)) {
487                 lua_pushstring(L, "Failed to delete world");
488                 return 1;
489         }
490         return 0;
491 }
492
493 /******************************************************************************/
494 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
495 {
496         GUIEngine* engine = getGuiEngine(L);
497         sanity_check(engine != NULL);
498
499         std::string text;
500
501         if (!lua_isnone(L,1) && !lua_isnil(L,1))
502                 text = luaL_checkstring(L, 1);
503
504         engine->setTopleftText(text);
505         return 0;
506 }
507
508 /******************************************************************************/
509 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
510 {
511         std::vector<const char *> names;
512         bool include_hidden = lua_isboolean(L, 1) && readParam<bool>(L, 1);
513         Mapgen::getMapgenNames(&names, include_hidden);
514
515         lua_newtable(L);
516         for (size_t i = 0; i != names.size(); i++) {
517                 lua_pushstring(L, names[i]);
518                 lua_rawseti(L, -2, i + 1);
519         }
520
521         return 1;
522 }
523
524
525 /******************************************************************************/
526 int ModApiMainMenu::l_get_user_path(lua_State *L)
527 {
528         std::string path = fs::RemoveRelativePathComponents(porting::path_user);
529         lua_pushstring(L, path.c_str());
530         return 1;
531 }
532
533 /******************************************************************************/
534 int ModApiMainMenu::l_get_modpath(lua_State *L)
535 {
536         std::string modpath = fs::RemoveRelativePathComponents(
537                 porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
538         lua_pushstring(L, modpath.c_str());
539         return 1;
540 }
541
542 /******************************************************************************/
543 int ModApiMainMenu::l_get_modpaths(lua_State *L)
544 {
545         lua_newtable(L);
546
547         ModApiMainMenu::l_get_modpath(L);
548         lua_setfield(L, -2, "mods");
549
550         for (const std::string &component : getEnvModPaths()) {
551                 lua_pushstring(L, component.c_str());
552                 lua_setfield(L, -2, fs::AbsolutePath(component).c_str());
553         }
554         return 1;
555 }
556
557 /******************************************************************************/
558 int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
559 {
560         std::string modpath = fs::RemoveRelativePathComponents(
561                 porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM);
562         lua_pushstring(L, modpath.c_str());
563         return 1;
564 }
565
566 /******************************************************************************/
567 int ModApiMainMenu::l_get_gamepath(lua_State *L)
568 {
569         std::string gamepath = fs::RemoveRelativePathComponents(
570                 porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
571         lua_pushstring(L, gamepath.c_str());
572         return 1;
573 }
574
575 /******************************************************************************/
576 int ModApiMainMenu::l_get_texturepath(lua_State *L)
577 {
578         std::string gamepath = fs::RemoveRelativePathComponents(
579                 porting::path_user + DIR_DELIM + "textures");
580         lua_pushstring(L, gamepath.c_str());
581         return 1;
582 }
583
584 /******************************************************************************/
585 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
586 {
587         std::string gamepath = fs::RemoveRelativePathComponents(
588                 porting::path_share + DIR_DELIM + "textures");
589         lua_pushstring(L, gamepath.c_str());
590         return 1;
591 }
592
593 /******************************************************************************/
594 int ModApiMainMenu::l_get_cache_path(lua_State *L)
595 {
596         lua_pushstring(L, fs::RemoveRelativePathComponents(porting::path_cache).c_str());
597         return 1;
598 }
599
600 /******************************************************************************/
601 int ModApiMainMenu::l_get_temp_path(lua_State *L)
602 {
603         if (lua_isnoneornil(L, 1) || !lua_toboolean(L, 1))
604                 lua_pushstring(L, fs::TempPath().c_str());
605         else
606                 lua_pushstring(L, fs::CreateTempFile().c_str());
607         return 1;
608 }
609
610 /******************************************************************************/
611 int ModApiMainMenu::l_create_dir(lua_State *L) {
612         const char *path = luaL_checkstring(L, 1);
613
614         if (ModApiMainMenu::mayModifyPath(path)) {
615                 lua_pushboolean(L, fs::CreateAllDirs(path));
616                 return 1;
617         }
618
619         lua_pushboolean(L, false);
620         return 1;
621 }
622
623 /******************************************************************************/
624 int ModApiMainMenu::l_delete_dir(lua_State *L)
625 {
626         const char *path = luaL_checkstring(L, 1);
627
628         std::string absolute_path = fs::RemoveRelativePathComponents(path);
629
630         if (ModApiMainMenu::mayModifyPath(absolute_path)) {
631                 lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
632                 return 1;
633         }
634
635         lua_pushboolean(L, false);
636         return 1;
637 }
638
639 /******************************************************************************/
640 int ModApiMainMenu::l_copy_dir(lua_State *L)
641 {
642         const char *source      = luaL_checkstring(L, 1);
643         const char *destination = luaL_checkstring(L, 2);
644
645         bool keep_source = true;
646         if (!lua_isnoneornil(L, 3))
647                 keep_source = readParam<bool>(L, 3);
648
649         std::string abs_destination = fs::RemoveRelativePathComponents(destination);
650         std::string abs_source = fs::RemoveRelativePathComponents(source);
651
652         if (!ModApiMainMenu::mayModifyPath(abs_destination) ||
653                 (!keep_source && !ModApiMainMenu::mayModifyPath(abs_source))) {
654                 lua_pushboolean(L, false);
655                 return 1;
656         }
657
658         bool retval;
659         if (keep_source)
660                 retval = fs::CopyDir(abs_source, abs_destination);
661         else
662                 retval = fs::MoveDir(abs_source, abs_destination);
663         lua_pushboolean(L, retval);
664         return 1;
665 }
666
667 /******************************************************************************/
668 int ModApiMainMenu::l_is_dir(lua_State *L)
669 {
670         const char *path = luaL_checkstring(L, 1);
671
672         lua_pushboolean(L, fs::IsDir(path));
673         return 1;
674 }
675
676 /******************************************************************************/
677 int ModApiMainMenu::l_extract_zip(lua_State *L)
678 {
679         const char *zipfile     = luaL_checkstring(L, 1);
680         const char *destination = luaL_checkstring(L, 2);
681
682         std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
683
684         if (ModApiMainMenu::mayModifyPath(absolute_destination)) {
685                 auto fs = RenderingEngine::get_raw_device()->getFileSystem();
686                 bool ok = fs::extractZipFile(fs, zipfile, destination);
687                 lua_pushboolean(L, ok);
688                 return 1;
689         }
690
691         lua_pushboolean(L,false);
692         return 1;
693 }
694
695 /******************************************************************************/
696 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
697 {
698         GUIEngine* engine = getGuiEngine(L);
699         sanity_check(engine != NULL);
700
701         lua_pushstring(L,engine->getScriptDir().c_str());
702         return 1;
703 }
704
705 /******************************************************************************/
706 bool ModApiMainMenu::mayModifyPath(std::string path)
707 {
708         path = fs::RemoveRelativePathComponents(path);
709
710         if (fs::PathStartsWith(path, fs::TempPath()))
711                 return true;
712
713         std::string path_user = fs::RemoveRelativePathComponents(porting::path_user);
714
715         if (fs::PathStartsWith(path, path_user + DIR_DELIM "client"))
716                 return true;
717         if (fs::PathStartsWith(path, path_user + DIR_DELIM "games"))
718                 return true;
719         if (fs::PathStartsWith(path, path_user + DIR_DELIM "mods"))
720                 return true;
721         if (fs::PathStartsWith(path, path_user + DIR_DELIM "textures"))
722                 return true;
723         if (fs::PathStartsWith(path, path_user + DIR_DELIM "worlds"))
724                 return true;
725
726         if (fs::PathStartsWith(path, fs::RemoveRelativePathComponents(porting::path_cache)))
727                 return true;
728
729         return false;
730 }
731
732
733 /******************************************************************************/
734 int ModApiMainMenu::l_may_modify_path(lua_State *L)
735 {
736         const char *target = luaL_checkstring(L, 1);
737         std::string absolute_destination = fs::RemoveRelativePathComponents(target);
738         lua_pushboolean(L, ModApiMainMenu::mayModifyPath(absolute_destination));
739         return 1;
740 }
741
742 /******************************************************************************/
743 int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
744 {
745         GUIEngine* engine = getGuiEngine(L);
746         sanity_check(engine != NULL);
747
748         const char *formname= luaL_checkstring(L, 1);
749         const char *title       = luaL_checkstring(L, 2);
750         bool is_file_select = readParam<bool>(L, 3);
751
752         GUIFileSelectMenu* fileOpenMenu =
753                 new GUIFileSelectMenu(engine->m_rendering_engine->get_gui_env(),
754                                 engine->m_parent,
755                                 -1,
756                                 engine->m_menumanager,
757                                 title,
758                                 formname,
759                                 is_file_select);
760         fileOpenMenu->setTextDest(engine->m_buttonhandler);
761         fileOpenMenu->drop();
762         return 0;
763 }
764
765 /******************************************************************************/
766 int ModApiMainMenu::l_download_file(lua_State *L)
767 {
768         const char *url    = luaL_checkstring(L, 1);
769         const char *target = luaL_checkstring(L, 2);
770
771         //check path
772         std::string absolute_destination = fs::RemoveRelativePathComponents(target);
773
774         if (ModApiMainMenu::mayModifyPath(absolute_destination)) {
775                 if (GUIEngine::downloadFile(url,absolute_destination)) {
776                         lua_pushboolean(L,true);
777                         return 1;
778                 }
779         } else {
780                 errorstream << "DOWNLOAD denied: " << absolute_destination
781                                 << " isn't a allowed path" << std::endl;
782         }
783         lua_pushboolean(L,false);
784         return 1;
785 }
786
787 /******************************************************************************/
788 int ModApiMainMenu::l_get_video_drivers(lua_State *L)
789 {
790         std::vector<irr::video::E_DRIVER_TYPE> drivers = RenderingEngine::getSupportedVideoDrivers();
791
792         lua_newtable(L);
793         for (u32 i = 0; i != drivers.size(); i++) {
794                 auto &info = RenderingEngine::getVideoDriverInfo(drivers[i]);
795
796                 lua_newtable(L);
797                 lua_pushstring(L, info.name.c_str());
798                 lua_setfield(L, -2, "name");
799                 lua_pushstring(L, info.friendly_name.c_str());
800                 lua_setfield(L, -2, "friendly_name");
801
802                 lua_rawseti(L, -2, i + 1);
803         }
804
805         return 1;
806 }
807
808 /******************************************************************************/
809 int ModApiMainMenu::l_gettext(lua_State *L)
810 {
811         const char *srctext = luaL_checkstring(L, 1);
812         const char *text = *srctext ? gettext(srctext) : "";
813         lua_pushstring(L, text);
814
815         return 1;
816 }
817
818 /******************************************************************************/
819 int ModApiMainMenu::l_get_screen_info(lua_State *L)
820 {
821         lua_newtable(L);
822         int top = lua_gettop(L);
823         lua_pushstring(L,"density");
824         lua_pushnumber(L,RenderingEngine::getDisplayDensity());
825         lua_settable(L, top);
826
827         const v2u32 &window_size = RenderingEngine::getWindowSize();
828         lua_pushstring(L,"window_width");
829         lua_pushnumber(L, window_size.X);
830         lua_settable(L, top);
831
832         lua_pushstring(L,"window_height");
833         lua_pushnumber(L, window_size.Y);
834         lua_settable(L, top);
835
836         lua_pushstring(L, "render_info");
837         lua_pushstring(L, wide_to_utf8(RenderingEngine::get_video_driver()->getName()).c_str());
838         lua_settable(L, top);
839         return 1;
840 }
841
842 /******************************************************************************/
843 int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
844 {
845         lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MIN);
846         return 1;
847 }
848
849 int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
850 {
851         lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX);
852         return 1;
853 }
854
855 /******************************************************************************/
856 int ModApiMainMenu::l_open_url(lua_State *L)
857 {
858         std::string url = luaL_checkstring(L, 1);
859         lua_pushboolean(L, porting::open_url(url));
860         return 1;
861 }
862
863 /******************************************************************************/
864 int ModApiMainMenu::l_open_dir(lua_State *L)
865 {
866         std::string path = luaL_checkstring(L, 1);
867         lua_pushboolean(L, porting::open_directory(path));
868         return 1;
869 }
870
871 /******************************************************************************/
872 int ModApiMainMenu::l_do_async_callback(lua_State *L)
873 {
874         MainMenuScripting *script = getScriptApi<MainMenuScripting>(L);
875
876         size_t func_length, param_length;
877         const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
878         const char* serialized_param_raw = luaL_checklstring(L, 2, &param_length);
879
880         sanity_check(serialized_func_raw != NULL);
881         sanity_check(serialized_param_raw != NULL);
882
883         u32 jobId = script->queueAsync(
884                 std::string(serialized_func_raw, func_length),
885                 std::string(serialized_param_raw, param_length));
886
887         lua_pushinteger(L, jobId);
888
889         return 1;
890 }
891
892 /******************************************************************************/
893 void ModApiMainMenu::Initialize(lua_State *L, int top)
894 {
895         API_FCT(update_formspec);
896         API_FCT(set_formspec_prepend);
897         API_FCT(set_clouds);
898         API_FCT(get_textlist_index);
899         API_FCT(get_table_index);
900         API_FCT(get_worlds);
901         API_FCT(get_games);
902         API_FCT(get_content_info);
903         API_FCT(start);
904         API_FCT(close);
905         API_FCT(show_keys_menu);
906         API_FCT(create_world);
907         API_FCT(delete_world);
908         API_FCT(set_background);
909         API_FCT(set_topleft_text);
910         API_FCT(get_mapgen_names);
911         API_FCT(get_user_path);
912         API_FCT(get_modpath);
913         API_FCT(get_modpaths);
914         API_FCT(get_clientmodpath);
915         API_FCT(get_gamepath);
916         API_FCT(get_texturepath);
917         API_FCT(get_texturepath_share);
918         API_FCT(get_cache_path);
919         API_FCT(get_temp_path);
920         API_FCT(create_dir);
921         API_FCT(delete_dir);
922         API_FCT(copy_dir);
923         API_FCT(is_dir);
924         API_FCT(extract_zip);
925         API_FCT(may_modify_path);
926         API_FCT(get_mainmenu_path);
927         API_FCT(show_path_select_dialog);
928         API_FCT(download_file);
929         API_FCT(gettext);
930         API_FCT(get_video_drivers);
931         API_FCT(get_screen_info);
932         API_FCT(get_min_supp_proto);
933         API_FCT(get_max_supp_proto);
934         API_FCT(open_url);
935         API_FCT(open_dir);
936         API_FCT(do_async_callback);
937 }
938
939 /******************************************************************************/
940 void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
941 {
942         API_FCT(get_worlds);
943         API_FCT(get_games);
944         API_FCT(get_mapgen_names);
945         API_FCT(get_user_path);
946         API_FCT(get_modpath);
947         API_FCT(get_modpaths);
948         API_FCT(get_clientmodpath);
949         API_FCT(get_gamepath);
950         API_FCT(get_texturepath);
951         API_FCT(get_texturepath_share);
952         API_FCT(get_cache_path);
953         API_FCT(get_temp_path);
954         API_FCT(create_dir);
955         API_FCT(delete_dir);
956         API_FCT(copy_dir);
957         API_FCT(is_dir);
958         API_FCT(extract_zip);
959         API_FCT(may_modify_path);
960         API_FCT(download_file);
961         API_FCT(get_min_supp_proto);
962         API_FCT(get_max_supp_proto);
963         API_FCT(gettext);
964 }