CDynLibHandler.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * CDynLibHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CDynLibHandler.h"
  12. #include "CGlobalAI.h"
  13. #include "../VCMIDirs.h"
  14. #ifdef STATIC_AI
  15. # ifdef ENABLE_NULLKILLER_AI
  16. # include "../../AI/Nullkiller/AIGateway.h"
  17. # endif
  18. # ifdef ENABLE_NULLKILLER2_AI
  19. # include "../../AI/Nullkiller2/AIGateway.h"
  20. # endif
  21. # ifdef ENABLE_BATTLE_AI
  22. # include "../../AI/BattleAI/BattleAI.h"
  23. # endif
  24. # ifdef ENABLE_STUPID_AI
  25. # include "../../AI/StupidAI/StupidAI.h"
  26. # endif
  27. # ifdef ENABLE_MMAI
  28. # include "../../AI/MMAI/MMAI.h"
  29. # endif
  30. # include "../../AI/EmptyAI/CEmptyAI.h"
  31. #else
  32. # ifdef VCMI_WINDOWS
  33. # include <windows.h> //for .dll libs
  34. # else
  35. # include <dlfcn.h>
  36. # endif // VCMI_WINDOWS
  37. #endif // STATIC_AI
  38. VCMI_LIB_NAMESPACE_BEGIN
  39. template<typename rett>
  40. std::shared_ptr<rett> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
  41. {
  42. #ifdef STATIC_AI
  43. // android currently doesn't support loading libs dynamically, so the access to the known libraries
  44. // is possible only via specializations of this template
  45. throw std::runtime_error("Could not resolve ai library " + libpath.generic_string());
  46. #else
  47. using TGetAIFun = void (*)(std::shared_ptr<rett> &);
  48. using TGetNameFun = void (*)(char *);
  49. char temp[150];
  50. TGetAIFun getAI = nullptr;
  51. TGetNameFun getName = nullptr;
  52. #ifdef VCMI_WINDOWS
  53. #ifdef __MINGW32__
  54. #pragma GCC diagnostic push
  55. #pragma GCC diagnostic ignored "-Wcast-function-type"
  56. #endif
  57. HMODULE dll = LoadLibraryExW(libpath.c_str(), NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  58. if (dll)
  59. {
  60. getName = reinterpret_cast<TGetNameFun>(GetProcAddress(dll, "GetAiName"));
  61. getAI = reinterpret_cast<TGetAIFun>(GetProcAddress(dll, methodName.c_str()));
  62. }
  63. #ifdef __MINGW32__
  64. #pragma GCC diagnostic pop
  65. #endif
  66. #else // !VCMI_WINDOWS
  67. void *dll = dlopen(libpath.string().c_str(), RTLD_LOCAL | RTLD_LAZY);
  68. if (dll)
  69. {
  70. getName = reinterpret_cast<TGetNameFun>(dlsym(dll, "GetAiName"));
  71. getAI = reinterpret_cast<TGetAIFun>(dlsym(dll, methodName.c_str()));
  72. }
  73. else
  74. {
  75. logGlobal->error("Cannot open dynamic library '%s'. Reason: %s", libpath.string(), dlerror());
  76. }
  77. #endif // VCMI_WINDOWS
  78. if (!dll)
  79. {
  80. logGlobal->error("Cannot open dynamic library (%s). Throwing...", libpath.string());
  81. throw std::runtime_error("Cannot open dynamic library");
  82. }
  83. else if(!getName || !getAI)
  84. {
  85. logGlobal->error("%s does not export method %s", libpath.string(), methodName);
  86. #ifdef VCMI_WINDOWS
  87. FreeLibrary(dll);
  88. #else
  89. dlclose(dll);
  90. #endif
  91. throw std::runtime_error("Cannot find method " + methodName);
  92. }
  93. getName(temp);
  94. logGlobal->info("Loaded %s", temp);
  95. std::shared_ptr<rett> ret;
  96. getAI(ret);
  97. if(!ret)
  98. logGlobal->error("Cannot get AI!");
  99. return ret;
  100. #endif // STATIC_AI
  101. }
  102. #ifdef STATIC_AI
  103. template<>
  104. std::shared_ptr<CGlobalAI> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
  105. {
  106. #ifdef ENABLE_NULLKILLER2_AI
  107. if(libpath.stem() == "libNullkiller2")
  108. return std::make_shared<NK2AI::AIGateway>();
  109. #endif
  110. #ifdef ENABLE_NULLKILLER_AI
  111. if(libpath.stem() == "libNullkiller")
  112. return std::make_shared<NKAI::AIGateway>();
  113. #endif
  114. return std::make_shared<CEmptyAI>();
  115. }
  116. template<>
  117. std::shared_ptr<CBattleGameInterface> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
  118. {
  119. #ifdef ENABLE_BATTLE_AI
  120. if(libpath.stem() == "libBattleAI")
  121. return std::make_shared<CBattleAI>();
  122. #endif
  123. #ifdef ENABLE_STUPID_AI
  124. if(libpath.stem() == "libStupidAI")
  125. return std::make_shared<CStupidAI>();
  126. #endif
  127. #ifdef ENABLE_MMAI
  128. if(libpath.stem() == "libMMAI")
  129. return std::make_shared<MMAI::BAI::Router>();
  130. #endif
  131. return std::make_shared<CEmptyAI>();
  132. }
  133. #endif // STATIC_AI
  134. template<typename rett>
  135. std::shared_ptr<rett> createAnyAI(const std::string & dllname, const std::string & methodName)
  136. {
  137. logGlobal->info("Opening %s", dllname);
  138. const boost::filesystem::path filePath = VCMIDirs::get().fullLibraryPath("AI", dllname);
  139. auto ret = createAny<rett>(filePath, methodName);
  140. ret->dllName = dllname;
  141. return ret;
  142. }
  143. std::shared_ptr<CGlobalAI> CDynLibHandler::getNewAI(const std::string & dllname)
  144. {
  145. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  146. }
  147. std::shared_ptr<CBattleGameInterface> CDynLibHandler::getNewBattleAI(const std::string & dllname)
  148. {
  149. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  150. }
  151. #if SCRIPTING_ENABLED
  152. std::shared_ptr<scripting::Module> CDynLibHandler::getNewScriptingModule(const boost::filesystem::path & dllname)
  153. {
  154. return createAny<scripting::Module>(dllname, "GetNewModule");
  155. }
  156. #endif
  157. VCMI_LIB_NAMESPACE_END