2013-06-23 18:30:21 +02:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 sapier
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "lua_api/l_mainmenu.h"
|
|
|
|
#include "lua_api/l_internal.h"
|
|
|
|
#include "common/c_content.h"
|
2014-04-15 19:41:07 +02:00
|
|
|
#include "cpp_api/s_async.h"
|
2017-11-08 23:56:20 +01:00
|
|
|
#include "gui/guiEngine.h"
|
|
|
|
#include "gui/guiMainMenu.h"
|
|
|
|
#include "gui/guiKeyChangeMenu.h"
|
|
|
|
#include "gui/guiPathSelectMenu.h"
|
2013-09-25 04:29:07 +02:00
|
|
|
#include "version.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "porting.h"
|
2013-06-23 18:30:21 +02:00
|
|
|
#include "filesys.h"
|
|
|
|
#include "convert_json.h"
|
2018-04-17 15:54:50 +02:00
|
|
|
#include "content/packages.h"
|
|
|
|
#include "content/content.h"
|
|
|
|
#include "content/subgames.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "serverlist.h"
|
2017-11-08 23:56:20 +01:00
|
|
|
#include "mapgen/mapgen.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "settings.h"
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
#include <IFileArchive.h>
|
|
|
|
#include <IFileSystem.h>
|
2017-06-26 20:11:17 +02:00
|
|
|
#include "client/renderingengine.h"
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2014-12-12 20:49:19 +01:00
|
|
|
|
2013-06-23 18:30:21 +02:00
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
|
|
|
lua_getglobal(L, "gamedata");
|
|
|
|
|
|
|
|
lua_getfield(L, -1, name.c_str());
|
|
|
|
|
|
|
|
if(lua_isnil(L, -1))
|
|
|
|
return "";
|
|
|
|
|
|
|
|
return luaL_checkstring(L, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
|
|
|
lua_getglobal(L, "gamedata");
|
|
|
|
|
|
|
|
lua_getfield(L, -1, name.c_str());
|
|
|
|
|
|
|
|
if(lua_isnil(L, -1)) {
|
|
|
|
valid = false;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
valid = true;
|
|
|
|
return luaL_checkinteger(L, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
|
|
|
lua_getglobal(L, "gamedata");
|
|
|
|
|
|
|
|
lua_getfield(L, -1, name.c_str());
|
|
|
|
|
|
|
|
if(lua_isnil(L, -1)) {
|
|
|
|
valid = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
valid = true;
|
|
|
|
return lua_toboolean(L, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_update_formspec(lua_State *L)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
2013-08-11 04:09:45 +02:00
|
|
|
GUIEngine* engine = getGuiEngine(L);
|
2015-03-06 11:21:51 +01:00
|
|
|
sanity_check(engine != NULL);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
if (engine->m_startgame)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
//read formspec
|
|
|
|
std::string formspec(luaL_checkstring(L, 1));
|
|
|
|
|
|
|
|
if (engine->m_formspecgui != 0) {
|
|
|
|
engine->m_formspecgui->setForm(formspec);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_start(lua_State *L)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
2013-08-11 04:09:45 +02:00
|
|
|
GUIEngine* engine = getGuiEngine(L);
|
2015-03-06 11:21:51 +01:00
|
|
|
sanity_check(engine != NULL);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
//update c++ gamedata from lua table
|
|
|
|
|
|
|
|
bool valid = false;
|
|
|
|
|
2015-07-17 16:40:41 +02:00
|
|
|
MainMenuData *data = engine->m_data;
|
|
|
|
|
|
|
|
data->selected_world = getIntegerData(L, "selected_world",valid) -1;
|
|
|
|
data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
|
|
|
|
data->do_reconnect = getBoolData(L, "do_reconnect", valid);
|
|
|
|
if (!data->do_reconnect) {
|
|
|
|
data->name = getTextData(L,"playername");
|
|
|
|
data->password = getTextData(L,"password");
|
|
|
|
data->address = getTextData(L,"address");
|
|
|
|
data->port = getTextData(L,"port");
|
|
|
|
}
|
|
|
|
data->serverdescription = getTextData(L,"serverdescription");
|
|
|
|
data->servername = getTextData(L,"servername");
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
//close menu next time
|
|
|
|
engine->m_startgame = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_close(lua_State *L)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
2013-08-11 04:09:45 +02:00
|
|
|
GUIEngine* engine = getGuiEngine(L);
|
2015-03-06 11:21:51 +01:00
|
|
|
sanity_check(engine != NULL);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2013-08-19 14:55:38 +02:00
|
|
|
engine->m_kill = true;
|
2013-06-23 18:30:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_set_background(lua_State *L)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
2013-08-11 04:09:45 +02:00
|
|
|
GUIEngine* engine = getGuiEngine(L);
|
2015-03-06 11:21:51 +01:00
|
|
|
sanity_check(engine != NULL);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
std::string backgroundlevel(luaL_checkstring(L, 1));
|
|
|
|
std::string texturename(luaL_checkstring(L, 2));
|
|
|
|
|
2014-05-17 12:06:36 +02:00
|
|
|
bool tile_image = false;
|
|
|
|
bool retval = false;
|
|
|
|
unsigned int minsize = 16;
|
|
|
|
|
|
|
|
if (!lua_isnone(L, 3)) {
|
|
|
|
tile_image = lua_toboolean(L, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lua_isnone(L, 4)) {
|
|
|
|
minsize = lua_tonumber(L, 4);
|
|
|
|
}
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
if (backgroundlevel == "background") {
|
2014-05-17 12:06:36 +02:00
|
|
|
retval |= engine->setTexture(TEX_LAYER_BACKGROUND, texturename,
|
|
|
|
tile_image, minsize);
|
2013-06-23 18:30:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (backgroundlevel == "overlay") {
|
2014-05-17 12:06:36 +02:00
|
|
|
retval |= engine->setTexture(TEX_LAYER_OVERLAY, texturename,
|
|
|
|
tile_image, minsize);
|
2013-06-23 18:30:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (backgroundlevel == "header") {
|
2014-05-17 12:06:36 +02:00
|
|
|
retval |= engine->setTexture(TEX_LAYER_HEADER, texturename,
|
|
|
|
tile_image, minsize);
|
2013-06-23 18:30:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (backgroundlevel == "footer") {
|
2014-05-17 12:06:36 +02:00
|
|
|
retval |= engine->setTexture(TEX_LAYER_FOOTER, texturename,
|
|
|
|
tile_image, minsize);
|
2013-06-23 18:30:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushboolean(L,retval);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_set_clouds(lua_State *L)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
2013-08-11 04:09:45 +02:00
|
|
|
GUIEngine* engine = getGuiEngine(L);
|
2015-03-06 11:21:51 +01:00
|
|
|
sanity_check(engine != NULL);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
bool value = lua_toboolean(L,1);
|
|
|
|
|
|
|
|
engine->m_clouds_enabled = value;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_get_textlist_index(lua_State *L)
|
2013-08-23 12:24:11 +02:00
|
|
|
{
|
|
|
|
// get_table_index accepts both tables and textlists
|
|
|
|
return l_get_table_index(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
int ModApiMainMenu::l_get_table_index(lua_State *L)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
2013-08-11 04:09:45 +02:00
|
|
|
GUIEngine* engine = getGuiEngine(L);
|
2015-03-06 11:21:51 +01:00
|
|
|
sanity_check(engine != NULL);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2015-06-10 01:54:33 +02:00
|
|
|
std::string tablename(luaL_checkstring(L, 1));
|
2013-08-23 12:24:11 +02:00
|
|
|
GUITable *table = engine->m_menu->getTable(tablename);
|
|
|
|
s32 selection = table ? table->getSelected() : 0;
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2013-08-23 12:24:11 +02:00
|
|
|
if (selection >= 1)
|
|
|
|
lua_pushinteger(L, selection);
|
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
2013-06-23 18:30:21 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_get_worlds(lua_State *L)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
|
|
|
std::vector<WorldSpec> worlds = getAvailableWorlds();
|
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
unsigned int index = 1;
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
for (const WorldSpec &world : worlds) {
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushnumber(L,index);
|
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
int top_lvl2 = lua_gettop(L);
|
|
|
|
|
|
|
|
lua_pushstring(L,"path");
|
2017-08-20 13:30:50 +02:00
|
|
|
lua_pushstring(L, world.path.c_str());
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
|
|
|
|
lua_pushstring(L,"name");
|
2017-08-20 13:30:50 +02:00
|
|
|
lua_pushstring(L, world.name.c_str());
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
|
|
|
|
lua_pushstring(L,"gameid");
|
2017-08-20 13:30:50 +02:00
|
|
|
lua_pushstring(L, world.gameid.c_str());
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
|
|
|
|
lua_settable(L, top);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_get_favorites(lua_State *L)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
|
|
|
std::string listtype = "local";
|
|
|
|
|
|
|
|
if (!lua_isnone(L,1)) {
|
|
|
|
listtype = luaL_checkstring(L,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ServerListSpec> servers;
|
2014-04-06 14:02:53 +02:00
|
|
|
|
2013-06-23 18:30:21 +02:00
|
|
|
if(listtype == "online") {
|
|
|
|
servers = ServerList::getOnline();
|
|
|
|
} else {
|
|
|
|
servers = ServerList::getLocal();
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
unsigned int index = 1;
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
for (const Json::Value &server : servers) {
|
2015-02-17 01:37:14 +01:00
|
|
|
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushnumber(L,index);
|
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
int top_lvl2 = lua_gettop(L);
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if (!server["clients"].asString().empty()) {
|
|
|
|
std::string clients_raw = server["clients"].asString();
|
2013-06-23 18:30:21 +02:00
|
|
|
char* endptr = 0;
|
2013-11-09 22:51:12 +01:00
|
|
|
int numbervalue = strtol(clients_raw.c_str(),&endptr,10);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if ((!clients_raw.empty()) && (*endptr == 0)) {
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushstring(L,"clients");
|
|
|
|
lua_pushnumber(L,numbervalue);
|
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if (!server["clients_max"].asString().empty()) {
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
std::string clients_max_raw = server["clients_max"].asString();
|
2013-06-23 18:30:21 +02:00
|
|
|
char* endptr = 0;
|
2013-11-09 22:51:12 +01:00
|
|
|
int numbervalue = strtol(clients_max_raw.c_str(),&endptr,10);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if ((!clients_max_raw.empty()) && (*endptr == 0)) {
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushstring(L,"clients_max");
|
|
|
|
lua_pushnumber(L,numbervalue);
|
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if (!server["version"].asString().empty()) {
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushstring(L,"version");
|
2017-08-20 13:30:50 +02:00
|
|
|
std::string topush = server["version"].asString();
|
2013-11-09 22:51:12 +01:00
|
|
|
lua_pushstring(L,topush.c_str());
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if (!server["proto_min"].asString().empty()) {
|
2015-02-17 01:37:14 +01:00
|
|
|
lua_pushstring(L,"proto_min");
|
2017-08-20 13:30:50 +02:00
|
|
|
lua_pushinteger(L, server["proto_min"].asInt());
|
2015-02-17 01:37:14 +01:00
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if (!server["proto_max"].asString().empty()) {
|
2015-02-17 01:37:14 +01:00
|
|
|
lua_pushstring(L,"proto_max");
|
2017-08-20 13:30:50 +02:00
|
|
|
lua_pushinteger(L, server["proto_max"].asInt());
|
2015-02-17 01:37:14 +01:00
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if (!server["password"].asString().empty()) {
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushstring(L,"password");
|
2017-08-20 13:30:50 +02:00
|
|
|
lua_pushboolean(L, server["password"].asBool());
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if (!server["creative"].asString().empty()) {
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushstring(L,"creative");
|
2017-08-20 13:30:50 +02:00
|
|
|
lua_pushboolean(L, server["creative"].asBool());
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if (!server["damage"].asString().empty()) {
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushstring(L,"damage");
|
2017-08-20 13:30:50 +02:00
|
|
|
lua_pushboolean(L, server["damage"].asBool());
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if (!server["pvp"].asString().empty()) {
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushstring(L,"pvp");
|
2017-08-20 13:30:50 +02:00
|
|
|
lua_pushboolean(L, server["pvp"].asBool());
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if (!server["description"].asString().empty()) {
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushstring(L,"description");
|
2017-08-20 13:30:50 +02:00
|
|
|
std::string topush = server["description"].asString();
|
2013-11-09 22:51:12 +01:00
|
|
|
lua_pushstring(L,topush.c_str());
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if (!server["name"].asString().empty()) {
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushstring(L,"name");
|
2017-08-20 13:30:50 +02:00
|
|
|
std::string topush = server["name"].asString();
|
2013-11-09 22:51:12 +01:00
|
|
|
lua_pushstring(L,topush.c_str());
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if (!server["address"].asString().empty()) {
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushstring(L,"address");
|
2017-08-20 13:30:50 +02:00
|
|
|
std::string topush = server["address"].asString();
|
2013-11-09 22:51:12 +01:00
|
|
|
lua_pushstring(L,topush.c_str());
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if (!server["port"].asString().empty()) {
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushstring(L,"port");
|
2017-08-20 13:30:50 +02:00
|
|
|
std::string topush = server["port"].asString();
|
2013-11-09 22:51:12 +01:00
|
|
|
lua_pushstring(L,topush.c_str());
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
if (server.isMember("ping")) {
|
|
|
|
float ping = server["ping"].asFloat();
|
2017-02-03 14:53:43 +01:00
|
|
|
lua_pushstring(L, "ping");
|
|
|
|
lua_pushnumber(L, ping);
|
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
}
|
|
|
|
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_settable(L, top);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_delete_favorite(lua_State *L)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
|
|
|
std::vector<ServerListSpec> servers;
|
|
|
|
|
|
|
|
std::string listtype = "local";
|
|
|
|
|
|
|
|
if (!lua_isnone(L,2)) {
|
|
|
|
listtype = luaL_checkstring(L,2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((listtype != "local") &&
|
|
|
|
(listtype != "online"))
|
|
|
|
return 0;
|
|
|
|
|
2014-04-06 14:02:53 +02:00
|
|
|
|
2013-06-23 18:30:21 +02:00
|
|
|
if(listtype == "online") {
|
|
|
|
servers = ServerList::getOnline();
|
|
|
|
} else {
|
|
|
|
servers = ServerList::getLocal();
|
|
|
|
}
|
|
|
|
|
|
|
|
int fav_idx = luaL_checkinteger(L,1) -1;
|
|
|
|
|
|
|
|
if ((fav_idx >= 0) &&
|
|
|
|
(fav_idx < (int) servers.size())) {
|
|
|
|
|
|
|
|
ServerList::deleteEntry(servers[fav_idx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-28 23:14:16 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
int ModApiMainMenu::l_get_games(lua_State *L)
|
|
|
|
{
|
|
|
|
std::vector<SubgameSpec> games = getAvailableGames();
|
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
unsigned int index = 1;
|
|
|
|
|
|
|
|
for (const SubgameSpec &game : games) {
|
|
|
|
lua_pushnumber(L, index);
|
|
|
|
lua_newtable(L);
|
|
|
|
int top_lvl2 = lua_gettop(L);
|
|
|
|
|
2018-05-16 22:52:12 +02:00
|
|
|
lua_pushstring(L, "id");
|
|
|
|
lua_pushstring(L, game.id.c_str());
|
|
|
|
lua_settable(L, top_lvl2);
|
2018-03-28 23:14:16 +02:00
|
|
|
|
2018-05-16 22:52:12 +02:00
|
|
|
lua_pushstring(L, "path");
|
|
|
|
lua_pushstring(L, game.path.c_str());
|
|
|
|
lua_settable(L, top_lvl2);
|
2018-03-28 23:14:16 +02:00
|
|
|
|
2018-05-16 22:52:12 +02:00
|
|
|
lua_pushstring(L, "type");
|
|
|
|
lua_pushstring(L, "game");
|
|
|
|
lua_settable(L, top_lvl2);
|
2018-04-17 15:54:50 +02:00
|
|
|
|
2018-05-16 22:52:12 +02:00
|
|
|
lua_pushstring(L, "gamemods_path");
|
|
|
|
lua_pushstring(L, game.gamemods_path.c_str());
|
|
|
|
lua_settable(L, top_lvl2);
|
2018-03-28 23:14:16 +02:00
|
|
|
|
2018-05-16 22:52:12 +02:00
|
|
|
lua_pushstring(L, "name");
|
|
|
|
lua_pushstring(L, game.name.c_str());
|
|
|
|
lua_settable(L, top_lvl2);
|
2018-03-28 23:14:16 +02:00
|
|
|
|
2018-05-16 22:52:12 +02:00
|
|
|
lua_pushstring(L, "author");
|
|
|
|
lua_pushstring(L, game.author.c_str());
|
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
|
|
|
|
lua_pushstring(L, "release");
|
|
|
|
lua_pushinteger(L, game.release);
|
|
|
|
lua_settable(L, top_lvl2);
|
2018-04-17 15:54:50 +02:00
|
|
|
|
2018-05-16 22:52:12 +02:00
|
|
|
lua_pushstring(L, "menuicon_path");
|
|
|
|
lua_pushstring(L, game.menuicon_path.c_str());
|
|
|
|
lua_settable(L, top_lvl2);
|
2018-03-28 23:14:16 +02:00
|
|
|
|
|
|
|
lua_pushstring(L, "addon_mods_paths");
|
|
|
|
lua_newtable(L);
|
|
|
|
int table2 = lua_gettop(L);
|
|
|
|
int internal_index = 1;
|
|
|
|
for (const std::string &addon_mods_path : game.addon_mods_paths) {
|
|
|
|
lua_pushnumber(L, internal_index);
|
|
|
|
lua_pushstring(L, addon_mods_path.c_str());
|
|
|
|
lua_settable(L, table2);
|
|
|
|
internal_index++;
|
|
|
|
}
|
|
|
|
lua_settable(L, top_lvl2);
|
|
|
|
lua_settable(L, top);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2018-04-17 15:54:50 +02:00
|
|
|
int ModApiMainMenu::l_get_content_info(lua_State *L)
|
2018-03-28 23:14:16 +02:00
|
|
|
{
|
|
|
|
std::string path = luaL_checkstring(L, 1);
|
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
ContentSpec spec;
|
2018-03-28 23:14:16 +02:00
|
|
|
spec.path = path;
|
2018-04-17 15:54:50 +02:00
|
|
|
parseContentInfo(spec);
|
2018-03-28 23:14:16 +02:00
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
|
|
|
lua_pushstring(L, spec.name.c_str());
|
|
|
|
lua_setfield(L, -2, "name");
|
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
lua_pushstring(L, spec.type.c_str());
|
2018-03-28 23:14:16 +02:00
|
|
|
lua_setfield(L, -2, "type");
|
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
lua_pushstring(L, spec.author.c_str());
|
|
|
|
lua_setfield(L, -2, "author");
|
|
|
|
|
2018-05-16 22:52:12 +02:00
|
|
|
lua_pushinteger(L, spec.release);
|
|
|
|
lua_setfield(L, -2, "release");
|
|
|
|
|
2018-03-28 23:14:16 +02:00
|
|
|
lua_pushstring(L, spec.desc.c_str());
|
|
|
|
lua_setfield(L, -2, "description");
|
|
|
|
|
|
|
|
lua_pushstring(L, spec.path.c_str());
|
|
|
|
lua_setfield(L, -2, "path");
|
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
if (spec.type == "mod") {
|
|
|
|
ModSpec spec;
|
|
|
|
spec.path = path;
|
|
|
|
parseModContents(spec);
|
2018-03-28 23:14:16 +02:00
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
// Dependencies
|
|
|
|
lua_newtable(L);
|
|
|
|
int i = 1;
|
|
|
|
for (const auto &dep : spec.depends) {
|
|
|
|
lua_pushstring(L, dep.c_str());
|
|
|
|
lua_rawseti(L, -2, i);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
lua_setfield(L, -2, "depends");
|
|
|
|
|
|
|
|
// Optional Dependencies
|
|
|
|
lua_newtable(L);
|
|
|
|
i = 1;
|
|
|
|
for (const auto &dep : spec.optdepends) {
|
|
|
|
lua_pushstring(L, dep.c_str());
|
|
|
|
lua_rawseti(L, -2, i);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
lua_setfield(L, -2, "optional_depends");
|
2018-03-28 23:14:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-23 18:30:21 +02:00
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_show_keys_menu(lua_State *L)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
2013-08-11 04:09:45 +02:00
|
|
|
GUIEngine* engine = getGuiEngine(L);
|
2015-03-06 11:21:51 +01:00
|
|
|
sanity_check(engine != NULL);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2017-06-26 20:11:17 +02:00
|
|
|
GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
|
2013-06-23 18:30:21 +02:00
|
|
|
engine->m_parent,
|
|
|
|
-1,
|
|
|
|
engine->m_menumanager);
|
|
|
|
kmenu->drop();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_create_world(lua_State *L)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
|
|
|
const char *name = luaL_checkstring(L, 1);
|
|
|
|
int gameidx = luaL_checkinteger(L,2) -1;
|
|
|
|
|
|
|
|
std::string path = porting::path_user + DIR_DELIM
|
|
|
|
"worlds" + DIR_DELIM
|
|
|
|
+ name;
|
|
|
|
|
|
|
|
std::vector<SubgameSpec> games = getAvailableGames();
|
|
|
|
|
|
|
|
if ((gameidx >= 0) &&
|
|
|
|
(gameidx < (int) games.size())) {
|
|
|
|
|
|
|
|
// Create world if it doesn't exist
|
2015-03-13 04:35:34 +01:00
|
|
|
if (!loadGameConfAndInitWorld(path, games[gameidx])) {
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushstring(L, "Failed to initialize world");
|
2015-03-13 04:35:34 +01:00
|
|
|
} else {
|
|
|
|
lua_pushnil(L);
|
2013-06-23 18:30:21 +02:00
|
|
|
}
|
2015-03-13 04:35:34 +01:00
|
|
|
} else {
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushstring(L, "Invalid game index");
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_delete_world(lua_State *L)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
|
|
|
int worldidx = luaL_checkinteger(L,1) -1;
|
|
|
|
|
|
|
|
std::vector<WorldSpec> worlds = getAvailableWorlds();
|
|
|
|
|
|
|
|
if ((worldidx >= 0) &&
|
|
|
|
(worldidx < (int) worlds.size())) {
|
|
|
|
|
|
|
|
WorldSpec spec = worlds[worldidx];
|
|
|
|
|
|
|
|
std::vector<std::string> paths;
|
|
|
|
paths.push_back(spec.path);
|
2017-09-13 22:03:18 +02:00
|
|
|
fs::GetRecursiveSubPaths(spec.path, paths, true);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
// Delete files
|
|
|
|
if (!fs::DeletePaths(paths)) {
|
|
|
|
lua_pushstring(L, "Failed to delete world");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lua_pushstring(L, "Invalid world index");
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_set_topleft_text(lua_State *L)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
2013-08-11 04:09:45 +02:00
|
|
|
GUIEngine* engine = getGuiEngine(L);
|
2015-03-06 11:21:51 +01:00
|
|
|
sanity_check(engine != NULL);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
std::string text;
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
if (!lua_isnone(L,1) && !lua_isnil(L,1))
|
|
|
|
text = luaL_checkstring(L, 1);
|
|
|
|
|
|
|
|
engine->setTopleftText(text);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-30 03:23:51 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
|
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
std::vector<const char *> names;
|
2016-06-14 06:10:55 +02:00
|
|
|
Mapgen::getMapgenNames(&names, lua_toboolean(L, 1));
|
2014-12-30 03:23:51 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
lua_newtable(L);
|
|
|
|
for (size_t i = 0; i != names.size(); i++) {
|
|
|
|
lua_pushstring(L, names[i]);
|
|
|
|
lua_rawseti(L, -2, i + 1);
|
2014-12-30 03:23:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-23 18:30:21 +02:00
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_get_modpath(lua_State *L)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
std::string modpath = fs::RemoveRelativePathComponents(
|
|
|
|
porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushstring(L, modpath.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-06-06 14:34:31 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
|
|
|
|
{
|
|
|
|
std::string modpath = fs::RemoveRelativePathComponents(
|
|
|
|
porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM);
|
|
|
|
lua_pushstring(L, modpath.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-23 18:30:21 +02:00
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_get_gamepath(lua_State *L)
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
std::string gamepath = fs::RemoveRelativePathComponents(
|
|
|
|
porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushstring(L, gamepath.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-08-02 15:18:48 +02:00
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_get_texturepath(lua_State *L)
|
2013-08-02 15:18:48 +02:00
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
std::string gamepath = fs::RemoveRelativePathComponents(
|
|
|
|
porting::path_user + DIR_DELIM + "textures");
|
2013-08-02 15:18:48 +02:00
|
|
|
lua_pushstring(L, gamepath.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-12-13 17:35:57 +01:00
|
|
|
int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
|
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
std::string gamepath = fs::RemoveRelativePathComponents(
|
|
|
|
porting::path_share + DIR_DELIM + "textures");
|
2013-12-13 17:35:57 +01:00
|
|
|
lua_pushstring(L, gamepath.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-23 18:30:21 +02:00
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_create_dir(lua_State *L) {
|
2015-10-04 08:54:25 +02:00
|
|
|
const char *path = luaL_checkstring(L, 1);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
if (ModApiMainMenu::isMinetestPath(path)) {
|
2015-10-04 08:54:25 +02:00
|
|
|
lua_pushboolean(L, fs::CreateAllDirs(path));
|
2013-06-23 18:30:21 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2015-10-04 08:54:25 +02:00
|
|
|
|
|
|
|
lua_pushboolean(L, false);
|
2013-06-23 18:30:21 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_delete_dir(lua_State *L)
|
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
const char *path = luaL_checkstring(L, 1);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
std::string absolute_path = fs::RemoveRelativePathComponents(path);
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
if (ModApiMainMenu::isMinetestPath(absolute_path)) {
|
2015-10-04 08:54:25 +02:00
|
|
|
lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
|
2013-06-23 18:30:21 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2015-10-04 08:54:25 +02:00
|
|
|
|
|
|
|
lua_pushboolean(L, false);
|
2013-06-23 18:30:21 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_copy_dir(lua_State *L)
|
|
|
|
{
|
2013-06-23 18:30:21 +02:00
|
|
|
const char *source = luaL_checkstring(L, 1);
|
|
|
|
const char *destination = luaL_checkstring(L, 2);
|
|
|
|
|
|
|
|
bool keep_source = true;
|
|
|
|
|
|
|
|
if ((!lua_isnone(L,3)) &&
|
|
|
|
(!lua_isnil(L,3))) {
|
|
|
|
keep_source = lua_toboolean(L,3);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
|
|
|
|
std::string absolute_source = fs::RemoveRelativePathComponents(source);
|
|
|
|
|
2017-07-04 09:27:29 +02:00
|
|
|
if ((ModApiMainMenu::isMinetestPath(absolute_destination))) {
|
2013-06-23 18:30:21 +02:00
|
|
|
bool retval = fs::CopyDir(absolute_source,absolute_destination);
|
|
|
|
|
|
|
|
if (retval && (!keep_source)) {
|
|
|
|
|
|
|
|
retval &= fs::RecursiveDelete(absolute_source);
|
|
|
|
}
|
|
|
|
lua_pushboolean(L,retval);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
lua_pushboolean(L,false);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_extract_zip(lua_State *L)
|
|
|
|
{
|
2013-06-23 18:30:21 +02:00
|
|
|
const char *zipfile = luaL_checkstring(L, 1);
|
|
|
|
const char *destination = luaL_checkstring(L, 2);
|
|
|
|
|
|
|
|
std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
|
2013-06-23 18:30:21 +02:00
|
|
|
fs::CreateAllDirs(absolute_destination);
|
|
|
|
|
2017-06-26 20:11:17 +02:00
|
|
|
io::IFileSystem *fs = RenderingEngine::get_filesystem();
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2013-12-11 23:07:38 +01:00
|
|
|
if (!fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP)) {
|
|
|
|
lua_pushboolean(L,false);
|
|
|
|
return 1;
|
|
|
|
}
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2015-03-06 11:21:51 +01:00
|
|
|
sanity_check(fs->getFileArchiveCount() > 0);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
/* WARNING this is not threadsafe!! */
|
|
|
|
/**********************************************************************/
|
|
|
|
io::IFileArchive* opened_zip =
|
|
|
|
fs->getFileArchive(fs->getFileArchiveCount()-1);
|
|
|
|
|
|
|
|
const io::IFileList* files_in_zip = opened_zip->getFileList();
|
|
|
|
|
|
|
|
unsigned int number_of_files = files_in_zip->getFileCount();
|
|
|
|
|
2015-01-22 17:09:29 +01:00
|
|
|
for (unsigned int i=0; i < number_of_files; i++) {
|
2013-06-23 18:30:21 +02:00
|
|
|
std::string fullpath = destination;
|
|
|
|
fullpath += DIR_DELIM;
|
|
|
|
fullpath += files_in_zip->getFullFileName(i).c_str();
|
2015-01-22 17:09:29 +01:00
|
|
|
std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2015-01-22 17:09:29 +01:00
|
|
|
if (!files_in_zip->isDirectory(i)) {
|
|
|
|
if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
|
2013-06-23 18:30:21 +02:00
|
|
|
fs->removeFileArchive(fs->getFileArchiveCount()-1);
|
|
|
|
lua_pushboolean(L,false);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-01-22 17:09:29 +01:00
|
|
|
|
2013-06-23 18:30:21 +02:00
|
|
|
io::IReadFile* toread = opened_zip->createAndOpenFile(i);
|
|
|
|
|
|
|
|
FILE *targetfile = fopen(fullpath.c_str(),"wb");
|
|
|
|
|
|
|
|
if (targetfile == NULL) {
|
|
|
|
fs->removeFileArchive(fs->getFileArchiveCount()-1);
|
|
|
|
lua_pushboolean(L,false);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char read_buffer[1024];
|
2015-01-22 17:09:29 +01:00
|
|
|
long total_read = 0;
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
while (total_read < toread->getSize()) {
|
|
|
|
|
|
|
|
unsigned int bytes_read =
|
|
|
|
toread->read(read_buffer,sizeof(read_buffer));
|
2014-02-07 21:29:31 +01:00
|
|
|
if ((bytes_read == 0 ) ||
|
|
|
|
(fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
|
2013-06-23 18:30:21 +02:00
|
|
|
{
|
|
|
|
fclose(targetfile);
|
|
|
|
fs->removeFileArchive(fs->getFileArchiveCount()-1);
|
|
|
|
lua_pushboolean(L,false);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
total_read += bytes_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(targetfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fs->removeFileArchive(fs->getFileArchiveCount()-1);
|
|
|
|
lua_pushboolean(L,true);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushboolean(L,false);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-04-27 23:55:49 +02:00
|
|
|
int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
|
2013-08-11 04:09:45 +02:00
|
|
|
{
|
|
|
|
GUIEngine* engine = getGuiEngine(L);
|
2015-03-06 11:21:51 +01:00
|
|
|
sanity_check(engine != NULL);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
lua_pushstring(L,engine->getScriptDir().c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
bool ModApiMainMenu::isMinetestPath(std::string path)
|
|
|
|
{
|
2013-06-23 18:30:21 +02:00
|
|
|
if (fs::PathStartsWith(path,fs::TempPath()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* games */
|
|
|
|
if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games")))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* mods */
|
2013-07-12 21:56:09 +02:00
|
|
|
if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods")))
|
2013-06-23 18:30:21 +02:00
|
|
|
return true;
|
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
/* mods */
|
|
|
|
if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "textures")))
|
|
|
|
return true;
|
|
|
|
|
2013-06-23 18:30:21 +02:00
|
|
|
/* worlds */
|
|
|
|
if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds")))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-06-11 09:43:31 +02:00
|
|
|
int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
|
2013-08-11 04:09:45 +02:00
|
|
|
{
|
|
|
|
GUIEngine* engine = getGuiEngine(L);
|
2015-03-06 11:21:51 +01:00
|
|
|
sanity_check(engine != NULL);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
const char *formname= luaL_checkstring(L, 1);
|
|
|
|
const char *title = luaL_checkstring(L, 2);
|
2017-06-11 09:43:31 +02:00
|
|
|
bool is_file_select = lua_toboolean(L, 3);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
|
|
|
GUIFileSelectMenu* fileOpenMenu =
|
2017-06-26 20:11:17 +02:00
|
|
|
new GUIFileSelectMenu(RenderingEngine::get_gui_env(),
|
2013-06-23 18:30:21 +02:00
|
|
|
engine->m_parent,
|
|
|
|
-1,
|
|
|
|
engine->m_menumanager,
|
|
|
|
title,
|
2017-06-11 09:43:31 +02:00
|
|
|
formname,
|
|
|
|
is_file_select);
|
2013-06-23 18:30:21 +02:00
|
|
|
fileOpenMenu->setTextDest(engine->m_buttonhandler);
|
|
|
|
fileOpenMenu->drop();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2013-08-11 04:09:45 +02:00
|
|
|
int ModApiMainMenu::l_download_file(lua_State *L)
|
|
|
|
{
|
2013-06-23 18:30:21 +02:00
|
|
|
const char *url = luaL_checkstring(L, 1);
|
|
|
|
const char *target = luaL_checkstring(L, 2);
|
|
|
|
|
|
|
|
//check path
|
|
|
|
std::string absolute_destination = fs::RemoveRelativePathComponents(target);
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
|
2013-11-26 18:15:31 +01:00
|
|
|
if (GUIEngine::downloadFile(url,absolute_destination)) {
|
2013-06-23 18:30:21 +02:00
|
|
|
lua_pushboolean(L,true);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-04-06 15:20:45 +02:00
|
|
|
} else {
|
|
|
|
errorstream << "DOWNLOAD denied: " << absolute_destination
|
|
|
|
<< " isn't a allowed path" << std::endl;
|
2013-06-23 18:30:21 +02:00
|
|
|
}
|
|
|
|
lua_pushboolean(L,false);
|
|
|
|
return 1;
|
|
|
|
}
|
2013-08-11 04:09:45 +02:00
|
|
|
|
2014-07-16 14:04:50 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
int ModApiMainMenu::l_get_video_drivers(lua_State *L)
|
|
|
|
{
|
2017-06-26 20:11:17 +02:00
|
|
|
std::vector<irr::video::E_DRIVER_TYPE> drivers = RenderingEngine::getSupportedVideoDrivers();
|
2015-01-18 19:14:25 +01:00
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
for (u32 i = 0; i != drivers.size(); i++) {
|
2017-06-26 20:11:17 +02:00
|
|
|
const char *name = RenderingEngine::getVideoDriverName(drivers[i]);
|
|
|
|
const char *fname = RenderingEngine::getVideoDriverFriendlyName(drivers[i]);
|
2015-01-18 19:14:25 +01:00
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushstring(L, name);
|
|
|
|
lua_setfield(L, -2, "name");
|
|
|
|
lua_pushstring(L, fname);
|
|
|
|
lua_setfield(L, -2, "friendly_name");
|
|
|
|
|
|
|
|
lua_rawseti(L, -2, i + 1);
|
2014-07-16 14:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-03-28 02:05:39 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
int ModApiMainMenu::l_get_video_modes(lua_State *L)
|
|
|
|
{
|
|
|
|
std::vector<core::vector3d<u32> > videomodes
|
2017-06-26 20:11:17 +02:00
|
|
|
= RenderingEngine::getSupportedVideoModes();
|
2015-03-28 02:05:39 +01:00
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
for (u32 i = 0; i != videomodes.size(); i++) {
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushnumber(L, videomodes[i].X);
|
|
|
|
lua_setfield(L, -2, "w");
|
|
|
|
lua_pushnumber(L, videomodes[i].Y);
|
|
|
|
lua_setfield(L, -2, "h");
|
|
|
|
lua_pushnumber(L, videomodes[i].Z);
|
|
|
|
lua_setfield(L, -2, "depth");
|
|
|
|
|
|
|
|
lua_rawseti(L, -2, i + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-08-14 19:22:23 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
int ModApiMainMenu::l_gettext(lua_State *L)
|
|
|
|
{
|
2015-10-18 02:29:06 +02:00
|
|
|
std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
|
|
|
|
lua_pushstring(L, text.c_str());
|
2013-08-14 19:22:23 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-05 14:12:36 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
int ModApiMainMenu::l_get_screen_info(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_newtable(L);
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
lua_pushstring(L,"density");
|
2017-06-26 20:11:17 +02:00
|
|
|
lua_pushnumber(L,RenderingEngine::getDisplayDensity());
|
2014-04-05 14:12:36 +02:00
|
|
|
lua_settable(L, top);
|
|
|
|
|
|
|
|
lua_pushstring(L,"display_width");
|
2017-06-26 20:11:17 +02:00
|
|
|
lua_pushnumber(L,RenderingEngine::getDisplaySize().X);
|
2014-04-05 14:12:36 +02:00
|
|
|
lua_settable(L, top);
|
|
|
|
|
|
|
|
lua_pushstring(L,"display_height");
|
2017-06-26 20:11:17 +02:00
|
|
|
lua_pushnumber(L,RenderingEngine::getDisplaySize().Y);
|
2014-04-05 14:12:36 +02:00
|
|
|
lua_settable(L, top);
|
|
|
|
|
2017-06-26 20:11:17 +02:00
|
|
|
const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
|
2014-04-05 14:12:36 +02:00
|
|
|
lua_pushstring(L,"window_width");
|
2017-06-26 20:11:17 +02:00
|
|
|
lua_pushnumber(L, window_size.X);
|
2014-04-05 14:12:36 +02:00
|
|
|
lua_settable(L, top);
|
|
|
|
|
|
|
|
lua_pushstring(L,"window_height");
|
2017-06-26 20:11:17 +02:00
|
|
|
lua_pushnumber(L, window_size.Y);
|
2014-04-05 14:12:36 +02:00
|
|
|
lua_settable(L, top);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
int ModApiMainMenu::l_get_package_list(lua_State *L)
|
|
|
|
{
|
|
|
|
std::string url = g_settings->get("contentdb_url");
|
2018-06-18 21:47:38 +02:00
|
|
|
std::vector<Package> packages = getPackagesFromURL(url + "/api/packages/");
|
2018-04-17 15:54:50 +02:00
|
|
|
|
|
|
|
// Make table
|
|
|
|
lua_newtable(L);
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
unsigned int index = 1;
|
|
|
|
|
|
|
|
// Fill table
|
|
|
|
for (const auto &package : packages) {
|
|
|
|
lua_pushnumber(L, index);
|
|
|
|
lua_newtable(L);
|
|
|
|
|
|
|
|
int top_lvl2 = lua_gettop(L);
|
|
|
|
|
2018-06-18 21:47:38 +02:00
|
|
|
lua_pushstring(L, "author");
|
|
|
|
lua_pushstring(L, package.author.c_str());
|
|
|
|
lua_settable (L, top_lvl2);
|
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
lua_pushstring(L, "name");
|
|
|
|
lua_pushstring(L, package.name.c_str());
|
|
|
|
lua_settable (L, top_lvl2);
|
|
|
|
|
|
|
|
lua_pushstring(L, "title");
|
|
|
|
lua_pushstring(L, package.title.c_str());
|
|
|
|
lua_settable (L, top_lvl2);
|
|
|
|
|
|
|
|
lua_pushstring(L, "type");
|
|
|
|
lua_pushstring(L, package.type.c_str());
|
|
|
|
lua_settable (L, top_lvl2);
|
|
|
|
|
|
|
|
lua_pushstring(L, "short_description");
|
|
|
|
lua_pushstring(L, package.shortDesc.c_str());
|
|
|
|
lua_settable (L, top_lvl2);
|
|
|
|
|
2018-05-20 01:25:17 +02:00
|
|
|
lua_pushstring (L, "release");
|
2018-05-16 22:52:12 +02:00
|
|
|
lua_pushinteger(L, package.release);
|
2018-05-20 01:25:17 +02:00
|
|
|
lua_settable (L, top_lvl2);
|
|
|
|
|
2018-06-18 21:47:38 +02:00
|
|
|
if (package.thumbnail != "") {
|
|
|
|
lua_pushstring(L, "thumbnail");
|
|
|
|
lua_pushstring(L, package.thumbnail.c_str());
|
|
|
|
lua_settable (L, top_lvl2);
|
2018-05-20 01:25:17 +02:00
|
|
|
}
|
2018-06-18 21:47:38 +02:00
|
|
|
|
|
|
|
lua_pushstring(L, "url");
|
|
|
|
lua_pushstring(L, package.getDownloadURL(url).c_str());
|
|
|
|
lua_settable (L, top_lvl2);
|
2018-05-16 22:52:12 +02:00
|
|
|
|
2018-04-17 15:54:50 +02:00
|
|
|
lua_settable(L, top);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-02-17 01:37:14 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
|
|
|
|
{
|
2017-09-12 19:48:09 +02:00
|
|
|
lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MIN);
|
2015-02-17 01:37:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-11-26 18:15:31 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
int ModApiMainMenu::l_do_async_callback(lua_State *L)
|
|
|
|
{
|
|
|
|
GUIEngine* engine = getGuiEngine(L);
|
|
|
|
|
2014-04-15 19:41:07 +02:00
|
|
|
size_t func_length, param_length;
|
|
|
|
const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
|
2013-11-26 18:15:31 +01:00
|
|
|
|
2014-04-15 19:41:07 +02:00
|
|
|
const char* serialized_param_raw = luaL_checklstring(L, 2, ¶m_length);
|
2013-11-26 18:15:31 +01:00
|
|
|
|
2015-03-06 11:21:51 +01:00
|
|
|
sanity_check(serialized_func_raw != NULL);
|
|
|
|
sanity_check(serialized_param_raw != NULL);
|
2013-11-26 18:15:31 +01:00
|
|
|
|
2014-04-15 19:41:07 +02:00
|
|
|
std::string serialized_func = std::string(serialized_func_raw, func_length);
|
|
|
|
std::string serialized_param = std::string(serialized_param_raw, param_length);
|
2013-11-26 18:15:31 +01:00
|
|
|
|
2014-04-15 21:10:30 +02:00
|
|
|
lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param));
|
2013-11-26 18:15:31 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
void ModApiMainMenu::Initialize(lua_State *L, int top)
|
|
|
|
{
|
|
|
|
API_FCT(update_formspec);
|
|
|
|
API_FCT(set_clouds);
|
|
|
|
API_FCT(get_textlist_index);
|
2013-08-23 12:24:11 +02:00
|
|
|
API_FCT(get_table_index);
|
2013-08-11 04:09:45 +02:00
|
|
|
API_FCT(get_worlds);
|
|
|
|
API_FCT(get_games);
|
2018-04-17 15:54:50 +02:00
|
|
|
API_FCT(get_content_info);
|
2013-08-11 04:09:45 +02:00
|
|
|
API_FCT(start);
|
|
|
|
API_FCT(close);
|
|
|
|
API_FCT(get_favorites);
|
|
|
|
API_FCT(show_keys_menu);
|
|
|
|
API_FCT(create_world);
|
|
|
|
API_FCT(delete_world);
|
|
|
|
API_FCT(delete_favorite);
|
|
|
|
API_FCT(set_background);
|
|
|
|
API_FCT(set_topleft_text);
|
2014-12-30 03:23:51 +01:00
|
|
|
API_FCT(get_mapgen_names);
|
2013-08-11 04:09:45 +02:00
|
|
|
API_FCT(get_modpath);
|
2017-06-06 14:34:31 +02:00
|
|
|
API_FCT(get_clientmodpath);
|
2013-08-11 04:09:45 +02:00
|
|
|
API_FCT(get_gamepath);
|
|
|
|
API_FCT(get_texturepath);
|
2013-12-13 17:35:57 +01:00
|
|
|
API_FCT(get_texturepath_share);
|
2013-08-11 04:09:45 +02:00
|
|
|
API_FCT(create_dir);
|
|
|
|
API_FCT(delete_dir);
|
|
|
|
API_FCT(copy_dir);
|
|
|
|
API_FCT(extract_zip);
|
2014-04-27 23:55:49 +02:00
|
|
|
API_FCT(get_mainmenu_path);
|
2017-06-11 09:43:31 +02:00
|
|
|
API_FCT(show_path_select_dialog);
|
2013-08-11 04:09:45 +02:00
|
|
|
API_FCT(download_file);
|
2013-08-14 19:22:23 +02:00
|
|
|
API_FCT(gettext);
|
2014-07-16 14:04:50 +02:00
|
|
|
API_FCT(get_video_drivers);
|
2015-03-28 02:05:39 +01:00
|
|
|
API_FCT(get_video_modes);
|
2014-04-05 14:12:36 +02:00
|
|
|
API_FCT(get_screen_info);
|
2018-04-17 15:54:50 +02:00
|
|
|
API_FCT(get_package_list);
|
2015-02-17 01:37:14 +01:00
|
|
|
API_FCT(get_min_supp_proto);
|
|
|
|
API_FCT(get_max_supp_proto);
|
2013-11-26 18:15:31 +01:00
|
|
|
API_FCT(do_async_callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-12-12 20:49:19 +01:00
|
|
|
void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
|
2013-11-26 18:15:31 +01:00
|
|
|
{
|
2014-12-12 20:49:19 +01:00
|
|
|
API_FCT(get_worlds);
|
|
|
|
API_FCT(get_games);
|
|
|
|
API_FCT(get_favorites);
|
|
|
|
API_FCT(get_mapgen_names);
|
|
|
|
API_FCT(get_modpath);
|
2017-06-06 14:34:31 +02:00
|
|
|
API_FCT(get_clientmodpath);
|
2014-12-12 20:49:19 +01:00
|
|
|
API_FCT(get_gamepath);
|
|
|
|
API_FCT(get_texturepath);
|
|
|
|
API_FCT(get_texturepath_share);
|
|
|
|
API_FCT(create_dir);
|
|
|
|
API_FCT(delete_dir);
|
|
|
|
API_FCT(copy_dir);
|
|
|
|
//API_FCT(extract_zip); //TODO remove dependency to GuiEngine
|
|
|
|
API_FCT(download_file);
|
|
|
|
//API_FCT(gettext); (gettext lib isn't threadsafe)
|
2018-04-17 15:54:50 +02:00
|
|
|
API_FCT(get_package_list);
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|