93 lines
2.3 KiB
C
93 lines
2.3 KiB
C
|
|
#pragma once
|
|||
|
|
|
|||
|
|
#include <types/base/types.h>
|
|||
|
|
#include <string>
|
|||
|
|
#include <vector>
|
|||
|
|
|
|||
|
|
namespace extra2d {
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 插件信息结构
|
|||
|
|
*/
|
|||
|
|
struct PluginInfo {
|
|||
|
|
std::string name; // 插件名称
|
|||
|
|
std::string version; // 版本号
|
|||
|
|
std::string author; // 作者
|
|||
|
|
std::string description; // 描述
|
|||
|
|
std::vector<std::string> dependencies; // 依赖的插件名称列表
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 插件接口 - 基于事件总线
|
|||
|
|
*
|
|||
|
|
* 插件是动态加载的扩展组件,可以在运行时加载/卸载
|
|||
|
|
* 插件通过事件总线与其他组件通信,零直接依赖
|
|||
|
|
*
|
|||
|
|
* 设计理念:
|
|||
|
|
* - 插件可以在运行时动态加载(动态库)或静态链接
|
|||
|
|
* - 插件之间可以声明依赖关系
|
|||
|
|
* - 插件通过事件总线发送和接收消息
|
|||
|
|
*/
|
|||
|
|
class IPlugin {
|
|||
|
|
public:
|
|||
|
|
virtual ~IPlugin() = default;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 获取插件信息
|
|||
|
|
* @return 插件信息结构
|
|||
|
|
*/
|
|||
|
|
virtual const PluginInfo& getInfo() const = 0;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 加载插件
|
|||
|
|
*
|
|||
|
|
* 在此注册事件监听器
|
|||
|
|
* 返回 false 表示加载失败
|
|||
|
|
*
|
|||
|
|
* @return 加载是否成功
|
|||
|
|
*/
|
|||
|
|
virtual bool load() { return true; }
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 卸载插件
|
|||
|
|
*
|
|||
|
|
* 在此执行清理工作
|
|||
|
|
* 事件监听器会自动注销(RAII)
|
|||
|
|
*/
|
|||
|
|
virtual void unload() {}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 获取依赖的插件列表
|
|||
|
|
* @return 依赖的插件名称列表
|
|||
|
|
*/
|
|||
|
|
virtual std::vector<std::string> getDependencies() const {
|
|||
|
|
return getInfo().dependencies;
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
} // namespace extra2d
|
|||
|
|
|
|||
|
|
// ============================================================================
|
|||
|
|
// 插件导出宏(用于动态库插件)
|
|||
|
|
// ============================================================================
|
|||
|
|
|
|||
|
|
#ifdef _WIN32
|
|||
|
|
#define EXTRA2D_PLUGIN_EXPORT extern "C" __declspec(dllexport)
|
|||
|
|
#else
|
|||
|
|
#define EXTRA2D_PLUGIN_EXPORT extern "C" __attribute__((visibility("default")))
|
|||
|
|
#endif
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 定义插件导出接口
|
|||
|
|
*
|
|||
|
|
* 在插件动态库中使用:
|
|||
|
|
* EXTRA2D_DEFINE_PLUGIN(MyPluginClass)
|
|||
|
|
*/
|
|||
|
|
#define EXTRA2D_DEFINE_PLUGIN(PluginClass) \
|
|||
|
|
EXTRA2D_PLUGIN_EXPORT extra2d::IPlugin* extra2d_create_plugin() { \
|
|||
|
|
return new PluginClass(); \
|
|||
|
|
} \
|
|||
|
|
EXTRA2D_PLUGIN_EXPORT void extra2d_destroy_plugin(extra2d::IPlugin* plugin) { \
|
|||
|
|
delete plugin; \
|
|||
|
|
}
|