-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdllmain.cpp
1741 lines (1250 loc) · 53.6 KB
/
dllmain.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 "pch.h"
#include "SDK.h"
#include<windows.h>
#include "MinHook.h"
#include <random>
#if defined _M_X64
#pragma comment(lib, "libMinHook-x64-v141-mt.lib")
#elif defined _M_IX86
#pragma comment(lib, "MinHook.x86.lib")
#endif
#include "set.h"
#include "SharedPointer.h"
#include <iostream>
#include <fstream>
enum EGameMode {
FREE_ROAM,
PLAYGROUND,
BR_SOLOS,
BR_DUOS,
BR_TRIOS,
BR_QUADS
};
EGameMode global_mode = EGameMode::FREE_ROAM;
bool serverListening = false;
uintptr_t global_baseaddress = 0;
CG::UWorld* global_world = nullptr;
CG::FName global_actor_name;
CG::UGameplayStatics* global_statics = nullptr;
CG::UEngine* global_engine = nullptr;
int32_t actorChannelNumber = 0;
std::vector<int> connectionsWithLevelsInit = std::vector<int>();
// - C++ Exceptions are /EHa (Yes with SEH Exceptions)
/*
typedef bool(__thiscall* func)(CG::AOnlineBeaconHost*);
uintptr_t baseAddress = (uintptr_t)GetModuleHandleA("rumbleverseclient-win64-shipping.exe");
std::cout << std::hex << baseAddress << std::endl;
//Add the offset of the function relative to the base
uintptr_t functAddress = baseAddress + 0x0ba78f0;
std::cout << std::hex << functAddress << std::endl;
func thefunc = (func)functAddress;
Sleep(10 * 1000);
std::cout << "ITS TIME!!!" << std::endl;
bool out = (*thefunc)(host);*/
static void log(std::string object, std::string function) {
/*
std::ofstream outputFile("groglog.txt", std::ios::app);
outputFile << "UFunction " << function << " called on " << object << std::endl;
outputFile.close();
*/
}
static void log(std::string msg) {
std::ofstream outputFile("groglog.txt", std::ios::app);
outputFile << msg << std::endl;
outputFile.close();
}
static CG::FString allocateFString(const wchar_t* string) {
CG::FString* orig = new CG::FString(string);
void* owo = reinterpret_cast<void* (*)(size_t size, size_t alignment)>(global_baseaddress + 0x1573d90)(sizeof(string), 0);
memcpy(owo, orig->Data(), sizeof(string));
orig->_data = (wchar_t*)owo;
return *orig;
}
template<typename T>
static void ListAllOfType() {
std::vector<T*> vec = CG::UObject::FindObjects<T>();
for (int i = 0; i < vec.size(); i++) {
log(vec[i]->GetFullName());
}
}
template<typename T>
static T* NewObject() {
CG::UGameplayStatics* statics = CG::UObject::FindObject<CG::UGameplayStatics>();
CG::UWorld* world = CG::UObject::FindObject<CG::UWorld>("World erder.erder");
return static_cast<T*>(statics->SpawnObject(T::StaticClass(), world));
}
bool CreateNamedNetDriver(CG::UEngine* engine, CG::UWorld* world) {
typedef bool(__thiscall* func)(CG::UEngine*, CG::UWorld*, CG::FName, CG::FName);
uintptr_t functAddress = global_baseaddress + 0x34f0af0;
func thefunc = (func)functAddress;
return (*thefunc)(engine, world, CG::FName("GameNetDriver"), CG::FName("GameNetDriver"));
}
CG::FWorldContext& GetWorldContextFromWorldChecked(CG::UEngine* engine, CG::UWorld* world) {
//0x34f91a0
typedef CG::FWorldContext&(__thiscall* func)(CG::UEngine* engine, CG::UWorld* world);
uintptr_t functAddress = global_baseaddress + 0x34f91a0;
func thefunc = (func)functAddress;
return (*thefunc)(engine, world);
}
int Browse(CG::UEngine* engine, CG::UWorld* world, CG::FURL url) {
//0x34f91a0
typedef bool(__thiscall* func)(CG::UEngine* engine, CG::FWorldContext& worldContext, CG::FURL url, CG::FString&);
uintptr_t functAddress = global_baseaddress + 0x34ebe10;
func thefunc = (func)functAddress;
CG::FWorldContext& wc = GetWorldContextFromWorldChecked(engine, world);
CG::FString error = CG::FString(L"");
return (*thefunc)(engine, wc, url, error);
}
bool InitListen(CG::UIpNetDriver* netdriver, CG::UObject* world) {
typedef bool(__thiscall* func)(CG::UIpNetDriver*, CG::UObject*, CG::FURL&, bool, CG::FString&);
uintptr_t functAddress = global_baseaddress + 0x0ba7e80;
func thefunc = (func)functAddress;
CG::FURL* furl = new CG::FURL();
furl->Map = CG::FString(L"erder");
furl->Port = 62169;
furl->Valid = 1;
CG::FString* error = new CG::FString(L"");
return (*thefunc)(netdriver, world, *furl, false, *error);
}
//0x3226800 - UNetDriver::TickFlush
//0x2dea830 - AActor::CallPreReplication
typedef void(__thiscall* TickFlush)(CG::UIpNetDriver*, float);
TickFlush originalTickFlush = NULL;
int ServerReplicateActors_PrepConnections(CG::UIpNetDriver* NetDriver, CG::UWorld* World)
{
auto& ClientConnections = NetDriver->ClientConnections;
int NumClientsToTick = ClientConnections.Count();
bool bFoundReadyConnection = false;
for (int ConnIdx = 0; ConnIdx < ClientConnections.Count(); ConnIdx++)
{
CG::UNetConnection* Connection = ClientConnections[ConnIdx];
if (!Connection) continue;
// check(Connection->State == USOCK_Pending || Connection->State == USOCK_Open || Connection->State == USOCK_Closed);
// checkSlow(Connection->GetUChildConnection() == NULL);
CG::AActor* OwningActor = Connection->OwningActor;
if (OwningActor != NULL) // && /* Connection->State == USOCK_Open && */ (Connection->Driver->Time - Connection->LastReceiveTime < 1.5f))
{
bFoundReadyConnection = true;
CG::AActor* DesiredViewTarget = OwningActor;
if (Connection->PlayerController)
{
if (CG::AActor* ViewTarget = Connection->PlayerController->GetViewTarget())
{
DesiredViewTarget = ViewTarget;
}
}
Connection->ViewTarget = DesiredViewTarget;
/*
for (int32 ChildIdx = 0; ChildIdx < Connection->Children.Count(); ChildIdx++)
{
CG::UNetConnection* Child = Connection->Children[ChildIdx];
CG::APlayerController* ChildPlayerController = Child->PlayerController;
if (ChildPlayerController != NULL)
{
Child->ViewTarget = ChildPlayerController->GetViewTarget();
}
else
{
Child->ViewTarget = NULL;
}
}
*/
}
else
{
Connection->ViewTarget = NULL;
/*
for (int32 ChildIdx = 0; ChildIdx < Connection->Children.Count(); ChildIdx++)
{
Connection->Children[ChildIdx]->ViewTarget = NULL;
}
*/
}
}
return bFoundReadyConnection ? NumClientsToTick : 0;
}
struct FNetworkObjectInfo
{
/** Pointer to the replicated actor. */
CG::AActor* Actor;
/** WeakPtr to actor. This is cached here to prevent constantly constructing one when needed for (things like) keys in TMaps/TSets */
CG::TWeakObjectPtr<CG::AActor> WeakActor;
/** Next time to consider replicating the actor. Based on FPlatformTime::Seconds(). */
double NextUpdateTime;
/** Last absolute time in seconds since actor actually sent something during replication */
double LastNetReplicateTime;
/** Optimal delta between replication updates based on how frequently actor properties are actually changing */
float OptimalNetUpdateDelta;
/** Last time this actor was updated for replication via NextUpdateTime
* @warning: internal net driver time, not related to WorldSettings.TimeSeconds */
float LastNetUpdateTime;
/** Is this object still pending a full net update due to clients that weren't able to replicate the actor at the time of LastNetUpdateTime */
UINT32 bPendingNetUpdate : 1;
/** Force this object to be considered relevant for at least one update */
UINT32 bForceRelevantNextUpdate : 1;
/** List of connections that this actor is dormant on */
TSet<CG::TWeakObjectPtr<CG::UNetConnection>> DormantConnections;
TSet<CG::TWeakObjectPtr<CG::UNetConnection>> RecentlyDormantConnections;
~FNetworkObjectInfo() {
//delete DormantConnections;
}
};
CG::ULevel* getLevelFromActor(CG::AActor* actor) {
CG::UObject* currentOuter = actor->Outer;
while (currentOuter->GetFullName().find("Level ") == std::string::npos) {
currentOuter = currentOuter->Outer;
}
return (CG::ULevel*)currentOuter;
}
class FNetworkObjectList
{
public:
typedef TSet<TSharedPtr<FNetworkObjectInfo>> FNetworkObjectSet;
FNetworkObjectSet AllNetworkObjects;
FNetworkObjectSet ActiveNetworkObjects;
FNetworkObjectSet ObjectsDormantOnAllConnections;
CG::TMap<CG::TWeakObjectPtr<CG::UNetConnection>, int32> NumDormantObjectsPerConnection;
void Remove(CG::AActor* const Actor) {
log("Remove init");
for (int i = 0; i < AllNetworkObjects.Num(); i++) {
if (AllNetworkObjects.IsIndexValid(i) && AllNetworkObjects[i].Object && (__int64)AllNetworkObjects[i].Object != 0xffffffffffffffff) {
if (AllNetworkObjects[i]->Actor == Actor) {
log("Remove from all");
AllNetworkObjects.Remove(i);
break;
}
}
}
log("scan active");
for (int i = 0; i < ActiveNetworkObjects.Num(); i++) {
if (ActiveNetworkObjects.IsIndexValid(i) && ActiveNetworkObjects[i].Object && (__int64)ActiveNetworkObjects[i].Object != 0xffffffffffffffff) {
if (ActiveNetworkObjects[i]->Actor == Actor) {
log("Remove from active");
ActiveNetworkObjects.Remove(i);
break;
}
}
}
log("Scan dormant");
for (int i = 0; i < ObjectsDormantOnAllConnections.Num(); i++) {
if (ObjectsDormantOnAllConnections.IsIndexValid(i) && ObjectsDormantOnAllConnections[i].Object && (__int64)ObjectsDormantOnAllConnections[i].Object != 0xffffffffffffffff) {
if (ObjectsDormantOnAllConnections[i]->Actor == Actor) {
log("Remove from dormant");
ObjectsDormantOnAllConnections.Remove(i);
break;
}
}
}
log("done!");
}
};
FNetworkObjectList& GetNetworkObjectList(CG::UNetDriver* NetDriver)
{
//log(std::to_string((__int64)(__int64(NetDriver) + 0x708)));
return *(*(TSharedPtr<FNetworkObjectList>*)(__int64(NetDriver) + 0x708));
}
FORCEINLINE float FRand()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(0, 1);
float random_number = dis(gen);
return random_number;
}
void ServerReplicateActors_BuildConsiderList(CG::UIpNetDriver* NetDriver, CG::UWorld* World, std::vector<TSharedPtr<FNetworkObjectInfo>>& OutConsiderList, float ServerTickTime)
{
/*
std::vector<CG::AActor*> ActorsToRemove;
auto& ActiveObjects = GetNetworkObjectList(NetDriver).ActiveNetworkObjects;
//auto World = global_world;
//AllNetworkObjects.IsIndexValid(i) && AllNetworkObjects[i].Object && (__int64)AllNetworkObjects[i].Object != 0xffffffffffffffff
//for(int i = 0; i < GetNetworkObjectList(NetDriver).ActiveNetworkObjects.Num(); i++)
for (const TSharedPtr<FNetworkObjectInfo>& ActorInfo : ActiveObjects)
{
//if (!(GetNetworkObjectList(NetDriver).ActiveNetworkObjects.IsIndexValid(i) && GetNetworkObjectList(NetDriver).ActiveNetworkObjects[i].Object && (__int64)GetNetworkObjectList(NetDriver).ActiveNetworkObjects[i].Object != 0xffffffffffffffff)) {
//continue;
//}
//const TSharedPtr<FNetworkObjectInfo>& ActorInfo = GetNetworkObjectList(NetDriver).ActiveNetworkObjects[i];
if (!ActorInfo->bPendingNetUpdate && global_statics->GetTimeSeconds(World) <= ActorInfo->NextUpdateTime)
{
continue;
}
auto Actor = ActorInfo->Actor;
if (!Actor)
continue;
if (Actor->IsActorBeingDestroyed())
// if (Actor->IsPendingKill())
{
ActorsToRemove.push_back(Actor);
continue;
}
//static auto RemoteRoleOffset = Actor->GetOffset("RemoteRole");
if (Actor->RemoteRole == CG::ENetRole::ROLE_None)
{
ActorsToRemove.push_back(Actor);
continue;
}
// We should add a NetDriverName check but I don't believe it is needed.
// We should check if the actor is initialized here.
// We should check the level stuff here.
//static auto NetDormancyOffset = Actor->GetOffset("NetDormancy");
if (Actor->NetDormancy == CG::ENetDormancy::DORM_Initial && Actor->bNetStartup) // IsDormInitialStartupActor
{
continue;
}
// We should check NeedsLoadForClient here.
// We should make sure the actor is in the same world here but I don't believe it is needed.
if (ActorInfo->LastNetReplicateTime == 0)
{
ActorInfo->LastNetReplicateTime = global_statics->GetTimeSeconds(World);
ActorInfo->OptimalNetUpdateDelta = 1.0f / Actor->NetUpdateFrequency;
}
const float ScaleDownStartTime = 2.0f;
const float ScaleDownTimeRange = 5.0f;
const float LastReplicateDelta = global_statics->GetTimeSeconds(World) - ActorInfo->LastNetReplicateTime;
if (LastReplicateDelta > ScaleDownStartTime)
{
if (Actor->MinNetUpdateFrequency == 0.0f)
{
Actor->MinNetUpdateFrequency = 2.0f;
}
const float MinOptimalDelta = 1.0f / Actor->NetUpdateFrequency; // Don't go faster than NetUpdateFrequency
const float MaxOptimalDelta = max(1.0f / Actor->MinNetUpdateFrequency, MinOptimalDelta); // Don't go slower than MinNetUpdateFrequency (or NetUpdateFrequency if it's slower)
const float Alpha = std::clamp((LastReplicateDelta - ScaleDownStartTime) / ScaleDownTimeRange, 0.0f, 1.0f); // should we use fmath?
ActorInfo->OptimalNetUpdateDelta = std::lerp(MinOptimalDelta, MaxOptimalDelta, Alpha); // should we use fmath?
}
if (!ActorInfo->bPendingNetUpdate)
{
constexpr bool bUseAdapativeNetFrequency = false;
const float NextUpdateDelta = bUseAdapativeNetFrequency ? ActorInfo->OptimalNetUpdateDelta : 1.0f / Actor->NetUpdateFrequency;
// then set the next update time
float ServerTickTime = 1.f / 30;
ActorInfo->NextUpdateTime = global_statics->GetTimeSeconds(World) + FRand() * ServerTickTime + NextUpdateDelta;
//static auto TimeOffset = GetOffset("Time");
ActorInfo->LastNetUpdateTime = NetDriver->Time;
}
ActorInfo->bPendingNetUpdate = false;
OutConsiderList.push_back(ActorInfo.Get());
typedef void(__thiscall* CallPreReplication)(CG::AActor*, CG::UNetDriver*);
//getLevelFromActor(Actor)->bIsVisible = true; //:(
((CallPreReplication)(global_baseaddress + 0x2dea830))(Actor, NetDriver);
}
*/
//CG::TArray<CG::AActor*>* Actors = new CG::TArray<CG::AActor*>();
//global_statics->GetAllActorsOfClass(World, CG::AActor::StaticClass(), Actors);
//for (int i = 0; i < Actors->Count(); ++i)
for (const TSharedPtr<FNetworkObjectInfo>& ActorInfo : GetNetworkObjectList(NetDriver).ActiveNetworkObjects)
{
CG::AActor* Actor = ActorInfo->Actor;
if (Actor->RemoteRole == CG::ENetRole::ROLE_None) {
//ActorsToRemove.push_back(Actor);
continue;
}
// We should add a NetDriverName check but I don't believe it is needed.
// We should check if the actor is initialized here.
// We should check the level stuff here.
if (Actor->NetDormancy == CG::ENetDormancy::DORM_Initial && Actor->bNetStartup) {
continue;
}
//auto ActorInfo = new FNetworkObjectInfo;
//ActorInfo->Actor = Actor;
OutConsiderList.push_back(ActorInfo);
typedef void(__thiscall* CallPreReplication)(CG::AActor*, CG::UNetDriver*);
//getLevelFromActor(Actor)->bIsVisible = true; //:(
((CallPreReplication)(global_baseaddress + 0x2dea830))(Actor, NetDriver);
}
/*
for (auto Actor : ActorsToRemove)
{
if (!Actor)
continue;
//LOG_INFO(LogDev, "Removing actor: {}", Actor ? Actor->GetFullName() : "InvalidObject");
//GetNetworkObjectList(NetDriver).Remove(Actor);
//LOG_INFO(LogDev, "Finished removing actor.");
}
*/
//delete Actors;
}
static CG::UActorChannel* FindChannel(CG::AActor* Actor, CG::UNetConnection* Connection)
{
auto& OpenChannels = Connection->OpenChannels;
//static auto ActorChannelClass = CG::UObject::FindClass("Engine.ActorChannel");
// LOG_INFO(LogReplication, "OpenChannels.Num(): {}", OpenChannels.Num());
for (int i = 0; i < OpenChannels.Count(); ++i)
{
auto Channel = OpenChannels[i];
if (!Channel)
continue;
// LOG_INFO(LogReplication, "[{}] Class {}", i, Channel->ClassPrivate ? Channel->ClassPrivate->GetFullName() : "InvalidObject");
if(Channel->Class->Name.Number != actorChannelNumber)
continue;
//if ((Channel->Class->GetFullName().find("ActorChannel") == std::string::npos)) // (Channel->ClassPrivate == ActorChannelClass)
auto ChannelActor = ((CG::UActorChannel*)Channel)->Actor;
// LOG_INFO(LogReplication, "[{}] {}", i, ChannelActor->GetFullName());
if (ChannelActor != Actor)
continue;
return (CG::UActorChannel*)Channel;
}
// LOG_INFO(LogDev, "Failed to find channel for {}!", Actor->GetName());
return nullptr;
}
struct FNetViewer
{
CG::UNetConnection* Connection; // 0x0000(0x0008) (ZeroConstructor, IsPlainOldData)
CG::AActor* InViewer; // 0x0008(0x0008) (ZeroConstructor, IsPlainOldData)
CG::AActor* ViewTarget; // 0x0010(0x0008) (ZeroConstructor, IsPlainOldData)
CG::FVector ViewLocation; // 0x0018(0x000C) (IsPlainOldData)
CG::FVector ViewDir;
};
#define INV_PI (0.31830988618f)
#define HALF_PI (1.57079632679f)
#define PI (3.1415926535897932f)
static FORCEINLINE void SinCos(float* ScalarSin, float* ScalarCos, float Value)
{
// Map Value to y in [-pi,pi], x = 2*pi*quotient + remainder.
float quotient = (INV_PI * 0.5f) * Value;
if (Value >= 0.0f)
{
quotient = (float)((int)(quotient + 0.5f));
}
else
{
quotient = (float)((int)(quotient - 0.5f));
}
float y = Value - (2.0f * PI) * quotient;
// Map y to [-pi/2,pi/2] with sin(y) = sin(Value).
float sign;
if (y > HALF_PI)
{
y = PI - y;
sign = -1.0f;
}
else if (y < -HALF_PI)
{
y = -PI - y;
sign = -1.0f;
}
else
{
sign = +1.0f;
}
float y2 = y * y;
// 11-degree minimax approximation
*ScalarSin = (((((-2.3889859e-08f * y2 + 2.7525562e-06f) * y2 - 0.00019840874f) * y2 + 0.0083333310f) * y2 - 0.16666667f) * y2 + 1.0f) * y;
// 10-degree minimax approximation
float p = ((((-2.6051615e-07f * y2 + 2.4760495e-05f) * y2 - 0.0013888378f) * y2 + 0.041666638f) * y2 - 0.5f) * y2 + 1.0f;
*ScalarCos = sign * p;
}
static CG::FVector rotatorToVector(CG::FRotator rotator) {
float CP, SP, CY, SY;
SinCos(&SP, &CP, rotator.Pitch * (PI / 180.0f));
SinCos(&SY, &CY, rotator.Yaw * (PI / 180.0f));
CG::FVector V = CG::FVector(CP * CY, CP * SY, SP);
return V;
}
static FNetViewer ConstructNetViewer(CG::UNetConnection* NetConnection)
{
FNetViewer newViewer{};
newViewer.Connection = NetConnection;
newViewer.InViewer = NetConnection->PlayerController ? NetConnection->PlayerController : NetConnection->OwningActor;
newViewer.ViewTarget = NetConnection->ViewTarget;
if (!NetConnection->OwningActor || !(!NetConnection->PlayerController || (NetConnection->PlayerController == NetConnection->OwningActor)))
return newViewer;
CG::APlayerController* ViewingController = NetConnection->PlayerController;
newViewer.ViewLocation = newViewer.ViewTarget->K2_GetActorLocation();
if (ViewingController)
{
CG::FRotator ViewRotation = ViewingController->ControlRotation; // hmmmm // ViewingController->GetControlRotation();
// AFortPlayerControllerAthena::GetPlayerViewPointHook(Cast<AFortPlayerControllerAthena>(ViewingController, false), newViewer.ViewLocation, ViewRotation);
ViewingController->GetActorEyesViewPoint(&newViewer.ViewLocation, &ViewRotation); // HMMM
// static auto GetActorEyesViewPointOffset = 0x5B0;
// void (*GetActorEyesViewPointOriginal)(AController*, FVector * a2, FRotator * a3) = decltype(GetActorEyesViewPointOriginal)(ViewingController->VFTable[GetActorEyesViewPointOffset / 8]);
// GetActorEyesViewPointOriginal(ViewingController, &newViewer.ViewLocation, &ViewRotation);
// AFortPlayerControllerAthena::GetPlayerViewPointHook((AFortPlayerControllerAthena*)ViewingController, newViewer.ViewLocation, ViewRotation);
newViewer.ViewDir = rotatorToVector(ViewRotation);
}
return newViewer;
}
static bool IsActorRelevantToConnection(CG::AActor* Actor, std::vector<FNetViewer>& ConnectionViewers)
{
for (int32 viewerIdx = 0; viewerIdx < ConnectionViewers.size(); viewerIdx++)
{
if (!ConnectionViewers[viewerIdx].ViewTarget)
continue;
// static bool (*IsNetRelevantFor)(AActor*, AActor*, AActor*, FVector&) = decltype(IsNetRelevantFor)(__int64(GetModuleHandleW(0)) + 0x1ECC700);
// if (Actor->IsNetRelevantFor(ConnectionViewers[viewerIdx].InViewer, ConnectionViewers[viewerIdx].ViewTarget, ConnectionViewers[viewerIdx].ViewLocation))
// if (IsNetRelevantFor(Actor, ConnectionViewers[viewerIdx].InViewer, ConnectionViewers[viewerIdx].ViewTarget, ConnectionViewers[viewerIdx].ViewLocation))
if (reinterpret_cast<bool(*)(CG::AActor*, CG::AActor*, CG::AActor*, CG::FVector&)>(global_baseaddress + 0x2dfb220)(
Actor, ConnectionViewers[viewerIdx].InViewer, ConnectionViewers[viewerIdx].ViewTarget, ConnectionViewers[viewerIdx].ViewLocation))
{
return true;
}
}
return false;
}
bool levelInitForConnection(int connectionIdx) {
for (int connIdx : connectionsWithLevelsInit) {
if (connectionIdx == connIdx) {
return true;
}
}
return false;
}
CG::UPackage* getPackageFromUObject(CG::UObject* object) {
CG::UObject* currentOuter = object->Outer;
while (currentOuter->GetFullName().find("Package ") == std::string::npos) {
currentOuter = currentOuter->Outer;
}
return (CG::UPackage*)currentOuter;
}
/*
bool IsActorDormant(CG::UNetDriver* NetDriver, FNetworkObjectInfo* ActorInfo, CG::TWeakObjectPtr<CG::UNetConnection> Connection) {
return ActorInfo->DormantConnections.Contains(Connection);
}
bool ShouldActorGoDormant(CG::AActor* Actor, std::vector<FNetViewer> ConnectionViewers, CG::UActorChannel* Channel, float Time, bool bLowNetBandwidth) {
if (Actor->NetDormancy <= CG::ENetDormancy::DORM_Awake || !Channel)
{
// Either shouldn't go dormant, or is already dormant
return false;
}
if (Actor->NetDormancy == CG::ENetDormancy::DORM_DormantPartial)
{
for (int viewerIdx = 0; viewerIdx < ConnectionViewers.size(); viewerIdx++)
{
if (!false)
{
return false;
}
}
}
return true;
}
*/
int ServerReplicateActors(CG::UIpNetDriver* NetDriver, CG::UWorld* World, float DeltaSeconds)
{
int Updated = 0;
++(*(int*)(__int64(NetDriver) + 0x420)); //might have to fix
const int NumClientsToTick = ServerReplicateActors_PrepConnections(NetDriver, World);
if (NumClientsToTick == 0)
{
// No connections are ready this frame
return 0;
}
// bool bCPUSaturated = false;
//0x34f88f0
float ServerTickTime = reinterpret_cast<bool(*)(CG::UEngine*, float, bool)>(global_baseaddress + 0x34f88f0)(global_engine, DeltaSeconds, false); //tickrate
//float ServerTickTime = NetDriver->NetServerMaxTickRate;
if (ServerTickTime == 0.f)
{
ServerTickTime = DeltaSeconds;
}
else
{
ServerTickTime = 1.f / ServerTickTime;
// bCPUSaturated = DeltaSeconds > 1.2f * ServerTickTime;
}
std::vector< TSharedPtr<FNetworkObjectInfo>> ConsiderList = std::vector<TSharedPtr<FNetworkObjectInfo>>();
ConsiderList.reserve(GetNetworkObjectList(NetDriver).ActiveNetworkObjects.Num());
//log("Pre ConsiderList");
ServerReplicateActors_BuildConsiderList(NetDriver, World, ConsiderList, ServerTickTime);
//log("Post ConsiderList");
//std::vector<CG::UNetConnection*> connectionsToClose = std::vector<CG::UNetConnection*>();
// LOG_INFO(LogReplication, "Considering {} actors.", ConsiderList.size());
for (int32 i = 0; i < NetDriver->ClientConnections.Count(); i++)
{
CG::UNetConnection* Connection = NetDriver->ClientConnections[i];
if (!Connection)
continue;
if (i >= NumClientsToTick)
continue;
if (!Connection->ViewTarget)
continue;
/*
if (!levelInitForConnection(i)) {
log("Init level time!");
connectionsWithLevelsInit.push_back(i);
for (CG::ULevel* level : CG::UObject::FindObjects<CG::ULevel>()) {
if (getPackageFromUObject(level)->Name.GetName().find("/Game/Maps/Island2/") != std::string::npos) {
log(getPackageFromUObject(level)->Name.GetName());
CG::FUpdateLevelVisibilityLevelInfo visInfo = CG::FUpdateLevelVisibilityLevelInfo();
visInfo.bIsVisible = true;
visInfo.Filename = getPackageFromUObject(level)->Name;
visInfo.PackageName = getPackageFromUObject(level)->Name;
//visInfo.Filename = level->Name;
//visInfo.PackageName = level->Name;
reinterpret_cast<void(*)(CG::UNetConnection*, CG::FUpdateLevelVisibilityLevelInfo&)>(global_baseaddress + 0x31feaa0)(Connection, visInfo);
level->bIsVisible = true;
}
}
for (CG::ULevelStreaming* level : CG::UObject::FindObjects<CG::ULevelStreaming>()) {
if (getPackageFromUObject(level)->Name.GetName().find("/Game/Maps/Island2/") != std::string::npos) {
log(getPackageFromUObject(level)->Name.GetName());
CG::FUpdateLevelVisibilityLevelInfo visInfo = CG::FUpdateLevelVisibilityLevelInfo();
visInfo.bIsVisible = true;
visInfo.Filename = getPackageFromUObject(level)->Name;
visInfo.PackageName = getPackageFromUObject(level)->Name;
//visInfo.Filename = level->Name;
//visInfo.PackageName = level->Name;
reinterpret_cast<void(*)(CG::UNetConnection*, CG::FUpdateLevelVisibilityLevelInfo&)>(global_baseaddress + 0x31feaa0)(Connection, visInfo);
level->bDisableDistanceStreaming = true;
level->SetShouldBeLoaded(true);
level->SetShouldBeVisible(true);
}
}
}
*/
if (Connection->PlayerController)
{
typedef void(__thiscall* SendClientAdjustment)(CG::APlayerController*);
//log("Pre SendClientAdjustment");
((SendClientAdjustment)((size_t*)Connection->PlayerController->VfTable)[0x1BD])(Connection->PlayerController); //MAYBE SCUFFED IF WE CRASH LOOK HERE
//log("Post SendClientAdjustment");
}
/*
for (int32 ChildIdx = 0; ChildIdx < Connection->Children.Count(); ChildIdx++)
{
if (Connection->Children[ChildIdx]->PlayerController != NULL)
{
typedef void(__thiscall* SendClientAdjustment)(CG::APlayerController*);
((SendClientAdjustment)((size_t*)Connection->PlayerController->VfTable)[0x1BD])(Connection->Children[ChildIdx]->PlayerController);
//->SendClientAdjustment();
}
}
*/
// Make weak ptr once for IsActorDormant call
//log("SerialNumber Bullshit");
CG::TWeakObjectPtr<CG::UNetConnection> WeakConnection{};
WeakConnection.ObjectIndex = Connection->InternalIndex;
WeakConnection.ObjectSerialNumber = CG::UObject::GObjects->GetItemByIndex(Connection->InternalIndex)->SerialNumber;
//log("Post SerialNumber Bullshit");
/* GetNetTag()++;
Connection->GetTickCount()++;
for (int32 j = 0; j < Connection->GetSentTemporaries().Num(); j++) // Set up to skip all sent temporary actors
{
Connection->GetSentTemporaries().at(j)->GetNetTag() = GetNetTag();
} */
for (auto& ActorInfo : ConsiderList)
{
//if (!ActorInfo || !ActorInfo->Actor)
if (!ActorInfo->Actor)
continue;
auto Actor = ActorInfo->Actor;
auto Channel = FindChannel(Actor, Connection);
/*
if (IsActorDormant(NetDriver, ActorInfo, WeakConnection))
{
continue;
}*/
std::vector<FNetViewer> ConnectionViewers = std::vector<FNetViewer>();
ConnectionViewers.push_back(ConstructNetViewer(Connection));
const bool bLevelInitializedForActor = true; //This makes me :(
if (!Channel)
{
// if (!IsLevelInitializedForActor(Actor, Connection))
if (!bLevelInitializedForActor)
{
// If the level this actor belongs to isn't loaded on client, don't bother sending
continue;
}
if (!IsActorRelevantToConnection(Actor, ConnectionViewers))
{
// If not relevant (and we don't have a channel), skip
continue;
}
}
bool bLowNetBandwidth = false;
/*
// See of actor wants to try and go dormant
if (ShouldActorGoDormant(Actor, ConnectionViewers, Channel, 0, bLowNetBandwidth))
{
// LOG_INFO(LogDev, "Actor is going dormant!");
// Channel is marked to go dormant now once all properties have been replicated (but is not dormant yet)
Channel->StartBecomingDormant();
}
*/
static void (*ActorChannelClose)(CG::UActorChannel*) = decltype(ActorChannelClose)(global_baseaddress + 0x2ff2c20);
if (!Actor->bAlwaysRelevant && !Actor->bNetUseOwnerRelevancy && !Actor->bOnlyRelevantToOwner)
{
if (Connection && Connection->ViewTarget)
{
auto Viewer = Connection->ViewTarget;
auto Loc = Viewer->K2_GetActorLocation();
if (!IsActorRelevantToConnection(Actor, ConnectionViewers))
{
// LOG_INFO(LogReplication, "Actor is not relevant!");
if (Channel) {
ActorChannelClose(Channel);
}
continue;
}
}
}
/*
if (Addresses::ActorChannelClose && Offsets::IsNetRelevantFor)
{
static void (*ActorChannelClose)(UActorChannel*) = decltype(ActorChannelClose)(Addresses::ActorChannelClose);
if (!Actor->IsAlwaysRelevant() && !Actor->UsesOwnerRelevancy() && !Actor->IsOnlyRelevantToOwner())
{
if (Connection && Connection->GetViewTarget())
{
auto Viewer = Connection->GetViewTarget();
auto Loc = Viewer->GetActorLocation();
if (!IsActorRelevantToConnection(Actor, ConnectionViewers))
{
// LOG_INFO(LogReplication, "Actor is not relevant!");
if (Channel)
ActorChannelClose(Channel);
continue;
}
}
}
}
*/
enum class EChannelCreateFlags : uint32_t
{
None = (1 << 0),
OpenedLocally = (1 << 1)
};
uintptr_t baseAddress = global_baseaddress;
static __int64 (*ReplicateActor)(CG::UActorChannel*) = decltype(ReplicateActor)(baseAddress + 0x300c2f0);
static CG::UObject* (*CreateChannelByName)(CG::UNetConnection * Connection, CG::FName * ChName, EChannelCreateFlags CreateFlags, int32_t ChannelIndex) = decltype(CreateChannelByName)(baseAddress + 0x31e2d40);
static __int64 (*SetChannelActor)(CG::UActorChannel*, CG::AActor*) = decltype(SetChannelActor)(baseAddress + 0x3010f30);
if (!Channel)
{
if (Actor->IsA(CG::APlayerController::StaticClass()) && Actor != Connection->PlayerController) // isnetrelevantfor should handle this iirc
continue;
if (bLevelInitializedForActor)
{
//CG::FString ActorStr = L"Actor";
CG::FName ActorName = global_actor_name;
int ChannelIndex = -1; // 4294967295
Channel = (CG::UActorChannel*)CreateChannelByName(Connection, &ActorName, EChannelCreateFlags::OpenedLocally, ChannelIndex);
if (Channel)
{
SetChannelActor(Channel, Actor);
}
}
else if (Actor->NetUpdateFrequency < 1.0f)
{
//ActorInfo->NextUpdateTime = global_statics->GetTimeSeconds(World) + 0.2f * FRand();
}
}
if (Channel)
{
if (ReplicateActor(Channel)) {
auto TimeSeconds = global_statics->GetTimeSeconds(World);
const float MinOptimalDelta = 1.0f / Actor->NetUpdateFrequency;
const float MaxOptimalDelta = max(1.0f / Actor->MinNetUpdateFrequency, MinOptimalDelta);
const float DeltaBetweenReplications = (TimeSeconds - ActorInfo->LastNetReplicateTime);
// Choose an optimal time, we choose 70% of the actual rate to allow frequency to go up if needed
ActorInfo->OptimalNetUpdateDelta = std::clamp(DeltaBetweenReplications * 0.7f, MinOptimalDelta, MaxOptimalDelta); // should we use fmath?
ActorInfo->LastNetReplicateTime = TimeSeconds;
}
}
}
}
/*