From 6f7b994684032060f0a2d7af82f4f3572293b8d6 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Thu, 12 Jun 2025 21:41:41 +0800 Subject: internal!: organized internal structure, using cmake now --- src/command/commandHandler.hpp | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/command/commandHandler.hpp (limited to 'src/command/commandHandler.hpp') diff --git a/src/command/commandHandler.hpp b/src/command/commandHandler.hpp new file mode 100644 index 0000000..7fc20d5 --- /dev/null +++ b/src/command/commandHandler.hpp @@ -0,0 +1,71 @@ +#pragma once + +#include "../core/session.hpp" +#include +#include +#include +#include +#include +#include + +class CCommandHandler { + public: + CCommandHandler(); + ~CCommandHandler(); + + struct SResult { + std::string result; + bool good = false; + }; + + struct SParseResult { + std::any data; + bool good = false; + }; + + struct SCommand { + bool requireAdmin = true; + std::string name; + std::function parser; + std::function exe; + }; + + bool isCommand(const std::string &str) const; + bool validCommand(const std::string &command) const; + // return false if command struct is invalid + bool registerCommand(const SCommand &command); + SResult newCommand(const std::string &command, const std::string &args, std::shared_ptr session) const; + void handleCommand(std::string input, std::shared_ptr user = nullptr) const; + + template + struct SMakeCommandArgs { + std::string name; + std::function(const std::string &args)> parser; + std::function exe; + }; + + template + static SCommand makeCommand(SMakeCommandArgs args) { + return { + .name = std::move(args.name), + .parser = [parser = std::move(args.parser)](const std::string &args) -> SParseResult { + auto [data, good] = parser(args); + return {.data = std::any(std::move(data)), .good = good}; + }, + .exe = [exe = std::move(args.exe)](const std::any &parsed) -> SResult { + try { + return exe(std::any_cast(parsed)); + } catch (const std::bad_any_cast &) { + return {.result = "Invalid data type", .good = false}; + } + } + }; + } + + private: + std::shared_ptr getCommand(const std::string &command) const; + SResult exeCommand(const std::shared_ptr command, const std::string &args) const; + + std::vector> m_vCommands; +}; +inline std::unique_ptr g_pCommandHandler; -- cgit v1.2.3