|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- #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();
- }
-
- // 处理文件路径
- QString cleanPath = path;
- if (cleanPath.startsWith("file:///")) {
- cleanPath = cleanPath.mid(8); // 移除 "file:///"
- }
-
- // 确保使用正确的路径分隔符
- cleanPath = QDir::toNativeSeparators(cleanPath);
-
- qDebug() << "处理后的路径:" << cleanPath;
-
- // 检查文件是否存在
- QFileInfo fileInfo(cleanPath);
- if (!fileInfo.exists()) {
- qDebug() << "插件文件不存在:" << cleanPath;
- return nullptr;
- }
-
- // 获取插件所在目录的绝对路径
- QString pluginDir = fileInfo.absolutePath();
-
- // 添加插件目录到库搜索路径
- if (!QCoreApplication::libraryPaths().contains(pluginDir)) {
- QCoreApplication::addLibraryPath(pluginDir);
- qDebug() << "添加库搜索路径:" << pluginDir;
- }
-
- // 使用绝对路径加载插件
- QString absolutePath = fileInfo.absoluteFilePath();
- m_loader.setFileName(absolutePath);
-
- qDebug() << "尝试加载插件:" << absolutePath;
- qDebug() << "当前库搜索路径:" << QCoreApplication::libraryPaths();
-
- if (!m_loader.load()) {
- qDebug() << "插件加载失败:" << m_loader.errorString();
- qDebug() << "完整路径:" << absolutePath;
- 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();
- }
|