forked from GeneralsOnlineDevelopmentTeam/GameClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnlineServices_Init.cpp
More file actions
1297 lines (1075 loc) · 40.1 KB
/
Copy pathOnlineServices_Init.cpp
File metadata and controls
1297 lines (1075 loc) · 40.1 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 "GameNetwork/GeneralsOnline/NGMP_interfaces.h"
#include "GameNetwork/GeneralsOnline/HTTP/HTTPManager.h"
#include "../json.hpp"
#include "GameClient/MessageBox.h"
#include "Common/FileSystem.h"
#include "Common/file.h"
#include "Common/System/NativeFileSystem.h"
#include "realcrc.h"
#include "GameNetwork/DownloadManager.h"
#include <ws2tcpip.h>
#include "GameClient/DisplayStringManager.h"
#include "GameNetwork/NetworkInterface.h"
#include "Common/MultiplayerSettings.h"
#include "GameNetwork/GameSpyOverlay.h"
#include "GameClient/Display.h"
#include "surfaceclass.h"
#include "dx8wrapper.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "GameNetwork/GeneralsOnline/Vendor/stb_image/stb_image_write.h"
#include "GameNetwork/GeneralsOnline/Vendor/stb_image/stb_image_resize.h"
#include "GameClient/GameText.h"
#include <unordered_set>
#ifdef __APPLE__
// Generals.exe (Vanilla): 287639043
// GeneralsOnlineZH.exe (30 FPS build): 3196037691
// GeneralsOnlineZH_60.exe (60 FPS build): 1431066825
namespace {
constexpr long MAC_PARITY_FALLBACK_EXE_CRC = 1431066825; // GeneralsOnlineZH_60.exe (IEEE CRC32)
constexpr long MAC_PARITY_SHIFT_ADD_BASE = 3966796141; // GeneralsOnlineZH_60.exe (Shift-Add CRC)
}
#endif
#ifdef _WIN32
extern "C"
{
__declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
}
#endif
NGMP_OnlineServicesManager* NGMP_OnlineServicesManager::m_pOnlineServicesManager = nullptr;
std::thread::id NGMP_OnlineServicesManager::g_MainThreadID;
std::mutex NGMP_OnlineServicesManager::m_ScreenshotMutex;
std::vector<S3ScreenshotEntry> NGMP_OnlineServicesManager::m_vecGuardedSSData;
bool NGMP_OnlineServicesManager::g_bAdvancedNetworkStats;
NetworkMesh* NGMP_OnlineServicesManager::GetNetworkMesh()
{
if (m_pOnlineServicesManager != nullptr)
{
NGMP_OnlineServices_LobbyInterface* pLobbyInterface = GetInterface< NGMP_OnlineServices_LobbyInterface>();
if (pLobbyInterface != nullptr)
{
return pLobbyInterface->GetNetworkMeshForLobby();
}
}
return nullptr;
}
void NGMP_OnlineServicesManager::GetAndParseServiceConfig(std::function<void(void)> cbOnDone)
{
std::string strURI = NGMP_OnlineServicesManager::GetAPIEndpoint("ServiceConfig");
std::map<std::string, std::string> mapHeaders;
NGMP_OnlineServicesManager::GetInstance()->GetHTTPManager()->SendGETRequest(strURI.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, [=](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq)
{
try
{
if (bSuccess && statusCode == 200)
{
nlohmann::json jsonObject = nlohmann::json::parse(strBody);
m_ServiceConfig = jsonObject.get<ServiceConfig>();
}
else
{
// It's OK to fail, we'll just use the sensible defaults
NetworkLog(ELogVerbosity::LOG_RELEASE, "[NGMP] Failed to get service config, using defaults. Status code: %d", statusCode);
m_ServiceConfig = ServiceConfig();
}
}
catch (...)
{
// It's OK to fail, we'll just use the sensible defaults
NetworkLog(ELogVerbosity::LOG_RELEASE, "[NGMP] Failed to get service config, using defaults. Exception.");
m_ServiceConfig = ServiceConfig();
}
if (cbOnDone != nullptr)
{
cbOnDone();
}
});
}
void NGMP_OnlineServicesManager::CaptureScreenshotToDisk()
{
// create dirs
std::string strScreenshotsDir = std::format("{}\\GeneralsOnlineScreenshots\\", TheGlobalData->getPath_UserData().str());
if (!NativeFileSystem::exists(strScreenshotsDir))
{
NativeFileSystem::create_directory(strScreenshotsDir);
}
// calculate path
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&in_time_t), "GeneralsOnline_Screenshot_%Y-%m-%d-%H-%M-%S.jpg");
std::string strFilePath = std::format("{}\\{}", strScreenshotsDir.c_str(), ss.str().c_str());
// do UI output immediately on mainthread (if ingame)
if (TheInGameUI != nullptr)
{
UnicodeString ufileName;
ufileName.translate(AsciiString(strFilePath.c_str()));
TheInGameUI->message(TheGameText->fetch("GUI:ScreenCapture"), ufileName.str());
}
NGMP_OnlineServicesManager::CaptureScreenshot(false, [strFilePath](std::vector<unsigned char> vecBuffer)
{
if (!vecBuffer.empty())
{
// write to disk
FILE* pFile = NativeFileSystem::fopen(strFilePath, "wb");
if (pFile != nullptr) {
fwrite(vecBuffer.data(), sizeof(uint8_t), vecBuffer.size(), pFile);
fclose(pFile);
}
}
});
}
void NGMP_OnlineServicesManager::CaptureScreenshotForProbe(EScreenshotType screenshotType, std::string strURI)
{
NGMP_OnlineServicesManager* pOnlineServicesMgr = NGMP_OnlineServicesManager::GetInstance();
if (pOnlineServicesMgr != nullptr)
{
ServiceConfig& serviceConf = pOnlineServicesMgr->GetServiceConfig();
if (serviceConf.do_probes)
{
CHECK_MAIN_THREAD;
NGMP_OnlineServices_LobbyInterface* pLobbyInterface = NGMP_OnlineServicesManager::GetInterface<NGMP_OnlineServices_LobbyInterface>();
if (pLobbyInterface != nullptr)
{
NGMP_OnlineServicesManager::GetInstance()->CaptureScreenshot(true, [strURI, screenshotType](std::vector<uint8_t> vecData)
{
CHECK_WORKER_THREAD;
if (vecData.empty())
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "Screenshot capture failed, no data");
return;
}
// certain screenshots require caching for later upload when we have a valid match id, so we store them
if (screenshotType == EScreenshotType::SCREENSHOT_TYPE_LOADSCREEN)
{
NGMP_OnlineServicesManager::GetInstance()->CacheScreenshotBytes_StartMatch(vecData);
}
else if (screenshotType == EScreenshotType::SCREENSHOT_TYPE_SCORESCREEN)
{
NGMP_OnlineServicesManager::GetInstance()->CacheScreenshotBytes_EndMatch(vecData);
}
else
{
// send back to main thread for processing
// NOTE: we don't lock in the above cases because the called functions lock
std::scoped_lock<std::mutex> ssLock(m_ScreenshotMutex);
S3ScreenshotEntry newEntry;
newEntry.vecBytes = std::move(vecData);
newEntry.strSignedURI = strURI;
newEntry.screenshotType = screenshotType;
m_vecGuardedSSData.push_back(newEntry);
}
});
}
}
}
}
enum class EVersionCheckResponseResult : int
{
OK = 0,
FAILED = 1,
NEEDS_UPDATE = 2
};
struct VersionCheckResponse
{
EVersionCheckResponseResult result;
std::string patcher_name;
std::string patcher_path;
int64_t patcher_size;
NLOHMANN_DEFINE_TYPE_INTRUSIVE(VersionCheckResponse, result, patcher_name, patcher_path, patcher_size)
};
struct VersionManifestResponse
{
int64_t execrc_60;
NLOHMANN_DEFINE_TYPE_INTRUSIVE(VersionManifestResponse, execrc_60)
};
GenOnlineSettings NGMP_OnlineServicesManager::Settings;
NGMP_OnlineServicesManager::NGMP_OnlineServicesManager()
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "[NGMP] Init");
m_pOnlineServicesManager = this;
}
std::string NGMP_OnlineServicesManager::GetAPIEndpoint(const char* szEndpoint)
{
if (g_Environment == EEnvironment::DEV)
{
return std::format("https://localhost:9000/env/dev/contract/1/{}", szEndpoint);
}
else if (g_Environment == EEnvironment::TEST)
{
return std::format("https://api.playgenerals.online:2087/env/test/contract/1/{}", szEndpoint);
}
else // PROD
{
if (NGMP_OnlineServicesManager::Settings.Network_UseAlternativeEndpoint())
{
return std::format("https://api-ru.playgenerals.online/env/prod/contract/1/{}", szEndpoint);
}
else
{
return std::format("https://api.playgenerals.online/env/prod/contract/1/{}", szEndpoint);
}
}
}
void NGMP_OnlineServicesManager::AttemptLoadSteam()
{
#ifdef _WIN32
// app id for ZH
SetEnvironmentVariableA("SteamAppId", "2732960");
// load steam 32bit dll from local dir if present
HMODULE steamDll = LoadLibraryA("steam_api.dll");
if (!steamDll)
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "steam_api.dll not found. Running without Steam.");
return;
}
// Init func
typedef bool (*SteamAPI_Init_t)();
SteamAPI_Init_t SteamAPI_Init = (SteamAPI_Init_t)GetProcAddress(steamDll, "SteamAPI_InitSafe");
if (!SteamAPI_Init)
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "SteamAPI_Init not found in DLL.");
FreeLibrary(steamDll);
return;
}
// steam init
if (SteamAPI_Init())
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "SteamAPI_Init succeeded.");
}
else
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "SteamAPI_Init failed.");
}
#endif
}
void NGMP_OnlineServicesManager::CommitReplay(AsciiString absoluteReplayPath)
{
NGMP_OnlineServicesManager* pOnlineServicesMgr = NGMP_OnlineServicesManager::GetInstance();
if (pOnlineServicesMgr != nullptr)
{
ServiceConfig& serviceConf = pOnlineServicesMgr->GetServiceConfig();
if (serviceConf.do_replay_upload)
{
FILE* pFile = NativeFileSystem::fopen(absoluteReplayPath, "rb");
std::vector<unsigned char> replayData;
if (pFile)
{
fseek(pFile, 0, SEEK_END);
long fileSize = ftell(pFile);
fseek(pFile, 0, SEEK_SET);
if (fileSize > 0)
{
replayData.resize(fileSize);
fread(replayData.data(), 1, fileSize, pFile);
}
fclose(pFile);
}
// cache the data until we get an S3 URL from server
NGMP_OnlineServicesManager::GetInstance()->CacheReplayBytes(replayData);
}
}
}
void NGMP_OnlineServicesManager::WaitForScreenshotThreads()
{
std::scoped_lock<std::mutex> lock(m_mutexScreenshotThreads);
NetworkLog(ELogVerbosity::LOG_RELEASE, "[NGMP] Waiting for %d screenshot threads to complete...", (int)m_vecScreenshotThreads.size());
for (std::thread* pThread : m_vecScreenshotThreads)
{
if (pThread != nullptr && pThread->joinable())
{
pThread->join();
delete pThread;
}
}
m_vecScreenshotThreads.clear();
NetworkLog(ELogVerbosity::LOG_RELEASE, "[NGMP] All screenshot threads completed");
}
void NGMP_OnlineServicesManager::Shutdown()
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "[NGMP] OnlineServicesManager shutdown initiated");
// First, wait for all screenshot threads to complete
// This prevents race conditions where threads might still be using resources
WaitForScreenshotThreads();
// Shutdown and completely destroy WebSocket BEFORE cleaning up HTTPManager
// This is critical because WebSocket has curl handles that must be freed
// before curl_global_cleanup() is called by HTTPManager
if (m_pWebSocket)
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "[NGMP] Shutting down WebSocket...");
m_pWebSocket->Shutdown();
// Reset shared_ptr to fully destroy WebSocket and free all its curl resources
// This must happen before HTTPManager shutdown to avoid accessing freed curl state
m_pWebSocket.reset();
NetworkLog(ELogVerbosity::LOG_RELEASE, "[NGMP] WebSocket shutdown complete");
}
// Now safe to shutdown HTTP manager which calls curl_global_cleanup()
if (m_pHTTPManager != nullptr)
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "[NGMP] Shutting down HTTPManager...");
m_pHTTPManager->Shutdown();
NetworkLog(ELogVerbosity::LOG_RELEASE, "[NGMP] HTTPManager shutdown complete");
}
NetworkLog(ELogVerbosity::LOG_RELEASE, "[NGMP] OnlineServicesManager shutdown complete");
}
void NGMP_OnlineServicesManager::FetchMacParityCRC(std::function<void(long)> fnCallback)
{
#ifdef __APPLE__
std::string strManifestURI = NGMP_OnlineServicesManager::GetAPIEndpoint("VersionManifest");
std::map<std::string, std::string> mapHeaders;
NGMP_OnlineServicesManager::GetInstance()->GetHTTPManager()->SendGETRequest(strManifestURI.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, [fnCallback](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq)
{
long rawExeCRC = MAC_PARITY_FALLBACK_EXE_CRC; // fallback
if (bSuccess && statusCode == 200)
{
try
{
nlohmann::json jsonObject = nlohmann::json::parse(strBody);
VersionManifestResponse manifestResp = jsonObject.get<VersionManifestResponse>();
rawExeCRC = manifestResp.execrc_60;
NetworkLog(ELogVerbosity::LOG_RELEASE, "VERSION MANIFEST: Dynamic CRC obtained: %llu", (unsigned long long)rawExeCRC);
DEBUG_NETWORK_MAC(("VERSION MANIFEST: Dynamic CRC obtained: %llu", (unsigned long long)rawExeCRC));
}
catch (...)
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "VERSION MANIFEST: Failed to parse response. Body: %s", strBody.c_str());
DEBUG_NETWORK_MAC(("VERSION MANIFEST: Failed to parse response. Body: %s", strBody.c_str()));
}
}
else
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "VERSION MANIFEST: Failed to get manifest (status %d)", statusCode);
DEBUG_NETWORK_MAC(("VERSION MANIFEST: Failed to get manifest (status %d)", statusCode));
}
fnCallback(rawExeCRC);
});
#else
fnCallback(0);
#endif
}
void NGMP_OnlineServicesManager::StartVersionCheck(std::function<void(bool bSuccess, bool bNeedsUpdate)> fnCallback)
{
FetchMacParityCRC([this, fnCallback](long macExeCRCOverride)
{
std::string strURI = NGMP_OnlineServicesManager::GetAPIEndpoint("VersionCheck");
// NOTE: Generals 'CRCs' are not true CRC's, its a custom algorithm. This is fine for lobby comparisons, but its not good for patch comparisons.
// exe crc
Char filePath[_MAX_PATH];
GetModuleFileName(NULL, filePath, sizeof(filePath));
std::ifstream file(NativeFileSystem::get_safe_path(filePath), std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();
if (!file.is_open() || size <= 0)
{
fnCallback(false, false);
return;
}
file.seekg(0, std::ios::beg);
std::vector<uint8_t> buffer(size);
file.read((char*)buffer.data(), size);
uint32_t realExeCRC = CRC_Memory((unsigned char*)buffer.data(), size);
#ifdef __APPLE__
if (macExeCRCOverride != 0)
{
realExeCRC = macExeCRCOverride;
// For P2P handshake, we need to seed the shift-add CRC algorithm.
// The VersionCheck API provides the IEEE CRC32 of the executable, so we map it
// to the corresponding runtime shift-add CRC base value for Windows parity.
long shiftAddBase = macExeCRCOverride;
if (macExeCRCOverride == MAC_PARITY_FALLBACK_EXE_CRC) {
shiftAddBase = MAC_PARITY_SHIFT_ADD_BASE; // 60 FPS GeneralsOnlineZH_60.exe
}
TheWritableGlobalData->m_exeCRC = GlobalData::generateExeCRCForMac(shiftAddBase);
}
#endif
nlohmann::json j;
j["execrc"] = realExeCRC;
j["ver"] = GENERALS_ONLINE_VERSION;
j["netver"] = GENERALS_ONLINE_NET_VERSION;
j["servicesver"] = GENERALS_ONLINE_SERVICE_VERSION;
std::string strPostData = j.dump();
std::map<std::string, std::string> mapHeaders;
NGMP_OnlineServicesManager::GetInstance()->GetHTTPManager()->SendPOSTRequest(strURI.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, strPostData.c_str(), [=](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq)
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "Version Check: Response code was %d and body was %s", statusCode, strBody.c_str());
DEBUG_NETWORK_MAC(("Version Check: Response code was %d and body was %s", statusCode, strBody.c_str()));
try
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "VERSION CHECK: Up To Date");
DEBUG_NETWORK_MAC(("VERSION CHECK: Up To Date"));
nlohmann::json jsonObject = nlohmann::json::parse(strBody);
VersionCheckResponse authResp = jsonObject.get<VersionCheckResponse>();
if (authResp.result == EVersionCheckResponseResult::OK)
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "VERSION CHECK: Up To Date");
DEBUG_NETWORK_MAC(("VERSION CHECK: Up To Date"));
fnCallback(true, false);
}
else if (authResp.result == EVersionCheckResponseResult::NEEDS_UPDATE)
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "VERSION CHECK: Needs Update");
DEBUG_NETWORK_MAC(("VERSION CHECK: Needs Update"));
// cache the data
m_patcher_name = authResp.patcher_name;
m_patcher_path = authResp.patcher_path;
m_patcher_size = authResp.patcher_size;
fnCallback(true, true);
}
else
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "VERSION CHECK: Failed");
DEBUG_NETWORK_MAC(("VERSION CHECK: Failed"));
fnCallback(false, false);
}
}
catch (...)
{
NetworkLog(ELogVerbosity::LOG_RELEASE, "VERSION CHECK: Failed to parse response");
DEBUG_NETWORK_MAC(("VERSION CHECK: Failed to parse response"));
fnCallback(false, false);
}
}, nullptr, -1);
});
}
void NGMP_OnlineServicesManager::ContinueUpdate()
{
if (m_vecFilesToDownload.size() > 0 && m_pHTTPManager != nullptr) // download next
{
std::string strDownloadPath = m_vecFilesToDownload.front();
m_vecFilesToDownload.pop();
uint32_t downloadSize = m_vecFilesSizes.front();
m_vecFilesSizes.pop();
if (TheDownloadManager != nullptr)
{
TheDownloadManager->SetFileName(AsciiString(strDownloadPath.c_str()));
TheDownloadManager->OnStatusUpdate(DOWNLOADSTATUS_DOWNLOADING);
}
// this isnt a super nice way of doing this, lets make a download manager
std::map<std::string, std::string> mapHeaders;
m_pHTTPManager->SendGETRequest(strDownloadPath.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, [=](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq)
{
if (statusCode != 200)
{
// show msg
ClearGSMessageBoxes();
MessageBoxOk(UnicodeString(L"Update Failed"), UnicodeString(L"Could not download the updater. Press below to exit."), []()
{
TheGameEngine->setQuitting(TRUE);
});
ShellExecuteA(NULL, "open", "https://www.playgenerals.online/updatefailed", NULL, NULL, SW_SHOWNORMAL);
}
else
{
// set done
if (TheDownloadManager != nullptr)
{
TheDownloadManager->OnProgressUpdate(downloadSize, downloadSize, 0, 0);
}
m_vecFilesDownloaded.push_back(strDownloadPath);
std::string strPatchDir = GetPatcherDirectoryPath();
if (strPatchDir.empty())
return;
// Extract the filename with extension from strDownloadPath
std::string strFileName = strDownloadPath.substr(strDownloadPath.find_last_of('/') + 1);
std::string strOutPath = std::format("{}/{}", strPatchDir, strFileName.c_str());
std::vector<uint8_t> vecBuffer = pReq->GetBuffer();
size_t bufSize = pReq->GetBufferSize();
if (!NativeFileSystem::exists(strPatchDir))
{
NativeFileSystem::create_directory(strPatchDir);
}
FILE* pFile = NativeFileSystem::fopen(strOutPath, "wb");
if (pFile != nullptr) {
fwrite(vecBuffer.data(), sizeof(uint8_t), bufSize, pFile);
fclose(pFile);
}
// call continue update again, thisll check if we're done or have more work to do
ContinueUpdate();
NetworkLog(ELogVerbosity::LOG_RELEASE, "GOT FILE: %s", strDownloadPath.c_str());
}
},
[=](size_t bytesReceived)
{
//m_bytesReceivedSoFar += bytesReceived;
if (TheDownloadManager != nullptr)
{
TheDownloadManager->OnProgressUpdate(bytesReceived, downloadSize, -1, -1);
}
}
);
}
else if (m_vecFilesToDownload.size() == 0 && m_vecFilesDownloaded.size() > 0) // nothing left but we did download something
{
if (TheDownloadManager != nullptr)
{
TheDownloadManager->SetFileName("Update is complete!");
TheDownloadManager->OnStatusUpdate(DOWNLOADSTATUS_FINISHING);
}
m_updateCompleteCallback();
}
}
void NGMP_OnlineServicesManager::CaptureScreenshot(bool bResizeForTransmit, std::function<void(std::vector<unsigned char>)> cbOnDataAvailable)
{
CHECK_MAIN_THREAD;
bool bSucceeded = false;
// no callback, nothing to do, early out
if (cbOnDataAvailable == nullptr)
{
return;
}
SurfaceClass* surface = DX8Wrapper::_Get_DX8_Back_Buffer();
LPDIRECT3DSURFACE8 surf = nullptr;
SurfaceClass* surfaceCopy = nullptr;
void* pBits = nullptr;
IDirect3DSurface8* pDXsurf = nullptr;
if (surface != nullptr)
{
SurfaceClass::SurfaceDescription surfaceDesc;
surface->Get_Description(surfaceDesc);
pDXsurf = DX8Wrapper::_Create_DX8_Surface(surfaceDesc.Width, surfaceDesc.Height, surfaceDesc.Format);
if (pDXsurf != nullptr)
{
surfaceCopy = NEW_REF(SurfaceClass, (pDXsurf));
if (surfaceCopy != nullptr)
{
DX8Wrapper::_Copy_DX8_Rects(surface->Peek_D3D_Surface(), NULL, 0, surfaceCopy->Peek_D3D_Surface(), NULL);
HRESULT hr;
D3DDISPLAYMODE mode;
if (SUCCEEDED(hr = DX8Wrapper::_Get_D3D_Device8()->GetDisplayMode(&mode)))
{
if (SUCCEEDED(hr = DX8Wrapper::_Get_D3D_Device8()->CreateImageSurface(mode.Width, mode.Height,
D3DFMT_A8R8G8B8, &surf)))
{
if (SUCCEEDED(hr = DX8Wrapper::_Get_D3D_Device8()->GetFrontBuffer(surf)))
{
// gather all our data
int pitch = 0;
pBits = surfaceCopy->Lock(&pitch);
if (pBits != nullptr)
{
int width = surfaceDesc.Width;
int height = surfaceDesc.Height;
// Copy pixel data into an owned buffer before spawning the thread so
// the surface can be safely unlocked on the main thread immediately after.
std::vector<uint8_t> pixelData(height * pitch);
memcpy(pixelData.data(), pBits, height * pitch);
// process on thread - track the thread so we can join it during shutdown
std::thread* pNewThread = new std::thread([cbOnDataAvailable, width, height, pixelData = std::move(pixelData), pDXsurf, pitch, bResizeForTransmit]()
{
CHECK_WORKER_THREAD;
unsigned char* rgbData = new unsigned char[width * height * 3];
std::vector<unsigned char> vecData;
int finalWidth = width;
int finalHeight = height;
for (int y = 0; y < height; ++y) {
const uint8_t* row = pixelData.data() + y * pitch;
int rowOffset = y * width * 3;
int srcOffset = 0;
for (int x = 0; x < width; ++x, srcOffset += 4)
{
int dstIndex = rowOffset + x * 3;
rgbData[dstIndex + 0] = row[srcOffset + 2]; // R
rgbData[dstIndex + 1] = row[srcOffset + 1]; // G
rgbData[dstIndex + 2] = row[srcOffset + 0]; // B
}
}
// resize
unsigned char* pBufferToWrite = rgbData;
if (bResizeForTransmit)
{
ServiceConfig& serviceConf = NGMP_OnlineServicesManager::GetInstance()->GetServiceConfig();
int new_width = serviceConf.screenshot_width;
int new_height = serviceConf.screenshot_height;
int channels = 3;
unsigned char* resized = new unsigned char[new_width * new_height * channels];
stbir_resize_uint8(rgbData, width, height, 0,
resized, new_width, new_height, 0,
channels
);
// update data
finalWidth = new_width;
finalHeight = new_height;
pBufferToWrite = resized;
}
// end resize
stbi_write_jpg_to_func([](void* context, void* data, int size)
{
std::vector<unsigned char>* buffer = static_cast<std::vector<unsigned char>*>(context);
buffer->insert(buffer->end(), (unsigned char*)data, (unsigned char*)data + size);
}, &vecData, finalWidth, finalHeight, 3, pBufferToWrite, bResizeForTransmit ? 0 : 90);
// cleanup
if (bResizeForTransmit)
{
delete[] pBufferToWrite; // This is 'resized'
pBufferToWrite = nullptr;
}
delete[] rgbData;
rgbData = nullptr;
if (pDXsurf != nullptr)
{
pDXsurf->Release();
}
// invoke cb
if (cbOnDataAvailable != nullptr)
{
cbOnDataAvailable(vecData);
}
}
);
// Store the thread so we can join it during shutdown
if (m_pOnlineServicesManager != nullptr)
{
std::scoped_lock<std::mutex> lock(m_pOnlineServicesManager->m_mutexScreenshotThreads);
m_pOnlineServicesManager->m_vecScreenshotThreads.push_back(pNewThread);
}
bSucceeded = true;
}
}
}
}
}
}
}
// clean everything up, whether we succeeded or not
// release the image surface
if (surf != nullptr)
{
surf->Release();
//delete surf;
surf = nullptr;
}
// unlock
if (surface != nullptr)
{
surface->Unlock();
surface->Release_Ref();
surface = nullptr;
}
if (surfaceCopy != nullptr)
{
surfaceCopy->Unlock();
surfaceCopy->Release_Ref();
surfaceCopy = nullptr;
}
if (!bSucceeded) // if success, thread uses this and then destroys it
{
if (pDXsurf != nullptr)
{
pDXsurf->Release();
}
}
// callback if failed
if (!bSucceeded)
{
cbOnDataAvailable(std::vector<unsigned char>());
}
}
void NGMP_OnlineServicesManager::CancelUpdate()
{
}
void NGMP_OnlineServicesManager::LaunchPatcher()
{
#ifdef _WIN32
char GameDir[MAX_PATH + 1] = {};
::GetCurrentDirectoryA(MAX_PATH + 1u, GameDir);
// Extract the filename with extension from strDownloadPath
std::string strPatcherDir = GetPatcherDirectoryPath();
std::string strPatcherPath = std::format("{}/{}", strPatcherDir, m_patcher_name);
SHELLEXECUTEINFOA shellexInfo = { sizeof(shellexInfo) };
shellexInfo.lpVerb = "runas"; // admin
shellexInfo.lpFile = strPatcherPath.c_str();
shellexInfo.nShow = SW_SHOWNORMAL;
shellexInfo.lpDirectory = GameDir;
//shellexInfo.lpParameters = "/VERYSILENT";
bool bPatcherExeExists = NativeFileSystem::exists(strPatcherPath) && NativeFileSystem::is_regular_file(strPatcherPath);
bool bPatcherDirExists = NativeFileSystem::exists(strPatcherDir) && NativeFileSystem::is_directory(strPatcherDir);
bool bInvalidSize = true;
// TODO_NGMP: Replace with CRC ASAP
// does the file size match?
if (bPatcherExeExists && bPatcherDirExists)
{
std::uintmax_t file_size = NativeFileSystem::file_size(strPatcherPath);
if (file_size == m_patcher_size)
{
bInvalidSize = false;
}
}
if (!bInvalidSize && bPatcherExeExists && bPatcherDirExists && ShellExecuteExA(&shellexInfo))
{
// Exit the application
TheGameEngine->setQuitting(TRUE);
}
else
{
// show msg
ClearGSMessageBoxes();
MessageBoxOk(UnicodeString(L"Update Failed"), UnicodeString(L"Could not run the updater. Press below to exit."), []()
{
TheGameEngine->setQuitting(TRUE);
});
ShellExecuteA(NULL, "open", "https://www.playgenerals.online/updatefailed", NULL, NULL, SW_SHOWNORMAL);
}
#endif
}
void NGMP_OnlineServicesManager::StartDownloadUpdate(std::function<void(void)> cb)
{
TheDownloadManager->SetFileName("Connecting to update service...");
TheDownloadManager->OnStatusUpdate(DOWNLOADSTATUS_CONNECTING);
m_vecFilesToDownload = std::queue<std::string>();
m_vecFilesDownloaded.clear();
// patcher
m_vecFilesToDownload.emplace(m_patcher_path);
m_vecFilesSizes.emplace(m_patcher_size);
m_updateCompleteCallback = cb;
// cleanup current folder
std::string strPatchDir = GetPatcherDirectoryPath();
NativeFileSystem::remove_all_in_directory(strPatchDir);
// start for real
ContinueUpdate();
}
void NGMP_OnlineServicesManager::OnLogin(ELoginResult loginResult, const char* szWSAddr, std::function<void(void)> fnWebsocketConnectedCallback)
{
if (loginResult == ELoginResult::Success)
{
// connect to WS
m_pWebSocket = std::make_shared<WebSocket>();
// TODO_NGMP: This should come from the service, if the service was russia-aware
std::string strWebsocketAddr = NGMP_OnlineServicesManager::Settings.Network_UseAlternativeEndpoint() ? "wss://api-ru.playgenerals.online/ws" : std::string(szWSAddr);
m_pWebSocket->Connect(strWebsocketAddr.c_str(), false, [=]()
{
// Get friends list and blocked list
// we need to wait until the websocket is connected so we have a session
NGMP_OnlineServices_SocialInterface* pSocialInterface = NGMP_OnlineServicesManager::GetInterface<NGMP_OnlineServices_SocialInterface>();
if (pSocialInterface == nullptr)
{
return;
}
pSocialInterface->GetFriendsList(false, nullptr);
pSocialInterface->GetBlockList(nullptr);
// and invoke callback
fnWebsocketConnectedCallback();
});
}
}
void NGMP_OnlineServicesManager::Init()
{
g_MainThreadID = std::this_thread::get_id();
// initialize child classes, these need the platform handle
m_pAuthInterface = new NGMP_OnlineServices_AuthInterface();
m_pLobbyInterface = new NGMP_OnlineServices_LobbyInterface();
m_pRoomInterface = new NGMP_OnlineServices_RoomsInterface();
m_pStatsInterface = new NGMP_OnlineServices_StatsInterface();
m_pMatchmakingInterface = new NGMP_OnlineServices_MatchmakingInterface();
m_pSocialInterface = new NGMP_OnlineServices_SocialInterface();
m_pHTTPManager = new HTTPManager();
m_pHTTPManager->Initialize();
// TODO_NGMP: Better location
// TODO_NGMP: Get all of this from the service
int moneyVal = 100000;
int maxMoneyVal = 1000000;
while (moneyVal <= maxMoneyVal)
{
Money newMoneyVal;
newMoneyVal.deposit(moneyVal, false);
TheMultiplayerSettings->addStartingMoneyChoice(newMoneyVal, false);
moneyVal += 50000;
}
#if 0
std::map<AsciiString, RGBColor> mapColors;
mapColors["Dark Red"] = RGBColor{ 0.53f, 0.f, 0.08f };
mapColors["Brown"] = RGBColor{ 0.46f, 0.26f, 0.26f };
mapColors["Dark Green"] = RGBColor{ 0.09f, 0.24f, 0.04f };
for (const auto& [colorName, rgbColor] : mapColors)
{
MultiplayerColorDefinition* newDef = TheMultiplayerSettings->newMultiplayerColorDefinition(colorName.str());
newDef->setColor(rgbColor);
newDef->setNightColor(rgbColor);
}
#endif
}
void NGMP_OnlineServicesManager::Tick()
{
// screenshots
{
// send screenshot
std::scoped_lock<std::mutex> ssLock(m_ScreenshotMutex);
// screenshot types that already have a presigned URL
for (S3ScreenshotEntry screenshotEntry : m_vecGuardedSSData)
{
// NOTE: Screenshot types start and end of match are captures and cached in memory until the server tells us where to upload them, so we need to wait for the upload URI
if ((screenshotEntry.screenshotType == EScreenshotType::SCREENSHOT_TYPE_LOADSCREEN || screenshotEntry.screenshotType == EScreenshotType::SCREENSHOT_TYPE_SCORESCREEN) && screenshotEntry.strSignedURI.empty())
{
continue;
}
else // we have the data we need, send away
{
std::map<std::string, std::string> mapHeaders;
mapHeaders["Content-Type"] = "image/jpeg";
NGMP_OnlineServicesManager::GetInstance()->GetHTTPManager()->SendS3PUTRequest(screenshotEntry.strSignedURI.c_str(), EIPProtocolVersion::DONT_CARE, mapHeaders, screenshotEntry.vecBytes, [=](bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq)
{
#if _DEBUG
if (statusCode != 200)
{
__debugbreak();
}
#endif
NetworkLog(ELogVerbosity::LOG_RELEASE, "Screenshot upload, result: %d", statusCode);
}, nullptr, HTTP_UPLOAD_TIMEOUT);
}
}
m_vecGuardedSSData.clear();
// screenshots and replays that are cached and awaiting S3 url, and now have said URL
if (!m_vecCachedScreenshotBytes_MatchStart.empty()) // we have data waiting
{
if (!m_strCachedScreenshot_MatchStart_S3URI.empty()) // and we have a URL
{
// queue it
S3ScreenshotEntry newEntry;
newEntry.screenshotType = EScreenshotType::SCREENSHOT_TYPE_LOADSCREEN;
newEntry.vecBytes = m_vecCachedScreenshotBytes_MatchStart;
newEntry.strSignedURI = m_strCachedScreenshot_MatchStart_S3URI;
m_vecGuardedSSData.push_back(newEntry);
// clear data
m_vecCachedScreenshotBytes_MatchStart = std::vector<uint8_t>();
m_strCachedScreenshot_MatchStart_S3URI = std::string();
}
}