Skip to content

Commit

Permalink
feat: Added version check
Browse files Browse the repository at this point in the history
  • Loading branch information
ardelan869 committed Jun 17, 2024
1 parent c8c6602 commit 9c4d1cf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ fx_version "cerulean"
game "gta5"
lua54 "yes"

version "1.0.0"

shared_scripts {
"lib/*.lua",
"config.lua"
Expand All @@ -14,5 +16,6 @@ client_scripts {

server_scripts {
"bridge/server/*.lua",
"server/version.lua",
"server/main.lua"
}
44 changes: 44 additions & 0 deletions server/version.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local UPDATE_RESOURCE = '^3A new update is avaible for %s. %s';
local BASE_URL <const> = 'https://api.github.com/repos/monolith-vision/%s/releases/latest';
local RESOURCE_NAME <const> = GetCurrentResourceName();
local RELEASE_URL <const> = BASE_URL:format(RESOURCE_NAME);
local CURRENT_VERSION <const> = GetResourceMetadata(RESOURCE_NAME, 'version', 0);

if not CURRENT_VERSION then
return print('^1Could not find the version for ' .. RESOURCE_NAME .. '!^0');
end

---@param str string
---@return number[]
local function getVersionNumbers(str)
return { string.strsplit('.', str:match('%d+%.%d+%.%d+')) };
end

PerformHttpRequest(RELEASE_URL, function(status, body)
if status ~= 200 or not body then
return print('^1An error occured, while checking the version. You can ignore this error message.^0');
end

local release = json.decode(body);

if release.prelease then
return print('^3This is a prelease, expect errors to occur.^0');
end

local LATEST_VERSION <const> = release.tag_name;
local LATEST_VERSION_NUMBERS <const> = getVersionNumbers(LATEST_VERSION);
local CURRENT_VERSION_NUMBERS <const> = getVersionNumbers(CURRENT_VERSION);

for index, current in next, CURRENT_VERSION_NUMBERS do
local currentVersionNumber = tonumber(current);
local latestVersionNumber = tonumber(LATEST_VERSION_NUMBERS[index]);

if currentVersionNumber > latestVersionNumber then
return;
end

if currentVersionNumber < latestVersionNumber then
return print(UPDATE_RESOURCE:format(RESOURCE_NAME, release.html_url));
end
end
end);

0 comments on commit 9c4d1cf

Please sign in to comment.