Use JSON for favorites, move server list code to Lua (#10085)

Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
rubenwardy 2021-01-22 15:08:57 +00:00 committed by GitHub
parent 4fcd000e20
commit 67aa75d444
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 388 additions and 528 deletions

View File

@ -62,24 +62,6 @@ function image_column(tooltip, flagname)
"5=" .. core.formspec_escape(defaulttexturedir .. "server_ping_1.png")
end
--------------------------------------------------------------------------------
function order_favorite_list(list)
local res = {}
--orders the favorite list after support
for i = 1, #list do
local fav = list[i]
if is_server_protocol_compat(fav.proto_min, fav.proto_max) then
res[#res + 1] = fav
end
end
for i = 1, #list do
local fav = list[i]
if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then
res[#res + 1] = fav
end
end
return res
end
--------------------------------------------------------------------------------
function render_serverlist_row(spec, is_favorite)
@ -226,41 +208,6 @@ function menu_handle_key_up_down(fields, textlist, settingname)
return false
end
--------------------------------------------------------------------------------
function asyncOnlineFavourites()
if not menudata.public_known then
menudata.public_known = {{
name = fgettext("Loading..."),
description = fgettext_ne("Try reenabling public serverlist and check your internet connection.")
}}
end
menudata.favorites = menudata.public_known
menudata.favorites_is_public = true
if not menudata.public_downloading then
menudata.public_downloading = true
else
return
end
core.handle_async(
function(param)
return core.get_favorites("online")
end,
nil,
function(result)
menudata.public_downloading = nil
local favs = order_favorite_list(result)
if favs[1] then
menudata.public_known = favs
menudata.favorites = menudata.public_known
menudata.favorites_is_public = true
end
core.event_handler("Refresh")
end
)
end
--------------------------------------------------------------------------------
function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
local textlines = core.wrap_text(text, textlen, true)

View File

@ -34,6 +34,7 @@ dofile(basepath .. "fstk" .. DIR_DELIM .. "ui.lua")
dofile(menupath .. DIR_DELIM .. "async_event.lua")
dofile(menupath .. DIR_DELIM .. "common.lua")
dofile(menupath .. DIR_DELIM .. "pkgmgr.lua")
dofile(menupath .. DIR_DELIM .. "serverlistmgr.lua")
dofile(menupath .. DIR_DELIM .. "textures.lua")
dofile(menupath .. DIR_DELIM .. "dlg_config_world.lua")

View File

@ -0,0 +1,241 @@
--Minetest
--Copyright (C) 2020 rubenwardy
--
--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.
serverlistmgr = {}
--------------------------------------------------------------------------------
local function order_server_list(list)
local res = {}
--orders the favorite list after support
for i = 1, #list do
local fav = list[i]
if is_server_protocol_compat(fav.proto_min, fav.proto_max) then
res[#res + 1] = fav
end
end
for i = 1, #list do
local fav = list[i]
if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then
res[#res + 1] = fav
end
end
return res
end
local public_downloading = false
--------------------------------------------------------------------------------
function serverlistmgr.sync()
if not serverlistmgr.servers then
serverlistmgr.servers = {{
name = fgettext("Loading..."),
description = fgettext_ne("Try reenabling public serverlist and check your internet connection.")
}}
end
if public_downloading then
return
end
public_downloading = true
core.handle_async(
function(param)
local http = core.get_http_api()
local url = ("%s/list?proto_version_min=%d&proto_version_max=%d"):format(
core.settings:get("serverlist_url"),
core.get_min_supp_proto(),
core.get_max_supp_proto())
local response = http.fetch_sync({ url = url })
if not response.succeeded then
return {}
end
local retval = core.parse_json(response.data)
return retval and retval.list or {}
end,
nil,
function(result)
public_downloading = nil
local favs = order_server_list(result)
if favs[1] then
serverlistmgr.servers = favs
end
core.event_handler("Refresh")
end
)
end
--------------------------------------------------------------------------------
local function get_favorites_path()
local base = core.get_user_path() .. DIR_DELIM .. "client" .. DIR_DELIM .. "serverlist" .. DIR_DELIM
return base .. core.settings:get("serverlist_file")
end
--------------------------------------------------------------------------------
local function save_favorites(favorites)
local filename = core.settings:get("serverlist_file")
-- If setting specifies legacy format change the filename to the new one
if filename:sub(#filename - 3):lower() == ".txt" then
core.settings:set("serverlist_file", filename:sub(1, #filename - 4) .. ".json")
end
local path = get_favorites_path()
core.create_dir(path)
core.safe_file_write(path, core.write_json(favorites))
end
--------------------------------------------------------------------------------
function serverlistmgr.read_legacy_favorites(path)
local file = io.open(path, "r")
if not file then
return nil
end
local lines = {}
for line in file:lines() do
lines[#lines + 1] = line
end
file:close()
local favorites = {}
local i = 1
while i < #lines do
local function pop()
local line = lines[i]
i = i + 1
return line and line:trim()
end
if pop():lower() == "[server]" then
local name = pop()
local address = pop()
local port = tonumber(pop())
local description = pop()
if name == "" then
name = nil
end
if description == "" then
description = nil
end
if not address or #address < 3 then
core.log("warning", "Malformed favorites file, missing address at line " .. i)
elseif not port or port < 1 or port > 65535 then
core.log("warning", "Malformed favorites file, missing port at line " .. i)
elseif (name and name:upper() == "[SERVER]") or
(address and address:upper() == "[SERVER]") or
(description and description:upper() == "[SERVER]") then
core.log("warning", "Potentially malformed favorites file, overran at line " .. i)
else
favorites[#favorites + 1] = {
name = name,
address = address,
port = port,
description = description
}
end
end
end
return favorites
end
--------------------------------------------------------------------------------
local function read_favorites()
local path = get_favorites_path()
-- If new format configured fall back to reading the legacy file
if path:sub(#path - 4):lower() == ".json" then
local file = io.open(path, "r")
if file then
local json = file:read("*all")
file:close()
return core.parse_json(json)
end
path = path:sub(1, #path - 5) .. ".txt"
end
local favs = serverlistmgr.read_legacy_favorites(path)
if favs then
save_favorites(favs)
os.remove(path)
end
return favs
end
--------------------------------------------------------------------------------
local function delete_favorite(favorites, del_favorite)
for i=1, #favorites do
local fav = favorites[i]
if fav.address == del_favorite.address and fav.port == del_favorite.port then
table.remove(favorites, i)
return
end
end
end
--------------------------------------------------------------------------------
function serverlistmgr.get_favorites()
if serverlistmgr.favorites then
return serverlistmgr.favorites
end
serverlistmgr.favorites = {}
-- Add favorites, removing duplicates
local seen = {}
for _, fav in ipairs(read_favorites() or {}) do
local key = ("%s:%d"):format(fav.address:lower(), fav.port)
if not seen[key] then
seen[key] = true
serverlistmgr.favorites[#serverlistmgr.favorites + 1] = fav
end
end
return serverlistmgr.favorites
end
--------------------------------------------------------------------------------
function serverlistmgr.add_favorite(new_favorite)
assert(type(new_favorite.port) == "number")
-- Whitelist favorite keys
new_favorite = {
name = new_favorite.name,
address = new_favorite.address,
port = new_favorite.port,
description = new_favorite.description,
}
local favorites = serverlistmgr.get_favorites()
delete_favorite(favorites, new_favorite)
table.insert(favorites, 1, new_favorite)
save_favorites(favorites)
end
--------------------------------------------------------------------------------
function serverlistmgr.delete_favorite(del_favorite)
local favorites = serverlistmgr.get_favorites()
delete_favorite(favorites, del_favorite)
save_favorites(favorites)
end

View File

@ -20,11 +20,11 @@ local function get_formspec(tabview, name, tabdata)
-- Update the cached supported proto info,
-- it may have changed after a change by the settings menu.
common_update_cached_supp_proto()
local fav_selected
local selected
if menudata.search_result then
fav_selected = menudata.search_result[tabdata.fav_selected]
selected = menudata.search_result[tabdata.selected]
else
fav_selected = menudata.favorites[tabdata.fav_selected]
selected = serverlistmgr.servers[tabdata.selected]
end
if not tabdata.search_for then
@ -58,18 +58,18 @@ local function get_formspec(tabview, name, tabdata)
-- Connect
"button[9.88,4.9;2.3,1;btn_mp_connect;" .. fgettext("Connect") .. "]"
if tabdata.fav_selected and fav_selected then
if tabdata.selected and selected then
if gamedata.fav then
retval = retval .. "button[7.73,4.9;2.3,1;btn_delete_favorite;" ..
fgettext("Del. Favorite") .. "]"
end
if fav_selected.description then
if selected.description then
retval = retval .. "textarea[8.1,2.3;4.23,2.9;;;" ..
core.formspec_escape((gamedata.serverdescription or ""), true) .. "]"
end
end
--favourites
--favorites
retval = retval .. "tablecolumns[" ..
image_column(fgettext("Favorite"), "favorite") .. ";" ..
image_column(fgettext("Ping")) .. ",padding=0.25;" ..
@ -83,13 +83,12 @@ local function get_formspec(tabview, name, tabdata)
image_column(fgettext("PvP enabled"), "pvp") .. ",padding=0.25;" ..
"color,span=1;" ..
"text,padding=1]" ..
"table[-0.15,0.6;7.75,5.15;favourites;"
"table[-0.15,0.6;7.75,5.15;favorites;"
if menudata.search_result then
local favs = serverlistmgr.get_favorites()
for i = 1, #menudata.search_result do
local favs = core.get_favorites("local")
local server = menudata.search_result[i]
for fav_id = 1, #favs do
if server.address == favs[fav_id].address and
server.port == favs[fav_id].port then
@ -103,29 +102,30 @@ local function get_formspec(tabview, name, tabdata)
retval = retval .. render_serverlist_row(server, server.is_favorite)
end
elseif #menudata.favorites > 0 then
local favs = core.get_favorites("local")
elseif #serverlistmgr.servers > 0 then
local favs = serverlistmgr.get_favorites()
if #favs > 0 then
for i = 1, #favs do
for j = 1, #menudata.favorites do
if menudata.favorites[j].address == favs[i].address and
menudata.favorites[j].port == favs[i].port then
table.insert(menudata.favorites, i, table.remove(menudata.favorites, j))
for j = 1, #serverlistmgr.servers do
if serverlistmgr.servers[j].address == favs[i].address and
serverlistmgr.servers[j].port == favs[i].port then
table.insert(serverlistmgr.servers, i, table.remove(serverlistmgr.servers, j))
end
end
end
if favs[i].address ~= menudata.favorites[i].address then
table.insert(menudata.favorites, i, favs[i])
if favs[i].address ~= serverlistmgr.servers[i].address then
table.insert(serverlistmgr.servers, i, favs[i])
end
end
end
retval = retval .. render_serverlist_row(menudata.favorites[1], (#favs > 0))
for i = 2, #menudata.favorites do
retval = retval .. "," .. render_serverlist_row(menudata.favorites[i], (i <= #favs))
retval = retval .. render_serverlist_row(serverlistmgr.servers[1], (#favs > 0))
for i = 2, #serverlistmgr.servers do
retval = retval .. "," .. render_serverlist_row(serverlistmgr.servers[i], (i <= #favs))
end
end
if tabdata.fav_selected then
retval = retval .. ";" .. tabdata.fav_selected .. "]"
if tabdata.selected then
retval = retval .. ";" .. tabdata.selected .. "]"
else
retval = retval .. ";0]"
end
@ -135,21 +135,20 @@ end
--------------------------------------------------------------------------------
local function main_button_handler(tabview, fields, name, tabdata)
local serverlist = menudata.search_result or menudata.favorites
local serverlist = menudata.search_result or serverlistmgr.servers
if fields.te_name then
gamedata.playername = fields.te_name
core.settings:set("name", fields.te_name)
end
if fields.favourites then
local event = core.explode_table_event(fields.favourites)
if fields.favorites then
local event = core.explode_table_event(fields.favorites)
local fav = serverlist[event.row]
if event.type == "DCL" then
if event.row <= #serverlist then
if menudata.favorites_is_public and
not is_server_protocol_compat_or_error(
if not is_server_protocol_compat_or_error(
fav.proto_min, fav.proto_max) then
return true
end
@ -178,7 +177,7 @@ local function main_button_handler(tabview, fields, name, tabdata)
if event.type == "CHG" then
if event.row <= #serverlist then
gamedata.fav = false
local favs = core.get_favorites("local")
local favs = serverlistmgr.get_favorites()
local address = fav.address
local port = fav.port
gamedata.serverdescription = fav.description
@ -194,28 +193,28 @@ local function main_button_handler(tabview, fields, name, tabdata)
core.settings:set("address", address)
core.settings:set("remote_port", port)
end
tabdata.fav_selected = event.row
tabdata.selected = event.row
end
return true
end
end
if fields.key_up or fields.key_down then
local fav_idx = core.get_table_index("favourites")
local fav_idx = core.get_table_index("favorites")
local fav = serverlist[fav_idx]
if fav_idx then
if fields.key_up and fav_idx > 1 then
fav_idx = fav_idx - 1
elseif fields.key_down and fav_idx < #menudata.favorites then
elseif fields.key_down and fav_idx < #serverlistmgr.servers then
fav_idx = fav_idx + 1
end
else
fav_idx = 1
end
if not menudata.favorites or not fav then
tabdata.fav_selected = 0
if not serverlistmgr.servers or not fav then
tabdata.selected = 0
return true
end
@ -227,17 +226,17 @@ local function main_button_handler(tabview, fields, name, tabdata)
core.settings:set("remote_port", port)
end
tabdata.fav_selected = fav_idx
tabdata.selected = fav_idx
return true
end
if fields.btn_delete_favorite then
local current_favourite = core.get_table_index("favourites")
if not current_favourite then return end
local current_favorite = core.get_table_index("favorites")
if not current_favorite then return end
core.delete_favorite(current_favourite)
asyncOnlineFavourites()
tabdata.fav_selected = nil
serverlistmgr.delete_favorite(serverlistmgr.servers[current_favorite])
serverlistmgr.sync()
tabdata.selected = nil
core.settings:set("address", "")
core.settings:set("remote_port", "30000")
@ -251,11 +250,11 @@ local function main_button_handler(tabview, fields, name, tabdata)
end
if fields.btn_mp_search or fields.key_enter_field == "te_search" then
tabdata.fav_selected = 1
tabdata.selected = 1
local input = fields.te_search:lower()
tabdata.search_for = fields.te_search
if #menudata.favorites < 2 then
if #serverlistmgr.servers < 2 then
return true
end
@ -275,8 +274,8 @@ local function main_button_handler(tabview, fields, name, tabdata)
-- Search the serverlist
local search_result = {}
for i = 1, #menudata.favorites do
local server = menudata.favorites[i]
for i = 1, #serverlistmgr.servers do
local server = serverlistmgr.servers[i]
local found = 0
for k = 1, #keywords do
local keyword = keywords[k]
@ -293,7 +292,7 @@ local function main_button_handler(tabview, fields, name, tabdata)
end
end
if found > 0 then
local points = (#menudata.favorites - i) / 5 + found
local points = (#serverlistmgr.servers - i) / 5 + found
server.points = points
table.insert(search_result, server)
end
@ -312,7 +311,7 @@ local function main_button_handler(tabview, fields, name, tabdata)
end
if fields.btn_mp_refresh then
asyncOnlineFavourites()
serverlistmgr.sync()
return true
end
@ -321,30 +320,36 @@ local function main_button_handler(tabview, fields, name, tabdata)
gamedata.playername = fields.te_name
gamedata.password = fields.te_pwd
gamedata.address = fields.te_address
gamedata.port = fields.te_port
gamedata.port = tonumber(fields.te_port)
gamedata.selected_world = 0
local fav_idx = core.get_table_index("favourites")
local fav_idx = core.get_table_index("favorites")
local fav = serverlist[fav_idx]
if fav_idx and fav_idx <= #serverlist and
fav.address == fields.te_address and
fav.port == fields.te_port then
fav.address == gamedata.address and
fav.port == gamedata.port then
serverlistmgr.add_favorite(fav)
gamedata.servername = fav.name
gamedata.serverdescription = fav.description
if menudata.favorites_is_public and
not is_server_protocol_compat_or_error(
if not is_server_protocol_compat_or_error(
fav.proto_min, fav.proto_max) then
return true
end
else
gamedata.servername = ""
gamedata.serverdescription = ""
serverlistmgr.add_favorite({
address = gamedata.address,
port = gamedata.port,
})
end
core.settings:set("address", fields.te_address)
core.settings:set("remote_port", fields.te_port)
core.settings:set("address", gamedata.address)
core.settings:set("remote_port", gamedata.port)
core.start()
return true
@ -354,7 +359,7 @@ end
local function on_change(type, old_tab, new_tab)
if type == "LEAVE" then return end
asyncOnlineFavourites()
serverlistmgr.sync()
end
--------------------------------------------------------------------------------

View File

@ -0,0 +1,29 @@
[server]
127.0.0.1
30000
[server]
localhost
30000
[server]
vps.rubenwardy.com
30001
[server]
gundul.ddnss.de
39155
[server]
VanessaE's Dreambuilder creative Server
daconcepts.com
30000
VanessaE's Dreambuilder creative-mode server. Lots of mods, whitelisted buckets.

View File

@ -0,0 +1,36 @@
_G.core = {}
_G.unpack = table.unpack
_G.serverlistmgr = {}
dofile("builtin/common/misc_helpers.lua")
dofile("builtin/mainmenu/serverlistmgr.lua")
local base = "builtin/mainmenu/tests/"
describe("legacy favorites", function()
it("loads well-formed correctly", function()
local favs = serverlistmgr.read_legacy_favorites(base .. "favorites_wellformed.txt")
local expected = {
{
address = "127.0.0.1",
port = 30000,
},
{ address = "localhost", port = 30000 },
{ address = "vps.rubenwardy.com", port = 30001 },
{ address = "gundul.ddnss.de", port = 39155 },
{
address = "daconcepts.com",
port = 30000,
name = "VanessaE's Dreambuilder creative Server",
description = "VanessaE's Dreambuilder creative-mode server. Lots of mods, whitelisted buckets."
},
}
assert.same(expected, favs)
end)
end)

View File

@ -964,7 +964,7 @@ serverlist_url (Serverlist URL) string servers.minetest.net
# File in client/serverlist/ that contains your favorite servers displayed in the
# Multiplayer Tab.
serverlist_file (Serverlist file) string favoriteservers.txt
serverlist_file (Serverlist file) string favoriteservers.json
# Maximum size of the out chat queue.
# 0 to disable queueing and -1 to make the queue size unlimited.

View File

@ -253,32 +253,6 @@ Package - content which is downloadable from the content db, may or may not be i
}
Favorites
---------
core.get_favorites(location) -> list of favorites (possible in async calls)
^ location: "local" or "online"
^ returns {
[1] = {
clients = <number of clients/nil>,
clients_max = <maximum number of clients/nil>,
version = <server version/nil>,
password = <true/nil>,
creative = <true/nil>,
damage = <true/nil>,
pvp = <true/nil>,
description = <server description/nil>,
name = <server name/nil>,
address = <address of server/nil>,
port = <port>
clients_list = <array of clients/nil>
mods = <array of mods/nil>
},
...
}
core.delete_favorite(id, location) -> success
Logging
-------

View File

@ -1160,7 +1160,7 @@
# File in client/serverlist/ that contains your favorite servers displayed in the
# Multiplayer Tab.
# type: string
# serverlist_file = favoriteservers.txt
# serverlist_file = favoriteservers.json
# Maximum size of the out chat queue.
# 0 to disable queueing and -1 to make the queue size unlimited.

View File

@ -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) {

View File

@ -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");

View File

@ -274,207 +274,6 @@ int ModApiMainMenu::l_get_worlds(lua_State *L)
return 1;
}
/******************************************************************************/
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)
{
@ -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)
}

View File

@ -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

View File

@ -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);

View File

@ -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);

View File

@ -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,

View File

@ -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,