CDynLibHandler.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. # include "../../AI/VCAI/VCAI.h"
  16. # include "../../AI/Nullkiller/AIGateway.h"
  17. # include "../../AI/Nullkiller2/AIGateway.h"
  18. # include "../../AI/BattleAI/BattleAI.h"
  19. # include "../../AI/StupidAI/StupidAI.h"
  20. # include "../../AI/EmptyAI/CEmptyAI.h"
  21. #else
  22. # ifdef VCMI_WINDOWS
  23. # include <windows.h> //for .dll libs
  24. # else
  25. # include <dlfcn.h>
  26. # endif // VCMI_WINDOWS
  27. #endif // STATIC_AI
  28. VCMI_LIB_NAMESPACE_BEGIN
  29. template<typename rett>
  30. std::shared_ptr<rett> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
  31. {
  32. #ifdef STATIC_AI
  33. // android currently doesn't support loading libs dynamically, so the access to the known libraries
  34. // is possible only via specializations of this template
  35. throw std::runtime_error("Could not resolve ai library " + libpath.generic_string());
  36. #else
  37. using TGetAIFun = void (*)(std::shared_ptr<rett> &);
  38. using TGetNameFun = void (*)(char *);
  39. char temp[150];
  40. TGetAIFun getAI = nullptr;
  41. TGetNameFun getName = nullptr;
  42. #ifdef VCMI_WINDOWS
  43. #ifdef __MINGW32__
  44. #pragma GCC diagnostic push
  45. #pragma GCC diagnostic ignored "-Wcast-function-type"
  46. #endif
  47. HMODULE dll = LoadLibraryW(libpath.c_str());
  48. if (dll)
  49. {
  50. getName = reinterpret_cast<TGetNameFun>(GetProcAddress(dll, "GetAiName"));
  51. getAI = reinterpret_cast<TGetAIFun>(GetProcAddress(dll, methodName.c_str()));
  52. }
  53. #ifdef __MINGW32__
  54. #pragma GCC diagnostic pop
  55. #endif
  56. #else // !VCMI_WINDOWS
  57. void *dll = dlopen(libpath.string().c_str(), RTLD_LOCAL | RTLD_LAZY);
  58. if (dll)
  59. {
  60. getName = reinterpret_cast<TGetNameFun>(dlsym(dll, "GetAiName"));
  61. getAI = reinterpret_cast<TGetAIFun>(dlsym(dll, methodName.c_str()));
  62. }
  63. else
  64. {
  65. logGlobal->error("Cannot open dynamic library '%s'. Reason: %s", libpath.string(), dlerror());
  66. }
  67. #endif // VCMI_WINDOWS
  68. if (!dll)
  69. {
  70. logGlobal->error("Cannot open dynamic library (%s). Throwing...", libpath.string());
  71. throw std::runtime_error("Cannot open dynamic library");
  72. }
  73. else if(!getName || !getAI)
  74. {
  75. logGlobal->error("%s does not export method %s", libpath.string(), methodName);
  76. #ifdef VCMI_WINDOWS
  77. FreeLibrary(dll);
  78. #else
  79. dlclose(dll);
  80. #endif
  81. throw std::runtime_error("Cannot find method " + methodName);
  82. }
  83. getName(temp);
  84. logGlobal->info("Loaded %s", temp);
  85. std::shared_ptr<rett> ret;
  86. getAI(ret);
  87. if(!ret)
  88. logGlobal->error("Cannot get AI!");
  89. return ret;
  90. #endif // STATIC_AI
  91. }
  92. #ifdef STATIC_AI
  93. template<>
  94. std::shared_ptr<CGlobalAI> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
  95. {
  96. if(libpath.stem() == "libNullkiller2") {
  97. return std::make_shared<NK2AI::AIGateway>();
  98. }
  99. else if(libpath.stem() == "libNullkiller") {
  100. return std::make_shared<NKAI::AIGateway>();
  101. }
  102. else{
  103. return std::make_shared<VCAI>();
  104. }
  105. }
  106. template<>
  107. std::shared_ptr<CBattleGameInterface> createAny(const boost::filesystem::path & libpath, const std::string & methodName)
  108. {
  109. if(libpath.stem() == "libBattleAI")
  110. return std::make_shared<CBattleAI>();
  111. else if(libpath.stem() == "libStupidAI")
  112. return std::make_shared<CStupidAI>();
  113. return std::make_shared<CEmptyAI>();
  114. }
  115. #endif // STATIC_AI
  116. template<typename rett>
  117. std::shared_ptr<rett> createAnyAI(const std::string & dllname, const std::string & methodName)
  118. {
  119. logGlobal->info("Opening %s", dllname);
  120. const boost::filesystem::path filePath = VCMIDirs::get().fullLibraryPath("AI", dllname);
  121. auto ret = createAny<rett>(filePath, methodName);
  122. ret->dllName = dllname;
  123. return ret;
  124. }
  125. std::shared_ptr<CGlobalAI> CDynLibHandler::getNewAI(const std::string & dllname)
  126. {
  127. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  128. }
  129. std::shared_ptr<CBattleGameInterface> CDynLibHandler::getNewBattleAI(const std::string & dllname)
  130. {
  131. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  132. }
  133. #if SCRIPTING_ENABLED
  134. std::shared_ptr<scripting::Module> CDynLibHandler::getNewScriptingModule(const boost::filesystem::path & dllname)
  135. {
  136. return createAny<scripting::Module>(dllname, "GetNewModule");
  137. }
  138. #endif
  139. VCMI_LIB_NAMESPACE_END