/
CloneExperiments.cpp
5286 lines (4211 loc) · 138 KB
/
CloneExperiments.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <StdInc.h>
#include <CoreConsole.h>
#include <jitasm.h>
#include <Hooking.h>
#include <ScriptEngine.h>
#include <netBlender.h>
#include <netInterface.h>
#include <netObject.h>
#include <netObjectMgr.h>
#include <netSyncTree.h>
#include <rlNetBuffer.h>
#include <CloneManager.h>
#include <CrossBuildRuntime.h>
#include <ICoreGameInit.h>
#include <base64.h>
#include <NetLibrary.h>
#include <NetBuffer.h>
#include <Pool.h>
#include <EntitySystem.h>
#include <sysAllocator.h>
#include <ResourceManager.h>
#include <StateBagComponent.h>
#include <atArray.h>
#include <Error.h>
extern NetLibrary* g_netLibrary;
class CNetGamePlayer;
ICoreGameInit* icgi;
namespace rage
{
class netObject;
class netPlayerMgrBase
{
public:
virtual ~netPlayerMgrBase() = 0;
virtual void Initialize() = 0;
virtual void Shutdown() = 0;
virtual void m_18() = 0;
#ifdef GTA_FIVE
private:
virtual CNetGamePlayer* AddPlayer_raw(void* scInAddr, void* unkNetValue, void* addedIn1290, void* playerData, void* nonPhysicalPlayerData) = 0;
public:
#elif IS_RDR3
virtual CNetGamePlayer* AddPlayer(void* scInAddr, uint32_t activePlayerIndex, void* playerData, void* playerAccountId) = 0;
#endif
virtual void RemovePlayer(CNetGamePlayer* player) = 0;
void UpdatePlayerListsForPlayer(CNetGamePlayer* player);
#ifdef GTA_FIVE
public:
CNetGamePlayer* AddPlayer(void* scInAddr, void* unkNetValue, void* addedIn1290, void* playerData, void* nonPhysicalPlayerData)
{
if (xbr::IsGameBuildOrGreater<2372>())
{
static auto addPlayerFunc = *(uint64_t*)(*(char**)this + 0x20);
return ((CNetGamePlayer*(*)(void*, void*, void*, void*, void*))(addPlayerFunc))(this, scInAddr, unkNetValue, playerData, nonPhysicalPlayerData);
}
return AddPlayer_raw(scInAddr, unkNetValue, addedIn1290, playerData, nonPhysicalPlayerData);
}
#endif
CNetGamePlayer* GetLocalPlayer()
{
#ifdef GTA_FIVE
const int offset = (xbr::IsGameBuildOrGreater<2944>() ? 240 : 232);
return *(CNetGamePlayer**)((uint64_t)this + offset);
#elif IS_RDR3
return *(CNetGamePlayer**)((uint64_t)this + 232);
#endif
}
};
static hook::thiscall_stub<void(netPlayerMgrBase*, CNetGamePlayer*)> _netPlayerMgrBase_UpdatePlayerListsForPlayer([]
{
#ifdef GTA_FIVE
return hook::get_call(hook::get_pattern("FF 57 30 48 8B D6 49 8B CE E8", 9));
#elif IS_RDR3
return hook::get_call(hook::get_pattern("48 8B 01 FF 50 ? 49 8B D7 48 8B CE E8", 12));
#endif
});
void netPlayerMgrBase::UpdatePlayerListsForPlayer(CNetGamePlayer* player)
{
_netPlayerMgrBase_UpdatePlayerListsForPlayer(this, player);
}
}
static rage::netPlayerMgrBase* g_playerMgr;
void* g_tempRemotePlayer;
CNetGamePlayer* g_players[256];
std::unordered_map<uint16_t, CNetGamePlayer*> g_playersByNetId;
std::unordered_map<CNetGamePlayer*, uint16_t> g_netIdsByPlayer;
static CNetGamePlayer* g_playerList[256];
static int g_playerListCount;
static CNetGamePlayer* g_playerListRemote[256];
static int g_playerListCountRemote;
static std::unordered_map<uint16_t, std::shared_ptr<fx::StateBag>> g_playerBags;
static CNetGamePlayer*(*g_origGetPlayerByIndex)(uint8_t);
static CNetGamePlayer* __fastcall GetPlayerByIndex(uint8_t index)
{
if (!icgi->OneSyncEnabled)
{
return g_origGetPlayerByIndex(index);
}
if (index < 0 || index >= 256)
{
return nullptr;
}
return g_players[index];
}
static void SetupLocalPlayer(CNetGamePlayer* player)
{
if (player != g_playerMgr->GetLocalPlayer())
{
return;
}
console::DPrintf("onesync", "Assigning physical player index for the local player.\n");
auto clientId = g_netLibrary->GetServerNetID();
auto idx = g_netLibrary->GetServerSlotID();
player->physicalPlayerIndex() = idx;
g_players[idx] = player;
g_playersByNetId[clientId] = player;
g_netIdsByPlayer[player] = clientId;
// add to sequential list
g_playerList[g_playerListCount] = player;
g_playerListCount++;
g_playerBags[clientId] = Instance<fx::ResourceManager>::Get()
->GetComponent<fx::StateBagComponent>()
->RegisterStateBag(fmt::sprintf("player:%d", clientId), true);
// don't add to g_playerListRemote(!)
}
CNetGamePlayer* GetLocalPlayer()
{
return g_playerMgr->GetLocalPlayer();
}
#ifdef GTA_FIVE
static void(*g_origJoinBubble)(void* bubbleMgr, CNetGamePlayer* player);
static void JoinPhysicalPlayerOnHost(void* bubbleMgr, CNetGamePlayer* player)
{
g_origJoinBubble(bubbleMgr, player);
if (icgi->OneSyncEnabled)
{
SetupLocalPlayer(player);
}
}
#elif IS_RDR3
static void*(*g_origJoinBubble)(void* bubbleMgr, void* scSessionImpl, void* playerDataMsg, void* a4, uint8_t slotIndex);
static void* JoinPhysicalPlayerOnHost(void* bubbleMgr, void* scSessionImpl, void* playerDataMsg, void* a4, uint8_t slotIndex)
{
if (!icgi->OneSyncEnabled)
{
return g_origJoinBubble(bubbleMgr, scSessionImpl, playerDataMsg, a4, slotIndex);
}
auto result = g_origJoinBubble(bubbleMgr, scSessionImpl, playerDataMsg, a4, g_netLibrary->GetServerSlotID());
SetupLocalPlayer(GetLocalPlayer());
return result;
}
#endif
CNetGamePlayer* GetPlayerByNetId(uint16_t netId)
{
return g_playersByNetId[netId];
}
static CNetGamePlayer*(*g_origGetPlayerByIndexNet)(int);
static CNetGamePlayer* GetPlayerByIndexNet(int index)
{
if (!icgi->OneSyncEnabled)
{
return g_origGetPlayerByIndexNet(index);
}
// todo: check network game flag
return GetPlayerByIndex(index);
}
static bool (*g_origNetPlayer_IsActive)(CNetGamePlayer*);
static bool netPlayer_IsActiveStub(CNetGamePlayer* player)
{
if (!icgi->OneSyncEnabled)
{
return g_origNetPlayer_IsActive(player);
}
return true;
}
static bool(*g_origIsNetworkPlayerActive)(int);
static bool IsNetworkPlayerActive(int index)
{
if (!icgi->OneSyncEnabled)
{
return g_origIsNetworkPlayerActive(index);
}
return (GetPlayerByIndex(index) != nullptr);
}
static bool(*g_origIsNetworkPlayerConnected)(int);
static bool IsNetworkPlayerConnected(int index)
{
if (!icgi->OneSyncEnabled)
{
return g_origIsNetworkPlayerConnected(index);
}
return (GetPlayerByIndex(index) != nullptr);
}
#ifdef GTA_FIVE
int g_physIdx = 42;
#elif IS_RDR3
int g_physIdx = 1;
#endif
#ifdef GTA_FIVE
static hook::cdecl_stub<void(void*)> _npCtor([]()
{
return hook::get_pattern("C7 41 08 0A 00 00 00 C7 41 0C 20 00 00 00", -0xA);
});
#endif
static hook::cdecl_stub<void(void*)> _pCtor([]()
{
#ifdef GTA_FIVE
return hook::get_address<void*>(hook::get_pattern("41 8B E9 4D 8B F0 48 8B DA E8", 9), 1, 5);
#elif IS_RDR3
return hook::get_pattern("83 4B 1C FF 48 8D 05 ? ? ? ? 33 C9 48", -0xE);
#endif
});
#ifdef IS_RDR3
static hook::cdecl_stub<void(uint8_t)> _addCachedPlayerArrayEntry([]()
{
return hook::get_call(hook::get_pattern("48 8B CB E8 ? ? ? ? 8A 4B 19 48 83 C4", 0x10));
});
static hook::cdecl_stub<void(uint8_t)> _removeCachedPlayerArrayEntry([]()
{
return hook::get_call(hook::get_pattern("40 8A CD E8 ? ? ? ? 48 8B 6C 24 38", 3));
});
#endif
namespace sync
{
static void SetupRemotePlayer(uint16_t clientId, CNetGamePlayer* player, int idx)
{
player->physicalPlayerIndex() = idx;
g_players[idx] = player;
g_playersByNetId[clientId] = player;
g_netIdsByPlayer[player] = clientId;
g_playerList[g_playerListCount] = player;
g_playerListCount++;
g_playerListRemote[g_playerListCountRemote] = player;
g_playerListCountRemote++;
// resort player lists
std::sort(g_playerList, g_playerList + g_playerListCount, [](CNetGamePlayer* left, CNetGamePlayer* right)
{
return (left->physicalPlayerIndex() < right->physicalPlayerIndex());
});
std::sort(g_playerListRemote, g_playerListRemote + g_playerListCountRemote, [](CNetGamePlayer* left, CNetGamePlayer* right)
{
return (left->physicalPlayerIndex() < right->physicalPlayerIndex());
});
g_playerBags[clientId] = Instance<fx::ResourceManager>::Get()
->GetComponent<fx::StateBagComponent>()
->RegisterStateBag(fmt::sprintf("player:%d", clientId), true);
}
#ifdef GTA_FIVE
template<int Build>
void TempHackMakePhysicalPlayerImpl(uint16_t clientId, int idx = -1)
{
auto npPool = rage::GetPoolBase("CNonPhysicalPlayerData");
// probably shutting down the network subsystem
if (!npPool)
{
return;
}
void* fakeInAddr = calloc(256, 1);
void* fakeFakeData = calloc(256, 1);
rlGamerInfo<Build>* inAddr = (rlGamerInfo<Build>*)fakeInAddr;
inAddr->peerAddress.localAddr().ip.addr = clientId ^ 0xFEED;
inAddr->peerAddress.relayAddr().ip.addr = clientId ^ 0xFEED;
inAddr->peerAddress.publicAddr().ip.addr = clientId ^ 0xFEED;
inAddr->peerAddress.rockstarAccountId() = clientId;
inAddr->gamerId = clientId;
// this has to come from the pool directly as the game will expect to free it
void* nonPhys = rage::PoolAllocate(npPool);
_npCtor(nonPhys); // ctor
void* phys = calloc(1024, 1);
_pCtor(phys);
auto player = g_playerMgr->AddPlayer(fakeInAddr, fakeFakeData, nullptr, phys, nonPhys);
g_tempRemotePlayer = player;
// so we can safely remove (do this *before* assigning physical player index, or the game will add
// to a lot of lists which aren't >32-safe)
//g_playerMgr->UpdatePlayerListsForPlayer(player);
// NOTE: THIS IS NOT SAFE unless array manager/etc. are patched
if (idx == -1)
{
idx = g_physIdx;
g_physIdx++;
}
SetupRemotePlayer(clientId, player, idx);
}
void TempHackMakePhysicalPlayer(uint16_t clientId, int idx = -1)
{
if (xbr::IsGameBuildOrGreater<2824>())
{
TempHackMakePhysicalPlayerImpl<2824>(clientId, idx);
}
else if (xbr::IsGameBuildOrGreater<2372>())
{
TempHackMakePhysicalPlayerImpl<2372>(clientId, idx);
}
else if (xbr::IsGameBuildOrGreater<2060>())
{
TempHackMakePhysicalPlayerImpl<2060>(clientId, idx);
}
else
{
TempHackMakePhysicalPlayerImpl<1604>(clientId, idx);
}
}
#elif IS_RDR3
void TempHackMakePhysicalPlayer(uint16_t clientId, int idx = -1)
{
void* fakeInAddr = calloc(256, 1);
void* fakeFakeData = calloc(256, 1);
rlGamerInfo* inAddr = (rlGamerInfo*)fakeInAddr;
inAddr->peerAddress.localAddr.ip.addr = (clientId ^ 0xFEED) | 0xc0a80000;
inAddr->peerAddress.relayAddr.ip.addr = clientId ^ 0xFEED;
inAddr->peerAddress.publicAddr.ip.addr = clientId ^ 0xFEED;
inAddr->peerAddress.rockstarAccountId = clientId;
void* phys = calloc(1024, 1);
_pCtor(phys);
auto player = g_playerMgr->AddPlayer(fakeInAddr, 0, phys, &fakeFakeData);
g_tempRemotePlayer = player;
if (idx == -1)
{
idx = g_physIdx;
g_physIdx++;
}
_addCachedPlayerArrayEntry(idx);
SetupRemotePlayer(clientId, player, idx);
}
#endif
}
void HandleClientInfo(const NetLibraryClientInfo& info)
{
if (info.netId != g_netLibrary->GetServerNetID())
{
console::DPrintf("onesync", "Creating physical player %d (%s)\n", info.slotId, info.name);
sync::TempHackMakePhysicalPlayer(info.netId, info.slotId);
}
}
static hook::cdecl_stub<void* (CNetGamePlayer*)> getPlayerPedForNetPlayer([]()
{
#ifdef GTA_FIVE
return hook::get_call(hook::get_pattern("84 C0 74 1C 48 8B CF E8 ? ? ? ? 48 8B D8", 7));
#elif IS_RDR3
return hook::get_call(hook::get_pattern("48 8B CD 0F 11 06 48 8B D8 E8", -8));
#endif
});
#ifdef GTA_FIVE
static constexpr uint32_t g_entityNetObjOffset = 208;
#elif IS_RDR3
static constexpr uint32_t g_entityNetObjOffset = 224;
#endif
rage::netObject* GetNetObjectFromEntity(void* entity)
{
// technically must be at least CDynamicEntity
return *(rage::netObject**)((char*)entity + g_entityNetObjOffset);
}
rage::netObject* GetLocalPlayerPedNetObject()
{
auto ped = getPlayerPedForNetPlayer(GetLocalPlayer());
if (ped)
{
return GetNetObjectFromEntity(ped);
}
return nullptr;
}
void HandleClientDrop(const NetLibraryClientInfo& info)
{
if (info.netId != g_netLibrary->GetServerNetID() && info.slotId != g_netLibrary->GetServerSlotID())
{
console::DPrintf("onesync", "Processing removal for player %d (%s)\n", info.slotId, info.name);
if (info.slotId < 0 || info.slotId >= _countof(g_players))
{
console::DPrintf("onesync", "That's not a valid slot! Aborting.\n");
return;
}
auto player = g_players[info.slotId];
if (!player)
{
console::DPrintf("onesync", "That slot has no player. Aborting.\n");
return;
}
// reassign the player's ped
// TODO: only do this on a single client(!)
// 1604 unused
uint16_t objectId = 0;
//auto ped = ((void*(*)(void*, uint16_t*, CNetGamePlayer*))hook::get_adjusted(0x141022B20))(nullptr, &objectId, player);
auto ped = getPlayerPedForNetPlayer(player);
// reset player (but not until we have gotten the ped)
player->Reset();
if (ped)
{
auto netObj = GetNetObjectFromEntity(ped);
if (netObj)
{
objectId = netObj->GetObjectId();
}
}
console::DPrintf("onesync", "reassigning ped: %016llx %d\n", (uintptr_t)ped, objectId);
if (ped)
{
// prevent stack overflow
if (!info.name.empty())
{
TheClones->DeleteObjectId(objectId, 0, true);
}
console::DPrintf("onesync", "deleted object id\n");
// 1604 unused
//((void(*)(void*, uint16_t, CNetGamePlayer*))hook::get_adjusted(0x141008D14))(ped, objectId, player);
console::DPrintf("onesync", "success! reassigned the ped!\n");
}
// make non-physical so we will remove from the non-physical list only
/*auto physIdx = player->physicalPlayerIndex();
player->physicalPlayerIndex() = -1;
// remove object manager pointer temporarily
static auto objectMgr = hook::get_address<void**>(hook::get_pattern("48 8B FA 0F B7 51 30 48 8B 0D ? ? ? ? 45 33 C0", 10));
auto ogMgr = *objectMgr;
*objectMgr = nullptr;
// remove
g_playerMgr->RemovePlayer(player);
// restore
*objectMgr = ogMgr;
player->physicalPlayerIndex() = physIdx;*/
// ^ is NOT safe, array handler manager etc. have 32 limits
// TEMP: properly handle order so that we don't have to fake out the game
g_playersByNetId[info.netId] = nullptr;
g_netIdsByPlayer[player] = -1;
#ifdef IS_RDR3
_removeCachedPlayerArrayEntry(info.slotId);
#endif
for (int i = 0; i < g_playerListCount; i++)
{
if (g_playerList[i] == player)
{
memmove(&g_playerList[i], &g_playerList[i + 1], sizeof(*g_playerList) * (g_playerListCount - i - 1));
g_playerListCount--;
break;
}
}
for (int i = 0; i < g_playerListCountRemote; i++)
{
if (g_playerListRemote[i] == player)
{
memmove(&g_playerListRemote[i], &g_playerListRemote[i + 1], sizeof(*g_playerListRemote) * (g_playerListCountRemote - i - 1));
g_playerListCountRemote--;
break;
}
}
// resort player lists
std::sort(g_playerList, g_playerList + g_playerListCount, [](CNetGamePlayer * left, CNetGamePlayer * right)
{
return (left->physicalPlayerIndex() < right->physicalPlayerIndex());
});
std::sort(g_playerListRemote, g_playerListRemote + g_playerListCountRemote, [](CNetGamePlayer* left, CNetGamePlayer* right)
{
return (left->physicalPlayerIndex() < right->physicalPlayerIndex());
});
g_players[info.slotId] = nullptr;
g_playerBags.erase(info.netId);
}
}
static CNetGamePlayer*(*g_origGetOwnerNetPlayer)(rage::netObject*);
static CNetGamePlayer* g_player31;
bool EnsurePlayer31();
CNetGamePlayer* netObject__GetPlayerOwner(rage::netObject* object)
{
if (!icgi->OneSyncEnabled)
{
return g_origGetOwnerNetPlayer(object);
}
if (object && object->syncData.ownerId == 31)
{
auto player = g_playersByNetId[TheClones->GetClientId(object)];
// FIXME: figure out why bad playerinfos occur
if (player != nullptr && player->GetPlayerInfo() != nullptr)
{
return player;
}
#ifdef IS_RDR3
return nullptr;
#endif
EnsurePlayer31();
return g_player31;
}
return GetLocalPlayer();
}
static uint8_t(*g_origGetOwnerPlayerId)(rage::netObject*);
static uint8_t netObject__GetPlayerOwnerId(rage::netObject* object)
{
if (!icgi->OneSyncEnabled)
{
return g_origGetOwnerPlayerId(object);
}
auto owner = netObject__GetPlayerOwner(object);
return owner ? owner->physicalPlayerIndex() : 0xFF;
}
static CNetGamePlayer*(*g_origGetPendingPlayerOwner)(rage::netObject*);
static CNetGamePlayer* netObject__GetPendingPlayerOwner(rage::netObject* object)
{
if (!icgi->OneSyncEnabled)
{
return g_origGetPendingPlayerOwner(object);
}
if (object->syncData.nextOwnerId != 0xFF)
{
auto player = g_playersByNetId[TheClones->GetPendingClientId(object)];
if (player != nullptr && player->GetPlayerInfo() != nullptr)
{
return player;
}
}
return nullptr;
}
static hook::cdecl_stub<uint32_t(void*)> getScriptGuidForEntity([]()
{
#ifdef GTA_FIVE
return hook::get_pattern("48 F7 F9 49 8B 48 08 48 63 D0 C1 E0 08 0F B6 1C 11 03 D8", -0x68);
#elif IS_RDR3
return hook::get_pattern("32 DB E8 ? ? ? ? 48 85 C0 75 ? 8A 05", -35);
#endif
});
#ifdef GTA_FIVE
struct ReturnedCallStub : public jitasm::Frontend
{
ReturnedCallStub(int idx, uintptr_t targetFunc)
: m_index(idx), m_targetFunc(targetFunc)
{
}
static void InstrumentedTarget(void* targetFn, void* callFn, int index)
{
console::DPrintf("onesync", "called %016llx (m_%x) from %016llx\n", (uintptr_t)targetFn, index, (uintptr_t)callFn);
}
virtual void InternalMain() override
{
push(rcx);
push(rdx);
push(r8);
push(r9);
mov(rcx, m_targetFunc);
mov(rdx, qword_ptr[rsp + 0x20]);
mov(r8d, m_index);
// scratch space (+ alignment for stack)
sub(rsp, 0x28);
mov(rax, (uintptr_t)InstrumentedTarget);
call(rax);
add(rsp, 0x28);
pop(r9);
pop(r8);
pop(rdx);
pop(rcx);
mov(rax, m_targetFunc);
jmp(rax);
}
private:
uintptr_t m_originFunc;
uintptr_t m_targetFunc;
int m_index;
};
static void NetLogStub_DoTrace(void*, const char* fmt, ...)
{
char buffer[2048];
va_list ap;
va_start(ap, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
console::DPrintf("onesync", "[NET] %s\n", buffer);
}
static void NetLogStub_DoLog(void*, const char* type, const char* fmt, ...)
{
char buffer[2048];
va_list ap;
va_start(ap, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
console::DPrintf("onesync", "[NET] %s %s\n", type, buffer);
}
#endif
static hook::cdecl_stub<CNetGamePlayer*(void*)> _netPlayerCtor([]()
{
#ifdef GTA_FIVE
return (xbr::IsGameBuildOrGreater<2944>()) ? hook::get_pattern("83 8B ? 00 00 00 FF 48 8D 05 ? ? ? ? 33 F6", -0x17) : hook::get_pattern("83 8B ? 00 00 00 FF 33 F6", -0x17);
#elif IS_RDR3
return hook::get_pattern("E8 ? ? ? ? 33 F6 48 8D 05 ? ? ? ? 48 8D 8B", -0x17);
#endif
});
#ifdef GTA_FIVE
// Offset is usually 0xA0, but can differ on ancient builds
static int g_CNetPlayerOffset_PlayerInfo = 0;
struct CPlayerInfo_Five;
static hook::cdecl_stub<CPlayerInfo_Five*(void*, uint64_t, void*)> _playerInfoCtor([]()
{
return hook::get_pattern("48 83 EC 30 65 4C 8B 0C 25 ? 00 00 00 0F 29 70 C8 45 33 ED", -0x18);
});
// this appears to be a substruct that makes up a good chunk of the beginning of the CPlayerInfo
static hook::cdecl_stub<void*()> _getLocalGamerInfo([]()
{
return hook::get_address<void*>(hook::get_pattern("E8 ? ? ? ? 33 D2 48 8B CB 4C 8B C0 E8 ? ? ? ? 4D 8B CE"), 1, 5);
});
#endif
static CNetGamePlayer*(*g_origAllocateNetPlayer)(void*);
static CNetGamePlayer* AllocateNetPlayer(void* mgr)
{
if (!icgi->OneSyncEnabled)
{
return g_origAllocateNetPlayer(mgr);
}
#ifdef GTA_FIVE
void* plr = malloc(xbr::IsGameBuildOrGreater<2824>() ? 800 : xbr::IsGameBuildOrGreater<2372>() ? 704: xbr::IsGameBuildOrGreater<2060>() ? 688 : 672);
#elif IS_RDR3
void* plr = malloc(xbr::IsGameBuildOrGreater<1436>() ? 2736 : 2784);
#endif
auto player = _netPlayerCtor(plr);
// RDR3 wants CNetworkPlayerMgr pointer in CNetGamePlayer
#ifdef IS_RDR3
*(rage::netPlayerMgrBase**)((uint64_t)player + 288) = g_playerMgr;
#endif
return player;
}
// fix: some Events expect the CPlayerInfo to be non-null in the CNetGamePlayer (It is not set in the constructor)
static void Player31_ApplyPlayerInfo(CNetGamePlayer* player)
{
static void* infoMem = nullptr;
if (!infoMem)
{
infoMem = malloc(0x2000);
CPlayerInfo_Five* info = _playerInfoCtor(infoMem, 0, _getLocalGamerInfo());
*(CPlayerInfo_Five**)((uint64_t)player + g_CNetPlayerOffset_PlayerInfo) = info;
}
else
{
*(CPlayerInfo_Five**)((uint64_t)player + g_CNetPlayerOffset_PlayerInfo) = (CPlayerInfo_Five*)infoMem;
}
}
// Clear the playerInfo after events
static void Player31_ClearPlayerInfo(CNetGamePlayer* player)
{
*(CPlayerInfo_Five**)((uint64_t)player + g_CNetPlayerOffset_PlayerInfo) = nullptr;
}
#include <minhook.h>
static void(*g_origPassObjectControl)(CNetGamePlayer* player, rage::netObject* netObject, int a3);
void ObjectIds_RemoveObjectId(int objectId);
static void PassObjectControlStub(CNetGamePlayer* player, rage::netObject* netObject, int a3)
{
if (!icgi->OneSyncEnabled)
{
return g_origPassObjectControl(player, netObject, a3);
}
auto owner = netObject__GetPlayerOwner(netObject);
if (!owner || player->physicalPlayerIndex() == owner->physicalPlayerIndex())
{
return;
}
console::DPrintf("onesync", "passing object %016llx (%d) control from %d to %d\n", (uintptr_t)netObject, netObject->GetObjectId(), netObject->syncData.ownerId, player->physicalPlayerIndex());
TheClones->Log("%s: passing object %016llx (%d) control from %d to %d\n", __func__, (uintptr_t)netObject, netObject->GetObjectId(), netObject->syncData.ownerId, player->physicalPlayerIndex());
ObjectIds_RemoveObjectId(netObject->GetObjectId());
netObject->syncData.nextOwnerId = 31;
TheClones->SetTargetOwner(netObject, g_netIdsByPlayer[player]);
// REDM1S: implement for vehicles and mounts
#ifdef GTA_FIVE
fwEntity* entity = (fwEntity*)netObject->GetGameObject();
if (entity && entity->IsOfType(HashString("CVehicle")))
{
CVehicle* vehicle = static_cast<CVehicle*>(entity);
VehicleSeatManager* seatManager = vehicle->GetSeatManager();
for (int i = 0; i < seatManager->GetNumSeats(); i++)
{
auto occupant = seatManager->GetOccupant(i);
if (occupant)
{
auto netOccupant = reinterpret_cast<rage::netObject*>(occupant->GetNetObject());
if (netOccupant)
{
if (!netOccupant->syncData.isRemote && netOccupant->GetObjectType() != 11)
{
console::DPrintf("onesync", "passing occupant %d control as well\n", netOccupant->GetObjectId());
PassObjectControlStub(player, netOccupant, a3);
}
}
}
}
}
#endif
//auto lastIndex = player->physicalPlayerIndex();
//player->physicalPlayerIndex() = 31;
g_origPassObjectControl(player, netObject, a3);
//player->physicalPlayerIndex() = lastIndex;
}
static void(*g_origSetOwner)(rage::netObject* object, CNetGamePlayer* newOwner);
static void SetOwnerStub(rage::netObject* netObject, CNetGamePlayer* newOwner)
{
if (!icgi->OneSyncEnabled)
{
return g_origSetOwner(netObject, newOwner);
}
if (newOwner->physicalPlayerIndex() == GetLocalPlayer()->physicalPlayerIndex())
{
TheClones->Log("%s: taking ownership of object id %d - stack trace:\n", __func__, netObject->GetObjectId());
uintptr_t* traceStart = (uintptr_t*)_AddressOfReturnAddress();
for (int i = 0; i < 96; i++)
{
if ((i % 6) == 0)
{
TheClones->Log("\n");
}
TheClones->Log("%016llx ", traceStart[i]);
}
TheClones->Log("\n");
}
g_origSetOwner(netObject, newOwner);
auto oldOwner = netObject__GetPlayerOwner(netObject);
auto oldIndex = (oldOwner) ? oldOwner->physicalPlayerIndex() : 31;
if (newOwner->physicalPlayerIndex() == oldIndex)
{
return;
}
TheClones->Log("%s: passing object %016llx (%d) ownership from %d to %d\n", __func__, (uintptr_t)netObject, netObject->GetObjectId(), netObject->syncData.ownerId, newOwner->physicalPlayerIndex());
ObjectIds_RemoveObjectId(netObject->GetObjectId());
netObject->syncData.nextOwnerId = 31;
TheClones->SetTargetOwner(netObject, g_netIdsByPlayer[newOwner]);
}
static bool netObject__CanPassControl(rage::netObject* object, CNetGamePlayer* player, int type, int* outReason)
{
int reason;
bool rv = object->CanPassControl(player, type, &reason);
if (!rv)
{
console::DPrintf("onesync", "couldn't pass control for reason %d\n", reason);
}
return rv;
}
#ifdef GTA_FIVE
static bool netObject__CanBlend(rage::netObject* object, int* outReason)
{
int reason;
bool rv = object->CanBlend(&reason);
if (!rv)
{
//trace("couldn't blend object for reason %d\n", reason);
}
return rv;
}
#endif
int getPlayerId()
{
return GetLocalPlayer()->physicalPlayerIndex();
}
static bool mD0Stub(rage::netSyncTree* tree, int a2)
{
if (icgi->OneSyncEnabled)
{
return false;
}
#ifdef GTA_FIVE
return tree->m_D0(a2);
#elif IS_RDR3
return tree->m_C0(a2);
#endif
}
static int(*g_origGetNetworkPlayerListCount)();
static CNetGamePlayer**(*g_origGetNetworkPlayerList)();
static int netInterface_GetNumRemotePhysicalPlayers()
{
if (!icgi->OneSyncEnabled)
{
return g_origGetNetworkPlayerListCount();
}
return g_playerListCountRemote;
}
static CNetGamePlayer** netInterface_GetRemotePhysicalPlayers()
{
if (!icgi->OneSyncEnabled)
{
return g_origGetNetworkPlayerList();
}
return g_playerListRemote;
}
static int(*g_origGetNetworkPlayerListCount2)();
static CNetGamePlayer**(*g_origGetNetworkPlayerList2)();
static int netInterface_GetNumPhysicalPlayers()
{
if (!icgi->OneSyncEnabled)
{
return g_origGetNetworkPlayerListCount2();
}
return g_playerListCount;
}
static CNetGamePlayer** netInterface_GetAllPhysicalPlayers()
{