Restore original version checker for compatibility; refactor export logic with reduced variable passing
- Reinstated the original version checker to maintain compatibility. - Reworked the export approach: the new export method and the legacy one are now split rather than merged. - Patch‑note data is handled separately, as seen in the example structure at resources.json (e.g. https://github.com/MrNewb/patchnotes/blob/main/resources.json). - Minimized variable passing by leveraging JSON template references directly, reducing redundancy.
This commit is contained in:
1 parent
d8f1599052
commit
e1a43ed918
1 file changed
+73
-71
@@ -1,9 +1,13 @@
|
|||||||
Version = Version or {}
|
Version = Version or {}
|
||||||
|
|
||||||
local function padVersion(v)
|
local function parseVersion(versionString)
|
||||||
|
return versionString and versionString:match('%d+%.%d+%.%d+') or versionString
|
||||||
|
end
|
||||||
|
|
||||||
|
local function padVersion(version)
|
||||||
local parts = {}
|
local parts = {}
|
||||||
for n in v:gmatch("%d+") do
|
for num in version:gmatch("%d+") do
|
||||||
table.insert(parts, tonumber(n))
|
table.insert(parts, tonumber(num))
|
||||||
end
|
end
|
||||||
while #parts < 3 do
|
while #parts < 3 do
|
||||||
table.insert(parts, 0)
|
table.insert(parts, 0)
|
||||||
@@ -23,26 +27,17 @@ local function compareVersions(v1, v2)
|
|||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
local function parseVersion(versionString)
|
|
||||||
return versionString:match('%d+%.%d+%.%d+') or versionString
|
|
||||||
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 function buildPatchNotesUrl(branch, repoPath)
|
||||||
local username, reponame = repoPath:match("([^/]+)/([^/]+)")
|
local username, reponame = repoPath:match("([^/]+)/([^/]+)")
|
||||||
return ('https://raw.githubusercontent.com/%s/%s/%s/resources.json'):format(username, reponame, branch)
|
return ('https://raw.githubusercontent.com/%s/%s/%s/resources.json'):format(username, reponame, branch)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function getDownloadUrl(repoPath, tebex, actualRepoPath)
|
local function getDownloadUrl(repoPath, isTebex, actualRepoPath)
|
||||||
if tebex then
|
if isTebex then
|
||||||
return "https://portal.cfx.re/assets/granted-assets"
|
return "https://portal.cfx.re/assets/granted-assets"
|
||||||
elseif actualRepoPath and actualRepoPath ~= false then
|
elseif actualRepoPath then
|
||||||
local repoUser, repoName = actualRepoPath:match("([^/]+)/([^/]+)")
|
local username, reponame = actualRepoPath:match("([^/]+)/([^/]+)")
|
||||||
if repoUser and repoName then return ('https://github.com/%s/%s/releases/latest'):format(repoUser, repoName) end
|
if username and reponame then return ('https://github.com/%s/%s/releases/latest'):format(username, reponame) end
|
||||||
end
|
end
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
@@ -61,8 +56,20 @@ local function printPatchNotes(notesToPrint)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function printError(message)
|
local function validateRepoPath(repoPath)
|
||||||
print(('^1[ERROR]^7 %s'):format(message))
|
local username, reponame = repoPath:match("([^/]+)/([^/]+)")
|
||||||
|
if not username or not reponame then return false, print('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, print('No version metadata found for resource "%s". Ensure fxmanifest.lua contains a valid version string.'):format(resource)
|
||||||
|
end
|
||||||
|
local version = parseVersion(localVersionRaw)
|
||||||
|
if not version then return nil, print('Invalid version format for resource "%s". Expected format: "x.x.x"'):format(resource) end
|
||||||
|
return version
|
||||||
end
|
end
|
||||||
|
|
||||||
local function processVersions(allVersions, localVersion)
|
local function processVersions(allVersions, localVersion)
|
||||||
@@ -81,7 +88,7 @@ local function processVersions(allVersions, localVersion)
|
|||||||
return notesToPrint
|
return notesToPrint
|
||||||
end
|
end
|
||||||
|
|
||||||
local function fetchPatchNotes(branch, resource, repoPath, localVersion, tebex, actualRepoPath)
|
local function fetchPatchNotes(branch, resource, repoPath, localVersion)
|
||||||
local url = buildPatchNotesUrl(branch, repoPath)
|
local url = buildPatchNotesUrl(branch, repoPath)
|
||||||
|
|
||||||
PerformHttpRequest(url, function(noteStatus, noteBody)
|
PerformHttpRequest(url, function(noteStatus, noteBody)
|
||||||
@@ -89,78 +96,73 @@ local function fetchPatchNotes(branch, resource, repoPath, localVersion, tebex,
|
|||||||
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 resourceData = patchData[resource]
|
||||||
if allVersions and type(allVersions) == "table" then
|
if resourceData and type(resourceData) == "table" then
|
||||||
|
local actualRepoPath = resourceData.repo or resourceData.actualRepoPath
|
||||||
|
local isTebex = not actualRepoPath -- If no repo path, it's Tebex
|
||||||
|
local allVersions = resourceData.versions or resourceData -- Support both new and old format
|
||||||
local notesToPrint = processVersions(allVersions, localVersion)
|
local notesToPrint = processVersions(allVersions, localVersion)
|
||||||
|
|
||||||
if #notesToPrint > 0 then
|
if #notesToPrint > 0 then
|
||||||
local downloadUrl = getDownloadUrl(repoPath, tebex, actualRepoPath) or url
|
local downloadUrl = getDownloadUrl(repoPath, isTebex, actualRepoPath) or url
|
||||||
printUpdateAvailable(resource, localVersion, downloadUrl)
|
printUpdateAvailable(resource, localVersion, downloadUrl)
|
||||||
printPatchNotes(notesToPrint)
|
printPatchNotes(notesToPrint)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif branch == "main" then
|
elseif branch == "main" then
|
||||||
fetchPatchNotes("master", resource, repoPath, localVersion, tebex, actualRepoPath)
|
-- Fallback to master branch if main doesn't exist
|
||||||
|
fetchPatchNotes("master", resource, repoPath, localVersion)
|
||||||
end
|
end
|
||||||
end, 'GET')
|
end, 'GET')
|
||||||
end
|
end
|
||||||
|
|
||||||
local function checkSimpleUpdate(repoPath, resource, localVersion, isTebex)
|
--- Pass a github username and repo name to check for the latest version of the resource.
|
||||||
local url = buildGithubApiUrl(repoPath)
|
--- Tebex is a optional boolean to determine if the message should be for a escrowed resource or not.
|
||||||
|
--- Example: Version.VersionChecker("TheOrderFivem/community_bridge", false)
|
||||||
PerformHttpRequest(url, function(status, response)
|
--- @param repoPath string
|
||||||
if status ~= 200 then return end
|
--- @param tebex boolean | nil
|
||||||
|
--- @return nil
|
||||||
local latest = parseVersion(json.decode(response).tag_name)
|
function Version.VersionChecker(repoPath, tebex)
|
||||||
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("([^/]+)/([^/]+)")
|
local username, reponame = repoPath:match("([^/]+)/([^/]+)")
|
||||||
if not username or not reponame then
|
local resource = reponame and repoPath:match("([^/]+)$")
|
||||||
return false, 'Invalid repository format. Expected format: "username/reponame"'
|
if not username or not reponame or not resource then return print('^1Invalid repository format. Expected format: "username/reponame"^0') end
|
||||||
end
|
|
||||||
return true, username, reponame
|
local version = GetResourceMetadata(resource, "version", 0)
|
||||||
|
if not version then return end
|
||||||
|
|
||||||
|
version = version:match('%d+%.%d+%.%d+')
|
||||||
|
if not version then return 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 == version or version: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
|
||||||
|
print(('^1An update is available for %s (current version: %s)\r\n - ^5Please download the latest version from %s^7'):format(resource, version, url))
|
||||||
|
end,
|
||||||
|
'GET')
|
||||||
end
|
end
|
||||||
|
|
||||||
local function getLocalVersion(resource)
|
--- Advanced version checker with patch notes support
|
||||||
local localVersionRaw = GetResourceMetadata(resource, "version", 0)
|
--- Reads Tebex information and repo path directly from the resources.json file
|
||||||
if not localVersionRaw then
|
--- @param repoPath string GitHub repository path for patch notes in format "username/reponame"
|
||||||
return nil, ('No version metadata found for resource "%s". Ensure fxmanifest.lua contains a valid version string.'):format(resource)
|
--- @param resourceName string The actual resource name to check
|
||||||
end
|
--- @return nil
|
||||||
return parseVersion(localVersionRaw)
|
function Version.AdvancedVersionChecker(repoPath, resourceName)
|
||||||
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.
|
|
||||||
--- Example - Version.VersionChecker("TheOrderFivem/patchnotes", true, true, "community_bridge", "TheOrderFivem/community_bridge")
|
|
||||||
---@param repoPath string
|
|
||||||
---@param isTebex boolean
|
|
||||||
---@param showPatchNotes boolean
|
|
||||||
---@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
|
|
||||||
---@return nil
|
|
||||||
function Version.VersionChecker(repoPath, isTebex, showPatchNotes, actualResourceName, actualRepoPathIfGithub)
|
|
||||||
local isValid, username, reponame = validateRepoPath(repoPath)
|
local isValid, username, reponame = validateRepoPath(repoPath)
|
||||||
if not isValid then return printError(username) end
|
if not isValid then return print(('^1[ERROR]^7 %s'):format(username)) end
|
||||||
|
|
||||||
local resource = actualResourceName or reponame
|
local resource = resourceName
|
||||||
local localVersion, errorMsg = getLocalVersion(resource)
|
local localVersion, errorMsg = getLocalVersion(resource)
|
||||||
if not localVersion then return printError(errorMsg) end
|
if not localVersion then return print(('^1[ERROR]^7 %s'):format(username)) end
|
||||||
|
fetchPatchNotes("main", resource, repoPath, localVersion)
|
||||||
if not showPatchNotes then return checkSimpleUpdate(repoPath, resource, localVersion, isTebex) end
|
|
||||||
|
|
||||||
fetchPatchNotes("main", resource, repoPath, localVersion, isTebex, actualRepoPathIfGithub)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
AddEventHandler('onResourceStart', function(resourceName)
|
AddEventHandler('onResourceStart', function(resourceName)
|
||||||
if GetCurrentResourceName() ~= resourceName then return end
|
if GetCurrentResourceName() ~= resourceName then return end
|
||||||
Version.VersionChecker("TheOrderFivem/patchnotes", false, true, "community_bridge", "TheOrderFivem/community_bridge")
|
Version.AdvancedVersionChecker("TheOrderFivem/patchnotes", resourceName)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
return Version
|
return Version
|
||||||
Reference in new issue
Block a user