CGameInterface.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "StdInc.h"
  2. #include "CGameInterface.h"
  3. #include "BattleState.h"
  4. #include "VCMIDirs.h"
  5. #ifdef VCMI_WINDOWS
  6. #include <windows.h> //for .dll libs
  7. #else
  8. #include <dlfcn.h>
  9. #endif
  10. #include "Connection.h"
  11. /*
  12. * CGameInterface.cpp, part of VCMI engine
  13. *
  14. * Authors: listed in file AUTHORS in main folder
  15. *
  16. * License: GNU General Public License v2.0 or later
  17. * Full text of license available in license.txt file, in main folder
  18. *
  19. */
  20. #ifdef VCMI_ANDROID
  21. // we can't use shared libraries on Android so here's a hack
  22. extern "C" DLL_EXPORT void VCAI_GetAiName(char* name);
  23. extern "C" DLL_EXPORT void VCAI_GetNewAI(shared_ptr<CGlobalAI> &out);
  24. extern "C" DLL_EXPORT void StupidAI_GetAiName(char* name);
  25. extern "C" DLL_EXPORT void StupidAI_GetNewBattleAI(shared_ptr<CGlobalAI> &out);
  26. extern "C" DLL_EXPORT void BattleAI_GetAiName(char* name);
  27. extern "C" DLL_EXPORT void BattleAI_GetNewBattleAI(shared_ptr<CBattleGameInterface> &out);
  28. #endif
  29. template<typename rett>
  30. shared_ptr<rett> createAny(const boost::filesystem::path& libpath, const std::string& methodName)
  31. {
  32. typedef void(*TGetAIFun)(shared_ptr<rett>&);
  33. typedef void(*TGetNameFun)(char*);
  34. char temp[150];
  35. TGetAIFun getAI = nullptr;
  36. TGetNameFun getName = nullptr;
  37. #ifndef VCMI_WINDOWS
  38. std::string dllname = libpath.string();
  39. // I don't know other platforms.
  40. // Somebody should remove it soon.
  41. #endif
  42. #ifdef VCMI_ANDROID
  43. // this is awful but it seems using shared libraries on some devices is even worse
  44. if (dllname.find("libVCAI.so") != std::string::npos) {
  45. getName = (TGetNameFun)VCAI_GetAiName;
  46. getAI = (TGetAIFun)VCAI_GetNewAI;
  47. } else if (dllname.find("libStupidAI.so") != std::string::npos) {
  48. getName = (TGetNameFun)StupidAI_GetAiName;
  49. getAI = (TGetAIFun)StupidAI_GetNewBattleAI;
  50. } else if (dllname.find("libBattleAI.so") != std::string::npos) {
  51. getName = (TGetNameFun)BattleAI_GetAiName;
  52. getAI = (TGetAIFun)BattleAI_GetNewBattleAI;
  53. } else {
  54. throw std::runtime_error("Don't know what to do with " + dllname + " and method " + methodName);
  55. }
  56. #else
  57. #ifdef VCMI_WINDOWS
  58. HMODULE dll = LoadLibraryW(libpath.c_str());
  59. if (dll)
  60. {
  61. getName = (TGetNameFun)GetProcAddress(dll, "GetAiName");
  62. getAI = (TGetAIFun)GetProcAddress(dll, methodName.c_str());
  63. }
  64. #else
  65. void *dll = dlopen(dllname.c_str(), RTLD_LOCAL | RTLD_LAZY);
  66. if (dll)
  67. {
  68. getName = (TGetNameFun)dlsym(dll,"GetAiName");
  69. getAI = (TGetAIFun)dlsym(dll,methodName.c_str());
  70. }
  71. else
  72. logGlobal->errorStream() << "Error: " << dlerror();
  73. #endif
  74. if (!dll)
  75. {
  76. logGlobal->errorStream() << "Cannot open dynamic library ("<<libpath<<"). Throwing...";
  77. throw std::runtime_error("Cannot open dynamic library");
  78. }
  79. else if(!getName || !getAI)
  80. {
  81. logGlobal->errorStream() << libpath << " does not export method " << methodName;
  82. #ifdef VCMI_WINDOWS
  83. FreeLibrary(dll);
  84. #else
  85. dlclose(dll);
  86. #endif
  87. throw std::runtime_error("Cannot find method " + methodName);
  88. }
  89. #endif // VCMI_ANDROID
  90. getName(temp);
  91. logGlobal->infoStream() << "Loaded " << temp;
  92. shared_ptr<rett> ret;
  93. getAI(ret);
  94. if(!ret)
  95. logGlobal->errorStream() << "Cannot get AI!";
  96. return ret;
  97. }
  98. template<typename rett>
  99. shared_ptr<rett> createAnyAI(std::string dllname, const std::string& methodName)
  100. {
  101. logGlobal->infoStream() << "Opening " << dllname;
  102. const boost::filesystem::path filePath =
  103. VCMIDirs::get().libraryPath() / "AI" / VCMIDirs::get().libraryName(dllname);
  104. auto ret = createAny<rett>(filePath, methodName);
  105. ret->dllName = std::move(dllname);
  106. return ret;
  107. }
  108. shared_ptr<CGlobalAI> CDynLibHandler::getNewAI(std::string dllname)
  109. {
  110. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  111. }
  112. shared_ptr<CBattleGameInterface> CDynLibHandler::getNewBattleAI(std::string dllname )
  113. {
  114. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  115. }
  116. shared_ptr<CScriptingModule> CDynLibHandler::getNewScriptingModule(std::string dllname)
  117. {
  118. return createAny<CScriptingModule>(dllname, "GetNewModule");
  119. }
  120. BattleAction CGlobalAI::activeStack( const CStack * stack )
  121. {
  122. BattleAction ba; ba.actionType = Battle::DEFEND;
  123. ba.stackNumber = stack->ID;
  124. return ba;
  125. }
  126. CGlobalAI::CGlobalAI()
  127. {
  128. human = false;
  129. }
  130. void CAdventureAI::battleNewRound(int round)
  131. {
  132. battleAI->battleNewRound(round);
  133. }
  134. void CAdventureAI::battleCatapultAttacked(const CatapultAttack & ca)
  135. {
  136. battleAI->battleCatapultAttacked(ca);
  137. }
  138. void CAdventureAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side)
  139. {
  140. assert(!battleAI);
  141. assert(cbc);
  142. battleAI = CDynLibHandler::getNewBattleAI(getBattleAIName());
  143. battleAI->init(cbc);
  144. battleAI->battleStart(army1, army2, tile, hero1, hero2, side);
  145. }
  146. void CAdventureAI::battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa)
  147. {
  148. battleAI->battleStacksAttacked(bsa);
  149. }
  150. void CAdventureAI::actionStarted(const BattleAction &action)
  151. {
  152. battleAI->actionStarted(action);
  153. }
  154. void CAdventureAI::battleNewRoundFirst(int round)
  155. {
  156. battleAI->battleNewRoundFirst(round);
  157. }
  158. void CAdventureAI::actionFinished(const BattleAction &action)
  159. {
  160. battleAI->actionFinished(action);
  161. }
  162. void CAdventureAI::battleStacksEffectsSet(const SetStackEffect & sse)
  163. {
  164. battleAI->battleStacksEffectsSet(sse);
  165. }
  166. void CAdventureAI::battleStacksRemoved(const BattleStacksRemoved & bsr)
  167. {
  168. battleAI->battleStacksRemoved(bsr);
  169. }
  170. void CAdventureAI::battleObstaclesRemoved(const std::set<si32> & removedObstacles)
  171. {
  172. battleAI->battleObstaclesRemoved(removedObstacles);
  173. }
  174. void CAdventureAI::battleNewStackAppeared(const CStack * stack)
  175. {
  176. battleAI->battleNewStackAppeared(stack);
  177. }
  178. void CAdventureAI::battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance)
  179. {
  180. battleAI->battleStackMoved(stack, dest, distance);
  181. }
  182. void CAdventureAI::battleAttack(const BattleAttack *ba)
  183. {
  184. battleAI->battleAttack(ba);
  185. }
  186. void CAdventureAI::battleSpellCast(const BattleSpellCast *sc)
  187. {
  188. battleAI->battleSpellCast(sc);
  189. }
  190. void CAdventureAI::battleEnd(const BattleResult *br)
  191. {
  192. battleAI->battleEnd(br);
  193. battleAI.reset();
  194. }
  195. void CAdventureAI::battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom)
  196. {
  197. battleAI->battleStacksHealedRes(healedStacks, lifeDrain, tentHeal, lifeDrainFrom);
  198. }
  199. BattleAction CAdventureAI::activeStack(const CStack * stack)
  200. {
  201. return battleAI->activeStack(stack);
  202. }
  203. void CAdventureAI::yourTacticPhase(int distance)
  204. {
  205. battleAI->yourTacticPhase(distance);
  206. }
  207. void CAdventureAI::saveGame(COSer<CSaveFile> &h, const int version) /*saving */
  208. {
  209. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  210. CGlobalAI::saveGame(h, version);
  211. bool hasBattleAI = static_cast<bool>(battleAI);
  212. h << hasBattleAI;
  213. if(hasBattleAI)
  214. {
  215. h << std::string(battleAI->dllName);
  216. battleAI->saveGame(h, version);
  217. }
  218. }
  219. void CAdventureAI::loadGame(CISer<CLoadFile> &h, const int version) /*loading */
  220. {
  221. LOG_TRACE_PARAMS(logAi, "version '%i'", version);
  222. CGlobalAI::loadGame(h, version);
  223. bool hasBattleAI = false;
  224. h >> hasBattleAI;
  225. if(hasBattleAI)
  226. {
  227. std::string dllName;
  228. h >> dllName;
  229. battleAI = CDynLibHandler::getNewBattleAI(dllName);
  230. assert(cbc); //it should have been set by the one who new'ed us
  231. battleAI->init(cbc);
  232. //battleAI->loadGame(h, version);
  233. }
  234. }
  235. void CBattleGameInterface::saveGame(COSer<CSaveFile> &h, const int version)
  236. {
  237. }
  238. void CBattleGameInterface::loadGame(CISer<CLoadFile> &h, const int version)
  239. {
  240. }