-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsteam.cpp
More file actions
914 lines (786 loc) · 27.2 KB
/
steam.cpp
File metadata and controls
914 lines (786 loc) · 27.2 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
#include "framework.h"
#include "steam/steam_api.h"
#include "SteamCBHandler.h"
#include "bdAuthXB1toSteam.h"
#include "steamugc.h"
#include "security.h"
#include "linux_helper.h"
#include <unordered_map>
#include <intrin.h>
#include <direct.h>
enum SteamAuthStatus
{
SteamAuthStatus_Unauthorized = 0,
SteamAuthStatus_ReceivedPending = 1,
SteamAuthStatus_OK = 2
};
struct SteamAuthInfo
{
SteamAuthStatus status;
uint64_t timeout;
uint64_t retry;
uint32_t retry_count;
};
void check_steamauth();
// the steam information fetched upon init
uint64_t gSteamID = 0;
const char* gSteamPlayername = NULL;
bool gSteamInit = false;
uint64_t gLobbyID = 0;
// are our stats dirty
bool gSteamStatsDirty = false;
// the original XUID of the user if GDK stuff is in use
uint64_t gOriginalXUID = 0;
// global handle for XUser stuff
void** gGlobalXUserHandle = (void**)REBASE(0x148f76c0);
// replace the XUID with the SteamID64
MDT_Define_FASTCALL(REBASE(0x29222c8), XalUserGetId_hook, int, (void* xuser, uint64_t* output))
{
// this is only ever used to reference the current user and is used before gGlobalXUserHandle is populated
// NOTE(Emma): if Xbox stuff is still enabled we want to make sure any Xal functions use the original XUID
#ifdef USES_GDK
// if we don't have our original xuid already, make it so
if (gOriginalXUID == 0) {
MDT_ORIGINAL(XalUserGetId_hook, (xuser, &gOriginalXUID));
ALOG("Original XUID: %llu\n", gOriginalXUID);
}
// only return the SteamID64 for
// TODO(Emma): figure out if this is good enough or if this still breaks dw
if (*gGlobalXUserHandle == NULL || xuser == *gGlobalXUserHandle)
*output = gSteamID;
else {
ALOG("Forwarding XUID to original instead of SteamID64");
return MDT_ORIGINAL(XalUserGetId_hook, (xuser, output));
}
#else
// xbl is disabled, return steamid
*output = gSteamID;
#endif
return 0;
}
// replace the username with the Steam persona name
MDT_Define_FASTCALL(REBASE(0x2922220), XalUserGetGamertag_hook, int, (void *xuser, int component, size_t gt_size, char *output, size_t *output_size))
{
// since this function is used a lot and i can't be bothered checking if everywhere is referencing the current user
// if it isn't referencing our global user then call into the original function
if (xuser != *gGlobalXUserHandle)
return MDT_ORIGINAL(XalUserGetGamertag_hook, (xuser, component, gt_size, output, output_size));
if (*security::conf.playername)
{
strncpy(output, security::conf.playername, gt_size);
*output_size = min(strlen(security::conf.playername), gt_size);
}
else
{
gSteamPlayername = SteamFriends()->GetPersonaName();
strncpy(output, gSteamPlayername, gt_size);
*output_size = min(strlen(gSteamPlayername), gt_size);
}
return 0;
}
// the packet "format" for a dwInstantMessage sent via Steam P2P
typedef struct _dwInstantMsgSteamPacket {
int id;
uint8_t instantMsg[0x400];
int instantMsgLength;
} dwInstantMsgSteamPacket;
// since we can't dispatch the message in our own thread,
// keep track of the most recent instant message to dispatch on the game thread
bool instantPacketReady = false;
CSteamID instantPacketFrom;
dwInstantMsgSteamPacket instantPacket = { 0 };
#define dwInstantDispatchMessage(fromId, controllerIndex, data, size) ((void(__fastcall*)(uint64_t, int, uint8_t *, int))REBASE(0x14f6710))(fromId, controllerIndex, data, size)
// doing friends stuff every now and again
uint64_t lastFriendsUpdateTime = 0;
void LiveFriends_Update_hook(); // forward declare
// notify the user that Linux isn't quite supported yet
bool show_linux_msg = false;
// we run this every frame in the game thread
void steam_dispatch_every_frame()
{
if (instantPacketReady)
{
dwInstantDispatchMessage(instantPacketFrom.ConvertToUint64(), 0, instantPacket.instantMsg, instantPacket.instantMsgLength);
instantPacketReady = false;
}
if (SteamCBHandler()->hasPendingInvite)
{
SteamCBHandler()->hasPendingInvite = false;
char xuidStr[64]{ 0 }; // <3
sprintf(xuidStr, "join %llu\n", SteamCBHandler()->pendingInvite.ConvertToUint64());
Cbuf_AddText(xuidStr);
}
check_steamauth();
uint64_t currentTime = time(NULL);
if (currentTime > (lastFriendsUpdateTime + 2))
{
LiveFriends_Update_hook();
lastFriendsUpdateTime = currentTime;
}
if (show_linux_msg)
{
show_linux_msg = false;
Com_Error(ERROR_UI, "Wine support is experimental in BO3Enhanced. Online play does not currently work, please don't file an issue report.");
}
if (g_needsUpdatePrompt)
{
g_needsUpdatePrompt = false;
Com_Error(ERROR_UI, "A new update has been installed for BO3Enhanced. Please restart the game to finish installing changes.");
}
}
void parse_steam_p2p_msg(CSteamID from, uint8_t* buf, uint32_t size) {
// TODO(Emma): implement, might be needed for lobbies with Steam hosts
if (buf[0] == 0x16) // auth ticket request
{
ALOG("unimplemented p2p steam auth request 0x16");
}
else if (buf[0] == 0x15) // recv auth ticket
{
ALOG("unimplemented p2p steam auth request 0x15");
}
// handle instant messages
else if (buf[0] == 0x28 && size == sizeof(dwInstantMsgSteamPacket)) {
while (instantPacketReady) {
Sleep(1); // wait for the ready state to be set to false
// NOTE(Emma): might be a bad idea since we'd be blocking our steam callbacks thread...
}
memcpy(&instantPacket, buf, sizeof(instantPacket));
instantPacketFrom = from;
instantPacketReady = true;
}
else
{
ALOG("unimplemented invalid packet");
}
}
void get_script_checksum(int scriptInst, uint32_t buf[3])
{
auto off = 120llu * scriptInst;
buf[0] = *(uint32_t*)(REBASE(0x3EE6810) + off + 72);
buf[1] = *(uint32_t*)(REBASE(0x3EE6810) + off + 76);
buf[2] = *(uint32_t*)(REBASE(0x3EE6810) + off + 80);
}
bool hasCachedTicket = false;
HAuthTicket cachedTicket = 0;
uint32_t cachedTicketLength = 0;
char cachedTicketBuf[0x1001]{ 0 };
void SteamAuth_HandleRequest(uint32_t controller, XUID senderXUID, uint8_t forceNew)
{
uint32_t script_checksums[3];
char msgBuf[2048];
msg_t reply;
get_script_checksum(1, script_checksums);
MSG_Init(&reply, msgBuf, sizeof(msgBuf));
MSG_WriteString(&reply, "steamAuth");
MSG_WriteInt64(&reply, GetXUID(0));
MSG_WriteInt64(&reply, script_checksums[0]);
if (!controller)
{
if (forceNew && hasCachedTicket)
{
hasCachedTicket = false;
SteamUser()->CancelAuthTicket(cachedTicket);
cachedTicketLength = cachedTicket = 0;
memset(cachedTicketBuf, 0, sizeof(cachedTicketBuf));
}
cachedTicket = SteamUser()->GetAuthSessionTicket(cachedTicketBuf, sizeof(cachedTicketBuf) - 1, &cachedTicketLength, NULL);
MSG_WriteUInt16(&reply, (uint16_t)cachedTicketLength);
MSG_WriteData(&reply, cachedTicketBuf, (uint16_t)cachedTicketLength);
}
else
{
MSG_WriteUInt16(&reply, 0);
}
netadr_t host = *(netadr_t*)((*(uint64_t*)(REBASE(0x40A9288)) + 0x25780llu * controller) + 16);
NET_OutOfBandData(NS_CLIENT1, host, reply.data, reply.cursize);
}
std::unordered_map<XUID, SteamAuthInfo> auth_cache;
void SteamAuth_HandleResponse(msg_t* msg, XUID senderXUID, uint16_t length, uint64_t scriptChecksum)
{
uint32_t script_checksums[3];
char ticketBuf[0x1001]{ 0 };
if (length >= sizeof(ticketBuf))
{
ALOG("REJECTED AUTH RESPONSE: %llu sent a buffer too big to be processed", senderXUID);
return;
}
// known clients will be placed in this cache when they join the match
if (auth_cache.find(senderXUID) == auth_cache.end())
{
return;
}
// check that we need their auth in the first place
if (auth_cache[senderXUID].status == SteamAuthStatus_OK)
{
ALOG("IGNORED AUTH RESPONSE: %llu doesnt need an auth response", senderXUID);
return;
}
// check that script checksum is valid
get_script_checksum(1, script_checksums);
if (script_checksums[0] != (uint32_t)scriptChecksum)
{
ALOG("REJECTED AUTH RESPONSE: %llu sent an invalid checksum", senderXUID);
return;
}
MSG_ReadData(msg, ticketBuf, length);
auto res = SteamUser()->BeginAuthSession(ticketBuf, length, senderXUID);
if (res != k_EBeginAuthSessionResultOK)
{
ALOG("REJECTED AUTH RESPONSE: %llu sent an invalid ticket", senderXUID);
return;
}
if (auth_cache[senderXUID].status < SteamAuthStatus_ReceivedPending)
{
ALOG("AUTH RESPONSE: %llu sent a valid ticket, waiting on identity verification", senderXUID);
auth_cache[senderXUID].status = SteamAuthStatus_ReceivedPending;
}
}
void OnGotAuthTicketResponse(ValidateAuthTicketResponse_t* pCallback)
{
auto xuid = (XUID)pCallback->m_SteamID.ConvertToUint64();
if (pCallback->m_eAuthSessionResponse != k_EAuthSessionResponseOK)
{
ALOG("AUTH TICKET INVALID FOR %llu, reason %u", xuid, pCallback->m_eAuthSessionResponse);
if (auth_cache[xuid].status != SteamAuthStatus_OK)
{
auth_cache[xuid].status = SteamAuthStatus_Unauthorized;
}
return;
}
ALOG("STEAM AUTHORIZATION COMPLETED FOR %llu", xuid);
auth_cache[xuid].status = SteamAuthStatus_OK;
}
uint64_t next_steamauth_check = 0;
void check_steamauth()
{
// dont check steamauth in UI level
if (s_runningUILevel)
{
return;
}
// only tick if we are the host
if (!HasHost())
{
return;
}
// only tick at an interval
if (GetTickCount64() < next_steamauth_check)
{
return;
}
next_steamauth_check = GetTickCount64() + STEAMAUTH_TICK_DELAY;
char msgBuf[2048];
LobbyType sessionType = LOBBY_TYPE_PRIVATE;
if (LobbySession_GetControllingLobbySession(LOBBY_MODULE_CLIENT))
{
sessionType = LOBBY_TYPE_GAME;
}
auto session = LobbySession_GetSession(sessionType);
for (int i = 0; i < 18; i++)
{
// get client info for this index
auto client = LobbySession_GetClientByClientNum(session, i);
// if not active client, skip
if (!client->activeClient)
{
continue;
}
// if the client is the host or the host's splitscreen player, skip
auto xuid = client->activeClient->fixedClientInfo.xuid;
if (xuid == GetXUID(0) || xuid == GetXUID(1))
{
continue;
}
// cant steamauth anyone that isnt a real steam account
if (CSteamID(xuid).GetEAccountType() != k_EAccountTypeIndividual)
{
continue;
}
// if the client has no authinfo, we need to send them an auth request
if (auth_cache.find(xuid) == auth_cache.end())
{
auth_cache[xuid] = SteamAuthInfo();
auth_cache[xuid].status = SteamAuthStatus_Unauthorized;
auth_cache[xuid].timeout = GetTickCount64() + STEAMAUTH_FAIL_TIMEOUT;
auth_cache[xuid].retry = 0;
auth_cache[xuid].retry_count = 0;
goto send_auth;
}
// client already authed, skip
if (auth_cache[xuid].status == SteamAuthStatus_OK)
{
continue;
}
// if client took too long, kick them
if (auth_cache[xuid].timeout <= GetTickCount64())
{
auth_cache.erase(xuid);
ALOG("STEAMAUTH TIMEOUT FOR %llu, KICKING", xuid);
memset(msgBuf, 0, sizeof(msgBuf));
sprintf_s(msgBuf, "xuidkick_for_reason %llu PLATFORM_STEAM_CONNECT_FAIL\n", xuid);
Cbuf_AddText(msgBuf);
continue;
}
// not ready to retry yet
if (auth_cache[xuid].retry > GetTickCount64())
{
continue;
}
send_auth:
{
ALOG("STEAMAUTH: sending %llu a steamauth request, attempt %u", xuid, auth_cache[xuid].retry_count + 1);
msg_t msg;
memset(msgBuf, 0, sizeof(msgBuf));
MSG_Init(&msg, msgBuf, sizeof(msgBuf));
MSG_WriteString(&msg, "steamAuthReq");
MSG_WriteInt64(&msg, GetXUID(0));
// if we arent on a multiple of retry delays that coincides with steamauth delay time, allow an old ticket to be sent again
if ((auth_cache[xuid].retry_count * STEAMAUTH_CLIENT_RETRY_DELAY) % STEAMAUTH_DELAY_NEW_TIME)
{
MSG_WriteBit0(&msg);
}
else
{
MSG_WriteBit1(&msg);
}
auto adr = LobbySession_GetClientNetAdrByIndex(sessionType, i);
NET_OutOfBandData(NS_SERVER, adr, msg.data, msg.cursize);
auth_cache[xuid].retry = GetTickCount64() + STEAMAUTH_CLIENT_RETRY_DELAY;
++auth_cache[xuid].retry_count;
// force the next steamauth check to be 1 second from now and break the loop such that we dont send multiple clients auth requests within the same second
next_steamauth_check = GetTickCount64() + 1000;
break;
}
}
}
MDT_Define_FASTCALL(REBASE(0x16579F0), Unk_G_InitGame_Sub16579F0_hook, uint64_t, ())
{
auth_cache.clear();
return MDT_ORIGINAL(Unk_G_InitGame_Sub16579F0_hook, ());
}
MDT_Define_FASTCALL(REBASE(0x13F7750), CL_DispatchConnectionlessPacket_hook, uint32_t, (uint32_t controller, netadr_t from, msg_t* msg))
{
int* v7 = *(int**)(*(uint64_t*)(*(uint64_t*)__readgsqword(0x58u) + 744llu) + 24llu);
auto firstArg = "";
if (v7[*v7 + 25] > 0)
{
firstArg = **(const CHAR***)&v7[2 * *v7 + 34];
}
if (!istrcmp_("steamAuthReq", firstArg))
{
XUID serverID = MSG_ReadInt64(msg);
uint8_t forceNew = MSG_ReadBit(msg);
ALOG("Handling steamAuthReq: %llu %u", serverID, forceNew);
SteamAuth_HandleRequest(controller, serverID, forceNew);
return 1;
}
return MDT_ORIGINAL(CL_DispatchConnectionlessPacket_hook, (controller, from, msg));
}
MDT_Define_FASTCALL(REBASE(0x23249E0), SV_ConnectionlessPacket_hook, void, (netadr_t from, msg_t* msg))
{
char strBuf[1024]{ 0 };
msg_t cpy = *msg;
MSG_ReadInt32(&cpy);
MSG_ReadStringLine(&cpy, strBuf, sizeof(strBuf));
if (!istrcmp_("steamauth", strBuf))
{
XUID sender = MSG_ReadInt64(&cpy);
uint64_t scriptChecksum = MSG_ReadInt64(&cpy);
uint16_t length = MSG_ReadInt16(&cpy);
ALOG("Handling steamauth response: %llu %u len(%u)", sender, scriptChecksum, (uint32_t)length);
if (length)
{
SteamAuth_HandleResponse(&cpy, sender, length, scriptChecksum);
}
else
{
ALOG("REJECTED AUTH RESPONSE: %llu sent an empty buffer", sender);
}
return;
}
MDT_ORIGINAL(SV_ConnectionlessPacket_hook, (from, msg));
}
DWORD steam_callbacks_thread(void *arg)
{
while (true)
{
SteamAPI_RunCallbacks();
Sleep(33);
// check for steam networking incoming packets
{
uint32_t incomingPacketSize = 0;
uint8_t incomingPacketBuf[0x800] = { 0 };
CSteamID incomingPacketFrom;
if (SteamNetworking()->IsP2PPacketAvailable(&incomingPacketSize))
{
// if the message is too big then discard it
if (incomingPacketSize > sizeof(incomingPacketBuf)) {
SteamNetworking()->ReadP2PPacket(incomingPacketBuf, sizeof(incomingPacketBuf), &incomingPacketSize, &incomingPacketFrom);
ALOG("discarding oversized message from %llu", incomingPacketFrom.ConvertToUint64());
}
else {
SteamNetworking()->ReadP2PPacket(incomingPacketBuf, sizeof(incomingPacketBuf), &incomingPacketSize, &incomingPacketFrom);
ALOG("steam user %llu sent us 0x%x bytes", incomingPacketFrom.ConvertToUint64(), incomingPacketSize);
parse_steam_p2p_msg(incomingPacketFrom, incomingPacketBuf, incomingPacketSize);
}
}
}
// store our stats if they're dirty
if (gSteamStatsDirty) {
ALOG("storing stats");
SteamUserStats()->StoreStats();
gSteamStatsDirty = false;
}
}
return 0;
}
// achievement unlocking hook
MDT_Define_FASTCALL(REBASE(0x1f04fd0), Live_AwardAchievement_Hook, void, (int controllerIndex, char *achievementName))
{
ALOG("unlocking %s on Steam", achievementName);
SteamUserStats()->SetAchievement(achievementName);
gSteamStatsDirty = true;
#ifdef USES_GDK
MDT_ORIGINAL(Live_AwardAchievement_Hook, (controllerIndex, achievementName));
#endif
}
MDT_Define_FASTCALL(REBASE(0x2cb6430), XblAchievementsUpdateAchievementAsync_Hook, int, (void* xblContext, uint64_t xuid, char* name, int progress, void* async))
{
#ifdef USES_GDK
ALOG("unlocking %s on Xbox Live for %llu", name, gOriginalXUID);
return MDT_ORIGINAL(XblAchievementsUpdateAchievementAsync_Hook, (xblContext, gOriginalXUID, name, progress, async));
#else
return 0;
#endif
}
MDT_Define_FASTCALL(REBASE(0x1EEDBE0), ShowPlatformProfile_hook, void, (XUID xuid)) // <3
{
if ((uint64_t)_ReturnAddress() == REBASE(0x201F558))
{
SteamFriends()->ActivateGameOverlayToUser("friendadd", CSteamID(xuid));
return;
}
SteamFriends()->ActivateGameOverlayToUser("steamid", CSteamID(xuid));
}
MDT_Define_FASTCALL(REBASE(0x292049C), XGameSaveFilesGetFolderWithUiResult_hook, uint64_t, (uint64_t a1, size_t folderSize, char* result))
{
if (!_getcwd(result, folderSize))
{
return MDT_ORIGINAL(XGameSaveFilesGetFolderWithUiResult_hook, (a1, folderSize, result));
}
return 0;
}
typedef struct _PresenceData {
char unk1[0x8];
uint64_t XUID;
char unk2[0x30];
int state;
int activity;
int ctx;
int joinable;
int gametypeID;
int mapID;
int difficulty;
int playlist;
char unk_[0x318];
} PresenceData;
typedef struct _FriendInfo {
char name[17];
char pad[0x8];
PresenceData presence;
int lastFetchedTime;
char unk[0x4C];
} FriendInfo;
int sizeofFriendInfo = sizeof(FriendInfo); // 1000
typedef struct _FriendsListTime {
int all;
int online;
int title;
} FriendsListTime;
typedef struct _SortInfo {
int index;
FriendInfo* friendInfo;
} SortInfo;
typedef struct _FriendsListSorting {
SortInfo online[100];
SortInfo alphabetical[100];
SortInfo* current;
} FriendsListSorting;
typedef struct _FriendsList {
FriendInfo* friends;
int numFriends;
int maxNumFriends;
FriendsListTime requestTime;
FriendsListTime fetchTime;
FriendsListSorting sorting;
} FriendsList;
// https://stackoverflow.com/a/28311607
static bool is_valid_utf8(const char* string)
{
if (string == NULL)
return false;
const unsigned char* bytes = (const unsigned char*)string;
unsigned int cp;
int num;
while (*bytes != 0x00)
{
if ((*bytes & 0x80) == 0x00)
{
// U+0000 to U+007F
cp = (*bytes & 0x7F);
num = 1;
}
else if ((*bytes & 0xE0) == 0xC0)
{
// U+0080 to U+07FF
cp = (*bytes & 0x1F);
num = 2;
}
else if ((*bytes & 0xF0) == 0xE0)
{
// U+0800 to U+FFFF
cp = (*bytes & 0x0F);
num = 3;
}
else if ((*bytes & 0xF8) == 0xF0)
{
// U+10000 to U+10FFFF
cp = (*bytes & 0x07);
num = 4;
}
else
return false;
bytes += 1;
for (int i = 1; i < num; ++i)
{
if ((*bytes & 0xC0) != 0x80)
return false;
cp = (cp << 6) | (*bytes & 0x3F);
bytes += 1;
}
if ((cp > 0x10FFFF) ||
((cp >= 0xD800) && (cp <= 0xDFFF)) ||
((cp <= 0x007F) && (num != 1)) ||
((cp >= 0x0080) && (cp <= 0x07FF) && (num != 2)) ||
((cp >= 0x0800) && (cp <= 0xFFFF) && (num != 3)) ||
((cp >= 0x10000) && (cp <= 0x1FFFFF) && (num != 4)))
return false;
}
return true;
}
static void validate_and_copy_name(const char* name, char* output)
{
bool invalid = false;
size_t namelen = strlen(name);
// make sure it's not too short/too long
if (namelen < 2 || namelen > 16)
invalid = true;
// make sure it's valid UTF8
if (!is_valid_utf8(name))
invalid = true;
// check for invalid characters
for (int i = 0; i < namelen; i++)
if (name[i] == '^' || name[i] == '@' || name[i] == '{' || name[i] == '$')
invalid = true;
// if the name isn't valid then they're an unknown soldier
if (invalid == true)
strncpy(output, "Unknown Soldier", 0x11);
else
{
strncpy(output, name, 0x10);
output[0x10] = 0; // null terminate
}
}
static void fill_in_friend(CSteamID steamID, FriendInfo* info)
{
memset(info, 0, sizeof(info));
uint64_t uintsteam = steamID.ConvertToUint64();
// copy the player name and XUID
validate_and_copy_name(SteamFriends()->GetFriendPersonaName(steamID), info->name);
info->presence.XUID = steamID.ConvertToUint64();
*(int*)info->presence.unk_ = time(NULL);
info->presence.unk_[4] = 0x12;
if (SteamFriends()->GetFriendPersonaState(steamID) == k_EPersonaStateOffline)
{
info->presence.state = 0x08;
info->presence.activity = 0x00; // PRESENCE_ACTIVITY_OFFLINE
info->presence.gametypeID = 0xFFFFFFFF;
info->presence.mapID = 0xFFFFFFFF;
return;
}
FriendGameInfo_t currentGame;
// check if user is playing BO3 (GetFriendGamePlayed fails = no game, PS3 mode engaged)
if (!SteamFriends()->GetFriendGamePlayed(steamID, ¤tGame) ||
currentGame.m_gameID.AppID() != 311210) {
info->presence.state = 0x04;
info->presence.activity = 0x01; // PRESENCE_ACTIVITY_ONLINE_NOT_IN_TITLE
info->presence.gametypeID = 0xFFFFFFFF;
info->presence.mapID = 0xFFFFFFFF;
return;
}
info->presence.state = 0x04;
info->presence.activity = 0x07; // PRESENCE_ACTIVITY_IN_TITLE
// they are playing BO3 so get rich presence data
const char* state_str = SteamFriends()->GetFriendRichPresence(steamID, "state");
const char *tActivity_str = SteamFriends()->GetFriendRichPresence(steamID, "tActivity");
const char* tCtx_str = SteamFriends()->GetFriendRichPresence(steamID, "tCtx");
const char* tJoinable_str = SteamFriends()->GetFriendRichPresence(steamID, "tJoinable");
const char* tGametype_str = SteamFriends()->GetFriendRichPresence(steamID, "tGametype");
const char* tMapId_str = SteamFriends()->GetFriendRichPresence(steamID, "tMapId");
const char* tDifficulty_str = SteamFriends()->GetFriendRichPresence(steamID, "tDifficulty");
const char* tPlaylist_str = SteamFriends()->GetFriendRichPresence(steamID, "tPlaylist");
// blank state string assume presence isn't valid
if (strlen(state_str) == 0)
return;
// otherwise fill in our struct
//info->presence.state |= atoi(state_str);
info->presence.activity = atoi(tActivity_str);
info->presence.ctx = atoi(tCtx_str);
info->presence.joinable = atoi(tJoinable_str);
info->presence.gametypeID = atoi(tGametype_str);
info->presence.mapID = atoi(tMapId_str);
info->presence.difficulty = atoi(tDifficulty_str);
info->presence.playlist = atoi(tPlaylist_str);
}
MDT_Define_FASTCALL(REBASE(0x1ee7610), LiveFriends_Update_hook, void, (void))
{
FriendsList* friendsList = (FriendsList*)REBASE(0x148f1510);
// clear out some of the friends list struct
friendsList->numFriends = 0;
int allowedFriends = SteamFriends()->GetFriendCount(k_EFriendFlagImmediate);
// enumerate through all steam friends
for (int i = 0; i < allowedFriends; i++)
{
int idx = friendsList->numFriends;
CSteamID cppDoesntLetYouSayFriend = SteamFriends()->GetFriendByIndex(i, k_EFriendFlagImmediate);
fill_in_friend(cppDoesntLetYouSayFriend, &friendsList->friends[idx]);
friendsList->sorting.alphabetical[idx].index = idx;
friendsList->sorting.alphabetical[idx].friendInfo = &friendsList->friends[idx];
friendsList->sorting.online[idx].index = idx;
friendsList->sorting.online[idx].friendInfo = &friendsList->friends[idx];
friendsList->numFriends++;
// cap out at 100 friends
if (friendsList->numFriends >= friendsList->maxNumFriends)
break;
}
}
MDT_Define_FASTCALL(REBASE(0x1f95bd0), LivePresence_UpdatePlatform_hook, void, (int controller, PresenceData* presence))
{
// set stuff we can't rip out of the presence blob
SteamFriends()->SetRichPresence("ref", "PRESENCE_NOTINGAME"); // never properly set
SteamFriends()->SetRichPresence("params", ""); // unsure, never seen it different
SteamFriends()->SetRichPresence("status", "BO3Enhanced"); // "Main Menu", "Multiplayer", "Zombies" etc
SteamFriends()->SetRichPresence("version", "1"); // unsure, always 1
// and set the stuff we can!
static char stateBuf[14];
static char tActivityBuf[14];
static char tCtxBuf[14];
static char tJoinableBuf[14];
static char tGametypeBuf[14];
static char tMapIdBuf[14];
static char tDifficultyBuf[14];
static char tPlaylistBuf[14];
snprintf(stateBuf, sizeof(stateBuf), "%i", presence->state);
snprintf(tActivityBuf, sizeof(tActivityBuf), "%i", presence->activity);
snprintf(tCtxBuf, sizeof(tCtxBuf), "%i", presence->ctx);
snprintf(tJoinableBuf, sizeof(tJoinableBuf), "%i", presence->joinable);
snprintf(tGametypeBuf, sizeof(tGametypeBuf), "%i", presence->gametypeID);
snprintf(tMapIdBuf, sizeof(tMapIdBuf), "%i", presence->mapID);
snprintf(tDifficultyBuf, sizeof(tDifficultyBuf), "%i", presence->difficulty);
snprintf(tPlaylistBuf, sizeof(tPlaylistBuf), "%i", presence->playlist);
SteamFriends()->SetRichPresence("state", stateBuf);
SteamFriends()->SetRichPresence("tActivity", tActivityBuf);
SteamFriends()->SetRichPresence("tCtx", tCtxBuf);
SteamFriends()->SetRichPresence("tJoinable", tJoinableBuf);
SteamFriends()->SetRichPresence("tGametype", tGametypeBuf);
SteamFriends()->SetRichPresence("tMapId", tMapIdBuf);
SteamFriends()->SetRichPresence("tDifficulty", tDifficultyBuf);
SteamFriends()->SetRichPresence("tPlaylist", tPlaylistBuf);
// tell steam if the lobby is joinable or not
if (presence->joinable == 1 || presence->joinable == 2) // LOBBY_JOINABLE_YES / LOBBY_JOINABLE_YES_FRIENDS_ONLY
SteamMatchmaking()->SetLobbyJoinable(CSteamID(gLobbyID), true);
else
SteamMatchmaking()->SetLobbyJoinable(CSteamID(gLobbyID), false);
#if USES_GDK
// set the presence on Xbox Live still, but disallow joining the lobby
PresenceData newPresenceData;
memcpy(&newPresenceData, presence, sizeof(PresenceData));
newPresenceData.joinable = 0xA; // LOBBY_JOINABLE_NO_YOU_NEED_DLC
MDT_ORIGINAL(LivePresence_UpdatePlatform_hook, (controller, &newPresenceData));
#endif
}
MDT_Define_FASTCALL(REBASE(0x1f44d90), Live_SendInvite_hook, void, (int controller, uint64_t xuid))
{
SteamMatchmaking()->InviteUserToLobby(CSteamID(gLobbyID), CSteamID(xuid));
}
void init_steamapi()
{
SetEnvironmentVariableA("SteamAppId", "311210");
SetEnvironmentVariableA("SteamGameId", "311210");
SetEnvironmentVariableA("SteamOverlayGameId", "311210");
// initialise the api
SteamErrMsg m;
if (SteamAPI_InitEx(&m) != k_ESteamAPIInitResult_OK) {
ALOG("Steam API Init failed: %s", m);
gSteamInit = false;
// NOTE(Emma): we need Steam to be functional on Linux as the GDK is generally unavailable -- our hooks are mandatory
if (is_on_linux())
{
MessageBoxA(NULL, "Steam is required to play Black Ops 3.\nPlease make sure Steam is open and running.", "BO3Enhanced", 0);
ExitProcess(0);
}
return;
}
ALOG("Steam initialised!");
gSteamInit = true;
// force them to subscribe to the updater workshop item
SteamUGC()->SubscribeItem(WSTORE_UPDATER_WSID);
// store and print our steamid and name
gSteamPlayername = SteamFriends()->GetPersonaName();
gSteamID = SteamUser()->GetSteamID().ConvertToUint64();
ALOG("Logged into Steam as \"%s\" (% llu)", gSteamPlayername, gSteamID);
// activate hooks for user info
MDT_Activate(XalUserGetId_hook);
MDT_Activate(XalUserGetGamertag_hook);
MDT_Activate(XGameSaveFilesGetFolderWithUiResult_hook);
// activate linux hooks
if (is_on_linux()) {
show_linux_msg = true;
do_linux_hooks();
}
// activate hooks for achievements
MDT_Activate(Live_AwardAchievement_Hook);
MDT_Activate(XblAchievementsUpdateAchievementAsync_Hook);
// handle steamauth requests from "server"
MDT_Activate(CL_DispatchConnectionlessPacket_hook);
MDT_Activate(SV_ConnectionlessPacket_hook);
MDT_Activate(Unk_G_InitGame_Sub16579F0_hook);
// show the steam profile for users instead of the xbox profile
MDT_Activate(ShowPlatformProfile_hook);
// invite users via steam instead of xbox
MDT_Activate(Live_SendInvite_hook);
// friends list patch
MDT_Activate(LiveFriends_Update_hook);
MDT_Activate(LivePresence_UpdatePlatform_hook);
gSteamCBHandler = new CSteamCBHandler();
gSteamCBHandler->OnValidateAuthTicket = OnGotAuthTicketResponse;
// run a thread and hook every frame to handle our callbacks
CreateThread(NULL, NULL, steam_callbacks_thread, NULL, NULL, NULL);
register_frame_event([]()
{
steam_dispatch_every_frame();
});
// request an appticket upon launch
uint8_t steamRequestData[0x58];
bdAuthXB1toSteam_createSteamRequestData(steamRequestData);
gSteamCBHandler->RequestEncryptedAppTicket(steamRequestData, sizeof(steamRequestData));
// add auth patches
apply_bdAuthXB1toSteam_patches();
// setup workshop content
steamugc::setup();
// create a default lobby for invite purposes
SteamMatchmaking()->CreateLobby(k_ELobbyTypePublic, 18);
// fix multiplayer dedicated server searches
chgmem<uint8_t>(REBASE(0x1FEAB49 + 2), 0); // lobbyDedicatedSearchSkip: 1 -> 0
chgmem<uint32_t>(REBASE(0x1FDE201) + 6, 0xD3FC12u); // changelist in LobbyHostMsg_SendJoinRequest -> steam changelist
}