奥特QT DDS 插件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PluginLoader.cxx 2.4KB

5 kuukautta sitten
5 kuukautta sitten
5 kuukautta sitten
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "PluginLoader.hpp"
  2. #include <QDebug>
  3. #include <QDir>
  4. #include <QFileInfo>
  5. #include <QCoreApplication>
  6. #include "interfaces/plugin_interface.hpp"
  7. PluginLoader::PluginLoader(QObject *parent)
  8. : QObject(parent)
  9. {
  10. }
  11. QObject* PluginLoader::loadPlugin(const QString& path)
  12. {
  13. if (m_loader.isLoaded()) {
  14. m_loader.unload();
  15. }
  16. // 处理文件路径
  17. QString cleanPath = path;
  18. if (cleanPath.startsWith("file:///")) {
  19. cleanPath = cleanPath.mid(8); // 移除 "file:///"
  20. }
  21. // 确保使用正确的路径分隔符
  22. cleanPath = QDir::toNativeSeparators(cleanPath);
  23. qDebug() << "处理后的路径:" << cleanPath;
  24. // 检查文件是否存在
  25. QFileInfo fileInfo(cleanPath);
  26. if (!fileInfo.exists()) {
  27. qDebug() << "插件文件不存在:" << cleanPath;
  28. return nullptr;
  29. }
  30. // 获取插件所在目录的绝对路径
  31. QString pluginDir = fileInfo.absolutePath();
  32. // 添加插件目录到库搜索路径
  33. if (!QCoreApplication::libraryPaths().contains(pluginDir)) {
  34. QCoreApplication::addLibraryPath(pluginDir);
  35. qDebug() << "添加库搜索路径:" << pluginDir;
  36. }
  37. // 使用绝对路径加载插件
  38. QString absolutePath = fileInfo.absoluteFilePath();
  39. m_loader.setFileName(absolutePath);
  40. qDebug() << "尝试加载插件:" << absolutePath;
  41. qDebug() << "当前库搜索路径:" << QCoreApplication::libraryPaths();
  42. if (!m_loader.load()) {
  43. qDebug() << "插件加载失败:" << m_loader.errorString();
  44. qDebug() << "完整路径:" << absolutePath;
  45. return nullptr;
  46. }
  47. QObject* plugin = m_loader.instance();
  48. if (!plugin) {
  49. qDebug() << "无法获取插件实例";
  50. return nullptr;
  51. }
  52. AuseftDDSPluginInterface* pluginInterface = qobject_cast<AuseftDDSPluginInterface*>(plugin);
  53. if (!pluginInterface) {
  54. qDebug() << "插件不是有效的 AuseftDDSPluginInterface";
  55. return nullptr;
  56. }
  57. // 连接插件的信号到加载器
  58. connect(plugin, SIGNAL(messagePublished(QString,int)),
  59. this, SIGNAL(messagePublished(QString,int)));
  60. return plugin;
  61. }
  62. bool PluginLoader::unloadPlugin()
  63. {
  64. return m_loader.unload();
  65. }
  66. QString PluginLoader::errorString() const
  67. {
  68. return m_loader.errorString();
  69. }