Skip to content

Commit

Permalink
Initial command handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Sep 21, 2024
1 parent 9526daf commit ffd5f86
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "render/Font.h"
#include "game/GameLoop.h"
#include "render/DrawBatcher.h"
#include "console/CommandManager.h"

// Modules
#include "modules/MainMenu.h"
Expand Down Expand Up @@ -128,6 +129,7 @@ void Hook::RegisterModules()
{
// Register these first
RegisterModule<Log>();
RegisterModule<CommandManager>();
RegisterModule<Options>();

RegisterModule<MainMenu>();
Expand Down
38 changes: 38 additions & 0 deletions src/console/CommandManager.cpp
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));
}
27 changes: 27 additions & 0 deletions src/console/CommandManager.h
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);
};
12 changes: 12 additions & 0 deletions src/modules/Log.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <imgui.h>

#include "Log.h"
#include "Hook.h"
#include "console/CommandManager.h"

void Log::OnMenu()
{
Expand Down Expand Up @@ -33,6 +35,16 @@ void Log::OnDraw()
}

ImGui::EndChild();

// Command input
if (ImGui::InputText("##Command", m_commandBuffer, sizeof(m_commandBuffer), ImGuiInputTextFlags_EnterReturnsTrue))
{
// Execute the command
Hook::GetInstance().GetModule<CommandManager>()->Execute(m_commandBuffer);

strcpy_s(m_commandBuffer, "");
}

ImGui::End();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/modules/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Log : public Module
private:
bool m_show = true;
ImGuiTextBuffer m_buffer;
char m_commandBuffer[256] = "";

public:
void OnMenu();
Expand Down

0 comments on commit ffd5f86

Please sign in to comment.