dllLoader.cpp 7.2 KB

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