-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin_eac.cpp
More file actions
1138 lines (962 loc) · 32.5 KB
/
plugin_eac.cpp
File metadata and controls
1138 lines (962 loc) · 32.5 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 "plugin_eac.h"
// Global notification IDs for callback cleanup
EOS_NotificationId g_NotifyClientIntegrityViolatedId = 0;
EOS_NotificationId g_NotifyMessageToPeerId = 0;
EOS_NotificationId g_NotifyPeerAuthStatusChangedId = 0;
EOS_NotificationId g_NotifyPeerActionRequiredId = 0;
typedef void (*LoginCallback)(bool bSuccess);
LoginCallback g_LoginCallback = nullptr;
BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD ul_reason_for_call, LPVOID /*lpReserved*/)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
break;
}
case DLL_PROCESS_DETACH:
{
// Clean up resources on DLL unload
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
if (g_EOSPlatformHandle != nullptr)
{
// Clear callback pointers to prevent use-after-free
g_fnLoggingFunc = nullptr;
g_fnLobbyChatOutput = nullptr;
g_fnAnticheatIntegrityViolationOccurredCallback = nullptr;
g_fnAnticheatActionCallback = nullptr;
g_fnSendMessageViaTransport = nullptr;
g_LoginCallback = nullptr;
}
break;
}
}
return TRUE;
}
void PluginLog(const char* fmt, ...)
{
if (fmt == nullptr)
{
return;
}
char buffer[8192];
va_list args;
va_start(args, fmt);
vsnprintf(buffer, 8192, fmt, args);
buffer[8192 - 1] = 0;
va_end(args);
// Lock before accessing global callback to prevent data race
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
if (g_fnLoggingFunc != nullptr)
{
g_fnLoggingFunc(buffer);
}
}
}
void SetLoggingFunction(LoggingFunc cb)
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
g_fnLoggingFunc = cb;
}
void SetLobbyChatOutputFunction(LoggingFunc cb)
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
g_fnLobbyChatOutput = cb;
}
void SetACActionRequiredCallback(ACPlayerActionRequiredCallbackFunc cb)
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
g_fnAnticheatActionCallback = cb;
}
void SetACIntegrityViolationOccurredCallback(ACIntegrityViolationCallbackFunc cb)
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
g_fnAnticheatIntegrityViolationOccurredCallback = cb;
}
void SetSendMessageViaTransportCallback(SendMessageViaTransportFunc cb)
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
g_fnSendMessageViaTransport = cb;
}
void ACMessageArrivedViaTransport(uint32_t sourceUserID, void* data, uint32_t dataLen)
{
if (data == nullptr || dataLen == 0)
{
PluginLog("[AC][EAC][REMOTE] ERROR: Invalid message data (null or zero length)");
return;
}
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
if (g_EOSPlatformHandle == nullptr)
{
PluginLog("[AC][EAC][REMOTE] ERROR: Platform handle is null");
return;
}
EOS_HAntiCheatClient acHandle = EOS_Platform_GetAntiCheatClientInterface(g_EOSPlatformHandle);
if (acHandle == nullptr)
{
PluginLog("[AC][EAC][REMOTE] ERROR: AC handle is null");
return;
}
EOS_AntiCheatClient_ReceiveMessageFromPeerOptions receiveOpts = {};
receiveOpts.ApiVersion = EOS_ANTICHEATCLIENT_RECEIVEMESSAGEFROMPEER_API_LATEST;
receiveOpts.PeerHandle = (void*)sourceUserID;
receiveOpts.Data = data;
receiveOpts.DataLengthBytes = dataLen;
EOS_EResult receiveRes = EOS_AntiCheatClient_ReceiveMessageFromPeer(acHandle, &receiveOpts);
if (receiveRes != EOS_EResult::EOS_Success)
{
PluginLog("[AC][EAC][REMOTE] MIDDLEWARE ERROR, EOS_AntiCheatClient_ReceiveMessageFromPeer: %s!", EOS_EResult_ToString(receiveRes));
}
else
{
PluginLog("[AC][EAC][REMOTE] AC RECEIVED MESSAGE FROM PEER: %u bytes (User %u)", dataLen, sourceUserID);
}
}
enum class ENetworkChannels
{
Game,
Anticheat,
Ping,
Pong,
InitialHolepunch
};
void Tick()
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
if (g_EOSPlatformHandle != nullptr)
{
EOS_Platform_Tick(g_EOSPlatformHandle);
}
}
bool IsLoggedIn()
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
return g_EOSUserID != nullptr;
}
bool GetMiddlewareAuthToken(char* buffer, size_t bufferSize)
{
// Validate caller-supplied buffer to prevent NULL dereference
if (buffer == nullptr || bufferSize == 0)
{
PluginLog("[EAC] GetMiddlewareAuthToken: Invalid buffer (null or zero size)");
return false;
}
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
if (g_EOSPlatformHandle == nullptr)
{
PluginLog("[EAC] MIDDLEWARE ERROR: Platform not initialized!");
return false;
}
EOS_HConnect ConnectHandle = EOS_Platform_GetConnectInterface(g_EOSPlatformHandle);
EOS_Connect_IdToken* epicToken = nullptr;
EOS_Connect_CopyIdTokenOptions opts = {};
opts.ApiVersion = EOS_CONNECT_COPYIDTOKEN_API_LATEST;
opts.LocalUserId = g_EOSUserID;
EOS_EResult res = EOS_Connect_CopyIdToken(ConnectHandle, &opts, &epicToken);
if (res != EOS_EResult::EOS_Success)
{
PluginLog("[EAC] MIDDLEWARE ERROR: %s!", EOS_EResult_ToString(res));
return false;
}
if (epicToken == nullptr)
{
PluginLog("[EAC] MIDDLEWARE ERROR: Token pointer is null!");
return false;
}
if (epicToken->JsonWebToken == nullptr)
{
PluginLog("[EAC] MIDDLEWARE ERROR: JsonWebToken is null!");
return false;
}
const char* msg = epicToken->JsonWebToken;
size_t len = strlen(msg) + 1;
// Validate token length doesn't exceed a reasonable size
if (len > 8192)
{
PluginLog("[EAC] MIDDLEWARE ERROR: Token too large!");
return false;
}
if (bufferSize < len)
{
PluginLog("[EAC] MIDDLEWARE BAD SIZE!");
return false;
}
memcpy(buffer, msg, len);
return true;
}
int Initialize()
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
// Check if already initialized
if (g_EOSPlatformHandle != nullptr)
{
PluginLog("[EAC] Already initialized - skipping re-initialization");
return 0;
}
// Init EOS SDK
EOS_InitializeOptions SDKOptions = {};
SDKOptions.ApiVersion = EOS_INITIALIZE_API_LATEST;
SDKOptions.AllocateMemoryFunction = nullptr;
SDKOptions.ReallocateMemoryFunction = nullptr;
SDKOptions.ReleaseMemoryFunction = nullptr;
static char szBuffer[MAX_PATH] = { 0 };
strcpy_s(szBuffer, sizeof(szBuffer), "GOClient");
SDKOptions.ProductName = szBuffer;
SDKOptions.ProductVersion = "1.0";
SDKOptions.Reserved = nullptr;
SDKOptions.SystemInitializeOptions = nullptr;
SDKOptions.OverrideThreadAffinity = nullptr;
EOS_EResult InitResult = EOS_Initialize(&SDKOptions);
// TODO: Decrease logging once confirmed stable
if (InitResult == EOS_EResult::EOS_Success)
{
// LOGGING
EOS_EResult SetLogCallbackResult = EOS_Logging_SetCallback([](const EOS_LogMessage* Message)
{
if (Message == nullptr)
{
return;
}
const char* category = Message->Category ? Message->Category : "UNKNOWN";
const char* msg = Message->Message ? Message->Message : "(null)";
PluginLog("[EOS - %s] %s", category, msg);
});
if (SetLogCallbackResult != EOS_EResult::EOS_Success)
{
PluginLog("[EAC] Set Logging Callback Failed!");
}
else
{
PluginLog("[EAC] Logging Callback Set");
#if _DEBUG
//EOS_Logging_SetLogLevel(EOS_ELogCategory::EOS_LC_ALL_CATEGORIES, EOS_ELogLevel::EOS_LOG_Info);
EOS_EResult SetLogLevelResult = EOS_Logging_SetLogLevel(EOS_ELogCategory::EOS_LC_ALL_CATEGORIES, EOS_ELogLevel::EOS_LOG_VeryVerbose);
if (SetLogLevelResult != EOS_EResult::EOS_Success)
{
PluginLog("[EAC] Set Logging Level Failed!");
}
#else
//EOS_Logging_SetLogLevel(EOS_ELogCategory::EOS_LC_ALL_CATEGORIES, EOS_ELogLevel::EOS_LOG_Error);
EOS_EResult SetLogLevelResult = EOS_Logging_SetLogLevel(EOS_ELogCategory::EOS_LC_ALL_CATEGORIES, EOS_ELogLevel::EOS_LOG_VeryVerbose);
if (SetLogLevelResult != EOS_EResult::EOS_Success)
{
PluginLog("[EAC] Set Logging Level Failed!");
}
#endif
}
std::filesystem::path tempPath = std::filesystem::current_path();
tempPath.append("cache");
std::string strCachePath = tempPath.string();
// PLATFORM OPTIONS
EOS_Platform_Options PlatformOptions = {};
PlatformOptions.ApiVersion = EOS_PLATFORM_OPTIONS_API_LATEST;
PlatformOptions.bIsServer = EOS_FALSE;
PlatformOptions.OverrideCountryCode = nullptr;
PlatformOptions.OverrideLocaleCode = nullptr;
PlatformOptions.Flags = EOS_PF_WINDOWS_ENABLE_OVERLAY_D3D9 | EOS_PF_WINDOWS_ENABLE_OVERLAY_D3D10;
PlatformOptions.CacheDirectory = strCachePath.c_str();
PlatformOptions.ProductId = "TODO";
PlatformOptions.SandboxId = "TODO";
PlatformOptions.EncryptionKey = "1111111111111111111111111111111111111111111111111111111"; // NOTE: unused
PlatformOptions.DeploymentId = "TODO";
PlatformOptions.ClientCredentials.ClientId = "TODO";
PlatformOptions.ClientCredentials.ClientSecret = "TODO";
double timeout = 5000.0;
PlatformOptions.TaskNetworkTimeoutSeconds = &timeout;
EOS_Platform_RTCOptions RtcOptions = {};
RtcOptions.ApiVersion = EOS_PLATFORM_RTCOPTIONS_API_LATEST;
#ifdef _WIN32
// Get absolute path for xaudio2_9redist.dll file
char CurDir[MAX_PATH + 1] = {};
::GetCurrentDirectoryA(MAX_PATH, CurDir);
// get exe path
char buffer[MAX_PATH] = {};
GetModuleFileNameA(NULL, buffer, MAX_PATH - 1);
buffer[MAX_PATH - 1] = '\0'; // Ensure null termination
std::string::size_type pos = std::string(buffer).find_last_of("\\/");
if (pos == std::string::npos)
{
PluginLog("FATAL ERROR: Failed to parse executable path");
return 1;
}
std::string ExePath = std::string(buffer).substr(0, pos);
std::string XAudio29DllPath = ExePath;
XAudio29DllPath.append("\\xaudio2_9redist.dll");
PluginLog("Current Directory: %s", CurDir);
PluginLog("EXE Directory: %s", ExePath.c_str());
PluginLog("XAudio Path: %s", XAudio29DllPath.c_str());
// does the DLL exist on disk?
std::fstream fileStream;
fileStream.open(XAudio29DllPath.c_str(), std::fstream::in | std::fstream::binary);
if (!fileStream.good())
{
PluginLog("FATAL ERROR: Failed to locate XAudio DLL");
return 1;
}
else
{
PluginLog("XAudio DLL located successfully");
}
EOS_Windows_RTCOptions WindowsRtcOptions = { 0 };
WindowsRtcOptions.ApiVersion = EOS_WINDOWS_RTCOPTIONS_API_LATEST;
WindowsRtcOptions.XAudio29DllPath = XAudio29DllPath.c_str();
RtcOptions.PlatformSpecificOptions = &WindowsRtcOptions;
#else
RtcOptions.PlatformSpecificOptions = nullptr;
#endif // _WIN32
PlatformOptions.RTCOptions = &RtcOptions;
#if ALLOW_RESERVED_PLATFORM_OPTIONS
SetReservedPlatformOptions(PlatformOptions);
#else
PlatformOptions.Reserved = NULL;
#endif // ALLOW_RESERVED_PLATFORM_OPTIONS
// platform integration settings
// Create the generic container.
const EOS_IntegratedPlatform_CreateIntegratedPlatformOptionsContainerOptions CreateOptions =
{
EOS_INTEGRATEDPLATFORM_CREATEINTEGRATEDPLATFORMOPTIONSCONTAINER_API_LATEST
};
const EOS_EResult Result = EOS_IntegratedPlatform_CreateIntegratedPlatformOptionsContainer(&CreateOptions, &PlatformOptions.IntegratedPlatformOptionsContainerHandle);
if (Result != EOS_EResult::EOS_Success)
{
PluginLog("EOS_IntegratedPlatform_CreateIntegratedPlatformOptionsContainer returned an error");
return 2;
}
g_EOSPlatformHandle = EOS_Platform_Create(&PlatformOptions);
if (g_EOSPlatformHandle == nullptr)
{
PluginLog("FATAL ERROR: EOS_Platform_Create failed - returned null handle");
return 4;
}
// END PLATFORM OPTIONS
return 0;
}
else
{
PluginLog("[EAC] INIT FAILED: %s", EOS_EResult_ToString(InitResult));
return 3;
}
}
PLUGIN_API void Shutdown()
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
if (g_EOSPlatformHandle != nullptr)
{
EOS_Platform_Release(g_EOSPlatformHandle);
g_EOSPlatformHandle = nullptr;
}
EOS_Shutdown();
// Reset global state
g_EOSUserID = nullptr;
g_goUserID = 0;
g_fnLoggingFunc = nullptr;
g_fnLobbyChatOutput = nullptr;
g_fnAnticheatIntegrityViolationOccurredCallback = nullptr;
g_fnAnticheatActionCallback = nullptr;
g_fnSendMessageViaTransport = nullptr;
g_LoginCallback = nullptr;
g_bEventsHooked = false;
}
bool IsExternalProcessRunning()
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
if (g_EOSPlatformHandle == nullptr)
{
return false;
}
EOS_HAntiCheatClient acHandle = EOS_Platform_GetAntiCheatClientInterface(g_EOSPlatformHandle);
return acHandle != nullptr;
}
PLUGIN_API int GetAnticheatIdentifier()
{
return 9481;
}
void HookupEvents()
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
if (g_bEventsHooked)
{
return;
}
EOS_HAntiCheatClient acHandle = EOS_Platform_GetAntiCheatClientInterface(g_EOSPlatformHandle);
if (acHandle == nullptr)
{
PluginLog("[EAC] AC HANDLE NULL - Cannot hook events");
return;
}
// Clean up any existing notification IDs before registering new ones (prevents memory leak on re-hook)
if (g_NotifyClientIntegrityViolatedId != 0)
{
EOS_AntiCheatClient_RemoveNotifyClientIntegrityViolated(acHandle, g_NotifyClientIntegrityViolatedId);
g_NotifyClientIntegrityViolatedId = 0;
}
if (g_NotifyMessageToPeerId != 0)
{
EOS_AntiCheatClient_RemoveNotifyMessageToPeer(acHandle, g_NotifyMessageToPeerId);
g_NotifyMessageToPeerId = 0;
}
if (g_NotifyPeerAuthStatusChangedId != 0)
{
EOS_AntiCheatClient_RemoveNotifyPeerAuthStatusChanged(acHandle, g_NotifyPeerAuthStatusChangedId);
g_NotifyPeerAuthStatusChangedId = 0;
}
if (g_NotifyPeerActionRequiredId != 0)
{
EOS_AntiCheatClient_RemoveNotifyPeerActionRequired(acHandle, g_NotifyPeerActionRequiredId);
g_NotifyPeerActionRequiredId = 0;
}
g_bEventsHooked = true;
EOS_AntiCheatClient_AddNotifyClientIntegrityViolatedOptions opts = {};
opts.ApiVersion = EOS_ANTICHEATCLIENT_ADDNOTIFYCLIENTINTEGRITYVIOLATED_API_LATEST;
g_NotifyClientIntegrityViolatedId = EOS_AntiCheatClient_AddNotifyClientIntegrityViolated(acHandle, &opts, nullptr, [](const EOS_AntiCheatClient_OnClientIntegrityViolatedCallbackInfo* Data)
{
if (Data == nullptr)
{
return;
}
const char* violationMsg = Data->ViolationMessage ? Data->ViolationMessage : "(null)";
PluginLog("[EAC] AC VIOLATION: %s (%d)", violationMsg, Data->ViolationType);
ACIntegrityViolationCallbackFunc callback = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
callback = g_fnAnticheatIntegrityViolationOccurredCallback;
}
// Lock released before calling callback
if (callback != nullptr)
{
callback(Data->ViolationMessage, (int)Data->ViolationType);
}
});
EOS_AntiCheatClient_AddNotifyMessageToPeerOptions AddNotifyMessageToPeerOpts = {};
AddNotifyMessageToPeerOpts.ApiVersion = EOS_ANTICHEATCLIENT_ADDNOTIFYMESSAGETOPEER_API_LATEST;
g_NotifyMessageToPeerId = EOS_AntiCheatClient_AddNotifyMessageToPeer(acHandle, &AddNotifyMessageToPeerOpts, nullptr, [](const EOS_AntiCheatCommon_OnMessageToClientCallbackInfo* Data)
{
if (Data == nullptr)
{
return;
}
uint32_t targetUserID = (uint32_t)Data->ClientHandle;
EOS_HPlatform platformHandle = nullptr;
EOS_HAntiCheatClient acHandle = nullptr;
SendMessageViaTransportFunc sendMessageCallback = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
if (g_EOSPlatformHandle == nullptr)
{
return;
}
platformHandle = g_EOSPlatformHandle;
acHandle = EOS_Platform_GetAntiCheatClientInterface(g_EOSPlatformHandle);
if (acHandle == nullptr)
{
return;
}
if (Data->ClientHandle == nullptr || Data->MessageData == nullptr)
{
return;
}
sendMessageCallback = g_fnSendMessageViaTransport;
}
// Lock released before processing
// was it ourselves? just process immediately
if (targetUserID == g_goUserID)
{
EOS_AntiCheatClient_ReceiveMessageFromPeerOptions receiveOpts = {};
receiveOpts.ApiVersion = EOS_ANTICHEATCLIENT_RECEIVEMESSAGEFROMPEER_API_LATEST;
receiveOpts.PeerHandle = Data->ClientHandle;
receiveOpts.Data = Data->MessageData;
receiveOpts.DataLengthBytes = Data->MessageDataSizeBytes;
EOS_EResult receiveRes = EOS_AntiCheatClient_ReceiveMessageFromPeer(acHandle, &receiveOpts);
if (receiveRes != EOS_EResult::EOS_Success)
{
PluginLog("[EAC][LOCAL] MIDDLEWARE ERROR, EOS_AntiCheatClient_ReceiveMessageFromPeer: %s!", EOS_EResult_ToString(receiveRes));
}
else
{
PluginLog("[EAC][LOCAL] AC SEND MESSAGE TO PEER: %u bytes (User %u)", Data->MessageDataSizeBytes, (uint32_t)Data->ClientHandle);
}
}
else // send via transport
{
PluginLog("[EAC][REMOTE] AC SEND MESSAGE TO PEER: %u bytes (User %u)", Data->MessageDataSizeBytes, targetUserID);
if (sendMessageCallback != nullptr)
{
sendMessageCallback(targetUserID, Data->MessageData, Data->MessageDataSizeBytes);
}
else
{
PluginLog("[EAC][REMOTE] ERROR: Send message callback is null!");
}
}
});
EOS_AntiCheatClient_AddNotifyPeerAuthStatusChangedOptions authChangedOpts = {};
authChangedOpts.ApiVersion = EOS_ANTICHEATCLIENT_ADDNOTIFYPEERAUTHSTATUSCHANGED_API_LATEST;
g_NotifyPeerAuthStatusChangedId = EOS_AntiCheatClient_AddNotifyPeerAuthStatusChanged(acHandle, &authChangedOpts, nullptr, [](const EOS_AntiCheatCommon_OnClientAuthStatusChangedCallbackInfo* Data)
{
if (Data == nullptr)
{
return;
}
uint32_t userID = (uint32_t)Data->ClientHandle;
PluginLog("[EAC] AC PEER AUTH STATUS CHANGED: %d (User %u)", Data->ClientAuthStatus, userID);
});
EOS_AntiCheatClient_AddNotifyPeerActionRequiredOptions actionRequiredOpts = {};
actionRequiredOpts.ApiVersion = EOS_ANTICHEATCLIENT_ADDNOTIFYPEERACTIONREQUIRED_API_LATEST;
g_NotifyPeerActionRequiredId = EOS_AntiCheatClient_AddNotifyPeerActionRequired(acHandle, &actionRequiredOpts, nullptr, [](const EOS_AntiCheatCommon_OnClientActionRequiredCallbackInfo* Data)
{
if (Data == nullptr)
{
return;
}
const char* reasonStr = Data->ActionReasonDetailsString ? Data->ActionReasonDetailsString : "(null)";
PluginLog("[EAC] AC PEER ACTION REQIRED: %s (%d - %d)", reasonStr, Data->ClientAction, Data->ActionReasonCode);
EOS_HPlatform platformHandle = nullptr;
ACPlayerActionRequiredCallbackFunc callback = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
if (g_EOSPlatformHandle == nullptr)
{
return;
}
platformHandle = g_EOSPlatformHandle;
callback = g_fnAnticheatActionCallback;
}
// Lock released before calling callback
if (callback != nullptr)
{
uint32_t userID = (uint32_t)Data->ClientHandle;
if (Data->ClientHandle == EOS_ANTICHEATCLIENT_PEER_SELF)
{
PluginLog("[EAC] AC PEER ACTION REQIRED: is self (%u)", userID);
}
else
{
PluginLog("[EAC] AC PEER ACTION REQIRED: is remote (%u)", userID);
}
callback(userID, Data->ActionReasonDetailsString, (int)(EAnticheatActionType)Data->ClientAction, (int)(EAnticheatActionReason)Data->ActionReasonCode);
}
});
}
void BeginSession()
{
PluginLog("[EAC] BEGIN SESSION");
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
EOS_HAntiCheatClient acHandle = EOS_Platform_GetAntiCheatClientInterface(g_EOSPlatformHandle);
if (acHandle == nullptr)
{
PluginLog("[EAC] AC HANDLE IS NULL");
return;
}
HookupEvents();
EOS_AntiCheatClient_BeginSessionOptions beginSessionOpts = {};
beginSessionOpts.ApiVersion = EOS_ANTICHEATCLIENT_BEGINSESSION_API_LATEST;
beginSessionOpts.LocalUserId = g_EOSUserID;
beginSessionOpts.Mode = EOS_EAntiCheatClientMode::EOS_ACCM_PeerToPeer;
EOS_EResult result = EOS_AntiCheatClient_BeginSession(acHandle, &beginSessionOpts);
if (result != EOS_EResult::EOS_Success)
{
PluginLog("[EAC] MIDDLEWARE ERROR, BEGIN SESSION: %s!", EOS_EResult_ToString(result));
}
else
{
PluginLog("[EAC] BEGIN SESSION SUCCEEDED");
}
}
bool DeregisterPlayer(const char* szMiddlewareUserID, uint32_t goUserID)
{
if (szMiddlewareUserID == nullptr)
{
PluginLog("[EAC] DeregisterPlayer: Invalid middleware user ID (null)");
return false;
}
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
EOS_HAntiCheatClient acHandle = EOS_Platform_GetAntiCheatClientInterface(g_EOSPlatformHandle);
if (acHandle == nullptr)
{
PluginLog("[EAC] AC HANDLE NULL DeregisterPlayer");
return false;
}
EOS_AntiCheatClient_UnregisterPeerOptions opts = {};
opts.ApiVersion = EOS_ANTICHEATCLIENT_UNREGISTERPEER_API_LATEST;
opts.PeerHandle = (void*)goUserID;
EOS_EResult res = EOS_AntiCheatClient_UnregisterPeer(acHandle, &opts);
PluginLog("[EAC] RegisterPlayer: Deregistering remote player %s - %d!", szMiddlewareUserID, goUserID);
if (res != EOS_EResult::EOS_Success)
{
PluginLog("[EAC] DeregisterPlayer ERROR: %s!", EOS_EResult_ToString(res));
return false;
}
return true;
}
bool RegisterPlayer(const char* szMiddlewareUserID, uint32_t goUserID)
{
if (szMiddlewareUserID == nullptr)
{
PluginLog("[EAC] RegisterPlayer: Invalid middleware user ID (null)");
return false;
}
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
EOS_HAntiCheatClient acHandle = EOS_Platform_GetAntiCheatClientInterface(g_EOSPlatformHandle);
if (acHandle == nullptr)
{
PluginLog("[EAC] AC HANDLE NULL RegisterPlayer");
return false;
}
if (EOS_ProductUserId_FromString(szMiddlewareUserID) == g_EOSUserID)
{
g_goUserID = goUserID;
return true;
}
EOS_AntiCheatClient_RegisterPeerOptions opts = {};
opts.ApiVersion = EOS_ANTICHEATCLIENT_REGISTERPEER_API_LATEST;
opts.PeerHandle = (void*)goUserID;
opts.ClientType = EOS_EAntiCheatCommonClientType::EOS_ACCCT_ProtectedClient;
opts.ClientPlatform = EOS_EAntiCheatCommonClientPlatform::EOS_ACCCP_Windows;
opts.AuthenticationTimeout = EOS_ANTICHEATCLIENT_REGISTERPEER_MAX_AUTHENTICATIONTIMEOUT;
opts.AccountId_DEPRECATED = nullptr;
opts.IpAddress = nullptr;
opts.PeerProductUserId = EOS_ProductUserId_FromString(szMiddlewareUserID);
EOS_EResult res = EOS_AntiCheatClient_RegisterPeer(acHandle, &opts);
if (opts.PeerProductUserId == g_EOSUserID)
{
PluginLog("[EAC] RegisterPlayer: Registering local player %s - %d!", szMiddlewareUserID, goUserID);
g_goUserID = goUserID;
}
else
{
PluginLog("[EAC] RegisterPlayer: Registering remote player %s - %d!", szMiddlewareUserID, goUserID);
}
if (res != EOS_EResult::EOS_Success)
{
PluginLog("[EAC] RegisterPlayer ERROR: %s!", EOS_EResult_ToString(res));
return false;
}
return true;
}
void EndSession()
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
EOS_HAntiCheatClient acHandle = EOS_Platform_GetAntiCheatClientInterface(g_EOSPlatformHandle);
if (acHandle == nullptr)
{
PluginLog("[EAC] AC HANDLE NULL 1");
return;
}
g_bEventsHooked = false;
// Remove all registered notification callbacks before ending the session
if (g_NotifyClientIntegrityViolatedId != 0)
{
EOS_AntiCheatClient_RemoveNotifyClientIntegrityViolated(acHandle, g_NotifyClientIntegrityViolatedId);
g_NotifyClientIntegrityViolatedId = 0;
PluginLog("[EAC] Removed ClientIntegrityViolated callback");
}
if (g_NotifyMessageToPeerId != 0)
{
EOS_AntiCheatClient_RemoveNotifyMessageToPeer(acHandle, g_NotifyMessageToPeerId);
g_NotifyMessageToPeerId = 0;
PluginLog("[EAC] Removed MessageToPeer callback");
}
if (g_NotifyPeerAuthStatusChangedId != 0)
{
EOS_AntiCheatClient_RemoveNotifyPeerAuthStatusChanged(acHandle, g_NotifyPeerAuthStatusChangedId);
g_NotifyPeerAuthStatusChangedId = 0;
PluginLog("[EAC] Removed PeerAuthStatusChanged callback");
}
if (g_NotifyPeerActionRequiredId != 0)
{
EOS_AntiCheatClient_RemoveNotifyPeerActionRequired(acHandle, g_NotifyPeerActionRequiredId);
g_NotifyPeerActionRequiredId = 0;
PluginLog("[EAC] Removed PeerActionRequired callback");
}
EOS_AntiCheatClient_EndSessionOptions endSessionOpts = {};
endSessionOpts.ApiVersion = EOS_ANTICHEATCLIENT_ENDSESSION_API_LATEST;
EOS_EResult result = EOS_AntiCheatClient_EndSession(acHandle, &endSessionOpts);
if (result != EOS_EResult::EOS_Success)
{
PluginLog("[EAC] MIDDLEWARE ERROR, END SESSION: %s!", EOS_EResult_ToString(result));
}
else
{
PluginLog("[EAC] End session succeeded: %s!", EOS_EResult_ToString(result));
}
}
void RefreshToken(const char* szGameToken, LoginCallback cb)
{
// Validate token pointer before logging to prevent format string vulnerabilities
if (szGameToken != nullptr)
{
PluginLog("[EAC] Refresh Token: %s", szGameToken);
}
else
{
PluginLog("[EAC] Refresh Token: (null token)");
}
// refresh shouldnt need to create accounts etc, so should be fine to do less things
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
g_LoginCallback = cb;
}
if (szGameToken != nullptr)
{
PluginLog("[EAC] Connect EOS: %s", szGameToken);
}
else
{
PluginLog("[EAC] Connect EOS: (null token)");
}
EOS_HPlatform platformHandle = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
if (g_EOSPlatformHandle == nullptr)
{
PluginLog("[EAC] MIDDLEWARE ERROR: Platform not initialized!");
if (cb != nullptr)
{
cb(false);
}
return;
}
platformHandle = g_EOSPlatformHandle;
}
// Lock released before async operation
EOS_HConnect ConnectHandle = EOS_Platform_GetConnectInterface(platformHandle);
if (ConnectHandle == nullptr)
{
PluginLog("[EAC] MIDDLEWARE ERROR: Connect handle is null!");
if (cb != nullptr)
{
cb(false);
}
return;
}
EOS_Connect_Credentials Credentials = {};
Credentials.ApiVersion = EOS_CONNECT_CREDENTIALS_API_LATEST;
Credentials.Token = szGameToken;
Credentials.Type = EOS_EExternalCredentialType::EOS_ECT_OPENID_ACCESS_TOKEN;
EOS_Connect_LoginOptions Options = {};
Options.ApiVersion = EOS_CONNECT_LOGIN_API_LATEST;
Options.Credentials = &Credentials;
EOS_Connect_UserLoginInfo userLoginInfo = {};
userLoginInfo.ApiVersion = EOS_CONNECT_USERLOGININFO_API_LATEST;
userLoginInfo.DisplayName = nullptr; // not set for oauth, retrieved from token instead
userLoginInfo.NsaIdToken = nullptr;
Options.UserLoginInfo = &userLoginInfo;
// TODO: Start a timeout
EOS_Connect_Login(ConnectHandle, &Options, nullptr, [](const EOS_Connect_LoginCallbackInfo* Data)
{
if (Data == nullptr)
{
return;
}
// TODO: Clear timeout
if (Data->ResultCode == EOS_EResult::EOS_Success)
{
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
g_EOSUserID = Data->LocalUserId;
}
char szBuffer[EOS_PRODUCTUSERID_MAX_LENGTH + 1] = { 0 };
int32_t outLen = sizeof(szBuffer);
EOS_ProductUserId_ToString(Data->LocalUserId, szBuffer, &outLen);
PluginLog("[EAC] Login Complete: %s", szBuffer);
// NOTE: dont need to hook up events again
LoginCallback localCallback = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
localCallback = g_LoginCallback;
g_LoginCallback = nullptr;
}
// Lock released before calling callback
if (localCallback != nullptr)
{
localCallback(Data->ResultCode == EOS_EResult::EOS_Success);
}
}
else
{
PluginLog("[EAC] Token Refresh Failed: %p", Data);
LoginCallback localCallback = nullptr;
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
localCallback = g_LoginCallback;
g_LoginCallback = nullptr;
}
// Lock released before calling callback
if (localCallback != nullptr)
{
localCallback(Data->ResultCode == EOS_EResult::EOS_Success);
}
}
});
}
void Login(const char* szGameToken, LoginCallback cb)
{
std::lock_guard<std::recursive_mutex> lock(g_StateMutex);
g_LoginCallback = cb;
// Validate token pointer before logging to prevent format string vulnerabilities
if (szGameToken != nullptr)
{
PluginLog("[EAC] Connect EOS: %s", szGameToken);
}
else
{
PluginLog("[EAC] Connect EOS: (null token)");
}
PluginLog("[EAC] A");
PluginLog("[EAC] B");
if (g_EOSPlatformHandle == nullptr)
{
PluginLog("[EAC] MIDDLEWARE ERROR: Platform not initialized!");
if (cb != nullptr)
{
cb(false);
}
return;
}
PluginLog("[EAC] C");
EOS_HConnect ConnectHandle = EOS_Platform_GetConnectInterface(g_EOSPlatformHandle);
if (ConnectHandle == nullptr)
{
PluginLog("[EAC] MIDDLEWARE ERROR: Connect handle is null!");
if (cb != nullptr)
{
cb(false);
}
return;
}
EOS_Connect_Credentials Credentials = {};
Credentials.ApiVersion = EOS_CONNECT_CREDENTIALS_API_LATEST;
Credentials.Token = szGameToken;
Credentials.Type = EOS_EExternalCredentialType::EOS_ECT_OPENID_ACCESS_TOKEN;
EOS_Connect_LoginOptions Options = {};
Options.ApiVersion = EOS_CONNECT_LOGIN_API_LATEST;
Options.Credentials = &Credentials;
EOS_Connect_UserLoginInfo userLoginInfo = {};
userLoginInfo.ApiVersion = EOS_CONNECT_USERLOGININFO_API_LATEST;
userLoginInfo.DisplayName = nullptr; // not set for oauth, retrieved from token instead
userLoginInfo.NsaIdToken = nullptr;
Options.UserLoginInfo = &userLoginInfo;
PluginLog("[EAC] D");
// TODO: Start a timeout
EOS_Connect_Login(ConnectHandle, &Options, nullptr, [](const EOS_Connect_LoginCallbackInfo* Data)
{
PluginLog("[EAC] done");
if (Data == nullptr)
{
return;