dllLoader.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #include "dllLoader.h"
  2. #include "pikaConfig.h"
  3. #include <logs/assert.h>
  4. #include <unordered_set>
  5. static std::filesystem::path dllPath = std::filesystem::current_path();
  6. #ifdef PIKA_DEVELOPMENT
  7. #ifdef PIKA_WINDOWS
  8. #define NOMINMAX
  9. #define WIN32_LEAN_AND_MEAN
  10. #include <Windows.h>
  11. static FILETIME getLastWriteFile(const char *name)
  12. {
  13. FILETIME time = {};
  14. WIN32_FILE_ATTRIBUTE_DATA Data = {};
  15. if (GetFileAttributesEx(name, GetFileExInfoStandard, &Data))
  16. {
  17. time = Data.ftLastWriteTime;
  18. }
  19. else
  20. {
  21. return {};
  22. }
  23. return(time);
  24. }
  25. #else
  26. #error "pika load dll works only on windows, change configuration to pikaProduction"
  27. #endif
  28. #endif
  29. bool pika::LoadedDll::constructRuntimeContainer(RuntimeContainer &c, const char *name)
  30. {
  31. PIKA_DEVELOPMENT_ONLY_ASSERT(constructContainer_ != nullptr, "dll not loaded");
  32. return constructContainer_(&c.pointer, &c.arena, name);
  33. }
  34. void pika::LoadedDll::bindAllocatorDllRealm(pika::memory::FreeListAllocator *allocator)
  35. {
  36. PIKA_DEVELOPMENT_ONLY_ASSERT(bindAllocator_ != nullptr, "dll not loaded");
  37. bindAllocator_(allocator);
  38. }
  39. void pika::LoadedDll::resetAllocatorDllRealm()
  40. {
  41. PIKA_DEVELOPMENT_ONLY_ASSERT(resetAllocator_ != nullptr, "dll not loaded");
  42. resetAllocator_();
  43. }
  44. void pika::LoadedDll::getContainerInfoAndCheck(pika::LogManager &logs)
  45. {
  46. containerInfo.clear();
  47. containerInfo.reserve(100);
  48. //todo check for valid containers
  49. getContainersInfo_(containerInfo);
  50. std::unordered_set<std::string> uniqueNames = {};
  51. for (int i = 0; i < containerInfo.size(); i++)
  52. {
  53. auto signalError = [&](const char *e)
  54. {
  55. std::string l = e + containerInfo[i].containerName;
  56. logs.log(l.c_str(), pika::logError);
  57. containerInfo.erase(containerInfo.begin() + i);
  58. i--;
  59. };
  60. if (uniqueNames.find(containerInfo[i].containerName) == uniqueNames.end())
  61. {
  62. uniqueNames.insert(containerInfo[i].containerName);
  63. }
  64. else
  65. {
  66. signalError("Duplicate container name: ");
  67. continue;
  68. }
  69. if (containerInfo[i].containerStaticInfo._internalNotImplemented)
  70. {
  71. signalError("Container did not implement containerInfo function: ");
  72. continue;
  73. }
  74. if (containerInfo[i].containerStaticInfo.defaultHeapMemorySize < 100)
  75. {
  76. signalError("Too little heap memory for container: ");
  77. continue;
  78. }
  79. }
  80. }
  81. #ifdef PIKA_DEVELOPMENT
  82. bool pika::LoadedDll::loadDll(int id, pika::LogManager &logs)
  83. {
  84. unloadDll();
  85. std::filesystem::path originalDll = dllPath / "pikaGameplay.dll";
  86. std::filesystem::path copyDll = dllPath / ( "pikaGameplayCopy" + std::to_string(id) + ".dll");
  87. filetime = getLastWriteFile(originalDll.string().c_str());
  88. if (filetime.dwLowDateTime == FILETIME().dwLowDateTime
  89. &&
  90. filetime.dwHighDateTime == FILETIME().dwHighDateTime
  91. ) { return false; }
  92. //std::filesystem::copy(originalDll, copyDll, std::filesystem::copy_options::overwrite_existing);
  93. if (!CopyFile(originalDll.string().c_str(), copyDll.string().c_str(), false) ) { return false; }
  94. dllHand = LoadLibraryA(copyDll.string().c_str());
  95. if (!dllHand) { return false; }
  96. gameplayStart_ = (gameplayStart_t *)GetProcAddress(dllHand, "gameplayStart");
  97. gameplayReload_ = (gameplayReload_t *)GetProcAddress(dllHand, "gameplayReload");
  98. getContainersInfo_ = (getContainersInfo_t *)GetProcAddress(dllHand, "getContainersInfo");
  99. constructContainer_ = (constructContainer_t *)GetProcAddress(dllHand, "constructContainer");
  100. destructContainer_ = (destructContainer_t *)GetProcAddress(dllHand, "destructContainer");
  101. bindAllocator_ = (bindAllocator_t *)GetProcAddress(dllHand, "bindAllocator");
  102. resetAllocator_ = (resetAllocator_t *)GetProcAddress(dllHand, "resetAllocator");
  103. dissableAllocators_ = (dissableAllocators_t *)GetProcAddress(dllHand, "dissableAllocators");
  104. if (!gameplayStart_) { return false; }
  105. if (!gameplayReload_) { return false; }
  106. if (!getContainersInfo_) { return false; }
  107. if (!constructContainer_) { return false; }
  108. if (!destructContainer_) { return false; }
  109. if (!bindAllocator_) { return false; }
  110. if (!resetAllocator_) { return false; }
  111. if (!dissableAllocators_) { return false; }
  112. //get container info
  113. getContainerInfoAndCheck(logs);
  114. this->id = id;
  115. return true;
  116. }
  117. bool pika::LoadedDll::checkIfDllIsOpenable()
  118. {
  119. HANDLE fileCheck = {};
  120. fileCheck = CreateFile((dllPath / "pikaGameplay.dll").string().c_str(),
  121. GENERIC_READ | GENERIC_WRITE, NULL, NULL,
  122. OPEN_EXISTING, 0, NULL);
  123. if (fileCheck == INVALID_HANDLE_VALUE)
  124. {
  125. return false;
  126. }
  127. else
  128. {
  129. CloseHandle(fileCheck);
  130. return true;
  131. }
  132. }
  133. bool pika::LoadedDll::tryToloadDllUntillPossible(int id, pika::LogManager &logs,
  134. std::chrono::duration<long long> timeout)
  135. {
  136. auto startTime = std::chrono::steady_clock::now();
  137. while (!checkIfDllIsOpenable())
  138. {
  139. if (timeout != std::chrono::seconds(0))
  140. {
  141. if (std::chrono::steady_clock::now() > startTime + timeout)
  142. {
  143. return false; //timeout
  144. }
  145. }
  146. //Wait till the dll can be oppened. It is possible that the compiler still keeps it busy.
  147. }
  148. unloadDll();
  149. //try to load (we loop since it is still possible that windows thinks that the dll is not available yet)
  150. while (!loadDll(id, logs))
  151. {
  152. if (timeout != std::chrono::seconds(0))
  153. {
  154. if (std::chrono::steady_clock::now() > startTime + timeout)
  155. {
  156. return false; //timeout
  157. }
  158. }
  159. };
  160. return true;
  161. }
  162. void pika::LoadedDll::unloadDll()
  163. {
  164. if (dllHand == 0) { return; }
  165. //dissableAllocators_();
  166. resetAllocatorDllRealm();
  167. FreeLibrary(dllHand);
  168. dllHand = {};
  169. filetime = {};
  170. containerInfo.clear();
  171. }
  172. bool pika::LoadedDll::shouldReloadDll()
  173. {
  174. if (dllHand == 0) { return 0; }
  175. std::filesystem::path originalDll = dllPath / "pikaGameplay.dll";
  176. FILETIME newFiletime = getLastWriteFile(originalDll.string().c_str());
  177. if (filetime.dwLowDateTime == FILETIME().dwLowDateTime
  178. &&
  179. filetime.dwHighDateTime == FILETIME().dwHighDateTime
  180. )
  181. {
  182. return false;
  183. }
  184. return (CompareFileTime(&filetime, &newFiletime) != 0);
  185. }
  186. #elif defined(PIKA_PRODUCTION)
  187. #include <dll/dllMain.h>
  188. bool pika::LoadedDll::loadDll(int id, pika::LogManager &logs)
  189. {
  190. gameplayStart_ = gameplayStart;
  191. gameplayReload_ = gameplayReload;
  192. getContainersInfo_ = getContainersInfo;
  193. constructContainer_ = constructContainer;
  194. destructContainer_ = destructContainer;
  195. bindAllocator_ = bindAllocator;
  196. resetAllocator_ = resetAllocator;
  197. dissableAllocators_ = dissableAllocators;
  198. getContainerInfoAndCheck(logs);
  199. this->id = id;
  200. return true;
  201. }
  202. bool pika::LoadedDll::tryToloadDllUntillPossible(int id, pika::LogManager &logs,
  203. std::chrono::duration<long long> timeout)
  204. {
  205. return loadDll(id, logs);
  206. }
  207. void pika::LoadedDll::unloadDll()
  208. {
  209. containerInfo.clear();
  210. }
  211. bool pika::LoadedDll::shouldReloadDll()
  212. {
  213. return false;
  214. }
  215. bool pika::LoadedDll::checkIfDllIsOpenable()
  216. {
  217. return true;
  218. }
  219. #endif