Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Commands.h
Go to the documentation of this file.
1#pragma once
2
3#include <ICommands.h>
4
5#include <algorithm>
6#include <memory>
7#include <utility>
8#include <vector>
9
10namespace AsaApi
11{
12 class Commands : public ICommands
13 {
14 public:
15 Commands() = default;
16
17 Commands(const Commands&) = delete;
18 Commands(Commands&&) = delete;
19 Commands& operator=(const Commands&) = delete;
20 Commands& operator=(Commands&&) = delete;
21
22 ~Commands() override = default;
23
24 void AddChatCommand(const FString& command, const std::function<void(AShooterPlayerController*, FString*, int, int)>&callback) override;
25 void AddConsoleCommand(const FString& command, const std::function<void(APlayerController*, FString*, bool)>& callback) override;
26 void AddRconCommand(const FString& command, const std::function<void(RCONClientConnection*, RCONPacket*, UWorld*)>& callback) override;
27
28 void AddOnTickCallback(const FString& id, const std::function<void(float)>& callback) override;
29 void AddOnTimerCallback(const FString& id, const std::function<void()>& callback) override;
30 void AddOnChatMessageCallback(const FString& id, const std::function<bool(AShooterPlayerController*, FString*, int, int, bool, bool)>& callback) override;
31
32 bool RemoveChatCommand(const FString& command) override;
33 bool RemoveConsoleCommand(const FString& command) override;
34 bool RemoveRconCommand(const FString& command) override;
35
36 bool RemoveOnTickCallback(const FString& id) override;
37 bool RemoveOnTimerCallback(const FString& id) override;
38 bool RemoveOnChatMessageCallback(const FString& id) override;
39
40 bool CheckChatCommands(AShooterPlayerController* shooter_player_controller, FString* message, int mode, int platform);
41 bool CheckConsoleCommands(APlayerController* a_player_controller, FString* cmd, bool write_to_log);
42 bool CheckRconCommands(RCONClientConnection* rcon_client_connection, RCONPacket* rcon_packet, UWorld* u_world);
43 void CheckOnTickCallbacks(float delta_seconds);
45 bool CheckOnChatMessageCallbacks(AShooterPlayerController* player_controller, FString* message, int mode, int platform, bool spam_check, bool command_executed);
46
47 private:
48 template <typename T>
49 struct Command
50 {
51 Command(FString command, std::function<T> callback)
54 {
55 }
56
58 std::function<T> callback;
59 };
60
61 using ChatCommand = Command<void(AShooterPlayerController*, FString*, int, int)>;
62 using ConsoleCommand = Command<void(APlayerController*, FString*, bool)>;
63 using RconCommand = Command<void(RCONClientConnection*, RCONPacket*, UWorld*)>;
64
65 using OnTickCallback = Command<void(float)>;
66 using OnTimerCallback = Command<void()>;
67 using OnChatMessageCallback = Command<bool(AShooterPlayerController*, FString*, int, int, bool, bool)>;
68
69 template <typename T>
70 bool RemoveCommand(const FString& command, std::vector<std::shared_ptr<T>>& commands)
71 {
73 [&command](const std::shared_ptr<T>& data) -> bool
74 {
75 return data->command == command;
76 });
77
78 if (iter != commands.end())
79 {
81
82 return true;
83 }
84
85 return false;
86 }
87
88 template <typename T, typename... Args>
89 bool CheckCommands(const FString& message, const std::vector<std::shared_ptr<T>>& commands, Args&&... args)
90 {
92 message.ParseIntoArray(parsed, L" ", true);
93
94 if (!parsed.IsValidIndex(0))
95 {
96 return false;
97 }
98
99 const FString command_text = parsed[0];
100
101 for (const auto& command : commands)
102 {
104 {
106
107 return true;
108 }
109 }
110
111 return false;
112 }
113
117
121 };
122} // namespace AsaApi
static void SetServerID(RCONClientConnection *, RCONPacket *, UWorld *)
std::string GetApiName() override
bool DownloadCacheFiles(const std::filesystem::path downloadFile, const std::filesystem::path localFile)
std::unique_ptr< AsaApi::IApiUtils > & GetApiUtils() override
static void LoadPluginCmd(APlayerController *, FString *, bool)
std::unique_ptr< AsaApi::ICommands > commands_
Definition ArkBaseApi.h:45
void RegisterCommands() override
float GetVersion() override
~ArkBaseApi() override=default
static FString LoadPlugin(FString *cmd)
static void UnloadPluginRcon(RCONClientConnection *, RCONPacket *, UWorld *)
nlohmann::json GetConfig()
std::unique_ptr< AsaApi::IApiUtils > api_utils_
Definition ArkBaseApi.h:47
bool Init() override
static void UnloadPluginCmd(APlayerController *, FString *, bool)
static void LoadPluginRcon(RCONClientConnection *, RCONPacket *, UWorld *)
static FString UnloadPlugin(FString *cmd)
std::unique_ptr< AsaApi::ICommands > & GetCommands() override
std::unique_ptr< AsaApi::IHooks > & GetHooks() override
std::unique_ptr< AsaApi::IHooks > hooks_
Definition ArkBaseApi.h:46
Hooks(const Hooks &)=delete
Hooks & operator=(const Hooks &)=delete
std::unordered_map< std::string, std::vector< std::shared_ptr< Hook > > > all_hooks_
Definition Hooks.h:41
~Hooks() override=default
Hooks & operator=(Hooks &&)=delete
bool DisableHook(const std::string &func_name, LPVOID detour) override
Removes a hook from a function.
Definition Hooks.cpp:66
bool SetHookInternal(const std::string &func_name, LPVOID detour, LPVOID *original) override
Definition Hooks.cpp:18
Hooks(Hooks &&)=delete
std::vector< std::shared_ptr< Plugin > > loaded_plugins_
std::vector< std::shared_ptr< Plugin > >::const_iterator FindPlugin(const std::string &plugin_name)
Find plugin by it's name.
PluginManager & operator=(const PluginManager &)=delete
PluginManager()=default
PluginManager(const PluginManager &)=delete
PluginManager(PluginManager &&)=delete
void LoadAllPlugins()
Find and load all plugins.
static PluginManager & Get()
static nlohmann::json ReadPluginInfo(const std::string &plugin_name)
bool IsPluginLoaded(const std::string &plugin_name)
Returns true if plugin was loaded, false otherwise.
std::shared_ptr< Plugin > & LoadPlugin(const std::string &plugin_name) noexcept(false)
Load plugin by it's name.
static void DetectPluginChangesTimerCallback()
Checks for auto plugin reloads.
void UnloadPlugin(const std::string &plugin_name) noexcept(false)
Unload plugin by it's name. Plugin must free all used resources.
PluginManager & operator=(PluginManager &&)=delete
~PluginManager()=default
static nlohmann::json ReadSettingsConfig()
void AddRconCommand(const FString &command, const std::function< void(RCONClientConnection *, RCONPacket *, UWorld *)> &callback) override
Adds a rcon command.
Definition Commands.cpp:19
std::vector< std::shared_ptr< OnTickCallback > > on_tick_callbacks_
Definition Commands.h:118
bool CheckCommands(const FString &message, const std::vector< std::shared_ptr< T > > &commands, Args &&... args)
Definition Commands.h:89
~Commands() override=default
std::vector< std::shared_ptr< ConsoleCommand > > console_commands_
Definition Commands.h:115
Commands()=default
void CheckOnTickCallbacks(float delta_seconds)
Definition Commands.cpp:86
bool CheckRconCommands(RCONClientConnection *rcon_client_connection, RCONPacket *rcon_packet, UWorld *u_world)
Definition Commands.cpp:79
void CheckOnTimerCallbacks()
Definition Commands.cpp:98
void AddOnTimerCallback(const FString &id, const std::function< void()> &callback) override
Added function will be called every second.
Definition Commands.cpp:29
bool CheckChatCommands(AShooterPlayerController *shooter_player_controller, FString *message, int mode, int platform)
Definition Commands.cpp:69
void AddOnChatMessageCallback(const FString &id, const std::function< bool(AShooterPlayerController *, FString *, int, int, bool, bool)> &callback) override
Added function will be called for AShooterPlayerController->ServerSendChatMessage events.
Definition Commands.cpp:34
std::vector< std::shared_ptr< ChatCommand > > chat_commands_
Definition Commands.h:114
std::vector< std::shared_ptr< OnTimerCallback > > on_timer_callbacks_
Definition Commands.h:119
Commands & operator=(const Commands &)=delete
std::vector< std::shared_ptr< OnChatMessageCallback > > on_chat_message_callbacks_
Definition Commands.h:120
bool RemoveConsoleCommand(const FString &command) override
Removes a console command.
Definition Commands.cpp:44
void AddChatCommand(const FString &command, const std::function< void(AShooterPlayerController *, FString *, int, int)> &callback) override
Adds a chat command.
Definition Commands.cpp:7
Commands(Commands &&)=delete
Commands(const Commands &)=delete
Commands & operator=(Commands &&)=delete
void AddConsoleCommand(const FString &command, const std::function< void(APlayerController *, FString *, bool)> &callback) override
Adds a console command.
Definition Commands.cpp:13
bool RemoveCommand(const FString &command, std::vector< std::shared_ptr< T > > &commands)
Definition Commands.h:70
bool RemoveRconCommand(const FString &command) override
Removes a rcon command.
Definition Commands.cpp:49
bool CheckConsoleCommands(APlayerController *a_player_controller, FString *cmd, bool write_to_log)
Definition Commands.cpp:74
void AddOnTickCallback(const FString &id, const std::function< void(float)> &callback) override
Added function will be called every frame.
Definition Commands.cpp:24
bool RemoveChatCommand(const FString &command) override
Removes a chat command.
Definition Commands.cpp:39
bool RemoveOnTimerCallback(const FString &id) override
Removes an on-timer callback.
Definition Commands.cpp:59
bool CheckOnChatMessageCallbacks(AShooterPlayerController *player_controller, FString *message, int mode, int platform, bool spam_check, bool command_executed)
Definition Commands.cpp:110
std::vector< std::shared_ptr< RconCommand > > rcon_commands_
Definition Commands.h:116
bool RemoveOnChatMessageCallback(const FString &id) override
Removes an on-chat-message callback.
Definition Commands.cpp:64
bool RemoveOnTickCallback(const FString &id) override
Removes a on-tick callback.
Definition Commands.cpp:54
static const FColor Green
Definition ColorList.h:24
UE_NODISCARD FORCEINLINE const TCHAR * operator*() const UE_LIFETIMEBOUND
Definition IBaseApi.h:9
constexpr float api_version
ARK_API std::string GetCurrentDir()
Definition Tools.cpp:8
IApiUtils & GetApiUtils()
Definition ApiUtils.cpp:206
void InitHooks()
Definition HooksImpl.cpp:43
Definition json.hpp:4518
LPVOID target
Definition Hooks.h:36
LPVOID * original
Definition Hooks.h:38
Hook(LPVOID target, LPVOID detour, LPVOID *original)
Definition Hooks.h:29
LPVOID detour
Definition Hooks.h:37
std::string description
std::vector< std::string > dependencies
Plugin(HINSTANCE h_module, std::string name, std::string full_name, std::string description, float version, float min_api_version, std::vector< std::string > dependencies)
HINSTANCE h_module
float min_api_version
std::string full_name
std::string name
Command(FString command, std::function< T > callback)
Definition Commands.h:51
std::function< T > callback
Definition Commands.h:58
void SendMessageW(int Id, int Type, FString *OutGoingMessage)
Definition Other.h:38
int Id
Definition Other.h:13