CGameInterface.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. std::string dllPath;
  24. #ifdef _WIN32
  25. dllPath = "AI/"+dllname+".dll";
  26. HINSTANCE dll = LoadLibraryA(dllPath.c_str());
  27. if (!dll)
  28. {
  29. tlog1 << "Cannot open AI library ("<<dllPath<<"). Throwing..."<<std::endl;
  30. throw new std::string("Cannot open AI library");
  31. }
  32. //int len = dllname.size()+1;
  33. getName = (void(*)(char*))GetProcAddress(dll,"GetAiName");
  34. getAI = (CGlobalAI*(*)())GetProcAddress(dll,"GetNewAI");
  35. #else
  36. dllPath = "AI/"+dllPath+".so";
  37. void *dll = dlopen(dllPath.c_str(), RTLD_LOCAL | RTLD_LAZY);
  38. if (!dll)
  39. {
  40. tlog1 << "Cannot open AI library ("<<dllPath<<"). Throwing..."<<std::endl;
  41. throw new std::string("Cannot open AI library");
  42. }
  43. getName = (void(*)(char*))dlsym(dll,"GetAiName");
  44. getAI = (CGlobalAI*(*)())dlsym(dll,"GetNewAI");
  45. #endif
  46. getName(temp);
  47. tlog0 << "Loaded AI named " << temp << std::endl;
  48. ret = getAI();
  49. if(!ret)
  50. tlog1 << "Cannot get AI!\n";
  51. ret->dllName = dllname;
  52. return ret;
  53. }