CDynLibHandler.cpp 4.2 KB

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