From ac58fe4bb71bc96ec374ef7241d9f14f1d2a5a1d Mon Sep 17 00:00:00 2001 From: Jim Shield Date: Tue, 22 Apr 2025 19:17:03 +0100 Subject: [PATCH] Fix and enhance update checker --- _versioncheck.lua | 47 +++++++++++++++++++++ fxmanifest.lua | 4 ++ shared/_scriptversioncheck.lua | 76 ++++++++++++++++++++++++++++++++++ shared/_versioncheck.lua | 47 --------------------- 4 files changed, 127 insertions(+), 47 deletions(-) create mode 100644 _versioncheck.lua create mode 100644 shared/_scriptversioncheck.lua delete mode 100644 shared/_versioncheck.lua diff --git a/_versioncheck.lua b/_versioncheck.lua new file mode 100644 index 0000000..0a17ebc --- /dev/null +++ b/_versioncheck.lua @@ -0,0 +1,47 @@ +function parseVersion(version) + local parts = {} + for num in version:gmatch("%d+") do + table.insert(parts, tonumber(num)) + end + return parts +end + +function compareVersions(current, newest) + local currentParts = parseVersion(current) + local newestParts = parseVersion(newest) + for i = 1, math.max(#currentParts, #newestParts) do + local c = currentParts[i] or 0 + local n = newestParts[i] or 0 + if c < n then return -1 + elseif c > n then return 1 end + end + return 0 -- equal +end + +function CheckBridgeVersion() + if IsDuplicityVersion() then + CreateThread(function() + Wait(4000) + local currentVersionRaw = GetResourceMetadata("jim_bridge", 'version') + PerformHttpRequest('https://raw.githubusercontent.com/jimathy/jim_bridge/master/version.txt', function(err, newestVersionRaw, headers) + if not newestVersionRaw then + print("^1Unable to run version check for ^7'^3jim_bridge^7' (^3"..currentVersionRaw.."^7)") + return + end + + newestVersionRaw = newestVersionRaw:match("[^\r\n]+") + local compareResult = compareVersions(currentVersionRaw, newestVersionRaw) + + if compareResult == 0 then + print("^7'^3jim_bridge^7' - ^2You are running the latest version^7. ^7(^3"..currentVersionRaw.."^7)") + elseif compareResult < 0 then + print("^7'^3jim_bridge^7' - ^1You are running an outdated version^7! ^7(^3"..currentVersionRaw.."^7 → ^3"..newestVersionRaw.."^7)") + else + print("^7'^3jim_bridge^7' - ^5You are running a newer version ^7(^3"..currentVersionRaw.."^7 ← ^3"..newestVersionRaw.."^7)") + end + end) + end) + end +end + +CheckBridgeVersion() \ No newline at end of file diff --git a/fxmanifest.lua b/fxmanifest.lua index 1da37c7..11e9e1a 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -7,6 +7,7 @@ rdr3_warning 'I acknowledge that this is a prerelease build of RedM, and I am aw games { 'gta5', 'rdr3' } lua54 'yes' + files { 'starter.lua', 'shared/*.lua', @@ -14,6 +15,9 @@ files { 'shared/scaleforms/*.lua', } +-- Version checker +server_scripts { '_versioncheck.lua' } + -- NUI Menu Loading client_scripts { 'nui/*.lua' } ui_page 'nui/index.html' diff --git a/shared/_scriptversioncheck.lua b/shared/_scriptversioncheck.lua new file mode 100644 index 0000000..5105767 --- /dev/null +++ b/shared/_scriptversioncheck.lua @@ -0,0 +1,76 @@ +function parseVersion(version) + local parts = {} + for num in version:gmatch("%d+") do + table.insert(parts, tonumber(num)) + end + return parts +end + +function compareVersions(current, newest) + local currentParts = parseVersion(current) + local newestParts = parseVersion(newest) + for i = 1, math.max(#currentParts, #newestParts) do + local c = currentParts[i] or 0 + local n = newestParts[i] or 0 + if c < n then return -1 + elseif c > n then return 1 end + end + return 0 -- equal +end + +function capitalize(str) + return (str:gsub("^%l", string.upper):gsub("%-%l", function(letter) return "-" .. string.upper(letter:sub(2)) end)) +end + +local scriptName = ("^2"..capitalize(getScript()):gsub("%-", "^7-^2"):gsub("%_", "^7_^2")) or "" +local scriptVersion = ("^5"..GetResourceMetadata(getScript(), 'version', nil):gsub("%.", "^7.^5")) or "" +local scriptDescription = ("^2"..GetResourceMetadata(getScript(), 'description', nil)) or "" +local scriptAuthor = ("^2by ^4"..GetResourceMetadata(getScript(), 'author', nil):gsub("%and", "^7and^4")) or "" + +print(scriptName.." ^7v"..scriptVersion.."^7 - "..scriptDescription.." "..scriptAuthor.."^7") + +function CheckVersion() + if isServer() then + CreateThread(function() + Wait(4000) + + local script = getScript() + local currentVersionRaw = GetResourceMetadata(script, 'version') + + PerformHttpRequest('https://raw.githubusercontent.com/jimathy/UpdateVersions/master/'..script..'.txt', function(err, newestVersionRaw, headers) + if not newestVersionRaw then + PerformHttpRequest('https://raw.githubusercontent.com/jimathy/'..script..'/master/version.txt', function(err, fallbackVersionRaw, headers) + if not fallbackVersionRaw then + print("^1Currently unable to run a version check for ^7'^3"..script.."^7' (^3"..currentVersionRaw.."^7)") + return + end + + fallbackVersionRaw = fallbackVersionRaw:match("[^\r\n]+"):gsub("v", "") + + local compareResult = compareVersions(currentVersionRaw, fallbackVersionRaw) + if compareResult == 0 then + print("^7'^3"..script.."^7' - ^2You are running the latest version^7. (^3"..currentVersionRaw.."^7)") + elseif compareResult < 0 then + print("^7'^3"..script.."^7' - ^1You are currently running an outdated version^7! (^3"..currentVersionRaw.."^7 → ^3"..fallbackVersionRaw.."^7)") + else + print("^7'^3"..script.."^7' - ^5You are running a newer version ^7(^3"..currentVersionRaw.."^7 ← ^3"..fallbackVersionRaw.."^7)") + end + end) + else + newestVersionRaw = newestVersionRaw:match("[^\r\n]+"):gsub("v", "") + + local compareResult = compareVersions(currentVersionRaw, newestVersionRaw) + if compareResult == 0 then + print("^7'^3"..script.."^7' - ^2You are running the latest version^7. (^3"..currentVersionRaw.."^7)") + elseif compareResult < 0 then + print("^7'^3"..script.."^7' - ^1You are currently running an outdated version^7! (^3"..currentVersionRaw.."^7 → ^3"..newestVersionRaw.."^7)") + else + print("^7'^3"..script.."^7' - ^5You are running a newer version ^7(^3"..currentVersionRaw.."^7 ← ^3"..newestVersionRaw.."^7)") + end + end + end) + end) + end +end + +CheckVersion() \ No newline at end of file diff --git a/shared/_versioncheck.lua b/shared/_versioncheck.lua deleted file mode 100644 index 2b2dbde..0000000 --- a/shared/_versioncheck.lua +++ /dev/null @@ -1,47 +0,0 @@ --- Version check for jim_bridge -- -function CheckBridgeVersion() - if isServer() then - local currentVersion = "^3"..GetResourceMetadata("jim_bridge", 'version'):gsub("%.", "^7.^3").."^7" - PerformHttpRequest('https://raw.githubusercontent.com/jimathy/jim_bridge/master/version.txt', function(err, newestVersion, headers) - if not newestVersion then print("^1Currently unable to run a version check for ^7'^3jim_bridge^7' ("..currentVersion.."^7)") return end - newestVersion = "^3"..newestVersion:sub(1, -2):gsub("%.", "^7.^3"):gsub("%\r", "").."^7" - print(newestVersion == currentVersion and "^7'^3jim_bridge^7' - ^6You are running the latest version.^7 ("..currentVersion..")" or "^7'^3jim_bridge^7' - ^1You are currently running an outdated version^7, ^1please update^7!") - end) - end -end ---CheckBridgeVersion() - --- Print Script names -function capitalize(str) - return (str:gsub("^%l", string.upper):gsub("%-%l", function(letter) return "-" .. string.upper(letter:sub(2)) end)) -end - -local scriptName = ("^2"..capitalize(getScript()):gsub("%-", "^7-^2"):gsub("%_", "^7_^2")) or "" -local scriptVersion = ("^5"..GetResourceMetadata(getScript(), 'version', nil):gsub("%.", "^7.^5")) or "" -local scriptDescription = ("^2"..GetResourceMetadata(getScript(), 'description', nil)) or "" -local scriptAuthor = ("^2by ^4"..GetResourceMetadata(getScript(), 'author', nil):gsub("%and", "^7and^4")) or "" - -print(scriptName.." ^7v"..scriptVersion.."^7 - "..scriptDescription.." "..scriptAuthor.."^7") - --- Loaded script Version Check, requires CheckVersion() to be placed in a server file -function CheckVersion() - if isServer() then - local currentVersion = "^3"..GetResourceMetadata(getScript(), 'version'):gsub("%.", "^7.^3").."^7" - PerformHttpRequest('https://raw.githubusercontent.com/jimathy/UpdateVersions/master/'..getScript()..'.txt', function(err, newestVersion, headers) - if not newestVersion then - PerformHttpRequest('https://raw.githubusercontent.com/jimathy/'..getScript()..'/master/version.txt', function(err, freeVersion, headers) - if not freeVersion then print("^1Currently unable to run a version check for ^7'^3"..getScript().."^7' ("..currentVersion.."^7)") return end - local currentVersion = "^3"..GetResourceMetadata(getScript(), 'version'):gsub("%.", "^7.^3").."^7" - freeVersion = "^3"..freeVersion:sub(1, -2):gsub("%.", "^7.^3"):gsub("%\r", "").."^7" - print("^6Version Check^7: ^2Running^7: "..currentVersion.." ^2Latest^7: "..freeVersion) - print(freeVersion == currentVersion and "^7'^3"..getScript().."^7' - ^6You are running the latest version.^7 ("..currentVersion..")" or "^7'^3"..getScript().."^7' - ^1You are currently running an outdated version^7, ^1please update^7!") - end) - else - newestVersion = "^3"..newestVersion:sub(1, -2):gsub("%.", "^7.^3"):gsub("%\r", "").."^7" - print("^6Version Check^7: ^2Running^7: "..currentVersion.." ^2Latest^7: "..newestVersion) - print(newestVersion == currentVersion and '^6You are running the latest version.^7 ('..currentVersion..')' or "^1You are currently running an outdated version^7, ^1please update^7!") - end - end) - end -end ---CheckVersion() \ No newline at end of file