Extra2D/include/plugin/plugin_loader.h

157 lines
3.6 KiB
C
Raw Normal View History

#pragma once
#include <plugin\iplugin.h>
#include <vector>
#include <unordered_map>
#include <string>
#include <memory>
namespace extra2d {
/**
* @brief -
*
* /
*
*/
class PluginLoader {
public:
PluginLoader();
~PluginLoader();
// 禁止拷贝
PluginLoader(const PluginLoader&) = delete;
PluginLoader& operator=(const PluginLoader&) = delete;
// 允许移动
PluginLoader(PluginLoader&&) noexcept;
PluginLoader& operator=(PluginLoader&&) noexcept;
/**
* @brief
* @param path
* @return
*/
bool loadFromLibrary(const char* path);
/**
* @brief
* @param plugin
*/
void registerPlugin(IPlugin* plugin);
/**
* @brief
* @param name
*/
void unloadPlugin(const char* name);
/**
* @brief
* @param name
* @return nullptr
*/
IPlugin* getPlugin(const char* name) const;
/**
* @brief
* @param name
* @return
*/
bool hasPlugin(const char* name) const;
/**
* @brief
* @return
*/
std::vector<IPlugin*> getAllPlugins() const;
/**
* @brief
* @return
*/
bool initAll();
/**
* @brief
*/
void shutdownAll();
/**
* @brief
* @return
*/
size_t getPluginCount() const;
/**
* @brief
* @param path
*/
void addSearchPath(const char* path);
/**
* @brief
* @param directory
* @return
*/
size_t loadPluginsFromDirectory(const char* directory);
private:
/**
* @brief
*/
struct PluginEntry {
IPlugin* plugin = nullptr; // 插件实例
void* handle = nullptr; // 动态库句柄
bool isDynamic = false; // 是否为动态加载
bool owned = false; // 是否由加载器管理生命周期
};
/**
* @brief
* @param plugin
* @return
*/
bool resolveDependencies(IPlugin* plugin);
/**
* @brief
* @param dependencies
* @return
*/
bool checkDependencies(const std::vector<std::string>& dependencies);
/**
* @brief
*/
void sortPluginsByDependencies();
/**
* @brief
* @param path
* @return
*/
void* loadDynamicLibrary(const char* path);
/**
* @brief
* @param handle
*/
void unloadDynamicLibrary(void* handle);
/**
* @brief
* @param handle
* @param name
* @return
*/
void* getSymbol(void* handle, const char* name);
std::unordered_map<std::string, PluginEntry> plugins_;
std::vector<std::string> searchPaths_;
std::vector<IPlugin*> sortedPlugins_; // 按依赖排序的插件列表
bool inited_ = false;
};
} // namespace extra2d