CGameInterface.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. CGlobalAI * ret=NULL;
  21. CGlobalAI*(*getAI)();
  22. void(*getName)(char*);
  23. #ifdef _WIN32
  24. dllname = "AI/"+dllname+".dll";
  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. dllname = "AI/"+dllname+".so";
  36. void *dll = dlopen(dllname.c_str(), RTLD_LOCAL | RTLD_LAZY);
  37. if (!dll)
  38. {
  39. tlog1 << "Cannot open AI library ("<<dllname<<"). Throwing..."<<std::endl;
  40. throw new std::string("Cannot open AI library");
  41. }
  42. getName = (void(*)(char*))dlsym(dll,"GetAiName");
  43. getAI = (CGlobalAI*(*)())dlsym(dll,"GetNewAI");
  44. #endif
  45. getName(temp);
  46. tlog0 << "Loaded AI named " << temp << std::endl;
  47. ret = getAI();
  48. if(!ret)
  49. tlog1 << "Cannot get AI!\n";
  50. ret->dllName = dllname;
  51. return ret;
  52. }