NETCore.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. /*
  9. mcs /nostdlib /noconfig /r:System.Console.dll /r:System.Runtime.dll /r:System.IO.dll /r:System.IO.FileSystem.dll /r:mscorlib.dll HelloWorld.cs
  10. */
  11. #ifdef ATOMIC_PLATFORM_OSX
  12. static const char * const sCoreClrDll = "libcoreclr.dylib";
  13. #else
  14. static const char * const sCoreClrDll = "libcoreclr.so";
  15. #endif
  16. // Prototype of the coreclr_initialize function from the libcoreclr.so
  17. typedef int (*InitializeCoreCLRFunction)(
  18. const char* exePath,
  19. const char* appDomainFriendlyName,
  20. int propertyCount,
  21. const char** propertyKeys,
  22. const char** propertyValues,
  23. void** hostHandle,
  24. unsigned int* domainId);
  25. // Prototype of the coreclr_shutdown function from the libcoreclr.so
  26. typedef int (*ShutdownCoreCLRFunction)(
  27. void* hostHandle,
  28. unsigned int domainId);
  29. // Prototype of the coreclr_execute_assembly function from the libcoreclr.so
  30. typedef int (*ExecuteAssemblyFunction)(
  31. void* hostHandle,
  32. unsigned int domainId,
  33. int argc,
  34. const char** argv,
  35. const char* managedAssemblyPath,
  36. unsigned int* exitCode);
  37. typedef int (*CreateDelegateFunction)(
  38. void* hostHandle,
  39. unsigned int domainId,
  40. const char* entryPointAssemblyName,
  41. const char* entryPointTypeName,
  42. const char* entryPointMethodName,
  43. void** delegate);
  44. static InitializeCoreCLRFunction sInitializeCoreCLR = 0;
  45. static ExecuteAssemblyFunction sExecuteAssembly = 0;
  46. static CreateDelegateFunction sCreateDelegate = 0;
  47. static ShutdownCoreCLRFunction sShutdownCoreCLR = 0;
  48. NETCore::NETCore(Context* context) :
  49. Object(context),
  50. coreCLRDLLHandle_(0),
  51. hostHandle_(0),
  52. domainId_(0)
  53. {
  54. }
  55. NETCore::~NETCore()
  56. {
  57. }
  58. void NETCore::GenerateTPAList(String& tpaList)
  59. {
  60. tpaList = String::EMPTY;
  61. FileSystem* fs = GetSubsystem<FileSystem>();
  62. Vector<String> results;
  63. fs->ScanDir(results, coreCLRFilesAbsPath_, "*.dll", SCAN_FILES, true);
  64. Vector<String> trustedAssemblies;
  65. for (unsigned i = 0; i < results.Size(); i++)
  66. {
  67. const String& assembly = results[i];
  68. // TODO: apply filtering if necessary
  69. trustedAssemblies.Push(coreCLRFilesAbsPath_ + assembly);
  70. }
  71. tpaList.Join(trustedAssemblies, ":");
  72. // LOGINFOF("NetCore:: TPALIST - %s", tpaList.CString());
  73. }
  74. void NETCore::Shutdown()
  75. {
  76. if (sShutdownCoreCLR && hostHandle_)
  77. {
  78. sShutdownCoreCLR(hostHandle_, domainId_);
  79. }
  80. if (coreCLRDLLHandle_)
  81. {
  82. SDL_UnloadObject(coreCLRDLLHandle_);
  83. }
  84. coreCLRDLLHandle_ = 0;
  85. hostHandle_ = 0;
  86. domainId_ = 0;
  87. sInitializeCoreCLR = 0;
  88. sExecuteAssembly = 0;
  89. sCreateDelegate = 0;
  90. sShutdownCoreCLR = 0;
  91. }
  92. bool NETCore::InitCoreCLRDLL(String& errorMsg)
  93. {
  94. String coreClrDllPath = AddTrailingSlash(coreCLRFilesAbsPath_) + sCoreClrDll;
  95. coreCLRDLLHandle_ = SDL_LoadObject(coreClrDllPath.CString());
  96. if (coreCLRDLLHandle_ == NULL)
  97. {
  98. errorMsg = ToString("NETCore: Unable to load %s with error %s",
  99. coreClrDllPath.CString(),
  100. SDL_GetError());
  101. return false;
  102. }
  103. sInitializeCoreCLR = (InitializeCoreCLRFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_initialize");
  104. if (!sInitializeCoreCLR)
  105. {
  106. errorMsg = ToString("NETCore: Unable to get coreclr_initialize entry point in %s", coreClrDllPath.CString());
  107. return false;
  108. }
  109. sShutdownCoreCLR = (ShutdownCoreCLRFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_shutdown");
  110. if (!sShutdownCoreCLR)
  111. {
  112. errorMsg = ToString("NETCore: Unable to get coreclr_shutdown entry point in %s", coreClrDllPath.CString());
  113. return false;
  114. }
  115. sCreateDelegate = (CreateDelegateFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_create_delegate");
  116. if (!sCreateDelegate)
  117. {
  118. errorMsg = ToString("NETCore: Unable to get coreclr_create_delegate entry point in %s", coreClrDllPath.CString());
  119. return false;
  120. }
  121. sExecuteAssembly = (ExecuteAssemblyFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_execute_assembly");
  122. if (!sExecuteAssembly)
  123. {
  124. errorMsg = ToString("NETCore: Unable to get coreclr_execute_assembly entry point in %s", coreClrDllPath.CString());
  125. return false;
  126. }
  127. return true;
  128. }
  129. bool NETCore::Initialize(const String &coreCLRFilesAbsPath, String& errorMsg)
  130. {
  131. coreCLRFilesAbsPath_ = AddTrailingSlash(coreCLRFilesAbsPath);
  132. if (!InitCoreCLRDLL(errorMsg))
  133. {
  134. Shutdown();
  135. return false;
  136. }
  137. // Allowed property names:
  138. // APPBASE
  139. // - The base path of the application from which the exe and other assemblies will be loaded
  140. //
  141. // TRUSTED_PLATFORM_ASSEMBLIES
  142. // - The list of complete paths to each of the fully trusted assemblies
  143. //
  144. // APP_PATHS
  145. // - The list of paths which will be probed by the assembly loader
  146. //
  147. // APP_NI_PATHS
  148. // - The list of additional paths that the assembly loader will probe for ngen images
  149. //
  150. // NATIVE_DLL_SEARCH_DIRECTORIES
  151. // - The list of paths that will be probed for native DLLs called by PInvoke
  152. //
  153. const char *propertyKeys[] = {
  154. "TRUSTED_PLATFORM_ASSEMBLIES",
  155. "APP_PATHS",
  156. "APP_NI_PATHS",
  157. "NATIVE_DLL_SEARCH_DIRECTORIES",
  158. "AppDomainCompatSwitch"
  159. };
  160. String tpaList;
  161. GenerateTPAList(tpaList);
  162. String appPath = "/Users/josh/Desktop/OSX.x64.Debug/";
  163. Vector<String> nativeSearch;
  164. nativeSearch.Push(coreCLRFilesAbsPath_);
  165. nativeSearch.Push("/Users/josh/Dev/atomic/AtomicGameEngineSharp-build/Source/AtomicNET/NETRuntime");
  166. String nativeDllSearchDirs;
  167. nativeDllSearchDirs.Join(nativeSearch, ":");
  168. const char *propertyValues[] = {
  169. // TRUSTED_PLATFORM_ASSEMBLIES
  170. tpaList.CString(),
  171. // APP_PATHS
  172. appPath.CString(),
  173. // APP_NI_PATHS
  174. appPath.CString(),
  175. // NATIVE_DLL_SEARCH_DIRECTORIES
  176. nativeDllSearchDirs.CString(),
  177. // AppDomainCompatSwitch
  178. "UseLatestBehaviorWhenTFMNotSpecified"
  179. };
  180. int st = sInitializeCoreCLR(
  181. "/Users/josh/Desktop/OSX.x64.Debug/HelloWorld.exe",
  182. "NETCore",
  183. sizeof(propertyKeys) / sizeof(propertyKeys[0]),
  184. propertyKeys,
  185. propertyValues,
  186. &hostHandle_,
  187. &domainId_);
  188. if (st < 0)
  189. {
  190. Shutdown();
  191. errorMsg = ToString("NETCore: coreclr_initialize failed - status: 0x%08x\n", st);
  192. return false;
  193. }
  194. /*
  195. void* hm;
  196. st = sCreateDelegate(hostHandle_,
  197. domainId_,
  198. "HelloWorld",
  199. "Hello1",
  200. "CallFromNative",
  201. &hm);
  202. if (st >= 0)
  203. {
  204. typedef void (*Hm)(const char* value);
  205. ((Hm)hm)("Hello From Native");
  206. }
  207. */
  208. /*
  209. unsigned int exitCode;
  210. st = sExecuteAssembly(
  211. hostHandle_,
  212. domainId_,
  213. 0,
  214. 0,
  215. "/Users/josh/Desktop/OSX.x64.Debug/HelloWorld.exe",
  216. (unsigned int*)&exitCode);
  217. */
  218. return true;
  219. }
  220. }