Skip to content

Commit

Permalink
add update checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando-A-Rocha committed Aug 1, 2024
1 parent fea1fb1 commit 7153001
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
3 changes: 3 additions & 0 deletions newmodels_reborn/meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<!-- ............................................................................... -->
<!-- OPTIONAL THINGS: .............................................................. -->

<!-- Update Checker (RECOMMENDED, please keep this to be warned of new releases!) -->
<script src="optional/update_checker/s_update_checker.lua" type="server"/>

<!-- Debugging / Testing -->
<script src="optional/debug/c_debug.lua" type="client"/>
<script src="optional/debug/s_debug.lua" type="server"/>
Expand Down
85 changes: 85 additions & 0 deletions newmodels_reborn/optional/update_checker/s_update_checker.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
local function outputInfo(msg, isOK)
outputServerLog("[" .. getResourceName(resource) .. "] " .. msg)
if not isOK then
outputDebugString(msg, 2)
end
end

local function fetchLatestCallback(data, info)
if not (data and info and info.success == true) then
outputInfo("Could not get data from GitHub")
return
end

data = fromJSON(data)
if not data then
outputInfo("Could not parse data from GitHub")
return
end

-- fetch version from data
local version = data.tag_name
if not version then
outputInfo("Could not get version from GitHub data")
outputInfo(inspect(data))
return
end

-- compare versions
local currentVersion = getResourceInfo(resource, "version")
if not currentVersion then
outputInfo("Could not get resource version")
return
end

local foundEqual = false
local tryStrings = { currentVersion, "v" .. currentVersion } -- check for v prefix
for _, str in pairs(tryStrings) do
if str == version then
foundEqual = true
break
end
end

if foundEqual then
outputInfo("You are running the latest version: " .. currentVersion, true)
return
end

-- check if version is superior than currentVersion
local currentVersionParts = split(currentVersion, ".")
local currentVersionPartsCount = #currentVersionParts
local versionParts = split(version, ".")
local versionPartsCount = #versionParts

versionParts[1] = versionParts[1]:gsub("v", "")

if currentVersionPartsCount > versionPartsCount then
versionPartsCount = currentVersionPartsCount
end

-- check if version is inferior than currentVersion
local inferior = true
for i = 1, versionPartsCount do
if tonumber(currentVersionParts[i]) > tonumber(versionParts[i]) then
inferior = false
break
end
end

if inferior then
outputInfo("You are running an inferior version of this resource (" .. currentVersion .. ")")
outputInfo("Get the latest from: " .. data.html_url)
return
end

outputInfo("You are running an unknown version of this resource (" .. currentVersion .. ")")
end

addEventHandler("onResourceStart", resourceRoot, function()
fetchRemote("https://api.github.com/repos/Fernando-A-Rocha/mta-add-models/releases/latest", {
queueName = "newmodels_updater",
connectionAttempts = 3,
connectTimeout = 5000,
}, fetchLatestCallback)
end, false)

0 comments on commit 7153001

Please sign in to comment.