-
Notifications
You must be signed in to change notification settings - Fork 17
/
UserInterface.cpp
1457 lines (1252 loc) · 60.5 KB
/
UserInterface.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
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
//Everything related to user interface is inside this file...
#include <type_traits>
#include <utility>
#include <algorithm>
#include <tuple>
#include <array>
#include <vector>
#include <string>
#include <map>
#include <optional>
#include <cstdio>
#include <locale>
#include <Windows.h>
#include <CommCtrl.h>
#include <ShellApi.h>
#include "UserInterface.hpp"
#include "WindowsWrapper.hpp"
#include "ReplaysAndMods.hpp"
#include "resource.h"
using namespace Windows;
using namespace MyCSF;
enum ID {
errorMessage = 1,
captionString,
splashScreen,
//buttons
playGame,
checkForUpdates,
setLanguage,
gameBrowser,
readMe,
visitEAWebsite,
technicalSupport,
deauthorize,
quit,
about,
//
commandLine,
//set language window
setLanguageDescription,
setLanguageList,
setLanguageOK,
setLanguageCancel,
//game browser window
gameBrowserTabs,
gameBrowserLaunchGame,
gameBrowserCancel,
//game browser mod window
mods,
modList,
modListModName,
modListModVersion,
modFolder,
//game browser replay window
replays,
replayList,
replayListReplayName,
replayListModName,
replayListGameVersion,
replayListDate,
getReplayDescriptionBox,
replayMatchInformation,
replayMap,
replayNumberOfPlayers,
replayDescription,
fixReplay,
fixReplayWarning,
fixReplaySucceeded,
fixReplayFailed,
replayFolder,
//launcher / game browser launching game (replay)
replayCantBePlayed,
replayCantBeParsedText,
replayDontHaveCommentator,
replayNeedsToBeFixed,
replayNeedsToBeFixedText,
webSiteLink,
eaSupportURL,
useOriginalRA3Title,
useOriginalRA3ToUpdate,
useOriginalRA3ToDeauthorize,
aboutText,
resourceAuthors,
gameVersionNotFound,
noModsFound,
multipleModsFound,
};
const std::wstring& getText(const LanguageData& data, ID id) {
static auto labels = std::map<ID, std::wstring> {
{errorMessage, L"RA3BarLauncher:ErrorMessage"},
{captionString, L"Launcher:Caption"},
{splashScreen, L"RA3BarLauncher:ClickSplashScreen"},
{playGame, L"Launcher:Play"},
{checkForUpdates, L"Launcher:CheckForUpdates"},
{setLanguage, L"Launcher:SelectLanguage"},
{gameBrowser, L"Launcher:ReplayBrowser"},
{readMe, L"Launcher:Readme"},
{visitEAWebsite, L"Launcher:Website"},
{technicalSupport, L"Launcher:TechnicalSupport"},
{deauthorize, L"Launcher:DeAuthorize"},
{quit, L"Launcher:Quit"},
{about, L"RA3BarLauncher:About"},
{commandLine, L"RA3BarLauncher:CommandLine"},
{setLanguageDescription, L"Launcher:SelectLanguageText"},
{setLanguageOK, L"Dialog:OK"},
{setLanguageCancel, L"Dialog:Cancel"},
{gameBrowserLaunchGame, L"REPLAYBROWSER:WATCHREPLAY"},
{gameBrowserCancel, L"Dialog:Cancel"},
{mods, L"LAUNCHER:MODTAB"},
{modListModName, L"MODBROWSER:NAMECOLUMN"},
{modListModVersion, L"MODBROWSER:VERSIONCOLUMN"},
{modFolder, L"RA3BarLauncher:OpenModFolder"},
{replays, L"LAUNCHER:REPLAYTAB"},
{replayListReplayName, L"REPLAYBROWSER:NAMECOLUMN"},
{replayListModName, L"REPLAYBROWSER:MODCOLUMN"},
{replayListGameVersion, L"REPLAYBROWSER:VERSIONCOLUMN"},
{replayListDate, L"REPLAYBROWSER:DATECOLUMN"},
{replayMatchInformation, L"REPLAYBROWSER:MATCHINFO"},
{replayMap, L"REPLAYBROWSER:MAP"},
{replayNumberOfPlayers, L"REPLAYBROWSER:NUMPLAYERS"},
{replayDescription, L"REPLAYBROWSER:DESCRIPTION"},
{fixReplay, L"RA3BarLauncher:FixReplay"},
{fixReplayWarning, L"RA3BarLauncher:FixReplayWillReplaceOriginal"},
{fixReplaySucceeded, L"RA3BarLauncher:FixReplaySuccess"},
{fixReplayFailed, L"RA3BarLauncher:FixReplayFailure"},
{replayFolder, L"RA3BarLauncher:OpenReplayFolder"},
{webSiteLink, L"Launcher:URL"},
{eaSupportURL, L"RA3BarLauncher:EASupportWebsite"},
{useOriginalRA3Title, L"RA3BarLauncher:NeedOriginalLauncher"},
{useOriginalRA3ToUpdate, L"RA3BarLauncher:UpdateNotSupported"},
{useOriginalRA3ToDeauthorize, L"RA3BarLauncher:DeauthorizeNotSupported"},
{replayCantBePlayed, L"RA3BarLauncher:ReplayCannotBePlayed"},
{replayCantBeParsedText, L"RA3BarLauncher:ReplayCannotBeParsed"},
{replayDontHaveCommentator, L"RA3BarLauncher:ReplayDoesNotHaveCommentator"},
{replayNeedsToBeFixed, L"RA3BarLauncher:ReplayNeedsToBeFixed"},
{replayNeedsToBeFixedText, L"RA3BarLauncher:ReplayNeedsToBeFixedText"},
{aboutText, L"RA3BarLauncher:AboutText"},
{resourceAuthors, L"RA3BarLauncher:ResourceAuthors"},
{gameVersionNotFound, L"Launcher:CantFindVersionN_InstallPatches"},
{noModsFound, L"REPLAYBROWSER:MODNOTINSTALLED"},
{multipleModsFound, L"RA3BarLauncher:ReplayModAmbiguity"},
};
auto loadMyCSF = [](HMODULE module, LPCWSTR resource, LPCWSTR type) {
auto data = loadBinaryDataResource<char>(module, resource, type);
return loadCSFStrings(std::begin(data), std::end(data));
};
static auto myEnglish = loadMyCSF(GetModuleHandle(nullptr), MAKEINTRESOURCEW(BUILTIN_ENGLISH), RT_RCDATA);
static auto myChinese = loadMyCSF(GetModuleHandle(nullptr), MAKEINTRESOURCEW(BUILTIN_CHINESE), RT_RCDATA);
auto currentMyMap = &myEnglish;
auto chinese = std::wstring_view{L"chinese"};
if(insensitiveSearch(std::begin(data.languageName), std::end(data.languageName),
std::begin(chinese), std::end(chinese)) != std::end(data.languageName)) {
currentMyMap = &myChinese;
}
const auto& label = labels.at(id);
auto string = data.table.find(label);
if(string == data.table.end()) {
string = currentMyMap->find(label);
if(string == currentMyMap->end()) {
static auto error = std::wstring{L"<ERROR NO CSFSTRING>"};
return error;
}
}
return string->second;
}
const std::wstring& getLanguageName(const LanguageData& data, const std::wstring& languageName) {
auto languageTag = L"LanguageName:" + languageName;
for(auto& character : languageTag) {
character = std::tolower(character, std::locale::classic());
}
auto string = data.table.find(languageTag);
if(string == data.table.end()) {
static auto error = std::wstring{L"<ERROR LANGUAGE NAME>"};
return error;
}
return string->second;
}
void displayErrorMessage(const std::exception& error, const LanguageData& languageData) {
auto message = getText(languageData, errorMessage);
message += L"\r\n";
message += toWide(error.what());
MessageBoxW(nullptr,
message.c_str(),
getText(languageData, captionString).c_str(),
MB_ICONERROR|MB_OK);
}
void notifyCantUpdate(const LanguageData& languageData) {
MessageBoxW(nullptr,
getText(languageData, useOriginalRA3ToUpdate).c_str(),
getText(languageData, useOriginalRA3Title).c_str(),
MB_ICONINFORMATION|MB_OK);
}
void notifyIsNotReplay(const LanguageData& languageData) {
MessageBoxW(nullptr,
getText(languageData, replayCantBeParsedText).c_str(),
getText(languageData, replayCantBePlayed).c_str(),
MB_ICONINFORMATION|MB_OK);
}
bool askUserIfFixReplay(const std::wstring& replayName, const LanguageData& languageData) {
auto text = getText(languageData, replayNeedsToBeFixedText) + L"\r\n"
+ getText(languageData, fixReplayWarning);
auto answer = MessageBoxW(nullptr,
text.c_str(),
getText(languageData, replayCantBePlayed).c_str(),
MB_ICONWARNING|MB_YESNO);
return answer == IDYES;
}
void notifyFixReplaySucceeded(const LanguageData& languageData) {
MessageBoxW(nullptr,
getText(languageData, fixReplaySucceeded).c_str(),
getText(languageData, fixReplay).c_str(),
MB_ICONINFORMATION|MB_OK);
}
void notifyFixReplayFailed(const std::exception& error, const LanguageData& languageData) {
MessageBoxW(nullptr,
(getText(languageData, fixReplayFailed) + L"\r\n" + toWide(error.what())).c_str(),
getText(languageData, fixReplay).c_str(),
MB_ICONERROR|MB_OK);
}
void notifyNoCommentatorAvailable(const LanguageData& languageData) {
MessageBoxW(nullptr,
getText(languageData, replayDontHaveCommentator).c_str(),
getText(languageData, replayCantBePlayed).c_str(),
MB_ICONINFORMATION|MB_OK);
}
void notifyGameVersionNotFound(const std::wstring& gameVersion, const LanguageData& languageData) {
constexpr auto placeholder = std::wstring_view{L"%s"};
auto message = getText(languageData, gameVersionNotFound);
message.replace(message.find(placeholder), placeholder.size(), gameVersion);
MessageBoxW(nullptr, message.c_str(), getText(languageData, captionString).c_str(), MB_ICONERROR|MB_OK);
}
void notifyModNotFound(const LanguageData& languageData) {
MessageBoxW(nullptr,
getText(languageData, noModsFound).c_str(),
getText(languageData, captionString).c_str(),
MB_ICONINFORMATION|MB_OK);
}
void notifyReplayModNotFound(const LanguageData& languageData) {
MessageBoxW(nullptr,
getText(languageData, noModsFound).c_str(),
getText(languageData, replayCantBePlayed).c_str(),
MB_ICONINFORMATION|MB_OK);
}
void notifyReplayModAmbiguity(const LanguageData& languageData) {
MessageBoxW(nullptr,
getText(languageData, multipleModsFound).c_str(),
getText(languageData, replayCantBePlayed).c_str(),
MB_ICONINFORMATION|MB_OK);
}
DWORD colorMultiply (DWORD color, std::uint8_t multiplier) {
auto result = DWORD{};
for(auto i = 0; i < 3; ++i) {
auto component = ((color >> (i * 8)) bitand 0xFFu) / static_cast<double>(0xFFu);
auto finalComponent = static_cast<std::uint8_t>(component * multiplier);
result = result bitor (finalComponent << (i * 8));
}
return result;
}
LanguageData getNewLanguage(HWND controlCenter, const std::wstring& ra3Path, HICON icon, const LanguageData& languageData);
std::optional<LaunchOptions> runGameBrowser(HWND controlCenter, const std::wstring& ra3Path, HICON icon, const LanguageData& languageData);
void aboutWindow(HWND parent, HICON icon, const LanguageData& languageData);
void centerDialogBox (HWND parent, HWND dialogBox, UINT additionalFlags = 0) {
auto parentRect = getWindowRect(parent);
auto dialogBoxRect = getWindowRect(dialogBox);
auto [left, top, right, bottom] = getWindowRect(GetDesktopWindow());
auto halfWidth = (rectWidth(parentRect) - rectWidth(dialogBoxRect)) / 2;
auto halfHeight = (rectHeight(parentRect) - rectHeight(dialogBoxRect)) / 2;
auto [x, y] = std::pair{parentRect.left + halfWidth, parentRect.top + halfHeight};
if(x < left) x = left;
if(y + rectHeight(dialogBoxRect) > bottom) y = bottom - rectHeight(dialogBoxRect);
if(x + rectWidth(dialogBoxRect) > right) x = right - rectWidth(dialogBoxRect);
if(y < top) y = top;
SetWindowPos(dialogBox, nullptr, x, y, rectWidth(dialogBoxRect), rectHeight(dialogBoxRect),
SWP_NOZORDER|additionalFlags) >> checkWin32Result("SetWindowPos", errorValue, false);
}
void myBeginDialog(HWND dialogBox, const std::wstring& title, HICON icon, HWND center, DWORD commonControls, RECT clientArea) {
auto controlsToBeInitialized = INITCOMMONCONTROLSEX{ sizeof(INITCOMMONCONTROLSEX), commonControls };
InitCommonControlsEx(&controlsToBeInitialized) >> checkWin32Result("InitCommonControlsEx", errorValue, FALSE);
auto realSize = setWindowRectByClientRect(dialogBox, rectWidth(clientArea), rectHeight(clientArea));
SetWindowPos(dialogBox, nullptr, 0, 0, rectWidth(realSize), rectHeight(realSize), SWP_NOMOVE|SWP_NOZORDER)
>> checkWin32Result("SetWindowPos", errorValue, false);
centerDialogBox(center, dialogBox);
SetWindowTextW(dialogBox, title.c_str()) >> checkWin32Result("SetWindowTextW", errorValue, false);
SendMessageW(dialogBox, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(icon));
SendMessageW(dialogBox, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(icon));
}
FontHandle getNormalFont(std::optional<int> fontHeight = std::nullopt) {
auto metrics = NONCLIENTMETRICSW{0};
metrics.cbSize = sizeof(NONCLIENTMETRICSW);
SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICSW), &metrics, 0)
>> checkWin32Result("SystemParametersInfoW", errorValue, FALSE);
auto& logicalFont = metrics.lfMessageFont;
if(fontHeight.has_value()) {
logicalFont.lfHeight = fontHeight.value();
logicalFont.lfWidth = 0;
}
return createFontIndirect(logicalFont);
}
int getRealFontHeight(HDC deviceContext, HFONT font) {
auto textMetrics = TEXTMETRIC{};
auto previouslySelected = selectObject(deviceContext, font);
GetTextMetrics(deviceContext, &textMetrics) >> checkWin32Result("GetTextMetrics", errorValue, false);
return textMetrics.tmHeight;
}
FontHandle getAdjustedNormalFont(HDC deviceContext, int height) {
auto adjustedHeight = height * height / getRealFontHeight(deviceContext, getNormalFont(height).get());
return getNormalFont(adjustedHeight);
}
BitmapHandle loadImageFromLauncherPath(const std::wstring& ra3Path, std::wstring_view item, const RECT& imageRect) {
return loadImage<BitmapHandle>((ra3Path + L"Launcher\\").append(item.data(), item.size()), {rectWidth(imageRect), rectHeight(imageRect)});
};
template<typename ValueGetter, typename... Alternatives>
typename std::conditional_t<std::is_invocable_v<ValueGetter>,
std::invoke_result<ValueGetter>,
std::enable_if<not std::is_invocable_v<ValueGetter>, ValueGetter>
>::type
tryWith(ValueGetter&& valueGetter, Alternatives&&... alternatives) {
try {
if constexpr(std::is_invocable_v<ValueGetter>) {
return valueGetter();
}
else {
return std::forward<ValueGetter>(valueGetter);
}
}
catch(...) {
if constexpr(sizeof...(alternatives) == 0) {
throw std::runtime_error("Alternatives exhausted.");
}
else {
return tryWith(std::forward<Alternatives>(alternatives)...);
}
}
}
SplashScreenResult displaySplashScreen(const std::wstring& ra3Path, const LanguageData& languageData) {
static constexpr auto splashRect = RECT{0, 0, 640, 480};
static constexpr auto noticeBarRect = RECT{
0,
rectHeight(splashRect) * 75 / 100,
rectWidth(splashRect),
rectHeight(splashRect) * 80 / 100
};
constexpr auto timerInterval = 30;
constexpr auto velocity = 1;
constexpr auto lifeSpan = rectWidth(splashRect) * 3;
constexpr auto virtualWidth = rectWidth(splashRect) * 2;
auto icon = tryWith([&ra3Path] { return loadImage<IconHandle>(ra3Path + L"ra3.ico"); },
[] { return loadImage<IconHandle>(GetModuleHandle(nullptr), DEFAULT_ICON); });
auto backgroundBrush = BrushHandle{nullptr};
auto backgroundBrushWithText = BrushHandle{nullptr};
auto lastTickCount = GetTickCount();
auto edge = LONG{0};
auto handlers = ModalDialogBox::HandlerTable{};
auto initializeBrushes = [&ra3Path, &languageData, &backgroundBrush, &backgroundBrushWithText](HDC deviceContext) {
using std::bind;
using std::ref;
auto backgroundLoader = bind(loadImageFromLauncherPath, ref(ra3Path), std::placeholders::_1, ref(splashRect));
auto background = tryWith(bind(backgroundLoader, languageData.languageName + L"_splash.bmp"),
bind(backgroundLoader, L"splash.bmp"),
createCompatibleBitmap(deviceContext, rectWidth(splashRect), rectHeight(splashRect)));
backgroundBrush = createPatternBrush(background.get());
auto bitmapBits = getBitmapBits(deviceContext, background.get(), {{rectWidth(splashRect), -rectHeight(splashRect)}});
for(auto y = noticeBarRect.top; y < noticeBarRect.bottom; ++y) {
auto row = y * rectWidth(splashRect);
for(auto column = noticeBarRect.left; column < noticeBarRect.right; ++column) {
auto& pixel = bitmapBits.buffer.at(row + column);
pixel = colorMultiply(pixel, 0x7F);
}
}
setBitmapBits(deviceContext, background.get(), bitmapBits);
{
auto compatibleContext = createCompatibleDeviceContext(deviceContext);
auto previousBitmap = selectObject(compatibleContext.get(), background.get());
auto font = getAdjustedNormalFont(compatibleContext.get(), rectHeight(noticeBarRect));
auto previousFont = selectObject(compatibleContext.get(), font.get());
auto previousMode = setBackgroundMode(compatibleContext.get(), TRANSPARENT);
auto previousTextColor = setTextColor(compatibleContext.get(), RGB(255, 255, 255));
auto textRect = noticeBarRect;
DrawTextW(compatibleContext.get(), getText(languageData, splashScreen).c_str(), -1, &textRect, DT_CENTER)
>> checkWin32Result("DrawTextW", errorValue, 0);
}
backgroundBrushWithText = createPatternBrush(background.get());
};
handlers[WM_INITDIALOG] = [&languageData, &icon, &lastTickCount, initializeBrushes](HWND window, WPARAM, LPARAM) {
myBeginDialog(window, getText(languageData, captionString), icon.get(),
GetDesktopWindow(), ICC_STANDARD_CLASSES, splashRect);
initializeBrushes(getDeviceContext(window)->context);
SetTimer(window, splashScreen, timerInterval, nullptr) >> checkWin32Result("SetTimer", errorValue, false);
return TRUE;
};
handlers[WM_TIMER] = [&lastTickCount, &edge](HWND window, WPARAM timerID, LPARAM) {
if(timerID == splashScreen) {
auto oldTickCount = lastTickCount;
lastTickCount = GetTickCount();
edge += (lastTickCount - oldTickCount) * velocity;
if(edge > lifeSpan) {
KillTimer(window, timerID) >> checkWin32Result("KillTimer", errorValue, false);
EndDialog(window, static_cast<INT_PTR>(SplashScreenResult::normal)) >> checkWin32Result("EndDialog", errorValue, false);
}
InvalidateRect(window, ¬iceBarRect, false) >> checkWin32Result("InvalidateRect", errorValue, false);
return TRUE;
}
return FALSE;
};
handlers[WM_LBUTTONDOWN] = [](HWND window, WPARAM, LPARAM) {
EndDialog(window, static_cast<INT_PTR>(SplashScreenResult::clicked)) >> checkWin32Result("EndDialog", errorValue, false);
return TRUE;
};
auto drawer = [&backgroundBrush, &backgroundBrushWithText, &edge](HDC context, const RECT& dirtyRect) {
auto textRect = dirtyRect;
textRect.left = std::clamp(edge, dirtyRect.left, dirtyRect.right);
textRect.right = std::clamp(edge - virtualWidth, dirtyRect.left, dirtyRect.right);
auto remainedLeft = dirtyRect;
auto remainedRight = dirtyRect;
remainedLeft.right = textRect.left;
remainedRight.left = textRect.right;
FillRect(context, &remainedLeft, backgroundBrush.get()) >> checkWin32Result("FillRect", errorValue, false);
FillRect(context, &remainedRight, backgroundBrush.get()) >> checkWin32Result("FillRect", errorValue, false);
FillRect(context, &textRect, backgroundBrushWithText.get()) >> checkWin32Result("FillRect", errorValue, false);
};
handlers[WM_PAINT] = [drawer](HWND window, WPARAM, LPARAM) {
auto paint = beginPaint(window);
auto dirtyRect = paint->paintStruct.rcPaint;
if(paint->paintStruct.fErase) {
dirtyRect = splashRect;
}
auto memoryContext = createCompatibleDeviceContext(paint->context);
auto compatibleBitmap = createCompatibleBitmap(paint->context, rectWidth(splashRect), rectHeight(splashRect));
auto previousMemoryBitmap = selectObject(memoryContext.get(), compatibleBitmap.get());
drawer(memoryContext.get(), dirtyRect);
auto [x, y, width, height] = std::tuple{dirtyRect.left, dirtyRect.top, rectWidth(dirtyRect), rectHeight(dirtyRect)};
BitBlt(paint->context, x, y, width, height, memoryContext.get(), x, y, SRCCOPY)
>> checkWin32Result("BitBlt", errorValue, false);
return TRUE;
};
return static_cast<SplashScreenResult>(modalDialogBox(handlers, WS_VISIBLE|WS_POPUP, 0));
}
std::optional<LaunchOptions> runControlCenter(const std::wstring& ra3Path, const std::wstring& userCommandLine, LanguageData& languageData, HBITMAP customBackground) {
constexpr auto firstLineStart = 67;
constexpr auto secondLineStart = 139;
constexpr auto commandLineY = 470;
constexpr auto commandLineWidth = 800 - 2 * firstLineStart;
constexpr auto commandLineHeight = 20;
constexpr auto commandLineTitleY = commandLineY - commandLineHeight - 2;
constexpr auto buttonWidth = 121;
constexpr auto buttonHeight = 34;
constexpr auto buttonPadding = 12;
static constexpr auto clientArea = RECT{0, 0, 800, 600};
static constexpr auto allButtons = {
playGame, checkForUpdates, setLanguage, gameBrowser, readMe,
visitEAWebsite, technicalSupport, deauthorize, quit, about,
};
auto icon = tryWith([&ra3Path] { return loadImage<IconHandle>(ra3Path + L"ra3.ico"); },
[] { return loadImage<IconHandle>(GetModuleHandle(nullptr), DEFAULT_ICON); });
auto backgroundBrushes = std::array<BrushHandle, 2> {};
const auto& [mainBackground, commandLineBackground] = backgroundBrushes;
auto buttonBrushes = std::array<BrushHandle, 3> {};
auto buttonFont = getNormalFont();
constexpr auto buttonTextFormats = DT_CENTER|DT_SINGLELINE|DT_VCENTER;
auto commandLineFont = getNormalFont(commandLineHeight);
auto handlers = ModalDialogBox::HandlerTable{};
auto returnValue = std::optional<LaunchOptions> {};
auto setUpBrushes = [&ra3Path, &languageData, customBackground, &backgroundBrushes, &buttonBrushes](HDC deviceContext) {
using std::bind;
using std::ref;
auto backgroundLoader = bind(loadImageFromLauncherPath, ref(ra3Path), std::placeholders::_1, ref(clientArea));
auto background = tryWith(bind(backgroundLoader, languageData.languageName + L"_cnc.bmp"),
bind(backgroundLoader, L"cnc.bmp"),
createCompatibleBitmap(deviceContext, rectWidth(clientArea), rectHeight(clientArea)));
auto rawBackground = customBackground ? customBackground : background.get();
backgroundBrushes[0] = createPatternBrush(rawBackground);
auto bitmapBits = getBitmapBits(deviceContext, rawBackground, {{rectWidth(clientArea), -rectHeight(clientArea)}});
for(auto y = commandLineY; y < commandLineY + commandLineHeight; ++y) {
auto row = y * rectWidth(clientArea);
for(auto column = firstLineStart; column < firstLineStart + commandLineWidth; ++column) {
auto sp = (y - commandLineY) * rectWidth(clientArea) + (column - firstLineStart);
bitmapBits.buffer.at(sp) = colorMultiply(bitmapBits.buffer.at(row + column), 150);
}
}
auto duplicate = createCompatibleBitmap(deviceContext, rectWidth(clientArea), rectHeight(clientArea));
setBitmapBits(deviceContext, duplicate.get(), bitmapBits);
backgroundBrushes[1] = createPatternBrush(duplicate.get());
auto maxDistance = 10;
auto greyButton = std::vector<std::uint8_t> {};
greyButton.resize(buttonWidth * buttonHeight);
for(auto y = 0; y < buttonHeight; ++y) {
auto row = y * buttonWidth;
for(auto column = 0; column < buttonWidth; ++column) {
auto [min, max] = std::minmax({std::min<double>(column, buttonWidth - column), std::min<double>(y, buttonHeight - y)});
min = std::clamp(min / maxDistance, 0.0, 1.0);
max = std::clamp(max / maxDistance, 0.0, 1.0);
auto value = std::clamp(min + (2.0 - max) * max - 1.0, 0.0, 1.0);
greyButton.at(row + column) = std::clamp<int>(255 * ((2.0 * value - 3.0) * value * value + 1.0), 0, 255);
}
}
auto getButtonBrush = [deviceContext, &greyButton](DWORD backgroundColor, DWORD borderColor) {
auto buttonBitmap = createCompatibleBitmap(deviceContext, buttonWidth, buttonHeight);
auto bitmapBits = getBitmapBits(deviceContext, buttonBitmap.get());
for(auto i = std::size_t{0}; i < bitmapBits.buffer.size(); ++i) {
auto& color = bitmapBits.buffer.at(i);
auto horizontalBorder = (i < buttonWidth) or (i / buttonWidth == buttonHeight - 1);
auto verticalBorder = (i % buttonWidth == 0) or (i % buttonWidth == buttonWidth - 1);
if(verticalBorder and horizontalBorder) {
color = colorMultiply(borderColor, 0x7F);
}
else if(verticalBorder or horizontalBorder) {
color = colorMultiply(borderColor, 0xFF);
}
else {
color = colorMultiply(backgroundColor, greyButton.at(i));
}
}
setBitmapBits(deviceContext, buttonBitmap.get(), bitmapBits);
return createPatternBrush(buttonBitmap.get());
};
buttonBrushes[0] = getButtonBrush(RGB(12, 29, 129), RGB(0, 118, 230));
buttonBrushes[1] = getButtonBrush(RGB(0, 230, 222), RGB(230, 230, 230));
buttonBrushes[2] = getButtonBrush(RGB(0, 157, 230), RGB(0, 118, 230));
};
auto initializeCommandLineWindow = [&userCommandLine, &commandLineFont](HWND window) {
auto commandLineWindow = createControl(window, commandLine, WC_EDITW, userCommandLine.c_str(),
WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL, WS_EX_TRANSPARENT,
firstLineStart, commandLineY, commandLineWidth, commandLineHeight).release();
commandLineFont = getAdjustedNormalFont(getDeviceContext(window)->context, commandLineHeight);
SendMessageW(commandLineWindow, WM_SETFONT, reinterpret_cast<WPARAM>(commandLineFont.get()), true);
};
auto adjustButtonFont = [&languageData, &buttonFont](HWND window) {
auto getTextRect = [&buttonFont](HDC deviceContext, std::wstring_view text) {
auto textRect = RECT{0, 0, buttonWidth, buttonHeight};
auto previousFont = selectObject(deviceContext, buttonFont.get());
DrawTextW(deviceContext, text.data(), text.size(), &textRect, buttonTextFormats|DT_CALCRECT)
>> checkWin32Result("DrawText", errorValue, 0);
return textRect;
};
buttonFont = getNormalFont();
for(auto id : allButtons) {
auto button = getControlByID(window, id);
InvalidateRect(button, nullptr, false) >> checkWin32Result("InvalidateRect", errorValue, false);
auto context = getDeviceContext(button);
const auto& text = getText(languageData, id);
auto textRect = getTextRect(context->context, text);
if(rectWidth(textRect) <= buttonWidth) {
continue;
}
auto realHeight = getRealFontHeight(context->context, buttonFont.get());
auto adjustedHeight = realHeight * buttonWidth / rectWidth(textRect);
buttonFont = getAdjustedNormalFont(context->context, adjustedHeight);
}
};
handlers[WM_INITDIALOG] = [&languageData, &icon, setUpBrushes, initializeCommandLineWindow, adjustButtonFont](HWND window, WPARAM, LPARAM) {
myBeginDialog(window, getText(languageData, captionString), icon.get(),
GetDesktopWindow(), ICC_STANDARD_CLASSES, clientArea);
setUpBrushes(getDeviceContext(window)->context);
initializeCommandLineWindow(window);
auto currentX = 0;
auto nextX = [¤tX] { return currentX += buttonWidth + buttonPadding; };
currentX = firstLineStart - (buttonWidth + buttonPadding);
for(auto id : {playGame, checkForUpdates, setLanguage, gameBrowser, readMe}) {
createControl(window, id, WC_BUTTONW, {}, WS_VISIBLE, 0,
nextX(), 520, buttonWidth, buttonHeight).release();
}
currentX = secondLineStart - (buttonWidth + buttonPadding);
for(auto id : {visitEAWebsite, technicalSupport, deauthorize, quit}) {
createControl(window, id, WC_BUTTONW, {}, WS_VISIBLE, 0,
nextX(), 562, buttonWidth, buttonHeight).release();
}
createControl(window, about, WC_BUTTONW, {}, WS_VISIBLE, 0,
clientArea.right - buttonWidth - buttonPadding, clientArea.top + buttonPadding,
buttonWidth, buttonHeight).release();
adjustButtonFont(window);
return TRUE;
};
handlers[WM_CTLCOLOREDIT] = [&commandLineBackground](HWND window, WPARAM hdcAddress, LPARAM childAddress) -> INT_PTR {
auto childWindow = reinterpret_cast<HWND>(childAddress);
if(getControlID(childWindow) != commandLine) {
return FALSE;
}
auto childDC = reinterpret_cast<HDC>(hdcAddress);
setBackgroundMode(childDC, TRANSPARENT).release();
setTextColor(childDC, RGB(255, 255, 255)).release();
return reinterpret_cast<INT_PTR>(commandLineBackground.get()); //per https://msdn.microsoft.com/en-us/library/windows/desktop/bb761691(v=vs.85).aspx
};
handlers[WM_PAINT] = [&languageData, &mainBackground, &commandLineFont](HWND window, WPARAM, LPARAM) {
auto paint = beginPaint(window);
auto previousMode = setBackgroundMode(paint->context, TRANSPARENT);
auto previousTextColor = setTextColor(paint->context, RGB(255, 255, 255));
auto previouslySelected = selectObject(paint->context, commandLineFont.get());
FillRect(paint->context, &paint->paintStruct.rcPaint, mainBackground.get()) >> checkWin32Result("FillRect", errorValue, false);
TextOutW(paint->context, firstLineStart, commandLineTitleY,
getText(languageData, commandLine).c_str(), getText(languageData, commandLine).size()) >> checkWin32Result("TextOut", errorValue, false);
return TRUE;
};
auto getCommandLines = [](HWND controlCenter) {
auto commandLineWindow = getControlByID(controlCenter, commandLine);
auto length = GetWindowTextLengthW(commandLineWindow);
auto windowString = std::wstring{static_cast<std::size_t>(length + 1), L'\0', std::wstring::allocator_type{}};
auto realLength = GetWindowTextW(commandLineWindow, windowString.data(), length + 1);
windowString.resize(realLength);
return windowString;
};
auto endControlCenter = [&returnValue](HWND window, std::optional<LaunchOptions> launchOptions) {
returnValue = std::move(launchOptions);
EndDialog(window, 0) >> checkWin32Result("EndDialog", errorValue, false);
};
handlers[WM_COMMAND] = [&ra3Path, &languageData, &icon, setUpBrushes, adjustButtonFont, getCommandLines, endControlCenter](HWND window, WPARAM codeAndIdentifier, LPARAM childWindow) {
auto notificationCode = HIWORD(codeAndIdentifier);
auto identifier = LOWORD(codeAndIdentifier);
if(notificationCode != BN_CLICKED) {
return FALSE;
}
switch(identifier) {
case playGame: {
endControlCenter(window, LaunchOptions{LaunchOptions::noFile, {}, getCommandLines(window)});
break;
}
case checkForUpdates: {
notifyCantUpdate(languageData);
break;
}
case setLanguage: {
languageData = getNewLanguage(window, ra3Path, icon.get(), languageData);
SetWindowTextW(window, getText(languageData, captionString).c_str()) >> checkWin32Result("SetWindowTextW", successValue, true);
setUpBrushes(getDeviceContext(window)->context);
adjustButtonFont(window);
InvalidateRect(window, nullptr, false) >> checkWin32Result("InvalidateRect", errorValue, false);
break;
}
case gameBrowser: {
auto launchOptions = runGameBrowser(window, ra3Path, icon.get(), languageData);
if(launchOptions.has_value()) {
launchOptions->extraCommandLine = getCommandLines(window);
endControlCenter(window, std::move(launchOptions));
}
break;
}
case readMe: {
try {
shellExecute(window, L"open", getRegistryString(getRa3RegistryKey(HKEY_LOCAL_MACHINE).get(), L"Readme"));
}
catch(...) { }
break;
}
case visitEAWebsite: {
shellExecute(window, L"open", getText(languageData, webSiteLink));
break;
}
case technicalSupport: {
shellExecute(window, L"open", getText(languageData, eaSupportURL));
break;
}
case deauthorize: {
MessageBoxW(window,
getText(languageData, useOriginalRA3ToDeauthorize).c_str(),
getText(languageData, useOriginalRA3Title).c_str(),
MB_ICONINFORMATION|MB_OK);
break;
}
case about: {
aboutWindow(window, icon.get(), languageData);
break;
}
case quit: {
endControlCenter(window, std::nullopt);
break;
}
}
return TRUE;
};
handlers[WM_NOTIFY] = [&languageData, &buttonBrushes, &buttonFont](HWND window, WPARAM, LPARAM dataHeaderAddress) {
const auto& dataHeader = *reinterpret_cast<const NMHDR*>(dataHeaderAddress);
auto id = static_cast<ID>(dataHeader.idFrom);
if(std::count(std::begin(allButtons), std::end(allButtons), id) == 0) {
return FALSE;
}
if(dataHeader.code != NM_CUSTOMDRAW) {
return FALSE;
}
const auto& customDraw = *reinterpret_cast<const NMCUSTOMDRAW*>(dataHeaderAddress);
if(customDraw.dwDrawStage != CDDS_PREPAINT) {
return FALSE;
}
const auto& buttonText = getText(languageData, id);
auto previousMode = setBackgroundMode(customDraw.hdc, TRANSPARENT);
auto previousColor = setTextColor(customDraw.hdc, RGB(255, 255, 255));
auto buttonState = SendMessageW(dataHeader.hwndFrom, BM_GETSTATE, 0, 0);
auto brush = HBRUSH{nullptr};
if(buttonState bitand BST_PUSHED) {
brush = buttonBrushes[2].get();
}
else if(buttonState bitand BST_HOT) {
brush = buttonBrushes[1].get();
}
else {
brush = buttonBrushes[0].get();
}
auto buttonRect = getClientRect(dataHeader.hwndFrom);
if(brush != nullptr) {
FillRect(customDraw.hdc, &buttonRect, brush) >> checkWin32Result("FillRect", errorValue, false);
}
auto previousFont = selectObject(customDraw.hdc, buttonFont.get());
DrawTextW(customDraw.hdc, buttonText.c_str(), buttonText.size(), &buttonRect, buttonTextFormats);
return CDRF_SKIPDEFAULT;
};
handlers[WM_CLOSE] = [endControlCenter](HWND window, WPARAM, LPARAM) {
endControlCenter(window, std::nullopt);
return TRUE;
};
modalDialogBox(handlers, WS_VISIBLE|WS_SYSMENU|WS_MINIMIZEBOX, WS_EX_OVERLAPPEDWINDOW);
return returnValue;
}
LanguageData getNewLanguage(HWND controlCenter, const std::wstring& ra3Path, HICON icon, const LanguageData& languageData) {
constexpr auto buttonWidth = 100;
constexpr auto buttonHeight = 30;
constexpr auto buttonPadding = 10;
static constexpr auto bannerRect = RECT{0, 0, 480, 100};
constexpr auto clientArea = RECT{0, 0, rectWidth(bannerRect), rectHeight(bannerRect) + 3 * buttonHeight + 2 * buttonPadding};
constexpr auto textX = buttonPadding;
constexpr auto textY = bannerRect.bottom;
constexpr auto textWidth = rectWidth(clientArea) - 2 * textX;
constexpr auto textHeight = buttonHeight;
constexpr auto listWidth = rectWidth(clientArea) * 0.75;
constexpr auto listHeight = buttonHeight;
constexpr auto listX = (rectWidth(clientArea) - listWidth) / 2;
constexpr auto listY = textY + textHeight;
constexpr auto buttonX = buttonPadding;
constexpr auto buttonY = listY + listHeight + buttonPadding;
auto bannerLoader = std::bind(loadImageFromLauncherPath, std::ref(ra3Path), std::placeholders::_1, std::ref(bannerRect));
auto banner480 = tryWith(std::bind(bannerLoader, languageData.languageName + L"_480banner.bmp"),
std::bind(bannerLoader, L"480banner.bmp"),
nullptr);
auto banner480Brush = tryWith(std::bind(createPatternBrush, banner480.get()), nullptr);
auto font = getNormalFont();
auto languages = getAllLanguages(ra3Path);
if(auto current = std::find(std::begin(languages), std::end(languages), languageData.languageName);
current != std::end(languages) and current != std::begin(languages)) {
std::swap(*current, *std::begin(languages));
}
auto handlers = ModalDialogBox::HandlerTable{};
handlers[WM_INITDIALOG] = [controlCenter, icon, &languageData, &font, &languages](HWND dialogBox, WPARAM wParam, LPARAM lParam) {
myBeginDialog(dialogBox, getText(languageData, setLanguage), icon,
controlCenter, ICC_STANDARD_CLASSES, clientArea);
auto list = createControl(dialogBox, setLanguageList, WC_COMBOBOXW, {},
WS_VISIBLE|CBS_DROPDOWNLIST, 0, listX, listY, listWidth, 0).release();
for(auto i = std::size_t{0}; i < languages.size(); ++i) {
SendMessageW(list, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(getLanguageName(languageData, languages[i]).c_str()))
>> checkWin32Result("SendMessageW CB_ADDSTRINGW", successValue, static_cast<LRESULT>(i));
}
SendMessageW(list, CB_SETCURSEL, 0, 0) >> checkWin32Result("SendMessageW CB_SETCURSEL", successValue, 0);
createControl(dialogBox, setLanguageDescription, WC_STATICW, getText(languageData, setLanguageDescription).c_str(),
WS_VISIBLE|SS_LEFT|SS_CENTERIMAGE, 0, textX, textY, textWidth, textHeight).release();
auto buttonOffset = buttonX;
for(auto id : {setLanguageOK, setLanguageCancel}) {
createControl(dialogBox, id, WC_BUTTONW, getText(languageData, id),
WS_VISIBLE, 0, buttonOffset, buttonY, buttonWidth, buttonHeight).release();
buttonOffset += buttonWidth + buttonPadding;
}
for(auto id : {setLanguageList, setLanguageDescription, setLanguageOK, setLanguageCancel}) {
SendMessageW(getControlByID(dialogBox, id), WM_SETFONT, reinterpret_cast<WPARAM>(font.get()), true);
}
return TRUE;
};
handlers[WM_PAINT] = [&languageData, &banner480Brush](HWND dialogBox, WPARAM, LPARAM lParam) {
if(banner480Brush.get() == nullptr) {
return FALSE;
}
auto painter = beginPaint(dialogBox);
FillRect(painter->context, &bannerRect, banner480Brush.get()) >> checkWin32Result("FillRect", errorValue, false);
return TRUE;
};
handlers[WM_COMMAND] = [](HWND dialogBox, WPARAM information, LPARAM childWindowHandle) {
auto notificationCode = HIWORD(information);
auto identifier = LOWORD(information);
if(notificationCode == BN_CLICKED) {
if(identifier == setLanguageOK) {
auto id = SendMessageW(getControlByID(dialogBox, setLanguageList), CB_GETCURSEL, 0, 0)
>> checkWin32Result("SendMessageW CB_GETCURSEL", errorValue, CB_ERR);
EndDialog(dialogBox, id) >> checkWin32Result("EndDialog", errorValue, false);
}
if(identifier == setLanguageCancel) {
EndDialog(dialogBox, 0) >> checkWin32Result("EndDialog", errorValue, false);
}
}
return TRUE;
};
handlers[WM_CLOSE] = [](HWND dialogBox, WPARAM, LPARAM) {
EndDialog(dialogBox, 0) >> checkWin32Result("EndDialog", errorValue, false);
return TRUE;
};
auto result = modalDialogBox(handlers, WS_VISIBLE|WS_SYSMENU, 0, controlCenter);
return loadLanguageData(ra3Path, languages.at(result));
};
template<typename IDWidthList>
WindowHandle createListView(HWND parent, ID id, const IDWidthList& columns, RECT windowRect, const LanguageData& languageData) {
auto listView = createControl(parent, id, WC_LISTVIEWW, {},
LVS_REPORT|LVS_SHOWSELALWAYS|LVS_SINGLESEL, 0,
windowRect.left, windowRect.top, rectWidth(windowRect), rectHeight(windowRect));
SendMessageW(listView.get(), LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_DOUBLEBUFFER);
auto listViewWidth = rectWidth(getClientRect(listView.get())) - GetSystemMetrics(SM_CXVSCROLL); //minus width of scroll bar
auto columnIndex = 0;
for(auto [id, widthPercent] : columns) {
auto buf = getText(languageData, id);
auto column = LVCOLUMNW{LVCF_TEXT|LVCF_WIDTH};
column.pszText = buf.data();
column.cx = widthPercent * listViewWidth;
SendMessageW(listView.get(), LVM_INSERTCOLUMNW, columnIndex, reinterpret_cast<LPARAM>(&column))
>> checkWin32Result("SendMessageW LVM_INSERTCOLUMNW", successValue, columnIndex);
columnIndex += 1;
}
return listView;
}
void replaceListView (HWND listView, std::vector<std::vector<std::wstring>> items) {
SendMessageW(listView, LVM_DELETEALLITEMS, 0, 0)
>> checkWin32Result("SendMessageW LVM_DELETEALLITEMS", errorValue, false);
for(auto index = std::size_t{0}; index < items.size(); ++index) {
for(auto subIndex = std::size_t{0}; subIndex < items[index].size(); ++subIndex) {
auto listItem = LVITEMW{LVIF_TEXT};
listItem.iItem = index;
listItem.iSubItem = subIndex;
listItem.pszText = items[index][subIndex].data();
if(listItem.iSubItem == 0) {
SendMessageW(listView, LVM_INSERTITEMW, 0, reinterpret_cast<LPARAM>(&listItem))
>> checkWin32Result("SendMessageW LVM_INSERTITEMW", errorValue, -1);
}
else {
SendMessageW(listView, LVM_SETITEMW, 0, reinterpret_cast<LPARAM>(&listItem))
>> checkWin32Result("SendMessageW LVM_INSERTITEMW", errorValue, false);
}
}
}
}
struct ReplaysAndModsData {
std::vector<std::vector<std::wstring>> replayDetailsToStrings() const {
auto allocateFormat = [](auto function, DWORD flags, const SYSTEMTIME& time) {
auto size = function(LOCALE_USER_DEFAULT, flags, &time, nullptr, nullptr, 0)
>> checkWin32Result("allocateFormat", errorValue, 0);
auto string = std::wstring{static_cast<std::size_t>(size), {}, std::wstring::allocator_type{}};
function(LOCALE_USER_DEFAULT, flags, &time, nullptr, string.data(), string.size())
>> checkWin32Result("allocateFormat", errorValue, 0);
string.erase(string.find(L'\0'));
return string;
};
auto strings = std::vector<std::vector<std::wstring>> {};
for(const auto& replay : this->replayDetails) {
auto fileTime = unixTimeToFileTime(replay.timeStamp);
auto systemTime = SYSTEMTIME{};
FileTimeToSystemTime(&fileTime, &systemTime)
>> checkWin32Result("FileTimeToSystemTime", errorValue, false);
auto localTime = SYSTEMTIME{};
SystemTimeToTzSpecificLocalTime(nullptr, &systemTime, &localTime)
>> checkWin32Result("SystemTimeToTzSpecificLocalTime", errorValue, false);
auto date = allocateFormat(GetDateFormatW, DATE_LONGDATE, localTime);
auto time = allocateFormat(GetTimeFormatW, TIME_NOSECONDS, localTime);
auto gameVersion = std::to_wstring(replay.gameVersion.first) + L'.' + std::to_wstring(replay.gameVersion.second);
strings.emplace_back(std::vector{replay.replayName, replay.modName, std::move(gameVersion)});
strings.back().emplace_back(date + L' ' + time);
}
return strings;
}