forked from citizenfx/fivem
/
CloneManager.cpp
2768 lines (2144 loc) · 66.8 KB
/
CloneManager.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 <CloneManager.h>
#include <state/RlMessageBuffer.h>
#include <NetBuffer.h>
#include <NetLibrary.h>
#include <rlNetBuffer.h>
#include <netBlender.h>
#include <netInterface.h>
#include <netObjectMgr.h>
#include <netSyncTree.h>
#include <lz4hc.h>
#include <boost/range/adaptor/map.hpp>
#include <ICoreGameInit.h>
#include <array>
#include <chrono>
#include <EntitySystem.h>
#include <Hooking.h>
#include <tbb/concurrent_queue.h>
#include <ResourceManager.h>
#include <StateBagComponent.h>
#include <IteratorView.h>
#include <CoreConsole.h>
#include <MinHook.h>
extern rage::netObject* g_curNetObjectSelection;
rage::netObject* g_curNetObject;
static std::set<uint16_t> g_dontParrotDeletionAcks;
#ifdef GTA_FIVE
static constexpr int kNetObjectTypeBitLength = 4;
#elif IS_RDR3
static constexpr int kNetObjectTypeBitLength = 5;
#endif
static constexpr int kSyncPacketMaxLength = 2400;
void ObjectIds_AddObjectId(int objectId);
void ObjectIds_StealObjectId(int objectId);
void ObjectIds_ConfirmObjectId(int objectId);
void AssociateSyncTree(int objectId, rage::netSyncTree* syncTree);
rage::netObject* GetLocalPlayerPedNetObject();
using namespace std::chrono_literals;
inline std::chrono::milliseconds msec()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch());
}
static ICoreGameInit* icgi;
static hook::cdecl_stub<uint32_t()> _getNetAckTimestamp([]()
{
#ifdef GTA_FIVE
return hook::get_pattern("3B CA 76 02 FF", -0x31);
#elif IS_RDR3
return hook::get_pattern("8B C3 2B 05 ? ? ? ? 39 ? ? ? ? 02 76 ? FF C8", -0x30);
#endif
});
extern CNetGamePlayer* g_players[256];
extern std::unordered_map<uint16_t, CNetGamePlayer*> g_playersByNetId;
extern std::unordered_map<CNetGamePlayer*, uint16_t> g_netIdsByPlayer;
std::string GetType(void* d);
CNetGamePlayer* GetLocalPlayer();
CNetGamePlayer* GetPlayerByNetId(uint16_t);
bool IsWaitingForTimeSync();
extern uint32_t* rage__s_NetworkTimeThisFrameStart;
extern uint32_t* rage__s_NetworkTimeLastFrameStart;
namespace sync
{
class msgClone;
enum class AckResult
{
OK,
ResendClone,
ResendCloneMinimal,
ResendCreate
};
class CloneManagerLocal : public CloneManager, public INetObjMgrAbstraction, public fx::StateBagGameInterface
{
public:
virtual void Reset() override;
virtual void Update() override;
virtual void BindNetLibrary(NetLibrary* netLibrary) override;
virtual uint16_t GetClientId(rage::netObject* netObject) override;
virtual void GiveObjectToClient(rage::netObject* object, uint16_t clientId) override;
virtual uint16_t GetPendingClientId(rage::netObject* netObject) override;
virtual void SetTargetOwner(rage::netObject* object, uint16_t clientId) override;
virtual void OnObjectDeletion(rage::netObject* object) override;
virtual rage::netObject* GetNetObject(uint16_t objectId) override;
virtual void DeleteObjectId(uint16_t objectId, uint16_t uniqifier, bool force) override;
virtual void Logv(const char* format, fmt::printf_args argumentList) override;
virtual const std::vector<rage::netObject*>& GetObjectList() override;
virtual bool IsRemovingObjectId(uint16_t objectId) override;
// netobjmgr abstraction
virtual bool RegisterNetworkObject(rage::netObject* object) override;
virtual void DestroyNetworkObject(rage::netObject* object) override;
virtual void ChangeOwner(rage::netObject* object, int oldOwnerId, CNetGamePlayer* player, int migrationType) override;
virtual rage::netObject* GetNetworkObject(uint16_t id) override;
virtual void ForAllNetObjects(int playerId, const std::function<void(rage::netObject*)>& callback, bool safe) override
{
if (safe)
{
auto nol = m_netObjects[playerId]; // copy the list, we want to remove from it
for (auto& entry : nol)
{
callback(entry.second);
}
}
else
{
for (auto& entry : m_netObjects[playerId])
{
callback(entry.second);
}
}
}
private:
void WriteUpdates();
void SendUpdates(rl::MessageBuffer& buffer, uint32_t msgType);
void AttemptFlushNetBuffer(rl::MessageBuffer& buffer, uint32_t msgType);
void AttemptFlushCloneBuffer();
void AttemptFlushAckBuffer();
private:
void HandleCloneAcks(const char* data, size_t len);
void HandleCloneAcksNew(const char* data, size_t len);
void HandleCloneSync(const char* data, size_t len);
void HandleCloneRemove(const char* data, size_t len);
bool HandleCloneCreate(const msgClone& msg);
AckResult HandleCloneUpdate(const msgClone& msg);
void CheckMigration(const msgClone& msg);
void AddCreateAck(uint16_t objectId, uint16_t uniqifier);
void AddRemoveAck(uint16_t objectId, uint16_t uniqifier);
void ProcessCreateAck(uint16_t objectId, uint16_t uniqifier = 0);
void ProcessSyncAck(uint16_t objectId, uint16_t uniqifier = 0);
void ProcessRemoveAck(uint16_t objectId, uint16_t uniqifier = 0);
void ProcessTimestampAck(uint32_t timestamp);
virtual void SendPacket(int peer, std::string_view data) override;
private:
NetLibrary* m_netLibrary;
private:
std::chrono::milliseconds m_lastSend;
std::chrono::milliseconds m_lastAck;
rl::MessageBuffer m_sendBuffer{ 16384 };
rl::MessageBuffer m_ackBuffer{ 16384 };
uint32_t m_ackTimestamp{ 0 };
private:
struct ObjectData
{
std::chrono::milliseconds lastSyncTime;
std::chrono::milliseconds lastSyncAck;
uint32_t lastChangeTime;
uint32_t lastResendTime;
uint32_t nextKeepaliveSync;
uint16_t uniqifier;
std::shared_ptr<fx::StateBag> stateBag;
uint64_t lastFrameUpdated = 0;
bool hi = false;
ObjectData()
{
lastSyncTime = 0ms;
lastSyncAck = 0ms;
nextKeepaliveSync = 0;
lastChangeTime = 0;
lastResendTime = 0;
uniqifier = rand();
}
};
struct ExtendedCloneData
{
uint16_t clientId;
uint16_t pendingClientId;
uint32_t dontSyncBefore;
inline ExtendedCloneData()
: clientId(0), pendingClientId(-1), dontSyncBefore(0)
{
}
inline ExtendedCloneData(uint16_t clientId)
: clientId(clientId), pendingClientId(-1), dontSyncBefore(0)
{
}
};
private:
LZ4_streamHC_t m_compStream;
std::unordered_map<int, ObjectData> m_trackedObjects;
std::unordered_map<uint32_t, rage::netObject*> m_savedEntities;
std::unordered_set<rage::netObject*> m_savedEntitySet;
std::vector<rage::netObject*> m_savedEntityVec;
std::unordered_map<uint16_t, ExtendedCloneData> m_extendedData;
std::map<std::tuple<int, int>, std::chrono::milliseconds> m_pendingRemoveAcks;
std::set<std::tuple<int, int>> m_mercyList;
std::set<int> m_pendingConfirmObjectIds;
tbb::concurrent_queue<std::string> m_logQueue;
std::condition_variable m_consoleCondVar;
std::mutex m_consoleMutex;
std::string m_logFile;
std::array<std::map<int, rage::netObject*>, 256> m_netObjects;
std::shared_ptr<fx::StateBag> m_globalBag;
fwRefContainer<fx::StateBagComponent> m_sbac;
FrameIndex m_lastReceivedFrame;
uint64_t m_serverSendFrame;
std::multimap<uint64_t, std::tuple<int /* type */, uint16_t /* object */, uint16_t /* identifier */, uint32_t /* timestamp */>> m_serverAcks;
};
uint16_t CloneManagerLocal::GetClientId(rage::netObject* netObject)
{
return m_extendedData[netObject->GetObjectId()].clientId;
}
uint16_t CloneManagerLocal::GetPendingClientId(rage::netObject* netObject)
{
return m_extendedData[netObject->GetObjectId()].pendingClientId;
}
void CloneManagerLocal::Logv(const char* format, fmt::printf_args argumentList)
{
if (!m_logFile.empty())
{
m_logQueue.push(fmt::sprintf("[% 10d] ", (!IsWaitingForTimeSync()) ? rage::netInterface_queryFunctions::GetInstance()->GetTimestamp() : 0));
m_logQueue.push(fmt::vsprintf(format, argumentList));
m_consoleCondVar.notify_all();
}
}
void CloneManagerLocal::OnObjectDeletion(rage::netObject* netObject)
{
Log("%s: %s\n", __func__, netObject->ToString());
if (!netObject->syncData.isRemote)
{
if (m_trackedObjects[netObject->GetObjectId()].lastSyncTime != 0ms)
{
m_pendingRemoveAcks.insert({ { netObject->GetObjectId(), m_trackedObjects[netObject->GetObjectId()].uniqifier }, msec() });
}
}
m_trackedObjects.erase(netObject->GetObjectId());
m_extendedData.erase(netObject->GetObjectId());
m_savedEntities.erase(netObject->GetObjectId());
m_savedEntitySet.erase(netObject);
m_savedEntityVec.erase(std::remove(m_savedEntityVec.begin(), m_savedEntityVec.end(), netObject), m_savedEntityVec.end());
}
void CloneManagerLocal::SendPacket(int peer, std::string_view data)
{
m_netLibrary->SendReliableCommand("msgStateBag", data.data(), data.size());
}
void CloneManagerLocal::BindNetLibrary(NetLibrary* netLibrary)
{
// set the net library
m_netLibrary = netLibrary;
// add message handlers
m_netLibrary->AddReliableHandler(
"msgCloneAcks", [this](const char* data, size_t len)
{
HandleCloneAcks(data, len);
},
true);
m_netLibrary->AddReliableHandler(
"msgPackedClones", [this](const char* data, size_t len)
{
HandleCloneSync(data, len);
},
true);
m_netLibrary->AddReliableHandler(
"msgPackedAcks", [this](const char* data, size_t len)
{
HandleCloneAcksNew(data, len);
},
true);
m_netLibrary->AddReliableHandler(
"msgCloneRemove", [this](const char* data, size_t len)
{
HandleCloneRemove(data, len);
},
true);
std::thread([this]()
{
while (true)
{
{
std::unique_lock<std::mutex> lock(m_consoleMutex);
m_consoleCondVar.wait(lock);
}
static std::string lastLogFile;
static FILE* file;
if (lastLogFile != m_logFile)
{
if (file)
{
fclose(file);
file = nullptr;
}
if (!m_logFile.empty())
{
file = _pfopen(MakeRelativeCitPath(m_logFile).c_str(), _P("w"));
}
lastLogFile = m_logFile;
}
std::string str;
while (m_logQueue.try_pop(str))
{
if (file)
{
fprintf(file, "%s", str.c_str());
}
}
}
})
.detach();
static ConVar<std::string> logFile("onesync_logFile", ConVar_None, "", &m_logFile);
static ConsoleCommand printObj("net_printOwner", [this](int objectId)
{
auto it = m_savedEntities.find(objectId);
if (it == m_savedEntities.end() || !it->second)
{
console::PrintError("CloneManager", "Couldn't find object by ID %d\n", objectId);
return;
}
rage::netObject* obj = it->second;
auto& extData = m_extendedData[obj->GetObjectId()];
console::Printf("CloneManager", "-- NETWORK OBJECT %d (%s) --\n", obj->GetObjectId(), GetType(obj));
console::Printf("CloneManager", "Owner: %s (%d)\n", g_playersByNetId[extData.clientId] ? g_playersByNetId[extData.clientId]->GetName() : "null?", extData.clientId);
console::Printf("CloneManager", "Is remote: %s\n", obj->syncData.isRemote ? "yes" : "no");
console::Printf("CloneManager", "Game client ID: %d\n", obj->syncData.ownerId);
console::Printf("CloneManager", "\n");
});
icgi = Instance<ICoreGameInit>::Get();
// #TODO: shutdown session logic!!
auto sbac = fx::StateBagComponent::Create(fx::StateBagRole::Client);
m_globalBag = sbac->RegisterStateBag("global");
sbac->RegisterTarget(0);
sbac->AddSafePreCreatePrefix("entity:");
sbac->AddSafePreCreatePrefix("player:");
sbac->SetGameInterface(this);
m_netLibrary->AddReliableHandler(
"msgStateBag", [sbac](const char* data, size_t len)
{
sbac->HandlePacket(0, std::string_view{ data, len });
},
true);
m_sbac = sbac;
fx::ResourceManager::OnInitializeInstance.Connect([sbac](fx::ResourceManager* rm)
{
rm->SetComponent(sbac);
});
m_serverSendFrame = 0;
LZ4_initStreamHC(&m_compStream, sizeof(m_compStream));
}
void CloneManagerLocal::Reset()
{
m_pendingRemoveAcks.clear();
m_pendingConfirmObjectIds.clear();
m_logQueue.clear();
m_mercyList.clear();
m_lastReceivedFrame = { 0 };
m_lastSend = 0ms;
m_lastAck = 0ms;
m_ackTimestamp = 0;
m_serverAcks.clear();
m_serverSendFrame = 0;
m_sbac->Reset();
// re-add the global bag, since Reset() removes it
m_globalBag = {}; // first unset, as the handle keeps by name and otherwise we'd remove it due to destruction order
m_globalBag = m_sbac->RegisterStateBag("global");
}
void CloneManagerLocal::ProcessCreateAck(uint16_t objId, uint16_t uniqifier)
{
if (icgi->NetProtoVersion >= 0x201912301309 && (m_trackedObjects.find(objId) == m_trackedObjects.end() || m_trackedObjects[objId].uniqifier != uniqifier))
{
Log("%s: invalid uniqifier for %d\n", __func__, objId);
return;
}
m_trackedObjects[objId].lastSyncAck = msec();
Log("%s: create ack %d\n", __func__, objId);
}
#ifdef GTA_FIVE
static hook::cdecl_stub<void(rage::netSyncTree*, rage::netObject*, uint8_t, uint16_t, uint32_t, uint32_t)> _processAck([]()
{
return hook::get_pattern("45 32 ED FF 50 20 8B CB 41", -0x34);
});
#elif IS_RDR3
static hook::cdecl_stub<void(rage::netSyncTree*, rage::netObject*, uint8_t, uint16_t, uint32_t, uint64_t)> _processAck([]()
{
return hook::get_pattern("44 8B F7 FF 50 ? 48 8B C8 8B D7 E8", -0x4B);
});
#endif
void CloneManagerLocal::ProcessSyncAck(uint16_t objId, uint16_t uniqifier)
{
if (icgi->NetProtoVersion >= 0x201912301309 && (m_trackedObjects.find(objId) == m_trackedObjects.end() || m_trackedObjects[objId].uniqifier != uniqifier))
{
Log("%s: invalid uniqifier for %d\n", __func__, objId);
return;
}
auto netObjIt = m_savedEntities.find(objId);
Log("%s: sync ack %d\n", __func__, objId);
if (netObjIt != m_savedEntities.end())
{
auto netObj = netObjIt->second;
if (netObj)
{
auto syncTree = netObj->GetSyncTree();
syncTree->AckCfx(netObj, m_ackTimestamp);
if (netObj->GetSyncData())
{
_processAck(syncTree, netObj, 31, 0 /* seq? */, m_ackTimestamp, 0xFFFFFFFF);
}
}
}
}
void CloneManagerLocal::ProcessRemoveAck(uint16_t objId, uint16_t uniqifier)
{
auto sure = [this, objId]()
{
if (m_pendingConfirmObjectIds.find(objId) != m_pendingConfirmObjectIds.end())
{
ObjectIds_ConfirmObjectId(objId);
m_pendingConfirmObjectIds.erase(objId);
}
};
if (icgi->NetProtoVersion >= 0x202002271209)
{
if (uniqifier == 0)
{
auto s = m_pendingRemoveAcks.lower_bound({ objId, 0 });
auto e = m_pendingRemoveAcks.upper_bound({ objId, INT32_MAX });
m_pendingRemoveAcks.erase(s, e);
sure();
}
else
{
m_pendingRemoveAcks.erase({ objId, uniqifier });
sure();
}
return;
}
if (icgi->NetProtoVersion >= 0x201912301309 && m_trackedObjects[objId].uniqifier != uniqifier && uniqifier != 0)
{
Log("%s: invalid uniqifier for %d\n", __func__, objId);
return;
}
// #NETVER: resend removes and handle acks here
if (icgi->NetProtoVersion >= 0x201905190829)
{
auto s = m_pendingRemoveAcks.lower_bound({ objId, 0 });
auto e = m_pendingRemoveAcks.upper_bound({ objId, INT32_MAX });
m_pendingRemoveAcks.erase(s, e);
}
sure();
}
bool CloneManagerLocal::IsRemovingObjectId(uint16_t objectId)
{
auto first = m_pendingRemoveAcks.lower_bound({ objectId, 0 });
auto second = m_pendingRemoveAcks.lower_bound({ objectId, INT32_MAX });
bool indeed = first != second;
if (indeed)
{
m_pendingConfirmObjectIds.insert(objectId);
}
return indeed;
}
void CloneManagerLocal::ProcessTimestampAck(uint32_t timestamp)
{
m_ackTimestamp = timestamp;
Log("%s: ts ack %d\n", __func__, timestamp);
}
void CloneManagerLocal::HandleCloneAcks(const char* data, size_t len)
{
net::Buffer buf(reinterpret_cast<const uint8_t*>(data), len);
while (!buf.IsAtEnd())
{
auto type = buf.Read<uint8_t>();
Log("%s: read ack type %d\n", __func__, type);
switch (type)
{
// create ack?
case 1:
{
auto objId = buf.Read<uint16_t>();
ProcessCreateAck(objId);
break;
}
// sync ack?
case 2:
{
auto objId = buf.Read<uint16_t>();
ProcessSyncAck(objId);
break;
}
// timestamp ack?
case 5:
{
auto timestamp = buf.Read<uint32_t>();
ProcessTimestampAck(timestamp);
break;
}
// remove ack?
case 3:
{
auto objId = buf.Read<uint16_t>();
ProcessRemoveAck(objId);
break;
}
default:
return;
}
}
}
void CloneManagerLocal::HandleCloneAcksNew(const char* data, size_t len)
{
net::Buffer buffer(reinterpret_cast<const uint8_t*>(data), len);
// dummy frame index
auto frameIndex = buffer.Read<uint64_t>();
FrameIndex frameIndexData{ frameIndex };
// new handling expects us to send our own little piece of data
if (frameIndexData.frameIndex != 0)
{
auto acks = fx::GetIteratorView(m_serverAcks.equal_range(frameIndexData.frameIndex));
for (const auto& [key, ack] : acks)
{
auto [type, objId, uniqifier, ts] = ack;
switch (type)
{
// create ack?
case 1:
{
ProcessTimestampAck(ts);
ProcessCreateAck(objId, uniqifier);
break;
}
// sync ack?
case 2:
{
ProcessTimestampAck(ts);
ProcessSyncAck(objId, uniqifier);
break;
}
// remove ack?
case 3:
{
ProcessTimestampAck(ts);
ProcessRemoveAck(objId, uniqifier);
break;
}
}
}
m_serverAcks.erase(frameIndexData.frameIndex);
return;
}
uint8_t bufferData[16384] = { 0 };
int bufferLength = LZ4_decompress_safe(reinterpret_cast<const char*>(&buffer.GetData()[buffer.GetCurOffset()]), reinterpret_cast<char*>(bufferData), buffer.GetRemainingBytes(), sizeof(bufferData));
if (bufferLength > 0)
{
rl::MessageBuffer msgBuf(bufferData, bufferLength);
bool end = false;
while (!msgBuf.IsAtEnd() && !end)
{
auto type = msgBuf.Read<uint8_t>(3);
Log("%s: read ack type %d\n", __func__, type);
switch (type)
{
// create ack?
case 1:
{
auto objId = msgBuf.Read<uint16_t>(13);
auto uniqifier = 0;
if (icgi->NetProtoVersion >= 0x201912301309)
{
uniqifier = msgBuf.Read<uint16_t>(16);
}
ProcessCreateAck(objId, uniqifier);
break;
}
// sync ack?
case 2:
{
auto objId = msgBuf.Read<uint16_t>(13);
auto uniqifier = 0;
if (icgi->NetProtoVersion >= 0x201912301309)
{
uniqifier = msgBuf.Read<uint16_t>(16);
}
ProcessSyncAck(objId, uniqifier);
break;
}
// remove ack?
case 3:
{
auto objId = msgBuf.Read<uint16_t>(13);
auto uniqifier = 0;
if (icgi->NetProtoVersion >= 0x201912301309)
{
uniqifier = msgBuf.Read<uint16_t>(16);
}
ProcessRemoveAck(objId, uniqifier);
break;
}
// timestamp ack?
case 5:
{
auto timestamp = msgBuf.Read<uint32_t>(32);
ProcessTimestampAck(timestamp);
break;
}
case 7:
default:
end = true;
break;
}
}
}
}
void CloneManagerLocal::AddCreateAck(uint16_t objectId, uint16_t uniqifier)
{
if (icgi->NetProtoVersion >= 0x202011231556 && icgi->SyncIsARQ)
{
m_ackBuffer.Write(3, 1);
m_ackBuffer.Write(13, objectId);
m_ackBuffer.Write(16, uniqifier);
AttemptFlushAckBuffer();
return;
}
if (icgi->NetProtoVersion >= 0x202007022353)
{
return;
}
m_ackBuffer.Write(3, 1);
m_ackBuffer.Write(13, objectId);
AttemptFlushAckBuffer();
}
void CloneManagerLocal::AddRemoveAck(uint16_t objectId, uint16_t uniqifier)
{
if (icgi->NetProtoVersion >= 0x202011231556 && icgi->SyncIsARQ)
{
m_ackBuffer.Write(3, 3);
m_ackBuffer.Write(13, objectId);
m_ackBuffer.Write(16, uniqifier);
AttemptFlushAckBuffer();
return;
}
if (icgi->NetProtoVersion >= 0x202007022353)
{
return;
}
m_ackBuffer.Write(3, 3);
m_ackBuffer.Write(13, objectId);
AttemptFlushAckBuffer();
}
class msgClone
{
public:
msgClone();
void Read(int syncType, rl::MessageBuffer& buffer);
inline uint8_t GetSyncType() const
{
return m_syncType;
}
inline uint16_t GetClientId() const
{
return m_clientId;
}
inline uint16_t GetObjectId() const
{
return m_objectId;
}
inline NetObjEntityType GetEntityType() const
{
return m_entityType;
}
inline uint32_t GetTimestamp() const
{
return m_timestamp;
}
inline const std::vector<uint8_t>& GetCloneData() const
{
return m_cloneData;
}
inline uint16_t GetUniqifier() const
{
return m_uniqifier;
}
//private:
uint32_t m_handle;
uint16_t m_clientId;
uint16_t m_objectId;
uint16_t m_uniqifier;
NetObjEntityType m_entityType;
uint8_t m_syncType;
uint32_t m_timestamp;
uint32_t m_creationToken;
uint64_t m_dependentFrameIndex;
std::vector<uint8_t> m_cloneData;
};
msgClone::msgClone()
{
}
void msgClone::Read(int syncType, rl::MessageBuffer& buffer)
{
m_syncType = syncType;
m_objectId = buffer.Read<uint16_t>(13);
m_clientId = buffer.Read<uint16_t>(16);
if (syncType == 1)
{
m_entityType = (NetObjEntityType)buffer.Read<uint8_t>(kNetObjectTypeBitLength);
m_creationToken = 0;
if (icgi->NetProtoVersion >= 0x202002271209)
{
m_creationToken = buffer.Read<uint32_t>(32);
}
}
if (icgi->NetProtoVersion >= 0x201912301309)
{
m_uniqifier = buffer.Read<uint16_t>(16);
}
else
{
m_uniqifier = 0;
}
if (icgi->NetProtoVersion >= 0x202010191044)
{
m_dependentFrameIndex = (uint64_t(buffer.Read<uint32_t>(32)) << 32) | buffer.Read<uint32_t>(32);
}
m_timestamp = buffer.Read<uint32_t>(32);
auto length = buffer.Read<uint16_t>(12);
m_cloneData.resize(length);
buffer.ReadBits(m_cloneData.data(), m_cloneData.size() * 8);
}
class msgPackedClones
{
public:
msgPackedClones();
void Read(net::Buffer& buffer);
inline std::list<msgClone>& GetClones()
{
return m_clones;
}
inline const std::vector<std::tuple<uint16_t, uint16_t, bool>>& GetRemoves() const
{
return m_removes;
}
inline uint64_t GetFrameIndex()
{
return m_frameIndex;
}
private:
uint64_t m_frameIndex;
std::list<msgClone> m_clones;
std::vector<std::tuple<uint16_t, uint16_t, bool>> m_removes;
};
msgPackedClones::msgPackedClones()
{
}
void msgPackedClones::Read(net::Buffer& buffer)
{
m_frameIndex = buffer.Read<uint64_t>();
uint8_t bufferData[16384] = { 0 };
int bufferLength = LZ4_decompress_safe(reinterpret_cast<const char*>(&buffer.GetData()[buffer.GetCurOffset()]), reinterpret_cast<char*>(bufferData), buffer.GetRemainingBytes(), sizeof(bufferData));
if (bufferLength > 0)
{
rl::MessageBuffer msgBuf(bufferData, bufferLength);
bool end = false;
while (!msgBuf.IsAtEnd() && !end)
{
auto dataType = msgBuf.Read<uint8_t>(3);
switch (dataType)
{
case 1:
case 2:
{
msgClone clone;