Ark Server API 3.54
Serverside plugin support for Ark Survival Evolved.
Loading...
Searching...
No Matches
AtlasBaseApi.cpp
Go to the documentation of this file.
1#include "AtlasBaseApi.h"
2
3#include <filesystem>
4
5#include <Tools.h>
6
7#include "../Offsets.h"
8#include "../PDBReader/PDBReader.h"
9#include "../PluginManager/PluginManager.h"
10#include "../Hooks.h"
11#include "../Commands.h"
12#include "Logger/Logger.h"
13#include "HooksImpl.h"
14#include "ApiUtils.h"
15
16namespace API
17{
18 constexpr float api_version = 1.7f;
19
21 : commands_(std::make_unique<ArkApi::Commands>()),
22 hooks_(std::make_unique<Hooks>()),
23 api_utils_(std::make_unique<AtlasApi::ApiUtils>())
24 {
25 }
26
28 {
29 Log::GetLog()->info("-----------------------------------------------");
30 Log::GetLog()->info("YAPI V{:.1f}", GetVersion());
31 Log::GetLog()->info("Loading...\n");
32
33 PdbReader pdb_reader;
34
35 std::unordered_map<std::string, intptr_t> offsets_dump;
36 std::unordered_map<std::string, BitField> bitfields_dump;
37
38 try
39 {
40 const std::string current_dir = Tools::GetCurrentDir();
41
42 const std::wstring dir = Tools::Utf8Decode(current_dir);
43 pdb_reader.Read(dir + L"/ShooterGameServer.pdb", &offsets_dump, &bitfields_dump);
44 }
45 catch (const std::exception& error)
46 {
47 Log::GetLog()->critical("Failed to read pdb - {}", error.what());
48 return false;
49 }
50
51 Offsets::Get().Init(move(offsets_dump), move(bitfields_dump));
52
54
55 Log::GetLog()->info("API was successfully loaded");
56 Log::GetLog()->info("-----------------------------------------------\n");
57
58 return true;
59 }
60
62 {
63 return api_version;
64 }
65
67 {
68 return "AtlasApi";
69 }
70
71 std::unique_ptr<ArkApi::ICommands>& AtlasBaseApi::GetCommands()
72 {
73 return commands_;
74 }
75
76 std::unique_ptr<ArkApi::IHooks>& AtlasBaseApi::GetHooks()
77 {
78 return hooks_;
79 }
80
81 std::unique_ptr<ArkApi::IApiUtils>& AtlasBaseApi::GetApiUtils()
82 {
83 return api_utils_;
84 }
85
87 {
88 GetCommands()->AddConsoleCommand("plugins.load", &LoadPluginCmd);
89 GetCommands()->AddConsoleCommand("plugins.unload", &UnloadPluginCmd);
90 GetCommands()->AddRconCommand("plugins.load", &LoadPluginRcon);
91 GetCommands()->AddRconCommand("plugins.unload", &UnloadPluginRcon);
92 }
93
95 {
96 TArray<FString> parsed;
97 cmd->ParseIntoArray(parsed, L" ", true);
98
99 if (parsed.IsValidIndex(1))
100 {
101 const std::string plugin_name = parsed[1].ToString();
102
103 try
104 {
105 PluginManager::Get().LoadPlugin(plugin_name);
106 }
107 catch (const std::exception& error)
108 {
109 Log::GetLog()->warn("({}) {}", __FUNCTION__, error.what());
110 return FString::Format("Failed to load plugin - {}", error.what());
111 }
112
113 Log::GetLog()->info("Loaded plugin - {}", plugin_name.c_str());
114
115 return "Successfully loaded plugin";
116 }
117
118 return "Plugin not found";
119 }
120
122 {
123 TArray<FString> parsed;
124 cmd->ParseIntoArray(parsed, L" ", true);
125
126 if (parsed.IsValidIndex(1))
127 {
128 const std::string plugin_name = parsed[1].ToString();
129
130 try
131 {
132 PluginManager::Get().UnloadPlugin(plugin_name);
133 }
134 catch (const std::exception& error)
135 {
136 Log::GetLog()->warn("({}) {}", __FUNCTION__, error.what());
137 return *FString::Format("Failed to unload plugin - {}", error.what());
138 }
139
140 Log::GetLog()->info("Unloaded plugin - {}", plugin_name.c_str());
141
142 return L"Successfully unloaded plugin";
143 }
144
145 return L"Plugin not found";
146 }
147
148 // Command Callbacks
149 void AtlasBaseApi::LoadPluginCmd(APlayerController* player_controller, FString* cmd, bool /*unused*/)
150 {
151 auto* shooter_controller = static_cast<AShooterPlayerController*>(player_controller);
153 }
154
155 void AtlasBaseApi::UnloadPluginCmd(APlayerController* player_controller, FString* cmd, bool /*unused*/)
156 {
157 auto* shooter_controller = static_cast<AShooterPlayerController*>(player_controller);
159 }
160
161 // RCON Command Callbacks
163 UWorld* /*unused*/)
164 {
165 FString reply = LoadPlugin(&rcon_packet->Body);
166 rcon_connection->SendMessageW(rcon_packet->Id, 0, &reply);
167 }
168
170 UWorld* /*unused*/)
171 {
172 FString reply = UnloadPlugin(&rcon_packet->Body);
173 rcon_connection->SendMessageW(rcon_packet->Id, 0, &reply);
174 }
175} // namespace API
int Id
Definition: Other.h:181
FString Body
Definition: Other.h:183
static void UnloadPluginCmd(APlayerController *, FString *, bool)
std::unique_ptr< ArkApi::ICommands > commands_
Definition: AtlasBaseApi.h:37
static FString LoadPlugin(FString *cmd)
std::unique_ptr< ArkApi::IApiUtils > api_utils_
Definition: AtlasBaseApi.h:39
std::unique_ptr< ArkApi::ICommands > & GetCommands() override
std::unique_ptr< ArkApi::IHooks > & GetHooks() override
static void LoadPluginRcon(RCONClientConnection *, RCONPacket *, UWorld *)
void RegisterCommands() override
std::unique_ptr< ArkApi::IApiUtils > & GetApiUtils() override
bool Init() override
std::unique_ptr< ArkApi::IHooks > hooks_
Definition: AtlasBaseApi.h:38
static void LoadPluginCmd(APlayerController *, FString *, bool)
static FString UnloadPlugin(FString *cmd)
float GetVersion() override
static void UnloadPluginRcon(RCONClientConnection *, RCONPacket *, UWorld *)
std::string GetApiName() override
void Init(std::unordered_map< std::string, intptr_t > &&offsets_dump, std::unordered_map< std::string, BitField > &&bitfields_dump)
Definition: Offsets.cpp:43
static Offsets & Get()
Definition: Offsets.cpp:37
void Read(const std::wstring &path, std::unordered_map< std::string, intptr_t > *offsets_dump, std::unordered_map< std::string, BitField > *bitfields_dump)
Definition: PDBReader.cpp:44
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.
FORCEINLINE void SendServerMessage(AShooterPlayerController *player_controller, FLinearColor msg_color, const T *msg, Args &&... args)
Sends server message to the specific player. Using fmt::format.
Definition: ArkApiUtils.h:52
static const FColor Green
Definition: ColorList.h:13
int32 ParseIntoArray(TArray< FString > &OutArray, const TCHAR *pchDelim, bool InCullEmpty=true) const
Definition: FString.h:2560
static FString Format(const T *format, Args &&... args)
Formats text using fmt::format.
Definition: FString.h:1633
static std::shared_ptr< spdlog::logger > & GetLog()
Definition: Logger.h:22
Definition: TArray.h:268
FORCEINLINE bool IsValidIndex(int32 Index) const
Definition: TArray.h:600
constexpr float api_version
Definition: ArkBaseApi.cpp:19
std::string GetCurrentDir()
Definition: Tools.cpp:7
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:28
STL namespace.
void SendMessageW(int Id, int Type, FString *OutGoingMessage)
Definition: Other.h:174