modulemanager.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // MIT License
  2. // Copyright(c) 2024 FastWeb - fwlua.com - nianhua
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files(the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions :
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. // ## Additional Terms for Commercial Use
  19. // This software is licensed for personal, educational, and non - commercial use.
  20. // For commercial use or use within a company, organization, or institution, a
  21. // separate commercial license is required.To obtain a commercial license,
  22. // please contact
  23. // EMail:1585346868@qq.com
  24. // Mobile:17367918735
  25. // QQ:1585346868
  26. #include "modulemanager.h"
  27. #include "util/file.h"
  28. #include "core/app.h"
  29. #include "core/config.h"
  30. #include "core/global.h"
  31. #ifdef _WIN32
  32. #include <Windows.h>
  33. #else
  34. #endif
  35. #include "module/http/request.h"
  36. #include "module/http/response.h"
  37. #include "module/http/session.h"
  38. #include "module/http/httpclient.h"
  39. #include "module/http/interceptor.h"
  40. #include "module/http/subscribe.h"
  41. #include "module/globalfuns.h"
  42. #include "module/mutex.h"
  43. #include "module/codec.h"
  44. #include "module/time.h"
  45. #include "module/filesystem.h"
  46. #include "module/sys.h"
  47. #include "module/timer.h"
  48. #include "module/process.h"
  49. #include "module/ini.h"
  50. fastweb::module_manager::module_manager(fastweb::app* app):Interface(app)
  51. {
  52. }
  53. fastweb::module_manager::~module_manager()
  54. {
  55. }
  56. void fastweb::module_manager::start()
  57. {
  58. #ifdef _WIN32
  59. auto get_last_error_desc = []()->std::string {
  60. DWORD dwError = GetLastError();
  61. // 获取错误描述
  62. char* lpMsgBuf = nullptr;
  63. FormatMessageA(
  64. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  65. FORMAT_MESSAGE_FROM_SYSTEM |
  66. FORMAT_MESSAGE_IGNORE_INSERTS,
  67. NULL,
  68. dwError,
  69. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  70. (LPTSTR)&lpMsgBuf,
  71. 0,
  72. NULL
  73. );
  74. // 输出错误描述
  75. return std::string(lpMsgBuf);
  76. };
  77. #endif
  78. close();
  79. auto ms = modules();
  80. for (size_t i = 0; i < ms.size(); i++)
  81. {
  82. module_info mi;
  83. std::string mod_filepath = app()->config->scripts.module_dir + "/" + ms[i];
  84. #ifdef _WIN32
  85. SetDllDirectoryA(ylib::file::parent_dir(mod_filepath).c_str());
  86. mi.dll = LoadLibrary(mod_filepath.c_str());
  87. if (mi.dll == nullptr)
  88. {
  89. LOG_ERROR("module loading failed, filename: " + mod_filepath
  90. +","+ codec::to_utf8(get_last_error_desc())
  91. );
  92. continue;
  93. }
  94. mi.func = (fastweb_module_regist)GetProcAddress((HMODULE)mi.dll, "fastweb_module_regist");
  95. if (mi.func == nullptr) {
  96. LOG_ERROR("function not found: `fastweb_module_regist`, filename: " + mod_filepath);
  97. FreeLibrary((HMODULE)mi.dll);
  98. continue;
  99. }
  100. m_modules.emplace(mod_filepath, mi);
  101. /*if (api_func(lua, lua->lua_state()) == 0)
  102. {
  103. LOG_INFO("successfully regist module, filename: " + mod_filepath);
  104. continue;
  105. }
  106. LOG_ERROR("regist module failed, filename: " + mod_filepath);*/
  107. #else
  108. #endif
  109. }
  110. }
  111. void fastweb::module_manager::close()
  112. {
  113. for_iter(iter, m_modules)
  114. {
  115. #ifdef _WIN32
  116. FreeLibrary((HMODULE)iter->second.dll);
  117. #else
  118. #endif
  119. }
  120. m_modules.clear();
  121. }
  122. void fastweb::module_manager::load(sol::state* lua)
  123. {
  124. load_core(lua);
  125. load_lualib(lua);
  126. load_3rdparty(lua);
  127. }
  128. void fastweb::module_manager::load_core(sol::state* lua)
  129. {
  130. lua->open_libraries(
  131. sol::lib::base,
  132. sol::lib::package,
  133. sol::lib::math,
  134. sol::lib::string,
  135. sol::lib::table,
  136. sol::lib::utf8,
  137. sol::lib::bit32,
  138. sol::lib::coroutine,
  139. sol::lib::count,
  140. sol::lib::ffi,
  141. sol::lib::io,
  142. sol::lib::jit,
  143. sol::lib::os
  144. );
  145. module::request::regist(lua);
  146. module::response::regist(lua);
  147. module::interceptor::regist(lua);
  148. module::subscribe::regist(lua);
  149. module::session::regist(lua);
  150. module::httpclient::regist(lua);
  151. module::globalfuncs::regist(lua);
  152. module::mutex::regist(lua);
  153. module::auto_lock::regist(lua);
  154. module::codec::regist(lua);
  155. module::time::regist(lua);
  156. module::filesystem::regist(lua);
  157. module::sys::regist(lua);
  158. module::timer::regist(lua);
  159. module::ini::regist(lua);
  160. module::process::regist(lua);
  161. app()->global->regist(lua);
  162. }
  163. void fastweb::module_manager::load_3rdparty(sol::state* lua)
  164. {
  165. for_iter(iter, m_modules)
  166. {
  167. if (iter->second.func(lua, lua->lua_state()) != 0)
  168. {
  169. LOG_ERROR("egist module failed, filename: "+iter->first);
  170. }
  171. }
  172. }
  173. void fastweb::module_manager::load_lualib(sol::state* lua)
  174. {
  175. // 获取当前的package.path,添加新的搜索路径
  176. std::string current_path = (*lua)["package"]["path"]; // 获取当前的路径
  177. //for (size_t i = 0; i < app()->config->scripts.lib_dir.size(); i++)
  178. // current_path += ";" + app()->config->scripts.lib_dir[i] + "/?.lua"; // 添加新的路径
  179. current_path += ";" + app()->config->website.dir + "/?.lua"; // 添加新的路径
  180. (*lua)["package"]["path"] = current_path; // 设置修改后的路径
  181. }
  182. std::vector<std::string> fastweb::module_manager::modules()
  183. {
  184. std::vector<std::string> results;
  185. auto luas = ylib::file::traverse(app()->config->scripts.module_dir, "(.*\\.dll)");
  186. for_iter(iter, luas)
  187. {
  188. if (iter->second == IS_DIRECTORY)
  189. continue;
  190. std::string path = strutils::replace(iter->first, '\\', '/');
  191. if (path.find("/") != -1)
  192. {
  193. if (strutils::split(path, '/').size() != 2)
  194. continue; //多级不支持
  195. if (ylib::file::parent_dir(path) != ylib::file::filename(path, false))
  196. continue;
  197. }
  198. results.push_back(path);
  199. }
  200. return results;
  201. }