-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartup_update_overlay_worker.cpp
More file actions
284 lines (246 loc) · 9.47 KB
/
startup_update_overlay_worker.cpp
File metadata and controls
284 lines (246 loc) · 9.47 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
#include "pch.h"
#include "startup_update_overlay_internal.h"
#include "http_client.h"
#include <algorithm>
#include <atomic>
#include <iomanip>
#include <memory>
#include <mutex>
#include <sstream>
#include <string>
#include <vector>
namespace mapupdater::startup {
namespace {
std::wstring FormatBytes(uint64_t bytes) {
constexpr double kKb = 1024.0;
constexpr double kMb = 1024.0 * 1024.0;
constexpr double kGb = 1024.0 * 1024.0 * 1024.0;
std::wstringstream ss;
ss << std::fixed << std::setprecision(1);
if (bytes >= static_cast<uint64_t>(kGb)) {
ss << (bytes / kGb) << L" GB";
} else if (bytes >= static_cast<uint64_t>(kMb)) {
ss << (bytes / kMb) << L" MB";
} else if (bytes >= static_cast<uint64_t>(kKb)) {
ss << (bytes / kKb) << L" KB";
} else {
ss << bytes << L" B";
}
return ss.str();
}
std::wstring OwnerFlagsLabel(uint32_t owner_flags) {
const bool dota = (owner_flags & kDownloadOwnerDota) != 0;
const bool lod = (owner_flags & kDownloadOwnerLod) != 0;
if (dota && lod) {
return L"Dota+LoD";
}
if (dota) {
return L"Dota";
}
if (lod) {
return L"LoD";
}
return L"-";
}
void OverlayPost(HWND hwnd, UINT msg) {
if (hwnd != nullptr) {
PostMessageW(hwnd, msg, WPARAM(0), LPARAM(0));
}
}
HWND OverlayGetSessionHwnd(const std::shared_ptr<OverlayDownloadSession> &session) {
std::lock_guard<std::mutex> lock(session->mutex);
return session->hwnd;
}
void RequestOverlayUiRefresh(const std::shared_ptr<OverlayDownloadSession> &session) {
const HWND hwnd = OverlayGetSessionHwnd(session);
if (hwnd == nullptr) {
session->ui_refresh_pending.store(false);
return;
}
bool expected = false;
if (session->ui_refresh_pending.compare_exchange_strong(expected, true)) {
OverlayPost(hwnd, kMsgOverlayUiRefresh);
}
}
void SetOverlayProgressState(const std::shared_ptr<OverlayDownloadSession> &session,
size_t index, uint64_t downloaded, uint64_t total) {
std::lock_guard<std::mutex> lock(session->mutex);
constexpr ULONGLONG kSpeedSampleMinMs = 180;
const uint64_t prev_aggregate_downloaded = session->speed_last_downloaded;
const ULONGLONG prev_tick_ms = session->speed_last_tick_ms;
session->current_index = index;
session->current_downloaded = downloaded;
session->current_total = total;
if (index < session->items.size()) {
const auto &item = session->items[index];
const uint64_t active_total = (total > 0) ? total : item.expected_size;
const uint64_t active_downloaded =
(active_total > 0) ? (std::min)(downloaded, active_total) : downloaded;
const uint64_t aggregate_downloaded =
session->completed_map_bytes + session->completed_mpq_bytes + active_downloaded;
if (item.kind == DownloadKind::Map) {
session->map_downloaded = session->completed_map_bytes + active_downloaded;
session->mpq_downloaded = session->completed_mpq_bytes;
} else {
session->mpq_downloaded = session->completed_mpq_bytes + active_downloaded;
session->map_downloaded = session->completed_map_bytes;
}
const ULONGLONG now_ms = GetTickCount64();
if (aggregate_downloaded < prev_aggregate_downloaded || prev_tick_ms == 0 ||
now_ms <= prev_tick_ms) {
session->current_speed_bps = 0;
session->speed_last_tick_ms = now_ms;
session->speed_last_downloaded = aggregate_downloaded;
} else {
const ULONGLONG delta_ms = now_ms - prev_tick_ms;
if (delta_ms >= kSpeedSampleMinMs) {
const uint64_t delta_bytes = aggregate_downloaded - prev_aggregate_downloaded;
if (delta_bytes > 0) {
session->current_speed_bps = (delta_bytes * 1000ull) / delta_ms;
if (session->current_speed_bps > 0) {
session->last_nonzero_speed_bps = session->current_speed_bps;
}
}
session->speed_last_tick_ms = now_ms;
session->speed_last_downloaded = aggregate_downloaded;
}
}
std::wstringstream title;
title << (session->autoupdate_mode ? L"Auto Map Update" : L"Map Update Download");
session->title_text = title.str();
const std::wstring owner_label = OwnerFlagsLabel(item.owner_flags);
std::wstringstream status;
status << L"Downloading " << (item.kind == DownloadKind::Map ? L"map" : L"mpq")
<< L" [" << owner_label << L"]"
<< L" (" << (index + 1) << L"/" << session->items.size() << L")";
session->status_text = status.str();
std::wstringstream detail;
detail << item.display_name;
if (active_total > 0) {
const int percent = static_cast<int>((active_downloaded * 100) / active_total);
detail << L"\r\n" << percent << L"% (" << FormatBytes(active_downloaded) << L" / "
<< FormatBytes(active_total) << L")";
} else if (item.expected_size > 0) {
const uint64_t safe_downloaded = downloaded;
const int percent = static_cast<int>(
(std::min)(safe_downloaded, item.expected_size) * 100 / item.expected_size);
detail << L"\r\n" << percent << L"% (" << FormatBytes(safe_downloaded) << L" / "
<< FormatBytes(item.expected_size) << L")";
} else {
detail << L"\r\n" << FormatBytes(downloaded);
}
session->detail_text = detail.str();
}
}
} // namespace
void SetOverlayFinishedState(const std::shared_ptr<OverlayDownloadSession> &session, bool success,
bool canceled, const std::wstring &error_text) {
std::lock_guard<std::mutex> lock(session->mutex);
session->running = false;
session->finished = true;
session->success = success;
session->canceled = canceled;
session->error_text = error_text;
session->current_speed_bps = 0;
session->last_nonzero_speed_bps = 0;
session->speed_last_tick_ms = 0;
session->speed_last_downloaded = 0;
if (success) {
if (session->map_needed) {
session->map_downloaded =
session->map_total > 0 ? session->map_total : session->map_downloaded;
}
if (session->mpq_needed) {
session->mpq_downloaded =
session->mpq_total > 0 ? session->mpq_total : session->mpq_downloaded;
}
session->title_text = L"MapUpdater";
session->status_text = L"Complete!";
session->detail_text = L"All required files have been downloaded.";
} else if (canceled) {
session->title_text = L"MapUpdater";
session->status_text = L"Download canceled";
session->detail_text = L"Download was canceled by user.";
} else {
session->title_text = L"MapUpdater";
session->status_text = L"Download failed";
session->detail_text = error_text;
}
}
void OverlayCloseWorkerHandle(const std::shared_ptr<OverlayDownloadSession> &session) {
HANDLE handle = nullptr;
{
std::lock_guard<std::mutex> lock(session->mutex);
handle = session->worker_handle;
session->worker_handle = nullptr;
}
if (handle != nullptr) {
CloseHandle(handle);
}
}
DWORD WINAPI OverlayDownloadWorkerProc(LPVOID param) {
std::unique_ptr<std::shared_ptr<OverlayDownloadSession>> holder(
static_cast<std::shared_ptr<OverlayDownloadSession> *>(param));
std::shared_ptr<OverlayDownloadSession> session = *holder;
bool overall_success = true;
bool was_canceled = false;
std::wstring error_text;
for (size_t i = 0; i < session->items.size(); ++i) {
if (session->cancel_requested.load()) {
overall_success = false;
was_canceled = true;
break;
}
const auto &item = session->items[i];
SetOverlayProgressState(session, i, 0, item.expected_size);
RequestOverlayUiRefresh(session);
auto on_progress = [session, i](uint64_t downloaded, uint64_t total) -> bool {
const uint64_t reported_total = (total > 0) ? total : session->items[i].expected_size;
SetOverlayProgressState(session, i, downloaded, reported_total);
RequestOverlayUiRefresh(session);
return !session->cancel_requested.load();
};
const auto result =
net::HttpDownloadToFile(item.url, item.target_path, on_progress, &session->cancel_requested);
if (result.canceled || session->cancel_requested.load()) {
overall_success = false;
was_canceled = true;
break;
}
if (!result.success) {
overall_success = false;
std::wstringstream ss;
ss << L"Failed to download:\r\n" << item.display_name;
if (result.status_code != 0) {
ss << L"\r\nHTTP status: " << result.status_code;
}
if (result.error_code != 0) {
ss << L"\r\nError code: " << result.error_code;
}
error_text = ss.str();
break;
}
{
std::lock_guard<std::mutex> lock(session->mutex);
const uint64_t completed_size = result.total_bytes > 0
? result.total_bytes
: (result.downloaded_bytes > 0
? result.downloaded_bytes
: item.expected_size);
if (item.kind == DownloadKind::Map) {
session->completed_map_bytes += completed_size;
session->map_downloaded = session->completed_map_bytes;
} else {
session->completed_mpq_bytes += completed_size;
session->mpq_downloaded = session->completed_mpq_bytes;
}
}
if (item.kind == DownloadKind::Map && !item.counter_map_name.empty()) {
IncrementDownloadCounter(item.counter_map_name, session->config);
}
}
SetOverlayFinishedState(session, overall_success, was_canceled, error_text);
OverlayPost(OverlayGetSessionHwnd(session), kMsgOverlayWorkerDone);
return 0;
}
} // namespace mapupdater::startup