NETCore.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #ifdef ATOMIC_PLATFORM_WINDOWS
  40. #pragma warning(disable: 4244) // possible loss of data
  41. #define ATOMIC_EXPORT_API __declspec(dllexport)
  42. #else
  43. #define ATOMIC_EXPORT_API
  44. #endif
  45. ATOMIC_EXPORT_API ClassID csb_Atomic_RefCounted_GetClassID(RefCounted* refCounted)
  46. {
  47. if (!refCounted)
  48. return 0;
  49. return refCounted->GetClassID();
  50. }
  51. ATOMIC_EXPORT_API RefCounted* csb_AtomicEngine_GetSubsystem(const char* name)
  52. {
  53. return NETCore::GetContext()->GetSubsystem(name);
  54. }
  55. ATOMIC_EXPORT_API void csb_AtomicEngine_ReleaseRef(RefCounted* ref)
  56. {
  57. if (!ref)
  58. return;
  59. ref->ReleaseRef();
  60. }
  61. }
  62. void NETCore::WaitForDebuggerConnect()
  63. {
  64. netHost_->WaitForDebuggerConnect();
  65. }
  66. void NETCore::AddAssemblyLoadPath(const String& assemblyPath)
  67. {
  68. typedef void (*AddAssemblyLoadPathFunction)(const char* assemblyPath);
  69. AddAssemblyLoadPathFunction addAssemblyLoadPath;
  70. bool result = CreateDelegate(
  71. "AtomicNETBootstrap",
  72. "Atomic.Bootstrap.AtomicLoadContext",
  73. "AddAssemblyLoadPath",
  74. (void**) &addAssemblyLoadPath);
  75. if (result)
  76. {
  77. addAssemblyLoadPath(assemblyPath.CString());
  78. }
  79. }
  80. int NETCore::ExecAssembly(const String& assemblyName, const Vector<String>& args)
  81. {
  82. return netHost_->ExecAssembly(assemblyName, args);
  83. }
  84. bool NETCore::CreateDelegate(const String& assemblyName, const String& qualifiedClassName, const String& methodName, void** funcOut)
  85. {
  86. return netHost_->CreateDelegate(assemblyName, qualifiedClassName, methodName, funcOut);
  87. }
  88. bool NETCore::Initialize(String& errorMsg)
  89. {
  90. #ifdef ATOMIC_PLATFORM_WINDOWS
  91. netHost_ = new NETHostWindows(context_);
  92. #else
  93. netHost_ = new NETHostUnix(context_);
  94. #endif
  95. if (!netHost_->Initialize())
  96. {
  97. errorMsg = "NETHost Failed to Initialize";
  98. return false;
  99. }
  100. // MOVE THIS!
  101. typedef void (*StartupFunction)(const char* assemblyLoadPaths);
  102. StartupFunction startup;
  103. // The coreclr binding model will become locked upon loading the first assembly that is not on the TPA list, or
  104. // upon initializing the default context for the first time. For this test, test assemblies are located alongside
  105. // corerun, and hence will be on the TPA list. So, we should be able to set the default context once successfully,
  106. // and fail on the second try.
  107. // AssemblyLoadContext
  108. // https://github.com/dotnet/corefx/issues/3054
  109. // dnx loader
  110. // https://github.com/aspnet/dnx/tree/dev/src/Microsoft.Dnx.Loader
  111. bool result = netHost_->CreateDelegate(
  112. "AtomicNETBootstrap",
  113. "Atomic.Bootstrap.AtomicLoadContext",
  114. "Startup",
  115. (void**) &startup);
  116. if (!result)
  117. return false;
  118. startup(netHost_->GetCoreCLRAssemblyLoadPaths().CString());
  119. return true;
  120. }
  121. void NETCore::HandleUpdate(StringHash eventType, VariantMap& eventData)
  122. {
  123. using namespace Update;
  124. //GetSubsystem<NETManaged>()->NETUpdate(eventData[P_TIMESTEP].GetFloat());
  125. }
  126. }