aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api/l_mainmenu.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/script/lua_api/l_mainmenu.cpp')
-rw-r--r--src/script/lua_api/l_mainmenu.cpp259
1 files changed, 49 insertions, 210 deletions
diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp
index f32c477c2..ba7f708a4 100644
--- a/src/script/lua_api/l_mainmenu.cpp
+++ b/src/script/lua_api/l_mainmenu.cpp
@@ -29,7 +29,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "porting.h"
#include "filesys.h"
#include "convert_json.h"
-#include "content/packages.h"
#include "content/content.h"
#include "content/subgames.h"
#include "serverlist.h"
@@ -276,207 +275,6 @@ int ModApiMainMenu::l_get_worlds(lua_State *L)
}
/******************************************************************************/
-int ModApiMainMenu::l_get_favorites(lua_State *L)
-{
- std::string listtype = "local";
-
- if (!lua_isnone(L, 1)) {
- listtype = luaL_checkstring(L, 1);
- }
-
- std::vector<ServerListSpec> servers;
-
- if(listtype == "online") {
- servers = ServerList::getOnline();
- } else {
- servers = ServerList::getLocal();
- }
-
- lua_newtable(L);
- int top = lua_gettop(L);
- unsigned int index = 1;
-
- for (const Json::Value &server : servers) {
-
- lua_pushnumber(L, index);
-
- lua_newtable(L);
- int top_lvl2 = lua_gettop(L);
-
- if (!server["clients"].asString().empty()) {
- std::string clients_raw = server["clients"].asString();
- char* endptr = 0;
- int numbervalue = strtol(clients_raw.c_str(), &endptr,10);
-
- if ((!clients_raw.empty()) && (*endptr == 0)) {
- lua_pushstring(L, "clients");
- lua_pushnumber(L, numbervalue);
- lua_settable(L, top_lvl2);
- }
- }
-
- if (!server["clients_max"].asString().empty()) {
-
- std::string clients_max_raw = server["clients_max"].asString();
- char* endptr = 0;
- int numbervalue = strtol(clients_max_raw.c_str(), &endptr,10);
-
- if ((!clients_max_raw.empty()) && (*endptr == 0)) {
- lua_pushstring(L, "clients_max");
- lua_pushnumber(L, numbervalue);
- lua_settable(L, top_lvl2);
- }
- }
-
- if (!server["version"].asString().empty()) {
- lua_pushstring(L, "version");
- std::string topush = server["version"].asString();
- lua_pushstring(L, topush.c_str());
- lua_settable(L, top_lvl2);
- }
-
- if (!server["proto_min"].asString().empty()) {
- lua_pushstring(L, "proto_min");
- lua_pushinteger(L, server["proto_min"].asInt());
- lua_settable(L, top_lvl2);
- }
-
- if (!server["proto_max"].asString().empty()) {
- lua_pushstring(L, "proto_max");
- lua_pushinteger(L, server["proto_max"].asInt());
- lua_settable(L, top_lvl2);
- }
-
- if (!server["password"].asString().empty()) {
- lua_pushstring(L, "password");
- lua_pushboolean(L, server["password"].asBool());
- lua_settable(L, top_lvl2);
- }
-
- if (!server["creative"].asString().empty()) {
- lua_pushstring(L, "creative");
- lua_pushboolean(L, server["creative"].asBool());
- lua_settable(L, top_lvl2);
- }
-
- if (!server["damage"].asString().empty()) {
- lua_pushstring(L, "damage");
- lua_pushboolean(L, server["damage"].asBool());
- lua_settable(L, top_lvl2);
- }
-
- if (!server["pvp"].asString().empty()) {
- lua_pushstring(L, "pvp");
- lua_pushboolean(L, server["pvp"].asBool());
- lua_settable(L, top_lvl2);
- }
-
- if (!server["description"].asString().empty()) {
- lua_pushstring(L, "description");
- std::string topush = server["description"].asString();
- lua_pushstring(L, topush.c_str());
- lua_settable(L, top_lvl2);
- }
-
- if (!server["name"].asString().empty()) {
- lua_pushstring(L, "name");
- std::string topush = server["name"].asString();
- lua_pushstring(L, topush.c_str());
- lua_settable(L, top_lvl2);
- }
-
- if (!server["address"].asString().empty()) {
- lua_pushstring(L, "address");
- std::string topush = server["address"].asString();
- lua_pushstring(L, topush.c_str());
- lua_settable(L, top_lvl2);
- }
-
- if (!server["port"].asString().empty()) {
- lua_pushstring(L, "port");
- std::string topush = server["port"].asString();
- lua_pushstring(L, topush.c_str());
- lua_settable(L, top_lvl2);
- }
-
- if (server.isMember("ping")) {
- float ping = server["ping"].asFloat();
- lua_pushstring(L, "ping");
- lua_pushnumber(L, ping);
- lua_settable(L, top_lvl2);
- }
-
- if (server["clients_list"].isArray()) {
- unsigned int index_lvl2 = 1;
- lua_pushstring(L, "clients_list");
- lua_newtable(L);
- int top_lvl3 = lua_gettop(L);
- for (const Json::Value &client : server["clients_list"]) {
- lua_pushnumber(L, index_lvl2);
- std::string topush = client.asString();
- lua_pushstring(L, topush.c_str());
- lua_settable(L, top_lvl3);
- index_lvl2++;
- }
- lua_settable(L, top_lvl2);
- }
-
- if (server["mods"].isArray()) {
- unsigned int index_lvl2 = 1;
- lua_pushstring(L, "mods");
- lua_newtable(L);
- int top_lvl3 = lua_gettop(L);
- for (const Json::Value &mod : server["mods"]) {
-
- lua_pushnumber(L, index_lvl2);
- std::string topush = mod.asString();
- lua_pushstring(L, topush.c_str());
- lua_settable(L, top_lvl3);
- index_lvl2++;
- }
- lua_settable(L, top_lvl2);
- }
-
- lua_settable(L, top);
- index++;
- }
- return 1;
-}
-
-/******************************************************************************/
-int ModApiMainMenu::l_delete_favorite(lua_State *L)
-{
- 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;
-
-
- 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;
-}
-
-/******************************************************************************/
int ModApiMainMenu::l_get_games(lua_State *L)
{
std::vector<SubgameSpec> games = getAvailableGames();
@@ -618,7 +416,7 @@ int ModApiMainMenu::l_create_world(lua_State *L)
std::string path = porting::path_user + DIR_DELIM
"worlds" + DIR_DELIM
- + name;
+ + sanitizeDirName(name, "world_");
std::vector<SubgameSpec> games = getAvailableGames();
@@ -626,10 +424,11 @@ int ModApiMainMenu::l_create_world(lua_State *L)
(gameidx < (int) games.size())) {
// Create world if it doesn't exist
- if (!loadGameConfAndInitWorld(path, games[gameidx])) {
- lua_pushstring(L, "Failed to initialize world");
- } else {
+ try {
+ loadGameConfAndInitWorld(path, name, games[gameidx], true);
lua_pushnil(L);
+ } catch (const BaseException &e) {
+ lua_pushstring(L, (std::string("Failed to initialize world: ") + e.what()).c_str());
}
} else {
lua_pushstring(L, "Invalid game index");
@@ -687,6 +486,14 @@ int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
/******************************************************************************/
+int ModApiMainMenu::l_get_user_path(lua_State *L)
+{
+ std::string path = fs::RemoveRelativePathComponents(porting::path_user);
+ lua_pushstring(L, path.c_str());
+ return 1;
+}
+
+/******************************************************************************/
int ModApiMainMenu::l_get_modpath(lua_State *L)
{
std::string modpath = fs::RemoveRelativePathComponents(
@@ -722,6 +529,7 @@ int ModApiMainMenu::l_get_texturepath(lua_State *L)
return 1;
}
+/******************************************************************************/
int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
{
std::string gamepath = fs::RemoveRelativePathComponents(
@@ -730,6 +538,7 @@ int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
return 1;
}
+/******************************************************************************/
int ModApiMainMenu::l_get_cache_path(lua_State *L)
{
lua_pushstring(L, fs::RemoveRelativePathComponents(porting::path_cache).c_str());
@@ -737,6 +546,13 @@ int ModApiMainMenu::l_get_cache_path(lua_State *L)
}
/******************************************************************************/
+int ModApiMainMenu::l_get_temp_path(lua_State *L)
+{
+ lua_pushstring(L, fs::TempPath().c_str());
+ return 1;
+}
+
+/******************************************************************************/
int ModApiMainMenu::l_create_dir(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
@@ -796,6 +612,15 @@ int ModApiMainMenu::l_copy_dir(lua_State *L)
}
/******************************************************************************/
+int ModApiMainMenu::l_is_dir(lua_State *L)
+{
+ const char *path = luaL_checkstring(L, 1);
+
+ lua_pushboolean(L, fs::IsDir(path));
+ return 1;
+}
+
+/******************************************************************************/
int ModApiMainMenu::l_extract_zip(lua_State *L)
{
const char *zipfile = luaL_checkstring(L, 1);
@@ -1067,7 +892,15 @@ int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
int ModApiMainMenu::l_open_url(lua_State *L)
{
std::string url = luaL_checkstring(L, 1);
- lua_pushboolean(L, porting::openURL(url));
+ lua_pushboolean(L, porting::open_url(url));
+ return 1;
+}
+
+/******************************************************************************/
+int ModApiMainMenu::l_open_dir(lua_State *L)
+{
+ std::string path = luaL_checkstring(L, 1);
+ lua_pushboolean(L, porting::open_directory(path));
return 1;
}
@@ -1105,23 +938,24 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
API_FCT(get_content_info);
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);
API_FCT(get_mapgen_names);
+ API_FCT(get_user_path);
API_FCT(get_modpath);
API_FCT(get_clientmodpath);
API_FCT(get_gamepath);
API_FCT(get_texturepath);
API_FCT(get_texturepath_share);
API_FCT(get_cache_path);
+ API_FCT(get_temp_path);
API_FCT(create_dir);
API_FCT(delete_dir);
API_FCT(copy_dir);
+ API_FCT(is_dir);
API_FCT(extract_zip);
API_FCT(may_modify_path);
API_FCT(get_mainmenu_path);
@@ -1134,6 +968,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
API_FCT(get_min_supp_proto);
API_FCT(get_max_supp_proto);
API_FCT(open_url);
+ API_FCT(open_dir);
API_FCT(do_async_callback);
}
@@ -1142,19 +977,23 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
{
API_FCT(get_worlds);
API_FCT(get_games);
- API_FCT(get_favorites);
API_FCT(get_mapgen_names);
+ API_FCT(get_user_path);
API_FCT(get_modpath);
API_FCT(get_clientmodpath);
API_FCT(get_gamepath);
API_FCT(get_texturepath);
API_FCT(get_texturepath_share);
API_FCT(get_cache_path);
+ API_FCT(get_temp_path);
API_FCT(create_dir);
API_FCT(delete_dir);
API_FCT(copy_dir);
+ API_FCT(is_dir);
//API_FCT(extract_zip); //TODO remove dependency to GuiEngine
API_FCT(may_modify_path);
API_FCT(download_file);
+ API_FCT(get_min_supp_proto);
+ API_FCT(get_max_supp_proto);
//API_FCT(gettext); (gettext lib isn't threadsafe)
}