Skip to content

Commit

Permalink
Use JSON for favorites, move server list code to Lua (#10085)
Browse files Browse the repository at this point in the history
Co-authored-by: sfan5 <sfan5@live.de>
  • Loading branch information
rubenwardy and sfan5 committed Jan 22, 2021
1 parent 4fcd000 commit 67aa75d
Show file tree
Hide file tree
Showing 17 changed files with 388 additions and 528 deletions.
53 changes: 0 additions & 53 deletions builtin/mainmenu/common.lua
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions builtin/mainmenu/init.lua
Expand Up @@ -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")
Expand Down
241 changes: 241 additions & 0 deletions builtin/mainmenu/serverlistmgr.lua
@@ -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

0 comments on commit 67aa75d

Please sign in to comment.