From 4b7a5489a7d590441e76d9f4ebb8d37fcf375fcb Mon Sep 17 00:00:00 2001 From: hoaaiww <75149340+hoaaiww@users.noreply.github.com> Date: Fri, 21 Jul 2023 20:41:07 +0200 Subject: [PATCH] Create update_checker.lua --- update_checker.lua | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 update_checker.lua diff --git a/update_checker.lua b/update_checker.lua new file mode 100644 index 0000000..c8d4dd9 --- /dev/null +++ b/update_checker.lua @@ -0,0 +1,58 @@ +CheckForUpdates = ((GetResourceMetadata(GetCurrentResourceName(), "check_for_updates") == 'yes') and true or false) +curVersion = GetResourceMetadata(GetCurrentResourceName(), "version") +resourceName = GetCurrentResourceName() + +if CheckForUpdates then + CreateThread(function() + if resourceName ~= "hoaaiww_measure_speed" then + resourceName = "hoaaiww_measure_speed (" .. GetCurrentResourceName() .. ")" + end + end) + + CreateThread(function() + while true do + PerformHttpRequest("https://api.github.com/repos/hoaaiww/hoaaiww_measure_speed/releases/latest", CheckVersion, "GET") + Wait(3600000) + end + end) + + CheckVersion = function(err, responseText, headers) + local repoVersion, repoURL, repoBody = GetRepoInformations() + + CreateThread(function() + if curVersion ~= repoVersion then + print("^0[^3WARNING^0] " .. resourceName .. " is ^1NOT ^0up to date!") + print("^0[^3WARNING^0] Your Version: ^2" .. curVersion .. "^0") + print("^0[^3WARNING^0] Latest Version: ^2" .. repoVersion .. "^0") + print("^0[^3WARNING^0] Get the latest Version from: ^2" .. repoURL .. "^0") + print("^0[^3WARNING^0] Changelog:^0") + print("^1" .. repoBody .. "^0") + else + print("^0[^2INFO^0] " .. resourceName .. " is up to date! (^2" .. curVersion .. "^0)") + end + end) + end + + GetRepoInformations = function() + local repoVersion, repoURL, repoBody = nil, nil, nil + + PerformHttpRequest("https://api.github.com/repos/hoaaiww/hoaaiww_measure_speed/releases/latest", function(err, response, headers) + if err == 200 then + local data = json.decode(response) + + repoVersion = data.tag_name + repoURL = data.html_url + repoBody = data.body + else + repoVersion = curVersion + repoURL = "https://github.com/hoaaiww/hoaaiww_measure_speed" + end + end, "GET") + + repeat + Wait(50) + until (repoVersion and repoURL and repoBody) + + return repoVersion, repoURL, repoBody + end +end