101 lines
3.0 KiB
C++
101 lines
3.0 KiB
C++
#pragma once
|
|
#include <Void/System/FileWatcher.hpp>
|
|
#include <Void/Plugin/Manager.hpp>
|
|
#include <CoreInterface.hpp>
|
|
#include <PluginTypes.hpp>
|
|
#include <Types.hpp>
|
|
#include <atomic>
|
|
#include <condition_variable>
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <queue>
|
|
#include <thread>
|
|
|
|
namespace Echoion::Host
|
|
{
|
|
|
|
class CoreLoader
|
|
{
|
|
public:
|
|
|
|
CoreLoader();
|
|
~CoreLoader();
|
|
|
|
Api::Result<void> load(const std::filesystem::path& libraryPath);
|
|
Api::Result<void> unload();
|
|
Api::Result<void>
|
|
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;
|
|
Api::Result<void>
|
|
checkpoint(const std::filesystem::path& destination);
|
|
Api::Result<void>
|
|
restoreCheckpoint(const std::filesystem::path& source);
|
|
|
|
// 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()>;
|
|
using HotReloadPrepareCallback = std::function<void()>;
|
|
void setHotReloadPrepareCallback(HotReloadPrepareCallback callback)
|
|
{
|
|
hotReloadPrepareCallback_ = std::move(callback);
|
|
}
|
|
void setHotReloadCallback(HotReloadCallback callback)
|
|
{
|
|
hotReloadCallback_ = std::move(callback);
|
|
}
|
|
|
|
private:
|
|
|
|
Api::Result<void>
|
|
loadLibraryInternal(const std::filesystem::path& path);
|
|
Api::Result<void> unloadLibraryInternal();
|
|
[[nodiscard]] Api::Result<void> refreshCoreHandle();
|
|
|
|
Void::Plugin::PluginManager pluginManager_;
|
|
std::string pluginName_;
|
|
Api::CoreInterfacePtr core_;
|
|
std::filesystem::path currentLibraryPath_;
|
|
std::filesystem::path configPath_;
|
|
mutable std::mutex mutex_;
|
|
std::atomic<bool> loaded_{false};
|
|
std::atomic<bool> hotReloading_{false};
|
|
|
|
// Auto reload
|
|
Void::FileSystem::FileWatcher fileWatcher_;
|
|
std::atomic<bool> autoReloadEnabled_{false};
|
|
|
|
// Deferred reload - waits for current track to finish
|
|
std::thread deferredReloadThread_;
|
|
std::atomic<bool> hasPendingReload_{false};
|
|
std::atomic<bool> stopDeferredReload_{false};
|
|
std::condition_variable deferredReloadCv_;
|
|
std::filesystem::path pendingReloadPath_;
|
|
std::string pendingTrackId_; // Track ID that was playing when
|
|
// reload scheduled
|
|
mutable std::mutex pendingReloadMutex_;
|
|
|
|
// Hot reload completion callback
|
|
HotReloadPrepareCallback hotReloadPrepareCallback_;
|
|
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::Host
|