Ark Server API (ASE) - Wiki
Loading...
Searching...
No Matches
ArkBaseApi.cpp
Go to the documentation of this file.
1#include "ArkBaseApi.h"
2
3#include <filesystem>
4
5#include <Tools.h>
6
7#include "API/UE/Math/ColorList.h"
8#include "../Offsets.h"
9#include "../PDBReader/PDBReader.h"
10#include "../PluginManager/PluginManager.h"
11#include "../Hooks.h"
12#include "../Commands.h"
13#include "Logger/Logger.h"
14#include "HooksImpl.h"
15#include "ApiUtils.h"
16
17namespace API
18{
19 constexpr float api_version = 3.56f;
20
22 : commands_(std::make_unique<ArkApi::Commands>()),
23 hooks_(std::make_unique<Hooks>()),
24 api_utils_(std::make_unique<ArkApi::ApiUtils>())
25 {
26 }
27
29 {
30 Log::GetLog()->info("-----------------------------------------------");
31 Log::GetLog()->info("ARK: Server Api V{:.2f}", GetVersion());
32 Log::GetLog()->info("Brought to you by ArkServerApi");
33 Log::GetLog()->info("https://github.com/orgs/ArkServerApi");
34 Log::GetLog()->info("Loading...\n");
35
36 PdbReader pdb_reader;
37
38 std::unordered_map<std::string, intptr_t> offsets_dump;
39 std::unordered_map<std::string, BitField> bitfields_dump;
40
41 try
42 {
43 const std::string current_dir = Tools::GetCurrentDir();
44
45 const std::wstring dir = Tools::Utf8Decode(current_dir);
46 pdb_reader.Read(dir + L"/ShooterGameServer.pdb", &offsets_dump, &bitfields_dump);
47 }
48 catch (const std::exception& error)
49 {
50 Log::GetLog()->critical("Failed to read pdb - {}", error.what());
51 return false;
52 }
53
54 Offsets::Get().Init(move(offsets_dump), move(bitfields_dump));
55
57
58 Log::GetLog()->info("API was successfully loaded");
59 Log::GetLog()->info("-----------------------------------------------\n");
60
61 return true;
62 }
63
65 {
66 return api_version;
67 }
68
70 {
71 return "ArkApi";
72 }
73
75 {
76 return commands_;
77 }
78
80 {
81 return hooks_;
82 }
83
85 {
86 return api_utils_;
87 }
88
90 {
95 }
96
98 {
99 TArray<FString> parsed;
100 cmd->ParseIntoArray(parsed, L" ", true);
101
102 if (parsed.IsValidIndex(1))
103 {
104 const std::string plugin_name = parsed[1].ToString();
105
106 try
107 {
109 }
110 catch (const std::exception& error)
111 {
112 Log::GetLog()->warn("({}) {}", __FUNCTION__, error.what());
113 return FString::Format("Failed to load plugin - {}", error.what());
114 }
115
116 Log::GetLog()->info("Loaded plugin - {}", plugin_name.c_str());
117
118 return "Successfully loaded plugin";
119 }
120
121 return "Plugin not found";
122 }
123
125 {
126 TArray<FString> parsed;
127 cmd->ParseIntoArray(parsed, L" ", true);
128
129 if (parsed.IsValidIndex(1))
130 {
131 const std::string plugin_name = parsed[1].ToString();
132
133 try
134 {
136 }
137 catch (const std::exception& error)
138 {
139 Log::GetLog()->warn("({}) {}", __FUNCTION__, error.what());
140 return *FString::Format("Failed to unload plugin - {}", error.what());
141 }
142
143 Log::GetLog()->info("Unloaded plugin - {}", plugin_name.c_str());
144
145 return L"Successfully unloaded plugin";
146 }
147
148 return L"Plugin not found";
149 }
150
151 // Command Callbacks
152 void ArkBaseApi::LoadPluginCmd(APlayerController* player_controller, FString* cmd, bool /*unused*/)
153 {
154 auto* shooter_controller = static_cast<AShooterPlayerController*>(player_controller);
155 ArkApi::GetApiUtils().SendServerMessage(shooter_controller, FColorList::Green, *LoadPlugin(cmd));
156 }
157
158 void ArkBaseApi::UnloadPluginCmd(APlayerController* player_controller, FString* cmd, bool /*unused*/)
159 {
160 auto* shooter_controller = static_cast<AShooterPlayerController*>(player_controller);
161 ArkApi::GetApiUtils().SendServerMessage(shooter_controller, FColorList::Green, *UnloadPlugin(cmd));
162 }
163
164 // RCON Command Callbacks
165 void ArkBaseApi::LoadPluginRcon(RCONClientConnection* rcon_connection, RCONPacket* rcon_packet, UWorld* /*unused*/)
166 {
167 FString reply = LoadPlugin(&rcon_packet->Body);
168 rcon_connection->SendMessageW(rcon_packet->Id, 0, &reply);
169 }
170
171 void ArkBaseApi::UnloadPluginRcon(RCONClientConnection* rcon_connection, RCONPacket* rcon_packet,
172 UWorld* /*unused*/)
173 {
174 FString reply = UnloadPlugin(&rcon_packet->Body);
175 rcon_connection->SendMessageW(rcon_packet->Id, 0, &reply);
176 }
177} // namespace API
std::string GetApiName() override
std::unique_ptr< ArkApi::IApiUtils > & GetApiUtils() override
static void LoadPluginCmd(APlayerController *, FString *, bool)
std::unique_ptr< ArkApi::IHooks > & GetHooks() override
std::unique_ptr< ArkApi::IApiUtils > api_utils_
Definition ArkBaseApi.h:39
void RegisterCommands() override
float GetVersion() override
std::unique_ptr< ArkApi::ICommands > & GetCommands() override
static FString LoadPlugin(FString *cmd)
static void UnloadPluginRcon(RCONClientConnection *, RCONPacket *, UWorld *)
std::unique_ptr< ArkApi::ICommands > commands_
Definition ArkBaseApi.h:37
bool Init() override
static void UnloadPluginCmd(APlayerController *, FString *, bool)
static void LoadPluginRcon(RCONClientConnection *, RCONPacket *, UWorld *)
std::unique_ptr< ArkApi::IHooks > hooks_
Definition ArkBaseApi.h:38
static FString UnloadPlugin(FString *cmd)
static PluginManager & Get()
std::shared_ptr< Plugin > & LoadPlugin(const std::string &plugin_name) noexcept(false)
Load plugin by it's name.
void UnloadPlugin(const std::string &plugin_name) noexcept(false)
Unload plugin by it's name. Plugin must free all used resources.
virtual void AddRconCommand(const FString &command, const std::function< void(RCONClientConnection *, RCONPacket *, UWorld *)> &callback)=0
Adds a rcon command.
virtual void AddConsoleCommand(const FString &command, const std::function< void(APlayerController *, FString *, bool)> &callback)=0
Adds a console command.
static const FColor Green
Definition ColorList.h:13
FORCEINLINE const TCHAR * operator*() const
Definition FString.h:282
Definition IBaseApi.h:9
constexpr float api_version
ARK_API std::string GetCurrentDir()
Definition Tools.cpp:7
ARK_API std::wstring Utf8Decode(const std::string &str)
Converts an UTF8 string to a wide Unicode String.
Definition Tools.cpp:56
IApiUtils & GetApiUtils()
Definition ApiUtils.cpp:99
void InitHooks()
Definition HooksImpl.cpp:31
Definition json.hpp:4518
void SendMessageW(int Id, int Type, FString *OutGoingMessage)
Definition Other.h:122
int Id
Definition Other.h:129
FString Body
Definition Other.h:131