dlg_version_info.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. --[[
  2. Minetest
  3. Copyright (C) 2018-2020 SmallJoker, 2022 rubenwardy
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. ]]
  16. if not core.get_http_api then
  17. function check_new_version()
  18. end
  19. return
  20. end
  21. local function version_info_formspec(data)
  22. local cur_ver = core.get_version()
  23. local title = fgettext("A new $1 version is available", cur_ver.project)
  24. local message =
  25. fgettext("Installed version: $1\nNew version: $2\n" ..
  26. "Visit $3 to find out how to get the newest version and stay up to date" ..
  27. " with features and bugfixes.",
  28. cur_ver.string, data.new_version or "", data.url or "")
  29. local fs = {
  30. "formspec_version[3]",
  31. "size[12.8,7]",
  32. "style_type[label;textcolor=#0E0]",
  33. "label[0.5,0.8;", title, "]",
  34. "textarea[0.4,1.6;12,3.4;;;", message, "]",
  35. "container[0.4,5.8]",
  36. "button[0.0,0;4.0,0.8;version_check_visit;", fgettext("Visit website"), "]",
  37. "button[4.5,0;3.5,0.8;version_check_remind;", fgettext("Later"), "]",
  38. "button[8.5.5,0;3.5,0.8;version_check_never;", fgettext("Never"), "]",
  39. "container_end[]",
  40. }
  41. return table.concat(fs, "")
  42. end
  43. local function version_info_buttonhandler(this, fields)
  44. if fields.version_check_remind then
  45. -- Erase last known, user will be reminded again at next check
  46. cache_settings:set("update_last_known", "")
  47. this:delete()
  48. return true
  49. end
  50. if fields.version_check_never then
  51. -- clear checked URL
  52. core.settings:set("update_information_url", "")
  53. this:delete()
  54. return true
  55. end
  56. if fields.version_check_visit then
  57. if type(this.data.url) == "string" then
  58. core.open_url(this.data.url)
  59. end
  60. this:delete()
  61. return true
  62. end
  63. return false
  64. end
  65. local function version_info_eventhandler(event)
  66. if event == "DialogShow" then
  67. mm_game_theme.set_engine()
  68. return true
  69. end
  70. return false
  71. end
  72. local function create_version_info_dlg(new_version, url)
  73. assert(type(new_version) == "string")
  74. assert(type(url) == "string")
  75. local retval = dialog_create("version_info",
  76. version_info_formspec,
  77. version_info_buttonhandler,
  78. version_info_eventhandler)
  79. retval.data.new_version = new_version
  80. retval.data.url = url
  81. return retval
  82. end
  83. local function get_current_version_code()
  84. -- Format: Major.Minor.Patch
  85. -- Convert to MMMNNNPPP
  86. local cur_string = core.get_version().string
  87. local cur_major, cur_minor, cur_patch = cur_string:match("^(%d+).(%d+).(%d+)")
  88. if not cur_patch then
  89. core.log("error", "Failed to parse version numbers (invalid tag format?)")
  90. return
  91. end
  92. return (cur_major * 1000 + cur_minor) * 1000 + cur_patch
  93. end
  94. local function on_version_info_received(json)
  95. local maintab = ui.find_by_name("maintab")
  96. if maintab.hidden then
  97. -- Another dialog is open, abort.
  98. return
  99. end
  100. local known_update = tonumber(cache_settings:get("update_last_known")) or 0
  101. -- Format: MMNNPPP (Major, Minor, Patch)
  102. local new_number = type(json.latest) == "table" and json.latest.version_code
  103. if type(new_number) ~= "number" then
  104. core.log("error", "Failed to read version number (invalid response?)")
  105. return
  106. end
  107. local cur_number = get_current_version_code()
  108. if new_number <= known_update or new_number < cur_number then
  109. return
  110. end
  111. -- Also consider updating from 1.2.3-dev to 1.2.3
  112. if new_number == cur_number and not core.get_version().is_dev then
  113. return
  114. end
  115. cache_settings:set("update_last_known", tostring(new_number))
  116. -- Show version info dialog (once)
  117. maintab:hide()
  118. local version_info_dlg = create_version_info_dlg(json.latest.version, json.latest.url)
  119. version_info_dlg:set_parent(maintab)
  120. version_info_dlg:show()
  121. ui.update()
  122. end
  123. function check_new_version()
  124. local url = core.settings:get("update_information_url")
  125. if url == "" then
  126. -- Never show any updates
  127. return
  128. end
  129. -- every 2 days
  130. if check_cache_age("update_last_checked", 2 * 24 * 3600) then
  131. return
  132. end
  133. cache_settings:set("update_last_checked", tostring(os.time()))
  134. -- Clean old leftovers (this can be removed after 5.9.0 or so)
  135. core.settings:remove("update_last_checked")
  136. core.settings:remove("update_last_known")
  137. core.handle_async(function(params)
  138. local http = core.get_http_api()
  139. return http.fetch_sync(params)
  140. end, { url = url }, function(result)
  141. local json = result.succeeded and core.parse_json(result.data)
  142. if type(json) ~= "table" or not json.latest then
  143. core.log("error", "Failed to read JSON output from " .. url ..
  144. ", status code = " .. result.code)
  145. return
  146. end
  147. on_version_info_received(json)
  148. end)
  149. end