-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_updater.lua
75 lines (63 loc) · 2.39 KB
/
server_updater.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
local repositoryOwner = "YelehaUwU"
local repositoryName = "3DEditor"
local localPath = "updates/"
local outputServerLog_ = outputServerLog
local outputServerLog = function(message)
outputServerLog_("[" .. repositoryName .. "] » " .. message)
print("[" .. repositoryName .. "] » " .. message)
end
local checkForUpdates, downloadUpdate, getVersion
checkForUpdates = function()
local apiUrl = "https://api.github.com/repos/" .. repositoryOwner .. "/" .. repositoryName .. "/releases/latest"
fetchRemote(apiUrl, function(response, error)
if error == 0 then
local releaseInfo = fromJSON(response)
local tagName = releaseInfo.tag_name
if tagName then
local localVersion = getVersion()
if localVersion < tagName then
outputServerLog("A new update is available! Downloading...")
downloadUpdate(tagName)
else
outputServerLog("You are already using the latest version.")
end
end
else
outputServerLog("Failed to check for updates: " .. error)
end
end)
end
addEventHandler("onResourceStart", resourceRoot, checkForUpdates, false)
downloadUpdate = function(version)
local downloadUrl = "https://github.com/" .. repositoryOwner .. "/" .. repositoryName .. "/archive/" .. version .. ".zip"
local localFilePath = localPath .. version .. ".zip"
fetchRemote(downloadUrl, function(response, error)
if error == 0 then
local file = fileCreate(localFilePath)
if file then
fileWrite(file, response)
fileClose(file)
outputServerLog("Update downloaded and saved to " .. localFilePath)
else
outputServerLog("Failed to create the local file: " .. localFilePath)
end
else
outputServerLog("Failed to download update: " .. error)
end
end)
end
getVersion = function()
local metaFile = xmlLoadFile("meta.xml")
if metaFile then
local infoNode = xmlFindChild(metaFile, "info", 0)
if infoNode then
local version = xmlNodeGetAttribute(infoNode, "version")
xmlUnloadFile(metaFile)
if version then
return version
end
end
xmlUnloadFile(metaFile)
end
return "1.0"
end