CGameInterface.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. CGlobalAI * CAIHandler::getNewAI(CCallback * cb, std::string dllname)
  9. {
  10. char temp[50];
  11. dllname = "AI/"+dllname;
  12. CGlobalAI * ret=NULL;
  13. CGlobalAI*(*getAI)();
  14. void(*getName)(char*);
  15. #ifdef _WIN32
  16. HINSTANCE dll = LoadLibraryA(dllname.c_str());
  17. if (!dll)
  18. {
  19. tlog1 << "Cannot open AI library ("<<dllname<<"). Throwing..."<<std::endl;
  20. throw new std::string("Cannot open AI library");
  21. }
  22. //int len = dllname.size()+1;
  23. getName = (void(*)(char*))GetProcAddress(dll,"GetAiName");
  24. getAI = (CGlobalAI*(*)())GetProcAddress(dll,"GetNewAI");
  25. #else
  26. void *dll = dlopen(dllname.c_str(), RTLD_LOCAL | RTLD_LAZY);
  27. getName = (void(*)(char*))dlsym(dll,"GetAiName");
  28. getAI = (CGlobalAI*(*)())dlsym(dll,"GetNewAI");
  29. ; //TODO: handle AI library on Linux
  30. #endif
  31. getName(temp);
  32. tlog0 << "Loaded .dll with AI named " << temp << std::endl;
  33. ret = getAI();
  34. return ret;
  35. }