Ark Server API (ASE) - 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
10#include <Logger/Logger.h>
11
12namespace ArkApi
13{
14 // Hooks declaration
15 DECLARE_HOOK(UEngine_Init, void, DWORD64, DWORD64);
17 DECLARE_HOOK(UWorld_Tick, void, DWORD64, DWORD64, float);
30
31 void InitHooks()
32 {
33 auto& hooks = API::game_api->GetHooks();
34
35 hooks->SetHook("UEngine.Init", &Hook_UEngine_Init, &UEngine_Init_original);
36 hooks->SetHook("UWorld.InitWorld", &Hook_UWorld_InitWorld, &UWorld_InitWorld_original);
37 hooks->SetHook("UWorld.Tick", &Hook_UWorld_Tick, &UWorld_Tick_original);
38 hooks->SetHook("AShooterGameMode.InitGame", &Hook_AShooterGameMode_InitGame,
39 &AShooterGameMode_InitGame_original);
40 hooks->SetHook("AShooterPlayerController.ServerSendChatMessage_Implementation",
41 &Hook_AShooterPlayerController_ServerSendChatMessage_Impl,
42 &AShooterPlayerController_ServerSendChatMessage_Impl_original);
43 hooks->SetHook("APlayerController.ConsoleCommand", &Hook_APlayerController_ConsoleCommand,
44 &APlayerController_ConsoleCommand_original);
45 hooks->SetHook("AShooterPlayerController.ConsoleCommand", &Hook_AShooterPlayerController_ConsoleCommand,
46 &AShooterPlayerController_ConsoleCommand_original);
47 hooks->SetHook("RCONClientConnection.ProcessRCONPacket", &Hook_RCONClientConnection_ProcessRCONPacket,
48 &RCONClientConnection_ProcessRCONPacket_original);
49 hooks->SetHook("AGameState.DefaultTimer", &Hook_AGameState_DefaultTimer, &AGameState_DefaultTimer_original);
50 hooks->SetHook("AShooterGameMode.BeginPlay", &Hook_AShooterGameMode_BeginPlay,
51 &AShooterGameMode_BeginPlay_original);
52 hooks->SetHook("URCONServer.Init", &Hook_URCONServer_Init, &URCONServer_Init_original);
53 hooks->SetHook("APlayerController.ServerReceivedPlayerControllerAck_Implementation", &Hook_APlayerController_ServerReceivedPlayerControllerAck_Implementation,
54 &APlayerController_ServerReceivedPlayerControllerAck_Implementation_original);
55 hooks->SetHook("AShooterPlayerController.Possess", &Hook_AShooterPlayerController_Possess,
56 &AShooterPlayerController_Possess_original);
57 hooks->SetHook("AShooterGameMode.Logout", &Hook_AShooterGameMode_Logout, &AShooterGameMode_Logout_original);
58
59 Log::GetLog()->info("Initialized hooks\n");
60 }
61
62 // Hooks
63
64 void Hook_UEngine_Init(DWORD64 _this, DWORD64 InEngineLoop)
65 {
66 UEngine_Init_original(_this, InEngineLoop);
67
68 Log::GetLog()->info("UGameEngine::Init was called");
69 Log::GetLog()->info("Loading plugins..\n");
70
72
73 dynamic_cast<API::IBaseApi&>(*API::game_api).RegisterCommands();
74 }
75
76 void Hook_UWorld_InitWorld(UWorld* world, DWORD64 ivs)
77 {
78 Log::GetLog()->info("UWorld::InitWorld was called");
79
80 dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).SetWorld(world);
81
82 UWorld_InitWorld_original(world, ivs);
83 }
84
85 void Hook_UWorld_Tick(DWORD64 world, DWORD64 tick_type, float delta_seconds)
86 {
87 Commands* command = dynamic_cast<Commands*>(API::game_api->GetCommands().get());
88 if (command)
89 {
90 command->CheckOnTickCallbacks(delta_seconds);
91 }
92
93 UWorld_Tick_original(world, tick_type, delta_seconds);
94 }
95
96 void Hook_AShooterGameMode_InitGame(AShooterGameMode* a_shooter_game_mode, FString* map_name, FString* options,
97 FString* error_message)
98 {
99 dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).SetShooterGameMode(a_shooter_game_mode);
100
101 AShooterGameMode_InitGame_original(a_shooter_game_mode, map_name, options, error_message);
102 }
103
105 AShooterPlayerController* player_controller, FString* message, EChatSendMode::Type mode)
106 {
107 const long double last_chat_time = player_controller->LastChatMessageTimeField();
108 const long double now_time = ArkApi::GetApiUtils().GetWorld()->TimeSecondsField();
109
110 const auto spam_check = now_time - last_chat_time < 1.0;
111 if (last_chat_time > 0 && spam_check)
112 {
113 return;
114 }
115
116 player_controller->LastChatMessageTimeField() = now_time;
117
118 const auto command_executed = dynamic_cast<ArkApi::Commands&>(*API::game_api->GetCommands()).
119 CheckChatCommands(player_controller, message, mode);
120
121 const auto prevent_default = dynamic_cast<ArkApi::Commands&>(*API::game_api->GetCommands()).
122 CheckOnChatMessageCallbacks(player_controller, message, mode, spam_check, command_executed);
123
124 if (command_executed || prevent_default)
125 {
126 return;
127 }
128
129 AShooterPlayerController_ServerSendChatMessage_Impl_original(player_controller, message, mode);
130 }
131
133 FString* cmd, bool write_to_log)
134 {
135 dynamic_cast<Commands&>(*API::game_api->GetCommands()).CheckConsoleCommands(
136 a_player_controller, cmd, write_to_log);
137
138 return APlayerController_ConsoleCommand_original(a_player_controller, result, cmd, write_to_log);
139 }
140
142 {
143 if (HideCommand)
144 return ((APlayerController*)_this)->ConsoleCommand(result, Command, false);
145 else
146 return AShooterPlayerController_ConsoleCommand_original(_this, result, Command, bWriteToLog);
147 }
148
150 UWorld* in_world)
151 {
153 {
154 dynamic_cast<Commands&>(*API::game_api->GetCommands()).CheckRconCommands(_this, packet, in_world);
155 }
156
157 RCONClientConnection_ProcessRCONPacket_original(_this, packet, in_world);
158 }
159
161 {
162 Commands* command = dynamic_cast<Commands*>(API::game_api->GetCommands().get());
163 if (command)
164 {
166 }
167
168 API::PluginManager::DetectPluginChangesTimerCallback(); // We call this here to avoid UnknownModule crashes
169
170 AGameState_DefaultTimer_original(_this);
171 }
172
174 {
175 AShooterGameMode_BeginPlay_original(_AShooterGameMode);
176
177 dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).SetStatus(ServerStatus::Ready);
178 }
179
180 bool Hook_URCONServer_Init(URCONServer* _this, FString Password, int InPort, UShooterCheatManager* SCheatManager)
181 {
182 dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).SetCheatManager(SCheatManager);
183
184 return URCONServer_Init_original(_this, Password, InPort, SCheatManager);
185 }
186
188 {
189 APlayerController_ServerReceivedPlayerControllerAck_Implementation_original(_this);
190
191 if (_this)
192 {
193 AShooterPlayerController* ASPC = static_cast<AShooterPlayerController*>(_this);
194 dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).SetPlayerController(ASPC);
195 }
196 }
197
199 {
200 dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).SetPlayerController(_this);
201
202 AShooterPlayerController_Possess_original(_this, inPawn);
203 }
204
206 {
207 AShooterPlayerController* Exiting_SPC = static_cast<AShooterPlayerController*>(Exiting);
208 dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).RemovePlayerController(Exiting_SPC);
209
210 AShooterGameMode_Logout_original(_this, Exiting);
211 }
212} // namespace ArkApi
#define DECLARE_HOOK(name, returnType,...)
Definition Base.h:690
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 CheckOnTimerCallbacks()
Definition Commands.cpp:103
void CheckOnTickCallbacks(float delta_seconds)
Definition Commands.cpp:91
virtual UWorld * GetWorld() const =0
Returns a pointer to UWorld.
Definition Logger.h:9
static std::shared_ptr< spdlog::logger > & GetLog()
Definition Logger.h:22
Definition IBaseApi.h:9
void Hook_AShooterGameMode_BeginPlay(AShooterGameMode *_AShooterGameMode)
void Hook_APlayerController_ServerReceivedPlayerControllerAck_Implementation(APlayerController *_this)
void Hook_AShooterPlayerController_Possess(AShooterPlayerController *_this, APawn *inPawn)
IApiUtils & GetApiUtils()
Definition ApiUtils.cpp:99
FString * Hook_AShooterPlayerController_ConsoleCommand(AShooterPlayerController *_this, FString *result, FString *Command, bool bWriteToLog)
void Hook_AShooterPlayerController_ServerSendChatMessage_Impl(AShooterPlayerController *player_controller, FString *message, EChatSendMode::Type mode)
void Hook_AShooterGameMode_InitGame(AShooterGameMode *a_shooter_game_mode, FString *map_name, FString *options, FString *error_message)
Definition HooksImpl.cpp:96
void Hook_UEngine_Init(DWORD64 _this, DWORD64 InEngineLoop)
Definition HooksImpl.cpp:64
FString * Hook_APlayerController_ConsoleCommand(APlayerController *a_player_controller, FString *result, FString *cmd, bool write_to_log)
void Hook_UWorld_Tick(DWORD64 world, DWORD64 tick_type, float delta_seconds)
Definition HooksImpl.cpp:85
void Hook_RCONClientConnection_ProcessRCONPacket(RCONClientConnection *_this, RCONPacket *packet, UWorld *in_world)
void Hook_AGameState_DefaultTimer(AGameState *_this)
void InitHooks()
Definition HooksImpl.cpp:31
bool Hook_URCONServer_Init(URCONServer *_this, FString Password, int InPort, UShooterCheatManager *SCheatManager)
void Hook_AShooterGameMode_Logout(AShooterGameMode *_this, AController *Exiting)
void Hook_UWorld_InitWorld(UWorld *world, DWORD64 ivs)
Definition HooksImpl.cpp:76
FString * ConsoleCommand(FString *result, FString *Cmd, bool bWriteToLog)
Definition Actor.h:2246
long double & LastChatMessageTimeField()
Definition Actor.h:2521
bool & IsAuthenticatedField()
Definition Other.h:110
long double & TimeSecondsField()
Definition GameMode.h:452