dllLoader.cpp 7.0 KB

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