init.lua 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. local enabled = minetest.settings:get_bool("update_checker_enabled", false)
  2. if not enabled
  3. then
  4. return
  5. end
  6. local S = minetest.get_translator("update_checker")
  7. local modpath = minetest.get_modpath("update_checker")
  8. --read game version from same file that's checked in the remote repo
  9. local GAME_VERSION
  10. do
  11. local file = io.open(modpath.."/game_version.txt")
  12. GAME_VERSION = file:read().."\n"
  13. file:close()
  14. end
  15. --(modpath.."/game_version.txt"), but in origin/master
  16. local URL = "https://notabug.org/NetherEran/The_End/raw/master/mods/misc/update_checker/game_version.txt"
  17. --this is called after the remote repo has been queried. If successful
  18. --and the version string read above is different from the result of the
  19. --query it displays a message to the player
  20. local function on_http_request_arrived(result)
  21. if result.succeeded and result.data ~= GAME_VERSION
  22. then
  23. local res = string.sub(result.data, 1, string.len(result.data) - 1)
  24. minetest.after(2,
  25. function()
  26. minetest.chat_send_all(S("[Update available]\n" ..
  27. "Version @1 of The End is available at " ..
  28. "https://notabug.org/NetherEran/The_End", res))
  29. end)
  30. end
  31. end
  32. local http_api = minetest.request_http_api()
  33. --http_api is nil if this mod is not listed as trusted with http
  34. --requests in minetest.conf
  35. if http_api
  36. then
  37. --query the remote repository
  38. local httprequest =
  39. {
  40. url = URL,
  41. timeout = 3,
  42. }
  43. http_api.fetch(httprequest, on_http_request_arrived)
  44. end