奥特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.

77 lines
2.0KB

  1. #include "PluginLoader.hpp"
  2. #include <QCoreApplication>
  3. #include <QDebug>
  4. #include <QDir>
  5. #include <QFileInfo>
  6. #include "interfaces/plugin_interface.hpp"
  7. PluginLoader::PluginLoader(QObject *parent)
  8. : QObject(parent)
  9. {}
  10. QObject *PluginLoader::loadPlugin(const QString &path)
  11. {
  12. if (m_loader.isLoaded()) {
  13. m_loader.unload();
  14. }
  15. // 添加当前工作目录的调试输出
  16. qDebug() << "当前工作:" << path;
  17. // 处理相对路径和绝对路径
  18. QString absolutePath = path;
  19. // 确保路径以 "/" 开头
  20. if (!absolutePath.startsWith('/')) {
  21. absolutePath = "/" + absolutePath;
  22. }
  23. qDebug() << "原始路径:" << path;
  24. qDebug() << "处理后的路径:" << absolutePath;
  25. // 检查文件是否存在
  26. if (!QFileInfo::exists(absolutePath)) {
  27. qDebug() << "插件文件不存在:" << absolutePath;
  28. return nullptr;
  29. }
  30. m_loader.setFileName(absolutePath); // 使用处理后的绝对路径
  31. if (!m_loader.load()) {
  32. qDebug() << "插件加载失败:" << m_loader.errorString() << path;
  33. qDebug() << "库加载错误代码:" << m_loader.loadHints();
  34. qDebug() << "库搜索路径:" << QCoreApplication::libraryPaths();
  35. return nullptr;
  36. }
  37. QObject *plugin = m_loader.instance();
  38. if (!plugin) {
  39. qDebug() << "无法获取插件实例";
  40. return nullptr;
  41. }
  42. AuseftDDSPluginInterface *pluginInterface = qobject_cast<AuseftDDSPluginInterface *>(plugin);
  43. if (!pluginInterface) {
  44. qDebug() << "插件不是有效的 AuseftDDSPluginInterface";
  45. return nullptr;
  46. }
  47. // 连接插件的信号到加载器
  48. connect(plugin,
  49. SIGNAL(messagePublished(QString, int)),
  50. this,
  51. SIGNAL(messagePublished(QString, int)));
  52. return plugin;
  53. }
  54. bool PluginLoader::unloadPlugin()
  55. {
  56. return m_loader.unload();
  57. }
  58. QString PluginLoader::errorString() const
  59. {
  60. return m_loader.errorString();
  61. }