Files
EchoionCore/include/echoion/core/commands/Command.hpp
T
faumarayandClaude Opus 4.8 a9ab0d6b75 Make the service registry a real read path for commands
The DI registry was write-only — populated in Core::initialize() but never
resolved through. Thread it into command execution so commands resolve
shared services by interface instead of constructing throwaways:

- CommandContext gains a non-owning ServiceRegistry* plus an ergonomic
  service<I>() resolver.
- CommandManager carries the registry (Core::setServices(&registry_)) and
  stamps it onto every CommandContext it builds (async + sync paths).
- RpcInfoCommand's getPublicIpAddress() now reuses the shared, initialized
  INetworkClient from the registry, falling back to a throwaway client only
  when none is registered.

Core itself (the composition root) keeps direct member wiring and commands
keep explicit MusicManager injection — converting those to a service locator
would only hide dependencies; the registry earns its keep at the genuine
seam where a command needs a service it isn't handed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-22 22:25:15 +03:00

124 lines
2.8 KiB
C++

#pragma once
#include "echoion/core/MusicManager.hpp"
#include <ConfigTypes.hpp>
#include <Types.hpp>
#include <Void/Service/Registry.hpp>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
namespace Echoion::Core
{
class MusicManager;
enum class CommandCategory { Music, Queue, Playback, Utility, Admin };
inline std::string toString(CommandCategory category)
{
switch (category) {
case CommandCategory::Music:
return "Music";
case CommandCategory::Queue:
return "Queue";
case CommandCategory::Playback:
return "Playback";
case CommandCategory::Utility:
return "Utility";
case CommandCategory::Admin:
return "Admin";
}
return "Utility";
}
/**
* @brief Context passed to every command execution.
* Wraps all dependencies required to perform an action.
*/
struct CommandContext {
uint64_t clientId = 0;
std::string prefix;
std::string args; // Arguments excluding the command name
std::string_view invokerName;
// Dependencies
std::weak_ptr<MusicManager> musicManager;
// Service registry for resolving shared services by interface
// (non-owning; outlives the command). Populated by CommandManager.
const Void::Service::ServiceRegistry* services = nullptr;
// Reply callback - set by CommandManager
std::function<void(std::string_view)> replyCallback;
// Helper to reply to the user
void reply(std::string_view message) const
{
if (replyCallback) {
replyCallback(message);
}
}
// Resolve a shared service by interface, or nullptr if the
// registry is absent / the service was not provided.
template <typename I>
[[nodiscard]] std::shared_ptr<I> service() const
{
return services ? services->get<I>() : nullptr;
}
};
/**
* @brief Interface that all commands must implement.
*/
class Command
{
public:
virtual ~Command() = default;
/**
* @brief The logic of the command.
*/
virtual void execute(const CommandContext& ctx) = 0;
/**
* @brief Returns the description used for help generation.
*/
virtual std::string_view getDescription() const = 0;
/**
* @brief Returns usage syntax (e.g., "<url>").
*/
virtual std::string_view getUsage() const { return ""; }
/**
* @brief Returns the command category for grouping in help.
*/
virtual CommandCategory getCategory() const
{
return CommandCategory::Utility;
}
/**
* @brief Returns aliases for this command.
*/
virtual std::vector<std::string_view> getAliases() const
{
return {};
}
/**
* @brief Returns whether this command requires admin privileges.
*/
virtual bool requiresAdmin() const { return false; }
};
using CommandPtr = std::shared_ptr<Command>;
} // namespace Echoion::Core