Skip to content

Commit

Permalink
Move values the mainmenu caches to dedicated file(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Mar 4, 2024
1 parent e734b3f commit f829d48
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 132 deletions.
35 changes: 30 additions & 5 deletions builtin/mainmenu/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@
-- Global menu data
menudata = {}

-- located in user cache path, for remembering this like e.g. last update check
cache_settings = Settings(core.get_cache_path() .. DIR_DELIM .. "common.conf")

--- Checks if the given key contains a timestamp less than a certain age.
--- Pair this with a call to `cache_settings:set(key, tostring(os.time()))`
--- after successfully refreshing the cache.
--- @param key Name of entry in cache_settings
--- @param max_age Age to check against, in seconds
--- @return true if the max age is not reached
function check_cache_age(key, max_age)
local time_now = os.time()
local time_checked = tonumber(cache_settings:get(key)) or 0
return time_now - time_checked < max_age
end

function core.on_before_close()
-- called before the menu is closed, either exit or to join a game
cache_settings:write()
end

-- Local cached values
local min_supp_proto, max_supp_proto

Expand All @@ -27,6 +47,16 @@ function common_update_cached_supp_proto()
end
common_update_cached_supp_proto()

-- Other global functions

function core.sound_stop(handle, ...)
return handle:stop(...)
end

function os.tmpname()
error('do not use') -- instead: core.get_temp_path()
end

-- Menu helper functions

local function render_client_count(n)
Expand Down Expand Up @@ -140,11 +170,6 @@ function render_serverlist_row(spec)

return table.concat(details, ",")
end
---------------------------------------------------------------------------------
os.tmpname = function()
error('do not use') -- instead use core.get_temp_path()
end
--------------------------------------------------------------------------------

function menu_render_worldlist()
local retval = {}
Expand Down
21 changes: 16 additions & 5 deletions builtin/mainmenu/content/update_detector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,23 @@ if not core.get_http_api then
end


assert(core.create_dir(core.get_cache_path() .. DIR_DELIM .. "cdb"))
local cache_file_path = core.get_cache_path() .. DIR_DELIM .. "cdb" .. DIR_DELIM .. "updates.json"
local has_fetched = false
local latest_releases
do
local tmp = core.get_once("cdb_latest_releases")
if tmp then
latest_releases = core.deserialize(tmp, true)
has_fetched = latest_releases ~= nil
if check_cache_age("cdb_updates_last_checked", 3 * 3600) then
local f = io.open(cache_file_path, "r")
local data = ""
if f then
data = f:read("*a")
f:close()
end
data = data ~= "" and core.parse_json(data) or nil
if type(data) == "table" then
latest_releases = data
has_fetched = true
end
end
end

Expand Down Expand Up @@ -97,7 +107,8 @@ local function fetch()
return
end
latest_releases = lowercase_keys(releases)
core.set_once("cdb_latest_releases", core.serialize(latest_releases))
core.safe_file_write(cache_file_path, core.write_json(latest_releases))
cache_settings:set("cdb_updates_last_checked", tostring(os.time()))

if update_detector.get_count() > 0 then
local maintab = ui.find_by_name("maintab")
Expand Down
23 changes: 19 additions & 4 deletions builtin/mainmenu/dlg_reinstall_mtg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,30 @@
--with this program; if not, write to the Free Software Foundation, Inc.,
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

---- IMPORTANT ----
-- This whole file can be removed after a while.
-- It was only directly useful for upgrades from 5.7.0 to 5.8.0, but
-- maybe some odd fellow directly upgrades from 5.6.1 to 5.9.0 in the future...
-- see <https://github.com/minetest/minetest/pull/13850> in case it's not obvious
---- ----

local SETTING_NAME = "no_mtg_notification"

function check_reinstall_mtg()
if core.settings:get_bool("no_mtg_notification") then
-- used to be in minetest.conf
if core.settings:get_bool(SETTING_NAME) then
cache_settings:set_bool(SETTING_NAME, true)
core.settings:remove(SETTING_NAME)
end

if cache_settings:get_bool(SETTING_NAME) then
return
end

local games = core.get_games()
for _, game in ipairs(games) do
if game.id == "minetest" then
core.settings:set_bool("no_mtg_notification", true)
cache_settings:set_bool(SETTING_NAME, true)
return
end
end
Expand All @@ -37,7 +52,7 @@ function check_reinstall_mtg()
end
end
if not mtg_world_found then
core.settings:set_bool("no_mtg_notification", true)
cache_settings:set_bool(SETTING_NAME, true)
return
end

Expand Down Expand Up @@ -87,7 +102,7 @@ local function buttonhandler(this, fields)
end

if fields.dismiss then
core.settings:set_bool("no_mtg_notification", true)
cache_settings:set_bool("no_mtg_notification", true)
this:delete()
return true
end
Expand Down
23 changes: 12 additions & 11 deletions builtin/mainmenu/dlg_version_info.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ end
local function version_info_buttonhandler(this, fields)
if fields.version_check_remind then
-- Erase last known, user will be reminded again at next check
core.settings:set("update_last_known", "")
cache_settings:set("update_last_known", "")
this:delete()
return true
end
if fields.version_check_never then
core.settings:set("update_last_checked", "disabled")
-- clear checked URL
core.settings:set("update_information_url", "")
this:delete()
return true
end
Expand Down Expand Up @@ -116,7 +117,7 @@ local function on_version_info_received(json)
return
end

local known_update = tonumber(core.settings:get("update_last_known")) or 0
local known_update = tonumber(cache_settings:get("update_last_known")) or 0

-- Format: MMNNPPP (Major, Minor, Patch)
local new_number = type(json.latest) == "table" and json.latest.version_code
Expand All @@ -135,7 +136,7 @@ local function on_version_info_received(json)
return
end

core.settings:set("update_last_known", tostring(new_number))
cache_settings:set("update_last_known", tostring(new_number))

-- Show version info dialog (once)
maintab:hide()
Expand All @@ -149,20 +150,20 @@ end

function check_new_version()
local url = core.settings:get("update_information_url")
if core.settings:get("update_last_checked") == "disabled" or
url == "" then
if url == "" then
-- Never show any updates
return
end

local time_now = os.time()
local time_checked = tonumber(core.settings:get("update_last_checked")) or 0
if time_now - time_checked < 2 * 24 * 3600 then
-- Check interval of 2 entire days
-- every 2 days
if check_cache_age("update_last_checked", 2 * 24 * 3600) then
return
end
cache_settings:set("update_last_checked", tostring(os.time()))

core.settings:set("update_last_checked", tostring(time_now))
-- Clean old leftovers (this can be removed after 5.9.0 or so)
core.settings:remove("update_last_checked")
core.settings:remove("update_last_known")

core.handle_async(function(params)
local http = core.get_http_api()
Expand Down
2 changes: 0 additions & 2 deletions builtin/mainmenu/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ local basepath = core.get_builtin_path()
defaulttexturedir = core.get_texturepath_share() .. DIR_DELIM .. "base" ..
DIR_DELIM .. "pack" .. DIR_DELIM

dofile(menupath .. DIR_DELIM .. "misc.lua")

dofile(basepath .. "common" .. DIR_DELIM .. "filterlist.lua")
dofile(basepath .. "fstk" .. DIR_DELIM .. "buttonbar.lua")
dofile(basepath .. "fstk" .. DIR_DELIM .. "dialog.lua")
Expand Down
6 changes: 0 additions & 6 deletions builtin/mainmenu/misc.lua

This file was deleted.

70 changes: 41 additions & 29 deletions builtin/mainmenu/serverlistmgr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

serverlistmgr = {
-- continent code we detected for ourselves
my_continent = core.get_once("continent"),
my_continent = nil,

-- list of locally favorites servers
favorites = nil,
Expand All @@ -26,6 +26,15 @@ serverlistmgr = {
servers = nil,
}

do
if check_cache_age("geoip_last_checked", 3600) then
local tmp = cache_settings:get("geoip") or ""
if tmp:match("^[A-Z][A-Z]$") then
serverlistmgr.my_continent = tmp
end
end
end

--------------------------------------------------------------------------------
-- Efficient data structure for normalizing arbitrary scores attached to objects
-- e.g. {{"a", 3.14}, {"b", 3.14}, {"c", 20}, {"d", 0}}
Expand Down Expand Up @@ -112,6 +121,22 @@ local public_downloading = false
local geoip_downloading = false

--------------------------------------------------------------------------------
local function fetch_geoip()
local http = core.get_http_api()
local url = core.settings:get("serverlist_url") .. "/geoip"

local response = http.fetch_sync({ url = url })
if not response.succeeded then
return
end

local retval = core.parse_json(response.data)
if type(retval) ~= "table" then
return
end
return type(retval.continent) == "string" and retval.continent
end

function serverlistmgr.sync()
if not serverlistmgr.servers then
serverlistmgr.servers = {{
Expand All @@ -129,44 +154,31 @@ function serverlistmgr.sync()
return
end

-- only fetched once per MT instance
if not serverlistmgr.my_continent and not geoip_downloading then
geoip_downloading = true
core.handle_async(
function(param)
local http = core.get_http_api()
local url = core.settings:get("serverlist_url") .. "/geoip"

local response = http.fetch_sync({ url = url })
if not response.succeeded then
return
end

local retval = core.parse_json(response.data)
return retval and type(retval.continent) == "string" and retval.continent
end,
nil,
function(result)
geoip_downloading = false
if not result then
return
end
serverlistmgr.my_continent = result
core.set_once("continent", result)
-- reorder list if we already have it
if serverlistmgr.servers then
serverlistmgr.servers = order_server_list(serverlistmgr.servers)
core.event_handler("Refresh")
end
core.handle_async(fetch_geoip, nil, function(result)
geoip_downloading = false
if not result then
return
end
serverlistmgr.my_continent = result
cache_settings:set("geoip", result)
cache_settings:set("geoip_last_checked", tostring(os.time()))

-- re-sort list if applicable
if serverlistmgr.servers then
serverlistmgr.servers = order_server_list(serverlistmgr.servers)
core.event_handler("Refresh")
end
)
end)
end

if public_downloading then
return
end
public_downloading = true

-- note: this isn't cached because it's way too dynamic
core.handle_async(
function(param)
local http = core.get_http_api()
Expand Down
3 changes: 2 additions & 1 deletion builtin/mainmenu/tests/serverlistmgr_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
_G.core = {get_once = function(_) end}
_G.core = {}
_G.unpack = table.unpack
_G.check_cache_age = function() return false end
_G.serverlistmgr = {}

dofile("builtin/common/vector.lua")
Expand Down
15 changes: 1 addition & 14 deletions builtin/settingtypes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ serverlist_url (Serverlist URL) string servers.minetest.net
enable_split_login_register (Enable split login/register) bool true

# URL to JSON file which provides information about the newest Minetest release
# If this is empty the engine will never check for updates.
update_information_url (Update information URL) string https://www.minetest.net/release_info.json

[*Server]
Expand Down Expand Up @@ -2287,20 +2288,6 @@ show_advanced (Show advanced settings) bool false
# Changing this setting requires a restart.
enable_sound (Sound) bool true

# Unix timestamp (integer) of when the client last checked for an update
# Set this value to "disabled" to never check for updates.
update_last_checked (Last update check) string

# Version number which was last seen during an update check.
#
# Representation: MMMIIIPPP, where M=Major, I=Minor, P=Patch
# Ex: 5.5.0 is 005005000
update_last_known (Last known version update) int 0

# If this is set to true, the user will never (again) be shown the
# "reinstall Minetest Game" notification.
no_mtg_notification (Don't show "reinstall Minetest Game" notification) bool false

# Key for moving the player forward.
keymap_forward (Forward key) key KEY_KEY_W

Expand Down
4 changes: 0 additions & 4 deletions doc/menu_lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ Functions
* Android only. Shares file using the share popup
* `core.get_version()` (possible in async calls)
* returns current core version
* `core.set_once(key, value)`:
* save a string value that persists even if menu is closed
* `core.get_once(key)`:
* get a string value saved by above function, or `nil`



Expand Down
5 changes: 2 additions & 3 deletions src/defaultsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,10 @@ void set_default_settings()
settings->setDefault("contentdb_flag_blacklist", "nonfree, desktop_default");
#endif

settings->setDefault("update_information_url", "https://www.minetest.net/release_info.json");
#if ENABLE_UPDATE_CHECKER
settings->setDefault("update_last_checked", "");
settings->setDefault("update_information_url", "https://www.minetest.net/release_info.json");
#else
settings->setDefault("update_last_checked", "disabled");
settings->setDefault("update_information_url", "");
#endif

// Server
Expand Down

0 comments on commit f829d48

Please sign in to comment.