From 1b294c976ac34befa30dcc5febc5cd3c776ed5b7 Mon Sep 17 00:00:00 2001 From: Kiminaze Date: Fri, 26 Jul 2024 03:29:32 +0200 Subject: [PATCH] Added version checker --- fxmanifest.lua | 6 ++++- server/versionChecker.lua | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 server/versionChecker.lua diff --git a/fxmanifest.lua b/fxmanifest.lua index eed750b..f3217a6 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -4,11 +4,15 @@ games { "gta5" } author "Philipp Decker" description "Allows toggling an orbit camera around a given point or entity." -version "1.1.0" +version "1.2.0" lua54 "yes" use_experimental_fxv2_oal "yes" +server_scripts { + "server/versionChecker.lua" +} + client_scripts { "config.lua", diff --git a/server/versionChecker.lua b/server/versionChecker.lua new file mode 100644 index 0000000..49ced92 --- /dev/null +++ b/server/versionChecker.lua @@ -0,0 +1,50 @@ + +local AUTHOR = "Kiminaze" +local RESOURCE_NAME = "OrbitCam" +local GITHUB_URL = "https://api.github.com/repos/%s/%s/releases/latest" + +local RENAME_WARNING = "^3[WARNING] This resource should not be renamed. This can and will lead to errors in the long run.^0" +local CHECK_FAILED = "^3[WARNING] Checking for latest version failed! Http Error: %s^0" +local NEW_VERSION = "^5[INFO] There is a new version available! Latest version: %s - Your version: %s\nDirect download: %s\nLatest patch notes:\n%s^0" + +CreateThread(function() + if (RESOURCE_NAME ~= GetCurrentResourceName()) then + print(RENAME_WARNING) + end + + PerformHttpRequest(GITHUB_URL:format(AUTHOR, RESOURCE_NAME), CheckVersionCallback, "GET") +end) + +local function SplitVersionNumber(version) + local nums = {} + + for num in version:gmatch("([^.]+)") do + nums[#nums + 1] = tonumber(num) + end + + return nums +end + +function CheckVersionCallback(status, response, headers) + if (status ~= 200) then + print(CHECK_FAILED:format(status)) + return + end + + local latestRelease = json.decode(response) + + local latestVersion = latestRelease.tag_name:gsub("v", "") + local currentVersion = GetResourceMetadata(GetCurrentResourceName(), "version", 0) + + if (latestVersion == currentVersion) then return end + + local current = SplitVersionNumber(currentVersion) + local latest = SplitVersionNumber(latestVersion) + + for i = 1, #current do + if (current[i] < latest[i]) then + print(NEW_VERSION:format(latestVersion, currentVersion, latestRelease.assets[1].browser_download_url, latestRelease.body)) + break + end + end +end