|
- #include "PluginLoader.hpp"
- #include <QDebug>
- #include <QDir>
- #include <QFileInfo>
- #include <QCoreApplication>
- #include "interfaces/plugin_interface.hpp"
-
- PluginLoader::PluginLoader(QObject *parent)
- : QObject(parent)
- {
- }
-
- QObject* PluginLoader::loadPlugin(const QString& path)
- {
- if (m_loader.isLoaded()) {
- m_loader.unload();
- }
-
- // 添加当前工作目录的调试输出
- qDebug() << "当前工作:" << path;
-
- // 处理相对路径和绝对路径
- QString absolutePath = path;
-
- // 确保路径以 "/" 开头
- if (!absolutePath.startsWith('/')) {
- absolutePath = "/" + absolutePath;
- }
-
- qDebug() << "原始路径:" << path;
- qDebug() << "处理后的路径:" << absolutePath;
-
- // 检查文件是否存在
- if (!QFileInfo::exists(absolutePath)) {
- qDebug() << "插件文件不存在:" << absolutePath;
- return nullptr;
- }
-
- m_loader.setFileName(absolutePath); // 使用处理后的绝对路径
-
- if (!m_loader.load()) {
- qDebug() << "插件加载失败:" << m_loader.errorString()<<path;
- qDebug() << "库加载错误代码:" << m_loader.loadHints();
- qDebug() << "库搜索路径:" << QCoreApplication::libraryPaths();
- return nullptr;
- }
-
- QObject* plugin = m_loader.instance();
- if (!plugin) {
- qDebug() << "无法获取插件实例";
- return nullptr;
- }
-
- AuseftDDSPluginInterface* pluginInterface = qobject_cast<AuseftDDSPluginInterface*>(plugin);
- if (!pluginInterface) {
- qDebug() << "插件不是有效的 AuseftDDSPluginInterface";
- return nullptr;
- }
-
- // 连接插件的信号到加载器
- connect(plugin, SIGNAL(messagePublished(QString,int)),
- this, SIGNAL(messagePublished(QString,int)));
-
- return plugin;
- }
-
- bool PluginLoader::unloadPlugin()
- {
- return m_loader.unload();
- }
-
- QString PluginLoader::errorString() const
- {
- return m_loader.errorString();
- }
|