-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartup_update_overlay.cpp
More file actions
1521 lines (1344 loc) · 48.6 KB
/
startup_update_overlay.cpp
File metadata and controls
1521 lines (1344 loc) · 48.6 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "pch.h"
#include "startup_update_internal.h"
#include "startup_update_overlay_internal.h"
#include "http_client.h"
#include <algorithm>
#include <atomic>
#include <commctrl.h>
#include <cwctype>
#include <iomanip>
#include <memory>
#include <mutex>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
#include <shellapi.h>
#pragma comment(lib, "comctl32.lib")
namespace mapupdater::startup {
namespace {
bool CaseInsensitiveEquals(std::wstring_view a, std::wstring_view b) {
if (a.size() != b.size()) {
return false;
}
for (size_t i = 0; i < a.size(); ++i) {
if (std::towlower(a[i]) != std::towlower(b[i])) {
return false;
}
}
return true;
}
struct OverlayMainWindowSearch {
DWORD pid = 0;
HWND exclude_hwnd = nullptr;
HWND found_hwnd = nullptr;
};
BOOL CALLBACK EnumProcessMainWindowProc(HWND hwnd, LPARAM lparam) {
auto *ctx = reinterpret_cast<OverlayMainWindowSearch *>(lparam);
if (ctx == nullptr) {
return TRUE;
}
if (hwnd == ctx->exclude_hwnd) {
return TRUE;
}
DWORD pid = 0;
GetWindowThreadProcessId(hwnd, &pid);
if (pid != ctx->pid) {
return TRUE;
}
if (!IsWindowVisible(hwnd) || IsIconic(hwnd)) {
return TRUE;
}
if (GetWindow(hwnd, GW_OWNER) != nullptr) {
return TRUE;
}
wchar_t class_name[128] = {};
GetClassNameW(hwnd, class_name,
static_cast<int>(sizeof(class_name) / sizeof(class_name[0])));
if (CaseInsensitiveEquals(class_name, L"MapUpdaterCustomOverlayWindow")) {
return TRUE;
}
const LONG_PTR ex_style = GetWindowLongPtrW(hwnd, GWL_EXSTYLE);
if ((ex_style & WS_EX_TOOLWINDOW) != 0) {
return TRUE;
}
ctx->found_hwnd = hwnd;
return FALSE;
}
HWND FindOwnMainWindowForOverlay(HWND exclude_hwnd) {
const DWORD self_pid = GetCurrentProcessId();
const HWND foreground = GetForegroundWindow();
if (foreground != nullptr && foreground != exclude_hwnd) {
DWORD fg_pid = 0;
GetWindowThreadProcessId(foreground, &fg_pid);
if (fg_pid == self_pid && IsWindowVisible(foreground) && !IsIconic(foreground)) {
return foreground;
}
}
OverlayMainWindowSearch search = {};
search.pid = self_pid;
search.exclude_hwnd = exclude_hwnd;
EnumWindows(EnumProcessMainWindowProc, reinterpret_cast<LPARAM>(&search));
return search.found_hwnd;
}
void OverlaySyncWithHostWindow(OverlayWindowContext *ctx) {
if (ctx == nullptr || ctx->hwnd == nullptr) {
return;
}
if (ctx->host_hwnd == nullptr || !IsWindow(ctx->host_hwnd)) {
ctx->host_hwnd = FindOwnMainWindowForOverlay(ctx->hwnd);
}
if (ctx->host_hwnd == nullptr || !IsWindow(ctx->host_hwnd)) {
SetWindowPos(ctx->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
return;
}
if (!IsWindowVisible(ctx->host_hwnd) || IsIconic(ctx->host_hwnd)) {
ShowWindow(ctx->hwnd, SW_HIDE);
return;
}
if (ctx->manual_position) {
SetWindowPos(ctx->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW |
SWP_NOOWNERZORDER);
if (!IsWindowVisible(ctx->hwnd)) {
ShowWindow(ctx->hwnd, SW_SHOWNA);
}
return;
}
RECT host_rect = {};
if (!GetWindowRect(ctx->host_hwnd, &host_rect)) {
return;
}
RECT overlay_rect = {};
GetWindowRect(ctx->hwnd, &overlay_rect);
const int width = overlay_rect.right - overlay_rect.left;
const int height = overlay_rect.bottom - overlay_rect.top;
int x = host_rect.left + 20;
int y = host_rect.top + 20;
const int screen_w = GetSystemMetrics(SM_CXSCREEN);
const int screen_h = GetSystemMetrics(SM_CYSCREEN);
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
if (x + width > screen_w) {
x = (std::max)(0, screen_w - width);
}
if (y + height > screen_h) {
y = (std::max)(0, screen_h - height);
}
SetWindowPos(ctx->hwnd, HWND_TOPMOST, x, y, 0, 0,
SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOOWNERZORDER);
if (!IsWindowVisible(ctx->hwnd)) {
ShowWindow(ctx->hwnd, SW_SHOWNA);
}
}
int PercentFromProgress(uint64_t downloaded, uint64_t total) {
if (total == 0) {
return 0;
}
return static_cast<int>(((std::min)(downloaded, total) * 100) / total);
}
std::wstring FormatOverlayBytes(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 TrimSingleLineForOverlay(std::wstring text, size_t max_chars) {
for (wchar_t &ch : text) {
if (ch == L'\r' || ch == L'\n' || ch == L'\t') {
ch = L' ';
}
}
while (!text.empty() && text.back() == L' ') {
text.pop_back();
}
if (text.size() <= max_chars) {
return text;
}
if (max_chars <= 3) {
return text.substr(0, max_chars);
}
text.resize(max_chars - 3);
text += L"...";
return text;
}
std::wstring GetWindowTextCopy(HWND hwnd) {
if (hwnd == nullptr) {
return {};
}
const int len = GetWindowTextLengthW(hwnd);
if (len <= 0) {
return {};
}
std::vector<wchar_t> buffer(static_cast<size_t>(len) + 1, L'\0');
GetWindowTextW(hwnd, buffer.data(), len + 1);
return std::wstring(buffer.data());
}
void SetWindowTextIfChanged(HWND hwnd, const std::wstring &text) {
if (hwnd == nullptr) {
return;
}
if (GetWindowTextCopy(hwnd) != text) {
SetWindowTextW(hwnd, text.c_str());
}
}
void ShowWindowIfChanged(HWND hwnd, bool show) {
if (hwnd == nullptr) {
return;
}
const bool visible = IsWindowVisible(hwnd) != FALSE;
if (visible != show) {
ShowWindow(hwnd, show ? SW_SHOW : SW_HIDE);
}
}
std::wstring BuildOverlayProgressSummary(uint64_t map_downloaded, uint64_t map_total,
uint64_t mpq_downloaded, uint64_t mpq_total,
bool map_needed, bool mpq_needed, bool finished,
bool success, bool canceled,
uint64_t current_speed_bps) {
if (finished && success) {
return L"Complete!";
}
if (finished && canceled) {
return L"Canceled";
}
std::wstringstream ss;
if (map_needed) {
const int map_percent = PercentFromProgress(map_downloaded, map_total);
ss << L"w3x: " << map_percent << L"%";
}
if (mpq_needed) {
if (map_needed) {
ss << L" ";
}
const int mpq_percent = PercentFromProgress(mpq_downloaded, mpq_total);
ss << L"mpq: " << mpq_percent << L"%";
}
if (!map_needed && !mpq_needed) {
ss << L"Waiting...";
}
const uint64_t total_all = map_total + mpq_total;
if (total_all > 0) {
const uint64_t downloaded_all = map_downloaded + mpq_downloaded;
ss << L" | total: " << PercentFromProgress(downloaded_all, total_all) << L"%";
}
if (current_speed_bps > 0) {
ss << L" | " << FormatOverlayBytes(current_speed_bps) << L"/s";
}
return ss.str();
}
std::wstring BuildVersionLine(const LatestMapsInfo &maps_info, const AppConfig &config) {
std::vector<std::wstring> parts;
if (config.dota_enabled) {
if (maps_info.dota.has_value() && !maps_info.dota->version.empty()) {
parts.push_back(L"Dota " + TrimSingleLineForOverlay(SafeUtf8ToWide(maps_info.dota->version), 32));
} else {
parts.push_back(L"Dota (unavailable)");
}
}
if (config.lod_enabled) {
if (maps_info.lod.has_value() && !maps_info.lod->version.empty()) {
parts.push_back(L"LoD " + TrimSingleLineForOverlay(SafeUtf8ToWide(maps_info.lod->version), 32));
} else {
parts.push_back(L"LoD (unavailable)");
}
}
if (parts.empty()) {
return L"Version: (unknown)";
}
std::wstring joined;
for (size_t i = 0; i < parts.size(); ++i) {
if (i > 0) {
joined += L" | ";
}
joined += parts[i];
}
return L"Version: " + joined;
}
std::wstring BuildChangelogLine(const LatestMapsInfo &maps_info, const AppConfig &config) {
std::vector<std::wstring> parts;
if (config.dota_enabled && maps_info.dota.has_value() && !maps_info.dota->changelog.empty()) {
parts.push_back(L"Dota (available)");
}
if (config.lod_enabled && maps_info.lod.has_value() && !maps_info.lod->changelog.empty()) {
parts.push_back(L"LoD (available)");
}
if (parts.empty()) {
return L"Changelog: -";
}
std::wstring joined;
for (size_t i = 0; i < parts.size(); ++i) {
if (i > 0) {
joined += L" | ";
}
joined += parts[i];
}
return L"Changelog: " + joined;
}
void SetOverlayInitialPromptState(const std::shared_ptr<OverlayDownloadSession> &session) {
std::lock_guard<std::mutex> lock(session->mutex);
session->title_text = session->autoupdate_mode ? L"Auto Map Update" : L"Map Update Available";
session->status_text = session->autoupdate_mode
? L"Missing files detected. Auto-download will start."
: L"Missing files detected. Click \"Download\" to start.";
session->map_needed = false;
session->mpq_needed = false;
session->map_total = 0;
session->mpq_total = 0;
session->map_downloaded = 0;
session->mpq_downloaded = 0;
session->completed_map_bytes = 0;
session->completed_mpq_bytes = 0;
session->current_speed_bps = 0;
session->speed_last_tick_ms = 0;
session->speed_last_downloaded = 0;
for (const auto &item : session->items) {
if (item.kind == DownloadKind::Map) {
session->map_needed = true;
session->map_total += item.expected_size;
} else {
session->mpq_needed = true;
session->mpq_total += item.expected_size;
}
}
size_t dota_count = 0;
size_t lod_count = 0;
for (const auto &item : session->items) {
if ((item.owner_flags & kDownloadOwnerDota) != 0) {
++dota_count;
}
if ((item.owner_flags & kDownloadOwnerLod) != 0) {
++lod_count;
}
}
std::wstringstream details;
details << L"Review missing files below. ";
details << L"Queue: " << session->items.size() << L" file(s)";
if (dota_count > 0 || lod_count > 0) {
details << L" | Dota: " << dota_count << L" | LoD: " << lod_count;
}
session->detail_text = details.str();
}
void OverlayApplyLayout(OverlayWindowContext *ctx, bool expanded, bool autoupdate_mode,
bool running, bool finished) {
if (ctx == nullptr || ctx->hwnd == nullptr) {
return;
}
const bool force_expanded = autoupdate_mode || running || finished;
const bool effective_expanded = expanded || force_expanded;
const bool show_toggle = !autoupdate_mode;
if (ctx->layout_initialized &&
ctx->last_layout_expanded == effective_expanded &&
ctx->last_layout_show_toggle == show_toggle) {
return;
}
ctx->layout_initialized = true;
ctx->last_layout_expanded = effective_expanded;
ctx->last_layout_show_toggle = show_toggle;
const int width = effective_expanded ? kOverlayExpandedWidth : kOverlayCollapsedWidth;
const int height = effective_expanded ? kOverlayExpandedHeight : kOverlayCollapsedHeight;
RECT rect = {};
GetWindowRect(ctx->hwnd, &rect);
const int current_width = rect.right - rect.left;
const int current_height = rect.bottom - rect.top;
if (current_width != width || current_height != height) {
SetWindowPos(ctx->hwnd, nullptr, 0, 0, width, height,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
}
const int inner_left = 12;
const int header_top = 10;
RECT new_toggle_rect = {inner_left, header_top, inner_left + 26, header_top + 24};
ctx->toggle_rect = new_toggle_rect;
ctx->toggle_visible = show_toggle;
if (!show_toggle) {
ctx->toggle_tracking = false;
ctx->toggle_pressed_visual = false;
}
MoveWindow(ctx->title_hwnd, inner_left + 34, header_top + 1, width - 220, 22, FALSE);
if (!effective_expanded) {
SetRectEmpty(&ctx->pending_list_rect);
SetRectEmpty(&ctx->downloaded_list_rect);
MoveWindow(ctx->btn_download_hwnd, width - 212, 14, 98, 26, FALSE);
MoveWindow(ctx->btn_later_hwnd, width - 108, 14, 86, 26, FALSE);
MoveWindow(ctx->btn_cancel_hwnd, width - 108, 14, 86, 26, FALSE);
MoveWindow(ctx->btn_close_hwnd, width - 108, 14, 86, 26, FALSE);
} else {
const int lists_top = 154;
const int lists_height = 108;
const int gap = 10;
const int panel_width = (width - 24 - gap) / 2;
const int left_x = inner_left;
const int right_x = inner_left + panel_width + gap;
MoveWindow(ctx->status_hwnd, inner_left, 44, width - 24, 20, FALSE);
MoveWindow(ctx->version_hwnd, inner_left, 68, width - 24, 18, FALSE);
MoveWindow(ctx->changelog_hwnd, inner_left, 88, width - 24, 18, FALSE);
MoveWindow(ctx->detail_hwnd, inner_left, 111, width - 24, 36, FALSE);
MoveWindow(ctx->progress_text_hwnd, inner_left, 272, width - 24, 18, FALSE);
MoveWindow(ctx->progress_hwnd, inner_left, 294, width - 24, 14, FALSE);
MoveWindow(ctx->btn_download_hwnd, inner_left, 312, 114, 28, FALSE);
MoveWindow(ctx->btn_later_hwnd, inner_left + 122, 312, 92, 28, FALSE);
MoveWindow(ctx->btn_cancel_hwnd, inner_left, 312, 114, 28, FALSE);
MoveWindow(ctx->btn_close_hwnd, width - 110, 312, 98, 28, FALSE);
ctx->pending_list_rect = {left_x, lists_top, left_x + panel_width, lists_top + lists_height};
ctx->downloaded_list_rect = {right_x, lists_top, right_x + panel_width,
lists_top + lists_height};
}
const bool show_body = effective_expanded;
ShowWindowIfChanged(ctx->status_hwnd, show_body);
ShowWindowIfChanged(ctx->version_hwnd, show_body);
ShowWindowIfChanged(ctx->changelog_hwnd, show_body);
ShowWindowIfChanged(ctx->detail_hwnd, show_body);
ShowWindowIfChanged(ctx->progress_text_hwnd, show_body);
ShowWindowIfChanged(ctx->progress_hwnd, show_body);
if (show_toggle) {
const int title_x = inner_left + 34;
MoveWindow(ctx->title_hwnd, title_x, header_top + 1, width - 220, 22, FALSE);
} else {
MoveWindow(ctx->title_hwnd, inner_left, header_top + 1, width - 198, 22, FALSE);
}
// Layered popup + child controls can leave stale control visuals after
// expand/collapse transitions. Force a full child redraw only when layout
// actually changed (this function returns early otherwise).
RedrawWindow(ctx->hwnd, nullptr, nullptr,
RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN | RDW_UPDATENOW);
}
void OverlayRefreshUi(OverlayWindowContext *ctx) {
if (ctx == nullptr || ctx->session == nullptr) {
return;
}
ctx->session->ui_refresh_pending.store(false);
LatestMapsInfo maps_info;
AppConfig config;
std::wstring status;
std::wstring detail;
bool started = false;
bool running = false;
bool finished = false;
bool success = false;
bool canceled = false;
bool autoupdate_mode = false;
bool map_needed = false;
bool mpq_needed = false;
size_t current_index = 0;
uint64_t downloaded = 0;
uint64_t total = 0;
uint64_t expected_size = 0;
uint64_t current_speed_bps = 0;
uint64_t last_nonzero_speed_bps = 0;
uint64_t map_downloaded = 0;
uint64_t map_total = 0;
uint64_t mpq_downloaded = 0;
uint64_t mpq_total = 0;
{
std::lock_guard<std::mutex> lock(ctx->session->mutex);
maps_info = ctx->session->maps_info;
config = ctx->session->config;
status = ctx->session->status_text;
detail = ctx->session->detail_text;
started = ctx->session->started;
running = ctx->session->running;
finished = ctx->session->finished;
success = ctx->session->success;
canceled = ctx->session->canceled;
autoupdate_mode = ctx->session->autoupdate_mode;
map_needed = ctx->session->map_needed;
mpq_needed = ctx->session->mpq_needed;
current_index = ctx->session->current_index;
downloaded = ctx->session->current_downloaded;
total = ctx->session->current_total;
current_speed_bps = ctx->session->current_speed_bps;
last_nonzero_speed_bps = ctx->session->last_nonzero_speed_bps;
map_downloaded = ctx->session->map_downloaded;
map_total = ctx->session->map_total;
mpq_downloaded = ctx->session->mpq_downloaded;
mpq_total = ctx->session->mpq_total;
if (current_index < ctx->session->items.size()) {
expected_size = ctx->session->items[current_index].expected_size;
}
}
const bool effective_expanded = ctx->expanded || autoupdate_mode || running || finished;
OverlayApplyLayout(ctx, ctx->expanded, autoupdate_mode, running, finished);
SetWindowTextIfChanged(ctx->title_hwnd, L"Map Updater");
SetWindowTextIfChanged(ctx->version_hwnd, BuildVersionLine(maps_info, config));
SetWindowTextIfChanged(ctx->changelog_hwnd, BuildChangelogLine(maps_info, config));
if (config.dota_enabled && maps_info.dota.has_value() && !maps_info.dota->changelog.empty()) {
ctx->changelog_url = SafeUtf8ToWide(maps_info.dota->changelog);
} else if (config.lod_enabled && maps_info.lod.has_value() && !maps_info.lod->changelog.empty()) {
ctx->changelog_url = SafeUtf8ToWide(maps_info.lod->changelog);
} else {
ctx->changelog_url.clear();
}
SetWindowTextIfChanged(ctx->status_hwnd, status);
SetWindowTextIfChanged(ctx->detail_hwnd, detail);
const uint64_t visible_speed_bps =
(current_speed_bps > 0) ? current_speed_bps : last_nonzero_speed_bps;
std::wstring progress_summary =
BuildOverlayProgressSummary(map_downloaded, map_total, mpq_downloaded, mpq_total,
map_needed, mpq_needed, finished, success, canceled,
running ? visible_speed_bps : 0);
SetWindowTextIfChanged(ctx->progress_text_hwnd, progress_summary);
int progress_percent = 0;
const uint64_t aggregate_total = map_total + mpq_total;
const uint64_t aggregate_downloaded = map_downloaded + mpq_downloaded;
const uint64_t progress_total = (aggregate_total > 0) ? aggregate_total
: ((total > 0) ? total : expected_size);
const uint64_t progress_downloaded =
(aggregate_total > 0) ? aggregate_downloaded : downloaded;
if (progress_total > 0) {
progress_percent =
static_cast<int>(((std::min)(progress_downloaded, progress_total) * 100) /
progress_total);
} else if (finished && success) {
progress_percent = 100;
}
SendMessageW(ctx->progress_hwnd, PBM_SETPOS, WPARAM(progress_percent), LPARAM(0));
const bool show_download = !started && !autoupdate_mode;
const bool show_later = !started && !autoupdate_mode;
const bool show_cancel = running;
const bool show_close = finished || (!running && started);
ShowWindowIfChanged(ctx->btn_download_hwnd, show_download);
ShowWindowIfChanged(ctx->btn_later_hwnd, show_later);
ShowWindowIfChanged(ctx->btn_cancel_hwnd, show_cancel);
ShowWindowIfChanged(ctx->btn_close_hwnd, show_close);
if (!effective_expanded && !show_close && !show_cancel && !show_download && !show_later) {
ShowWindowIfChanged(ctx->btn_close_hwnd, true);
}
if (running) {
if (!IsWindowEnabled(ctx->btn_cancel_hwnd)) {
EnableWindow(ctx->btn_cancel_hwnd, TRUE);
}
}
SetWindowTextIfChanged(ctx->btn_close_hwnd, L"Close");
if (effective_expanded && ctx->hwnd != nullptr) {
bool lists_changed = false;
if (ctx->last_list_current_index != current_index ||
ctx->last_list_started != started ||
ctx->last_list_running != running ||
ctx->last_list_finished != finished ||
ctx->last_list_success != success ||
ctx->last_list_canceled != canceled) {
lists_changed = true;
ctx->last_list_current_index = current_index;
ctx->last_list_started = started;
ctx->last_list_running = running;
ctx->last_list_finished = finished;
ctx->last_list_success = success;
ctx->last_list_canceled = canceled;
}
if (lists_changed) {
if (!IsRectEmpty(&ctx->pending_list_rect)) {
InvalidateRect(ctx->hwnd, &ctx->pending_list_rect, FALSE);
}
if (!IsRectEmpty(&ctx->downloaded_list_rect)) {
InvalidateRect(ctx->hwnd, &ctx->downloaded_list_rect, FALSE);
}
}
}
}
void OverlayStartDownload(OverlayWindowContext *ctx) {
if (ctx == nullptr || ctx->session == nullptr) {
return;
}
ctx->expanded = true;
{
std::lock_guard<std::mutex> lock(ctx->session->mutex);
if (ctx->session->started || ctx->session->finished) {
return;
}
ctx->session->started = true;
ctx->session->running = true;
ctx->session->cancel_requested.store(false);
if (ctx->session->autoupdate_mode) {
ctx->session->status_text = L"Starting auto-download...";
} else {
ctx->session->status_text = L"Starting download...";
}
ctx->session->detail_text.clear();
}
auto *shared_param = new std::shared_ptr<OverlayDownloadSession>(ctx->session);
HANDLE thread = CreateThread(nullptr, 0, OverlayDownloadWorkerProc, shared_param, 0, nullptr);
if (thread == nullptr) {
delete shared_param;
SetOverlayFinishedState(ctx->session, false, false,
L"Failed to create download thread.");
OverlayRefreshUi(ctx);
return;
}
{
std::lock_guard<std::mutex> lock(ctx->session->mutex);
ctx->session->worker_handle = thread;
}
OverlayRefreshUi(ctx);
}
HBRUSH OverlayBgBrush() {
static HBRUSH brush = CreateSolidBrush(RGB(22, 24, 28));
return brush;
}
HBRUSH OverlayHeaderBrush() {
static HBRUSH brush = CreateSolidBrush(RGB(30, 34, 40));
return brush;
}
HBRUSH OverlayDividerBrush() {
static HBRUSH brush = CreateSolidBrush(RGB(56, 63, 71));
return brush;
}
void FillSolidRectColor(HDC hdc, const RECT &rc, COLORREF color) {
HBRUSH brush = CreateSolidBrush(color);
FillRect(hdc, &rc, brush);
DeleteObject(brush);
}
bool IsOverlayActionButtonId(int id) {
return id == kBtnDownloadId || id == kBtnLaterId || id == kBtnCancelId ||
id == kBtnCloseId;
}
std::wstring OverlayOwnerLabel(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"-";
}
std::wstring OverlayItemKindLabel(DownloadKind kind) {
return kind == DownloadKind::Map ? L"w3x" : L"mpq";
}
struct OverlayPaintListLine {
std::wstring text;
COLORREF color = RGB(220, 224, 229);
};
struct OverlayPaintListsSnapshot {
bool show_lists = false;
std::vector<OverlayPaintListLine> pending;
std::vector<OverlayPaintListLine> done;
};
OverlayPaintListsSnapshot BuildOverlayPaintListsSnapshot(const OverlayWindowContext *ctx) {
OverlayPaintListsSnapshot snapshot;
if (ctx == nullptr || ctx->session == nullptr || !ctx->last_layout_expanded) {
return snapshot;
}
if (IsRectEmpty(&ctx->pending_list_rect) || IsRectEmpty(&ctx->downloaded_list_rect)) {
return snapshot;
}
snapshot.show_lists = true;
std::vector<DownloadItem> items;
size_t current_index = 0;
bool started = false;
bool running = false;
bool finished = false;
bool success = false;
bool canceled = false;
{
std::lock_guard<std::mutex> lock(ctx->session->mutex);
items = ctx->session->items;
current_index = ctx->session->current_index;
started = ctx->session->started;
running = ctx->session->running;
finished = ctx->session->finished;
success = ctx->session->success;
canceled = ctx->session->canceled;
}
for (size_t i = 0; i < items.size(); ++i) {
const auto &item = items[i];
const bool completed = started && ((finished && success) || i < current_index);
const bool active = started && running && i == current_index;
const bool failed_or_canceled_current =
started && finished && !success && i == current_index;
OverlayPaintListLine line;
line.text = L"[";
line.text += OverlayOwnerLabel(item.owner_flags);
line.text += L"] ";
line.text += OverlayItemKindLabel(item.kind);
line.text += L": ";
line.text += item.display_name;
if (completed) {
line.color = RGB(118, 217, 126);
snapshot.done.push_back(std::move(line));
continue;
}
if (active) {
line.color = RGB(153, 197, 255);
line.text = L"\x2022 " + line.text + L" (downloading)";
} else if (failed_or_canceled_current) {
line.color = canceled ? RGB(231, 196, 108) : RGB(230, 120, 120);
line.text = L"\x2022 " + line.text +
(canceled ? L" (canceled)" : L" (failed)");
} else {
line.color = RGB(214, 218, 223);
line.text = L"\x2022 " + line.text;
}
snapshot.pending.push_back(std::move(line));
}
return snapshot;
}
void PaintOverlayListPanel(HDC hdc, const RECT &panel_rect, const wchar_t *title,
const std::vector<OverlayPaintListLine> &lines,
COLORREF title_color) {
if (IsRectEmpty(&panel_rect)) {
return;
}
FillSolidRectColor(hdc, panel_rect, RGB(26, 29, 34));
HPEN panel_pen = CreatePen(PS_SOLID, 1, RGB(70, 78, 87));
HGDIOBJ old_pen = SelectObject(hdc, panel_pen);
HGDIOBJ old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
Rectangle(hdc, panel_rect.left, panel_rect.top, panel_rect.right, panel_rect.bottom);
SelectObject(hdc, old_brush);
SelectObject(hdc, old_pen);
DeleteObject(panel_pen);
RECT header = panel_rect;
header.bottom = (std::min)(panel_rect.bottom, panel_rect.top + 20);
FillSolidRectColor(hdc, header, RGB(34, 38, 44));
RECT title_rc = header;
InflateRect(&title_rc, -6, 0);
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, title_color);
DrawTextW(hdc, title, -1, &title_rc, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX);
RECT text_rc = panel_rect;
text_rc.top = header.bottom + 4;
InflateRect(&text_rc, -6, 0);
TEXTMETRICW tm = {};
GetTextMetricsW(hdc, &tm);
const int line_height = (std::max)(14, static_cast<int>(tm.tmHeight) + 1);
int y = text_rc.top;
if (lines.empty()) {
SetTextColor(hdc, RGB(132, 139, 147));
RECT empty_rc = text_rc;
DrawTextW(hdc, L"(empty)", -1, &empty_rc, DT_LEFT | DT_TOP | DT_SINGLELINE | DT_NOPREFIX);
return;
}
const int max_lines = (text_rc.bottom > text_rc.top)
? (std::max)(1, static_cast<int>(text_rc.bottom - text_rc.top) / line_height)
: 1;
const int visible_count = (std::min)(max_lines, static_cast<int>(lines.size()));
for (int i = 0; i < visible_count; ++i) {
RECT line_rc = text_rc;
line_rc.top = y;
line_rc.bottom = y + line_height;
SetTextColor(hdc, lines[static_cast<size_t>(i)].color);
std::wstring line_text = TrimSingleLineForOverlay(lines[static_cast<size_t>(i)].text, 46);
DrawTextW(hdc, line_text.c_str(), -1, &line_rc,
DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX);
y += line_height;
}
if (static_cast<int>(lines.size()) > visible_count) {
RECT more_rc = text_rc;
more_rc.top = y;
more_rc.bottom = y + line_height;
SetTextColor(hdc, RGB(132, 139, 147));
std::wstringstream ss;
ss << L"+" << (lines.size() - static_cast<size_t>(visible_count)) << L" more...";
const std::wstring more = ss.str();
DrawTextW(hdc, more.c_str(), -1, &more_rc,
DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX);
}
}
void PaintOverlayActionButton(const DRAWITEMSTRUCT *dis) {
if (dis == nullptr || dis->hwndItem == nullptr) {
return;
}
const int control_id = static_cast<int>(dis->CtlID);
const bool disabled = (dis->itemState & ODS_DISABLED) != 0;
const bool pressed = (dis->itemState & ODS_SELECTED) != 0;
const bool primary = control_id == kBtnDownloadId;
const bool cancel = control_id == kBtnCancelId;
COLORREF bg = RGB(34, 38, 44);
COLORREF border = RGB(86, 96, 106);
COLORREF text = RGB(232, 236, 241);
if (primary) {
bg = RGB(30, 66, 44);
border = RGB(78, 175, 108);
} else if (cancel) {
bg = RGB(74, 42, 42);
border = RGB(178, 92, 92);
}
if (pressed) {
if (primary) {
bg = RGB(40, 84, 56);
} else if (cancel) {
bg = RGB(92, 50, 50);
} else {
bg = RGB(48, 54, 62);
}
}
if (disabled) {
bg = RGB(46, 49, 54);
border = RGB(72, 78, 86);
text = RGB(138, 145, 153);
}
RECT rc = dis->rcItem;
FillSolidRectColor(dis->hDC, rc, bg);
HPEN pen = CreatePen(PS_SOLID, 1, border);
HGDIOBJ old_pen = SelectObject(dis->hDC, pen);
HGDIOBJ old_brush = SelectObject(dis->hDC, GetStockObject(NULL_BRUSH));
Rectangle(dis->hDC, rc.left, rc.top, rc.right, rc.bottom);
SelectObject(dis->hDC, old_brush);
SelectObject(dis->hDC, old_pen);
DeleteObject(pen);
std::wstring text_value = GetWindowTextCopy(dis->hwndItem);
RECT text_rc = rc;
InflateRect(&text_rc, -8, -2);
if (pressed && !disabled) {
OffsetRect(&text_rc, 1, 1);
}
HFONT font = reinterpret_cast<HFONT>(SendMessageW(dis->hwndItem, WM_GETFONT, 0, 0));
HGDIOBJ old_font = nullptr;
if (font != nullptr) {
old_font = SelectObject(dis->hDC, font);
}
SetBkMode(dis->hDC, TRANSPARENT);
SetTextColor(dis->hDC, text);
DrawTextW(dis->hDC, text_value.c_str(), -1, &text_rc,
DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX);
if (old_font != nullptr) {
SelectObject(dis->hDC, old_font);
}
}
LRESULT CALLBACK OverlayNoActivateButtonSubclassProc(HWND hwnd, UINT msg, WPARAM wparam,
LPARAM lparam, UINT_PTR subclass_id,
DWORD_PTR ref_data) {
auto point_in_client = [](HWND target, LPARAM pos_lparam) -> bool {
RECT rc = {};
if (!GetClientRect(target, &rc)) {
return false;
}
POINT pt = {static_cast<LONG>(static_cast<short>(LOWORD(pos_lparam))),
static_cast<LONG>(static_cast<short>(HIWORD(pos_lparam)))};
return PtInRect(&rc, pt) != FALSE;
};
switch (msg) {
case WM_MOUSEACTIVATE:
return MA_NOACTIVATE;
case WM_SETFOCUS:
// Prevent focus transfer to child buttons in fullscreen mode.
return 0;
case WM_LBUTTONDOWN:
SetCapture(hwnd);
SendMessageW(hwnd, BM_SETSTATE, TRUE, 0);
return 0;
case WM_MOUSEMOVE:
if (GetCapture() == hwnd) {
SendMessageW(hwnd, BM_SETSTATE, point_in_client(hwnd, lparam) ? TRUE : FALSE, 0);
return 0;
}
break;
case WM_LBUTTONUP:
if (GetCapture() == hwnd) {
const bool should_click = point_in_client(hwnd, lparam);
SendMessageW(hwnd, BM_SETSTATE, FALSE, 0);
ReleaseCapture();
if (should_click) {
const int control_id = GetDlgCtrlID(hwnd);
HWND parent = GetParent(hwnd);
if (parent != nullptr && control_id != 0) {
SendMessageW(parent, WM_COMMAND, MAKEWPARAM(control_id, BN_CLICKED),
reinterpret_cast<LPARAM>(hwnd));
}
}
return 0;
}
break;
case WM_CAPTURECHANGED:
SendMessageW(hwnd, BM_SETSTATE, FALSE, 0);
return 0;
default:
break;
}
return DefSubclassProc(hwnd, msg, wparam, lparam);
}