#pragma once #include "CommandManager.hpp" #include "ConfigLoader.hpp" #include "LyricsService.hpp" #include "MusicManager.hpp" #include "NetworkClient.hpp" #include "RpcServer.hpp" #ifdef ECHOION_WITH_WEB #include "echoion/web/HttpServer.hpp" #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Void::Logging { class MultiLogger; } namespace Echoion::Core { class PluginHost; class TeamSpeakDirectoryService; // Main Core implementation of ICoreInterface class Core : public Api::ICoreInterface { public: Core(); ~Core() override; // ICoreInterface - Lifecycle Api::Result initialize(const std::filesystem::path& configPath) override; Api::Result shutdown() override; [[nodiscard]] bool isInitialized() const noexcept override; // ICoreInterface - Playback control (decoupled) Api::Result play(const std::string& trackId) override; Api::Result pause() override; Api::Result resume() override; Api::Result stop() override; Api::Result skip(uint32_t count) override; Api::Result seek(std::chrono::milliseconds position) override; Api::Result setVolume(float volume) override; // ICoreInterface - Queue management (decoupled) Api::Result queueTrack(const std::string& trackId, uint64_t clientId) override; Api::Result removeFromQueue(uint64_t queueId) override; Api::Result clearQueue() override; Api::Result moveInQueue(uint64_t queueId, uint32_t newPosition) override; // ICoreInterface - Track info (decoupled) [[nodiscard]] Api::Result> getCurrentTrackSummary() const override; [[nodiscard]] Api::Result getPlaybackSummary() const override; // ICoreInterface - Playlist (decoupled) Api::Result loadPlaylist(const std::string& playlistId, uint64_t clientId) override; [[nodiscard]] Api::Result> getPlaylistIds() const override; // ICoreInterface - Configuration (decoupled) Api::Result updateConfigJson(const std::string& configJson) override; [[nodiscard]] Api::Result getConfigJson() const override; // ICoreInterface - Hot reload (decoupled) [[nodiscard]] Api::Result prepareForHotReload() override; Api::Result restoreAfterHotReload(const std::string& state) override; // ICoreInterface - Decoupled state management [[nodiscard]] Api::StateBlob saveState() override; Api::Result restoreState(const Api::StateBlob& state) override; [[nodiscard]] uint32_t getStateFormatVersion() const override; [[nodiscard]] bool canRestoreStateVersion(uint32_t version) const override; // ICoreInterface - Event handling void registerEventCallback( const Api::EventCallback& callback) override; void unregisterEventCallback( const Api::EventCallback& callback) override; // ICoreInterface - Plugin event processing Api::Result onPluginEvent(const Api::PluginEvent& event) override; // ICoreInterface - Status [[nodiscard]] std::string getVersion() const override; [[nodiscard]] std::chrono::system_clock::time_point getUptime() const override; // ICoreInterface - Plugin interface void setPluginInterface( Api::PluginInterfacePtr pluginInterface) override; void triggerAutoConnect() override; // ICoreInterface - Advanced (for internal Core-to-Core use) [[nodiscard]] Api::Result> getCurrentTrackHandle() const override; [[nodiscard]] Api::Result< std::vector>> getQueueHandles() const override; [[nodiscard]] Api::Result getConfigHandle() const override; Api::Result updateConfigFromHandle(Api::AppConfigHandle config) override; // Additional methods for internal use (not in interface) void setAudioDataCallback( const MusicManager::AudioDataCallback& callback); Api::Result reloadConfig() override; [[nodiscard]] Api::Result managePluginsJson(const std::string& requestJson) override; // Internal implementations using full types Api::Result playTrack(const Api::TrackInfo& track); Api::Result queueTrackFull(const Api::TrackInfo& track, uint64_t clientId); [[nodiscard]] Api::Result> getQueue() const; [[nodiscard]] Api::Result> getCurrentTrack() const; [[nodiscard]] Api::Result getPlaybackState() const; [[nodiscard]] Api::Result> getPlaylists() const; Api::Result updateConfig(const Api::AppConfig& config); [[nodiscard]] Api::Result getConfig() const; [[nodiscard]] Api::Result prepareForHotReloadFull(); Api::Result restoreAfterHotReloadFull(const Api::HotReloadState& state); // Generate RPC tokens for existing subscribers void generateRpcTokensForSubscribers(); private: void onMusicEvent(const Api::PluginEvent& event); void broadcastEvent(const Api::PluginEvent& event); [[nodiscard]] bool isAdmin(uint64_t clientId) const; [[nodiscard]] bool isDj(uint64_t clientId) const; void updateWhisperList(); void setupRpcHandlers(); void setupEventBusSubscriptions() noexcept; void applyUrlShorteningConfig(); // Fetch an online client's avatar bytes (TS local cache, then the // myTeamSpeak URL, then a legacy file-transfer download) and // persist them — and the myTS URL — to the database keyed by uid. // Returns {contentType, bytes} on success. Web-agnostic; used by // both the avatar HTTP provider and the background warmer. [[nodiscard]] std::optional> fetchClientAvatarBytes(const std::string& uid, uint64_t clientId); // Periodically cache avatars for every online client so they remain // available (from the DB) once the user goes offline. Runs on // avatarWarmer_. void warmAvatarCache(); // Build the console + split-file logging stack and install it as // the global Void::Log sink (logSink_ must outlive logging). void installLogging(const std::filesystem::path& logDirectory, const Api::LoggingConfig& config); // Guard helper: ok if initialized, else a ServiceUnavailable error. [[nodiscard]] Api::Result requireInitialized() const noexcept; // Components std::shared_ptr networkClient_; std::shared_ptr lyricsService_; std::shared_ptr musicManager_; std::unique_ptr commandManager_; std::shared_ptr extensionPlugins_; std::shared_ptr directoryService_; Api::PluginInterfacePtr pluginInterface_; // State Api::AppConfig config_; std::filesystem::path configPath_; std::chrono::system_clock::time_point startTime_; std::atomic initialized_{false}; // Guards triggerAutoConnect() against duplicate connection // attempts (reset on a failed initiation so a retry can run). std::atomic autoConnectInitiated_{false}; // Event callbacks std::vector eventCallbacks_; mutable std::mutex eventMutex_; // Authenticated RPC client -> stable TS uid + nickname, resolved // at token auth. Keys per-user features (favorites, vote-skip). struct RpcIdentity { std::string uid; std::string nickname; }; std::map rpcIdentities_; mutable std::mutex rpcIdentityMutex_; [[nodiscard]] std::optional rpcIdentity(uint64_t clientId) const; [[nodiscard]] bool hasLiveTeamSpeakRole( uint64_t clientId, const std::vector& configuredGroups) const; // Full identity behind an RPC token: uid, display nickname and // admin standing. Shared by token auth and IP autologin. struct TokenIdentity { std::string uid; std::string nickname; bool isAdmin = false; bool isDj = false; }; [[nodiscard]] std::optional identityForToken(const std::string& token); // Mark an RPC connection authenticated as `identity` and retain its // stable TeamSpeak identity. Roles are checked live for each // request. void registerRpcAuth(uint64_t clientId, const TokenIdentity& identity); // Guards RestartWebInterface against overlapping restarts. std::atomic webRestartPending_{false}; // Database for persistence std::shared_ptr database_; // Background avatar cache warmer + the uids already cached this // session (so we don't re-download an unchanged avatar every // cycle). std::jthread avatarWarmer_; std::unordered_set warmedUids_; // RPC server for Desktop/API clients std::shared_ptr rpcServer_; #ifdef ECHOION_WITH_WEB // Browser web interface: serves the static dashboard and // same-origin RPC bridge. Bot control still terminates at the RPC // server. std::shared_ptr webServer_; #endif // Composition root: a type-keyed registry of all Core services // (logger, config, event bus, network, database, music, rpc). // initialize() provides each instance; resolveServices() and other // code can require() them by interface instead of reaching for // global singletons. The former singletons stay as thin macro // shims over the same instances. Void::Service::ServiceRegistry registry_; // Application event bus (async): playback/queue/subscription events // published by MusicManager and consumed here for RPC broadcast + // DB persistence. Owned by Core, provided as a service. Void::EventBus::EventBus eventBus_{Void::EventBus::Mode::Async}; // Config holder kept in sync with config_ and provided as a service // (replaces the former ConfigManager singleton). Api::ConfigManager configManager_; // Owns the logging stack installed as the global Void::Log sink; // must outlive any logging (i.e. the whole process). std::shared_ptr logSink_; public: // Access the service registry (composition root). [[nodiscard]] Void::Service::ServiceRegistry& services() noexcept { return registry_; } }; } // namespace Echoion::Core