-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9526daf
commit ffd5f86
Showing
5 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "CommandManager.h" | ||
|
||
#include <stdexcept> | ||
|
||
#include "Hook.h" | ||
|
||
CommandManager::CommandManager() : m_commands() | ||
{ | ||
m_log = Hook::GetInstance().GetModule<Log>(); | ||
} | ||
|
||
void CommandManager::Register(const std::string& name, THandler handler) | ||
{ | ||
if (m_commands.find(name) != m_commands.end()) | ||
{ | ||
throw std::invalid_argument("A command with the same name has already been registered"); | ||
} | ||
|
||
m_commands.insert({ name, handler }); | ||
} | ||
|
||
void CommandManager::Execute(const std::string& str) | ||
{ | ||
// Get the command name from the string | ||
auto pos = str.find(" "); | ||
auto name = pos == std::string::npos ? str : str.substr(0, pos); | ||
|
||
if (m_commands.find(name) == m_commands.end()) | ||
{ | ||
m_log->WriteLine("No such command"); | ||
return; | ||
} | ||
|
||
auto command = m_commands.find(name); | ||
|
||
// Execute the command | ||
command->second(pos == std::string::npos ? "" : str.substr(pos + 1)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <map> | ||
#include <functional> | ||
#include <memory> | ||
|
||
#include "modules/Module.h" | ||
#include "modules/Log.h" | ||
|
||
class CommandManager : public Module | ||
{ | ||
private: | ||
using THandler = std::function<void(const std::string&)>; | ||
|
||
std::unordered_map<std::string, THandler> m_commands; | ||
std::shared_ptr<Log> m_log; | ||
|
||
public: | ||
CommandManager(); | ||
|
||
// Registers a command with a handler | ||
void Register(const std::string& name, THandler handler); | ||
|
||
// Executes a command string | ||
void Execute(const std::string& str); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters