dlg_version_info.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. core.settings:set("update_last_known", "")
  47. this:delete()
  48. return true
  49. end
  50. if fields.version_check_never then
  51. core.settings:set("update_last_checked", "disabled")
  52. this:delete()
  53. return true
  54. end
  55. if fields.version_check_visit then
  56. if type(this.data.url) == "string" then
  57. core.open_url(this.data.url)
  58. end
  59. this:delete()
  60. return true
  61. end
  62. return false
  63. end
  64. local function version_info_eventhandler(event)
  65. if event == "DialogShow" then
  66. mm_game_theme.set_engine()
  67. return true
  68. end
  69. return false
  70. end
  71. local function create_version_info_dlg(new_version, url)
  72. assert(type(new_version) == "string")
  73. assert(type(url) == "string")
  74. local retval = dialog_create("version_info",
  75. version_info_formspec,
  76. version_info_buttonhandler,
  77. version_info_eventhandler)
  78. retval.data.new_version = new_version
  79. retval.data.url = url
  80. return retval
  81. end
  82. local function get_current_version_code()
  83. -- Format: Major.Minor.Patch
  84. -- Convert to MMMNNNPPP
  85. local cur_string = core.get_version().string
  86. local cur_major, cur_minor, cur_patch = cur_string:match("^(%d+).(%d+).(%d+)")
  87. if not cur_patch then
  88. core.log("error", "Failed to parse version numbers (invalid tag format?)")
  89. return
  90. end
  91. return (cur_major * 1000 + cur_minor) * 1000 + cur_patch
  92. end
  93. local function on_version_info_received(json)
  94. local maintab = ui.find_by_name("maintab")
  95. if maintab.hidden then
  96. -- Another dialog is open, abort.
  97. return
  98. end
  99. local known_update = tonumber(core.settings:get("update_last_known")) or 0
  100. -- Format: MMNNPPP (Major, Minor, Patch)
  101. local new_number = type(json.latest) == "table" and json.latest.version_code
  102. if type(new_number) ~= "number" then
  103. core.log("error", "Failed to read version number (invalid response?)")
  104. return
  105. end
  106. local cur_number = get_current_version_code()
  107. if new_number <= known_update or new_number < cur_number then
  108. return
  109. end
  110. -- Also consider updating from 1.2.3-dev to 1.2.3
  111. if new_number == cur_number and not core.get_version().is_dev then
  112. return
  113. end
  114. core.settings:set("update_last_known", tostring(new_number))
  115. -- Show version info dialog (once)
  116. maintab:hide()
  117. local version_info_dlg = create_version_info_dlg(json.latest.version, json.latest.url)
  118. version_info_dlg:set_parent(maintab)
  119. version_info_dlg:show()
  120. ui.update()
  121. end
  122. function check_new_version()
  123. local url = core.settings:get("update_information_url")
  124. if core.settings:get("update_last_checked") == "disabled" or
  125. url == "" then
  126. -- Never show any updates
  127. return
  128. end
  129. local time_now = os.time()
  130. local time_checked = tonumber(core.settings:get("update_last_checked")) or 0
  131. if time_now - time_checked < 2 * 24 * 3600 then
  132. -- Check interval of 2 entire days
  133. return
  134. end
  135. core.settings:set("update_last_checked", tostring(time_now))
  136. core.handle_async(function(params)
  137. local http = core.get_http_api()
  138. return http.fetch_sync(params)
  139. end, { url = url }, function(result)
  140. local json = result.succeeded and core.parse_json(result.data)
  141. if type(json) ~= "table" or not json.latest then
  142. core.log("error", "Failed to read JSON output from " .. url ..
  143. ", status code = " .. result.code)
  144. return
  145. end
  146. on_version_info_received(json)
  147. end)
  148. end