2
0

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