#pragma once #include #include #include "FileWatcher.hpp" #include "echoion/api/ConfigTypes.hpp" #include #include #include #include #include #include #include #include namespace Echoion::Plugin { class CoreLoader { public: CoreLoader(); ~CoreLoader(); Api::Result load(const std::filesystem::path& libraryPath); Api::Result unload(); Api::Result hotReload(const std::filesystem::path& newLibraryPath); [[nodiscard]] bool isLoaded() const noexcept; [[nodiscard]] Api::CoreInterfacePtr getCore() const; void setConfigPath(const std::filesystem::path& configPath); [[nodiscard]] std::filesystem::path getConfigPath() const; [[nodiscard]] bool isHotReloading() const noexcept; // Automatic hot reload void enableAutoReload(); void disableAutoReload(); [[nodiscard]] bool isAutoReloadEnabled() const noexcept; // Callback for hot reload completion (called after auto-reload finishes) using HotReloadCallback = std::function; void setHotReloadCallback(HotReloadCallback callback) { hotReloadCallback_ = std::move(callback); } private: Api::Result loadLibraryInternal(const std::filesystem::path& path); Api::Result unloadLibraryInternal(); [[nodiscard]] Api::HotReloadState saveStateInternal(); [[nodiscard]] Api::Result restoreStateInternal(const Api::HotReloadState& state); [[nodiscard]] std::filesystem::path getTempLibraryPath(const std::filesystem::path& original) const; void cleanupTempFiles(); std::unique_ptr library_; Api::CoreInterfacePtr core_; std::filesystem::path currentLibraryPath_; std::filesystem::path tempLibraryPath_; std::filesystem::path configPath_; mutable std::mutex mutex_; std::atomic loaded_{false}; std::atomic hotReloading_{false}; // Auto reload FileWatcher fileWatcher_; std::atomic autoReloadEnabled_{false}; // Deferred reload - waits for current track to finish std::thread deferredReloadThread_; std::atomic hasPendingReload_{false}; std::filesystem::path pendingReloadPath_; std::string pendingTrackId_; // Track ID that was playing when reload scheduled mutable std::mutex pendingReloadMutex_; // Hot reload completion callback HotReloadCallback hotReloadCallback_; void onCoreFileChanged(); void waitForTrackAndReload(const std::filesystem::path& targetPath, const std::string& trackId); [[nodiscard]] bool isPlaybackActive() const; [[nodiscard]] bool isSameTrackPlaying(const std::string& trackId) const; }; } // namespace Echoion::Plugin