CGameInterface.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "stdafx.h"
  2. #include "CGameInterface.h"
  3. #include "lib/BattleState.h"
  4. #ifdef _WIN32
  5. #define WIN32_LEAN_AND_MEAN //excludes rarely used stuff from windows headers - delete this line if something is missing
  6. #include <windows.h> //for .dll libs
  7. #else
  8. #include <dlfcn.h>
  9. #endif
  10. /*
  11. * CGameInterface.cpp, part of VCMI engine
  12. *
  13. * Authors: listed in file AUTHORS in main folder
  14. *
  15. * License: GNU General Public License v2.0 or later
  16. * Full text of license available in license.txt file, in main folder
  17. *
  18. */
  19. template<typename rett>
  20. rett * createAny(std::string dllname, std::string methodName)
  21. {
  22. char temp[50];
  23. rett * ret=NULL;
  24. rett*(*getAI)();
  25. void(*getName)(char*);
  26. #ifdef _WIN32
  27. HINSTANCE dll = LoadLibraryA(dllname.c_str());
  28. if (dll)
  29. {
  30. getName = (void(*)(char*))GetProcAddress(dll,"GetAiName");
  31. getAI = (rett*(*)())GetProcAddress(dll,methodName.c_str());
  32. }
  33. #else
  34. void *dll = dlopen(dllname.c_str(), RTLD_LOCAL | RTLD_LAZY);
  35. if (dll)
  36. {
  37. getName = (void(*)(char*))dlsym(dll,"GetAiName");
  38. getAI = (rett*(*)())dlsym(dll,methodName.c_str());
  39. }
  40. #endif
  41. if (!dll)
  42. {
  43. tlog1 << "Cannot open dynamic library ("<<dllname<<"). Throwing..."<<std::endl;
  44. throw new std::string("Cannot open dynamic library");
  45. }
  46. getName(temp);
  47. tlog0 << "Loaded " << temp << std::endl;
  48. ret = getAI();
  49. if(!ret)
  50. tlog1 << "Cannot get AI!\n";
  51. return ret;
  52. }
  53. //Currently AI libraries use "lib" prefix only on non-win systems.
  54. //May be applied to Win systems as well to remove this ifdef
  55. #ifdef _WIN32
  56. std::string getAIFileName(std::string input)
  57. {
  58. return input + '.' + LIB_EXT;
  59. }
  60. #else
  61. std::string getAIFileName(std::string input)
  62. {
  63. return "lib" + input + '.' + LIB_EXT;
  64. }
  65. #endif
  66. template<typename rett>
  67. rett * createAnyAI(std::string dllname, std::string methodName)
  68. {
  69. std::string filename = getAIFileName(dllname);
  70. rett* ret = createAny<rett>(LIB_DIR "/AI/" + filename, methodName);
  71. ret->dllName = filename;
  72. return ret;
  73. }
  74. CGlobalAI * CDynLibHandler::getNewAI(std::string dllname)
  75. {
  76. return createAnyAI<CGlobalAI>(dllname, "GetNewAI");
  77. }
  78. CBattleGameInterface * CDynLibHandler::getNewBattleAI(std::string dllname )
  79. {
  80. return createAnyAI<CBattleGameInterface>(dllname, "GetNewBattleAI");
  81. }
  82. CScriptingModule * CDynLibHandler::getNewScriptingModule(std::string dllname)
  83. {
  84. return createAny<CScriptingModule>(dllname, "GetNewModule");
  85. }
  86. BattleAction CGlobalAI::activeStack( const CStack * stack )
  87. {
  88. BattleAction ba; ba.actionType = BattleAction::DEFEND;
  89. ba.stackNumber = stack->ID;
  90. return ba;
  91. }