109 lines
3.5 KiB
C++
109 lines
3.5 KiB
C++
#pragma once
|
|
|
|
#include <ExtensionPlugin.hpp>
|
|
#include <NetworkTypes.hpp>
|
|
#include <Void/Plugin/Manager.hpp>
|
|
#include <Void/Service/PluginService.hpp>
|
|
#include <Void/Service/Registry.hpp>
|
|
#include <Void/ShortUrl/ShortUrl.hpp>
|
|
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
namespace Echoion::Core
|
|
{
|
|
class CommandManager;
|
|
class MusicManager;
|
|
class ITrackSource;
|
|
|
|
class PluginHost final : public std::enable_shared_from_this<PluginHost>
|
|
{
|
|
public:
|
|
|
|
struct Configuration {
|
|
std::filesystem::path configDirectory;
|
|
std::filesystem::path stateDirectory;
|
|
std::string publicUrl;
|
|
std::optional<Void::Net::ProxyConfig> proxy;
|
|
std::string shortenerProvider = "url-shortener-internal";
|
|
};
|
|
|
|
PluginHost(CommandManager& commands,
|
|
std::shared_ptr<MusicManager>
|
|
music,
|
|
std::function<bool(uint64_t)>
|
|
isAdmin,
|
|
std::function<void(std::shared_ptr<ITrackSource>)>
|
|
mediaSink,
|
|
Configuration configuration);
|
|
~PluginHost();
|
|
|
|
void loadDirectory(const std::filesystem::path& directory);
|
|
void loadManagedDirectory(const std::filesystem::path& directory);
|
|
[[nodiscard]] Api::Result<std::string>
|
|
manage(const std::string& requestJson);
|
|
void setChangeCallback(std::function<void()> callback);
|
|
void dispatch(const Api::PluginEvent& event) const;
|
|
[[nodiscard]] Api::Result<std::string>
|
|
call(std::string_view plugin, std::string_view method,
|
|
std::string params, uint64_t clientId) const;
|
|
[[nodiscard]] std::vector<Api::ExtensionWebAsset> webAssets() const;
|
|
[[nodiscard]] std::vector<std::string> names() const;
|
|
[[nodiscard]] bool reconfigure(Configuration configuration);
|
|
[[nodiscard]] std::shared_ptr<Void::ShortUrl::IShortener>
|
|
shortener() const;
|
|
[[nodiscard]] Void::ShortUrl::ResolveResult
|
|
resolveShortLink(std::string_view key) const;
|
|
struct State {
|
|
std::string name;
|
|
std::string json;
|
|
std::uint32_t version{};
|
|
};
|
|
[[nodiscard]] std::vector<State> snapshotStates() const;
|
|
void restoreStates(const std::vector<State>& states);
|
|
|
|
private:
|
|
|
|
void publish(std::string_view name);
|
|
void unpublish(std::string_view name);
|
|
void notifyChanged() const;
|
|
[[nodiscard]] std::string
|
|
pluginConfiguration(std::string_view name) const;
|
|
[[nodiscard]] bool bindShortenerServices();
|
|
|
|
CommandManager& commands_;
|
|
std::shared_ptr<MusicManager> music_;
|
|
std::function<bool(uint64_t)> isAdmin_;
|
|
std::function<void(std::shared_ptr<ITrackSource>)> mediaSink_;
|
|
Void::Plugin::PluginManager plugins_;
|
|
Void::Service::ServiceRegistry shortenerRegistry_;
|
|
Void::Service::ServiceRegistry internalShortenerRegistry_;
|
|
std::unique_ptr<
|
|
Void::Service::PluginServiceBinding<Void::ShortUrl::IShortener>>
|
|
shortenerBinding_;
|
|
std::unique_ptr<
|
|
Void::Service::PluginServiceBinding<Void::ShortUrl::IShortener>>
|
|
internalShortenerBinding_;
|
|
Void::Plugin::ReloadCallbackId reloadCallback_{};
|
|
mutable std::mutex reconfigureMutex_;
|
|
mutable std::mutex mutex_;
|
|
Configuration configuration_;
|
|
std::vector<std::shared_ptr<class PluginTrackSource>> sources_;
|
|
std::function<void()> changeCallback_;
|
|
struct ManagedPlugin {
|
|
std::string version;
|
|
std::filesystem::path path;
|
|
};
|
|
std::filesystem::path managedDirectory_;
|
|
std::unordered_map<std::string, ManagedPlugin> managed_;
|
|
std::unordered_map<std::string, std::filesystem::path> loadedPaths_;
|
|
};
|
|
} // namespace Echoion::Core
|