-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathversion.lua
93 lines (79 loc) · 2.64 KB
/
version.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
lib.versionCheck('Force-Developing/force-sling')
local latestVersionUrl =
"https://gist.githubusercontent.com/Force-Developing/ee739a3263bc3421257d901e53e27b10/raw/force-sling"
local currentVersion = GetResourceMetadata(GetCurrentResourceName(), 'version', 0)
local function parseVersion(version)
local major, minor, patch = version:match("(%d+)%.(%d+)%.(%d+)")
if not major then return nil end
return {
major = tonumber(major),
minor = tonumber(minor),
patch = tonumber(patch)
}
end
local function isNewerVersion(current, latest)
local currentParsed = parseVersion(current)
local latestParsed = parseVersion(latest)
if not currentParsed or not latestParsed then
return false
end
if latestParsed.major > currentParsed.major then return true end
if latestParsed.major < currentParsed.major then return false end
if latestParsed.minor > currentParsed.minor then return true end
if latestParsed.minor < currentParsed.minor then return false end
return latestParsed.patch > currentParsed.patch
end
local function formatChangelogs(changelogs)
if not changelogs then return "No changelog available" end
return changelogs:gsub("%-", "\n-"):gsub("^%s*(.-)%s*$", "%1")
end
local function versionCheck()
PerformHttpRequest(latestVersionUrl, function(err, response, headers)
if err ~= 200 then
lib.print.error(string.format("Version check failed with error code: %s", err))
return
end
local success, result = pcall(function()
local version, changelogs = response:match("<(.-)>(.-)<")
if not version then
version = response:match("<(.-)>")
changelogs = response:match(">(.-)<")
end
if not version then
error("Invalid version format in response")
end
version = version:gsub("[<>]", "")
local isNewer = isNewerVersion(currentVersion, version)
local output = string.format([[
-------------
Current Version: %s
Latest Version: %s
-------------
%s
-------------]],
currentVersion,
version,
isNewer and string.format(
"Update available!\nChangelogs:\n%s",
formatChangelogs(changelogs)
) or "You are running the latest version."
)
if isNewer then
lib.print.warn(output)
else
lib.print.info(output)
end
end)
if not success then
lib.print.error(string.format("Failed to process version check: %s", result))
end
end, 'GET', '', {
['Cache-Control'] = 'no-cache',
['Content-Type'] = 'application/json',
['User-Agent'] = string.format('force-appearance/%s', currentVersion)
})
end
CreateThread(function()
Wait(5000)
versionCheck()
end)