summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubenwardy <rw@rubenwardy.com>2021-01-22 15:08:57 +0000
committerGitHub <noreply@github.com>2021-01-22 15:08:57 +0000
commit67aa75d444d0e5cfff2728dbbcffd6f95b2fe88b (patch)
treef3a2079a9b17056508e8197a701cf90447e4ea4b /src
parent4fcd000e20a26120349184cb9d40342b7876e6b8 (diff)
downloadminetest-67aa75d444d0e5cfff2728dbbcffd6f95b2fe88b.tar.gz
minetest-67aa75d444d0e5cfff2728dbbcffd6f95b2fe88b.tar.bz2
minetest-67aa75d444d0e5cfff2728dbbcffd6f95b2fe88b.zip
Use JSON for favorites, move server list code to Lua (#10085)
Co-authored-by: sfan5 <sfan5@live.de>
Diffstat (limited to 'src')
-rw-r--r--src/client/clientlauncher.cpp8
-rw-r--r--src/defaultsettings.cpp2
-rw-r--r--src/script/lua_api/l_mainmenu.cpp206
-rw-r--r--src/script/lua_api/l_mainmenu.h4
-rw-r--r--src/script/lua_api/l_util.cpp13
-rw-r--r--src/script/lua_api/l_util.h3
-rw-r--r--src/serverlist.cpp162
-rw-r--r--src/serverlist.h13
8 files changed, 19 insertions, 392 deletions
diff --git a/src/client/clientlauncher.cpp b/src/client/clientlauncher.cpp
index 29427f609..7245f29f0 100644
--- a/src/client/clientlauncher.cpp
+++ b/src/client/clientlauncher.cpp
@@ -487,14 +487,6 @@ bool ClientLauncher::launch_game(std::string &error_message,
start_data.socket_port = myrand_range(49152, 65535);
} else {
g_settings->set("name", start_data.name);
- if (!start_data.address.empty()) {
- ServerListSpec server;
- server["name"] = server_name;
- server["address"] = start_data.address;
- server["port"] = itos(start_data.socket_port);
- server["description"] = server_description;
- ServerList::insert(server);
- }
}
if (start_data.name.length() > PLAYERNAME_SIZE - 1) {
diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp
index e8fb18e05..114351d86 100644
--- a/src/defaultsettings.cpp
+++ b/src/defaultsettings.cpp
@@ -283,7 +283,7 @@ void set_default_settings(Settings *settings)
// Main menu
settings->setDefault("main_menu_path", "");
- settings->setDefault("serverlist_file", "favoriteservers.txt");
+ settings->setDefault("serverlist_file", "favoriteservers.json");
#if USE_FREETYPE
settings->setDefault("freetype", "true");
diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp
index 5070ec7d4..4733c4003 100644
--- a/src/script/lua_api/l_mainmenu.cpp
+++ b/src/script/lua_api/l_mainmenu.cpp
@@ -275,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();
@@ -1130,11 +929,9 @@ 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);
@@ -1170,7 +967,6 @@ 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);
@@ -1186,5 +982,7 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
//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)
}
diff --git a/src/script/lua_api/l_mainmenu.h b/src/script/lua_api/l_mainmenu.h
index 0b02ed892..580a0df72 100644
--- a/src/script/lua_api/l_mainmenu.h
+++ b/src/script/lua_api/l_mainmenu.h
@@ -74,10 +74,6 @@ private:
static int l_get_mapgen_names(lua_State *L);
- static int l_get_favorites(lua_State *L);
-
- static int l_delete_favorite(lua_State *L);
-
static int l_gettext(lua_State *L);
//packages
diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp
index 6490eb578..203a0dd28 100644
--- a/src/script/lua_api/l_util.cpp
+++ b/src/script/lua_api/l_util.cpp
@@ -250,6 +250,17 @@ int ModApiUtil::l_get_builtin_path(lua_State *L)
return 1;
}
+// get_user_path()
+int ModApiUtil::l_get_user_path(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+
+ std::string path = porting::path_user;
+ lua_pushstring(L, path.c_str());
+
+ return 1;
+}
+
// compress(data, method, level)
int ModApiUtil::l_compress(lua_State *L)
{
@@ -486,6 +497,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)
API_FCT(is_yes);
API_FCT(get_builtin_path);
+ API_FCT(get_user_path);
API_FCT(compress);
API_FCT(decompress);
@@ -539,6 +551,7 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
API_FCT(is_yes);
API_FCT(get_builtin_path);
+ API_FCT(get_user_path);
API_FCT(compress);
API_FCT(decompress);
diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h
index b6c1b58af..dbdd62b99 100644
--- a/src/script/lua_api/l_util.h
+++ b/src/script/lua_api/l_util.h
@@ -68,6 +68,9 @@ private:
// get_builtin_path()
static int l_get_builtin_path(lua_State *L);
+ // get_user_path()
+ static int l_get_user_path(lua_State *L);
+
// compress(data, method, ...)
static int l_compress(lua_State *L);
diff --git a/src/serverlist.cpp b/src/serverlist.cpp
index 80a8c2f1a..3bcab3d58 100644
--- a/src/serverlist.cpp
+++ b/src/serverlist.cpp
@@ -17,181 +17,19 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include <fstream>
#include <iostream>
-#include <sstream>
-#include <algorithm>
-
#include "version.h"
#include "settings.h"
#include "serverlist.h"
#include "filesys.h"
-#include "porting.h"
#include "log.h"
#include "network/networkprotocol.h"
#include <json/json.h>
#include "convert_json.h"
#include "httpfetch.h"
-#include "util/string.h"
namespace ServerList
{
-
-std::string getFilePath()
-{
- std::string serverlist_file = g_settings->get("serverlist_file");
-
- std::string dir_path = "client" DIR_DELIM "serverlist" DIR_DELIM;
- fs::CreateDir(porting::path_user + DIR_DELIM "client");
- fs::CreateDir(porting::path_user + DIR_DELIM + dir_path);
- return porting::path_user + DIR_DELIM + dir_path + serverlist_file;
-}
-
-
-std::vector<ServerListSpec> getLocal()
-{
- std::string path = ServerList::getFilePath();
- std::string liststring;
- fs::ReadFile(path, liststring);
-
- return deSerialize(liststring);
-}
-
-
-std::vector<ServerListSpec> getOnline()
-{
- std::ostringstream geturl;
-
- u16 proto_version_min = CLIENT_PROTOCOL_VERSION_MIN;
-
- geturl << g_settings->get("serverlist_url") <<
- "/list?proto_version_min=" << proto_version_min <<
- "&proto_version_max=" << CLIENT_PROTOCOL_VERSION_MAX;
- Json::Value root = fetchJsonValue(geturl.str(), NULL);
-
- std::vector<ServerListSpec> server_list;
-
- if (!root.isObject()) {
- return server_list;
- }
-
- root = root["list"];
- if (!root.isArray()) {
- return server_list;
- }
-
- for (const Json::Value &i : root) {
- if (i.isObject()) {
- server_list.push_back(i);
- }
- }
-
- return server_list;
-}
-
-
-// Delete a server from the local favorites list
-bool deleteEntry(const ServerListSpec &server)
-{
- std::vector<ServerListSpec> serverlist = ServerList::getLocal();
- for (std::vector<ServerListSpec>::iterator it = serverlist.begin();
- it != serverlist.end();) {
- if ((*it)["address"] == server["address"] &&
- (*it)["port"] == server["port"]) {
- it = serverlist.erase(it);
- } else {
- ++it;
- }
- }
-
- std::string path = ServerList::getFilePath();
- std::ostringstream ss(std::ios_base::binary);
- ss << ServerList::serialize(serverlist);
- if (!fs::safeWriteToFile(path, ss.str()))
- return false;
- return true;
-}
-
-// Insert a server to the local favorites list
-bool insert(const ServerListSpec &server)
-{
- // Remove duplicates
- ServerList::deleteEntry(server);
-
- std::vector<ServerListSpec> serverlist = ServerList::getLocal();
-
- // Insert new server at the top of the list
- serverlist.insert(serverlist.begin(), server);
-
- std::string path = ServerList::getFilePath();
- std::ostringstream ss(std::ios_base::binary);
- ss << ServerList::serialize(serverlist);
- if (!fs::safeWriteToFile(path, ss.str()))
- return false;
-
- return true;
-}
-
-std::vector<ServerListSpec> deSerialize(const std::string &liststring)
-{
- std::vector<ServerListSpec> serverlist;
- std::istringstream stream(liststring);
- std::string line, tmp;
- while (std::getline(stream, line)) {
- std::transform(line.begin(), line.end(), line.begin(), ::toupper);
- if (line == "[SERVER]") {
- ServerListSpec server;
- std::getline(stream, tmp);
- server["name"] = tmp;
- std::getline(stream, tmp);
- server["address"] = tmp;
- std::getline(stream, tmp);
- server["port"] = tmp;
- bool unique = true;
- for (const ServerListSpec &added : serverlist) {
- if (server["name"] == added["name"]
- && server["port"] == added["port"]) {
- unique = false;
- break;
- }
- }
- if (!unique)
- continue;
- std::getline(stream, tmp);
- server["description"] = tmp;
- serverlist.push_back(server);
- }
- }
- return serverlist;
-}
-
-const std::string serialize(const std::vector<ServerListSpec> &serverlist)
-{
- std::string liststring;
- for (const ServerListSpec &it : serverlist) {
- liststring += "[server]\n";
- liststring += it["name"].asString() + '\n';
- liststring += it["address"].asString() + '\n';
- liststring += it["port"].asString() + '\n';
- liststring += it["description"].asString() + '\n';
- liststring += '\n';
- }
- return liststring;
-}
-
-const std::string serializeJson(const std::vector<ServerListSpec> &serverlist)
-{
- Json::Value root;
- Json::Value list(Json::arrayValue);
- for (const ServerListSpec &it : serverlist) {
- list.append(it);
- }
- root["list"] = list;
-
- return fastWriteJson(root);
-}
-
-
#if USE_CURL
void sendAnnounce(AnnounceAction action,
const u16 port,
diff --git a/src/serverlist.h b/src/serverlist.h
index 2b82b7431..4a0bd5efa 100644
--- a/src/serverlist.h
+++ b/src/serverlist.h
@@ -24,21 +24,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
-typedef Json::Value ServerListSpec;
-
namespace ServerList
{
-std::vector<ServerListSpec> getLocal();
-std::vector<ServerListSpec> getOnline();
-
-bool deleteEntry(const ServerListSpec &server);
-bool insert(const ServerListSpec &server);
-
-std::vector<ServerListSpec> deSerialize(const std::string &liststring);
-const std::string serialize(const std::vector<ServerListSpec> &serverlist);
-std::vector<ServerListSpec> deSerializeJson(const std::string &liststring);
-const std::string serializeJson(const std::vector<ServerListSpec> &serverlist);
-
#if USE_CURL
enum AnnounceAction {AA_START, AA_UPDATE, AA_DELETE};
void sendAnnounce(AnnounceAction, u16 port,