CGameInterface.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "stdafx.h"
  2. #include "CGameInterface.h"
  3. #ifdef _WIN32
  4. #include <windows.h> //for .dll libs
  5. #else
  6. #include <dlfcn.h>
  7. #endif
  8. /*
  9. * CGameInterface.cpp, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  17. CGlobalAI * CAIHandler::getNewAI(CCallback * cb, std::string dllname)
  18. {
  19. char temp[50];
  20. dllname = "AI/"+dllname;
  21. CGlobalAI * ret=NULL;
  22. CGlobalAI*(*getAI)();
  23. void(*getName)(char*);
  24. #ifdef _WIN32
  25. HINSTANCE dll = LoadLibraryA(dllname.c_str());
  26. if (!dll)
  27. {
  28. tlog1 << "Cannot open AI library ("<<dllname<<"). Throwing..."<<std::endl;
  29. throw new std::string("Cannot open AI library");
  30. }
  31. //int len = dllname.size()+1;
  32. getName = (void(*)(char*))GetProcAddress(dll,"GetAiName");
  33. getAI = (CGlobalAI*(*)())GetProcAddress(dll,"GetNewAI");
  34. #else
  35. void *dll = dlopen(dllname.c_str(), RTLD_LOCAL | RTLD_LAZY);
  36. if (!dll)
  37. {
  38. tlog1 << "Cannot open AI library ("<<dllname<<"). Throwing..."<<std::endl;
  39. throw new std::string("Cannot open AI library");
  40. }
  41. getName = (void(*)(char*))dlsym(dll,"GetAiName");
  42. getAI = (CGlobalAI*(*)())dlsym(dll,"GetNewAI");
  43. #endif
  44. getName(temp);
  45. tlog0 << "Loaded .dll with AI named " << temp << std::endl;
  46. ret = getAI();
  47. if(!ret)
  48. tlog1 << "Cannot get AI!\n";
  49. ret->dllName = dllname;
  50. return ret;
  51. }