l_mainmenu.cpp 30 KB

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