Files
EchoionPlugin/include/echoion/plugin/CoreLoader.hpp
T
2026-02-01 23:07:06 +03:00

61 lines
1.9 KiB
C++

#pragma once
#include <echoion/api.hpp>
#include <dylib.hpp>
#include "FileWatcher.hpp"
#include "echoion/api/ConfigTypes.hpp"
#include <memory>
#include <mutex>
#include <atomic>
#include <filesystem>
#include <thread>
#include <condition_variable>
#include <queue>
namespace Echoion::Plugin {
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;
// Automatic hot reload
void enableAutoReload();
void disableAutoReload();
[[nodiscard]] bool isAutoReloadEnabled() const noexcept;
private:
Api::Result<void> loadLibraryInternal(const std::filesystem::path& path);
Api::Result<void> unloadLibraryInternal();
[[nodiscard]] Api::HotReloadState saveStateInternal();
[[nodiscard]] Api::Result<void> restoreStateInternal(const Api::HotReloadState& state);
[[nodiscard]] std::filesystem::path getTempLibraryPath(const std::filesystem::path& original) const;
void cleanupTempFiles();
std::unique_ptr<dylib::library> library_;
Api::CoreInterfacePtr core_;
std::filesystem::path currentLibraryPath_;
std::filesystem::path tempLibraryPath_;
std::filesystem::path configPath_;
mutable std::mutex mutex_;
std::atomic<bool> loaded_{false};
std::atomic<bool> hotReloading_{false};
// Auto reload
FileWatcher fileWatcher_;
std::atomic<bool> autoReloadEnabled_{false};
void onCoreFileChanged();
};
} // namespace Echoion::Plugin