ToolDepository.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. #include "ToolDepository.h"
  2. // QT运行库相关的文件夹和dll名称(用于识别tools的时候进行排除,仅用于Qt6)
  3. #define QT_PLATFORMS "platforms"
  4. #define QT6_DEBUG_DLL1 "Qt6Cored.dll"
  5. #define QT6_DEBUG_DLL2 "Qt6Guid.dll"
  6. #define QT6_DEBUG_DLL3 "Qt6Widgetsd.dll"
  7. #define QT6_RELEASE_DLL1 "Qt6Core.dll"
  8. #define QT6_RELEASE_DLL2 "Qt6Gui.dll"
  9. #define QT6_RELEASE_DLL3 "Qt6Widgets.dll"
  10. // 内置的工具的默认分类名称(包括Port工具、注释工具、Goto工具等)
  11. #define BUILDIN_CATEGORY_NAME ("内置工具")
  12. // 工具仓库实例
  13. ToolDepository toolDepository;
  14. ToolDepository::ToolDepository()
  15. {
  16. }
  17. ToolDepository::~ToolDepository()
  18. {
  19. }
  20. ////////////////////////////////////////////////////////////
  21. // 加载文件中保存的工具信息
  22. bool ToolDepository::LoadTools()
  23. {
  24. // m_TotalTools.clear();
  25. // 确保只加载一次
  26. if (m_TotalTools.size() > 0)
  27. {
  28. return true;
  29. }
  30. // 从文件中加载工具信息
  31. this->LoadFromFile(DEFAULT_TOOL_PATH);
  32. // 2021-3-29增加,直接在内部生成port工具,不从外部加载了
  33. this->createPortTool();
  34. // 2022-3-15,增加内部生成的Comment工具
  35. this->createCommentTool();
  36. // 2022-3-25,内部生成Goto工具
  37. this->createGotoTool();
  38. // 2022-4-18 生成内置的并行计算工具
  39. this->createParallelTool();
  40. // 2022-8-22 生成内置的ForLoop工具
  41. this->createForLoopTool();
  42. // 2022-9-2 生成内置的Wait工具
  43. this->createWaitTool();
  44. return true;
  45. }
  46. ////////////////////////////////////////////////////////////
  47. // 从文件中加载工具信息
  48. bool ToolDepository::LoadFromFile(const QString& strPath)
  49. {
  50. QDir dir(strPath);
  51. if (!dir.exists() || strPath.indexOf("platforms") > 1)
  52. return false;
  53. // 取所有的文件和文件名,但是去掉.和..的文件夹
  54. dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
  55. // 文件夹优先
  56. dir.setSorting(QDir::DirsFirst);
  57. QFileInfoList list = dir.entryInfoList();
  58. int i = 0;
  59. while(i < list.size())
  60. {
  61. QFileInfo fileInfo = list.at(i);
  62. bool bisDir = fileInfo.isDir();
  63. if (bisDir)
  64. {
  65. LoadFromFile(fileInfo.filePath());
  66. }
  67. else
  68. {
  69. QString strFileName = fileInfo.fileName();
  70. // 加载所有的dll工具
  71. if (strFileName.endsWith(TOOL_EXT)
  72. // 跳过Qt6相关运行Dll
  73. //&& strFileName.compare(QT6_DEBUG_DLL1) != 0
  74. //&& strFileName.compare(QT6_DEBUG_DLL2) != 0
  75. //&& strFileName.compare(QT6_DEBUG_DLL3) != 0
  76. //&& strFileName.compare(QT6_RELEASE_DLL1) != 0
  77. //&& strFileName.compare(QT6_RELEASE_DLL2) != 0
  78. //&& strFileName.compare(QT6_RELEASE_DLL3) != 0
  79. )
  80. {
  81. QString strDllFullPath = fileInfo.absoluteFilePath();
  82. // 取出新的dll工具信息,加入到集合中
  83. if (!ParseToolsFromDll(strDllFullPath, strFileName))
  84. {
  85. // Report Error
  86. // 2021-8-5,居然发现了一个隐藏多年的bug,此处会导致代码死循环
  87. i++;
  88. continue;
  89. }
  90. }
  91. }
  92. i++;
  93. };
  94. return true;
  95. }
  96. /////////////////////////////////////////////////////////////
  97. // 从dll中提取工具信息
  98. bool ToolDepository::ParseToolsFromDll(const QString& strDllPath, const QString& strDllName)
  99. {
  100. // 加载Dll
  101. // 2021-01-17,设计思路调整,此处工具仓库加载完工具信息后,直接释放掉,后面用到的时候再加载,优化资源占用
  102. QLibrary dllTool(strDllPath);
  103. if (!dllTool.load())
  104. {
  105. vWarning() << "[Error] ToolDepository::ParseToolsFromDll - load [" << strDllName << "] failed, Reason:"
  106. << dllTool.errorString();
  107. return false;
  108. }
  109. // 调用NewToolPtr接口
  110. typedef DllTool* (*GETTOOL)();
  111. GETTOOL GetTool = (GETTOOL)dllTool.resolve(FUNCTION_GETTOOL);
  112. if (GetTool == nullptr)
  113. {
  114. qDebug() << "[Error] ToolDepository::ParseToolsFromDll - [" << strDllName << "] GetTool failed, Reason:"
  115. << dllTool.errorString();
  116. return false;
  117. }
  118. // 获取指针
  119. DllTool* pDllPtr = GetTool();
  120. if (pDllPtr == nullptr)
  121. {
  122. qDebug() << "[Error] ToolDepository::ParseToolsFromDll - [" << strDllName << "] GetTool failed, Reason:"
  123. << dllTool.errorString();
  124. return false;
  125. }
  126. // 初始化工具信息
  127. // 2021-8-2 改成了将dll中的参数值传递进exe中,否则event带不进来
  128. pDllPtr->InitTool();
  129. // 获取工具描述
  130. const DLL_TOOL_DESC& toolDescriotion = pDllPtr->Description();
  131. STATIC_TOOL newStaticTool;
  132. newStaticTool.Type = toolDescriotion.Type;
  133. newStaticTool.strName = toolDescriotion.strName;
  134. newStaticTool.strAliasName = toolDescriotion.strAliasName;
  135. newStaticTool.strCategory = toolDescriotion.strCategory;
  136. newStaticTool.strVersion = toolDescriotion.strVersion;
  137. newStaticTool.strDllPath = strDllPath;
  138. newStaticTool.strInfo = toolDescriotion.strInfo;
  139. // 对于某些未设置别名的工具,就将工具名称设置为别名
  140. if (newStaticTool.strAliasName.isEmpty())
  141. {
  142. newStaticTool.strAliasName = newStaticTool.strName;
  143. }
  144. // 获取工具接口信息
  145. for (int i = 0; i < pDllPtr->GetInterfaceSize(); i++)
  146. {
  147. const DLL_INF& dllinf = pDllPtr->Interface(i);
  148. STATIC_INTERFACE newStaticInf;
  149. newStaticInf.strName = dllinf.strName;
  150. newStaticInf.Direction = dllinf.Direction;
  151. newStaticInf.Discard = dllinf.Discard;
  152. newStaticInf.Type = dllinf.Type;
  153. // 2021-8-2 ,将value改成了从dll中全部复制,否则dll中的event带不进来
  154. //newStaticInf.value = dllinf.value;
  155. //newStaticInf.value.Ptr = nullptr;
  156. // 2022-9-19,如果是Event类型的接口,此处要把Dll中的Event拷贝进来
  157. // MEMO:值在这里拷贝没有意义,因为执行到这里的时候,Tool那边是采用空值初始化的,这里的Value值都是空的
  158. // 这里无需复制,在PouScene中添加完工具执行 pNewTool->pDllPtr->InitTool() 的时候才是带值初始化
  159. // 而且原则上exe中是不保存dll中的值的,所以需要用到dll中值的时候应该直接过去取,而不是拷贝到exe中
  160. // 所以Event的值是在Task绑定event句柄的时候,直接从dll中取值了,而不是从这里拷贝过来
  161. //if (newStaticInf.Type == INF_TYPE::INF_TYPE_EVENT)
  162. //{
  163. // newStaticInf.value.Ptr = dllinf.value.Ptr;
  164. //}
  165. // newStaticInf.eventTrigger = dllinf.eventTrigger;
  166. newStaticInf.value.passMode = dllinf.value.passMode;
  167. newStaticInf.value.type = dllinf.value.type;
  168. newStaticInf.nIndex = i;
  169. newStaticInf.bEnable = dllinf.bEnable;
  170. newStaticTool.staticInterfaces.push_back(newStaticInf);
  171. }
  172. //newTool.hIcon = (HICON)::LoadImage(hDllTool, MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE);
  173. //g_imagelist->Add(newTool.hIcon);
  174. // 调用ReleaseToolPtr接口
  175. typedef void (*RELEASETOOL)();
  176. RELEASETOOL ReleaseTool = (RELEASETOOL)dllTool.resolve(FUNCTION_RELEASETOOL);
  177. if (ReleaseTool == nullptr)
  178. {
  179. // CUtility::CriticalMessageBox(_T("获取ReleaseToolPtr 失败!\n\n%s"), strDllPath.GetString());
  180. qDebug() << "[Error] ToolDepository::ParseToolsFromDll - [" << strDllName << "] ReleaseTool failed, Reason:" << dllTool.errorString();
  181. return false;
  182. }
  183. // 2021-3-29 增加,只加入标准类型工具
  184. if (newStaticTool.isStandardTool())
  185. {
  186. // 添加到工具集中
  187. AddToCategory(newStaticTool, toolDescriotion.strCategory);
  188. m_toolDllPaths.insert(newStaticTool.strName, newStaticTool.strDllPath);
  189. qDebug() << "ToolDepository::ParseToolsFromDll - Add [" << strDllName << "] to toolbox.";
  190. }
  191. else
  192. {
  193. qDebug() << "ToolDepository::ParseToolsFromDll - Ignore [" << strDllName << "], Reason: is not standard tool.";
  194. }
  195. // 释放工具内存
  196. ReleaseTool();
  197. // dllTool.unload();
  198. return true;
  199. }
  200. /////////////////////////////////////////////////////////////
  201. // 将工具信息添加到Category中
  202. bool ToolDepository::AddToCategory(const STATIC_TOOL& newTool, const QString& strCategoryName)
  203. {
  204. // 找到对应的Category,添加
  205. for (int i = 0; i < m_TotalTools.size(); i++)
  206. {
  207. TOOL_CATEGORY& cate = m_TotalTools[i];
  208. if (cate.strName.compare(strCategoryName) == 0)
  209. {
  210. cate.staticTools.push_back(newTool);
  211. return true;
  212. }
  213. }
  214. // 如果没找到,则建一个新的工具集
  215. TOOL_CATEGORY cate;
  216. cate.strName = strCategoryName;
  217. cate.staticTools.push_back(newTool);
  218. m_TotalTools.push_back(cate);
  219. return true;
  220. }
  221. /// <summary>
  222. /// 2021-3-29增加,直接在内部生成port工具,不从外部加载了
  223. /// </summary>
  224. void ToolDepository::createPortTool()
  225. {
  226. // Output
  227. m_toolPortOutput.Type = TOOL_TYPE::TOOL_TYPE_PORT_OUTPUT;
  228. m_toolPortOutput.strName = ("OutputPort");;
  229. m_toolPortOutput.strAliasName = m_toolPortOutput.strName;
  230. m_toolPortOutput.strVersion = ("2.0");
  231. m_toolPortOutput.strDllPath = ("");
  232. m_toolPortOutput.strInfo = ("Output port tool.");
  233. STATIC_INTERFACE newStaticInf;
  234. newStaticInf.strName = ("");
  235. newStaticInf.Direction = INF_DIRECTION::INF_DIR_OUT;
  236. newStaticInf.Type = INF_TYPE::INF_TYPE_UNKNOWN;
  237. newStaticInf.value.passMode = VALUE_PASS_MODE::PASS_BY_UNKNOWN;
  238. newStaticInf.value.type = VALUE_TYPE::Type_Unknown;
  239. newStaticInf.value.Ptr = nullptr;
  240. newStaticInf.nIndex = 0;
  241. newStaticInf.bEnable = true;
  242. m_toolPortOutput.staticInterfaces.push_back(newStaticInf);
  243. AddToCategory(m_toolPortOutput, BUILDIN_CATEGORY_NAME);
  244. // Input
  245. m_toolPortInput.Type = TOOL_TYPE::TOOL_TYPE_PORT_INPUT;
  246. m_toolPortInput.strName = ("InputPort");;
  247. m_toolPortInput.strAliasName = m_toolPortInput.strName;
  248. m_toolPortInput.strVersion = ("2.0");
  249. m_toolPortInput.strDllPath = ("");
  250. m_toolPortInput.strInfo = ("Input port tool.");
  251. newStaticInf.strName = ("");
  252. newStaticInf.Direction = INF_DIRECTION::INF_DIR_IN;
  253. newStaticInf.Type = INF_TYPE::INF_TYPE_UNKNOWN;
  254. newStaticInf.value.passMode = VALUE_PASS_MODE::PASS_BY_UNKNOWN;
  255. newStaticInf.value.type = VALUE_TYPE::Type_Unknown;
  256. newStaticInf.value.Ptr = nullptr;
  257. newStaticInf.nIndex = 0;
  258. newStaticInf.bEnable = true;
  259. m_toolPortInput.staticInterfaces.push_back(newStaticInf);
  260. AddToCategory(m_toolPortInput, BUILDIN_CATEGORY_NAME);
  261. }
  262. /// <summary>
  263. /// 2022-3-13 内置注释工具
  264. /// </summary>
  265. void ToolDepository::createCommentTool()
  266. {
  267. m_toolComment.Type = TOOL_TYPE::TOOL_TYPE_COMMENT;
  268. m_toolComment.strName = ("Comment");;
  269. m_toolComment.strAliasName = m_toolComment.strName;
  270. m_toolComment.strVersion = ("1.0");
  271. m_toolComment.strDllPath = ("");
  272. m_toolComment.strInfo = ("DoubleClick to edit content.");
  273. AddToCategory(m_toolComment, BUILDIN_CATEGORY_NAME);
  274. }
  275. /// <summary>
  276. /// 2022-3-22 生成内置的Goto工具
  277. /// </summary>
  278. void ToolDepository::createGotoTool()
  279. {
  280. m_toolGoto.Type = TOOL_TYPE::TOOL_TYPE_GOTO;
  281. m_toolGoto.strName = ("Goto");;
  282. m_toolGoto.strAliasName = m_toolGoto.strName;
  283. m_toolGoto.strVersion = ("1.0");
  284. m_toolGoto.strDllPath = ("");
  285. m_toolGoto.strInfo = ("");
  286. // 输入接口固定为bool
  287. STATIC_INTERFACE newStaticInf;
  288. newStaticInf.strName = ("bool"); // 虽然设置了,但是不显示输入接口名称
  289. newStaticInf.Direction = INF_DIRECTION::INF_DIR_IN;
  290. newStaticInf.Type = INF_TYPE::INF_TYPE_STANDARD;
  291. newStaticInf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE;
  292. newStaticInf.value.type = VALUE_TYPE::Type_Bool;
  293. // 输入接口默认值为false
  294. // m_bDefaultGotoInput = false;
  295. // newStaticInf.value.Ptr = (void**)&m_bDefaultGotoInput;
  296. newStaticInf.value.setValue<bool>(false);
  297. newStaticInf.nIndex = 0;
  298. newStaticInf.bEnable = true;
  299. newStaticInf.bShowName = false; // Goto的输入接口不显示名字
  300. m_toolGoto.staticInterfaces.push_back(newStaticInf);
  301. // 输出接口为空,等待绑定
  302. newStaticInf.reset();
  303. newStaticInf.strName = ("Output");
  304. newStaticInf.Direction = INF_DIRECTION::INF_DIR_OUT;
  305. newStaticInf.Type = INF_TYPE::INF_TYPE_TOOL; // 设置为Tool的输出接口,用于链接Tool
  306. newStaticInf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE;
  307. newStaticInf.value.type = VALUE_TYPE::Type_Tool; // 设置为Tool类型,表示连接到Tool类型的接口
  308. newStaticInf.value.Ptr = nullptr;
  309. newStaticInf.nIndex = 0;
  310. newStaticInf.bEnable = true;
  311. newStaticInf.bShowName = false; // Goto的输出接口默认不显示名字,绑定后才显示
  312. m_toolGoto.staticInterfaces.push_back(newStaticInf);
  313. AddToCategory(m_toolGoto, BUILDIN_CATEGORY_NAME);
  314. }
  315. /// <summary>
  316. /// 2022-4-18 生成内置的并行计算工具
  317. /// </summary>
  318. void ToolDepository::createParallelTool()
  319. {
  320. // 工具信息
  321. m_toolParallel.Type = TOOL_TYPE::TOOL_TYPE_PARALLEL;
  322. m_toolParallel.strName = ("Parallel");;
  323. m_toolParallel.strAliasName = m_toolParallel.strName;
  324. m_toolParallel.strVersion = ("1.0");
  325. m_toolParallel.strDllPath = ("");
  326. m_toolParallel.strInfo = ("");
  327. // 接口信息
  328. STATIC_INTERFACE newStaticInf;
  329. // 2022-4-28 屏蔽,为了和标准工具统一,输入接口移至到运行时动态增加
  330. // 输入接口(用于绑定Goto,Top类型)
  331. //newStaticInf.strName = ("Top.In"); // 虽然设置了,但是不显示输入接口名称
  332. //newStaticInf.Direction = INF_DIRECTION::INF_DIR_IN;
  333. //newStaticInf.Type = INF_TYPE::INF_TYPE_TOP;
  334. //newStaticInf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE;
  335. //newStaticInf.value.type = VALUE_TYPE::Type_Tool; // 设置为Tool类型,表示只能接收Goto连接
  336. //newStaticInf.value.Ptr = nullptr;
  337. //newStaticInf.nIndex = 0;
  338. //newStaticInf.bEnable = true;
  339. //newStaticInf.bShowName = false; // 并行工具的输入接口不显示名字
  340. //m_toolParallel.staticInterfaces.push_back(newStaticInf);
  341. //newStaticInf.reset();
  342. //newStaticInf.strName = ("Tool.End");
  343. //newStaticInf.Direction = INF_DIRECTION::INF_DIR_OUT;
  344. //newStaticInf.Type = INF_TYPE::INF_TYPE_TOOL; // 设置为Tool的输出接口,用于链接Tool
  345. //newStaticInf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE;
  346. //newStaticInf.value.type = VALUE_TYPE::Type_Tool; // 设置为Tool类型,表示连接到Tool类型的接口
  347. //newStaticInf.value.Ptr = nullptr;
  348. //newStaticInf.nIndex = 0;
  349. //newStaticInf.bEnable = true;
  350. //newStaticInf.bShowName = false; // 输出接口默认不显示名字
  351. //m_toolParallel.staticInterfaces.push_back(newStaticInf);
  352. AddToCategory(m_toolParallel, BUILDIN_CATEGORY_NAME);
  353. }
  354. /// <summary>
  355. /// 2022-8-22 生成内置的For循环工具
  356. /// </summary>
  357. void ToolDepository::createForLoopTool()
  358. {
  359. // 工具信息
  360. m_toolForLoop.Type = TOOL_TYPE::TOOL_TYPE_FORLOOP;
  361. m_toolForLoop.strName = ("ForLoop");;
  362. m_toolForLoop.strAliasName = m_toolForLoop.strName;
  363. m_toolForLoop.strVersion = ("1.0");
  364. m_toolForLoop.strDllPath = ("");
  365. m_toolForLoop.strInfo = ("");
  366. // 接口信息
  367. STATIC_INTERFACE newStaticInf;
  368. //// 默认ToolEnd接口,用于链接循环工具
  369. //newStaticInf.strName = ("Tool.End");
  370. //newStaticInf.Direction = INF_DIRECTION::INF_DIR_OUT;
  371. //newStaticInf.Type = INF_TYPE::INF_TYPE_TOOL; // 设置为Tool的输出接口,用于链接Tool
  372. //newStaticInf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE;
  373. //newStaticInf.value.type = VALUE_TYPE::Type_Tool; // 设置为Tool类型,表示连接到Tool类型的接口
  374. //newStaticInf.value.Ptr = nullptr;
  375. //newStaticInf.nIndex = 1;
  376. //newStaticInf.bEnable = true;
  377. //newStaticInf.bShowName = false; // 输出接口默认不显示名字
  378. //m_toolForLoop.staticInterfaces.push_back(newStaticInf);
  379. // 默认size输入接口,用于确定循环体的执行次数
  380. newStaticInf.strName = ("size"); // size接口名称
  381. newStaticInf.Direction = INF_DIRECTION::INF_DIR_IN;
  382. newStaticInf.Type = INF_TYPE::INF_TYPE_STANDARD;
  383. newStaticInf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE;
  384. newStaticInf.value.type = VALUE_TYPE::Type_Int;
  385. // 输入接口不需要默认值,全靠上级接口Link进来
  386. // newStaticInf.value.setValue<int>(0);
  387. newStaticInf.nIndex = 0;
  388. newStaticInf.bEnable = true;
  389. newStaticInf.bShowName = true; // 显示接口名称
  390. m_toolForLoop.staticInterfaces.push_back(newStaticInf);
  391. // 默认index输出接口,用于输出当前For循环的index
  392. newStaticInf.strName = ("index"); // Index接口名称
  393. newStaticInf.Direction = INF_DIRECTION::INF_DIR_OUT;
  394. newStaticInf.Type = INF_TYPE::INF_TYPE_STANDARD;
  395. newStaticInf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE;
  396. newStaticInf.value.type = VALUE_TYPE::Type_Int;
  397. //// 输出接口默认值为0(不需要默认值,运行时再设置)
  398. //newStaticInf.value.setValue<int>(0);
  399. newStaticInf.nIndex = 1;
  400. newStaticInf.bEnable = true;
  401. newStaticInf.bShowName = true; // 显示接口名称
  402. m_toolForLoop.staticInterfaces.push_back(newStaticInf);
  403. AddToCategory(m_toolForLoop, BUILDIN_CATEGORY_NAME);
  404. }
  405. /// <summary>
  406. /// 2022-9-2 生成内置的等待Event工具
  407. /// </summary>
  408. void ToolDepository::createWaitTool()
  409. {
  410. // 工具信息
  411. m_toolWait.Type = TOOL_TYPE::TOOL_TYPE_WAIT;
  412. m_toolWait.strName = ("Wait");;
  413. m_toolWait.strAliasName = m_toolForLoop.strName;
  414. m_toolWait.strVersion = ("1.0");
  415. m_toolWait.strDllPath = ("");
  416. m_toolWait.strInfo = ("");
  417. // 接口信息
  418. STATIC_INTERFACE newStaticInf;
  419. // 默认waitValue输出接口,用于输出工具当前等待的值
  420. newStaticInf.strName = ("waitValue"); // 接口名称
  421. newStaticInf.Direction = INF_DIRECTION::INF_DIR_OUT;
  422. newStaticInf.Type = INF_TYPE::INF_TYPE_STANDARD;
  423. newStaticInf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE;
  424. newStaticInf.value.type = VALUE_TYPE::Type_Int;
  425. // 接口默认值
  426. // newStaticInf.value.setValue<int>(0);
  427. newStaticInf.nIndex = 0;
  428. newStaticInf.bEnable = true;
  429. newStaticInf.bShowName = true; // 显示接口名称
  430. m_toolWait.staticInterfaces.push_back(newStaticInf);
  431. // 默认waitTime输出接口,用于输出当前启动后等待的总时长
  432. newStaticInf.strName = ("waitTime"); // 接口名称
  433. newStaticInf.Direction = INF_DIRECTION::INF_DIR_OUT;
  434. newStaticInf.Type = INF_TYPE::INF_TYPE_STANDARD;
  435. newStaticInf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE;
  436. newStaticInf.value.type = VALUE_TYPE::Type_Int;
  437. //// 输出接口默认值为0(不需要默认值,运行时再设置)
  438. //newStaticInf.value.setValue<int>(0);
  439. newStaticInf.nIndex = 1;
  440. newStaticInf.bEnable = true;
  441. newStaticInf.bShowName = true; // 显示接口名称
  442. m_toolWait.staticInterfaces.push_back(newStaticInf);
  443. AddToCategory(m_toolWait, BUILDIN_CATEGORY_NAME);
  444. }
  445. /// <summary>
  446. /// 根据名称,获取工具 (根据名字或者曾用名)
  447. /// </summary>
  448. /// <param name="strName"></param>
  449. /// <returns></returns>
  450. TOOL* ToolDepository::getToolByName(QString strName)
  451. {
  452. TOOL* pTool = nullptr;
  453. for (int i = 0; i < toolDepository.m_TotalTools.size(); i++)
  454. {
  455. const TOOL_CATEGORY& cate = toolDepository.m_TotalTools[i];
  456. for (int j = 0; j < cate.staticTools.size(); j++)
  457. {
  458. const STATIC_TOOL& tool = cate.staticTools[j];
  459. if (tool.strName == strName || tool.strAliasName == strName)
  460. {
  461. pTool = new TOOL(&tool);
  462. }
  463. }
  464. }
  465. return pTool;
  466. }
  467. /// <summary>
  468. /// 获取指定DLL的指针
  469. /// </summary>
  470. /// <param name="strToolName"></param>
  471. /// <returns></returns>
  472. DllTool* ToolDepository::GetToolPtr(const QString& strToolName)
  473. {
  474. const QString& strDllPath = GetDllPathByName(strToolName);
  475. if (strDllPath.isEmpty())
  476. {
  477. return nullptr;
  478. }
  479. // 加载指定dll
  480. QLibrary dllTool(strDllPath);
  481. if (!dllTool.load())
  482. {
  483. return nullptr;
  484. }
  485. // 调用GetToolPtr接口
  486. typedef DllTool* (*GETTOOL)();
  487. GETTOOL GetTool = (GETTOOL)dllTool.resolve(FUNCTION_GETTOOL);
  488. if (GetTool == nullptr)
  489. {
  490. return nullptr;
  491. }
  492. // 获取指针
  493. DllTool* pDllPtr = GetTool();
  494. if (pDllPtr == nullptr)
  495. {
  496. return nullptr;
  497. }
  498. return pDllPtr;
  499. }
  500. /// <summary>
  501. /// 获取指定DLL的全路径
  502. /// </summary>
  503. /// <param name="strToolName"></param>
  504. /// <returns></returns>
  505. QString ToolDepository::GetDllPathByName(const QString& strToolName)
  506. {
  507. if (m_toolDllPaths.contains(strToolName))
  508. {
  509. return m_toolDllPaths.value(strToolName);
  510. }
  511. else
  512. {
  513. return "";
  514. }
  515. }
  516. ////////////////////////////////////////////////
  517. //// 释放工具dll
  518. //void CToolManager::FreeToolsLibrary()
  519. //{
  520. // for (unsigned int i = 0; i < m_TotalTools.size(); i++)
  521. // {
  522. // TOOL_CATEGORY& cate = m_TotalTools[i];
  523. //
  524. // for (unsigned int j = 0; j < cate.Tools.size(); j++)
  525. // {
  526. // TOOL& mtool = cate.Tools[j];
  527. // mtool.Interfaces.clear();
  528. //
  529. // // RELEASE_BUFFER(mtool.pDllPtr);
  530. //
  531. // //// 加载dll
  532. // //if (mtool.hDllTool)
  533. // //{
  534. // // // 调用Release接口
  535. // // typedef void(*RELEASE)();
  536. // // RELEASE Release = (RELEASE)GetProcAddress(mtool.hDllTool, FUNCTION_RELEASE);
  537. // // if (Release)
  538. // // {
  539. // // Release();
  540. // // }
  541. //
  542. // FreeLibrary(mtool.hDllTool);
  543. // //}
  544. // }
  545. // cate.Tools.clear();
  546. //
  547. // }
  548. // m_TotalTools.clear();
  549. //}
  550. //
  551. ////////////////////////////////////////////////////////
  552. //// 检测Tool的有效性
  553. //bool ToolDepository::ValidateTool(const TOOL& toolInfo)
  554. //{
  555. // // 加载Dll
  556. // QLibrary dllTool(toolInfo.strDll);
  557. // if (!dllTool.load())
  558. // {
  559. // // CUtility::CriticalMessageBox(_T("如下工具缺失,加载存档失败!\n\n%s"), toolInfo.strDll);
  560. // return false;
  561. // }
  562. //
  563. // // 调用Description接口
  564. // typedef const DLL_TOOL_DESC& (*DESCRIPTION)();
  565. // DESCRIPTION Description = (DESCRIPTION)dllTool.resolve(FUNCTION_DESCRIPTION);
  566. // if (Description == nullptr)
  567. // {
  568. // // 获取版本号失败
  569. // // CUtility::CriticalMessageBox(_T("获取版本号失败!\n\n%s"), toolInfo.strDll);
  570. // return false;
  571. // }
  572. //
  573. // // 校验版本号
  574. // const DLL_TOOL_DESC& toolDescriotion = Description();
  575. // if (toolInfo->strVersion.compare(toolDescriotion.strVersion) != 0)
  576. // {
  577. // //// 版本号不一致
  578. // //CUtility::CriticalMessageBox(_T("版本号不一致!\n\n%s \r\n需要版本号为:%s\r\n本地文件版本号为:%s"),
  579. // // toolInfo.strDll, toolInfo.strVersion, toolDescriotion.strVersion);
  580. //
  581. // return false;
  582. // }
  583. //
  584. // // FreeLibrary(hDllTool);
  585. //
  586. // return true;
  587. //}