Ark Server API (ASE) - Wiki
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
72 {
73 return commands_;
74 }
75
77 {
78 return hooks_;
79 }
80
82 {
83 return api_utils_;
84 }
85
87 {
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 {
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 {
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);
152 ArkApi::GetApiUtils().SendServerMessage(shooter_controller, FColorList::Green, *LoadPlugin(cmd));
153 }
154
155 void AtlasBaseApi::UnloadPluginCmd(APlayerController* player_controller, FString* cmd, bool /*unused*/)
156 {
157 auto* shooter_controller = static_cast<AShooterPlayerController*>(player_controller);
158 ArkApi::GetApiUtils().SendServerMessage(shooter_controller, FColorList::Green, *UnloadPlugin(cmd));
159 }
160
161 // RCON Command Callbacks
162 void AtlasBaseApi::LoadPluginRcon(RCONClientConnection* rcon_connection, RCONPacket* rcon_packet,
163 UWorld* /*unused*/)
164 {
165 FString reply = LoadPlugin(&rcon_packet->Body);
166 rcon_connection->SendMessageW(rcon_packet->Id, 0, &reply);
167 }
168
169 void AtlasBaseApi::UnloadPluginRcon(RCONClientConnection* rcon_connection, RCONPacket* rcon_packet,
170 UWorld* /*unused*/)
171 {
172 FString reply = UnloadPlugin(&rcon_packet->Body);
173 rcon_connection->SendMessageW(rcon_packet->Id, 0, &reply);
174 }
175} // namespace API
static void UnloadPluginCmd(APlayerController *, FString *, bool)
std::unique_ptr< ArkApi::ICommands > commands_
static FString LoadPlugin(FString *cmd)
std::unique_ptr< ArkApi::IApiUtils > api_utils_
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_
static void LoadPluginCmd(APlayerController *, FString *, bool)
static FString UnloadPlugin(FString *cmd)
float GetVersion() override
static void UnloadPluginRcon(RCONClientConnection *, RCONPacket *, UWorld *)
std::string GetApiName() override
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
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:28
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