NETCore.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include <ThirdParty/SDL/include/SDL.h>
  2. #include <Atomic/IO/FileSystem.h>
  3. #include <Atomic/IO/Log.h>
  4. #include <Atomic/Core/StringUtils.h>
  5. #include "NETCore.h"
  6. namespace Atomic
  7. {
  8. #ifdef ATOMIC_PLATFORM_OSX
  9. static const char * const sCoreClrDll = "libcoreclr.dylib";
  10. #else
  11. static const char * const sCoreClrDll = "libcoreclr.so";
  12. #endif
  13. // Prototype of the coreclr_initialize function from the libcoreclr.so
  14. typedef int (*InitializeCoreCLRFunction)(
  15. const char* exePath,
  16. const char* appDomainFriendlyName,
  17. int propertyCount,
  18. const char** propertyKeys,
  19. const char** propertyValues,
  20. void** hostHandle,
  21. unsigned int* domainId);
  22. // Prototype of the coreclr_shutdown function from the libcoreclr.so
  23. typedef int (*ShutdownCoreCLRFunction)(
  24. void* hostHandle,
  25. unsigned int domainId);
  26. // Prototype of the coreclr_execute_assembly function from the libcoreclr.so
  27. typedef int (*ExecuteAssemblyFunction)(
  28. void* hostHandle,
  29. unsigned int domainId,
  30. int argc,
  31. const char** argv,
  32. const char* managedAssemblyPath,
  33. unsigned int* exitCode);
  34. static InitializeCoreCLRFunction sInitializeCoreCLR = 0;
  35. static ExecuteAssemblyFunction sExecuteAssembly = 0;
  36. static ShutdownCoreCLRFunction sShutdownCoreCLR = 0;
  37. NETCore::NETCore(Context* context) :
  38. Object(context),
  39. coreCLRDLLHandle_(0),
  40. hostHandle_(0),
  41. domainId_(0)
  42. {
  43. }
  44. NETCore::~NETCore()
  45. {
  46. }
  47. void NETCore::GenerateTPAList(String& tpaList)
  48. {
  49. tpaList = String::EMPTY;
  50. FileSystem* fs = GetSubsystem<FileSystem>();
  51. Vector<String> results;
  52. fs->ScanDir(results, coreCLRFilesAbsPath_, "*.dll", SCAN_FILES, true);
  53. Vector<String> trustedAssemblies;
  54. for (unsigned i = 0; i < results.Size(); i++)
  55. {
  56. const String& assembly = results[i];
  57. // TODO: apply filtering if necessary
  58. trustedAssemblies.Push(coreCLRFilesAbsPath_ + "/" + assembly);
  59. }
  60. tpaList.Join(trustedAssemblies, ":");
  61. }
  62. void NETCore::Shutdown()
  63. {
  64. if (sShutdownCoreCLR && hostHandle_)
  65. {
  66. sShutdownCoreCLR(hostHandle_, domainId_);
  67. }
  68. if (coreCLRDLLHandle_)
  69. {
  70. SDL_UnloadObject(coreCLRDLLHandle_);
  71. }
  72. coreCLRDLLHandle_ = 0;
  73. hostHandle_ = 0;
  74. domainId_ = 0;
  75. sInitializeCoreCLR = 0;
  76. sExecuteAssembly = 0;
  77. sShutdownCoreCLR = 0;
  78. }
  79. bool NETCore::Initialize(const String &coreCLRFilesAbsPath, String& errorMsg)
  80. {
  81. coreCLRFilesAbsPath_ = coreCLRFilesAbsPath;
  82. String coreClrDllPath = AddTrailingSlash(coreCLRFilesAbsPath) + sCoreClrDll;
  83. coreCLRDLLHandle_ = SDL_LoadObject(coreClrDllPath.CString());
  84. if (coreCLRDLLHandle_ == NULL)
  85. {
  86. errorMsg = ToString("NETCore: Unable to load %s with error %s",
  87. coreClrDllPath.CString(),
  88. SDL_GetError());
  89. return false;
  90. }
  91. sInitializeCoreCLR = (InitializeCoreCLRFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_initialize");
  92. if (!sInitializeCoreCLR)
  93. {
  94. errorMsg = ToString("NETCore: Unable to get coreclr_initialize entry point in %s", coreClrDllPath.CString());
  95. return false;
  96. }
  97. sShutdownCoreCLR = (ShutdownCoreCLRFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_shutdown");
  98. if (!sShutdownCoreCLR)
  99. {
  100. errorMsg = ToString("NETCore: Unable to get coreclr_shutdown entry point in %s", coreClrDllPath.CString());
  101. return false;
  102. }
  103. sExecuteAssembly = (ExecuteAssemblyFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_execute_assembly");
  104. if (!sExecuteAssembly)
  105. {
  106. errorMsg = ToString("NETCore: Unable to get coreclr_execute_assembly entry point in %s", coreClrDllPath.CString());
  107. return false;
  108. }
  109. String tpaList;
  110. GenerateTPAList(tpaList);
  111. // Allowed property names:
  112. // APPBASE
  113. // - The base path of the application from which the exe and other assemblies will be loaded
  114. //
  115. // TRUSTED_PLATFORM_ASSEMBLIES
  116. // - The list of complete paths to each of the fully trusted assemblies
  117. //
  118. // APP_PATHS
  119. // - The list of paths which will be probed by the assembly loader
  120. //
  121. // APP_NI_PATHS
  122. // - The list of additional paths that the assembly loader will probe for ngen images
  123. //
  124. // NATIVE_DLL_SEARCH_DIRECTORIES
  125. // - The list of paths that will be probed for native DLLs called by PInvoke
  126. //
  127. const char *propertyKeys[] = {
  128. "TRUSTED_PLATFORM_ASSEMBLIES",
  129. "APP_PATHS",
  130. "APP_NI_PATHS",
  131. "NATIVE_DLL_SEARCH_DIRECTORIES",
  132. "AppDomainCompatSwitch"
  133. };
  134. String appPath = "";
  135. Vector<String> nativeSearch;
  136. nativeSearch.Push(coreCLRFilesAbsPath_);
  137. nativeSearch.Push("/Users/josh/Dev/atomic/AtomicGameEngineSharp-build/Source/AtomicNET/NETRuntime");
  138. String nativeDllSearchDirs;
  139. nativeDllSearchDirs.Join(nativeSearch, ":");
  140. const char *propertyValues[] = {
  141. // TRUSTED_PLATFORM_ASSEMBLIES
  142. tpaList.CString(),
  143. // APP_PATHS
  144. appPath.CString(),
  145. // APP_NI_PATHS
  146. appPath.CString(),
  147. // NATIVE_DLL_SEARCH_DIRECTORIES
  148. nativeDllSearchDirs.CString(),
  149. // AppDomainCompatSwitch
  150. "UseLatestBehaviorWhenTFMNotSpecified"
  151. };
  152. int st = sInitializeCoreCLR(
  153. "", // "/Users/josh/Desktop/OSX.x64.Debug/HelloWorld.exe",
  154. "NETCore",
  155. sizeof(propertyKeys) / sizeof(propertyKeys[0]),
  156. propertyKeys,
  157. propertyValues,
  158. &hostHandle_,
  159. &domainId_);
  160. if (st < 0)
  161. {
  162. errorMsg = ToString("NETCore: coreclr_initialize failed - status: 0x%08x\n", st);
  163. return false;
  164. }
  165. /*
  166. unsigned int exitCode;
  167. st = sExecuteAssembly(
  168. hostHandle_,
  169. domainId_,
  170. 0,
  171. 0,
  172. "/Users/josh/Desktop/OSX.x64.Debug/HelloWorld.exe",
  173. (unsigned int*)&exitCode);
  174. */
  175. return true;
  176. }
  177. }