| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- #include <ThirdParty/SDL/include/SDL.h>
- #include <Atomic/Core/CoreEvents.h>
- #include <Atomic/IO/FileSystem.h>
- #include <Atomic/IO/Log.h>
- #include <Atomic/Core/StringUtils.h>
- #include "CSEventHelper.h"
- #include "CSComponent.h"
- #include "NETCore.h"
- #include "NETManaged.h"
- #include "NETAssemblyFile.h"
- #ifdef ATOMIC_PLATFORM_WINDOWS
- #include "Platforms/Windows/NETHostWindows.h"
- #else
- #endif
- namespace Atomic
- {
- /*
- 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
- export PAL_DBG_CHANNELS="+all.all"
- */
- #ifdef ATOMIC_PLATFORM_OSX
- static const char * const sCoreClrDll = "libcoreclr.dylib";
- #elif ATOMIC_PLATFORM_WINDOWS
- static const char * const sCoreClrDll = "coreclr.dll";
- #else
- static const char * const sCoreClrDll = "libcoreclr.so";
- #endif
- // Prototype of the coreclr_initialize function from the libcoreclr.so
- typedef int (*InitializeCoreCLRFunction)(
- const char* exePath,
- const char* appDomainFriendlyName,
- int propertyCount,
- const char** propertyKeys,
- const char** propertyValues,
- void** hostHandle,
- unsigned int* domainId);
- // Prototype of the coreclr_shutdown function from the libcoreclr.so
- typedef int (*ShutdownCoreCLRFunction)(
- void* hostHandle,
- unsigned int domainId);
- // Prototype of the coreclr_execute_assembly function from the libcoreclr.so
- typedef int (*ExecuteAssemblyFunction)(
- void* hostHandle,
- unsigned int domainId,
- int argc,
- const char** argv,
- const char* managedAssemblyPath,
- unsigned int* exitCode);
- typedef int (*CreateDelegateFunction)(
- void* hostHandle,
- unsigned int domainId,
- const char* entryPointAssemblyName,
- const char* entryPointTypeName,
- const char* entryPointMethodName,
- void** delegate);
- static InitializeCoreCLRFunction sInitializeCoreCLR = 0;
- static ExecuteAssemblyFunction sExecuteAssembly = 0;
- static CreateDelegateFunction sCreateDelegate = 0;
- static ShutdownCoreCLRFunction sShutdownCoreCLR = 0;
- /// Register NETCore library objects.
- void ATOMIC_API RegisterNETCoreLibrary(Context* context);
- WeakPtr<Context> NETCore::csContext_;
- WeakPtr<NETCore> NETCore::instance_;
- NETCore::NETCore(Context* context) :
- Object(context),
- coreCLRDLLHandle_(0),
- hostHandle_(0),
- domainId_(0)
- {
- RegisterNETCoreLibrary(context_);
- assert(!instance_);
- instance_ = this;
- csContext_ = context;
- }
- NETCore::~NETCore()
- {
- context_->RemoveGlobalEventListener(context_->GetSubsystem<CSEventDispatcher>());
- context_->RemoveSubsystem(CSEventDispatcher::GetTypeStatic());
- context_->RemoveSubsystem(NETManaged::GetTypeStatic());
- instance_ = NULL;
- }
- void NETCore::GenerateTPAList(String& tpaList)
- {
- tpaList = String::EMPTY;
- FileSystem* fs = GetSubsystem<FileSystem>();
- Vector<String> results;
- fs->ScanDir(results, coreCLRFilesAbsPath_, "*.dll", SCAN_FILES, true);
- Vector<String> trustedAssemblies;
- for (unsigned i = 0; i < results.Size(); i++)
- {
- const String& assembly = results[i];
- // TODO: apply filtering if necessary
- trustedAssemblies.Push(coreCLRFilesAbsPath_ + assembly);
- }
- // trustedAssemblies.Push(coreCLRFilesAbsPath_ + "HelloWorld.exe");
- tpaList.Join(trustedAssemblies, ":");
- // LOGINFOF("NetCore:: TPALIST - %s", tpaList.CString());
- }
- void NETCore::Shutdown()
- {
- RefCounted::SetRefCountedDeletedFunction(0);
- if (sShutdownCoreCLR && hostHandle_)
- {
- sShutdownCoreCLR(hostHandle_, domainId_);
- }
- if (coreCLRDLLHandle_)
- {
- SDL_UnloadObject(coreCLRDLLHandle_);
- }
- coreCLRDLLHandle_ = 0;
- hostHandle_ = 0;
- domainId_ = 0;
- sInitializeCoreCLR = 0;
- sExecuteAssembly = 0;
- sCreateDelegate = 0;
- sShutdownCoreCLR = 0;
- }
- bool NETCore::InitCoreCLRDLL(String& errorMsg)
- {
- String coreClrDllPath = AddTrailingSlash(coreCLRFilesAbsPath_) + sCoreClrDll;
- coreCLRDLLHandle_ = SDL_LoadObject(coreClrDllPath.CString());
- if (coreCLRDLLHandle_ == NULL)
- {
- errorMsg = ToString("NETCore: Unable to load %s with error %s",
- coreClrDllPath.CString(),
- SDL_GetError());
- return false;
- }
- sInitializeCoreCLR = (InitializeCoreCLRFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_initialize");
- if (!sInitializeCoreCLR)
- {
- errorMsg = ToString("NETCore: Unable to get coreclr_initialize entry point in %s", coreClrDllPath.CString());
- return false;
- }
- sShutdownCoreCLR = (ShutdownCoreCLRFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_shutdown");
- if (!sShutdownCoreCLR)
- {
- errorMsg = ToString("NETCore: Unable to get coreclr_shutdown entry point in %s", coreClrDllPath.CString());
- return false;
- }
- sCreateDelegate = (CreateDelegateFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_create_delegate");
- if (!sCreateDelegate)
- {
- errorMsg = ToString("NETCore: Unable to get coreclr_create_delegate entry point in %s", coreClrDllPath.CString());
- return false;
- }
- sExecuteAssembly = (ExecuteAssemblyFunction) SDL_LoadFunction(coreCLRDLLHandle_, "coreclr_execute_assembly");
- if (!sExecuteAssembly)
- {
- errorMsg = ToString("NETCore: Unable to get coreclr_execute_assembly entry point in %s", coreClrDllPath.CString());
- return false;
- }
- return true;
- }
- extern "C"
- {
- // http://ybeernet.blogspot.com/2011/03/techniques-of-calling-unmanaged-code.html
- // pinvoke is faster than [UnmanagedFunctionPointer] :/
- // [SuppressUnmanagedCodeSecurity] <--- add this attribute, in any event
- #ifdef ATOMIC_PLATFORM_WINDOWS
- #pragma warning(disable: 4244) // possible loss of data
- #define ATOMIC_EXPORT_API __declspec(dllexport)
- #else
- #define ATOMIC_EXPORT_API
- #endif
- ATOMIC_EXPORT_API ClassID csb_Atomic_RefCounted_GetClassID(RefCounted* refCounted)
- {
- if (!refCounted)
- return 0;
- return refCounted->GetClassID();
- }
- ATOMIC_EXPORT_API RefCounted* csb_AtomicEngine_GetSubsystem(const char* name)
- {
- return NETCore::GetContext()->GetSubsystem(name);
- }
- ATOMIC_EXPORT_API void csb_AtomicEngine_ReleaseRef(RefCounted* ref)
- {
- if (!ref)
- return;
- ref->ReleaseRef();
- }
- }
- bool NETCore::CreateDelegate(const String& assemblyName, const String& qualifiedClassName, const String& methodName, void** funcOut)
- {
- return netHost_->CreateDelegate(assemblyName, qualifiedClassName, methodName, funcOut);
- /*
- if (!sCreateDelegate)
- return false;
- *funcOut = 0;
- // TODO: wrap in SharedPtr to control delegate lifetime?
- int st = sCreateDelegate(hostHandle_,
- domainId_,
- assemblyName.CString(),
- qualifiedClassName.CString(),
- methodName.CString(),
- funcOut);
- // ensure ptr isn't uninitialized
- if (st < 0)
- *funcOut = 0;
- return st >= 0;
- */
- }
- bool NETCore::Initialize(const String &coreCLRFilesAbsPath, String& errorMsg)
- {
- coreCLRFilesAbsPath_ = AddTrailingSlash(coreCLRFilesAbsPath);
- #ifdef ATOMIC_PLATFORM_WINDOWS
- netHost_ = new NETHostWindows(context_);
- #else
- #endif
- netHost_->Initialize(coreCLRFilesAbsPath_);
- SharedPtr<NETManaged> managed(new NETManaged(context_));
- context_->RegisterSubsystem(managed);
- SharedPtr<CSEventDispatcher> dispatcher(new CSEventDispatcher(context_));
- context_->RegisterSubsystem(dispatcher);
- context_->AddGlobalEventListener(dispatcher);
- if (!context_->GetEditorContext())
- {
- SubscribeToEvent(E_UPDATE, HANDLER(NETCore, HandleUpdate));
- }
- managed->Initialize();
- #ifdef disabled
- if (!InitCoreCLRDLL(errorMsg))
- {
- Shutdown();
- return false;
- }
- // Allowed property names:
- // APPBASE
- // - The base path of the application from which the exe and other assemblies will be loaded
- //
- // TRUSTED_PLATFORM_ASSEMBLIES
- // - The list of complete paths to each of the fully trusted assemblies
- //
- // APP_PATHS
- // - The list of paths which will be probed by the assembly loader
- //
- // APP_NI_PATHS
- // - The list of additional paths that the assembly loader will probe for ngen images
- //
- // NATIVE_DLL_SEARCH_DIRECTORIES
- // - The list of paths that will be probed for native DLLs called by PInvoke
- //
- const char *propertyKeys[] = {
- "TRUSTED_PLATFORM_ASSEMBLIES",
- "APP_PATHS",
- "APP_NI_PATHS",
- "NATIVE_DLL_SEARCH_DIRECTORIES",
- "AppDomainCompatSwitch"
- };
- String tpaList;
- GenerateTPAList(tpaList);
- String appPath = coreCLRFilesAbsPath_;// "/Users/josh/Desktop/";
- Vector<String> nativeSearch;
- nativeSearch.Push(coreCLRFilesAbsPath_);
- //nativeSearch.Push("/Users/josh/Dev/atomic/AtomicGameEngine-build/Source/AtomicNET/NETNative");
- //nativeSearch.Push("/Users/josh/Dev/atomic/AtomicGameEngine-build/Source/AtomicEditor/AtomicEditor.app/Contents/MacOS");
- String nativeDllSearchDirs;
- nativeDllSearchDirs.Join(nativeSearch, ":");
- const char *propertyValues[] = {
- // TRUSTED_PLATFORM_ASSEMBLIES
- tpaList.CString(),
- // APP_PATHS
- appPath.CString(),
- // APP_NI_PATHS
- appPath.CString(),
- // NATIVE_DLL_SEARCH_DIRECTORIES
- nativeDllSearchDirs.CString(),
- // AppDomainCompatSwitch
- "UseLatestBehaviorWhenTFMNotSpecified"
- };
- int st = sInitializeCoreCLR(
- "AtomicEditor.exe",
- "NETCore",
- sizeof(propertyKeys) / sizeof(propertyKeys[0]),
- propertyKeys,
- propertyValues,
- &hostHandle_,
- &domainId_);
- if (st < 0)
- {
- Shutdown();
- errorMsg = ToString("NETCore: coreclr_initialize failed - status: 0x%08x\n", st);
- return false;
- }
- typedef void (*StartupFunction)();
- StartupFunction startup;
- // The coreclr binding model will become locked upon loading the first assembly that is not on the TPA list, or
- // upon initializing the default context for the first time. For this test, test assemblies are located alongside
- // corerun, and hence will be on the TPA list. So, we should be able to set the default context once successfully,
- // and fail on the second try.
- // AssemblyLoadContext
- // https://github.com/dotnet/corefx/issues/3054
- // dnx loader
- // https://github.com/aspnet/dnx/tree/dev/src/Microsoft.Dnx.Loader
- st = sCreateDelegate(hostHandle_,
- domainId_,
- "AtomicNETBootstrap",
- "Atomic.Bootstrap.AtomicLoadContext",
- "Startup",
- (void**) &startup);
- if (st >= 0)
- {
- startup();
- }
- st = sCreateDelegate(hostHandle_,
- domainId_,
- "AtomicNETEngine",
- "AtomicEngine.Atomic",
- "Initialize",
- (void**) &startup);
- if (st >= 0)
- {
- startup();
- }
- /*
- RefCountedDeletedFunction rcdFunction;
- st = sCreateDelegate(hostHandle_,
- domainId_,
- "AtomicNETEngine",
- "AtomicEngine.NativeCore",
- "RefCountedDeleted",
- (void**) &rcdFunction);
- if (st >= 0)
- {
- RefCounted::SetRefCountedDeletedFunction(rcdFunction);
- }
- NETUpdateFunctionPtr updateFunction;
- st = sCreateDelegate(hostHandle_,
- domainId_,
- "AtomicNETEngine",
- "AtomicEngine.NativeCore",
- "NETUpdate",
- (void**) &updateFunction);
- if (st >= 0)
- {
- GetSubsystem<NETManaged>()->SetNETUpdate(updateFunction);
- }
- typedef void (*InpectAssemblyFuctionPtr)(const char* path);
- InpectAssemblyFuctionPtr inspectAssembly;
- st = sCreateDelegate(hostHandle_,
- domainId_,
- "AtomicEditor",
- "AtomicEditor.AtomicEditor",
- "InspectAssembly",
- (void**) &inspectAssembly);
- if (st >= 0)
- {
- //inspectAssembly("/Users/josh/Desktop/AtomicNETTest.dll");
- }
- */
- /*
- unsigned int exitCode;
- st = sExecuteAssembly(
- hostHandle_,
- domainId_,
- 0,
- 0,
- "/Users/josh/Desktop/OSX.x64.Debug/HelloWorld.exe",
- (unsigned int*)&exitCode);
- */
- #endif
- return true;
- }
- void NETCore::HandleUpdate(StringHash eventType, VariantMap& eventData)
- {
- using namespace Update;
- GetSubsystem<NETManaged>()->NETUpdate(eventData[P_TIMESTEP].GetFloat());
- }
- void RegisterNETCoreLibrary(Context* context)
- {
- NETAssemblyFile::RegisterObject(context);
- CSComponent::RegisterObject(context);
- }
- }
|