Update VersionChecker.lua
This commit is contained in:
1 parent
69703e803f
commit
f42f853c81
1 file changed
+114
-59
@@ -11,98 +11,153 @@ local function padVersion(v)
|
|||||||
return parts
|
return parts
|
||||||
end
|
end
|
||||||
|
|
||||||
function CompareVersions(v1, v2)
|
local function compareVersions(v1, v2)
|
||||||
local a, b = padVersion(v1), padVersion(v2)
|
local a, b = padVersion(v1), padVersion(v2)
|
||||||
for i = 1, 3 do
|
for i = 1, 3 do
|
||||||
if a[i] < b[i] then return -1
|
if a[i] < b[i] then
|
||||||
elseif a[i] > b[i] then return 1
|
return -1
|
||||||
|
elseif a[i] > b[i] then
|
||||||
|
return 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
local function fetchPatchNotes(branch, resource, username, reponame, localVersion, tebex, actualRepoPath)
|
local function parseVersion(versionString)
|
||||||
local rawUrl = ('https://raw.githubusercontent.com/%s/%s/%s/resources.json'):format(username, reponame, branch)
|
return versionString:match('%d+%.%d+%.%d+') or versionString
|
||||||
PerformHttpRequest(rawUrl, function(noteStatus, noteBody)
|
end
|
||||||
|
|
||||||
|
local function buildGithubApiUrl(repoPath)
|
||||||
|
local username, reponame = repoPath:match("([^/]+)/([^/]+)")
|
||||||
|
return ('https://api.github.com/repos/%s/%s/releases/latest'):format(username, reponame)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function buildPatchNotesUrl(branch, repoPath)
|
||||||
|
local username, reponame = repoPath:match("([^/]+)/([^/]+)")
|
||||||
|
return ('https://raw.githubusercontent.com/%s/%s/%s/resources.json'):format(username, reponame, branch)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function getDownloadUrl(repoPath, tebex, actualRepoPath)
|
||||||
|
if tebex then
|
||||||
|
return "https://portal.cfx.re/assets/granted-assets"
|
||||||
|
elseif actualRepoPath and actualRepoPath ~= false then
|
||||||
|
local repoUser, repoName = actualRepoPath:match("([^/]+)/([^/]+)")
|
||||||
|
if repoUser and repoName then
|
||||||
|
return ('https://github.com/%s/%s/releases/latest'):format(repoUser, repoName)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local function printUpdateAvailable(resource, localVersion, downloadUrl)
|
||||||
|
print(('^1An update is available for %s (current version: %s)\r\n - ^5Please download the latest version from %s^7'):format(resource, localVersion, downloadUrl))
|
||||||
|
end
|
||||||
|
|
||||||
|
local function printPatchNotes(notesToPrint)
|
||||||
|
print('^3Patch Notes:^7')
|
||||||
|
for _, entry in ipairs(notesToPrint) do
|
||||||
|
print(('^2Version %s:^7'):format(entry.version))
|
||||||
|
for _, line in ipairs(entry.notes) do
|
||||||
|
print((' - %s'):format(line))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function printError(message)
|
||||||
|
print(('^1[ERROR]^7 %s'):format(message))
|
||||||
|
end
|
||||||
|
|
||||||
|
local function processVersions(allVersions, localVersion)
|
||||||
|
local notesToPrint = {}
|
||||||
|
|
||||||
|
for versionStr, notes in pairs(allVersions) do
|
||||||
|
if compareVersions(versionStr, localVersion) > 0 and type(notes) == "table" then
|
||||||
|
table.insert(notesToPrint, { version = versionStr, notes = notes })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
table.sort(notesToPrint, function(a, b)
|
||||||
|
return compareVersions(a.version, b.version) < 0
|
||||||
|
end)
|
||||||
|
|
||||||
|
return notesToPrint
|
||||||
|
end
|
||||||
|
|
||||||
|
local function fetchPatchNotes(branch, resource, repoPath, localVersion, tebex, actualRepoPath)
|
||||||
|
local url = buildPatchNotesUrl(branch, repoPath)
|
||||||
|
|
||||||
|
PerformHttpRequest(url, function(noteStatus, noteBody)
|
||||||
if noteStatus == 200 then
|
if noteStatus == 200 then
|
||||||
local patchData = json.decode(noteBody)
|
local patchData = json.decode(noteBody)
|
||||||
if not patchData then return end
|
if not patchData then return end
|
||||||
|
|
||||||
local allVersions = patchData[resource]
|
local allVersions = patchData[resource]
|
||||||
|
|
||||||
if allVersions and type(allVersions) == "table" then
|
if allVersions and type(allVersions) == "table" then
|
||||||
local notesToPrint = {}
|
local notesToPrint = processVersions(allVersions, localVersion)
|
||||||
|
|
||||||
for versionStr, notes in pairs(allVersions) do
|
|
||||||
if CompareVersions(versionStr, localVersion) > 0 and type(notes) == "table" then
|
|
||||||
table.insert(notesToPrint, {version = versionStr, notes = notes})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
table.sort(notesToPrint, function(a, b)
|
|
||||||
return CompareVersions(a.version, b.version) < 0
|
|
||||||
end)
|
|
||||||
|
|
||||||
if #notesToPrint > 0 then
|
if #notesToPrint > 0 then
|
||||||
if tebex then
|
local downloadUrl = getDownloadUrl(repoPath, tebex, actualRepoPath) or url
|
||||||
rawUrl = "https://portal.cfx.re/assets/granted-assets"
|
printUpdateAvailable(resource, localVersion, downloadUrl)
|
||||||
elseif actualRepoPath then
|
printPatchNotes(notesToPrint)
|
||||||
local repoUser, repoName = actualRepoPath:match("([^/]+)/([^/]+)")
|
|
||||||
if repoUser and repoName then
|
|
||||||
rawUrl = ('https://github.com/%s/%s/releases/latest'):format(repoUser, repoName)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
print(('^1An update is available for %s (current version: %s)\r\n - ^5Please download the latest version from %s^7'):format(reponame, localVersion, rawUrl))
|
|
||||||
print('^3Patch Notes:^7')
|
|
||||||
for _, entry in ipairs(notesToPrint) do
|
|
||||||
print(('^2Version %s:^7'):format(entry.version))
|
|
||||||
for _, line in ipairs(entry.notes) do
|
|
||||||
print((' - %s'):format(line))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
elseif branch == "main" then
|
elseif branch == "main" then
|
||||||
fetchPatchNotes("master", resource, username, reponame, localVersion, tebex)
|
fetchPatchNotes("master", resource, repoPath, localVersion, tebex, actualRepoPath)
|
||||||
end
|
end
|
||||||
end, 'GET')
|
end, 'GET')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function checkSimpleUpdate(repoPath, resource, localVersion, isTebex)
|
||||||
|
local url = buildGithubApiUrl(repoPath)
|
||||||
|
|
||||||
|
PerformHttpRequest(url, function(status, response)
|
||||||
|
if status ~= 200 then return end
|
||||||
|
|
||||||
|
local latest = parseVersion(json.decode(response).tag_name)
|
||||||
|
if not latest or latest == localVersion or localVersion:gsub('%D', '') >= latest:gsub('%D', '') then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local downloadUrl = isTebex and "https://portal.cfx.re/assets/granted-assets" or json.decode(response).html_url
|
||||||
|
printUpdateAvailable(resource, localVersion, downloadUrl)
|
||||||
|
end, 'GET')
|
||||||
|
end
|
||||||
|
|
||||||
|
local function validateRepoPath(repoPath)
|
||||||
|
local username, reponame = repoPath:match("([^/]+)/([^/]+)")
|
||||||
|
if not username or not reponame then
|
||||||
|
return false, 'Invalid repository format. Expected format: "username/reponame"'
|
||||||
|
end
|
||||||
|
return true, username, reponame
|
||||||
|
end
|
||||||
|
|
||||||
|
local function getLocalVersion(resource)
|
||||||
|
local localVersionRaw = GetResourceMetadata(resource, "version", 0)
|
||||||
|
if not localVersionRaw then
|
||||||
|
return nil, ('No version metadata found for resource "%s". Ensure fxmanifest.lua contains a valid version string.'):format(resource)
|
||||||
|
end
|
||||||
|
return parseVersion(localVersionRaw)
|
||||||
|
end
|
||||||
|
|
||||||
--- Checks for updates to a resource by comparing the local version (from fxmanifest.lua) with the latest release on GitHub, and optionally prints patch notes if using the template.
|
--- Checks for updates to a resource by comparing the local version (from fxmanifest.lua) with the latest release on GitHub, and optionally prints patch notes if using the template.
|
||||||
--- Example - Version.VersionChecker("TheOrderFivem/patchnotes", true, true, "community_bridge", "TheOrderFivem/community_bridge")
|
--- Example - Version.VersionChecker("TheOrderFivem/patchnotes", true, true, "community_bridge", "TheOrderFivem/community_bridge")
|
||||||
---@param repoPath string
|
---@param repoPath string
|
||||||
---@param tebex boolean
|
---@param isTebex boolean
|
||||||
---@param showPatchNotes boolean
|
---@param showPatchNotes boolean
|
||||||
---@param actualResourceName string
|
---@param actualResourceName string
|
||||||
---@param actualRepoPathIfGithub string this is the githubAccount/repoName if the resource is hosted on github, otherwise it will use the repoPath initially passed, this is not needed for tebex = true
|
---@param actualRepoPathIfGithub string this is the githubAccount/repoName if the resource is hosted on github, otherwise it will use the repoPath initially passed, this is not needed for tebex = true
|
||||||
---@return nil
|
---@return nil
|
||||||
function Version.VersionChecker(repoPath, tebex, showPatchNotes, actualResourceName, actualRepoPathIfGithub)
|
function Version.VersionChecker(repoPath, isTebex, showPatchNotes, actualResourceName, actualRepoPathIfGithub)
|
||||||
local username, reponame = repoPath:match("([^/]+)/([^/]+)")
|
local isValid, username, reponame = validateRepoPath(repoPath)
|
||||||
if not username or not reponame then
|
if not isValid then return printError(username) end
|
||||||
return Prints.Error('^1Invalid repository format. Expected format: "username/reponame"^0')
|
|
||||||
end
|
|
||||||
|
|
||||||
local resource = actualResourceName or reponame
|
local resource = actualResourceName or reponame
|
||||||
local localVersionRaw = GetResourceMetadata(resource, "version", 0)
|
local localVersion, errorMsg = getLocalVersion(resource)
|
||||||
if not localVersionRaw then
|
if not localVersion then return printError(errorMsg) end
|
||||||
return print(('^1[ERROR]^7 No version metadata found for resource "%s". Ensure fxmanifest.lua contains a valid version string.'):format(resource))
|
|
||||||
end
|
|
||||||
local localVersion = localVersionRaw:match('%d+%.%d+%.%d+') or localVersionRaw
|
|
||||||
|
|
||||||
if not showPatchNotes then
|
if not showPatchNotes then return checkSimpleUpdate(repoPath, resource, localVersion, isTebex) end
|
||||||
PerformHttpRequest(('https://api.github.com/repos/%s/%s/releases/latest'):format(username, reponame), function(status, response)
|
|
||||||
if status ~= 200 then return end
|
|
||||||
local latest = json.decode(response).tag_name:match('%d+%.%d+%.%d+')
|
|
||||||
if not latest or latest == localVersion or localVersion:gsub('%D', '') >= latest:gsub('%D', '') then return end
|
|
||||||
|
|
||||||
local url = tebex and "https://portal.cfx.re/assets/granted-assets" or json.decode(response).html_url
|
fetchPatchNotes("main", resource, repoPath, localVersion, isTebex, actualRepoPathIfGithub)
|
||||||
print(('^1An update is available for %s (current version: %s)\r\n - ^5Please download the latest version from %s^7'):format(reponame, localVersion, url))
|
|
||||||
end, 'GET')
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
fetchPatchNotes("main", resource, username, reponame, localVersion, tebex, actualRepoPathIfGithub)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
AddEventHandler('onResourceStart', function(resourceName)
|
AddEventHandler('onResourceStart', function(resourceName)
|
||||||
|
|||||||
Reference in new issue
Block a user