-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fea1fb1
commit 7153001
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
newmodels_reborn/optional/update_checker/s_update_checker.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |