forked from scummvm/scummvm
/
dlcmanager.cpp
131 lines (110 loc) · 3.43 KB
/
dlcmanager.cpp
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
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/system.h"
#include "backends/dlc/dlcmanager.h"
#include "backends/dlc/dlcdesc.h"
#include "backends/dlc/scummvmcloud.h"
#include "gui/dlcsdialog.h"
#include "gui/launcher.h"
namespace Common {
DECLARE_SINGLETON(DLC::DLCManager);
}
namespace DLC {
DLCManager::DLCManager() : CommandSender(nullptr) {
_store = g_system->getDLCStore();
}
DLCManager::~DLCManager() {
for (uint32 i = 0; i < _dlcs.size(); ++i) {
delete _dlcs[i];
}
}
void DLCManager::init() {}
void DLCManager::getAllDLCs() {
_store->getAllDLCs();
}
void DLCManager::refreshDLCList() {
sendCommand(GUI::kRefreshDLCList, 0);
}
void DLCManager::refreshLauncherGameList() {
if (_launcher) {
_launcher->rebuild();
}
}
void DLCManager::setLauncher(GUI::LauncherDialog *launcher) {
_launcher = launcher;
}
void DLCManager::addDownload(uint32 idx) {
if (_dlcs[idx]->state == DLCDesc::kInProgress) {
// if DLC is already in queue, don't add again
return;
}
_dlcs[idx]->state = DLCDesc::kInProgress;
_queuedDownloadTasks.push(_dlcs[idx]);
_dlcsInProgress.push_back(_dlcs[idx]);
if (!_isDLCDownloading) {
// if queue is not already processing
DLCManager::processDownloadQueue();
}
}
void DLCManager::processDownloadQueue() {
_currentDownloadedSize = 0;
_isDLCDownloading = true;
if (!_queuedDownloadTasks.empty()) {
if (_queuedDownloadTasks.front()->state == DLCDesc::kInProgress) {
_currentDownloadingDLC = _queuedDownloadTasks.front()->id;
DLCManager::startDownloadAsync(_queuedDownloadTasks.front()->id, _queuedDownloadTasks.front()->url);
} else {
// state is already cancelled/downloaded -> skip download
_queuedDownloadTasks.pop();
_dlcsInProgress.remove_at(0);
// process next download in the queue
processDownloadQueue();
}
} else {
// no more download in queue
_isDLCDownloading = false;
_currentDownloadingDLC = "";
}
}
void DLCManager::startDownloadAsync(const Common::String &id, const Common::String &url) {
_store->startDownloadAsync(id, url);
}
bool DLCManager::cancelDownload(uint32 idx) {
if (_queuedDownloadTasks.front()->idx == idx) {
// if already downloading, interrupt startDownloadAsync
_interruptCurrentDownload = true;
} else {
// if not started, skip it in processDownload()
_dlcs[idx]->state = DLCDesc::kCancelled;
DLCMan.refreshDLCList();
}
return true;
}
Common::String DLCManager::getCurrentDownloadingDLC() const {
return _currentDownloadingDLC;
}
int DLCManager::getDLCIdxFromId(const Common::String &id) const {
for (uint32 i = 0; i < _dlcs.size(); ++i) {
if (_dlcs[i]->id == id) return i;
}
return -1;
}
} // End of namespace DLC