NETCore.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include <Atomic/Core/StringUtils.h>
  2. #include <Atomic/Core/CoreEvents.h>
  3. #include <Atomic/IO/FileSystem.h>
  4. #include <Atomic/IO/Log.h>
  5. #include <Atomic/Resource/ResourceCache.h>
  6. #include "NETCore.h"
  7. #ifdef ATOMIC_PLATFORM_WINDOWS
  8. #include "Platforms/Windows/NETHostWindows.h"
  9. #else
  10. #include "Platforms/Unix/NETHostUnix.h"
  11. #endif
  12. namespace Atomic
  13. {
  14. WeakPtr<Context> NETCore::csContext_;
  15. WeakPtr<NETCore> NETCore::instance_;
  16. NETCore::NETCore(Context* context) :
  17. Object(context)
  18. {
  19. assert(!instance_);
  20. instance_ = this;
  21. csContext_ = context;
  22. }
  23. NETCore::~NETCore()
  24. {
  25. Shutdown();
  26. instance_ = NULL;
  27. }
  28. void NETCore::Shutdown()
  29. {
  30. RefCounted::SetRefCountedDeletedFunction(0);
  31. if (netHost_.NotNull())
  32. {
  33. netHost_->Shutdown();
  34. netHost_ = 0;
  35. }
  36. }
  37. extern "C"
  38. {
  39. // http://ybeernet.blogspot.com/2011/03/techniques-of-calling-unmanaged-code.html
  40. // pinvoke is faster than [UnmanagedFunctionPointer] :/
  41. // [SuppressUnmanagedCodeSecurity] <--- add this attribute, in any event
  42. #ifdef ATOMIC_PLATFORM_WINDOWS
  43. #pragma warning(disable: 4244) // possible loss of data
  44. #define ATOMIC_EXPORT_API __declspec(dllexport)
  45. #else
  46. #define ATOMIC_EXPORT_API
  47. #endif
  48. ATOMIC_EXPORT_API ClassID csb_Atomic_RefCounted_GetClassID(RefCounted* refCounted)
  49. {
  50. if (!refCounted)
  51. return 0;
  52. return refCounted->GetClassID();
  53. }
  54. ATOMIC_EXPORT_API RefCounted* csb_AtomicEngine_GetSubsystem(const char* name)
  55. {
  56. return NETCore::GetContext()->GetSubsystem(name);
  57. }
  58. ATOMIC_EXPORT_API void csb_AtomicEngine_ReleaseRef(RefCounted* ref)
  59. {
  60. if (!ref)
  61. return;
  62. ref->ReleaseRef();
  63. }
  64. }
  65. void NETCore::WaitForDebuggerConnect()
  66. {
  67. netHost_->WaitForDebuggerConnect();
  68. }
  69. void NETCore::AddAssemblyLoadPath(const String& assemblyPath)
  70. {
  71. typedef void (*AddAssemblyLoadPathFunction)(const char* assemblyPath);
  72. AddAssemblyLoadPathFunction addAssemblyLoadPath;
  73. bool result = CreateDelegate(
  74. "AtomicNETBootstrap",
  75. "Atomic.Bootstrap.AtomicLoadContext",
  76. "AddAssemblyLoadPath",
  77. (void**) &addAssemblyLoadPath);
  78. if (result)
  79. {
  80. addAssemblyLoadPath(assemblyPath.CString());
  81. }
  82. }
  83. int NETCore::ExecAssembly(const String& assemblyName, const Vector<String>& args)
  84. {
  85. return netHost_->ExecAssembly(assemblyName, args);
  86. }
  87. bool NETCore::CreateDelegate(const String& assemblyName, const String& qualifiedClassName, const String& methodName, void** funcOut)
  88. {
  89. return netHost_->CreateDelegate(assemblyName, qualifiedClassName, methodName, funcOut);
  90. }
  91. bool NETCore::Initialize(String& errorMsg)
  92. {
  93. #ifdef ATOMIC_PLATFORM_WINDOWS
  94. netHost_ = new NETHostWindows(context_);
  95. #else
  96. netHost_ = new NETHostUnix(context_);
  97. #endif
  98. if (!netHost_->Initialize())
  99. {
  100. errorMsg = "NETHost Failed to Initialize";
  101. return false;
  102. }
  103. // MOVE THIS!
  104. typedef void (*StartupFunction)(const char* assemblyLoadPaths);
  105. StartupFunction startup;
  106. // The coreclr binding model will become locked upon loading the first assembly that is not on the TPA list, or
  107. // upon initializing the default context for the first time. For this test, test assemblies are located alongside
  108. // corerun, and hence will be on the TPA list. So, we should be able to set the default context once successfully,
  109. // and fail on the second try.
  110. // AssemblyLoadContext
  111. // https://github.com/dotnet/corefx/issues/3054
  112. // dnx loader
  113. // https://github.com/aspnet/dnx/tree/dev/src/Microsoft.Dnx.Loader
  114. bool result = netHost_->CreateDelegate(
  115. "AtomicNETBootstrap",
  116. "Atomic.Bootstrap.AtomicLoadContext",
  117. "Startup",
  118. (void**) &startup);
  119. if (!result)
  120. return false;
  121. startup(netHost_->GetCoreCLRAssemblyLoadPaths().CString());
  122. return true;
  123. }
  124. void NETCore::HandleUpdate(StringHash eventType, VariantMap& eventData)
  125. {
  126. using namespace Update;
  127. //GetSubsystem<NETManaged>()->NETUpdate(eventData[P_TIMESTEP].GetFloat());
  128. }
  129. }