forked from Spring-Chobby/Chobby
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathapi_download_handler.lua
More file actions
452 lines (386 loc) · 12.9 KB
/
api_download_handler.lua
File metadata and controls
452 lines (386 loc) · 12.9 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function widget:GetInfo()
return {
name = "Download Handler",
desc = "Handles downloads",
author = "GoogleFrog",
date = "10 April 2017",
license = "GPL-v2",
layer = 0,
handler = true,
api = true,
enabled = true,
}
end
local externalFunctions = {}
local listeners = {}
local wrapperFunctions = {}
local downloadQueue = {} -- {name, fileType, priority, id, retryCount}
local downloadCount = 0
local topPriority = 0
local removedDownloads = {}
local requestUpdate = false
local USE_WRAPPER_DOWNLOAD = true
-- Wrapper types are RAPID, MAP, MISSION, DEMO, ENGINE, NOTKNOWN
local typeMap = {
game = "RAPID",
map = "MAP",
}
local reverseTypeMap = {
RAPID = "game",
MAP = "map",
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Listeners
local function CallListeners(event, ...)
if listeners[event] == nil then
return nil -- no event listeners
end
local eventListeners = Spring.Utilities.ShallowCopy(listeners[event])
for i = 1, #eventListeners do
local listener = eventListeners[i]
args = {...}
xpcall(function() listener(listener, unpack(args)) end,
function(err) Spring.Echo("Campaign Listener Error", err) end )
end
return true
end
function externalFunctions.AddListener(event, listener)
if listener == nil then
Spring.Log(LOG_SECTION, LOG.ERROR, "Event: " .. tostring(event) .. ", listener cannot be nil")
return
end
local eventListeners = listeners[event]
if eventListeners == nil then
eventListeners = {}
listeners[event] = eventListeners
end
table.insert(eventListeners, listener)
end
function externalFunctions.RemoveListener(event, listener)
if listeners[event] then
for k, v in pairs(listeners[event]) do
if v == listener then
table.remove(listeners[event], k)
if #listeners[event] == 0 then
listeners[event] = nil
end
break
end
end
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Utilities
local function DownloadSortFunc(a, b)
return a.priority > b.priority or (a.priority == b.priority and a.id < b.id)
end
local function StartDownload(id, name, fileType)
if USE_WRAPPER_DOWNLOAD and WG.WrapperLoopback and WG.WrapperLoopback.DownloadFile then
WG.WrapperLoopback.DownloadFile(name, typeMap[fileType])
CallListeners("DownloadStarted", id, name, fileType)
else
VFS.DownloadArchive(name, fileType)
end
end
local function DownloadQueueUpdate()
requestUpdate = false
if #downloadQueue == 0 then
CallListeners("DownloadQueueUpdate", downloadQueue, removedDownloads)
return
end
table.sort(downloadQueue, DownloadSortFunc)
local front = downloadQueue[1]
if not front.active then
StartDownload(front.id, front.name, front.fileType)
front.active = true
end
CallListeners("DownloadQueueUpdate", downloadQueue, removedDownloads)
end
local function GetDownloadIndex(downloadList, name, fileType)
for i = 1, #downloadList do
local data = downloadList[i]
if data.name == name and (data.fileType == fileType or fileType == "map") then
return i
end
end
return nil
end
local function GetDownloadBySpringDownloadID(downloadList, springDownloadID)
for i = 1, #downloadList do
local data = downloadList[i]
if data.springDownloadID == springDownloadID then
return i
end
end
return nil
end
local function GetDownloadIndexByName(downloadList, downloadName)
for i = 1, #downloadList do
local data = downloadList[i]
if data.name == downloadName then
return i
end
end
return nil
end
local function AssociatedSpringDownloadID(springDownloadID, name, fileType)
local index = GetDownloadIndex(downloadQueue, name, fileType)
if not index then
return false
end
downloadQueue[index].springDownloadID = springDownloadID
end
local function RetryDownload(name, fileType)
local index = GetDownloadIndex(downloadQueue, name, fileType)
if not (index and downloadQueue[index]) then
return false
end
local download = downloadQueue[index]
download.attempts = (download.attempts or 0) + 1
if download.attempts >= (WG.Chobby.Configuration.downloadRetryCount or 0) then
return false
end
StartDownload(download.id, download.name, download.fileType)
download.active = true
return true
end
local function RemoveDownload(name, fileType, putInRemoveList, removalType)
local index = GetDownloadIndex(downloadQueue, name, fileType)
if not index then
return false
end
fileType = downloadQueue[index].fileType or fileType
if removalType == "success" then
CallListeners("DownloadFinished", downloadQueue[index].id, name, fileType)
else
CallListeners("DownloadFailed", downloadQueue[index].id, removalType, name, fileType)
end
if putInRemoveList then
downloadQueue[index].removalType = removalType
removedDownloads[#removedDownloads + 1] = downloadQueue[index]
end
downloadQueue[index] = downloadQueue[#downloadQueue]
downloadQueue[#downloadQueue] = nil
if putInRemoveList and removalType == "fail" and WG.Chobby.Configuration.downloadRetryCount then
local lastFailed = removedDownloads[#removedDownloads]
if lastFailed.retryCount < WG.Chobby.Configuration.downloadRetryCount then
Spring.Echo("Downloading of ", name, fileType, "failed, retryCount=", lastFailed.retryCount)
lastFailed.retryCount = lastFailed.retryCount + 1
externalFunctions.RetryDownload(name,fileType)
end
end
requestUpdate = true
return true
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Externals Functions
function externalFunctions.QueueDownload(name, fileType, priority)
priority = priority or 1
if priority == -1 then
priority = topPriority + 1
end
if topPriority < priority then
topPriority = priority
end
local index = GetDownloadIndex(downloadQueue, name, fileType)
if index then
local data = downloadQueue[index]
if priority > data.priority then
data.priority = priority
requestUpdate = true
end
return
end
downloadCount = downloadCount + 1
downloadQueue[#downloadQueue + 1] = {
name = name,
fileType = fileType,
priority = priority,
id = downloadCount,
retryCount = 0,
}
requestUpdate = true
CallListeners("DownloadQueued", downloadCount, name, fileType)
end
function externalFunctions.SetDownloadTopPriority(name, fileType)
local index = GetDownloadIndex(downloadQueue, name, fileType)
if not index then
return
end
topPriority = topPriority + 1
downloadQueue[index].priority = topPriority
requestUpdate = true
return true
end
function externalFunctions.CancelDownload(name, fileType)
local index = GetDownloadIndex(downloadQueue, name, fileType)
if not index then
return false
end
if downloadQueue[index].active then
if USE_WRAPPER_DOWNLOAD and WG.WrapperLoopback and WG.WrapperLoopback.AbortDownload then
WG.WrapperLoopback.AbortDownload(name, typeMap[fileType])
end
return
end
downloadQueue[index].removalType = "cancel"
removedDownloads[#removedDownloads + 1] = downloadQueue[index]
downloadQueue[index] = downloadQueue[#downloadQueue]
downloadQueue[#downloadQueue] = nil
requestUpdate = true
end
function externalFunctions.RetryDownload(name, fileType)
local index = GetDownloadIndex(removedDownloads, name, fileType)
if not index then
return false
end
externalFunctions.QueueDownload(name, fileType, removedDownloads[index].priority)
removedDownloads[index] = removedDownloads[#removedDownloads]
removedDownloads[#removedDownloads] = nil
requestUpdate = true
return true
end
function externalFunctions.RemoveRemovedDownload(name, fileType)
local index = GetDownloadIndex(removedDownloads, name, fileType)
if not index then
return false
end
removedDownloads[index] = removedDownloads[#removedDownloads]
removedDownloads[#removedDownloads] = nil
requestUpdate = true
return true
end
function externalFunctions.MaybeDownloadArchive(name, archiveType, priority)
if not VFS.HasArchive(name) then
externalFunctions.QueueDownload(name, archiveType, priority)
end
end
function externalFunctions.GetDownloadQueue()
return downloadQueue, removedDownloads
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Wrapper Interface
function wrapperFunctions.DownloadFinished(name, fileType, success, aborted)
fileType = fileType and reverseTypeMap[fileType]
if fileType then
if not VFS.HasArchive(name) then
VFS.ScanAllDirs() -- Find downloaded file (if it exists).
end
local possiblyRetry = not success and not aborted
if not (possiblyRetry and RetryDownload(name, fileType)) then
if possiblyRetry and fileType == "game" then
WG.WrapperLoopback.DownloadFile(name, typeMap["map"])
else
if (not aborted) and (not success) and VFS.HasArchive(name) then
success = true
end
RemoveDownload(name, fileType, true, (aborted and "cancel") or (success and "success") or "fail")
end
end
end
--Chotify:Post({
-- title = "Download " .. ((success and "Finished") or "Failed"),
-- body = (name or "???") .. " of type " .. (fileType or "???"),
--})
end
function wrapperFunctions.DownloadFileProgress(name, fileType, progress, secondsRemaining, totalLength, currentSpeed)
local index = GetDownloadIndexByName(downloadQueue, name)
if not index then
return
end
totalLength = (tonumber(totalLength or 0) or 0)/1023^2
CallListeners("DownloadProgress", downloadQueue[index].id, totalLength*math.min(1, (tonumber(progress or 0) or 0)/100), totalLength, name)
end
function wrapperFunctions.ImageDownloadFinished(requestToken, imageUrl, imagePath)
CallListeners("ImageDownloadFinished", requestToken, imageUrl, imagePath)
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Widget Interface
function widget:Update()
if requestUpdate then
DownloadQueueUpdate()
end
end
function widget:DownloadProgress(downloadID, downloaded, total)
local index = GetDownloadBySpringDownloadID(downloadQueue, downloadID)
if not index then
return
end
downloaded = downloaded / 1024 / 1024
total = total / 1024 / 1024
CallListeners("DownloadProgress", downloadQueue[index].id, downloaded, total, downloadQueue[index].name)
end
function widget:DownloadStarted(downloadID)
local index = GetDownloadBySpringDownloadID(downloadQueue, downloadID)
if not index then
return
end
local data = downloadQueue[index]
CallListeners("DownloadStarted", data.id, data.name, data.fileType)
end
function widget:DownloadFinished(downloadID)
local index = GetDownloadBySpringDownloadID(downloadQueue, downloadID)
if not index then
return
end
local data = downloadQueue[index]
RemoveDownload(data.name, data.fileType, true, "success")
end
function widget:DownloadFailed(downloadID, errorID)
local index = GetDownloadBySpringDownloadID(downloadQueue, downloadID)
if not index then
return
end
local data = downloadQueue[index]
RemoveDownload(data.name, data.fileType, true, "fail")
end
function widget:DownloadQueued(downloadID, archiveName, archiveType)
AssociatedSpringDownloadID(downloadID, archiveName, archiveType)
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Initialization
local function TestDownload()
WG.WrapperLoopback.DownloadFile("Sands of Time v1.0", "MAP")
Spring.Echo("TestDownload")
Chotify:Post({
title = "Download Failed",
body = "Starting backup download for " .. ("Sands of Time v1.0" or "???"),
})
end
local function GetRequiredDownloads()
local Configuration = WG.Chobby.Configuration
local campaignStartMaps = Configuration.campaignConfig and Configuration.campaignConfig.planetDefs
if campaignStartMaps then
campaignStartMaps = campaignStartMaps.startingPlanetMaps or {}
for i = 1, #campaignStartMaps do
externalFunctions.MaybeDownloadArchive(campaignStartMaps[i], "map", 1.5)
end
end
local skirmishPages = Configuration.gameConfig and Configuration.gameConfig.skirmishSetupData and Configuration.gameConfig.skirmishSetupData.pages
if skirmishPages then
for i = 1, #skirmishPages do
local pageData = skirmishPages[i]
if pageData.name == "map" and pageData.options then
for j = 1, #pageData.options do
externalFunctions.MaybeDownloadArchive(pageData.options[j], "map", 2)
end
end
end
end
end
-- Allow for earling listener registration.
WG.DownloadHandler = externalFunctions
function widget:Initialize()
CHOBBY_DIR = LUA_DIRNAME .. "widgets/chobby/"
VFS.Include(LUA_DIRNAME .. "widgets/chobby/headers/exports.lua", nil, VFS.RAW_FIRST)
WG.DownloadWrapperInterface = wrapperFunctions
WG.Delay(GetRequiredDownloads, 1)
end