Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
HooksImpl.cpp
Go to the documentation of this file.
1#include "HooksImpl.h"
2
3#include "ApiUtils.h"
4#include "../Commands.h"
5#include "../Hooks.h"
6#include "../PluginManager/PluginManager.h"
7#include "../IBaseApi.h"
8#include <../Private/Ark/Globals.h>
9#include "Containers/UnrealString.h"
10#include "HAL/UnrealMemory.h"
11#include <Logger/Logger.h>
12//temp predefines
13struct AShooterGameMode;
17struct AGameState;
18struct UWorld;
20struct RCONPacket;
21struct URCONServer;
22struct APawn;
23struct AController;
24//temp predefines end
25
26namespace AsaApi
27{
28 // Hooks declaration
29 DECLARE_HOOK(UEngine_Init, void, DWORD64, DWORD64);
30 DECLARE_HOOK(UWorld_Tick, void, DWORD64, DWORD64, float);
37 DECLARE_HOOK(URCONServer_Init, bool, URCONServer*, FString*, unsigned int, UShooterCheatManager*);
42
43 void InitHooks()
44 {
45 auto& hooks = API::game_api->GetHooks();
46 hooks->SetHook("UEngine.Init(IEngineLoop*)", &Hook_UEngine_Init, &UEngine_Init_original);
47 hooks->SetHook("UWorld.Tick(ELevelTick,float)", &Hook_UWorld_Tick, &UWorld_Tick_original);
48 hooks->SetHook("AShooterGameMode.InitGame(FString&,FString&,FString&)", &Hook_AShooterGameMode_InitGame, &AShooterGameMode_InitGame_original);
49 hooks->SetHook("AShooterPlayerController.ServerSendChatMessage_Implementation(FString&,EChatSendMode::Type,int)", &Hook_AShooterPlayerController_ServerSendChatMessage_Impl, &AShooterPlayerController_ServerSendChatMessage_Impl_original);
50 hooks->SetHook("AShooterPlayerController.ConsoleCommand(FString&,bool)", &Hook_AShooterPlayerController_ConsoleCommand, &AShooterPlayerController_ConsoleCommand_original);
51 hooks->SetHook("RCONClientConnection.ProcessRCONPacket(RCONPacket&,UWorld*)", &Hook_RCONClientConnection_ProcessRCONPacket, &RCONClientConnection_ProcessRCONPacket_original);
52 hooks->SetHook("AGameState.DefaultTimer()", &Hook_AGameState_DefaultTimer, &AGameState_DefaultTimer_original);
53 hooks->SetHook("AShooterGameMode.BeginPlay()", &Hook_AShooterGameMode_BeginPlay, &AShooterGameMode_BeginPlay_original);
54 hooks->SetHook("URCONServer.Init(FString,int,UShooterCheatManager*)", &Hook_URCONServer_Init, &URCONServer_Init_original);
55 hooks->SetHook("AShooterPlayerController.OnPossess(APawn*)", &Hook_AShooterPlayerController_OnPossess, &AShooterPlayerController_OnPossess_original);
56 hooks->SetHook("AShooterGameMode.Logout(AController*)", &Hook_AShooterGameMode_Logout, &AShooterGameMode_Logout_original);
57 hooks->SetHook("UShooterCheatManager.Broadcast(FString&)", &Hook_UShooterCheatManager_Broadcast, &UShooterCheatManager_Broadcast_original);
58 hooks->SetHook("AShooterGameMode.HandleNewPlayer_Implementation(AShooterPlayerController*,UPrimalPlayerData*,AShooterCharacter*,bool)", &Hook_AShooterGameMode_HandleNewPlayer_Implementation, &AShooterGameMode_HandleNewPlayer_Implementation_original);
59
60 Log::GetLog()->info("Initialized hooks\n");
61 }
62
63 // Hooks
64
65 void Hook_UEngine_Init(DWORD64 _this, DWORD64 InEngineLoop)
66 {
67 UEngine_Init_original(_this, InEngineLoop);
68
69 Log::GetLog()->info("UGameEngine::Init was called");
70 Log::GetLog()->info("Loading plugins..\n");
71
73
74 dynamic_cast<API::IBaseApi&>(*API::game_api).RegisterCommands();
75 }
76
77 void Hook_UWorld_Tick(DWORD64 world, DWORD64 tick_type, float delta_seconds)
78 {
79 Commands* command = dynamic_cast<Commands*>(API::game_api->GetCommands().get());
80 if (command)
81 command->CheckOnTickCallbacks(delta_seconds);
82
83 UWorld_Tick_original(world, tick_type, delta_seconds);
84 }
85
86 void Hook_AShooterGameMode_InitGame(AShooterGameMode* a_shooter_game_mode, FString* map_name, FString* options, FString* error_message)
87 {
88 Log::GetLog()->info("AShooterGameMode::InitGame was called");
89 dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).SetShooterGameMode(a_shooter_game_mode);
90 dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).SetWorld(a_shooter_game_mode->GetWorld());
91
92 AShooterGameMode_InitGame_original(a_shooter_game_mode, map_name, options, error_message);
93
94 const auto& actors = AsaApi::GetApiUtils().GetWorld()->PersistentLevelField().Get()->ActorsField();
95 for (auto actor : actors)
96 {
97 FString bp = AsaApi::GetApiUtils().GetBlueprint(actor);
98 if (bp.Equals("Blueprint'/Script/ShooterGame.PrimalPersistentWorldData'"))
99 {
100 if (actor->TargetingTeamField() == 0)
101 actor->TargetingTeamField() = a_shooter_game_mode->ServerIDField();
102
103 a_shooter_game_mode->MyServerIdField() = FString(std::to_string(actor->TargetingTeamField()));
104 a_shooter_game_mode->ServerIDField() = actor->TargetingTeamField();
105 Log::GetLog()->info("SERVER ID: {}", a_shooter_game_mode->ServerIDField());
106
107 break;
108 }
109 }
110
111 dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).CheckMessagingManagersRequirements();
112 }
113
114 void Hook_AShooterPlayerController_ServerSendChatMessage_Impl(AShooterPlayerController* player_controller, FString* message, int mode, int senderPlatform)
115 {
116 const long double last_chat_time = player_controller->LastChatMessageTimeField();
117 const long double now_time = AsaApi::GetApiUtils().GetWorld()->TimeSecondsField();
118
119 const auto spam_check = now_time - last_chat_time < 1.0;
120 if (last_chat_time > 0 && spam_check)
121 return;
122
123 player_controller->LastChatMessageTimeField() = now_time;
124
125 const auto command_executed = dynamic_cast<AsaApi::Commands&>(*API::game_api->GetCommands()).CheckChatCommands(player_controller, message, mode, senderPlatform);
126
127 const auto prevent_default = dynamic_cast<AsaApi::Commands&>(*API::game_api->GetCommands()).CheckOnChatMessageCallbacks(player_controller, message, mode, senderPlatform, spam_check, command_executed);
128
129 if (command_executed || prevent_default)
130 return;
131
132 AShooterPlayerController_ServerSendChatMessage_Impl_original(player_controller, message, mode, senderPlatform);
133 }
134
136 {
137 if (dynamic_cast<Commands&>(*API::game_api->GetCommands()).CheckConsoleCommands(_this, Command, bWriteToLog))
138 Command->Empty();
139
140 if (HideCommand)
141 return _this->PlayerField().Get()->ConsoleCommand(result, Command, false);
142 else
143 return AShooterPlayerController_ConsoleCommand_original(_this, result, Command, bWriteToLog);
144 }
145
147 {
149 {
150 if (dynamic_cast<Commands&>(*API::game_api->GetCommands()).CheckRconCommands(_this, packet, in_world))
151 return;
152 }
153
154 RCONClientConnection_ProcessRCONPacket_original(_this, packet, in_world);
155 }
156
158 {
159 Commands* command = dynamic_cast<Commands*>(API::game_api->GetCommands().get());
160 if (command)
162
163 API::PluginManager::DetectPluginChangesTimerCallback(); // We call this here to avoid UnknownModule crashes
164
165 AGameState_DefaultTimer_original(_this);
166 }
167
169 {
170 AShooterGameMode_BeginPlay_original(_AShooterGameMode);
171 dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).SetStatus(ServerStatus::Ready);
172 }
173
174 bool Hook_URCONServer_Init(URCONServer* _this, FString* Password, unsigned int InPort, UShooterCheatManager* SCheatManager)
175 {
176 dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).SetCheatManager(SCheatManager);
177
178 return URCONServer_Init_original(_this, Password, InPort, SCheatManager);
179 }
180
182 {
183 dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).SetPlayerController(_this);
184
185 AShooterPlayerController_OnPossess_original(_this, inPawn);
186 }
187
189 {
190 AShooterPlayerController* Exiting_SPC = static_cast<AShooterPlayerController*>(Exiting);
191 dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).RemovePlayerController(Exiting_SPC);
192
193 AShooterGameMode_Logout_original(_this, Exiting);
194 }
195
197 {
198 if (!_this->MyPCField())
199 {
202 }
203 else
204 return UShooterCheatManager_Broadcast_original(_this, msg);
205 }
206
208 {
209 dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).SetPlayerController(NewPlayer);
210
211 return AShooterGameMode_HandleNewPlayer_Implementation_original(_this, NewPlayer, PlayerData, PlayerCharacter, bIsFromLogin);
212 }
213} // namespace AsaApi
#define DECLARE_HOOK(name, returnType,...)
Definition Base.h:829
bool HideCommand
Definition Globals.h:3
void LoadAllPlugins()
Find and load all plugins.
static PluginManager & Get()
static void DetectPluginChangesTimerCallback()
Checks for auto plugin reloads.
void CheckOnTickCallbacks(float delta_seconds)
Definition Commands.cpp:86
void CheckOnTimerCallbacks()
Definition Commands.cpp:98
virtual AShooterGameMode * GetShooterGameMode() const =0
Returns a pointer to AShooterGameMode.
static FORCEINLINE FString GetEOSIDFromController(AController *controller)
Returns EOS ID from player controller.
virtual UWorld * GetWorld() const =0
Returns a pointer to UWorld.
static const FColor Yellow
Definition ColorList.h:28
ARK_API void Empty()
Definition String.cpp:321
Definition IBaseApi.h:9
void Hook_UShooterCheatManager_Broadcast(UShooterCheatManager *_this, FString *msg)
void Hook_AShooterGameMode_BeginPlay(AShooterGameMode *_AShooterGameMode)
void Hook_AShooterGameMode_Logout(AShooterGameMode *_this, AController *Exiting)
void Hook_AShooterPlayerController_OnPossess(AShooterPlayerController *_this, APawn *inPawn)
IApiUtils & GetApiUtils()
Definition ApiUtils.cpp:206
void Hook_AShooterGameMode_InitGame(AShooterGameMode *a_shooter_game_mode, FString *map_name, FString *options, FString *error_message)
Definition HooksImpl.cpp:86
void Hook_AShooterPlayerController_ServerSendChatMessage_Impl(AShooterPlayerController *player_controller, FString *message, int mode, int senderPlatform)
bool Hook_AShooterGameMode_HandleNewPlayer_Implementation(AShooterGameMode *_this, AShooterPlayerController *NewPlayer, UPrimalPlayerData *PlayerData, AShooterCharacter *PlayerCharacter, bool bIsFromLogin)
bool Hook_URCONServer_Init(URCONServer *_this, FString *Password, unsigned int InPort, UShooterCheatManager *SCheatManager)
void InitHooks()
Definition HooksImpl.cpp:43
void Hook_AGameState_DefaultTimer(AGameState *_this)
void Hook_UEngine_Init(DWORD64 _this, DWORD64 InEngineLoop)
Definition HooksImpl.cpp:65
void Hook_RCONClientConnection_ProcessRCONPacket(RCONClientConnection *_this, RCONPacket *packet, UWorld *in_world)
FString * Hook_AShooterPlayerController_ConsoleCommand(AShooterPlayerController *_this, FString *result, FString *Command, bool bWriteToLog)
void Hook_UWorld_Tick(DWORD64 world, DWORD64 tick_type, float delta_seconds)
Definition HooksImpl.cpp:77
void SendServerChatMessage(FString *MessageText, FLinearColor MessageColor, bool bIsBold, int ReceiverTeamId, int ReceiverPlayerID, FString SenderID)
Definition GameMode.h:2364
long double & LastChatMessageTimeField()
Definition Actor.h:2879
bool & IsAuthenticatedField()
Definition Other.h:26
AShooterPlayerController *& MyPCField()
Definition Actor.h:8534
APlayerController * GetFirstPlayerController()
Definition GameMode.h:743
long double & TimeSecondsField()
Definition GameMode.h:558