Skip to content

Commit

Permalink
Added version checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiminaze committed Jul 26, 2024
1 parent 097f226 commit 1b294c9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
6 changes: 5 additions & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Expand Down
50 changes: 50 additions & 0 deletions server/versionChecker.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

local AUTHOR <const> = "Kiminaze"
local RESOURCE_NAME <const> = "OrbitCam"
local GITHUB_URL <const> = "https://api.github.com/repos/%s/%s/releases/latest"

local RENAME_WARNING <const> = "^3[WARNING] This resource should not be renamed. This can and will lead to errors in the long run.^0"
local CHECK_FAILED <const> = "^3[WARNING] Checking for latest version failed! Http Error: %s^0"
local NEW_VERSION <const> = "^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

0 comments on commit 1b294c9

Please sign in to comment.