CGameInterface.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. if (!dll)
  28. {
  29. tlog1 << "Cannot open AI library ("<<dllname<<"). Throwing..."<<std::endl;
  30. throw new std::string("Cannot open AI library");
  31. }
  32. getName = (void(*)(char*))dlsym(dll,"GetAiName");
  33. getAI = (CGlobalAI*(*)())dlsym(dll,"GetNewAI");
  34. #endif
  35. getName(temp);
  36. tlog0 << "Loaded .dll with AI named " << temp << std::endl;
  37. ret = getAI();
  38. if(!ret)
  39. tlog1 << "Cannot get AI!\n";
  40. ret->dllName = dllname;
  41. return ret;
  42. }