Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
ApiUtils.cpp
Go to the documentation of this file.
1#include "ApiUtils.h"
2
3#include "../IBaseApi.h"
4#include "Ark/MessagingManager.h"
5#include "Ark/AsaApiUtilsMessagingManager.h"
6#include <fstream>
7#include "json.hpp"
8
9namespace AsaApi
10{
11 // UWorld
12
13 void ApiUtils::SetWorld(UWorld* uworld)
14 {
15 u_world_ = uworld;
16
17 std::shared_ptr<MessagingManager> manager = ReadApiMessagingManager();
18 manager->SetWorldContext(uworld);
19 SetMessagingManagerInternal("Default", manager);
20 }
21
23 {
24 return u_world_;
25 }
26
27 // AShooterGameMode
28
30 {
31 shooter_game_mode_ = shooter_game_mode;
32 }
33
35 {
36 return shooter_game_mode_;
37 }
38
39 // Status
40
42 {
43 status_ = status;
44 }
45
47 {
48 return status_;
49 }
50
51 // Cheat Manager
53 {
54 cheatmanager_ = cheatmanager;
55 }
56
58 {
59 if (!player_controller)
60 return;
61
62 const FString eos_id = AsaApi::IApiUtils::GetEOSIDFromController(player_controller);
63
64 if (!eos_id.IsEmpty())
65 {
66 eos_id_map_[eos_id] = player_controller;
67 }
68 }
69
71 {
72 if (!player_controller)
73 return;
74
75 const FString eos_id = AsaApi::IApiUtils::GetEOSIDFromController(player_controller);
76
77 if (!eos_id.IsEmpty())
78 {
79 eos_id_map_.erase(eos_id);
80 }
81 }
82
84 {
85 AShooterPlayerController* found_player = nullptr;
86 if (eos_id.IsEmpty())
87 {
88 return found_player;
89 }
90 auto iter = eos_id_map_.find(eos_id);
91 if (iter != eos_id_map_.end()
92 && iter->first == eos_id)
93 {
94 found_player = iter->second;
95 }
96
97 if (found_player == nullptr)
98 {
99 const auto& player_controllers = GetWorld()->PlayerControllerListField();
100 for (TWeakObjectPtr<APlayerController> player_controller : player_controllers)
101 {
102 AShooterPlayerController* shooter_pc = static_cast<AShooterPlayerController*>(player_controller.Get());
103
104 if (shooter_pc)
105 {
106 const FString& iter_eos_id = *API::game_api->GetApiUtils()->GetEOSIDFromController(shooter_pc);
107 if (iter_eos_id == eos_id)
108 {
109 found_player = shooter_pc;
110 break;
111 }
112 }
113 }
114 }
115
116 return found_player;
117 }
118
120 {
121 return cheatmanager_;
122 }
123
125 {
126 auto iter = messaging_managers_.find(forPlugin);
127 if (forPlugin.IsEmpty()
128 || iter == messaging_managers_.end())
129 {
130 return messaging_managers_.find("Default")->second;
131 }
132 return iter->second;
133 }
134
135 void ApiUtils::SetMessagingManagerInternal(const FString& forPlugin, std::shared_ptr<MessagingManager> manager)
136 {
137 if (manager == nullptr)
138 throw std::invalid_argument("MessagingManager cannot be null");
139
140 messaging_managers_[forPlugin] = manager;
141 manager->SetWorldContext(u_world_);
142
145 }
146
148 {
149 if (forPlugin.IsEmpty())
150 return;
151
152 messaging_managers_.erase(forPlugin);
153 }
154
156 {
157 for (auto it = messaging_managers_.begin(); it != messaging_managers_.end(); /*manual increm*/)
158 {
159 auto& manager = it->second;
160 if (!manager)
161 {
162 it = messaging_managers_.erase(it);
163 continue;
164 }
165 else
166 {
167 std::optional<std::string> error = manager->MeetsRequirementsToWork();
168 if (error.has_value())
169 {
170 TArray<FString> parsedName;
171 it->first.ParseIntoArray(parsedName, L"\\", true);
172 Log::GetLog()->error("Custom messaging error for '{}', using normal messaging. Reason: {}", parsedName.Last().ToString(), error.value());
173 it = messaging_managers_.erase(it);
174 continue;
175 }
176 else
177 {
178 manager->SetWorldContext(u_world_);
179 ++it;
180 }
181 }
182 }
183 }
184
186 {
187 const std::string config_path = AsaApi::Tools::GetCurrentDir() + "/config.json";
188 std::ifstream file{ config_path };
189 if (!file.is_open())
190 return std::make_shared<MessagingManager>();
191
192 nlohmann::json config;
193 file >> config;
194 file.close();
195
196 std::string messaging_manager_name = config["settings"].value("DefaultMessaging", "Default");
197 if (messaging_manager_name == "Default")
198 return std::make_shared<MessagingManager>();
199 else if (messaging_manager_name == "AsaApiUtilsMod")
200 return std::make_shared<AsaApiUtilsMessagingManager>();
201 else
202 return std::make_shared<MessagingManager>(); // fall back to default
203 }
204
205 // Free function
207 {
208 return *API::game_api->GetApiUtils();
209 }
210} // namespace AsaApi
void SetStatus(ServerStatus status)
Definition ApiUtils.cpp:41
std::shared_ptr< MessagingManager > GetMessagingManagerInternal(const FString &forPlugin) const override
Definition ApiUtils.cpp:124
void SetCheatManager(UShooterCheatManager *cheatmanager)
Definition ApiUtils.cpp:52
std::shared_ptr< MessagingManager > ReadApiMessagingManager()
Definition ApiUtils.cpp:185
UShooterCheatManager * GetCheatManager() const override
Returns a point to URCON CheatManager.
Definition ApiUtils.cpp:119
void CheckMessagingManagersRequirements()
Definition ApiUtils.cpp:155
AShooterPlayerController * FindPlayerFromEOSID_Internal(const FString &eos_id) const override
Definition ApiUtils.cpp:83
AShooterGameMode * GetShooterGameMode() const override
Returns a pointer to AShooterGameMode.
Definition ApiUtils.cpp:34
void SetShooterGameMode(AShooterGameMode *shooter_game_mode)
Definition ApiUtils.cpp:29
void RemoveMessagingManagerInternal(const FString &forPlugin)
Definition ApiUtils.cpp:147
AShooterGameMode * shooter_game_mode_
Definition ApiUtils.h:43
UWorld * GetWorld() const override
Returns a pointer to UWorld.
Definition ApiUtils.cpp:22
void SetMessagingManagerInternal(const FString &forPlugin, std::shared_ptr< MessagingManager > manager) override
Definition ApiUtils.cpp:135
ServerStatus GetStatus() const override
Returns the current server status.
Definition ApiUtils.cpp:46
void SetPlayerController(AShooterPlayerController *player_controller)
Definition ApiUtils.cpp:57
UWorld * u_world_
Definition ApiUtils.h:42
void RemovePlayerController(AShooterPlayerController *player_controller)
Definition ApiUtils.cpp:70
void SetWorld(UWorld *uworld)
Definition ApiUtils.cpp:13
UShooterCheatManager * cheatmanager_
Definition ApiUtils.h:45
static FORCEINLINE FString GetEOSIDFromController(AController *controller)
Returns EOS ID from player controller.
UE_NODISCARD FORCEINLINE bool IsEmpty() const
Messaging manager. Allows to send server messages, notifications and chat messages.
IApiUtils & GetApiUtils()
Definition ApiUtils.cpp:206
Definition json.hpp:4518
TArray< TWeakObjectPtr< APlayerController >, TSizedDefaultAllocator< 32 > > & PlayerControllerListField()
Definition GameMode.h:516