Skip to content

Commit

Permalink
Initial Implementation of External Command Processor
Browse files Browse the repository at this point in the history
  • Loading branch information
MrNen committed Aug 13, 2023
1 parent 1dc40a7 commit bb2cc7d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 3 deletions.
21 changes: 21 additions & 0 deletions include/Core/Commands/ExternalCommandProcessor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

class ExternalCommandProcessor
{
// clang-format off
std::unordered_map<std::wstring, std::function<nlohmann::json(const nlohmann::json&)>> functions =
{
{
std::wstring(L"beam"), Beam
},
{

}
};
// clang-format on

static nlohmann::json Beam(const nlohmann::json& parameters);

public:
std::optional<nlohmann::json> ProcessCommand(const nlohmann::json& command);
};
2 changes: 2 additions & 0 deletions include/Core/MessageHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ class MessageHandler final : public AMQP::ConnectionHandler, public Singleton<Me
enum class Queue
{
ServerStats,
ExternalCommands,
};

static std::wstring_view QueueToStr(const Queue queue) { return magic_enum::enum_name<Queue>(queue); }
void Subscribe(const std::wstring& queue, QueueOnData callback, std::optional<QueueOnFail> onFail = std::nullopt);
void Publish(const std::wstring& jsonData, const std::wstring& exchange = L"", const std::wstring& queue = L"") const;
void DeclareQueue(const std::wstring& queue, int flags = 0) const;
void DeclareExchange(const std::wstring& exchange, AMQP::ExchangeType type = AMQP::fanout, int flags = 0) const;
void RemoteProcedureCall(nlohmann::json call);
};
3 changes: 0 additions & 3 deletions include/PCH.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
#include <vector>
#include <random>


#include "Core/Templates/Constexpr.hpp"
#include "Core/Templates/Typedefs.hpp"

#define MAGIC_ENUM_USING_ALIAS_STRING_VIEW using string_view = std::wstring_view;
#define MAGIC_ENUM_USING_ALIAS_STRING using string = std::wstring;
Expand Down Expand Up @@ -68,7 +66,6 @@
#include "Core/Codec.hpp"

#include "Core/Action.hpp"
#include "Core/MessageHandler.hpp"
#include "Core/TempBan.hpp"
#include "Defs/ServerStats.hpp"

Expand Down
2 changes: 2 additions & 0 deletions project/FLHook.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<ClCompile Include="..\Source\Core\Codec.cpp" />
<ClCompile Include="..\Source\Core\Commands\AdminCommandProcessor.cpp" />
<ClCompile Include="..\Source\Core\Commands\CommandLineParser.cpp" />
<ClCompile Include="..\Source\Core\Commands\ExternalCommandProcessor.cpp" />
<ClCompile Include="..\Source\Core\Commands\UserCommandProcessor.cpp" />
<ClCompile Include="..\Source\Core\Debug.cpp" />
<ClCompile Include="..\Source\Core\FLPacket.cpp" />
Expand Down Expand Up @@ -104,6 +105,7 @@
<ClInclude Include="..\Include\Core\Commands\AbstractUserCommandProcessor.hpp" />
<ClInclude Include="..\Include\Core\Commands\AdminCommandProcessor.hpp" />
<ClInclude Include="..\Include\Core\Commands\CommandLineParser.hpp" />
<ClInclude Include="..\Include\Core\Commands\ExternalCommandProcessor.hpp" />
<ClInclude Include="..\Include\Core\Commands\UserCommandProcessor.hpp" />
<ClInclude Include="..\Include\Core\Detour.hpp" />
<ClInclude Include="..\Include\Core\Exceptions\ErrorInfo.hpp" />
Expand Down
6 changes: 6 additions & 0 deletions project/FLHook.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@
<ClCompile Include="..\Source\Hooks\ClientServerInterface\HookEntry.cpp">
<Filter>Source\Hooks\ClientServerInterface</Filter>
</ClCompile>
<ClCompile Include="..\Source\Core\Commands\ExternalCommandProcessor.cpp">
<Filter>Source\Core\Commands</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\Include\Core\Commands\AbstractAdminCommandProcessor.hpp">
Expand Down Expand Up @@ -405,5 +408,8 @@
<ClInclude Include="..\Include\API\API.hpp">
<Filter>Include\API</Filter>
</ClInclude>
<ClInclude Include="..\Include\Core\Commands\ExternalCommandProcessor.hpp">
<Filter>Include\Core\Commands</Filter>
</ClInclude>
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions source/API/FLHook/MessageHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,7 @@ void MessageHandler::Publish(const std::wstring& jsonData, const std::wstring& e
channel->publish(StringUtils::wstos(exchange), StringUtils::wstos(queue), StringUtils::wstos(jsonData));
}

void MessageHandler::RemoteProcedureCall(nlohmann::json call)
{

}
28 changes: 28 additions & 0 deletions source/Core/Commands/ExternalCommandProcessor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "PCH.hpp"
#include "Core/Commands/ExternalCommandProcessor.hpp"
#include "API/API.hpp"



std::optional<nlohmann::json> ExternalCommandProcessor::ProcessCommand(const nlohmann::json& command)
{
try
{
std::wstring functionName = command.at("command").get<std::wstring>();

return functions[functionName](command);
}
catch (nlohmann::json::exception& ex)
{
Logger::i()->Log(LogLevel::Warn, std::format(L"Exception was thrown while trying to process an external command. {}",StringUtils::stows(ex.what())));
}

}

nlohmann::json ExternalCommandProcessor::Beam(const nlohmann::json& parameters)
{
const auto client = Hk::Client::GetClientIdFromCharName(parameters.at("characterName").get<std::wstring>()).Handle();
Hk::Player::Beam(client, parameters.at("baseName").get<std::wstring>());

return {};
}
1 change: 1 addition & 0 deletions source/Core/Timers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Global.hpp"

#include "API/API.hpp"
#include "Core/MessageHandler.hpp"

PerfTimer::PerfTimer(const std::wstring& func, uint warn) : function(func), warning(warn) {}

Expand Down

0 comments on commit bb2cc7d

Please sign in to comment.