1 parent
01362d2e51
commit
39aff68d8d
29 files changed
+2061
-1818
No files matched your search
@@ -0,0 +1,400 @@
|
||||
#include "echoion/core/PluginHost.hpp"
|
||||
|
||||
#include "echoion/core/CommandManager.hpp"
|
||||
#include "echoion/core/MusicManager.hpp"
|
||||
#include "echoion/core/sources/TrackSource.hpp"
|
||||
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/Plugin/Loader.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
namespace Echoion::Core
|
||||
{
|
||||
namespace
|
||||
{
|
||||
class ExtensionCommandAdapter final : public Command
|
||||
{
|
||||
public:
|
||||
ExtensionCommandAdapter(Void::Plugin::PluginManager& plugins,
|
||||
std::string plugin, Api::ExtensionCommand command)
|
||||
: plugins_(plugins), plugin_(std::move(plugin)),
|
||||
command_(std::move(command))
|
||||
{
|
||||
}
|
||||
|
||||
void execute(const CommandContext& context) override
|
||||
{
|
||||
try {
|
||||
auto pin = plugins_.acquire(plugin_);
|
||||
auto* extension = pin
|
||||
? dynamic_cast<Api::IExtensionPlugin*>(pin.get()) : nullptr;
|
||||
if (!extension) {
|
||||
context.reply("Plugin is temporarily unavailable.");
|
||||
return;
|
||||
}
|
||||
auto result = extension->executeCommand({
|
||||
.command = command_.name,
|
||||
.arguments = context.args,
|
||||
.clientId = context.clientId,
|
||||
.invokerName = std::string(context.invokerName)});
|
||||
context.reply(result ? *result : result.error().message);
|
||||
} catch (const std::exception& error) {
|
||||
Void::Log::error("PluginHost", "Plugin '{}' command '{}' threw: {}",
|
||||
plugin_, command_.name, error.what());
|
||||
context.reply("Plugin command failed.");
|
||||
} catch (...) {
|
||||
Void::Log::error("PluginHost", "Plugin '{}' command '{}' threw",
|
||||
plugin_, command_.name);
|
||||
context.reply("Plugin command failed.");
|
||||
}
|
||||
}
|
||||
|
||||
std::string_view getDescription() const override
|
||||
{
|
||||
return command_.description;
|
||||
}
|
||||
std::string_view getUsage() const override { return command_.usage; }
|
||||
CommandCategory getCategory() const override
|
||||
{
|
||||
return static_cast<CommandCategory>(
|
||||
std::min<std::uint8_t>(command_.category,
|
||||
static_cast<std::uint8_t>(CommandCategory::Admin)));
|
||||
}
|
||||
std::vector<std::string_view> getAliases() const override
|
||||
{
|
||||
std::vector<std::string_view> aliases;
|
||||
aliases.reserve(command_.aliases.size());
|
||||
for (const auto& alias : command_.aliases) aliases.push_back(alias);
|
||||
return aliases;
|
||||
}
|
||||
bool requiresAdmin() const override { return command_.adminOnly; }
|
||||
|
||||
private:
|
||||
Void::Plugin::PluginManager& plugins_;
|
||||
std::string plugin_;
|
||||
Api::ExtensionCommand command_;
|
||||
};
|
||||
}
|
||||
|
||||
class PluginTrackSource final : public ITrackSource
|
||||
{
|
||||
public:
|
||||
PluginTrackSource(std::weak_ptr<PluginHost> host,
|
||||
Void::Plugin::PluginManager& plugins, std::string plugin,
|
||||
Api::ExtensionMediaSource source)
|
||||
: host_(std::move(host)), plugins_(plugins), plugin_(std::move(plugin)),
|
||||
source_(std::move(source))
|
||||
{
|
||||
}
|
||||
std::string_view name() const override { return source_.name; }
|
||||
bool canHandle(const std::string& query) const override
|
||||
{
|
||||
try {
|
||||
auto pin = plugins_.acquire(plugin_);
|
||||
auto* extension = pin
|
||||
? dynamic_cast<Api::IExtensionPlugin*>(pin.get()) : nullptr;
|
||||
return extension && declaredBy(*extension) &&
|
||||
extension->canHandleMedia(source_.name, query);
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool isAvailable() const override
|
||||
{
|
||||
try {
|
||||
if (host_.expired()) return false;
|
||||
auto pin = plugins_.acquire(plugin_);
|
||||
auto* extension = pin
|
||||
? dynamic_cast<Api::IExtensionPlugin*>(pin.get()) : nullptr;
|
||||
return extension && declaredBy(*extension);
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool supportsSearch() const override
|
||||
{
|
||||
try {
|
||||
auto pin = plugins_.acquire(plugin_);
|
||||
auto* extension = pin
|
||||
? dynamic_cast<Api::IExtensionPlugin*>(pin.get()) : nullptr;
|
||||
if (!extension) return false;
|
||||
auto descriptors = extension->mediaSources();
|
||||
auto descriptor = std::ranges::find(
|
||||
descriptors, source_.name, &Api::ExtensionMediaSource::name);
|
||||
return descriptor != descriptors.end() && descriptor->supportsSearch;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
[[nodiscard]] bool represents(std::string_view plugin,
|
||||
std::string_view source) const
|
||||
{
|
||||
return plugin_ == plugin && source_.name == source;
|
||||
}
|
||||
Api::Result<Api::SearchResult> search(std::string_view query) override
|
||||
{
|
||||
try {
|
||||
auto pin = plugins_.acquire(plugin_);
|
||||
auto* extension = pin
|
||||
? dynamic_cast<Api::IExtensionPlugin*>(pin.get()) : nullptr;
|
||||
if (!extension) return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
||||
"plugin media source unavailable");
|
||||
return extension->searchMedia(source_.name, query);
|
||||
} catch (const std::exception& error) {
|
||||
return Api::fail(Api::ErrorCode::UnknownError, error.what());
|
||||
} catch (...) {
|
||||
return Api::fail(Api::ErrorCode::UnknownError,
|
||||
"plugin media search threw an exception");
|
||||
}
|
||||
}
|
||||
Api::Result<Api::TrackInfo> resolve(const std::string& query) override
|
||||
{
|
||||
try {
|
||||
auto pin = plugins_.acquire(plugin_);
|
||||
auto* extension = pin
|
||||
? dynamic_cast<Api::IExtensionPlugin*>(pin.get()) : nullptr;
|
||||
if (!extension) return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
||||
"plugin media source unavailable");
|
||||
return extension->resolveMedia(source_.name, query);
|
||||
} catch (const std::exception& error) {
|
||||
return Api::fail(Api::ErrorCode::UnknownError, error.what());
|
||||
} catch (...) {
|
||||
return Api::fail(Api::ErrorCode::UnknownError,
|
||||
"plugin media resolve threw an exception");
|
||||
}
|
||||
}
|
||||
Api::Result<StreamHandle> getStream(const Api::TrackInfo& track) override
|
||||
{
|
||||
try {
|
||||
auto pin = plugins_.acquire(plugin_);
|
||||
auto* extension = pin
|
||||
? dynamic_cast<Api::IExtensionPlugin*>(pin.get()) : nullptr;
|
||||
if (!extension) return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
||||
"plugin media source unavailable");
|
||||
auto result = extension->streamMedia(source_.name, track);
|
||||
if (!result) return std::unexpected(result.error());
|
||||
return StreamHandle{.url = std::move(result->url),
|
||||
.isLocalFile = result->localFile, .sourceName = source_.name};
|
||||
} catch (const std::exception& error) {
|
||||
return Api::fail(Api::ErrorCode::UnknownError, error.what());
|
||||
} catch (...) {
|
||||
return Api::fail(Api::ErrorCode::UnknownError,
|
||||
"plugin media stream threw an exception");
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
[[nodiscard]] bool declaredBy(Api::IExtensionPlugin& extension) const
|
||||
{
|
||||
auto descriptors = extension.mediaSources();
|
||||
return std::ranges::find(descriptors, source_.name,
|
||||
&Api::ExtensionMediaSource::name) != descriptors.end();
|
||||
}
|
||||
|
||||
std::weak_ptr<PluginHost> host_;
|
||||
Void::Plugin::PluginManager& plugins_;
|
||||
std::string plugin_;
|
||||
Api::ExtensionMediaSource source_;
|
||||
};
|
||||
|
||||
PluginHost::PluginHost(CommandManager& commands,
|
||||
std::shared_ptr<MusicManager> music, std::function<bool(uint64_t)> isAdmin,
|
||||
std::function<void(std::shared_ptr<ITrackSource>)> mediaSink)
|
||||
: commands_(commands), music_(std::move(music)),
|
||||
isAdmin_(std::move(isAdmin)), mediaSink_(std::move(mediaSink)),
|
||||
plugins_(Void::Plugin::PluginManager::Options{
|
||||
.dlclose_on_swap = false,
|
||||
.copy_on_load = true,
|
||||
.config_provider = {},
|
||||
.instance_validator = [](Void::Plugin::IPlugin& plugin,
|
||||
const Void::Plugin::PluginInfo&) {
|
||||
return dynamic_cast<Api::IExtensionPlugin*>(&plugin) != nullptr;
|
||||
}})
|
||||
{
|
||||
reloadCallback_ = plugins_.on_reloaded(
|
||||
[this](std::string_view name) { publish(name); });
|
||||
}
|
||||
|
||||
PluginHost::~PluginHost()
|
||||
{
|
||||
plugins_.remove_on_reloaded(reloadCallback_);
|
||||
for (const auto& name : plugins_.names()) unpublish(name);
|
||||
}
|
||||
|
||||
void PluginHost::loadDirectory(const std::filesystem::path& directory)
|
||||
{
|
||||
for (auto& [path, result] : plugins_.load_directory(directory)) {
|
||||
if (!result) {
|
||||
Void::Log::warn("PluginHost", "Failed to load '{}': {}",
|
||||
path.string(), Void::Plugin::to_string(result.error()));
|
||||
continue;
|
||||
}
|
||||
publish(*result);
|
||||
plugins_.watch(*result);
|
||||
}
|
||||
}
|
||||
|
||||
void PluginHost::setChangeCallback(std::function<void()> callback)
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
changeCallback_ = std::move(callback);
|
||||
}
|
||||
|
||||
void PluginHost::notifyChanged() const
|
||||
{
|
||||
std::function<void()> callback;
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
callback = changeCallback_;
|
||||
}
|
||||
if (callback) callback();
|
||||
}
|
||||
|
||||
void PluginHost::publish(std::string_view name)
|
||||
{
|
||||
unpublish(name);
|
||||
auto pin = plugins_.acquire(name);
|
||||
auto* extension = pin
|
||||
? dynamic_cast<Api::IExtensionPlugin*>(pin.get()) : nullptr;
|
||||
if (!extension) return;
|
||||
std::vector<Api::ExtensionCommand> commands;
|
||||
std::vector<Api::ExtensionMediaSource> mediaSources;
|
||||
try {
|
||||
commands = extension->commands();
|
||||
mediaSources = extension->mediaSources();
|
||||
} catch (const std::exception& error) {
|
||||
Void::Log::error("PluginHost", "Plugin '{}' descriptors threw: {}",
|
||||
name, error.what());
|
||||
return;
|
||||
} catch (...) {
|
||||
Void::Log::error("PluginHost", "Plugin '{}' descriptors threw", name);
|
||||
return;
|
||||
}
|
||||
for (const auto& descriptor : commands) {
|
||||
auto command = std::make_shared<ExtensionCommandAdapter>(
|
||||
plugins_, std::string(name), descriptor);
|
||||
if (!commands_.registerExternalCommand(
|
||||
std::string(name), descriptor.name, std::move(command))) {
|
||||
Void::Log::warn("PluginHost", "Command '{}' from '{}' conflicts",
|
||||
descriptor.name, name);
|
||||
}
|
||||
}
|
||||
for (auto source : mediaSources) {
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
if (std::ranges::any_of(sources_, [&](const auto& existing) {
|
||||
return existing->represents(name, source.name);
|
||||
})) continue;
|
||||
}
|
||||
auto proxy = std::make_shared<PluginTrackSource>(
|
||||
weak_from_this(), plugins_, std::string(name), std::move(source));
|
||||
music_->addTrackSource(proxy);
|
||||
if (mediaSink_) mediaSink_(proxy);
|
||||
std::lock_guard lock(mutex_);
|
||||
sources_.push_back(std::move(proxy));
|
||||
}
|
||||
Void::Log::info("PluginHost", "Published Echoion plugin '{}'", name);
|
||||
notifyChanged();
|
||||
}
|
||||
|
||||
void PluginHost::unpublish(std::string_view name)
|
||||
{
|
||||
commands_.unregisterExternalCommands(name);
|
||||
}
|
||||
|
||||
void PluginHost::dispatch(const Api::PluginEvent& event) const
|
||||
{
|
||||
for (const auto& name : plugins_.names()) {
|
||||
try {
|
||||
auto pin = plugins_.acquire(name);
|
||||
if (auto* extension = pin
|
||||
? dynamic_cast<Api::IExtensionPlugin*>(pin.get()) : nullptr) {
|
||||
extension->onEvent(event);
|
||||
}
|
||||
} catch (const std::exception& error) {
|
||||
Void::Log::error("PluginHost", "Plugin '{}' event handler threw: {}",
|
||||
name, error.what());
|
||||
} catch (...) {
|
||||
Void::Log::error("PluginHost", "Plugin '{}' event handler threw", name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Api::Result<std::string> PluginHost::call(
|
||||
std::string_view plugin, std::string_view method, std::string params,
|
||||
uint64_t clientId) const
|
||||
{
|
||||
try {
|
||||
auto pin = plugins_.acquire(plugin);
|
||||
auto* extension = pin
|
||||
? dynamic_cast<Api::IExtensionPlugin*>(pin.get()) : nullptr;
|
||||
if (!extension) return Api::fail(Api::ErrorCode::NotFound,
|
||||
"plugin not found");
|
||||
auto rpc = extension->rpcMethods();
|
||||
auto descriptor = std::ranges::find(rpc, method, &Api::ExtensionRpc::name);
|
||||
if (descriptor == rpc.end()) return Api::fail(Api::ErrorCode::NotFound,
|
||||
"plugin RPC method not found");
|
||||
if (descriptor->adminOnly && (!isAdmin_ || !isAdmin_(clientId))) {
|
||||
return Api::fail(Api::ErrorCode::PermissionDenied,
|
||||
"admin privileges required");
|
||||
}
|
||||
return extension->callRpc({.method = std::string(method),
|
||||
.params = std::move(params), .clientId = clientId});
|
||||
} catch (const std::exception& error) {
|
||||
Void::Log::error("PluginHost", "Plugin '{}' RPC '{}' threw: {}",
|
||||
plugin, method, error.what());
|
||||
return Api::fail(Api::ErrorCode::UnknownError, "plugin RPC failed");
|
||||
} catch (...) {
|
||||
Void::Log::error("PluginHost", "Plugin '{}' RPC '{}' threw", plugin, method);
|
||||
return Api::fail(Api::ErrorCode::UnknownError, "plugin RPC failed");
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Api::ExtensionWebAsset> PluginHost::webAssets() const
|
||||
{
|
||||
std::vector<Api::ExtensionWebAsset> assets;
|
||||
for (const auto& name : plugins_.names()) {
|
||||
try {
|
||||
auto pin = plugins_.acquire(name);
|
||||
if (auto* extension = pin
|
||||
? dynamic_cast<Api::IExtensionPlugin*>(pin.get()) : nullptr) {
|
||||
auto current = extension->webAssets();
|
||||
assets.insert(assets.end(), std::make_move_iterator(current.begin()),
|
||||
std::make_move_iterator(current.end()));
|
||||
}
|
||||
} catch (const std::exception& error) {
|
||||
Void::Log::error("PluginHost", "Plugin '{}' web assets threw: {}",
|
||||
name, error.what());
|
||||
} catch (...) {
|
||||
Void::Log::error("PluginHost", "Plugin '{}' web assets threw", name);
|
||||
}
|
||||
}
|
||||
return assets;
|
||||
}
|
||||
|
||||
std::vector<std::string> PluginHost::names() const { return plugins_.names(); }
|
||||
|
||||
std::vector<PluginHost::State> PluginHost::snapshotStates() const
|
||||
{
|
||||
std::vector<State> states;
|
||||
for (const auto& name : plugins_.names()) {
|
||||
if (auto state = plugins_.serialize_state(name)) {
|
||||
states.push_back({.name = name, .json = std::move(state->json),
|
||||
.version = state->version});
|
||||
}
|
||||
}
|
||||
return states;
|
||||
}
|
||||
|
||||
void PluginHost::restoreStates(const std::vector<State>& states)
|
||||
{
|
||||
for (const auto& state : states) {
|
||||
if (!plugins_.deserialize_state(state.name, state.json, state.version)) {
|
||||
Void::Log::warn("PluginHost", "Plugin '{}' rejected restored state",
|
||||
state.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user