forked from thisdp/dgs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.lua
299 lines (288 loc) · 8.99 KB
/
update.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
local check
if fileExists("update.cfg") then
check = fileOpen("update.cfg")
else
check = fileCreate("update.cfg")
end
local allstr = fileRead(check,fileGetSize(check))
setElementData(resourceRoot,"Version",allstr)
fileClose(check)
Version = tonumber(allstr) or 0
RemoteVersion = 0
ManualUpdate = false
updateTimer = false
updatePeriodTimer = false
function checkUpdate()
outputDebugString("[DGS]Connecting to github...")
fetchRemote("https://raw.githubusercontent.com/thisdp/dgs/master/update.cfg",function(data,err)
if err == 0 then
RemoteVersion = tonumber(data)
if not ManualUpdate then
if RemoteVersion > Version then
outputDebugString("[DGS]Remote Version Got [Remote:"..data.." Current:"..allstr.."]. See the update log: http://angel.mtaip.cn:233/dgsUpdate")
outputDebugString("[DGS]Update? Command: updatedgs")
if isTimer(updateTimer) then killTimer(updateTimer) end
updateTimer = setTimer(function()
if RemoteVersion > Version then
outputDebugString("[DGS]Remote Version Got [Remote:"..RemoteVersion.." Current:"..allstr.."]. See the update log: http://angel.mtaip.cn:233/dgsUpdate")
outputDebugString("[DGS]Update? Command: updatedgs")
else
killTimer(updateTimer)
end
end,dgsConfig.updateCheckNoticeInterval*60000,0)
else
outputDebugString("[DGS]Current Version("..allstr..") is the latest!")
end
else
startUpdate()
end
else
outputDebugString("[DGS]Can't Get Remote Version ("..err..")")
end
end)
end
if dgsConfig.updateCheckAuto then
checkUpdate()
updatePeriodTimer = setTimer(checkUpdate,dgsConfig.updateCheckInterval*3600000,0)
end
addCommandHandler("updatedgs",function(player)
local account = getPlayerAccount(player)
local accName = getAccountName(account)
local isAdmin = isObjectInACLGroup("user."..accName,aclGetGroup("Admin")) or isObjectInACLGroup("user."..accName,aclGetGroup("Console"))
if isAdmin then
outputDebugString("[DGS]Player "..getPlayerName(player).." attempt to update dgs (Allowed)")
outputDebugString("[DGS]Preparing for updating dgs")
outputChatBox("[DGS]Preparing for updating dgs",root,0,255,0)
if RemoteVersion > Version then
startUpdate()
else
ManualUpdate = true
checkUpdate()
end
else
outputChatBox("[DGS]!Access Denined!",player,255,0,0)
outputDebugString("[DGS]!Player "..getPlayerName(player).." attempt to update dgs (Denied)!",2)
end
end)
function startUpdate()
ManualUpdate = false
setTimer(function()
outputDebugString("[DGS]Requesting Update Data (From github)...")
fetchRemote("https://raw.githubusercontent.com/thisdp/dgs/master/meta.xml",function(data,err)
if err == 0 then
outputDebugString("[DGS]Update Data Acquired")
if fileExists("updated/meta.xml") then
fileDelete("updated/meta.xml")
end
local meta = fileCreate("updated/meta.xml")
fileWrite(meta,data)
fileClose(meta)
outputDebugString("[DGS]Requesting Verification Data...")
getGitHubTree()
else
outputDebugString("[DGS]!Can't Get Remote Update Data (ERROR:"..err..")",2)
end
end)
end,50,1)
end
preUpdate = {}
fileHash = {}
preUpdateCount = 0
UpdateCount = 0
FetchCount = 0
preFetch = 0
folderGetting = {}
function getGitHubTree(path,nextPath)
nextPath = nextPath or ""
fetchRemote(path or "https://api.github.com/repos/thisdp/dgs/git/trees/master",{},function(data,err)
if err.success then
local theTable = fromJSON(data)
folderGetting[theTable.sha] = nil
for k,v in pairs(theTable.tree) do
local thePath = nextPath..(v.path)
if v.mode == "040000" then
folderGetting[v.sha] = true
getGitHubTree(v.url,thePath.."/")
else
fileHash[thePath] = v.sha
end
end
if not next(folderGetting) then
checkFiles()
end
else
outputDebugString("[DGS]!Failed To Get Verification Data, Please Try Again Later (API Cool Down 60 mins)!",2)
end
end)
end
function checkFiles()
local xml = xmlLoadFile("updated/meta.xml")
for k,v in pairs(xmlNodeGetChildren(xml)) do
if xmlNodeGetName(v) == "script" or xmlNodeGetName(v) == "file" then
local path = xmlNodeGetAttribute(v,"src")
if path ~= "colorScheme.txt" then
local sha = ""
if fileExists(path) then
local file = fileOpen(path)
local size = fileGetSize(file)
local text = fileRead(file,size)
fileClose(file)
sha = hash("sha1","blob " .. size .. "\0" ..text)
end
if sha ~= fileHash[path] then
outputDebugString("[DGS]Update Required: ("..path..")")
table.insert(preUpdate,path)
end
end
end
end
table.insert(preUpdate,"colorScheme.txt")
DownloadFiles()
end
function DownloadFiles()
UpdateCount = UpdateCount + 1
if not preUpdate[UpdateCount] then
DownloadFinish()
return
end
outputDebugString("[DGS]Requesting ("..UpdateCount.."/"..(#preUpdate or "Unknown").."): "..tostring(preUpdate[UpdateCount]).."")
fetchRemote("https://raw.githubusercontent.com/thisdp/dgs/master/"..preUpdate[UpdateCount],function(data,err,path)
if err == 0 then
local size = 0
if path == "colorScheme.txt" then
if not fileExists(path) then
local file = fileCreate(path)
fileWrite(file,data)
local newsize = fileGetSize(file)
fileClose(file)
outputDebugString("[DGS]File Got ("..UpdateCount.."/"..#preUpdate.."): "..path.." [ "..size.."B -> "..newsize.."B ]")
else
local newsize,size = updateColorScheme(path,data)
outputDebugString("[DGS]Color Scheme Updated ("..UpdateCount.."/"..#preUpdate.."): "..path.." [ "..size.."B -> "..newsize.."B ]")
end
else
if fileExists(path) then
local file = fileOpen(path)
size = fileGetSize(file)
fileClose(file)
fileDelete(path)
end
local file = fileCreate(path)
fileWrite(file,data)
local newsize = fileGetSize(file)
fileClose(file)
outputDebugString("[DGS]File Got ("..UpdateCount.."/"..#preUpdate.."): "..path.." [ "..size.."B -> "..newsize.."B ]")
end
else
outputDebugString("[DGS]!Download Failed: "..path.." ("..err..")!",2)
end
if preUpdate[UpdateCount+1] then
DownloadFiles()
else
DownloadFinish()
end
end,"",false,preUpdate[UpdateCount])
end
function updateColorScheme(path,data)
schemeColor = {}
local file = fileOpen("colorSchemeIndex.txt")
local str = fileRead(file,fileGetSize(file))
local size = fileGetSize(file)
fileClose(file)
loadstring(str)()
-------------------------------------
local file = fileOpen(path)
local str = fileRead(file,fileGetSize(file))
local size = fileGetSize(file)
fileClose(file)
if fileExists(path..".bak") then
fileDelete(path..".bak")
end
fileRename(path,path..".bak")
loadstring(data)()
loadstring(str)()
local newData = ""
local newData_ = ""
for k,v in pairs(schemeColor) do
if type(v) == "table" then
for a,b in pairs(v) do
if type(b) == "table" then
local pstr = ""
for i = 1,#b do
local cr,cg,cb,ca = fromcolor(b[i])
pstr = pstr.."tocolor("..cr..","..cg..","..cb..","..ca.."),"
end
local pstr = pstr:sub(1,#pstr-1)
newData = newData.."schemeColor."..k.."."..a.." = {"..pstr.."}".."\n"
else
local cr,cg,cb,ca = fromcolor(b)
newData = newData.."schemeColor."..k.."."..a.." = tocolor("..cr..","..cg..","..cb..","..ca..")\n"
end
end
newData = newData.."\n"
else
newData_ = newData_.."schemeColor."..k.." = "..tostring(v).."\n"
end
end
local file = fileCreate(path)
fileWrite(file,newData..newData_)
local newsize = fileGetSize(file)
fileClose(file)
return newsize,size
end
function DownloadFinish()
outputDebugString("[DGS]Changing Config File")
if fileExists("update.cfg") then
fileDelete("update.cfg")
end
local file = fileCreate("update.cfg")
fileWrite(file,tostring(RemoteVersion))
fileClose(file)
if fileExists("meta.xml") then
fileDelete("meta.xml")
end
fileRename("updated/meta.xml","meta.xml")
outputDebugString("[DGS]Update Complete (Updated "..#preUpdate.." Files)")
outputDebugString("[DGS]Please Restart DGS")
outputChatBox("[DGS]Update Complete (Updated "..#preUpdate.." Files)",root,0,255,0)
preUpdate = {}
preUpdateCount = 0
UpdateCount = 0
FetchCount = 0
preFetch = 0
end
addCommandHandler("dgsver",function(pla,cmd)
local vsdd
if fileExists("update.cfg") then
local file = fileOpen("update.cfg")
local vscd = fileRead(file,fileGetSize(file))
fileClose(file)
vsdd = tonumber(vscd)
if vsdd then
outputDebugString("[DGS]Version: "..vsdd,3)
else
outputDebugString("[DGS]Version State is damaged! Please use /updatedgs to update",1)
end
else
outputDebugString("[DGS]Version State is damaged! Please use /updatedgs to update",1)
end
if getPlayerName(pla) ~= "Console" then
if vsdd then
outputChatBox("[DGS]Version: "..vsdd,pla,0,255,0)
else
outputChatBox("[DGS]Version State is damaged! Please use /updatedgs to update",pla,255,0,0)
end
end
end)
function fromcolor(int)
local a,r,g,b
b,g,r,a = bitExtract(int,0,8),bitExtract(int,8,8),bitExtract(int,16,8),bitExtract(int,24,8)
return r,g,b,a
end
function tocolor(r,g,b,a)
local color = a*256^3+r*256^2+g*256+b
if color > 2147483647 then
color = color-0xFFFFFFFF-1
end
return color
end