CGameInterface.cpp 1.6 KB

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