forked from thisdp/dgs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.lua
More file actions
282 lines (269 loc) · 8.71 KB
/
update.lua
File metadata and controls
282 lines (269 loc) · 8.71 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
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
--Check whether you enable/disable dgs update system..
--If you don't trust dgs.. Please Disable It In "config.txt"
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)
if dgsConfig.updateSystemDisabled then return end
local _fetchRemote = fetchRemote
function fetchRemote(...)
if hasObjectPermissionTo(getThisResource(),"function.fetchRemote",true) then
return _fetchRemote(...)
else
outputDebugString("[DGS]Request 'fetchRemote', but access denied. Use the command 'aclrequest allow dgs all'",2)
end
return false
end
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.."].")
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.."].")
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 == 0 then
local theTable = fromJSON(data)
folderGetting[theTable.sha] = nil
for k,v in pairs(theTable.tree) do
if (v.path ~= "styleMapper.lua" and fileExists("styleManager/styleMapper.lua")) and v.path ~= "meta.xml" then
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
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 not string.find(path,"styleMapper.lua") and path ~= "meta.xml" 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
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 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 ]")
else
outputDebugString("[DGS]Download Failed: "..path.." ("..err..")!",2)
end
if preUpdate[UpdateCount+1] then
DownloadFiles()
else
DownloadFinish()
end
end,"",false,preUpdate[UpdateCount])
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
backupStyleMapper()
fileDelete("meta.xml")
end
recoverStyleMapper()
outputDebugString("[DGS]Update Complete ( "..#preUpdate.." File"..(#preUpdate==1 and "" or "s")" Changed )")
outputDebugString("[DGS]Please Restart DGS")
outputChatBox("[DGS]Update Complete ( "..#preUpdate.." File"..(#preUpdate==1 and "" or "s")" Changed )",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)
styleBackupStr = ""
locator = [[ <export]]
function backupStyleMapper()
if dgsConfig.backupMeta then
fileCopy("meta.xml","meta.xml.bak",true)
end
assert(fileExists("meta.xml"),"[DGS] Please rename the meta xml as meta.xml")
local meta = fileOpen("meta.xml")
local str = fileRead(meta,fileGetSize(meta))
local startStr = "<!----$Add Your Styles Here---->"
local endStr = "<!----&Add Your Styles Here---->"
local startPos = str:find(startStr)
local endPos = str:find(endStr)
styleBackupStr = str:sub(startPos,endPos-1).."<!--&Add Your Styles Here-->"
fileClose(meta)
if fileExists("styleMapperBackup.bak") then
fileDelete("styleMapperBackup.bak")
end
if dgsConfig.backupStyleMeta then
local file = fileCreate("styleMapperBackup.bak")
fileWrite(file,styleBackupStr)
fileClose(file)
end
end
function recoverStyleMapper()
assert(styleBackupStr ~= "","[DGS] Failed to recover style mapper")
local meta = fileOpen("updated/meta.xml")
local str = fileRead(meta,fileGetSize(meta))
fileClose(meta)
local newMeta = fileCreate("meta.xml")
local startStr = "<!----$Add Your Styles Here---->"
local startPos = str:find(startStr)
local exportPos = str:find(locator)
local scriptsStr = str:sub(1,startPos-1)
local exportsStr = str:sub(exportPos)
fileWrite(newMeta,scriptsStr..styleBackupStr.."\r\n"..exportsStr)
fileClose(newMeta)
fileDelete("updated/meta.xml")
end