-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoupdate_server.lua
More file actions
239 lines (212 loc) · 6.1 KB
/
autoupdate_server.lua
File metadata and controls
239 lines (212 loc) · 6.1 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
--[[
* This script was made from scratch by chris1384.
* Do not redistribute this (under other names) without my permission, do not edit & upload without my permission or take any credit from it.
* For any questions, bug reports or any suggestions, send a message to @chris1384 on Discord.
* Have fun mapping! - chris1384 <3
* Thanks to chris1384 to letting me use this script! - primelprime
]]
local autoUpdate = true -- // make MT+ auto-update itself, set this to 'false' or delete this file to remove the auto-update feature
local filesFetched = 0
local remoteFiles = {}
addEventHandler("onResourceStart", resourceRoot, function()
queueGitRepo()
setTimer(queueGitRepo, 24 * 60 * 60 * 1000, 0) -- 24 hours
end)
function queueGitRepo() -- starting sequence
filesFetched = 0
remoteFiles = {}
if not autoUpdate then
return
end
if not hasObjectPermissionTo(resource, "function.fetchRemote") then
setTimer(function()
local resourceName = getResourceName(getThisResource())
outputChatBox(
"[PAS] #FFFFFFResource is #AA0000not allowed #FFFFFFto auto-update itself or modify other content, please allow it using #FF64FF'/aclrequest allow "
.. resourceName
.. " all'#FFFFFF then restart!",
root,
255,
100,
255,
true
)
outputDebugString(
"[PAS]: Resource is not allowed to fetch GitHub updates using 'fetchRemote', please allow it using '/aclrequest allow "
.. resourceName
.. " all' then restart!",
0,
255,
100,
100
)
end, 1000, 1)
return
end
fetchRemote("https://api.github.com/repos/primelprime/pas/contents", function(response, err)
if response == "ERROR" then
outputDebugString(
"[PAS]: Resource failed to fetch for updates, returned "
.. tostring(response)
.. " with code: "
.. tostring(err),
0,
255,
100,
100
)
return
else
outputDebugString("[PAS]: Checking for updates..", 4, 255, 255, 100)
end
local response = { fromJSON(response) }
local dataToSave = {}
for k, v in ipairs(response) do
if v.download_url then
remoteFiles[v.name] = { url = v.download_url, sha = v.sha }
dataToSave[v.name] = { sha = v.sha }
filesFetched = filesFetched + 1
end
end
setTimer(function(dataLoL)
downloadRepoFiles(dataLoL)
downloadTimer = nil
end, 1000, 1, dataToSave, response)
end)
end
function downloadRepoFiles(data2save, response)
if filesFetched == 0 then
outputDebugString("[PAS]: Resource file paths failed to fetch, aborting download.", 0, 255, 100, 100)
return
end
local targetFiles = remoteFiles
remoteFiles = {}
for k, v in pairs(targetFiles) do
fetchRemote(v.url, function(response)
remoteFiles[k] = { data = response, sha = v.sha }
filesFetched = filesFetched - 1
if filesFetched == 0 then
processFiles(data2save)
end
end)
end
end
function processFiles(data2save)
local filesModified = {}
local resourceData = loadDirectoryData() or ""
local unformattedData = fromJSON(resourceData)
if resourceData and unformattedData and type(unformattedData) == "table" then
for fileName, remoteData in pairs(remoteFiles) do
remoteData.sha = remoteData.sha:upper()
if unformattedData[fileName] and fileExists(fileName) then
unformattedData[fileName].sha = unformattedData[fileName].sha:upper()
if remoteData.sha ~= unformattedData[fileName].sha then
if fileExists("addons/backups/" .. fileName .. ".bak") then
fileDelete("addons/backups/" .. fileName .. ".bak")
end
if fileExists(fileName) then
fileRename(fileName, "addons/backups/" .. fileName .. ".bak")
end
local file = fileCreate(fileName)
if file then
fileWrite(file, remoteData.data)
table.insert(filesModified, fileName)
fileClose(file)
end
end
else
local file = fileCreate(fileName)
if file then
fileWrite(file, remoteData.data)
table.insert(filesModified, fileName)
fileClose(file)
end
end
end
else
for fileName, remoteData in pairs(remoteFiles) do
if fileExists("addons/backups/" .. fileName .. ".bak") then
fileDelete("addons/backups/" .. fileName .. ".bak")
end
if fileExists(fileName) then
fileRename(fileName, "addons/backups/" .. fileName .. ".bak")
end
local file = fileCreate(fileName)
if file then
fileWrite(file, remoteData.data)
table.insert(filesModified, fileName)
fileClose(file)
end
end
end
saveDirectoryData(toJSON(data2save))
if #filesModified > 0 then
fetchRemote("https://api.github.com/repos/primelprime/pas/commits", function(...)
local commitData = { fromJSON(...) }
if commitData then
outputDebugString(
"[PAS]: Auto-updater has finished. Modified ["
.. table.concat(filesModified, ", ")
.. "] "
.. tostring(#filesModified)
.. " files.",
4,
100,
255,
100
)
outputDebugString(
"[PAS]: Update title: '" .. string.gsub(commitData[1].commit.message, "\n\n", " - ") .. "'",
4,
100,
255,
100
)
outputChatBox(
"[PAS] #FFFFFFThe resource has been updated! Title: #FF64FF'"
.. string.gsub(commitData[1].commit.message, "\n\n", " #FFFFFF- #FF64FF")
.. "'",
root,
255,
100,
255,
true
)
if hasObjectPermissionTo(resource, "function.restartResource") then
restartResource(resource)
else
outputDebugString(
"[PAS]: Resource was not able to restart itself, please restart it to apply updates.",
0,
255,
100,
100
)
end
end
end)
else
outputDebugString("[PAS]: Auto-updater has finished. No updates.", 4, 100, 255, 100)
end
end
function saveDirectoryData(data)
if fileExists("addons/autoupdater.json") then
fileDelete("addons/autoupdater.json")
end
local file = fileCreate("addons/autoupdater.json")
if file then
fileWrite(file, data)
fileClose(file)
return true
end
return false
end
function loadDirectoryData()
local returnedData
local file = fileExists("addons/autoupdater.json") == true and fileOpen("addons/autoupdater.json") or nil
if file then
returnedData = fileRead(file, fileGetSize(file))
fileClose(file)
end
return returnedData
end